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