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