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