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