Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* List object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
| 4 | |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 5 | #ifdef STDC_HEADERS |
| 6 | #include <stddef.h> |
| 7 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | #include <sys/types.h> /* For size_t */ |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 9 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | |
Tim Peters | 8d9eb10 | 2004-07-31 02:24:20 +0000 | [diff] [blame] | 11 | /* Ensure ob_item has room for at least newsize elements, and set |
| 12 | * ob_size to newsize. If newsize > ob_size on entry, the content |
| 13 | * of the new slots at exit is undefined heap trash; it's the caller's |
| 14 | * responsiblity to overwrite them with sane values. |
| 15 | * The number of allocated elements may grow, shrink, or stay the same. |
| 16 | * Failure is impossible if newsize <= self.allocated on entry, although |
| 17 | * that partly relies on an assumption that the system realloc() never |
| 18 | * fails when passed a number of bytes <= the number of bytes last |
| 19 | * allocated (the C standard doesn't guarantee this, but it's hard to |
| 20 | * imagine a realloc implementation where it wouldn't be true). |
| 21 | * Note that self->ob_item may change, and even if newsize is less |
| 22 | * than ob_size on entry. |
| 23 | */ |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 24 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 25 | list_resize(PyListObject *self, Py_ssize_t newsize) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 26 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | PyObject **items; |
| 28 | size_t new_allocated; |
| 29 | Py_ssize_t allocated = self->allocated; |
Tim Peters | 65b8b84 | 2001-05-26 05:28:40 +0000 | [diff] [blame] | 30 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | /* Bypass realloc() when a previous overallocation is large enough |
| 32 | to accommodate the newsize. If the newsize falls lower than half |
| 33 | the allocated size, then proceed with the realloc() to shrink the list. |
| 34 | */ |
| 35 | if (allocated >= newsize && newsize >= (allocated >> 1)) { |
| 36 | assert(self->ob_item != NULL || newsize == 0); |
| 37 | Py_SIZE(self) = newsize; |
| 38 | return 0; |
| 39 | } |
Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 40 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | /* This over-allocates proportional to the list size, making room |
| 42 | * for additional growth. The over-allocation is mild, but is |
| 43 | * enough to give linear-time amortized behavior over a long |
| 44 | * sequence of appends() in the presence of a poorly-performing |
| 45 | * system realloc(). |
| 46 | * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ... |
| 47 | */ |
| 48 | new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | /* check for integer overflow */ |
| 51 | if (new_allocated > PY_SIZE_MAX - newsize) { |
| 52 | PyErr_NoMemory(); |
| 53 | return -1; |
| 54 | } else { |
| 55 | new_allocated += newsize; |
| 56 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | if (newsize == 0) |
| 59 | new_allocated = 0; |
| 60 | items = self->ob_item; |
| 61 | if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *))) |
| 62 | PyMem_RESIZE(items, PyObject *, new_allocated); |
| 63 | else |
| 64 | items = NULL; |
| 65 | if (items == NULL) { |
| 66 | PyErr_NoMemory(); |
| 67 | return -1; |
| 68 | } |
| 69 | self->ob_item = items; |
| 70 | Py_SIZE(self) = newsize; |
| 71 | self->allocated = new_allocated; |
| 72 | return 0; |
Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 73 | } |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 74 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 75 | /* Debug statistic to compare allocations with reuse through the free list */ |
| 76 | #undef SHOW_ALLOC_COUNT |
| 77 | #ifdef SHOW_ALLOC_COUNT |
| 78 | static size_t count_alloc = 0; |
| 79 | static size_t count_reuse = 0; |
| 80 | |
| 81 | static void |
| 82 | show_alloc(void) |
| 83 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n", |
| 85 | count_alloc); |
| 86 | fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T |
| 87 | "d\n", count_reuse); |
| 88 | fprintf(stderr, "%.2f%% reuse rate\n\n", |
| 89 | (100.0*count_reuse/(count_alloc+count_reuse))); |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 90 | } |
| 91 | #endif |
| 92 | |
Raymond Hettinger | 0468e41 | 2004-05-05 05:37:53 +0000 | [diff] [blame] | 93 | /* Empty list reuse scheme to save calls to malloc and free */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 94 | #ifndef PyList_MAXFREELIST |
| 95 | #define PyList_MAXFREELIST 80 |
| 96 | #endif |
| 97 | static PyListObject *free_list[PyList_MAXFREELIST]; |
| 98 | static int numfree = 0; |
Raymond Hettinger | 0468e41 | 2004-05-05 05:37:53 +0000 | [diff] [blame] | 99 | |
Raymond Hettinger | fb09f0e | 2004-10-07 03:58:07 +0000 | [diff] [blame] | 100 | void |
| 101 | PyList_Fini(void) |
| 102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | PyListObject *op; |
Raymond Hettinger | fb09f0e | 2004-10-07 03:58:07 +0000 | [diff] [blame] | 104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | while (numfree) { |
| 106 | op = free_list[--numfree]; |
| 107 | assert(PyList_CheckExact(op)); |
| 108 | PyObject_GC_Del(op); |
| 109 | } |
Raymond Hettinger | fb09f0e | 2004-10-07 03:58:07 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 113 | PyList_New(Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | PyListObject *op; |
| 116 | size_t nbytes; |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 117 | #ifdef SHOW_ALLOC_COUNT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | static int initialized = 0; |
| 119 | if (!initialized) { |
| 120 | Py_AtExit(show_alloc); |
| 121 | initialized = 1; |
| 122 | } |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 123 | #endif |
Tim Peters | 3986d4e | 2004-07-29 02:28:42 +0000 | [diff] [blame] | 124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | if (size < 0) { |
| 126 | PyErr_BadInternalCall(); |
| 127 | return NULL; |
| 128 | } |
| 129 | /* Check for overflow without an actual overflow, |
| 130 | * which can cause compiler to optimise out */ |
| 131 | if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *)) |
| 132 | return PyErr_NoMemory(); |
| 133 | nbytes = size * sizeof(PyObject *); |
| 134 | if (numfree) { |
| 135 | numfree--; |
| 136 | op = free_list[numfree]; |
| 137 | _Py_NewReference((PyObject *)op); |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 138 | #ifdef SHOW_ALLOC_COUNT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | count_reuse++; |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 140 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | } else { |
| 142 | op = PyObject_GC_New(PyListObject, &PyList_Type); |
| 143 | if (op == NULL) |
| 144 | return NULL; |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 145 | #ifdef SHOW_ALLOC_COUNT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | count_alloc++; |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 147 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | } |
| 149 | if (size <= 0) |
| 150 | op->ob_item = NULL; |
| 151 | else { |
| 152 | op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); |
| 153 | if (op->ob_item == NULL) { |
| 154 | Py_DECREF(op); |
| 155 | return PyErr_NoMemory(); |
| 156 | } |
| 157 | memset(op->ob_item, 0, nbytes); |
| 158 | } |
| 159 | Py_SIZE(op) = size; |
| 160 | op->allocated = size; |
| 161 | _PyObject_GC_TRACK(op); |
| 162 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 165 | Py_ssize_t |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 166 | PyList_Size(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | if (!PyList_Check(op)) { |
| 169 | PyErr_BadInternalCall(); |
| 170 | return -1; |
| 171 | } |
| 172 | else |
| 173 | return Py_SIZE(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Raymond Hettinger | fdfe618 | 2004-05-05 06:28:16 +0000 | [diff] [blame] | 176 | static PyObject *indexerr = NULL; |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 177 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 178 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 179 | PyList_GetItem(PyObject *op, Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | if (!PyList_Check(op)) { |
| 182 | PyErr_BadInternalCall(); |
| 183 | return NULL; |
| 184 | } |
| 185 | if (i < 0 || i >= Py_SIZE(op)) { |
| 186 | if (indexerr == NULL) { |
| 187 | indexerr = PyUnicode_FromString( |
| 188 | "list index out of range"); |
| 189 | if (indexerr == NULL) |
| 190 | return NULL; |
| 191 | } |
| 192 | PyErr_SetObject(PyExc_IndexError, indexerr); |
| 193 | return NULL; |
| 194 | } |
| 195 | return ((PyListObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 199 | PyList_SetItem(register PyObject *op, register Py_ssize_t i, |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 200 | register PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | register PyObject *olditem; |
| 203 | register PyObject **p; |
| 204 | if (!PyList_Check(op)) { |
| 205 | Py_XDECREF(newitem); |
| 206 | PyErr_BadInternalCall(); |
| 207 | return -1; |
| 208 | } |
| 209 | if (i < 0 || i >= Py_SIZE(op)) { |
| 210 | Py_XDECREF(newitem); |
| 211 | PyErr_SetString(PyExc_IndexError, |
| 212 | "list assignment index out of range"); |
| 213 | return -1; |
| 214 | } |
| 215 | p = ((PyListObject *)op) -> ob_item + i; |
| 216 | olditem = *p; |
| 217 | *p = newitem; |
| 218 | Py_XDECREF(olditem); |
| 219 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 223 | ins1(PyListObject *self, Py_ssize_t where, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | Py_ssize_t i, n = Py_SIZE(self); |
| 226 | PyObject **items; |
| 227 | if (v == NULL) { |
| 228 | PyErr_BadInternalCall(); |
| 229 | return -1; |
| 230 | } |
| 231 | if (n == PY_SSIZE_T_MAX) { |
| 232 | PyErr_SetString(PyExc_OverflowError, |
| 233 | "cannot add more objects to list"); |
| 234 | return -1; |
| 235 | } |
Tim Peters | b38e2b6 | 2004-07-29 02:29:26 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | if (list_resize(self, n+1) == -1) |
| 238 | return -1; |
Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | if (where < 0) { |
| 241 | where += n; |
| 242 | if (where < 0) |
| 243 | where = 0; |
| 244 | } |
| 245 | if (where > n) |
| 246 | where = n; |
| 247 | items = self->ob_item; |
| 248 | for (i = n; --i >= where; ) |
| 249 | items[i+1] = items[i]; |
| 250 | Py_INCREF(v); |
| 251 | items[where] = v; |
| 252 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 256 | PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | if (!PyList_Check(op)) { |
| 259 | PyErr_BadInternalCall(); |
| 260 | return -1; |
| 261 | } |
| 262 | return ins1((PyListObject *)op, where, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 265 | static int |
| 266 | app1(PyListObject *self, PyObject *v) |
| 267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | Py_ssize_t n = PyList_GET_SIZE(self); |
Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | assert (v != NULL); |
| 271 | if (n == PY_SSIZE_T_MAX) { |
| 272 | PyErr_SetString(PyExc_OverflowError, |
| 273 | "cannot add more objects to list"); |
| 274 | return -1; |
| 275 | } |
Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | if (list_resize(self, n+1) == -1) |
| 278 | return -1; |
Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | Py_INCREF(v); |
| 281 | PyList_SET_ITEM(self, n, v); |
| 282 | return 0; |
Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 286 | PyList_Append(PyObject *op, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | if (PyList_Check(op) && (newitem != NULL)) |
| 289 | return app1((PyListObject *)op, newitem); |
| 290 | PyErr_BadInternalCall(); |
| 291 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* Methods */ |
| 295 | |
| 296 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 297 | list_dealloc(PyListObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | Py_ssize_t i; |
| 300 | PyObject_GC_UnTrack(op); |
| 301 | Py_TRASHCAN_SAFE_BEGIN(op) |
| 302 | if (op->ob_item != NULL) { |
| 303 | /* Do it backwards, for Christian Tismer. |
| 304 | There's a simple test case where somehow this reduces |
| 305 | thrashing when a *very* large list is created and |
| 306 | immediately deleted. */ |
| 307 | i = Py_SIZE(op); |
| 308 | while (--i >= 0) { |
| 309 | Py_XDECREF(op->ob_item[i]); |
| 310 | } |
| 311 | PyMem_FREE(op->ob_item); |
| 312 | } |
| 313 | if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op)) |
| 314 | free_list[numfree++] = op; |
| 315 | else |
| 316 | Py_TYPE(op)->tp_free((PyObject *)op); |
| 317 | Py_TRASHCAN_SAFE_END(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 321 | list_repr(PyListObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | Py_ssize_t i; |
| 324 | PyObject *s, *temp; |
| 325 | PyObject *pieces = NULL, *result = NULL; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | i = Py_ReprEnter((PyObject*)v); |
| 328 | if (i != 0) { |
| 329 | return i > 0 ? PyUnicode_FromString("[...]") : NULL; |
| 330 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | if (Py_SIZE(v) == 0) { |
| 333 | result = PyUnicode_FromString("[]"); |
| 334 | goto Done; |
| 335 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | pieces = PyList_New(0); |
| 338 | if (pieces == NULL) |
| 339 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | /* Do repr() on each element. Note that this may mutate the list, |
| 342 | so must refetch the list size on each iteration. */ |
| 343 | for (i = 0; i < Py_SIZE(v); ++i) { |
| 344 | int status; |
| 345 | if (Py_EnterRecursiveCall(" while getting the repr of a list")) |
| 346 | goto Done; |
| 347 | s = PyObject_Repr(v->ob_item[i]); |
| 348 | Py_LeaveRecursiveCall(); |
| 349 | if (s == NULL) |
| 350 | goto Done; |
| 351 | status = PyList_Append(pieces, s); |
| 352 | Py_DECREF(s); /* append created a new ref */ |
| 353 | if (status < 0) |
| 354 | goto Done; |
| 355 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | /* Add "[]" decorations to the first and last items. */ |
| 358 | assert(PyList_GET_SIZE(pieces) > 0); |
| 359 | s = PyUnicode_FromString("["); |
| 360 | if (s == NULL) |
| 361 | goto Done; |
| 362 | temp = PyList_GET_ITEM(pieces, 0); |
| 363 | PyUnicode_AppendAndDel(&s, temp); |
| 364 | PyList_SET_ITEM(pieces, 0, s); |
| 365 | if (s == NULL) |
| 366 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | s = PyUnicode_FromString("]"); |
| 369 | if (s == NULL) |
| 370 | goto Done; |
| 371 | temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 372 | PyUnicode_AppendAndDel(&temp, s); |
| 373 | PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 374 | if (temp == NULL) |
| 375 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | /* Paste them all together with ", " between. */ |
| 378 | s = PyUnicode_FromString(", "); |
| 379 | if (s == NULL) |
| 380 | goto Done; |
| 381 | result = PyUnicode_Join(s, pieces); |
| 382 | Py_DECREF(s); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 383 | |
| 384 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | Py_XDECREF(pieces); |
| 386 | Py_ReprLeave((PyObject *)v); |
| 387 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 390 | static Py_ssize_t |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 391 | list_length(PyListObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | return Py_SIZE(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 396 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 397 | list_contains(PyListObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | Py_ssize_t i; |
| 400 | int cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) |
| 403 | cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i), |
| 404 | Py_EQ); |
| 405 | return cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 408 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 409 | list_item(PyListObject *a, Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | if (i < 0 || i >= Py_SIZE(a)) { |
| 412 | if (indexerr == NULL) { |
| 413 | indexerr = PyUnicode_FromString( |
| 414 | "list index out of range"); |
| 415 | if (indexerr == NULL) |
| 416 | return NULL; |
| 417 | } |
| 418 | PyErr_SetObject(PyExc_IndexError, indexerr); |
| 419 | return NULL; |
| 420 | } |
| 421 | Py_INCREF(a->ob_item[i]); |
| 422 | return a->ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 425 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 426 | list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 427 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | PyListObject *np; |
| 429 | PyObject **src, **dest; |
| 430 | Py_ssize_t i, len; |
| 431 | if (ilow < 0) |
| 432 | ilow = 0; |
| 433 | else if (ilow > Py_SIZE(a)) |
| 434 | ilow = Py_SIZE(a); |
| 435 | if (ihigh < ilow) |
| 436 | ihigh = ilow; |
| 437 | else if (ihigh > Py_SIZE(a)) |
| 438 | ihigh = Py_SIZE(a); |
| 439 | len = ihigh - ilow; |
| 440 | np = (PyListObject *) PyList_New(len); |
| 441 | if (np == NULL) |
| 442 | return NULL; |
Raymond Hettinger | 99842b6 | 2004-03-08 05:56:15 +0000 | [diff] [blame] | 443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | src = a->ob_item + ilow; |
| 445 | dest = np->ob_item; |
| 446 | for (i = 0; i < len; i++) { |
| 447 | PyObject *v = src[i]; |
| 448 | Py_INCREF(v); |
| 449 | dest[i] = v; |
| 450 | } |
| 451 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 454 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 455 | PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | if (!PyList_Check(a)) { |
| 458 | PyErr_BadInternalCall(); |
| 459 | return NULL; |
| 460 | } |
| 461 | return list_slice((PyListObject *)a, ilow, ihigh); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 464 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 465 | list_concat(PyListObject *a, PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | Py_ssize_t size; |
| 468 | Py_ssize_t i; |
| 469 | PyObject **src, **dest; |
| 470 | PyListObject *np; |
| 471 | if (!PyList_Check(bb)) { |
| 472 | PyErr_Format(PyExc_TypeError, |
| 473 | "can only concatenate list (not \"%.200s\") to list", |
| 474 | bb->ob_type->tp_name); |
| 475 | return NULL; |
| 476 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 477 | #define b ((PyListObject *)bb) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | size = Py_SIZE(a) + Py_SIZE(b); |
| 479 | if (size < 0) |
| 480 | return PyErr_NoMemory(); |
| 481 | np = (PyListObject *) PyList_New(size); |
| 482 | if (np == NULL) { |
| 483 | return NULL; |
| 484 | } |
| 485 | src = a->ob_item; |
| 486 | dest = np->ob_item; |
| 487 | for (i = 0; i < Py_SIZE(a); i++) { |
| 488 | PyObject *v = src[i]; |
| 489 | Py_INCREF(v); |
| 490 | dest[i] = v; |
| 491 | } |
| 492 | src = b->ob_item; |
| 493 | dest = np->ob_item + Py_SIZE(a); |
| 494 | for (i = 0; i < Py_SIZE(b); i++) { |
| 495 | PyObject *v = src[i]; |
| 496 | Py_INCREF(v); |
| 497 | dest[i] = v; |
| 498 | } |
| 499 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | #undef b |
| 501 | } |
| 502 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 503 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 504 | list_repeat(PyListObject *a, Py_ssize_t n) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | Py_ssize_t i, j; |
| 507 | Py_ssize_t size; |
| 508 | PyListObject *np; |
| 509 | PyObject **p, **items; |
| 510 | PyObject *elem; |
| 511 | if (n < 0) |
| 512 | n = 0; |
| 513 | size = Py_SIZE(a) * n; |
| 514 | if (n && size/n != Py_SIZE(a)) |
| 515 | return PyErr_NoMemory(); |
| 516 | if (size == 0) |
| 517 | return PyList_New(0); |
| 518 | np = (PyListObject *) PyList_New(size); |
| 519 | if (np == NULL) |
| 520 | return NULL; |
Raymond Hettinger | 6624e68 | 2003-05-21 05:58:46 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | items = np->ob_item; |
| 523 | if (Py_SIZE(a) == 1) { |
| 524 | elem = a->ob_item[0]; |
| 525 | for (i = 0; i < n; i++) { |
| 526 | items[i] = elem; |
| 527 | Py_INCREF(elem); |
| 528 | } |
| 529 | return (PyObject *) np; |
| 530 | } |
| 531 | p = np->ob_item; |
| 532 | items = a->ob_item; |
| 533 | for (i = 0; i < n; i++) { |
| 534 | for (j = 0; j < Py_SIZE(a); j++) { |
| 535 | *p = items[j]; |
| 536 | Py_INCREF(*p); |
| 537 | p++; |
| 538 | } |
| 539 | } |
| 540 | return (PyObject *) np; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 543 | static int |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 544 | list_clear(PyListObject *a) |
| 545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | Py_ssize_t i; |
| 547 | PyObject **item = a->ob_item; |
| 548 | if (item != NULL) { |
| 549 | /* Because XDECREF can recursively invoke operations on |
| 550 | this list, we make it empty first. */ |
| 551 | i = Py_SIZE(a); |
| 552 | Py_SIZE(a) = 0; |
| 553 | a->ob_item = NULL; |
| 554 | a->allocated = 0; |
| 555 | while (--i >= 0) { |
| 556 | Py_XDECREF(item[i]); |
| 557 | } |
| 558 | PyMem_FREE(item); |
| 559 | } |
| 560 | /* Never fails; the return value can be ignored. |
| 561 | Note that there is no guarantee that the list is actually empty |
| 562 | at this point, because XDECREF may have populated it again! */ |
| 563 | return 0; |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Tim Peters | 8fc4a91 | 2004-07-31 21:53:19 +0000 | [diff] [blame] | 566 | /* a[ilow:ihigh] = v if v != NULL. |
| 567 | * del a[ilow:ihigh] if v == NULL. |
| 568 | * |
| 569 | * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's |
| 570 | * guaranteed the call cannot fail. |
| 571 | */ |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 572 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 573 | list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | /* Because [X]DECREF can recursively invoke list operations on |
| 576 | this list, we must postpone all [X]DECREF activity until |
| 577 | after the list is back in its canonical shape. Therefore |
| 578 | we must allocate an additional array, 'recycle', into which |
| 579 | we temporarily copy the items that are deleted from the |
| 580 | list. :-( */ |
| 581 | PyObject *recycle_on_stack[8]; |
| 582 | PyObject **recycle = recycle_on_stack; /* will allocate more if needed */ |
| 583 | PyObject **item; |
| 584 | PyObject **vitem = NULL; |
| 585 | PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */ |
| 586 | Py_ssize_t n; /* # of elements in replacement list */ |
| 587 | Py_ssize_t norig; /* # of elements in list getting replaced */ |
| 588 | Py_ssize_t d; /* Change in size */ |
| 589 | Py_ssize_t k; |
| 590 | size_t s; |
| 591 | int result = -1; /* guilty until proved innocent */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 592 | #define b ((PyListObject *)v) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | if (v == NULL) |
| 594 | n = 0; |
| 595 | else { |
| 596 | if (a == b) { |
| 597 | /* Special case "a[i:j] = a" -- copy b first */ |
| 598 | v = list_slice(b, 0, Py_SIZE(b)); |
| 599 | if (v == NULL) |
| 600 | return result; |
| 601 | result = list_ass_slice(a, ilow, ihigh, v); |
| 602 | Py_DECREF(v); |
| 603 | return result; |
| 604 | } |
| 605 | v_as_SF = PySequence_Fast(v, "can only assign an iterable"); |
| 606 | if(v_as_SF == NULL) |
| 607 | goto Error; |
| 608 | n = PySequence_Fast_GET_SIZE(v_as_SF); |
| 609 | vitem = PySequence_Fast_ITEMS(v_as_SF); |
| 610 | } |
| 611 | if (ilow < 0) |
| 612 | ilow = 0; |
| 613 | else if (ilow > Py_SIZE(a)) |
| 614 | ilow = Py_SIZE(a); |
Tim Peters | 8d9eb10 | 2004-07-31 02:24:20 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | if (ihigh < ilow) |
| 617 | ihigh = ilow; |
| 618 | else if (ihigh > Py_SIZE(a)) |
| 619 | ihigh = Py_SIZE(a); |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | norig = ihigh - ilow; |
| 622 | assert(norig >= 0); |
| 623 | d = n - norig; |
| 624 | if (Py_SIZE(a) + d == 0) { |
| 625 | Py_XDECREF(v_as_SF); |
| 626 | return list_clear(a); |
| 627 | } |
| 628 | item = a->ob_item; |
| 629 | /* recycle the items that we are about to remove */ |
| 630 | s = norig * sizeof(PyObject *); |
| 631 | if (s > sizeof(recycle_on_stack)) { |
| 632 | recycle = (PyObject **)PyMem_MALLOC(s); |
| 633 | if (recycle == NULL) { |
| 634 | PyErr_NoMemory(); |
| 635 | goto Error; |
| 636 | } |
| 637 | } |
| 638 | memcpy(recycle, &item[ilow], s); |
Tim Peters | 8d9eb10 | 2004-07-31 02:24:20 +0000 | [diff] [blame] | 639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | if (d < 0) { /* Delete -d items */ |
| 641 | memmove(&item[ihigh+d], &item[ihigh], |
| 642 | (Py_SIZE(a) - ihigh)*sizeof(PyObject *)); |
| 643 | list_resize(a, Py_SIZE(a) + d); |
| 644 | item = a->ob_item; |
| 645 | } |
| 646 | else if (d > 0) { /* Insert d items */ |
| 647 | k = Py_SIZE(a); |
| 648 | if (list_resize(a, k+d) < 0) |
| 649 | goto Error; |
| 650 | item = a->ob_item; |
| 651 | memmove(&item[ihigh+d], &item[ihigh], |
| 652 | (k - ihigh)*sizeof(PyObject *)); |
| 653 | } |
| 654 | for (k = 0; k < n; k++, ilow++) { |
| 655 | PyObject *w = vitem[k]; |
| 656 | Py_XINCREF(w); |
| 657 | item[ilow] = w; |
| 658 | } |
| 659 | for (k = norig - 1; k >= 0; --k) |
| 660 | Py_XDECREF(recycle[k]); |
| 661 | result = 0; |
Tim Peters | 8d9eb10 | 2004-07-31 02:24:20 +0000 | [diff] [blame] | 662 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | if (recycle != recycle_on_stack) |
| 664 | PyMem_FREE(recycle); |
| 665 | Py_XDECREF(v_as_SF); |
| 666 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 667 | #undef b |
| 668 | } |
| 669 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 670 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 671 | PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | if (!PyList_Check(a)) { |
| 674 | PyErr_BadInternalCall(); |
| 675 | return -1; |
| 676 | } |
| 677 | return list_ass_slice((PyListObject *)a, ilow, ihigh, v); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 680 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 681 | list_inplace_repeat(PyListObject *self, Py_ssize_t n) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 682 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | PyObject **items; |
| 684 | Py_ssize_t size, i, j, p; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 685 | |
| 686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | size = PyList_GET_SIZE(self); |
| 688 | if (size == 0 || n == 1) { |
| 689 | Py_INCREF(self); |
| 690 | return (PyObject *)self; |
| 691 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | if (n < 1) { |
| 694 | (void)list_clear(self); |
| 695 | Py_INCREF(self); |
| 696 | return (PyObject *)self; |
| 697 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | if (size > PY_SSIZE_T_MAX / n) { |
| 700 | return PyErr_NoMemory(); |
| 701 | } |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | if (list_resize(self, size*n) == -1) |
| 704 | return NULL; |
Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | p = size; |
| 707 | items = self->ob_item; |
| 708 | for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */ |
| 709 | for (j = 0; j < size; j++) { |
| 710 | PyObject *o = items[j]; |
| 711 | Py_INCREF(o); |
| 712 | items[p++] = o; |
| 713 | } |
| 714 | } |
| 715 | Py_INCREF(self); |
| 716 | return (PyObject *)self; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 719 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 720 | list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 721 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | PyObject *old_value; |
| 723 | if (i < 0 || i >= Py_SIZE(a)) { |
| 724 | PyErr_SetString(PyExc_IndexError, |
| 725 | "list assignment index out of range"); |
| 726 | return -1; |
| 727 | } |
| 728 | if (v == NULL) |
| 729 | return list_ass_slice(a, i, i+1, v); |
| 730 | Py_INCREF(v); |
| 731 | old_value = a->ob_item[i]; |
| 732 | a->ob_item[i] = v; |
| 733 | Py_DECREF(old_value); |
| 734 | return 0; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 737 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 738 | listinsert(PyListObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | Py_ssize_t i; |
| 741 | PyObject *v; |
| 742 | if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) |
| 743 | return NULL; |
| 744 | if (ins1(self, i, v) == 0) |
| 745 | Py_RETURN_NONE; |
| 746 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 749 | static PyObject * |
Eli Bendersky | cbbaa96 | 2011-02-25 05:47:53 +0000 | [diff] [blame] | 750 | listclear(PyListObject *self) |
| 751 | { |
| 752 | list_clear(self); |
| 753 | Py_RETURN_NONE; |
| 754 | } |
| 755 | |
| 756 | static PyObject * |
| 757 | listcopy(PyListObject *self) |
| 758 | { |
| 759 | return list_slice(self, 0, Py_SIZE(self)); |
| 760 | } |
| 761 | |
| 762 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 763 | listappend(PyListObject *self, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | if (app1(self, v) == 0) |
| 766 | Py_RETURN_NONE; |
| 767 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 770 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 771 | listextend(PyListObject *self, PyObject *b) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | PyObject *it; /* iter(v) */ |
| 774 | Py_ssize_t m; /* size of self */ |
| 775 | Py_ssize_t n; /* guess for size of b */ |
| 776 | Py_ssize_t mn; /* m + n */ |
| 777 | Py_ssize_t i; |
| 778 | PyObject *(*iternext)(PyObject *); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | /* Special cases: |
| 781 | 1) lists and tuples which can use PySequence_Fast ops |
| 782 | 2) extending self to self requires making a copy first |
| 783 | */ |
| 784 | if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) { |
| 785 | PyObject **src, **dest; |
| 786 | b = PySequence_Fast(b, "argument must be iterable"); |
| 787 | if (!b) |
| 788 | return NULL; |
| 789 | n = PySequence_Fast_GET_SIZE(b); |
| 790 | if (n == 0) { |
| 791 | /* short circuit when b is empty */ |
| 792 | Py_DECREF(b); |
| 793 | Py_RETURN_NONE; |
| 794 | } |
| 795 | m = Py_SIZE(self); |
| 796 | if (list_resize(self, m + n) == -1) { |
| 797 | Py_DECREF(b); |
| 798 | return NULL; |
| 799 | } |
| 800 | /* note that we may still have self == b here for the |
| 801 | * situation a.extend(a), but the following code works |
| 802 | * in that case too. Just make sure to resize self |
| 803 | * before calling PySequence_Fast_ITEMS. |
| 804 | */ |
| 805 | /* populate the end of self with b's items */ |
| 806 | src = PySequence_Fast_ITEMS(b); |
| 807 | dest = self->ob_item + m; |
| 808 | for (i = 0; i < n; i++) { |
| 809 | PyObject *o = src[i]; |
| 810 | Py_INCREF(o); |
| 811 | dest[i] = o; |
| 812 | } |
| 813 | Py_DECREF(b); |
| 814 | Py_RETURN_NONE; |
| 815 | } |
Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | it = PyObject_GetIter(b); |
| 818 | if (it == NULL) |
| 819 | return NULL; |
| 820 | iternext = *it->ob_type->tp_iternext; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | /* Guess a result list size. */ |
| 823 | n = _PyObject_LengthHint(b, 8); |
| 824 | if (n == -1) { |
| 825 | Py_DECREF(it); |
| 826 | return NULL; |
| 827 | } |
| 828 | m = Py_SIZE(self); |
| 829 | mn = m + n; |
| 830 | if (mn >= m) { |
| 831 | /* Make room. */ |
| 832 | if (list_resize(self, mn) == -1) |
| 833 | goto error; |
| 834 | /* Make the list sane again. */ |
| 835 | Py_SIZE(self) = m; |
| 836 | } |
| 837 | /* Else m + n overflowed; on the chance that n lied, and there really |
| 838 | * is enough room, ignore it. If n was telling the truth, we'll |
| 839 | * eventually run out of memory during the loop. |
| 840 | */ |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | /* Run iterator to exhaustion. */ |
| 843 | for (;;) { |
| 844 | PyObject *item = iternext(it); |
| 845 | if (item == NULL) { |
| 846 | if (PyErr_Occurred()) { |
| 847 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 848 | PyErr_Clear(); |
| 849 | else |
| 850 | goto error; |
| 851 | } |
| 852 | break; |
| 853 | } |
| 854 | if (Py_SIZE(self) < self->allocated) { |
| 855 | /* steals ref */ |
| 856 | PyList_SET_ITEM(self, Py_SIZE(self), item); |
| 857 | ++Py_SIZE(self); |
| 858 | } |
| 859 | else { |
| 860 | int status = app1(self, item); |
| 861 | Py_DECREF(item); /* append creates a new ref */ |
| 862 | if (status < 0) |
| 863 | goto error; |
| 864 | } |
| 865 | } |
Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | /* Cut back result list if initial guess was too large. */ |
| 868 | if (Py_SIZE(self) < self->allocated) |
| 869 | list_resize(self, Py_SIZE(self)); /* shrinking can't fail */ |
Raymond Hettinger | aa241e0 | 2004-09-26 19:24:20 +0000 | [diff] [blame] | 870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | Py_DECREF(it); |
| 872 | Py_RETURN_NONE; |
Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 873 | |
| 874 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | Py_DECREF(it); |
| 876 | return NULL; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 879 | PyObject * |
| 880 | _PyList_Extend(PyListObject *self, PyObject *b) |
| 881 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | return listextend(self, b); |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 885 | static PyObject * |
Raymond Hettinger | 97bc618 | 2004-03-11 07:34:19 +0000 | [diff] [blame] | 886 | list_inplace_concat(PyListObject *self, PyObject *other) |
| 887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | PyObject *result; |
Raymond Hettinger | 97bc618 | 2004-03-11 07:34:19 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | result = listextend(self, other); |
| 891 | if (result == NULL) |
| 892 | return result; |
| 893 | Py_DECREF(result); |
| 894 | Py_INCREF(self); |
| 895 | return (PyObject *)self; |
Raymond Hettinger | 97bc618 | 2004-03-11 07:34:19 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 899 | listpop(PyListObject *self, PyObject *args) |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | Py_ssize_t i = -1; |
| 902 | PyObject *v; |
| 903 | int status; |
Raymond Hettinger | 9eb86b3 | 2004-02-17 11:36:16 +0000 | [diff] [blame] | 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | if (!PyArg_ParseTuple(args, "|n:pop", &i)) |
| 906 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | if (Py_SIZE(self) == 0) { |
| 909 | /* Special-case most common failure cause */ |
| 910 | PyErr_SetString(PyExc_IndexError, "pop from empty list"); |
| 911 | return NULL; |
| 912 | } |
| 913 | if (i < 0) |
| 914 | i += Py_SIZE(self); |
| 915 | if (i < 0 || i >= Py_SIZE(self)) { |
| 916 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 917 | return NULL; |
| 918 | } |
| 919 | v = self->ob_item[i]; |
| 920 | if (i == Py_SIZE(self) - 1) { |
| 921 | status = list_resize(self, Py_SIZE(self) - 1); |
| 922 | assert(status >= 0); |
| 923 | return v; /* and v now owns the reference the list had */ |
| 924 | } |
| 925 | Py_INCREF(v); |
| 926 | status = list_ass_slice(self, i, i+1, (PyObject *)NULL); |
| 927 | assert(status >= 0); |
| 928 | /* Use status, so that in a release build compilers don't |
| 929 | * complain about the unused name. |
| 930 | */ |
| 931 | (void) status; |
Brett Cannon | 651dd52 | 2004-08-08 21:21:18 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | return v; |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 936 | /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */ |
| 937 | static void |
| 938 | reverse_slice(PyObject **lo, PyObject **hi) |
| 939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | assert(lo && hi); |
Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | --hi; |
| 943 | while (lo < hi) { |
| 944 | PyObject *t = *lo; |
| 945 | *lo = *hi; |
| 946 | *hi = t; |
| 947 | ++lo; |
| 948 | --hi; |
| 949 | } |
Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 952 | /* Lots of code for an adaptive, stable, natural mergesort. There are many |
| 953 | * pieces to this algorithm; read listsort.txt for overviews and details. |
| 954 | */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 955 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 956 | /* A sortslice contains a pointer to an array of keys and a pointer to |
| 957 | * an array of corresponding values. In other words, keys[i] |
| 958 | * corresponds with values[i]. If values == NULL, then the keys are |
| 959 | * also the values. |
| 960 | * |
| 961 | * Several convenience routines are provided here, so that keys and |
| 962 | * values are always moved in sync. |
| 963 | */ |
| 964 | |
| 965 | typedef struct { |
| 966 | PyObject **keys; |
| 967 | PyObject **values; |
| 968 | } sortslice; |
| 969 | |
| 970 | Py_LOCAL_INLINE(void) |
| 971 | sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j) |
| 972 | { |
| 973 | s1->keys[i] = s2->keys[j]; |
| 974 | if (s1->values != NULL) |
| 975 | s1->values[i] = s2->values[j]; |
| 976 | } |
| 977 | |
| 978 | Py_LOCAL_INLINE(void) |
Benjamin Peterson | 9efdcca | 2010-12-03 01:44:10 +0000 | [diff] [blame] | 979 | sortslice_copy_incr(sortslice *dst, sortslice *src) |
| 980 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 981 | *dst->keys++ = *src->keys++; |
| 982 | if (dst->values != NULL) |
| 983 | *dst->values++ = *src->values++; |
| 984 | } |
| 985 | |
| 986 | Py_LOCAL_INLINE(void) |
Benjamin Peterson | 9efdcca | 2010-12-03 01:44:10 +0000 | [diff] [blame] | 987 | sortslice_copy_decr(sortslice *dst, sortslice *src) |
| 988 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 989 | *dst->keys-- = *src->keys--; |
| 990 | if (dst->values != NULL) |
| 991 | *dst->values-- = *src->values--; |
| 992 | } |
| 993 | |
| 994 | |
| 995 | Py_LOCAL_INLINE(void) |
| 996 | sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j, |
Benjamin Peterson | 9efdcca | 2010-12-03 01:44:10 +0000 | [diff] [blame] | 997 | Py_ssize_t n) |
| 998 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 999 | memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n); |
| 1000 | if (s1->values != NULL) |
| 1001 | memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n); |
| 1002 | } |
| 1003 | |
| 1004 | Py_LOCAL_INLINE(void) |
| 1005 | sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j, |
Benjamin Peterson | 9efdcca | 2010-12-03 01:44:10 +0000 | [diff] [blame] | 1006 | Py_ssize_t n) |
| 1007 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1008 | memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n); |
| 1009 | if (s1->values != NULL) |
| 1010 | memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n); |
| 1011 | } |
| 1012 | |
| 1013 | Py_LOCAL_INLINE(void) |
Benjamin Peterson | 9efdcca | 2010-12-03 01:44:10 +0000 | [diff] [blame] | 1014 | sortslice_advance(sortslice *slice, Py_ssize_t n) |
| 1015 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1016 | slice->keys += n; |
| 1017 | if (slice->values != NULL) |
| 1018 | slice->values += n; |
| 1019 | } |
| 1020 | |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 1021 | /* Comparison function: PyObject_RichCompareBool with Py_LT. |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1022 | * Returns -1 on error, 1 if x < y, 0 if x >= y. |
| 1023 | */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1024 | |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 1025 | #define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT)) |
Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 1026 | |
| 1027 | /* Compare X to Y via "<". Goto "fail" if the comparison raises an |
Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 1028 | error. Else "k" is set to true iff X<Y, and an "if (k)" block is |
| 1029 | started. It makes more sense in context <wink>. X and Y are PyObject*s. |
| 1030 | */ |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 1031 | #define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | if (k) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1033 | |
| 1034 | /* binarysort is the best method for sorting small arrays: it does |
| 1035 | few compares, but can do data movement quadratic in the number of |
| 1036 | elements. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1037 | [lo, hi) is a contiguous slice of a list, and is sorted via |
Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 1038 | binary insertion. This sort is stable. |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1039 | On entry, must have lo <= start <= hi, and that [lo, start) is already |
| 1040 | sorted (pass start == lo if you don't know!). |
Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 1041 | If islt() complains return -1, else 0. |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1042 | Even in case of error, the output slice will be some permutation of |
| 1043 | the input (nothing is lost or duplicated). |
| 1044 | */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1045 | static int |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1046 | binarysort(sortslice lo, PyObject **hi, PyObject **start) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1047 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | register Py_ssize_t k; |
| 1049 | register PyObject **l, **p, **r; |
| 1050 | register PyObject *pivot; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1051 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1052 | assert(lo.keys <= start && start <= hi); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | /* assert [lo, start) is sorted */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1054 | if (lo.keys == start) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | ++start; |
| 1056 | for (; start < hi; ++start) { |
| 1057 | /* set l to where *start belongs */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1058 | l = lo.keys; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | r = start; |
| 1060 | pivot = *r; |
| 1061 | /* Invariants: |
| 1062 | * pivot >= all in [lo, l). |
| 1063 | * pivot < all in [r, start). |
| 1064 | * The second is vacuously true at the start. |
| 1065 | */ |
| 1066 | assert(l < r); |
| 1067 | do { |
| 1068 | p = l + ((r - l) >> 1); |
| 1069 | IFLT(pivot, *p) |
| 1070 | r = p; |
| 1071 | else |
| 1072 | l = p+1; |
| 1073 | } while (l < r); |
| 1074 | assert(l == r); |
| 1075 | /* The invariants still hold, so pivot >= all in [lo, l) and |
| 1076 | pivot < all in [l, start), so pivot belongs at l. Note |
| 1077 | that if there are elements equal to pivot, l points to the |
| 1078 | first slot after them -- that's why this sort is stable. |
| 1079 | Slide over to make room. |
| 1080 | Caution: using memmove is much slower under MSVC 5; |
| 1081 | we're not usually moving many slots. */ |
| 1082 | for (p = start; p > l; --p) |
| 1083 | *p = *(p-1); |
| 1084 | *l = pivot; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1085 | if (lo.values != NULL) { |
| 1086 | Py_ssize_t offset = lo.values - lo.keys; |
| 1087 | p = start + offset; |
| 1088 | pivot = *p; |
| 1089 | l += offset; |
| 1090 | for (p = start + offset; p > l; --p) |
| 1091 | *p = *(p-1); |
| 1092 | *l = pivot; |
| 1093 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | } |
| 1095 | return 0; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 1096 | |
| 1097 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | return -1; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1101 | /* |
| 1102 | Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi |
| 1103 | is required on entry. "A run" is the longest ascending sequence, with |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1104 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1105 | lo[0] <= lo[1] <= lo[2] <= ... |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1106 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1107 | or the longest descending sequence, with |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1108 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1109 | lo[0] > lo[1] > lo[2] > ... |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 1110 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1111 | Boolean *descending is set to 0 in the former case, or to 1 in the latter. |
| 1112 | For its intended use in a stable mergesort, the strictness of the defn of |
| 1113 | "descending" is needed so that the caller can safely reverse a descending |
| 1114 | sequence without violating stability (strict > ensures there are no equal |
| 1115 | elements to get out of order). |
| 1116 | |
| 1117 | Returns -1 in case of error. |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1118 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1119 | static Py_ssize_t |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 1120 | count_run(PyObject **lo, PyObject **hi, int *descending) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1121 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | Py_ssize_t k; |
| 1123 | Py_ssize_t n; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | assert(lo < hi); |
| 1126 | *descending = 0; |
| 1127 | ++lo; |
| 1128 | if (lo == hi) |
| 1129 | return 1; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | n = 2; |
| 1132 | IFLT(*lo, *(lo-1)) { |
| 1133 | *descending = 1; |
| 1134 | for (lo = lo+1; lo < hi; ++lo, ++n) { |
| 1135 | IFLT(*lo, *(lo-1)) |
| 1136 | ; |
| 1137 | else |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | else { |
| 1142 | for (lo = lo+1; lo < hi; ++lo, ++n) { |
| 1143 | IFLT(*lo, *(lo-1)) |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | return n; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1149 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | return -1; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1151 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1152 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | Locate the proper position of key in a sorted vector; if the vector contains |
| 1155 | an element equal to key, return the position immediately to the left of |
| 1156 | the leftmost equal element. [gallop_right() does the same except returns |
| 1157 | the position to the right of the rightmost equal element (if any).] |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1158 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1159 | "a" is a sorted vector with n elements, starting at a[0]. n must be > 0. |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1160 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1161 | "hint" is an index at which to begin the search, 0 <= hint < n. The closer |
| 1162 | hint is to the final result, the faster this runs. |
| 1163 | |
| 1164 | The return value is the int k in 0..n such that |
| 1165 | |
| 1166 | a[k-1] < key <= a[k] |
| 1167 | |
| 1168 | pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW, |
| 1169 | key belongs at index k; or, IOW, the first k elements of a should precede |
| 1170 | key, and the last n-k should follow key. |
| 1171 | |
| 1172 | Returns -1 on error. See listsort.txt for info on the method. |
| 1173 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1174 | static Py_ssize_t |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 1175 | gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | Py_ssize_t ofs; |
| 1178 | Py_ssize_t lastofs; |
| 1179 | Py_ssize_t k; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | assert(key && a && n > 0 && hint >= 0 && hint < n); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | a += hint; |
| 1184 | lastofs = 0; |
| 1185 | ofs = 1; |
| 1186 | IFLT(*a, key) { |
| 1187 | /* a[hint] < key -- gallop right, until |
| 1188 | * a[hint + lastofs] < key <= a[hint + ofs] |
| 1189 | */ |
| 1190 | const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */ |
| 1191 | while (ofs < maxofs) { |
| 1192 | IFLT(a[ofs], key) { |
| 1193 | lastofs = ofs; |
| 1194 | ofs = (ofs << 1) + 1; |
| 1195 | if (ofs <= 0) /* int overflow */ |
| 1196 | ofs = maxofs; |
| 1197 | } |
| 1198 | else /* key <= a[hint + ofs] */ |
| 1199 | break; |
| 1200 | } |
| 1201 | if (ofs > maxofs) |
| 1202 | ofs = maxofs; |
| 1203 | /* Translate back to offsets relative to &a[0]. */ |
| 1204 | lastofs += hint; |
| 1205 | ofs += hint; |
| 1206 | } |
| 1207 | else { |
| 1208 | /* key <= a[hint] -- gallop left, until |
| 1209 | * a[hint - ofs] < key <= a[hint - lastofs] |
| 1210 | */ |
| 1211 | const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */ |
| 1212 | while (ofs < maxofs) { |
| 1213 | IFLT(*(a-ofs), key) |
| 1214 | break; |
| 1215 | /* key <= a[hint - ofs] */ |
| 1216 | lastofs = ofs; |
| 1217 | ofs = (ofs << 1) + 1; |
| 1218 | if (ofs <= 0) /* int overflow */ |
| 1219 | ofs = maxofs; |
| 1220 | } |
| 1221 | if (ofs > maxofs) |
| 1222 | ofs = maxofs; |
| 1223 | /* Translate back to positive offsets relative to &a[0]. */ |
| 1224 | k = lastofs; |
| 1225 | lastofs = hint - ofs; |
| 1226 | ofs = hint - k; |
| 1227 | } |
| 1228 | a -= hint; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | assert(-1 <= lastofs && lastofs < ofs && ofs <= n); |
| 1231 | /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the |
| 1232 | * right of lastofs but no farther right than ofs. Do a binary |
| 1233 | * search, with invariant a[lastofs-1] < key <= a[ofs]. |
| 1234 | */ |
| 1235 | ++lastofs; |
| 1236 | while (lastofs < ofs) { |
| 1237 | Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | IFLT(a[m], key) |
| 1240 | lastofs = m+1; /* a[m] < key */ |
| 1241 | else |
| 1242 | ofs = m; /* key <= a[m] */ |
| 1243 | } |
| 1244 | assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */ |
| 1245 | return ofs; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1246 | |
| 1247 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | return -1; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | Exactly like gallop_left(), except that if key already exists in a[0:n], |
| 1253 | finds the position immediately to the right of the rightmost equal value. |
| 1254 | |
| 1255 | The return value is the int k in 0..n such that |
| 1256 | |
| 1257 | a[k-1] <= key < a[k] |
| 1258 | |
| 1259 | or -1 if error. |
| 1260 | |
| 1261 | The code duplication is massive, but this is enough different given that |
| 1262 | we're sticking to "<" comparisons that it's much harder to follow if |
| 1263 | written as one routine with yet another "left or right?" flag. |
| 1264 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1265 | static Py_ssize_t |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 1266 | gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | Py_ssize_t ofs; |
| 1269 | Py_ssize_t lastofs; |
| 1270 | Py_ssize_t k; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | assert(key && a && n > 0 && hint >= 0 && hint < n); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | a += hint; |
| 1275 | lastofs = 0; |
| 1276 | ofs = 1; |
| 1277 | IFLT(key, *a) { |
| 1278 | /* key < a[hint] -- gallop left, until |
| 1279 | * a[hint - ofs] <= key < a[hint - lastofs] |
| 1280 | */ |
| 1281 | const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */ |
| 1282 | while (ofs < maxofs) { |
| 1283 | IFLT(key, *(a-ofs)) { |
| 1284 | lastofs = ofs; |
| 1285 | ofs = (ofs << 1) + 1; |
| 1286 | if (ofs <= 0) /* int overflow */ |
| 1287 | ofs = maxofs; |
| 1288 | } |
| 1289 | else /* a[hint - ofs] <= key */ |
| 1290 | break; |
| 1291 | } |
| 1292 | if (ofs > maxofs) |
| 1293 | ofs = maxofs; |
| 1294 | /* Translate back to positive offsets relative to &a[0]. */ |
| 1295 | k = lastofs; |
| 1296 | lastofs = hint - ofs; |
| 1297 | ofs = hint - k; |
| 1298 | } |
| 1299 | else { |
| 1300 | /* a[hint] <= key -- gallop right, until |
| 1301 | * a[hint + lastofs] <= key < a[hint + ofs] |
| 1302 | */ |
| 1303 | const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */ |
| 1304 | while (ofs < maxofs) { |
| 1305 | IFLT(key, a[ofs]) |
| 1306 | break; |
| 1307 | /* a[hint + ofs] <= key */ |
| 1308 | lastofs = ofs; |
| 1309 | ofs = (ofs << 1) + 1; |
| 1310 | if (ofs <= 0) /* int overflow */ |
| 1311 | ofs = maxofs; |
| 1312 | } |
| 1313 | if (ofs > maxofs) |
| 1314 | ofs = maxofs; |
| 1315 | /* Translate back to offsets relative to &a[0]. */ |
| 1316 | lastofs += hint; |
| 1317 | ofs += hint; |
| 1318 | } |
| 1319 | a -= hint; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | assert(-1 <= lastofs && lastofs < ofs && ofs <= n); |
| 1322 | /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the |
| 1323 | * right of lastofs but no farther right than ofs. Do a binary |
| 1324 | * search, with invariant a[lastofs-1] <= key < a[ofs]. |
| 1325 | */ |
| 1326 | ++lastofs; |
| 1327 | while (lastofs < ofs) { |
| 1328 | Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | IFLT(key, a[m]) |
| 1331 | ofs = m; /* key < a[m] */ |
| 1332 | else |
| 1333 | lastofs = m+1; /* a[m] <= key */ |
| 1334 | } |
| 1335 | assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */ |
| 1336 | return ofs; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1337 | |
| 1338 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | return -1; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* The maximum number of entries in a MergeState's pending-runs stack. |
| 1343 | * This is enough to sort arrays of size up to about |
| 1344 | * 32 * phi ** MAX_MERGE_PENDING |
| 1345 | * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array |
| 1346 | * with 2**64 elements. |
| 1347 | */ |
| 1348 | #define MAX_MERGE_PENDING 85 |
| 1349 | |
Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1350 | /* When we get into galloping mode, we stay there until both runs win less |
| 1351 | * often than MIN_GALLOP consecutive times. See listsort.txt for more info. |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1352 | */ |
Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1353 | #define MIN_GALLOP 7 |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1354 | |
| 1355 | /* Avoid malloc for small temp arrays. */ |
| 1356 | #define MERGESTATE_TEMP_SIZE 256 |
| 1357 | |
| 1358 | /* One MergeState exists on the stack per invocation of mergesort. It's just |
| 1359 | * a convenient way to pass state around among the helper functions. |
| 1360 | */ |
Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1361 | struct s_slice { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1362 | sortslice base; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | Py_ssize_t len; |
Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1364 | }; |
| 1365 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1366 | typedef struct s_MergeState { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | /* This controls when we get *into* galloping mode. It's initialized |
| 1368 | * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for |
| 1369 | * random data, and lower for highly structured data. |
| 1370 | */ |
| 1371 | Py_ssize_t min_gallop; |
Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | /* 'a' is temp storage to help with merges. It contains room for |
| 1374 | * alloced entries. |
| 1375 | */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1376 | sortslice a; /* may point to temparray below */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | Py_ssize_t alloced; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | /* A stack of n pending runs yet to be merged. Run #i starts at |
| 1380 | * address base[i] and extends for len[i] elements. It's always |
| 1381 | * true (so long as the indices are in bounds) that |
| 1382 | * |
| 1383 | * pending[i].base + pending[i].len == pending[i+1].base |
| 1384 | * |
| 1385 | * so we could cut the storage for this, but it's a minor amount, |
| 1386 | * and keeping all the info explicit simplifies the code. |
| 1387 | */ |
| 1388 | int n; |
| 1389 | struct s_slice pending[MAX_MERGE_PENDING]; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | /* 'a' points to this when possible, rather than muck with malloc. */ |
| 1392 | PyObject *temparray[MERGESTATE_TEMP_SIZE]; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1393 | } MergeState; |
| 1394 | |
| 1395 | /* Conceptually a MergeState's constructor. */ |
| 1396 | static void |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 1397 | merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | assert(ms != NULL); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1400 | if (has_keyfunc) { |
| 1401 | /* The temporary space for merging will need at most half the list |
| 1402 | * size rounded up. Use the minimum possible space so we can use the |
| 1403 | * rest of temparray for other things. In particular, if there is |
| 1404 | * enough extra space, listsort() will use it to store the keys. |
| 1405 | */ |
| 1406 | ms->alloced = (list_size + 1) / 2; |
| 1407 | |
| 1408 | /* ms->alloced describes how many keys will be stored at |
| 1409 | ms->temparray, but we also need to store the values. Hence, |
| 1410 | ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */ |
| 1411 | if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced) |
| 1412 | ms->alloced = MERGESTATE_TEMP_SIZE / 2; |
| 1413 | ms->a.values = &ms->temparray[ms->alloced]; |
| 1414 | } |
| 1415 | else { |
| 1416 | ms->alloced = MERGESTATE_TEMP_SIZE; |
| 1417 | ms->a.values = NULL; |
| 1418 | } |
| 1419 | ms->a.keys = ms->temparray; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | ms->n = 0; |
| 1421 | ms->min_gallop = MIN_GALLOP; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /* Free all the temp memory owned by the MergeState. This must be called |
| 1425 | * when you're done with a MergeState, and may be called before then if |
| 1426 | * you want to free the temp memory early. |
| 1427 | */ |
| 1428 | static void |
| 1429 | merge_freemem(MergeState *ms) |
| 1430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1431 | assert(ms != NULL); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1432 | if (ms->a.keys != ms->temparray) |
| 1433 | PyMem_Free(ms->a.keys); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* Ensure enough temp memory for 'need' array slots is available. |
| 1437 | * Returns 0 on success and -1 if the memory can't be gotten. |
| 1438 | */ |
| 1439 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1440 | merge_getmem(MergeState *ms, Py_ssize_t need) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1441 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1442 | int multiplier; |
| 1443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | assert(ms != NULL); |
| 1445 | if (need <= ms->alloced) |
| 1446 | return 0; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1447 | |
| 1448 | multiplier = ms->a.values != NULL ? 2 : 1; |
| 1449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | /* Don't realloc! That can cost cycles to copy the old data, but |
| 1451 | * we don't care what's in the block. |
| 1452 | */ |
| 1453 | merge_freemem(ms); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1454 | if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | PyErr_NoMemory(); |
| 1456 | return -1; |
| 1457 | } |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1458 | ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need |
| 1459 | * sizeof(PyObject *)); |
| 1460 | if (ms->a.keys != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | ms->alloced = need; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1462 | if (ms->a.values != NULL) |
| 1463 | ms->a.values = &ms->a.keys[need]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | return 0; |
| 1465 | } |
| 1466 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | return -1; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1468 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \ |
| 1470 | merge_getmem(MS, NEED)) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1471 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1472 | /* Merge the na elements starting at ssa with the nb elements starting at |
| 1473 | * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0. |
| 1474 | * Must also have that ssa.keys[na-1] belongs at the end of the merge, and |
| 1475 | * should have na <= nb. See listsort.txt for more info. Return 0 if |
| 1476 | * successful, -1 if error. |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1477 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1478 | static Py_ssize_t |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1479 | merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na, |
| 1480 | sortslice ssb, Py_ssize_t nb) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | Py_ssize_t k; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1483 | sortslice dest; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | int result = -1; /* guilty until proved innocent */ |
| 1485 | Py_ssize_t min_gallop; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1486 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1487 | assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0); |
| 1488 | assert(ssa.keys + na == ssb.keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | if (MERGE_GETMEM(ms, na) < 0) |
| 1490 | return -1; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1491 | sortslice_memcpy(&ms->a, 0, &ssa, 0, na); |
| 1492 | dest = ssa; |
| 1493 | ssa = ms->a; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1494 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1495 | sortslice_copy_incr(&dest, &ssb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | --nb; |
| 1497 | if (nb == 0) |
| 1498 | goto Succeed; |
| 1499 | if (na == 1) |
| 1500 | goto CopyB; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | min_gallop = ms->min_gallop; |
| 1503 | for (;;) { |
| 1504 | Py_ssize_t acount = 0; /* # of times A won in a row */ |
| 1505 | Py_ssize_t bcount = 0; /* # of times B won in a row */ |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | /* Do the straightforward thing until (if ever) one run |
| 1508 | * appears to win consistently. |
| 1509 | */ |
| 1510 | for (;;) { |
| 1511 | assert(na > 1 && nb > 0); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1512 | k = ISLT(ssb.keys[0], ssa.keys[0]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | if (k) { |
| 1514 | if (k < 0) |
| 1515 | goto Fail; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1516 | sortslice_copy_incr(&dest, &ssb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | ++bcount; |
| 1518 | acount = 0; |
| 1519 | --nb; |
| 1520 | if (nb == 0) |
| 1521 | goto Succeed; |
| 1522 | if (bcount >= min_gallop) |
| 1523 | break; |
| 1524 | } |
| 1525 | else { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1526 | sortslice_copy_incr(&dest, &ssa); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | ++acount; |
| 1528 | bcount = 0; |
| 1529 | --na; |
| 1530 | if (na == 1) |
| 1531 | goto CopyB; |
| 1532 | if (acount >= min_gallop) |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | /* One run is winning so consistently that galloping may |
| 1538 | * be a huge win. So try that, and continue galloping until |
| 1539 | * (if ever) neither run appears to be winning consistently |
| 1540 | * anymore. |
| 1541 | */ |
| 1542 | ++min_gallop; |
| 1543 | do { |
| 1544 | assert(na > 1 && nb > 0); |
| 1545 | min_gallop -= min_gallop > 1; |
| 1546 | ms->min_gallop = min_gallop; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1547 | k = gallop_right(ssb.keys[0], ssa.keys, na, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | acount = k; |
| 1549 | if (k) { |
| 1550 | if (k < 0) |
| 1551 | goto Fail; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1552 | sortslice_memcpy(&dest, 0, &ssa, 0, k); |
| 1553 | sortslice_advance(&dest, k); |
| 1554 | sortslice_advance(&ssa, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1555 | na -= k; |
| 1556 | if (na == 1) |
| 1557 | goto CopyB; |
| 1558 | /* na==0 is impossible now if the comparison |
| 1559 | * function is consistent, but we can't assume |
| 1560 | * that it is. |
| 1561 | */ |
| 1562 | if (na == 0) |
| 1563 | goto Succeed; |
| 1564 | } |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1565 | sortslice_copy_incr(&dest, &ssb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | --nb; |
| 1567 | if (nb == 0) |
| 1568 | goto Succeed; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1569 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1570 | k = gallop_left(ssa.keys[0], ssb.keys, nb, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | bcount = k; |
| 1572 | if (k) { |
| 1573 | if (k < 0) |
| 1574 | goto Fail; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1575 | sortslice_memmove(&dest, 0, &ssb, 0, k); |
| 1576 | sortslice_advance(&dest, k); |
| 1577 | sortslice_advance(&ssb, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | nb -= k; |
| 1579 | if (nb == 0) |
| 1580 | goto Succeed; |
| 1581 | } |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1582 | sortslice_copy_incr(&dest, &ssa); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | --na; |
| 1584 | if (na == 1) |
| 1585 | goto CopyB; |
| 1586 | } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
| 1587 | ++min_gallop; /* penalize it for leaving galloping mode */ |
| 1588 | ms->min_gallop = min_gallop; |
| 1589 | } |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1590 | Succeed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | result = 0; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1592 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | if (na) |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1594 | sortslice_memcpy(&dest, 0, &ssa, 0, na); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1595 | return result; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1596 | CopyB: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | assert(na == 1 && nb > 0); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1598 | /* The last element of ssa belongs at the end of the merge. */ |
| 1599 | sortslice_memmove(&dest, 0, &ssb, 0, nb); |
| 1600 | sortslice_copy(&dest, nb, &ssa, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | return 0; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1602 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1603 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1604 | /* Merge the na elements starting at pa with the nb elements starting at |
| 1605 | * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0. |
| 1606 | * Must also have that ssa.keys[na-1] belongs at the end of the merge, and |
| 1607 | * should have na >= nb. See listsort.txt for more info. Return 0 if |
| 1608 | * successful, -1 if error. |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1609 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1610 | static Py_ssize_t |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1611 | merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na, |
| 1612 | sortslice ssb, Py_ssize_t nb) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | Py_ssize_t k; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1615 | sortslice dest, basea, baseb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | int result = -1; /* guilty until proved innocent */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | Py_ssize_t min_gallop; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1618 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1619 | assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0); |
| 1620 | assert(ssa.keys + na == ssb.keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | if (MERGE_GETMEM(ms, nb) < 0) |
| 1622 | return -1; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1623 | dest = ssb; |
| 1624 | sortslice_advance(&dest, nb-1); |
| 1625 | sortslice_memcpy(&ms->a, 0, &ssb, 0, nb); |
| 1626 | basea = ssa; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | baseb = ms->a; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1628 | ssb.keys = ms->a.keys + nb - 1; |
| 1629 | if (ssb.values != NULL) |
| 1630 | ssb.values = ms->a.values + nb - 1; |
| 1631 | sortslice_advance(&ssa, na - 1); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1632 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1633 | sortslice_copy_decr(&dest, &ssa); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1634 | --na; |
| 1635 | if (na == 0) |
| 1636 | goto Succeed; |
| 1637 | if (nb == 1) |
| 1638 | goto CopyA; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | min_gallop = ms->min_gallop; |
| 1641 | for (;;) { |
| 1642 | Py_ssize_t acount = 0; /* # of times A won in a row */ |
| 1643 | Py_ssize_t bcount = 0; /* # of times B won in a row */ |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | /* Do the straightforward thing until (if ever) one run |
| 1646 | * appears to win consistently. |
| 1647 | */ |
| 1648 | for (;;) { |
| 1649 | assert(na > 0 && nb > 1); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1650 | k = ISLT(ssb.keys[0], ssa.keys[0]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | if (k) { |
| 1652 | if (k < 0) |
| 1653 | goto Fail; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1654 | sortslice_copy_decr(&dest, &ssa); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | ++acount; |
| 1656 | bcount = 0; |
| 1657 | --na; |
| 1658 | if (na == 0) |
| 1659 | goto Succeed; |
| 1660 | if (acount >= min_gallop) |
| 1661 | break; |
| 1662 | } |
| 1663 | else { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1664 | sortslice_copy_decr(&dest, &ssb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1665 | ++bcount; |
| 1666 | acount = 0; |
| 1667 | --nb; |
| 1668 | if (nb == 1) |
| 1669 | goto CopyA; |
| 1670 | if (bcount >= min_gallop) |
| 1671 | break; |
| 1672 | } |
| 1673 | } |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | /* One run is winning so consistently that galloping may |
| 1676 | * be a huge win. So try that, and continue galloping until |
| 1677 | * (if ever) neither run appears to be winning consistently |
| 1678 | * anymore. |
| 1679 | */ |
| 1680 | ++min_gallop; |
| 1681 | do { |
| 1682 | assert(na > 0 && nb > 1); |
| 1683 | min_gallop -= min_gallop > 1; |
| 1684 | ms->min_gallop = min_gallop; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1685 | k = gallop_right(ssb.keys[0], basea.keys, na, na-1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | if (k < 0) |
| 1687 | goto Fail; |
| 1688 | k = na - k; |
| 1689 | acount = k; |
| 1690 | if (k) { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1691 | sortslice_advance(&dest, -k); |
| 1692 | sortslice_advance(&ssa, -k); |
| 1693 | sortslice_memmove(&dest, 1, &ssa, 1, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | na -= k; |
| 1695 | if (na == 0) |
| 1696 | goto Succeed; |
| 1697 | } |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1698 | sortslice_copy_decr(&dest, &ssb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | --nb; |
| 1700 | if (nb == 1) |
| 1701 | goto CopyA; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1702 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1703 | k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | if (k < 0) |
| 1705 | goto Fail; |
| 1706 | k = nb - k; |
| 1707 | bcount = k; |
| 1708 | if (k) { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1709 | sortslice_advance(&dest, -k); |
| 1710 | sortslice_advance(&ssb, -k); |
| 1711 | sortslice_memcpy(&dest, 1, &ssb, 1, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | nb -= k; |
| 1713 | if (nb == 1) |
| 1714 | goto CopyA; |
| 1715 | /* nb==0 is impossible now if the comparison |
| 1716 | * function is consistent, but we can't assume |
| 1717 | * that it is. |
| 1718 | */ |
| 1719 | if (nb == 0) |
| 1720 | goto Succeed; |
| 1721 | } |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1722 | sortslice_copy_decr(&dest, &ssa); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | --na; |
| 1724 | if (na == 0) |
| 1725 | goto Succeed; |
| 1726 | } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
| 1727 | ++min_gallop; /* penalize it for leaving galloping mode */ |
| 1728 | ms->min_gallop = min_gallop; |
| 1729 | } |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1730 | Succeed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | result = 0; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1732 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | if (nb) |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1734 | sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | return result; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1736 | CopyA: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | assert(nb == 1 && na > 0); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1738 | /* The first element of ssb belongs at the front of the merge. */ |
| 1739 | sortslice_memmove(&dest, 1-na, &ssa, 1-na, na); |
| 1740 | sortslice_advance(&dest, -na); |
| 1741 | sortslice_advance(&ssa, -na); |
| 1742 | sortslice_copy(&dest, 0, &ssb, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | return 0; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | /* Merge the two runs at stack indices i and i+1. |
| 1747 | * Returns 0 on success, -1 on error. |
| 1748 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1749 | static Py_ssize_t |
| 1750 | merge_at(MergeState *ms, Py_ssize_t i) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1751 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1752 | sortslice ssa, ssb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1753 | Py_ssize_t na, nb; |
| 1754 | Py_ssize_t k; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1756 | assert(ms != NULL); |
| 1757 | assert(ms->n >= 2); |
| 1758 | assert(i >= 0); |
| 1759 | assert(i == ms->n - 2 || i == ms->n - 3); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1760 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1761 | ssa = ms->pending[i].base; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | na = ms->pending[i].len; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1763 | ssb = ms->pending[i+1].base; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | nb = ms->pending[i+1].len; |
| 1765 | assert(na > 0 && nb > 0); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1766 | assert(ssa.keys + na == ssb.keys); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | /* Record the length of the combined runs; if i is the 3rd-last |
| 1769 | * run now, also slide over the last run (which isn't involved |
| 1770 | * in this merge). The current run i+1 goes away in any case. |
| 1771 | */ |
| 1772 | ms->pending[i].len = na + nb; |
| 1773 | if (i == ms->n - 3) |
| 1774 | ms->pending[i+1] = ms->pending[i+2]; |
| 1775 | --ms->n; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | /* Where does b start in a? Elements in a before that can be |
| 1778 | * ignored (already in place). |
| 1779 | */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1780 | k = gallop_right(*ssb.keys, ssa.keys, na, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | if (k < 0) |
| 1782 | return -1; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1783 | sortslice_advance(&ssa, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | na -= k; |
| 1785 | if (na == 0) |
| 1786 | return 0; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | /* Where does a end in b? Elements in b after that can be |
| 1789 | * ignored (already in place). |
| 1790 | */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1791 | nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | if (nb <= 0) |
| 1793 | return nb; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | /* Merge what remains of the runs, using a temp array with |
| 1796 | * min(na, nb) elements. |
| 1797 | */ |
| 1798 | if (na <= nb) |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1799 | return merge_lo(ms, ssa, na, ssb, nb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1800 | else |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1801 | return merge_hi(ms, ssa, na, ssb, nb); |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* Examine the stack of runs waiting to be merged, merging adjacent runs |
| 1805 | * until the stack invariants are re-established: |
| 1806 | * |
| 1807 | * 1. len[-3] > len[-2] + len[-1] |
| 1808 | * 2. len[-2] > len[-1] |
| 1809 | * |
| 1810 | * See listsort.txt for more info. |
| 1811 | * |
| 1812 | * Returns 0 on success, -1 on error. |
| 1813 | */ |
| 1814 | static int |
| 1815 | merge_collapse(MergeState *ms) |
| 1816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | struct s_slice *p = ms->pending; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | assert(ms); |
| 1820 | while (ms->n > 1) { |
| 1821 | Py_ssize_t n = ms->n - 2; |
| 1822 | if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) { |
| 1823 | if (p[n-1].len < p[n+1].len) |
| 1824 | --n; |
| 1825 | if (merge_at(ms, n) < 0) |
| 1826 | return -1; |
| 1827 | } |
| 1828 | else if (p[n].len <= p[n+1].len) { |
| 1829 | if (merge_at(ms, n) < 0) |
| 1830 | return -1; |
| 1831 | } |
| 1832 | else |
| 1833 | break; |
| 1834 | } |
| 1835 | return 0; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* Regardless of invariants, merge all runs on the stack until only one |
| 1839 | * remains. This is used at the end of the mergesort. |
| 1840 | * |
| 1841 | * Returns 0 on success, -1 on error. |
| 1842 | */ |
| 1843 | static int |
| 1844 | merge_force_collapse(MergeState *ms) |
| 1845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | struct s_slice *p = ms->pending; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | assert(ms); |
| 1849 | while (ms->n > 1) { |
| 1850 | Py_ssize_t n = ms->n - 2; |
| 1851 | if (n > 0 && p[n-1].len < p[n+1].len) |
| 1852 | --n; |
| 1853 | if (merge_at(ms, n) < 0) |
| 1854 | return -1; |
| 1855 | } |
| 1856 | return 0; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /* Compute a good value for the minimum run length; natural runs shorter |
| 1860 | * than this are boosted artificially via binary insertion. |
| 1861 | * |
| 1862 | * If n < 64, return n (it's too small to bother with fancy stuff). |
| 1863 | * Else if n is an exact power of 2, return 32. |
| 1864 | * Else return an int k, 32 <= k <= 64, such that n/k is close to, but |
| 1865 | * strictly less than, an exact power of 2. |
| 1866 | * |
| 1867 | * See listsort.txt for more info. |
| 1868 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1869 | static Py_ssize_t |
| 1870 | merge_compute_minrun(Py_ssize_t n) |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */ |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | assert(n >= 0); |
| 1875 | while (n >= 64) { |
| 1876 | r |= n & 1; |
| 1877 | n >>= 1; |
| 1878 | } |
| 1879 | return n + r; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1880 | } |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 1881 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1882 | static void |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1883 | reverse_sortslice(sortslice *s, Py_ssize_t n) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1884 | { |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1885 | reverse_slice(s->keys, &s->keys[n]); |
| 1886 | if (s->values != NULL) |
| 1887 | reverse_slice(s->values, &s->values[n]); |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1890 | /* An adaptive, stable, natural mergesort. See listsort.txt. |
| 1891 | * Returns Py_None on success, NULL on error. Even in case of error, the |
| 1892 | * list will be some permutation of its input state (nothing is lost or |
| 1893 | * duplicated). |
| 1894 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1895 | static PyObject * |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1896 | listsort(PyListObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1897 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | MergeState ms; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | Py_ssize_t nremaining; |
| 1900 | Py_ssize_t minrun; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1901 | sortslice lo; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | Py_ssize_t saved_ob_size, saved_allocated; |
| 1903 | PyObject **saved_ob_item; |
| 1904 | PyObject **final_ob_item; |
| 1905 | PyObject *result = NULL; /* guilty until proved innocent */ |
| 1906 | int reverse = 0; |
| 1907 | PyObject *keyfunc = NULL; |
| 1908 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | static char *kwlist[] = {"key", "reverse", 0}; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1910 | PyObject **keys; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | assert(self != NULL); |
| 1913 | assert (PyList_Check(self)); |
| 1914 | if (args != NULL) { |
| 1915 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort", |
| 1916 | kwlist, &keyfunc, &reverse)) |
| 1917 | return NULL; |
| 1918 | if (Py_SIZE(args) > 0) { |
| 1919 | PyErr_SetString(PyExc_TypeError, |
| 1920 | "must use keyword argument for key function"); |
| 1921 | return NULL; |
| 1922 | } |
| 1923 | } |
| 1924 | if (keyfunc == Py_None) |
| 1925 | keyfunc = NULL; |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | /* The list is temporarily made empty, so that mutations performed |
| 1928 | * by comparison functions can't affect the slice of memory we're |
| 1929 | * sorting (allowing mutations during sorting is a core-dump |
| 1930 | * factory, since ob_item may change). |
| 1931 | */ |
| 1932 | saved_ob_size = Py_SIZE(self); |
| 1933 | saved_ob_item = self->ob_item; |
| 1934 | saved_allocated = self->allocated; |
| 1935 | Py_SIZE(self) = 0; |
| 1936 | self->ob_item = NULL; |
| 1937 | self->allocated = -1; /* any operation will reset it to >= 0 */ |
Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 1938 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1939 | if (keyfunc == NULL) { |
| 1940 | keys = NULL; |
| 1941 | lo.keys = saved_ob_item; |
| 1942 | lo.values = NULL; |
| 1943 | } |
| 1944 | else { |
| 1945 | if (saved_ob_size < MERGESTATE_TEMP_SIZE/2) |
| 1946 | /* Leverage stack space we allocated but won't otherwise use */ |
| 1947 | keys = &ms.temparray[saved_ob_size+1]; |
| 1948 | else { |
| 1949 | keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size); |
| 1950 | if (keys == NULL) |
| 1951 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | } |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1953 | |
| 1954 | for (i = 0; i < saved_ob_size ; i++) { |
| 1955 | keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i], |
| 1956 | NULL); |
| 1957 | if (keys[i] == NULL) { |
| 1958 | for (i=i-1 ; i>=0 ; i--) |
| 1959 | Py_DECREF(keys[i]); |
Daniel Stutzbach | 8eda5f7 | 2011-03-02 23:37:50 +0000 | [diff] [blame] | 1960 | if (keys != &ms.temparray[saved_ob_size+1]) |
| 1961 | PyMem_FREE(keys); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1962 | goto keyfunc_fail; |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | lo.keys = keys; |
| 1967 | lo.values = saved_ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1968 | } |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 1969 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1970 | merge_init(&ms, saved_ob_size, keys != NULL); |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 1971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 | nremaining = saved_ob_size; |
| 1973 | if (nremaining < 2) |
| 1974 | goto succeed; |
Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 1975 | |
Benjamin Peterson | 0538064 | 2010-08-23 19:35:39 +0000 | [diff] [blame] | 1976 | /* Reverse sort stability achieved by initially reversing the list, |
| 1977 | applying a stable forward sort, then reversing the final result. */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1978 | if (reverse) { |
| 1979 | if (keys != NULL) |
| 1980 | reverse_slice(&keys[0], &keys[saved_ob_size]); |
| 1981 | reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]); |
| 1982 | } |
Benjamin Peterson | 0538064 | 2010-08-23 19:35:39 +0000 | [diff] [blame] | 1983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | /* March over the array once, left to right, finding natural runs, |
| 1985 | * and extending short natural runs to minrun elements. |
| 1986 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | minrun = merge_compute_minrun(nremaining); |
| 1988 | do { |
| 1989 | int descending; |
| 1990 | Py_ssize_t n; |
Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 1991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | /* Identify next run. */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1993 | n = count_run(lo.keys, lo.keys + nremaining, &descending); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | if (n < 0) |
| 1995 | goto fail; |
| 1996 | if (descending) |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 1997 | reverse_sortslice(&lo, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | /* If short, extend to min(minrun, nremaining). */ |
| 1999 | if (n < minrun) { |
| 2000 | const Py_ssize_t force = nremaining <= minrun ? |
| 2001 | nremaining : minrun; |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 2002 | if (binarysort(lo, lo.keys + force, lo.keys + n) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | goto fail; |
| 2004 | n = force; |
| 2005 | } |
| 2006 | /* Push run onto pending-runs stack, and maybe merge. */ |
| 2007 | assert(ms.n < MAX_MERGE_PENDING); |
| 2008 | ms.pending[ms.n].base = lo; |
| 2009 | ms.pending[ms.n].len = n; |
| 2010 | ++ms.n; |
| 2011 | if (merge_collapse(&ms) < 0) |
| 2012 | goto fail; |
| 2013 | /* Advance to find next run. */ |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 2014 | sortslice_advance(&lo, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | nremaining -= n; |
| 2016 | } while (nremaining); |
Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | if (merge_force_collapse(&ms) < 0) |
| 2019 | goto fail; |
| 2020 | assert(ms.n == 1); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 2021 | assert(keys == NULL |
| 2022 | ? ms.pending[0].base.keys == saved_ob_item |
| 2023 | : ms.pending[0].base.keys == &keys[0]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | assert(ms.pending[0].len == saved_ob_size); |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 2025 | lo = ms.pending[0].base; |
Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 2026 | |
| 2027 | succeed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | result = Py_None; |
Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2029 | fail: |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 2030 | if (keys != NULL) { |
| 2031 | for (i = 0; i < saved_ob_size; i++) |
| 2032 | Py_DECREF(keys[i]); |
| 2033 | if (keys != &ms.temparray[saved_ob_size+1]) |
| 2034 | PyMem_FREE(keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | } |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 2036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | if (self->allocated != -1 && result != NULL) { |
| 2038 | /* The user mucked with the list during the sort, |
| 2039 | * and we don't already have another error to report. |
| 2040 | */ |
| 2041 | PyErr_SetString(PyExc_ValueError, "list modified during sort"); |
| 2042 | result = NULL; |
| 2043 | } |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 2044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | if (reverse && saved_ob_size > 1) |
| 2046 | reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size); |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 2047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | merge_freemem(&ms); |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 2049 | |
Daniel Stutzbach | 9833822 | 2010-12-02 21:55:33 +0000 | [diff] [blame] | 2050 | keyfunc_fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | final_ob_item = self->ob_item; |
| 2052 | i = Py_SIZE(self); |
| 2053 | Py_SIZE(self) = saved_ob_size; |
| 2054 | self->ob_item = saved_ob_item; |
| 2055 | self->allocated = saved_allocated; |
| 2056 | if (final_ob_item != NULL) { |
| 2057 | /* we cannot use list_clear() for this because it does not |
| 2058 | guarantee that the list is really empty when it returns */ |
| 2059 | while (--i >= 0) { |
| 2060 | Py_XDECREF(final_ob_item[i]); |
| 2061 | } |
| 2062 | PyMem_FREE(final_ob_item); |
| 2063 | } |
| 2064 | Py_XINCREF(result); |
| 2065 | return result; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 2066 | } |
Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2067 | #undef IFLT |
Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 2068 | #undef ISLT |
Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2069 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2070 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 2071 | PyList_Sort(PyObject *v) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | if (v == NULL || !PyList_Check(v)) { |
| 2074 | PyErr_BadInternalCall(); |
| 2075 | return -1; |
| 2076 | } |
| 2077 | v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL); |
| 2078 | if (v == NULL) |
| 2079 | return -1; |
| 2080 | Py_DECREF(v); |
| 2081 | return 0; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Guido van Rossum | b86c549 | 2001-02-12 22:06:02 +0000 | [diff] [blame] | 2084 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2085 | listreverse(PyListObject *self) |
Guido van Rossum | b86c549 | 2001-02-12 22:06:02 +0000 | [diff] [blame] | 2086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | if (Py_SIZE(self) > 1) |
| 2088 | reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self)); |
| 2089 | Py_RETURN_NONE; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 2092 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 2093 | PyList_Reverse(PyObject *v) |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 2094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | PyListObject *self = (PyListObject *)v; |
Tim Peters | 6063e26 | 2002-08-08 01:06:39 +0000 | [diff] [blame] | 2096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | if (v == NULL || !PyList_Check(v)) { |
| 2098 | PyErr_BadInternalCall(); |
| 2099 | return -1; |
| 2100 | } |
| 2101 | if (Py_SIZE(self) > 1) |
| 2102 | reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self)); |
| 2103 | return 0; |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2106 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 2107 | PyList_AsTuple(PyObject *v) |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | PyObject *w; |
| 2110 | PyObject **p, **q; |
| 2111 | Py_ssize_t n; |
| 2112 | if (v == NULL || !PyList_Check(v)) { |
| 2113 | PyErr_BadInternalCall(); |
| 2114 | return NULL; |
| 2115 | } |
| 2116 | n = Py_SIZE(v); |
| 2117 | w = PyTuple_New(n); |
| 2118 | if (w == NULL) |
| 2119 | return NULL; |
| 2120 | p = ((PyTupleObject *)w)->ob_item; |
| 2121 | q = ((PyListObject *)v)->ob_item; |
| 2122 | while (--n >= 0) { |
| 2123 | Py_INCREF(*q); |
| 2124 | *p = *q; |
| 2125 | p++; |
| 2126 | q++; |
| 2127 | } |
| 2128 | return w; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2131 | static PyObject * |
Raymond Hettinger | d05abde | 2003-06-17 05:05:49 +0000 | [diff] [blame] | 2132 | listindex(PyListObject *self, PyObject *args) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | Py_ssize_t i, start=0, stop=Py_SIZE(self); |
| 2135 | PyObject *v, *format_tuple, *err_string; |
| 2136 | static PyObject *err_format = NULL; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 2137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2138 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, |
| 2139 | _PyEval_SliceIndex, &start, |
| 2140 | _PyEval_SliceIndex, &stop)) |
| 2141 | return NULL; |
| 2142 | if (start < 0) { |
| 2143 | start += Py_SIZE(self); |
| 2144 | if (start < 0) |
| 2145 | start = 0; |
| 2146 | } |
| 2147 | if (stop < 0) { |
| 2148 | stop += Py_SIZE(self); |
| 2149 | if (stop < 0) |
| 2150 | stop = 0; |
| 2151 | } |
| 2152 | for (i = start; i < stop && i < Py_SIZE(self); i++) { |
| 2153 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 2154 | if (cmp > 0) |
| 2155 | return PyLong_FromSsize_t(i); |
| 2156 | else if (cmp < 0) |
| 2157 | return NULL; |
| 2158 | } |
| 2159 | if (err_format == NULL) { |
| 2160 | err_format = PyUnicode_FromString("%r is not in list"); |
| 2161 | if (err_format == NULL) |
| 2162 | return NULL; |
| 2163 | } |
| 2164 | format_tuple = PyTuple_Pack(1, v); |
| 2165 | if (format_tuple == NULL) |
| 2166 | return NULL; |
| 2167 | err_string = PyUnicode_Format(err_format, format_tuple); |
| 2168 | Py_DECREF(format_tuple); |
| 2169 | if (err_string == NULL) |
| 2170 | return NULL; |
| 2171 | PyErr_SetObject(PyExc_ValueError, err_string); |
| 2172 | Py_DECREF(err_string); |
| 2173 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2176 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2177 | listcount(PyListObject *self, PyObject *v) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 2178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | Py_ssize_t count = 0; |
| 2180 | Py_ssize_t i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 2181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2182 | for (i = 0; i < Py_SIZE(self); i++) { |
| 2183 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 2184 | if (cmp > 0) |
| 2185 | count++; |
| 2186 | else if (cmp < 0) |
| 2187 | return NULL; |
| 2188 | } |
| 2189 | return PyLong_FromSsize_t(count); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2192 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2193 | listremove(PyListObject *self, PyObject *v) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | Py_ssize_t i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | for (i = 0; i < Py_SIZE(self); i++) { |
| 2198 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 2199 | if (cmp > 0) { |
| 2200 | if (list_ass_slice(self, i, i+1, |
| 2201 | (PyObject *)NULL) == 0) |
| 2202 | Py_RETURN_NONE; |
| 2203 | return NULL; |
| 2204 | } |
| 2205 | else if (cmp < 0) |
| 2206 | return NULL; |
| 2207 | } |
| 2208 | PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); |
| 2209 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2212 | static int |
| 2213 | list_traverse(PyListObject *o, visitproc visit, void *arg) |
| 2214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2215 | Py_ssize_t i; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | for (i = Py_SIZE(o); --i >= 0; ) |
| 2218 | Py_VISIT(o->ob_item[i]); |
| 2219 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2222 | static PyObject * |
| 2223 | list_richcompare(PyObject *v, PyObject *w, int op) |
| 2224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | PyListObject *vl, *wl; |
| 2226 | Py_ssize_t i; |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | if (!PyList_Check(v) || !PyList_Check(w)) { |
| 2229 | Py_INCREF(Py_NotImplemented); |
| 2230 | return Py_NotImplemented; |
| 2231 | } |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | vl = (PyListObject *)v; |
| 2234 | wl = (PyListObject *)w; |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) { |
| 2237 | /* Shortcut: if the lengths differ, the lists differ */ |
| 2238 | PyObject *res; |
| 2239 | if (op == Py_EQ) |
| 2240 | res = Py_False; |
| 2241 | else |
| 2242 | res = Py_True; |
| 2243 | Py_INCREF(res); |
| 2244 | return res; |
| 2245 | } |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | /* Search for the first index where items are different */ |
| 2248 | for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) { |
| 2249 | int k = PyObject_RichCompareBool(vl->ob_item[i], |
| 2250 | wl->ob_item[i], Py_EQ); |
| 2251 | if (k < 0) |
| 2252 | return NULL; |
| 2253 | if (!k) |
| 2254 | break; |
| 2255 | } |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) { |
| 2258 | /* No more items to compare -- compare sizes */ |
| 2259 | Py_ssize_t vs = Py_SIZE(vl); |
| 2260 | Py_ssize_t ws = Py_SIZE(wl); |
| 2261 | int cmp; |
| 2262 | PyObject *res; |
| 2263 | switch (op) { |
| 2264 | case Py_LT: cmp = vs < ws; break; |
| 2265 | case Py_LE: cmp = vs <= ws; break; |
| 2266 | case Py_EQ: cmp = vs == ws; break; |
| 2267 | case Py_NE: cmp = vs != ws; break; |
| 2268 | case Py_GT: cmp = vs > ws; break; |
| 2269 | case Py_GE: cmp = vs >= ws; break; |
| 2270 | default: return NULL; /* cannot happen */ |
| 2271 | } |
| 2272 | if (cmp) |
| 2273 | res = Py_True; |
| 2274 | else |
| 2275 | res = Py_False; |
| 2276 | Py_INCREF(res); |
| 2277 | return res; |
| 2278 | } |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | /* We have an item that differs -- shortcuts for EQ/NE */ |
| 2281 | if (op == Py_EQ) { |
| 2282 | Py_INCREF(Py_False); |
| 2283 | return Py_False; |
| 2284 | } |
| 2285 | if (op == Py_NE) { |
| 2286 | Py_INCREF(Py_True); |
| 2287 | return Py_True; |
| 2288 | } |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | /* Compare the final item again using the proper operator */ |
| 2291 | return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op); |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2294 | static int |
| 2295 | list_init(PyListObject *self, PyObject *args, PyObject *kw) |
| 2296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | PyObject *arg = NULL; |
| 2298 | static char *kwlist[] = {"sequence", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg)) |
| 2301 | return -1; |
Raymond Hettinger | c0aaa2d | 2004-07-29 23:31:29 +0000 | [diff] [blame] | 2302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2303 | /* Verify list invariants established by PyType_GenericAlloc() */ |
| 2304 | assert(0 <= Py_SIZE(self)); |
| 2305 | assert(Py_SIZE(self) <= self->allocated || self->allocated == -1); |
| 2306 | assert(self->ob_item != NULL || |
| 2307 | self->allocated == 0 || self->allocated == -1); |
Raymond Hettinger | c0aaa2d | 2004-07-29 23:31:29 +0000 | [diff] [blame] | 2308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | /* Empty previous contents */ |
| 2310 | if (self->ob_item != NULL) { |
| 2311 | (void)list_clear(self); |
| 2312 | } |
| 2313 | if (arg != NULL) { |
| 2314 | PyObject *rv = listextend(self, arg); |
| 2315 | if (rv == NULL) |
| 2316 | return -1; |
| 2317 | Py_DECREF(rv); |
| 2318 | } |
| 2319 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2322 | static PyObject * |
| 2323 | list_sizeof(PyListObject *self) |
| 2324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | Py_ssize_t res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2327 | res = sizeof(PyListObject) + self->allocated * sizeof(void*); |
| 2328 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2331 | static PyObject *list_iter(PyObject *seq); |
| 2332 | static PyObject *list_reversed(PyListObject* seq, PyObject* unused); |
| 2333 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2334 | PyDoc_STRVAR(getitem_doc, |
| 2335 | "x.__getitem__(y) <==> x[y]"); |
Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2336 | PyDoc_STRVAR(reversed_doc, |
| 2337 | "L.__reversed__() -- return a reverse iterator over the list"); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2338 | PyDoc_STRVAR(sizeof_doc, |
| 2339 | "L.__sizeof__() -- size of L in memory, in bytes"); |
Eli Bendersky | cbbaa96 | 2011-02-25 05:47:53 +0000 | [diff] [blame] | 2340 | PyDoc_STRVAR(clear_doc, |
| 2341 | "L.clear() -> None -- remove all items from L"); |
| 2342 | PyDoc_STRVAR(copy_doc, |
| 2343 | "L.copy() -> list -- a shallow copy of L"); |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2344 | PyDoc_STRVAR(append_doc, |
| 2345 | "L.append(object) -- append object to end"); |
| 2346 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | f8bcfb1 | 2002-12-29 05:49:09 +0000 | [diff] [blame] | 2347 | "L.extend(iterable) -- extend list by appending elements from the iterable"); |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2348 | PyDoc_STRVAR(insert_doc, |
| 2349 | "L.insert(index, object) -- insert object before index"); |
| 2350 | PyDoc_STRVAR(pop_doc, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2351 | "L.pop([index]) -> item -- remove and return item at index (default last).\n" |
| 2352 | "Raises IndexError if list is empty or index is out of range."); |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2353 | PyDoc_STRVAR(remove_doc, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2354 | "L.remove(value) -- remove first occurrence of value.\n" |
| 2355 | "Raises ValueError if the value is not present."); |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2356 | PyDoc_STRVAR(index_doc, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2357 | "L.index(value, [start, [stop]]) -> integer -- return first index of value.\n" |
| 2358 | "Raises ValueError if the value is not present."); |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2359 | PyDoc_STRVAR(count_doc, |
| 2360 | "L.count(value) -> integer -- return number of occurrences of value"); |
| 2361 | PyDoc_STRVAR(reverse_doc, |
| 2362 | "L.reverse() -- reverse *IN PLACE*"); |
| 2363 | PyDoc_STRVAR(sort_doc, |
Benjamin Peterson | cb9a551 | 2008-09-30 02:08:36 +0000 | [diff] [blame] | 2364 | "L.sort(key=None, reverse=False) -- stable sort *IN PLACE*"); |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 2365 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2366 | static PyObject *list_subscript(PyListObject*, PyObject*); |
| 2367 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2368 | static PyMethodDef list_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2369 | {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc}, |
| 2370 | {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc}, |
| 2371 | {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc}, |
Eli Bendersky | cbbaa96 | 2011-02-25 05:47:53 +0000 | [diff] [blame] | 2372 | {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc}, |
| 2373 | {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | {"append", (PyCFunction)listappend, METH_O, append_doc}, |
| 2375 | {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, |
Eli Bendersky | cbbaa96 | 2011-02-25 05:47:53 +0000 | [diff] [blame] | 2376 | {"extend", (PyCFunction)listextend, METH_O, extend_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc}, |
| 2378 | {"remove", (PyCFunction)listremove, METH_O, remove_doc}, |
| 2379 | {"index", (PyCFunction)listindex, METH_VARARGS, index_doc}, |
| 2380 | {"count", (PyCFunction)listcount, METH_O, count_doc}, |
| 2381 | {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc}, |
| 2382 | {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc}, |
| 2383 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2384 | }; |
| 2385 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2386 | static PySequenceMethods list_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | (lenfunc)list_length, /* sq_length */ |
| 2388 | (binaryfunc)list_concat, /* sq_concat */ |
| 2389 | (ssizeargfunc)list_repeat, /* sq_repeat */ |
| 2390 | (ssizeargfunc)list_item, /* sq_item */ |
| 2391 | 0, /* sq_slice */ |
| 2392 | (ssizeobjargproc)list_ass_item, /* sq_ass_item */ |
| 2393 | 0, /* sq_ass_slice */ |
| 2394 | (objobjproc)list_contains, /* sq_contains */ |
| 2395 | (binaryfunc)list_inplace_concat, /* sq_inplace_concat */ |
| 2396 | (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2397 | }; |
| 2398 | |
Neal Norwitz | 2c2e827 | 2002-06-14 02:04:18 +0000 | [diff] [blame] | 2399 | PyDoc_STRVAR(list_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2400 | "list() -> new empty list\n" |
| 2401 | "list(iterable) -> new list initialized from iterable's items"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2402 | |
Jeremy Hylton | a4b4c3b | 2002-07-13 03:51:17 +0000 | [diff] [blame] | 2403 | static PyObject * |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2404 | list_subscript(PyListObject* self, PyObject* item) |
| 2405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | if (PyIndex_Check(item)) { |
| 2407 | Py_ssize_t i; |
| 2408 | i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 2409 | if (i == -1 && PyErr_Occurred()) |
| 2410 | return NULL; |
| 2411 | if (i < 0) |
| 2412 | i += PyList_GET_SIZE(self); |
| 2413 | return list_item(self, i); |
| 2414 | } |
| 2415 | else if (PySlice_Check(item)) { |
| 2416 | Py_ssize_t start, stop, step, slicelength, cur, i; |
| 2417 | PyObject* result; |
| 2418 | PyObject* it; |
| 2419 | PyObject **src, **dest; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2420 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2421 | if (PySlice_GetIndicesEx(item, Py_SIZE(self), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | &start, &stop, &step, &slicelength) < 0) { |
| 2423 | return NULL; |
| 2424 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2426 | if (slicelength <= 0) { |
| 2427 | return PyList_New(0); |
| 2428 | } |
| 2429 | else if (step == 1) { |
| 2430 | return list_slice(self, start, stop); |
| 2431 | } |
| 2432 | else { |
| 2433 | result = PyList_New(slicelength); |
| 2434 | if (!result) return NULL; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | src = self->ob_item; |
| 2437 | dest = ((PyListObject *)result)->ob_item; |
| 2438 | for (cur = start, i = 0; i < slicelength; |
| 2439 | cur += step, i++) { |
| 2440 | it = src[cur]; |
| 2441 | Py_INCREF(it); |
| 2442 | dest[i] = it; |
| 2443 | } |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | return result; |
| 2446 | } |
| 2447 | } |
| 2448 | else { |
| 2449 | PyErr_Format(PyExc_TypeError, |
| 2450 | "list indices must be integers, not %.200s", |
| 2451 | item->ob_type->tp_name); |
| 2452 | return NULL; |
| 2453 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2456 | static int |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2457 | list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) |
| 2458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | if (PyIndex_Check(item)) { |
| 2460 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 2461 | if (i == -1 && PyErr_Occurred()) |
| 2462 | return -1; |
| 2463 | if (i < 0) |
| 2464 | i += PyList_GET_SIZE(self); |
| 2465 | return list_ass_item(self, i, value); |
| 2466 | } |
| 2467 | else if (PySlice_Check(item)) { |
| 2468 | Py_ssize_t start, stop, step, slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2469 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2470 | if (PySlice_GetIndicesEx(item, Py_SIZE(self), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | &start, &stop, &step, &slicelength) < 0) { |
| 2472 | return -1; |
| 2473 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | if (step == 1) |
| 2476 | return list_ass_slice(self, start, stop, value); |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | /* Make sure s[5:2] = [..] inserts at the right place: |
| 2479 | before 5, not before 2. */ |
| 2480 | if ((step < 0 && start < stop) || |
| 2481 | (step > 0 && start > stop)) |
| 2482 | stop = start; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 2483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 | if (value == NULL) { |
| 2485 | /* delete slice */ |
| 2486 | PyObject **garbage; |
| 2487 | size_t cur; |
| 2488 | Py_ssize_t i; |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2490 | if (slicelength <= 0) |
| 2491 | return 0; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | if (step < 0) { |
| 2494 | stop = start + 1; |
| 2495 | start = stop + step*(slicelength - 1) - 1; |
| 2496 | step = -step; |
| 2497 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2499 | assert((size_t)slicelength <= |
| 2500 | PY_SIZE_MAX / sizeof(PyObject*)); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 2501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | garbage = (PyObject**) |
| 2503 | PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
| 2504 | if (!garbage) { |
| 2505 | PyErr_NoMemory(); |
| 2506 | return -1; |
| 2507 | } |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | /* drawing pictures might help understand these for |
| 2510 | loops. Basically, we memmove the parts of the |
| 2511 | list that are *not* part of the slice: step-1 |
| 2512 | items for each item that is part of the slice, |
| 2513 | and then tail end of the list that was not |
| 2514 | covered by the slice */ |
| 2515 | for (cur = start, i = 0; |
| 2516 | cur < (size_t)stop; |
| 2517 | cur += step, i++) { |
| 2518 | Py_ssize_t lim = step - 1; |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 2519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2520 | garbage[i] = PyList_GET_ITEM(self, cur); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | if (cur + step >= (size_t)Py_SIZE(self)) { |
| 2523 | lim = Py_SIZE(self) - cur - 1; |
| 2524 | } |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | memmove(self->ob_item + cur - i, |
| 2527 | self->ob_item + cur + 1, |
| 2528 | lim * sizeof(PyObject *)); |
| 2529 | } |
| 2530 | cur = start + slicelength*step; |
| 2531 | if (cur < (size_t)Py_SIZE(self)) { |
| 2532 | memmove(self->ob_item + cur - slicelength, |
| 2533 | self->ob_item + cur, |
| 2534 | (Py_SIZE(self) - cur) * |
| 2535 | sizeof(PyObject *)); |
| 2536 | } |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | Py_SIZE(self) -= slicelength; |
| 2539 | list_resize(self, Py_SIZE(self)); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | for (i = 0; i < slicelength; i++) { |
| 2542 | Py_DECREF(garbage[i]); |
| 2543 | } |
| 2544 | PyMem_FREE(garbage); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2546 | return 0; |
| 2547 | } |
| 2548 | else { |
| 2549 | /* assign slice */ |
| 2550 | PyObject *ins, *seq; |
| 2551 | PyObject **garbage, **seqitems, **selfitems; |
| 2552 | Py_ssize_t cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | /* protect against a[::-1] = a */ |
| 2555 | if (self == (PyListObject*)value) { |
| 2556 | seq = list_slice((PyListObject*)value, 0, |
| 2557 | PyList_GET_SIZE(value)); |
| 2558 | } |
| 2559 | else { |
| 2560 | seq = PySequence_Fast(value, |
| 2561 | "must assign iterable " |
| 2562 | "to extended slice"); |
| 2563 | } |
| 2564 | if (!seq) |
| 2565 | return -1; |
Michael W. Hudson | a69c030 | 2002-12-05 21:32:32 +0000 | [diff] [blame] | 2566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | if (PySequence_Fast_GET_SIZE(seq) != slicelength) { |
| 2568 | PyErr_Format(PyExc_ValueError, |
| 2569 | "attempt to assign sequence of " |
| 2570 | "size %zd to extended slice of " |
| 2571 | "size %zd", |
| 2572 | PySequence_Fast_GET_SIZE(seq), |
| 2573 | slicelength); |
| 2574 | Py_DECREF(seq); |
| 2575 | return -1; |
| 2576 | } |
Michael W. Hudson | a69c030 | 2002-12-05 21:32:32 +0000 | [diff] [blame] | 2577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | if (!slicelength) { |
| 2579 | Py_DECREF(seq); |
| 2580 | return 0; |
| 2581 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | garbage = (PyObject**) |
| 2584 | PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
| 2585 | if (!garbage) { |
| 2586 | Py_DECREF(seq); |
| 2587 | PyErr_NoMemory(); |
| 2588 | return -1; |
| 2589 | } |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | selfitems = self->ob_item; |
| 2592 | seqitems = PySequence_Fast_ITEMS(seq); |
| 2593 | for (cur = start, i = 0; i < slicelength; |
| 2594 | cur += step, i++) { |
| 2595 | garbage[i] = selfitems[cur]; |
| 2596 | ins = seqitems[i]; |
| 2597 | Py_INCREF(ins); |
| 2598 | selfitems[cur] = ins; |
| 2599 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2601 | for (i = 0; i < slicelength; i++) { |
| 2602 | Py_DECREF(garbage[i]); |
| 2603 | } |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | PyMem_FREE(garbage); |
| 2606 | Py_DECREF(seq); |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | return 0; |
| 2609 | } |
| 2610 | } |
| 2611 | else { |
| 2612 | PyErr_Format(PyExc_TypeError, |
| 2613 | "list indices must be integers, not %.200s", |
| 2614 | item->ob_type->tp_name); |
| 2615 | return -1; |
| 2616 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | static PyMappingMethods list_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | (lenfunc)list_length, |
| 2621 | (binaryfunc)list_subscript, |
| 2622 | (objobjargproc)list_ass_subscript |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2623 | }; |
| 2624 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2625 | PyTypeObject PyList_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2627 | "list", |
| 2628 | sizeof(PyListObject), |
| 2629 | 0, |
| 2630 | (destructor)list_dealloc, /* tp_dealloc */ |
| 2631 | 0, /* tp_print */ |
| 2632 | 0, /* tp_getattr */ |
| 2633 | 0, /* tp_setattr */ |
| 2634 | 0, /* tp_reserved */ |
| 2635 | (reprfunc)list_repr, /* tp_repr */ |
| 2636 | 0, /* tp_as_number */ |
| 2637 | &list_as_sequence, /* tp_as_sequence */ |
| 2638 | &list_as_mapping, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2639 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | 0, /* tp_call */ |
| 2641 | 0, /* tp_str */ |
| 2642 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2643 | 0, /* tp_setattro */ |
| 2644 | 0, /* tp_as_buffer */ |
| 2645 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2646 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */ |
| 2647 | list_doc, /* tp_doc */ |
| 2648 | (traverseproc)list_traverse, /* tp_traverse */ |
| 2649 | (inquiry)list_clear, /* tp_clear */ |
| 2650 | list_richcompare, /* tp_richcompare */ |
| 2651 | 0, /* tp_weaklistoffset */ |
| 2652 | list_iter, /* tp_iter */ |
| 2653 | 0, /* tp_iternext */ |
| 2654 | list_methods, /* tp_methods */ |
| 2655 | 0, /* tp_members */ |
| 2656 | 0, /* tp_getset */ |
| 2657 | 0, /* tp_base */ |
| 2658 | 0, /* tp_dict */ |
| 2659 | 0, /* tp_descr_get */ |
| 2660 | 0, /* tp_descr_set */ |
| 2661 | 0, /* tp_dictoffset */ |
| 2662 | (initproc)list_init, /* tp_init */ |
| 2663 | PyType_GenericAlloc, /* tp_alloc */ |
| 2664 | PyType_GenericNew, /* tp_new */ |
| 2665 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2666 | }; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2667 | |
| 2668 | |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2669 | /*********************** List Iterator **************************/ |
| 2670 | |
| 2671 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | PyObject_HEAD |
| 2673 | long it_index; |
| 2674 | PyListObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2675 | } listiterobject; |
| 2676 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2677 | static PyObject *list_iter(PyObject *); |
| 2678 | static void listiter_dealloc(listiterobject *); |
| 2679 | static int listiter_traverse(listiterobject *, visitproc, void *); |
| 2680 | static PyObject *listiter_next(listiterobject *); |
| 2681 | static PyObject *listiter_len(listiterobject *); |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 2682 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2683 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2684 | |
| 2685 | static PyMethodDef listiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc}, |
| 2687 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 2688 | }; |
| 2689 | |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2690 | PyTypeObject PyListIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2692 | "list_iterator", /* tp_name */ |
| 2693 | sizeof(listiterobject), /* tp_basicsize */ |
| 2694 | 0, /* tp_itemsize */ |
| 2695 | /* methods */ |
| 2696 | (destructor)listiter_dealloc, /* tp_dealloc */ |
| 2697 | 0, /* tp_print */ |
| 2698 | 0, /* tp_getattr */ |
| 2699 | 0, /* tp_setattr */ |
| 2700 | 0, /* tp_reserved */ |
| 2701 | 0, /* tp_repr */ |
| 2702 | 0, /* tp_as_number */ |
| 2703 | 0, /* tp_as_sequence */ |
| 2704 | 0, /* tp_as_mapping */ |
| 2705 | 0, /* tp_hash */ |
| 2706 | 0, /* tp_call */ |
| 2707 | 0, /* tp_str */ |
| 2708 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2709 | 0, /* tp_setattro */ |
| 2710 | 0, /* tp_as_buffer */ |
| 2711 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2712 | 0, /* tp_doc */ |
| 2713 | (traverseproc)listiter_traverse, /* tp_traverse */ |
| 2714 | 0, /* tp_clear */ |
| 2715 | 0, /* tp_richcompare */ |
| 2716 | 0, /* tp_weaklistoffset */ |
| 2717 | PyObject_SelfIter, /* tp_iter */ |
| 2718 | (iternextfunc)listiter_next, /* tp_iternext */ |
| 2719 | listiter_methods, /* tp_methods */ |
| 2720 | 0, /* tp_members */ |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2721 | }; |
Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2722 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2723 | |
| 2724 | static PyObject * |
| 2725 | list_iter(PyObject *seq) |
| 2726 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | listiterobject *it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | if (!PyList_Check(seq)) { |
| 2730 | PyErr_BadInternalCall(); |
| 2731 | return NULL; |
| 2732 | } |
| 2733 | it = PyObject_GC_New(listiterobject, &PyListIter_Type); |
| 2734 | if (it == NULL) |
| 2735 | return NULL; |
| 2736 | it->it_index = 0; |
| 2737 | Py_INCREF(seq); |
| 2738 | it->it_seq = (PyListObject *)seq; |
| 2739 | _PyObject_GC_TRACK(it); |
| 2740 | return (PyObject *)it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | static void |
| 2744 | listiter_dealloc(listiterobject *it) |
| 2745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | _PyObject_GC_UNTRACK(it); |
| 2747 | Py_XDECREF(it->it_seq); |
| 2748 | PyObject_GC_Del(it); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | static int |
| 2752 | listiter_traverse(listiterobject *it, visitproc visit, void *arg) |
| 2753 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | Py_VISIT(it->it_seq); |
| 2755 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | static PyObject * |
| 2759 | listiter_next(listiterobject *it) |
| 2760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | PyListObject *seq; |
| 2762 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | assert(it != NULL); |
| 2765 | seq = it->it_seq; |
| 2766 | if (seq == NULL) |
| 2767 | return NULL; |
| 2768 | assert(PyList_Check(seq)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | if (it->it_index < PyList_GET_SIZE(seq)) { |
| 2771 | item = PyList_GET_ITEM(seq, it->it_index); |
| 2772 | ++it->it_index; |
| 2773 | Py_INCREF(item); |
| 2774 | return item; |
| 2775 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | Py_DECREF(seq); |
| 2778 | it->it_seq = NULL; |
| 2779 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | static PyObject * |
| 2783 | listiter_len(listiterobject *it) |
| 2784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | Py_ssize_t len; |
| 2786 | if (it->it_seq) { |
| 2787 | len = PyList_GET_SIZE(it->it_seq) - it->it_index; |
| 2788 | if (len >= 0) |
| 2789 | return PyLong_FromSsize_t(len); |
| 2790 | } |
| 2791 | return PyLong_FromLong(0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2792 | } |
Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2793 | /*********************** List Reverse Iterator **************************/ |
| 2794 | |
| 2795 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | PyObject_HEAD |
| 2797 | Py_ssize_t it_index; |
| 2798 | PyListObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2799 | } listreviterobject; |
| 2800 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2801 | static PyObject *list_reversed(PyListObject *, PyObject *); |
| 2802 | static void listreviter_dealloc(listreviterobject *); |
| 2803 | static int listreviter_traverse(listreviterobject *, visitproc, void *); |
| 2804 | static PyObject *listreviter_next(listreviterobject *); |
Raymond Hettinger | f5b6411 | 2008-12-02 21:33:45 +0000 | [diff] [blame] | 2805 | static PyObject *listreviter_len(listreviterobject *); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2806 | |
Raymond Hettinger | f5b6411 | 2008-12-02 21:33:45 +0000 | [diff] [blame] | 2807 | static PyMethodDef listreviter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc}, |
| 2809 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2810 | }; |
| 2811 | |
Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2812 | PyTypeObject PyListRevIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2814 | "list_reverseiterator", /* tp_name */ |
| 2815 | sizeof(listreviterobject), /* tp_basicsize */ |
| 2816 | 0, /* tp_itemsize */ |
| 2817 | /* methods */ |
| 2818 | (destructor)listreviter_dealloc, /* tp_dealloc */ |
| 2819 | 0, /* tp_print */ |
| 2820 | 0, /* tp_getattr */ |
| 2821 | 0, /* tp_setattr */ |
| 2822 | 0, /* tp_reserved */ |
| 2823 | 0, /* tp_repr */ |
| 2824 | 0, /* tp_as_number */ |
| 2825 | 0, /* tp_as_sequence */ |
| 2826 | 0, /* tp_as_mapping */ |
| 2827 | 0, /* tp_hash */ |
| 2828 | 0, /* tp_call */ |
| 2829 | 0, /* tp_str */ |
| 2830 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2831 | 0, /* tp_setattro */ |
| 2832 | 0, /* tp_as_buffer */ |
| 2833 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2834 | 0, /* tp_doc */ |
| 2835 | (traverseproc)listreviter_traverse, /* tp_traverse */ |
| 2836 | 0, /* tp_clear */ |
| 2837 | 0, /* tp_richcompare */ |
| 2838 | 0, /* tp_weaklistoffset */ |
| 2839 | PyObject_SelfIter, /* tp_iter */ |
| 2840 | (iternextfunc)listreviter_next, /* tp_iternext */ |
| 2841 | listreviter_methods, /* tp_methods */ |
| 2842 | 0, |
Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2843 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2844 | |
| 2845 | static PyObject * |
| 2846 | list_reversed(PyListObject *seq, PyObject *unused) |
| 2847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | listreviterobject *it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type); |
| 2851 | if (it == NULL) |
| 2852 | return NULL; |
| 2853 | assert(PyList_Check(seq)); |
| 2854 | it->it_index = PyList_GET_SIZE(seq) - 1; |
| 2855 | Py_INCREF(seq); |
| 2856 | it->it_seq = seq; |
| 2857 | PyObject_GC_Track(it); |
| 2858 | return (PyObject *)it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | static void |
| 2862 | listreviter_dealloc(listreviterobject *it) |
| 2863 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | PyObject_GC_UnTrack(it); |
| 2865 | Py_XDECREF(it->it_seq); |
| 2866 | PyObject_GC_Del(it); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | static int |
| 2870 | listreviter_traverse(listreviterobject *it, visitproc visit, void *arg) |
| 2871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | Py_VISIT(it->it_seq); |
| 2873 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | static PyObject * |
| 2877 | listreviter_next(listreviterobject *it) |
| 2878 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | PyObject *item; |
| 2880 | Py_ssize_t index = it->it_index; |
| 2881 | PyListObject *seq = it->it_seq; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | if (index>=0 && index < PyList_GET_SIZE(seq)) { |
| 2884 | item = PyList_GET_ITEM(seq, index); |
| 2885 | it->it_index--; |
| 2886 | Py_INCREF(item); |
| 2887 | return item; |
| 2888 | } |
| 2889 | it->it_index = -1; |
| 2890 | if (seq != NULL) { |
| 2891 | it->it_seq = NULL; |
| 2892 | Py_DECREF(seq); |
| 2893 | } |
| 2894 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Raymond Hettinger | f5b6411 | 2008-12-02 21:33:45 +0000 | [diff] [blame] | 2897 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2898 | listreviter_len(listreviterobject *it) |
| 2899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | Py_ssize_t len = it->it_index + 1; |
| 2901 | if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len) |
| 2902 | len = 0; |
| 2903 | return PyLong_FromSsize_t(len); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2904 | } |