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