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