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