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