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