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