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