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