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