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