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