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