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