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