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