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