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