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