Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* List object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
| 5 | |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 6 | #ifdef STDC_HEADERS |
| 7 | #include <stddef.h> |
| 8 | #else |
| 9 | #include <sys/types.h> /* For size_t */ |
| 10 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 12 | #define ROUNDUP(n, PyTryBlock) \ |
| 13 | ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock)) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 16 | roundupsize(int n) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 17 | { |
| 18 | if (n < 500) |
| 19 | return ROUNDUP(n, 10); |
| 20 | else |
| 21 | return ROUNDUP(n, 100); |
| 22 | } |
| 23 | |
Guido van Rossum | a27d112 | 1997-08-25 18:36:23 +0000 | [diff] [blame] | 24 | #define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems)) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 26 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 27 | PyList_New(int size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | { |
| 29 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 30 | PyListObject *op; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 31 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | if (size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 33 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | return NULL; |
| 35 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 36 | nbytes = size * sizeof(PyObject *); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 37 | /* Check for overflow */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | if (nbytes / sizeof(PyObject *) != (size_t)size) { |
| 39 | return PyErr_NoMemory(); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 40 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 41 | /* PyObject_NewVar is inlined */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 42 | op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 43 | + PyGC_HEAD_SIZE); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | if (op == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 47 | op = (PyListObject *) PyObject_FROM_GC(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | if (size <= 0) { |
| 49 | op->ob_item = NULL; |
| 50 | } |
| 51 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 52 | op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | if (op->ob_item == NULL) { |
Vladimir Marangozov | 467a67e | 2000-07-15 03:31:31 +0000 | [diff] [blame] | 54 | PyObject_FREE(PyObject_AS_GC(op)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 58 | PyObject_INIT_VAR(op, &PyList_Type, size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | for (i = 0; i < size; i++) |
| 60 | op->ob_item[i] = NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 61 | PyObject_GC_Init(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 66 | PyList_Size(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 68 | if (!PyList_Check(op)) { |
| 69 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 73 | return ((PyListObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | static PyObject *indexerr; |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 79 | PyList_GetItem(PyObject *op, int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | if (!PyList_Check(op)) { |
| 82 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 86 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 87 | indexerr = PyString_FromString( |
| 88 | "list index out of range"); |
| 89 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 92 | return ((PyListObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 96 | PyList_SetItem(register PyObject *op, register int i, |
| 97 | register PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | register PyObject *olditem; |
| 100 | register PyObject **p; |
| 101 | if (!PyList_Check(op)) { |
| 102 | Py_XDECREF(newitem); |
| 103 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 104 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 106 | if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
| 107 | Py_XDECREF(newitem); |
| 108 | PyErr_SetString(PyExc_IndexError, |
| 109 | "list assignment index out of range"); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 110 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | p = ((PyListObject *)op) -> ob_item + i; |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 113 | olditem = *p; |
| 114 | *p = newitem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | Py_XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 120 | ins1(PyListObject *self, int where, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | { |
| 122 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 123 | PyObject **items; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 124 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 126 | return -1; |
| 127 | } |
Trent Mick | a584664 | 2000-08-13 22:47:45 +0000 | [diff] [blame] | 128 | if (self->ob_size == INT_MAX) { |
| 129 | PyErr_SetString(PyExc_OverflowError, |
| 130 | "cannot add more objects to list"); |
| 131 | return -1; |
| 132 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | items = self->ob_item; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 134 | NRESIZE(items, PyObject *, self->ob_size+1); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 135 | if (items == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 136 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 137 | return -1; |
| 138 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | if (where < 0) |
| 140 | where = 0; |
| 141 | if (where > self->ob_size) |
| 142 | where = self->ob_size; |
| 143 | for (i = self->ob_size; --i >= where; ) |
| 144 | items[i+1] = items[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | items[where] = v; |
| 147 | self->ob_item = items; |
| 148 | self->ob_size++; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 153 | PyList_Insert(PyObject *op, int where, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 155 | if (!PyList_Check(op)) { |
| 156 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 157 | return -1; |
| 158 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 159 | return ins1((PyListObject *)op, where, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 163 | PyList_Append(PyObject *op, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | if (!PyList_Check(op)) { |
| 166 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 167 | return -1; |
| 168 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 169 | return ins1((PyListObject *)op, |
| 170 | (int) ((PyListObject *)op)->ob_size, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* Methods */ |
| 174 | |
| 175 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 176 | list_dealloc(PyListObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | { |
| 178 | int i; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 179 | Py_TRASHCAN_SAFE_BEGIN(op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 180 | PyObject_GC_Fini(op); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 181 | if (op->ob_item != NULL) { |
Guido van Rossum | fa71701 | 1999-06-09 15:19:34 +0000 | [diff] [blame] | 182 | /* Do it backwards, for Christian Tismer. |
| 183 | There's a simple test case where somehow this reduces |
| 184 | thrashing when a *very* large list is created and |
| 185 | immediately deleted. */ |
| 186 | i = op->ob_size; |
| 187 | while (--i >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 188 | Py_XDECREF(op->ob_item[i]); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 189 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 190 | PyMem_FREE(op->ob_item); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 191 | } |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 192 | op = (PyListObject *) PyObject_AS_GC(op); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 193 | PyObject_DEL(op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 194 | Py_TRASHCAN_SAFE_END(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 197 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 198 | list_print(PyListObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 199 | { |
| 200 | int i; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 201 | |
| 202 | i = Py_ReprEnter((PyObject*)op); |
| 203 | if (i != 0) { |
| 204 | if (i < 0) |
| 205 | return i; |
| 206 | fprintf(fp, "[...]"); |
| 207 | return 0; |
| 208 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | fprintf(fp, "["); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 210 | for (i = 0; i < op->ob_size; i++) { |
| 211 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | fprintf(fp, ", "); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 213 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) { |
| 214 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 215 | return -1; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 216 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | } |
| 218 | fprintf(fp, "]"); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 219 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 220 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 223 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 224 | list_repr(PyListObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 226 | PyObject *s, *comma; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 227 | int i; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 228 | |
| 229 | i = Py_ReprEnter((PyObject*)v); |
| 230 | if (i != 0) { |
| 231 | if (i > 0) |
| 232 | return PyString_FromString("[...]"); |
| 233 | return NULL; |
| 234 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 235 | s = PyString_FromString("["); |
| 236 | comma = PyString_FromString(", "); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | for (i = 0; i < v->ob_size && s != NULL; i++) { |
| 238 | if (i > 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 239 | PyString_Concat(&s, comma); |
| 240 | PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | Py_XDECREF(comma); |
| 243 | PyString_ConcatAndDel(&s, PyString_FromString("]")); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 244 | Py_ReprLeave((PyObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | return s; |
| 246 | } |
| 247 | |
| 248 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 249 | list_compare(PyListObject *v, PyListObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 251 | int i; |
Jack Jansen | e979160 | 2000-08-22 21:51:22 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | ae621ff | 1998-05-28 20:18:46 +0000 | [diff] [blame] | 253 | for (i = 0; i < v->ob_size && i < w->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 254 | int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 255 | if (cmp != 0) |
| 256 | return cmp; |
| 257 | } |
| 258 | return v->ob_size - w->ob_size; |
| 259 | } |
| 260 | |
| 261 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 262 | list_length(PyListObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | { |
| 264 | return a->ob_size; |
| 265 | } |
| 266 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 270 | list_contains(PyListObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 271 | { |
| 272 | int i, cmp; |
| 273 | |
| 274 | for (i = 0; i < a->ob_size; ++i) { |
| 275 | cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i)); |
| 276 | if (cmp == 0) |
| 277 | return 1; |
| 278 | if (PyErr_Occurred()) |
| 279 | return -1; |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 285 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 286 | list_item(PyListObject *a, int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | { |
| 288 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 289 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | indexerr = PyString_FromString( |
| 291 | "list index out of range"); |
| 292 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 295 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | return a->ob_item[i]; |
| 297 | } |
| 298 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 300 | list_slice(PyListObject *a, int ilow, int ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | PyListObject *np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | int i; |
| 304 | if (ilow < 0) |
| 305 | ilow = 0; |
| 306 | else if (ilow > a->ob_size) |
| 307 | ilow = a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | if (ihigh < ilow) |
| 309 | ihigh = ilow; |
| 310 | else if (ihigh > a->ob_size) |
| 311 | ihigh = a->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 312 | np = (PyListObject *) PyList_New(ihigh - ilow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | if (np == NULL) |
| 314 | return NULL; |
| 315 | for (i = ilow; i < ihigh; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 316 | PyObject *v = a->ob_item[i]; |
| 317 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | np->ob_item[i - ilow] = v; |
| 319 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 323 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 324 | PyList_GetSlice(PyObject *a, int ilow, int ihigh) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 325 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 326 | if (!PyList_Check(a)) { |
| 327 | PyErr_BadInternalCall(); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 328 | return NULL; |
| 329 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 330 | return list_slice((PyListObject *)a, ilow, ihigh); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 334 | list_concat(PyListObject *a, PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | { |
| 336 | int size; |
| 337 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 338 | PyListObject *np; |
| 339 | if (!PyList_Check(bb)) { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 340 | PyErr_Format(PyExc_TypeError, |
Fred Drake | 914a2ed | 2000-06-01 14:31:03 +0000 | [diff] [blame] | 341 | "can only concatenate list (not \"%.200s\") to list", |
| 342 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 345 | #define b ((PyListObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 347 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | if (np == NULL) { |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 349 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 350 | } |
| 351 | for (i = 0; i < a->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 352 | PyObject *v = a->ob_item[i]; |
| 353 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | np->ob_item[i] = v; |
| 355 | } |
| 356 | for (i = 0; i < b->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 357 | PyObject *v = b->ob_item[i]; |
| 358 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 359 | np->ob_item[i + a->ob_size] = v; |
| 360 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 361 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | #undef b |
| 363 | } |
| 364 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 365 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 366 | list_repeat(PyListObject *a, int n) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 367 | { |
| 368 | int i, j; |
| 369 | int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 370 | PyListObject *np; |
| 371 | PyObject **p; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 372 | if (n < 0) |
| 373 | n = 0; |
| 374 | size = a->ob_size * n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 376 | if (np == NULL) |
| 377 | return NULL; |
| 378 | p = np->ob_item; |
| 379 | for (i = 0; i < n; i++) { |
| 380 | for (j = 0; j < a->ob_size; j++) { |
| 381 | *p = a->ob_item[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 382 | Py_INCREF(*p); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 383 | p++; |
| 384 | } |
| 385 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 386 | return (PyObject *) np; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 389 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 390 | list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 391 | { |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 392 | /* Because [X]DECREF can recursively invoke list operations on |
| 393 | this list, we must postpone all [X]DECREF activity until |
| 394 | after the list is back in its canonical shape. Therefore |
| 395 | we must allocate an additional array, 'recycle', into which |
| 396 | we temporarily copy the items that are deleted from the |
| 397 | list. :-( */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 398 | PyObject **recycle, **p; |
| 399 | PyObject **item; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | int n; /* Size of replacement list */ |
| 401 | int d; /* Change in size */ |
| 402 | int k; /* Loop index */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 403 | #define b ((PyListObject *)v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 404 | if (v == NULL) |
| 405 | n = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 406 | else if (PyList_Check(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 407 | n = b->ob_size; |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 408 | if (a == b) { |
| 409 | /* Special case "a[i:j] = a" -- copy b first */ |
| 410 | int ret; |
| 411 | v = list_slice(b, 0, n); |
| 412 | ret = list_ass_slice(a, ilow, ihigh, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 413 | Py_DECREF(v); |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 414 | return ret; |
| 415 | } |
| 416 | } |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 417 | else { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 418 | PyErr_Format(PyExc_TypeError, |
| 419 | "must assign list (not \"%.200s\") to slice", |
| 420 | v->ob_type->tp_name); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 421 | return -1; |
| 422 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 423 | if (ilow < 0) |
| 424 | ilow = 0; |
| 425 | else if (ilow > a->ob_size) |
| 426 | ilow = a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 427 | if (ihigh < ilow) |
| 428 | ihigh = ilow; |
| 429 | else if (ihigh > a->ob_size) |
| 430 | ihigh = a->ob_size; |
| 431 | item = a->ob_item; |
| 432 | d = n - (ihigh-ilow); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 433 | if (ihigh > ilow) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 434 | p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow)); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 435 | else |
| 436 | p = recycle = NULL; |
| 437 | if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 438 | for (k = ilow; k < ihigh; k++) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 439 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 440 | if (d < 0) { |
| 441 | for (/*k = ihigh*/; k < a->ob_size; k++) |
| 442 | item[k+d] = item[k]; |
| 443 | a->ob_size += d; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 444 | NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 445 | a->ob_item = item; |
| 446 | } |
| 447 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 448 | else { /* Insert d items; recycle ihigh-ilow items */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 449 | NRESIZE(item, PyObject *, a->ob_size + d); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 450 | if (item == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 451 | if (recycle != NULL) |
| 452 | PyMem_DEL(recycle); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 453 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 454 | return -1; |
| 455 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 456 | for (k = a->ob_size; --k >= ihigh; ) |
| 457 | item[k+d] = item[k]; |
| 458 | for (/*k = ihigh-1*/; k >= ilow; --k) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 459 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 460 | a->ob_item = item; |
| 461 | a->ob_size += d; |
| 462 | } |
| 463 | for (k = 0; k < n; k++, ilow++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 464 | PyObject *w = b->ob_item[k]; |
| 465 | Py_XINCREF(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 466 | item[ilow] = w; |
| 467 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 468 | if (recycle) { |
| 469 | while (--p >= recycle) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 470 | Py_XDECREF(*p); |
| 471 | PyMem_DEL(recycle); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 472 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | #undef b |
| 475 | } |
| 476 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 477 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 478 | PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 479 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | if (!PyList_Check(a)) { |
| 481 | PyErr_BadInternalCall(); |
Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 482 | return -1; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 484 | return list_ass_slice((PyListObject *)a, ilow, ihigh, v); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 487 | static PyObject * |
| 488 | list_inplace_repeat(PyListObject *self, int n) |
| 489 | { |
| 490 | PyObject **items; |
| 491 | int size, i, j; |
| 492 | |
| 493 | |
| 494 | size = PyList_GET_SIZE(self); |
| 495 | if (size == 0) { |
| 496 | Py_INCREF(self); |
| 497 | return (PyObject *)self; |
| 498 | } |
| 499 | |
| 500 | items = self->ob_item; |
| 501 | |
| 502 | if (n < 1) { |
| 503 | self->ob_item = NULL; |
| 504 | self->ob_size = 0; |
| 505 | for (i = 0; i < size; i++) |
| 506 | Py_XDECREF(items[i]); |
| 507 | PyMem_DEL(items); |
| 508 | Py_INCREF(self); |
| 509 | return (PyObject *)self; |
| 510 | } |
| 511 | |
| 512 | NRESIZE(items, PyObject*, size*n); |
| 513 | if (items == NULL) { |
| 514 | PyErr_NoMemory(); |
| 515 | goto finally; |
| 516 | } |
| 517 | self->ob_item = items; |
| 518 | for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */ |
| 519 | for (j = 0; j < size; j++) { |
| 520 | PyObject *o = PyList_GET_ITEM(self, j); |
| 521 | Py_INCREF(o); |
| 522 | PyList_SET_ITEM(self, self->ob_size++, o); |
| 523 | } |
| 524 | } |
| 525 | Py_INCREF(self); |
| 526 | return (PyObject *)self; |
| 527 | finally: |
| 528 | return NULL; |
| 529 | } |
| 530 | |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 531 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 532 | list_ass_item(PyListObject *a, int i, PyObject *v) |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 533 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 534 | PyObject *old_value; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 535 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 536 | PyErr_SetString(PyExc_IndexError, |
| 537 | "list assignment index out of range"); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 538 | return -1; |
| 539 | } |
| 540 | if (v == NULL) |
| 541 | return list_ass_slice(a, i, i+1, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 542 | Py_INCREF(v); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 543 | old_value = a->ob_item[i]; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 544 | a->ob_item[i] = v; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 545 | Py_DECREF(old_value); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 549 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 550 | ins(PyListObject *self, int where, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | { |
| 552 | if (ins1(self, where, v) != 0) |
| 553 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 554 | Py_INCREF(Py_None); |
| 555 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 558 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 559 | listinsert(PyListObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 560 | { |
| 561 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 562 | PyObject *v; |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 563 | if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 564 | return NULL; |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 565 | return ins(self, i, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 568 | /* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */ |
| 569 | |
| 570 | #ifndef NO_STRICT_LIST_APPEND |
| 571 | #define PyArg_ParseTuple_Compat1 PyArg_ParseTuple |
| 572 | #else |
| 573 | #define PyArg_ParseTuple_Compat1(args, format, ret) \ |
| 574 | ( \ |
| 575 | PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \ |
| 576 | PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \ |
| 577 | PyArg_ParseTuple(args, format, ret) \ |
| 578 | ) |
| 579 | #endif |
| 580 | |
| 581 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 583 | listappend(PyListObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 584 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 585 | PyObject *v; |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 586 | if (!PyArg_ParseTuple_Compat1(args, "O:append", &v)) |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 587 | return NULL; |
| 588 | return ins(self, (int) self->ob_size, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 591 | static int |
| 592 | listextend_internal(PyListObject *self, PyObject *b) |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 593 | { |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 594 | PyObject **items; |
| 595 | int selflen = PyList_GET_SIZE(self); |
| 596 | int blen; |
| 597 | register int i; |
| 598 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 599 | if (PyObject_Size(b) == 0) |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 600 | /* short circuit when b is empty */ |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 601 | return 0; |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 602 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 603 | if (self == (PyListObject*)b) { |
| 604 | /* as in list_ass_slice() we must special case the |
| 605 | * situation: a.extend(a) |
| 606 | * |
| 607 | * XXX: I think this way ought to be faster than using |
| 608 | * list_slice() the way list_ass_slice() does. |
| 609 | */ |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 610 | Py_DECREF(b); |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 611 | b = PyList_New(selflen); |
| 612 | if (!b) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 613 | return -1; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 614 | for (i = 0; i < selflen; i++) { |
| 615 | PyObject *o = PyList_GET_ITEM(self, i); |
| 616 | Py_INCREF(o); |
| 617 | PyList_SET_ITEM(b, i, o); |
| 618 | } |
| 619 | } |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 620 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 621 | blen = PyObject_Size(b); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 622 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 623 | /* resize a using idiom */ |
| 624 | items = self->ob_item; |
| 625 | NRESIZE(items, PyObject*, selflen + blen); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 626 | if (items == NULL) { |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 627 | PyErr_NoMemory(); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 628 | Py_DECREF(b); |
| 629 | return -1; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 630 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 631 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 632 | self->ob_item = items; |
| 633 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 634 | /* populate the end of self with b's items */ |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 635 | for (i = 0; i < blen; i++) { |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 636 | PyObject *o = PySequence_Fast_GET_ITEM(b, i); |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 637 | Py_INCREF(o); |
| 638 | PyList_SET_ITEM(self, self->ob_size++, o); |
| 639 | } |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 640 | Py_DECREF(b); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 641 | return 0; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | |
| 645 | static PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 646 | list_inplace_concat(PyListObject *self, PyObject *other) |
| 647 | { |
| 648 | other = PySequence_Fast(other, "argument to += must be a sequence"); |
| 649 | if (!other) |
| 650 | return NULL; |
| 651 | |
| 652 | if (listextend_internal(self, other) < 0) |
| 653 | return NULL; |
| 654 | |
| 655 | Py_INCREF(self); |
| 656 | return (PyObject *)self; |
| 657 | } |
| 658 | |
| 659 | static PyObject * |
| 660 | listextend(PyListObject *self, PyObject *args) |
| 661 | { |
| 662 | |
| 663 | PyObject *b; |
| 664 | |
| 665 | if (!PyArg_ParseTuple(args, "O:extend", &b)) |
| 666 | return NULL; |
| 667 | |
| 668 | b = PySequence_Fast(b, "list.extend() argument must be a sequence"); |
| 669 | if (!b) |
| 670 | return NULL; |
| 671 | |
| 672 | if (listextend_internal(self, b) < 0) |
| 673 | return NULL; |
| 674 | |
| 675 | Py_INCREF(Py_None); |
| 676 | return Py_None; |
| 677 | } |
| 678 | |
| 679 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 680 | listpop(PyListObject *self, PyObject *args) |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 681 | { |
| 682 | int i = -1; |
| 683 | PyObject *v; |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 684 | if (!PyArg_ParseTuple(args, "|i:pop", &i)) |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 685 | return NULL; |
| 686 | if (self->ob_size == 0) { |
| 687 | /* Special-case most common failure cause */ |
| 688 | PyErr_SetString(PyExc_IndexError, "pop from empty list"); |
| 689 | return NULL; |
| 690 | } |
| 691 | if (i < 0) |
| 692 | i += self->ob_size; |
| 693 | if (i < 0 || i >= self->ob_size) { |
| 694 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 695 | return NULL; |
| 696 | } |
| 697 | v = self->ob_item[i]; |
| 698 | Py_INCREF(v); |
| 699 | if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 700 | Py_DECREF(v); |
| 701 | return NULL; |
| 702 | } |
| 703 | return v; |
| 704 | } |
| 705 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 706 | /* New quicksort implementation for arrays of object pointers. |
| 707 | Thanks to discussions with Tim Peters. */ |
| 708 | |
| 709 | /* CMPERROR is returned by our comparison function when an error |
| 710 | occurred. This is the largest negative integer (0x80000000 on a |
| 711 | 32-bit system). */ |
Guido van Rossum | 19700b6 | 1997-03-05 00:45:43 +0000 | [diff] [blame] | 712 | #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) ) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 713 | |
| 714 | /* Comparison function. Takes care of calling a user-supplied |
| 715 | comparison function (any callable Python object). Calls the |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 716 | standard comparison function, PyObject_Compare(), if the user- |
| 717 | supplied function is NULL. */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 718 | |
| 719 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 720 | docompare(PyObject *x, PyObject *y, PyObject *compare) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 721 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 722 | PyObject *args, *res; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 723 | int i; |
| 724 | |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 725 | if (compare == NULL) { |
| 726 | i = PyObject_Compare(x, y); |
| 727 | if (i && PyErr_Occurred()) |
| 728 | i = CMPERROR; |
| 729 | return i; |
| 730 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 731 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 732 | args = Py_BuildValue("(OO)", x, y); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 733 | if (args == NULL) |
| 734 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 735 | res = PyEval_CallObject(compare, args); |
| 736 | Py_DECREF(args); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 737 | if (res == NULL) |
| 738 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 739 | if (!PyInt_Check(res)) { |
| 740 | Py_DECREF(res); |
| 741 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 9bcd1d7 | 1999-04-19 17:44:39 +0000 | [diff] [blame] | 742 | "comparison function must return int"); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 743 | return CMPERROR; |
| 744 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 745 | i = PyInt_AsLong(res); |
| 746 | Py_DECREF(res); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 747 | if (i < 0) |
| 748 | return -1; |
| 749 | if (i > 0) |
| 750 | return 1; |
| 751 | return 0; |
| 752 | } |
| 753 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 754 | /* MINSIZE is the smallest array that will get a full-blown samplesort |
| 755 | treatment; smaller arrays are sorted using binary insertion. It must |
| 756 | be at least 7 for the samplesort implementation to work. Binary |
| 757 | insertion does fewer compares, but can suffer O(N**2) data movement. |
| 758 | The more expensive compares, the larger MINSIZE should be. */ |
| 759 | #define MINSIZE 100 |
| 760 | |
| 761 | /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to |
| 762 | partition; smaller slices are passed to binarysort. It must be at |
| 763 | least 2, and no larger than MINSIZE. Setting it higher reduces the # |
| 764 | of compares slowly, but increases the amount of data movement quickly. |
| 765 | The value here was chosen assuming a compare costs ~25x more than |
| 766 | swapping a pair of memory-resident pointers -- but under that assumption, |
| 767 | changing the value by a few dozen more or less has aggregate effect |
| 768 | under 1%. So the value is crucial, but not touchy <wink>. */ |
| 769 | #define MINPARTITIONSIZE 40 |
| 770 | |
| 771 | /* MAXMERGE is the largest number of elements we'll always merge into |
| 772 | a known-to-be sorted chunk via binary insertion, regardless of the |
| 773 | size of that chunk. Given a chunk of N sorted elements, and a group |
| 774 | of K unknowns, the largest K for which it's better to do insertion |
| 775 | (than a full-blown sort) is a complicated function of N and K mostly |
| 776 | involving the expected number of compares and data moves under each |
| 777 | approach, and the relative cost of those operations on a specific |
| 778 | architecure. The fixed value here is conservative, and should be a |
| 779 | clear win regardless of architecture or N. */ |
| 780 | #define MAXMERGE 15 |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 781 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 782 | /* STACKSIZE is the size of our work stack. A rough estimate is that |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 783 | this allows us to sort arrays of size N where |
| 784 | N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough |
| 785 | for arrays of size 2**64. Because we push the biggest partition |
| 786 | first, the worst case occurs when all subarrays are always partitioned |
| 787 | exactly in two. */ |
| 788 | #define STACKSIZE 60 |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 789 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 790 | |
| 791 | #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail |
| 792 | |
| 793 | /* binarysort is the best method for sorting small arrays: it does |
| 794 | few compares, but can do data movement quadratic in the number of |
| 795 | elements. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 796 | [lo, hi) is a contiguous slice of a list, and is sorted via |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 797 | binary insertion. |
| 798 | On entry, must have lo <= start <= hi, and that [lo, start) is already |
| 799 | sorted (pass start == lo if you don't know!). |
| 800 | If docompare complains (returns CMPERROR) return -1, else 0. |
| 801 | Even in case of error, the output slice will be some permutation of |
| 802 | the input (nothing is lost or duplicated). |
| 803 | */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 804 | |
| 805 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 806 | binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare) |
| 807 | /* compare -- comparison function object, or NULL for default */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 808 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 809 | /* assert lo <= start <= hi |
| 810 | assert [lo, start) is sorted */ |
| 811 | register int k; |
| 812 | register PyObject **l, **p, **r; |
| 813 | register PyObject *pivot; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 814 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 815 | if (lo == start) |
| 816 | ++start; |
| 817 | for (; start < hi; ++start) { |
| 818 | /* set l to where *start belongs */ |
| 819 | l = lo; |
| 820 | r = start; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 821 | pivot = *r; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 822 | do { |
| 823 | p = l + ((r - l) >> 1); |
| 824 | SETK(pivot, *p); |
| 825 | if (k < 0) |
| 826 | r = p; |
| 827 | else |
| 828 | l = p + 1; |
| 829 | } while (l < r); |
| 830 | /* Pivot should go at l -- slide over to make room. |
| 831 | Caution: using memmove is much slower under MSVC 5; |
| 832 | we're not usually moving many slots. */ |
| 833 | for (p = start; p > l; --p) |
| 834 | *p = *(p-1); |
| 835 | *l = pivot; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 836 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 837 | return 0; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 838 | |
| 839 | fail: |
| 840 | return -1; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /* samplesortslice is the sorting workhorse. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 844 | [lo, hi) is a contiguous slice of a list, to be sorted in place. |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 845 | On entry, must have lo <= hi, |
| 846 | If docompare complains (returns CMPERROR) return -1, else 0. |
| 847 | Even in case of error, the output slice will be some permutation of |
| 848 | the input (nothing is lost or duplicated). |
| 849 | |
| 850 | samplesort is basically quicksort on steroids: a power of 2 close |
| 851 | to n/ln(n) is computed, and that many elements (less 1) are picked at |
| 852 | random from the array and sorted. These 2**k - 1 elements are then |
| 853 | used as preselected pivots for an equal number of quicksort |
| 854 | partitioning steps, partitioning the slice into 2**k chunks each of |
| 855 | size about ln(n). These small final chunks are then usually handled |
| 856 | by binarysort. Note that when k=1, this is roughly the same as an |
| 857 | ordinary quicksort using a random pivot, and when k=2 this is roughly |
| 858 | a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes |
| 859 | this a "median of n/ln(n)" quicksort. You can also view it as a kind |
| 860 | of bucket sort, where 2**k-1 bucket boundaries are picked dynamically. |
| 861 | |
| 862 | The large number of samples makes a quadratic-time case almost |
| 863 | impossible, and asymptotically drives the average-case number of |
| 864 | compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of- |
| 865 | 3 variant) down to N lg N. |
| 866 | |
| 867 | We also play lots of low-level tricks to cut the number of compares. |
| 868 | |
| 869 | Very obscure: To avoid using extra memory, the PPs are stored in the |
| 870 | array and shuffled around as partitioning proceeds. At the start of a |
| 871 | partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order, |
| 872 | adjacent (either on the left or the right!) to a chunk of X elements |
| 873 | that are to be partitioned: P X or X P. In either case we need to |
| 874 | shuffle things *in place* so that the 2**(m-1) smaller PPs are on the |
| 875 | left, followed by the PP to be used for this step (that's the middle |
| 876 | of the PPs), followed by X, followed by the 2**(m-1) larger PPs: |
| 877 | P X or X P -> Psmall pivot X Plarge |
| 878 | and the order of the PPs must not be altered. It can take a while |
| 879 | to realize this isn't trivial! It can take even longer <wink> to |
| 880 | understand why the simple code below works, using only 2**(m-1) swaps. |
| 881 | The key is that the order of the X elements isn't necessarily |
| 882 | preserved: X can end up as some cyclic permutation of its original |
| 883 | order. That's OK, because X is unsorted anyway. If the order of X |
| 884 | had to be preserved too, the simplest method I know of using O(1) |
| 885 | scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes. |
| 886 | Since len(X) is typically several times larger than 2**(m-1), that |
| 887 | would slow things down. |
| 888 | */ |
| 889 | |
| 890 | struct SamplesortStackNode { |
| 891 | /* Represents a slice of the array, from (& including) lo up |
| 892 | to (but excluding) hi. "extra" additional & adjacent elements |
| 893 | are pre-selected pivots (PPs), spanning [lo-extra, lo) if |
| 894 | extra > 0, or [hi, hi-extra) if extra < 0. The PPs are |
| 895 | already sorted, but nothing is known about the other elements |
| 896 | in [lo, hi). |extra| is always one less than a power of 2. |
| 897 | When extra is 0, we're out of PPs, and the slice must be |
| 898 | sorted by some other means. */ |
| 899 | PyObject **lo; |
| 900 | PyObject **hi; |
| 901 | int extra; |
| 902 | }; |
| 903 | |
| 904 | /* The number of PPs we want is 2**k - 1, where 2**k is as close to |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 905 | N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 906 | is undesirable, so cutoff values are canned in the "cutoff" table |
| 907 | below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */ |
| 908 | #define CUTOFFBASE 4 |
Guido van Rossum | 3aa23fd | 1999-01-14 19:01:53 +0000 | [diff] [blame] | 909 | static long cutoff[] = { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 910 | 43, /* smallest N such that k == 4 */ |
| 911 | 106, /* etc */ |
| 912 | 250, |
| 913 | 576, |
| 914 | 1298, |
| 915 | 2885, |
| 916 | 6339, |
| 917 | 13805, |
| 918 | 29843, |
| 919 | 64116, |
| 920 | 137030, |
| 921 | 291554, |
| 922 | 617916, |
| 923 | 1305130, |
| 924 | 2748295, |
| 925 | 5771662, |
| 926 | 12091672, |
| 927 | 25276798, |
| 928 | 52734615, |
| 929 | 109820537, |
| 930 | 228324027, |
| 931 | 473977813, |
| 932 | 982548444, /* smallest N such that k == 26 */ |
| 933 | 2034159050 /* largest N that fits in signed 32-bit; k == 27 */ |
| 934 | }; |
| 935 | |
| 936 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 937 | samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare) |
| 938 | /* compare -- comparison function object, or NULL for default */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 939 | { |
| 940 | register PyObject **l, **r; |
| 941 | register PyObject *tmp, *pivot; |
| 942 | register int k; |
| 943 | int n, extra, top, extraOnRight; |
| 944 | struct SamplesortStackNode stack[STACKSIZE]; |
| 945 | |
| 946 | /* assert lo <= hi */ |
| 947 | n = hi - lo; |
| 948 | |
| 949 | /* ---------------------------------------------------------- |
| 950 | * Special cases |
| 951 | * --------------------------------------------------------*/ |
| 952 | if (n < 2) |
| 953 | return 0; |
| 954 | |
| 955 | /* Set r to the largest value such that [lo,r) is sorted. |
| 956 | This catches the already-sorted case, the all-the-same |
| 957 | case, and the appended-a-few-elements-to-a-sorted-list case. |
| 958 | If the array is unsorted, we're very likely to get out of |
| 959 | the loop fast, so the test is cheap if it doesn't pay off. |
| 960 | */ |
| 961 | /* assert lo < hi */ |
| 962 | for (r = lo+1; r < hi; ++r) { |
| 963 | SETK(*r, *(r-1)); |
| 964 | if (k < 0) |
| 965 | break; |
| 966 | } |
| 967 | /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are |
| 968 | few unknowns, or few elements in total. */ |
| 969 | if (hi - r <= MAXMERGE || n < MINSIZE) |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 970 | return binarysort(lo, hi, r, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 971 | |
| 972 | /* Check for the array already being reverse-sorted. Typical |
| 973 | benchmark-driven silliness <wink>. */ |
| 974 | /* assert lo < hi */ |
| 975 | for (r = lo+1; r < hi; ++r) { |
| 976 | SETK(*(r-1), *r); |
| 977 | if (k < 0) |
| 978 | break; |
| 979 | } |
| 980 | if (hi - r <= MAXMERGE) { |
| 981 | /* Reverse the reversed prefix, then insert the tail */ |
| 982 | PyObject **originalr = r; |
| 983 | l = lo; |
| 984 | do { |
| 985 | --r; |
| 986 | tmp = *l; *l = *r; *r = tmp; |
| 987 | ++l; |
| 988 | } while (l < r); |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 989 | return binarysort(lo, hi, originalr, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* ---------------------------------------------------------- |
| 993 | * Normal case setup: a large array without obvious pattern. |
| 994 | * --------------------------------------------------------*/ |
| 995 | |
| 996 | /* extra := a power of 2 ~= n/ln(n), less 1. |
| 997 | First find the smallest extra s.t. n < cutoff[extra] */ |
| 998 | for (extra = 0; |
| 999 | extra < sizeof(cutoff) / sizeof(cutoff[0]); |
| 1000 | ++extra) { |
| 1001 | if (n < cutoff[extra]) |
| 1002 | break; |
| 1003 | /* note that if we fall out of the loop, the value of |
| 1004 | extra still makes *sense*, but may be smaller than |
| 1005 | we would like (but the array has more than ~= 2**31 |
| 1006 | elements in this case!) */ |
| 1007 | } |
| 1008 | /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can |
| 1009 | have is CUTOFFBASE-1, so |
| 1010 | assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */ |
| 1011 | extra = (1 << (extra - 1 + CUTOFFBASE)) - 1; |
| 1012 | /* assert extra > 0 and n >= extra */ |
| 1013 | |
| 1014 | /* Swap that many values to the start of the array. The |
| 1015 | selection of elements is pseudo-random, but the same on |
| 1016 | every run (this is intentional! timing algorithm changes is |
| 1017 | a pain if timing varies across runs). */ |
| 1018 | { |
| 1019 | unsigned int seed = n / extra; /* arbitrary */ |
| 1020 | unsigned int i; |
| 1021 | for (i = 0; i < (unsigned)extra; ++i) { |
| 1022 | /* j := random int in [i, n) */ |
| 1023 | unsigned int j; |
| 1024 | seed = seed * 69069 + 7; |
| 1025 | j = i + seed % (n - i); |
| 1026 | tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /* Recursively sort the preselected pivots. */ |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1031 | if (samplesortslice(lo, lo + extra, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1032 | goto fail; |
| 1033 | |
| 1034 | top = 0; /* index of available stack slot */ |
| 1035 | lo += extra; /* point to first unknown */ |
| 1036 | extraOnRight = 0; /* the PPs are at the left end */ |
| 1037 | |
| 1038 | /* ---------------------------------------------------------- |
| 1039 | * Partition [lo, hi), and repeat until out of work. |
| 1040 | * --------------------------------------------------------*/ |
| 1041 | for (;;) { |
| 1042 | /* assert lo <= hi, so n >= 0 */ |
| 1043 | n = hi - lo; |
| 1044 | |
| 1045 | /* We may not want, or may not be able, to partition: |
| 1046 | If n is small, it's quicker to insert. |
| 1047 | If extra is 0, we're out of pivots, and *must* use |
| 1048 | another method. |
| 1049 | */ |
| 1050 | if (n < MINPARTITIONSIZE || extra == 0) { |
| 1051 | if (n >= MINSIZE) { |
| 1052 | /* assert extra == 0 |
| 1053 | This is rare, since the average size |
| 1054 | of a final block is only about |
| 1055 | ln(original n). */ |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1056 | if (samplesortslice(lo, hi, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1057 | goto fail; |
| 1058 | } |
| 1059 | else { |
| 1060 | /* Binary insertion should be quicker, |
| 1061 | and we can take advantage of the PPs |
| 1062 | already being sorted. */ |
| 1063 | if (extraOnRight && extra) { |
| 1064 | /* swap the PPs to the left end */ |
| 1065 | k = extra; |
| 1066 | do { |
| 1067 | tmp = *lo; |
| 1068 | *lo = *hi; |
| 1069 | *hi = tmp; |
| 1070 | ++lo; ++hi; |
| 1071 | } while (--k); |
| 1072 | } |
| 1073 | if (binarysort(lo - extra, hi, lo, |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1074 | compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1075 | goto fail; |
| 1076 | } |
| 1077 | |
| 1078 | /* Find another slice to work on. */ |
| 1079 | if (--top < 0) |
| 1080 | break; /* no more -- done! */ |
| 1081 | lo = stack[top].lo; |
| 1082 | hi = stack[top].hi; |
| 1083 | extra = stack[top].extra; |
| 1084 | extraOnRight = 0; |
| 1085 | if (extra < 0) { |
| 1086 | extraOnRight = 1; |
| 1087 | extra = -extra; |
| 1088 | } |
| 1089 | continue; |
| 1090 | } |
| 1091 | |
| 1092 | /* Pretend the PPs are indexed 0, 1, ..., extra-1. |
| 1093 | Then our preselected pivot is at (extra-1)/2, and we |
| 1094 | want to move the PPs before that to the left end of |
| 1095 | the slice, and the PPs after that to the right end. |
| 1096 | The following section changes extra, lo, hi, and the |
| 1097 | slice such that: |
| 1098 | [lo-extra, lo) contains the smaller PPs. |
| 1099 | *lo == our PP. |
| 1100 | (lo, hi) contains the unknown elements. |
| 1101 | [hi, hi+extra) contains the larger PPs. |
| 1102 | */ |
| 1103 | k = extra >>= 1; /* num PPs to move */ |
| 1104 | if (extraOnRight) { |
| 1105 | /* Swap the smaller PPs to the left end. |
| 1106 | Note that this loop actually moves k+1 items: |
| 1107 | the last is our PP */ |
| 1108 | do { |
| 1109 | tmp = *lo; *lo = *hi; *hi = tmp; |
| 1110 | ++lo; ++hi; |
| 1111 | } while (k--); |
| 1112 | } |
| 1113 | else { |
| 1114 | /* Swap the larger PPs to the right end. */ |
| 1115 | while (k--) { |
| 1116 | --lo; --hi; |
| 1117 | tmp = *lo; *lo = *hi; *hi = tmp; |
| 1118 | } |
| 1119 | } |
| 1120 | --lo; /* *lo is now our PP */ |
| 1121 | pivot = *lo; |
| 1122 | |
| 1123 | /* Now an almost-ordinary quicksort partition step. |
| 1124 | Note that most of the time is spent here! |
| 1125 | Only odd thing is that we partition into < and >=, |
| 1126 | instead of the usual <= and >=. This helps when |
| 1127 | there are lots of duplicates of different values, |
| 1128 | because it eventually tends to make subfiles |
| 1129 | "pure" (all duplicates), and we special-case for |
| 1130 | duplicates later. */ |
| 1131 | l = lo + 1; |
| 1132 | r = hi - 1; |
| 1133 | /* assert lo < l < r < hi (small n weeded out above) */ |
| 1134 | |
| 1135 | do { |
| 1136 | /* slide l right, looking for key >= pivot */ |
| 1137 | do { |
| 1138 | SETK(*l, pivot); |
| 1139 | if (k < 0) |
| 1140 | ++l; |
| 1141 | else |
| 1142 | break; |
| 1143 | } while (l < r); |
| 1144 | |
| 1145 | /* slide r left, looking for key < pivot */ |
| 1146 | while (l < r) { |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1147 | register PyObject *rval = *r--; |
| 1148 | SETK(rval, pivot); |
| 1149 | if (k < 0) { |
| 1150 | /* swap and advance */ |
| 1151 | r[1] = *l; |
| 1152 | *l++ = rval; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1153 | break; |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1154 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | } while (l < r); |
| 1158 | |
| 1159 | /* assert lo < r <= l < hi |
| 1160 | assert r == l or r+1 == l |
| 1161 | everything to the left of l is < pivot, and |
| 1162 | everything to the right of r is >= pivot */ |
| 1163 | |
| 1164 | if (l == r) { |
| 1165 | SETK(*r, pivot); |
| 1166 | if (k < 0) |
| 1167 | ++l; |
| 1168 | else |
| 1169 | --r; |
| 1170 | } |
| 1171 | /* assert lo <= r and r+1 == l and l <= hi |
| 1172 | assert r == lo or a[r] < pivot |
| 1173 | assert a[lo] is pivot |
| 1174 | assert l == hi or a[l] >= pivot |
| 1175 | Swap the pivot into "the middle", so we can henceforth |
| 1176 | ignore it. |
| 1177 | */ |
| 1178 | *lo = *r; |
| 1179 | *r = pivot; |
| 1180 | |
| 1181 | /* The following is true now, & will be preserved: |
| 1182 | All in [lo,r) are < pivot |
| 1183 | All in [r,l) == pivot (& so can be ignored) |
| 1184 | All in [l,hi) are >= pivot */ |
| 1185 | |
| 1186 | /* Check for duplicates of the pivot. One compare is |
| 1187 | wasted if there are no duplicates, but can win big |
| 1188 | when there are. |
| 1189 | Tricky: we're sticking to "<" compares, so deduce |
| 1190 | equality indirectly. We know pivot <= *l, so they're |
| 1191 | equal iff not pivot < *l. |
| 1192 | */ |
| 1193 | while (l < hi) { |
| 1194 | /* pivot <= *l known */ |
| 1195 | SETK(pivot, *l); |
| 1196 | if (k < 0) |
| 1197 | break; |
| 1198 | else |
| 1199 | /* <= and not < implies == */ |
| 1200 | ++l; |
| 1201 | } |
| 1202 | |
| 1203 | /* assert lo <= r < l <= hi |
| 1204 | Partitions are [lo, r) and [l, hi) */ |
| 1205 | |
| 1206 | /* push fattest first; remember we still have extra PPs |
| 1207 | to the left of the left chunk and to the right of |
| 1208 | the right chunk! */ |
| 1209 | /* assert top < STACKSIZE */ |
| 1210 | if (r - lo <= hi - l) { |
| 1211 | /* second is bigger */ |
| 1212 | stack[top].lo = l; |
| 1213 | stack[top].hi = hi; |
| 1214 | stack[top].extra = -extra; |
| 1215 | hi = r; |
| 1216 | extraOnRight = 0; |
| 1217 | } |
| 1218 | else { |
| 1219 | /* first is bigger */ |
| 1220 | stack[top].lo = lo; |
| 1221 | stack[top].hi = r; |
| 1222 | stack[top].extra = extra; |
| 1223 | lo = l; |
| 1224 | extraOnRight = 1; |
| 1225 | } |
| 1226 | ++top; |
| 1227 | |
| 1228 | } /* end of partitioning loop */ |
| 1229 | |
| 1230 | return 0; |
| 1231 | |
| 1232 | fail: |
| 1233 | return -1; |
| 1234 | } |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 1235 | |
| 1236 | #undef SETK |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1237 | |
| 1238 | staticforward PyTypeObject immutable_list_type; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1239 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1240 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1241 | listsort(PyListObject *self, PyObject *args) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1242 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1243 | int err; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1244 | PyObject *compare = NULL; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1245 | |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1246 | if (args != NULL) { |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 1247 | if (!PyArg_ParseTuple(args, "|O:sort", &compare)) |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1248 | return NULL; |
| 1249 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1250 | self->ob_type = &immutable_list_type; |
| 1251 | err = samplesortslice(self->ob_item, |
| 1252 | self->ob_item + self->ob_size, |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1253 | compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1254 | self->ob_type = &PyList_Type; |
| 1255 | if (err < 0) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1256 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1257 | Py_INCREF(Py_None); |
| 1258 | return Py_None; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1261 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1262 | PyList_Sort(PyObject *v) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1263 | { |
| 1264 | if (v == NULL || !PyList_Check(v)) { |
| 1265 | PyErr_BadInternalCall(); |
| 1266 | return -1; |
| 1267 | } |
| 1268 | v = listsort((PyListObject *)v, (PyObject *)NULL); |
| 1269 | if (v == NULL) |
| 1270 | return -1; |
| 1271 | Py_DECREF(v); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1275 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1276 | listreverse(PyListObject *self, PyObject *args) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1277 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1278 | register PyObject **p, **q; |
| 1279 | register PyObject *tmp; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1280 | |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 1281 | if (!PyArg_ParseTuple(args, ":reverse")) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1282 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1283 | |
| 1284 | if (self->ob_size > 1) { |
| 1285 | for (p = self->ob_item, q = self->ob_item + self->ob_size - 1; |
| 1286 | p < q; p++, q--) { |
| 1287 | tmp = *p; |
| 1288 | *p = *q; |
| 1289 | *q = tmp; |
| 1290 | } |
| 1291 | } |
| 1292 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1293 | Py_INCREF(Py_None); |
| 1294 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 1297 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1298 | PyList_Reverse(PyObject *v) |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1299 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1300 | if (v == NULL || !PyList_Check(v)) { |
| 1301 | PyErr_BadInternalCall(); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1302 | return -1; |
| 1303 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1304 | v = listreverse((PyListObject *)v, (PyObject *)NULL); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1305 | if (v == NULL) |
| 1306 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1307 | Py_DECREF(v); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1311 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1312 | PyList_AsTuple(PyObject *v) |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1313 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1314 | PyObject *w; |
| 1315 | PyObject **p; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1316 | int n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1317 | if (v == NULL || !PyList_Check(v)) { |
| 1318 | PyErr_BadInternalCall(); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1319 | return NULL; |
| 1320 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1321 | n = ((PyListObject *)v)->ob_size; |
| 1322 | w = PyTuple_New(n); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1323 | if (w == NULL) |
| 1324 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1325 | p = ((PyTupleObject *)w)->ob_item; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1326 | memcpy((void *)p, |
| 1327 | (void *)((PyListObject *)v)->ob_item, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1328 | n*sizeof(PyObject *)); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1329 | while (--n >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1330 | Py_INCREF(*p); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1331 | p++; |
| 1332 | } |
| 1333 | return w; |
| 1334 | } |
| 1335 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1336 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1337 | listindex(PyListObject *self, PyObject *args) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1338 | { |
| 1339 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1340 | PyObject *v; |
| 1341 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 1342 | if (!PyArg_ParseTuple_Compat1(args, "O:index", &v)) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1343 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1344 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1345 | if (PyObject_Compare(self->ob_item[i], v) == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1346 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1347 | if (PyErr_Occurred()) |
| 1348 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1349 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1350 | PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1351 | return NULL; |
| 1352 | } |
| 1353 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1354 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1355 | listcount(PyListObject *self, PyObject *args) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1356 | { |
| 1357 | int count = 0; |
| 1358 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1359 | PyObject *v; |
| 1360 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 1361 | if (!PyArg_ParseTuple_Compat1(args, "O:count", &v)) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1362 | return NULL; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1363 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1364 | if (PyObject_Compare(self->ob_item[i], v) == 0) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1365 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1366 | if (PyErr_Occurred()) |
| 1367 | return NULL; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1368 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1369 | return PyInt_FromLong((long)count); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1372 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1373 | listremove(PyListObject *self, PyObject *args) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1374 | { |
| 1375 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1376 | PyObject *v; |
| 1377 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 1378 | if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v)) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1379 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1380 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1381 | if (PyObject_Compare(self->ob_item[i], v) == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1382 | if (list_ass_slice(self, i, i+1, |
| 1383 | (PyObject *)NULL) != 0) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1384 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1385 | Py_INCREF(Py_None); |
| 1386 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1387 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1388 | if (PyErr_Occurred()) |
| 1389 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1390 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1391 | PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1392 | return NULL; |
| 1393 | } |
| 1394 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1395 | static int |
| 1396 | list_traverse(PyListObject *o, visitproc visit, void *arg) |
| 1397 | { |
| 1398 | int i, err; |
| 1399 | PyObject *x; |
| 1400 | |
| 1401 | for (i = o->ob_size; --i >= 0; ) { |
| 1402 | x = o->ob_item[i]; |
| 1403 | if (x != NULL) { |
| 1404 | err = visit(x, arg); |
| 1405 | if (err) |
| 1406 | return err; |
| 1407 | } |
| 1408 | } |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static int |
| 1413 | list_clear(PyListObject *lp) |
| 1414 | { |
| 1415 | (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0); |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1419 | static char append_doc[] = |
| 1420 | "L.append(object) -- append object to end"; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 1421 | static char extend_doc[] = |
| 1422 | "L.extend(list) -- extend list by appending list elements"; |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1423 | static char insert_doc[] = |
| 1424 | "L.insert(index, object) -- insert object before index"; |
| 1425 | static char pop_doc[] = |
| 1426 | "L.pop([index]) -> item -- remove and return item at index (default last)"; |
| 1427 | static char remove_doc[] = |
| 1428 | "L.remove(value) -- remove first occurrence of value"; |
| 1429 | static char index_doc[] = |
| 1430 | "L.index(value) -> integer -- return index of first occurrence of value"; |
| 1431 | static char count_doc[] = |
| 1432 | "L.count(value) -> integer -- return number of occurrences of value"; |
| 1433 | static char reverse_doc[] = |
| 1434 | "L.reverse() -- reverse *IN PLACE*"; |
| 1435 | static char sort_doc[] = |
| 1436 | "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1"; |
| 1437 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1438 | static PyMethodDef list_methods[] = { |
Tim Peters | 0e76ab2 | 2000-12-13 22:35:46 +0000 | [diff] [blame] | 1439 | {"append", (PyCFunction)listappend, METH_VARARGS, append_doc}, |
| 1440 | {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, |
| 1441 | {"extend", (PyCFunction)listextend, METH_VARARGS, extend_doc}, |
| 1442 | {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc}, |
| 1443 | {"remove", (PyCFunction)listremove, METH_VARARGS, remove_doc}, |
| 1444 | {"index", (PyCFunction)listindex, METH_VARARGS, index_doc}, |
| 1445 | {"count", (PyCFunction)listcount, METH_VARARGS, count_doc}, |
| 1446 | {"reverse", (PyCFunction)listreverse, METH_VARARGS, reverse_doc}, |
| 1447 | {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1448 | {NULL, NULL} /* sentinel */ |
| 1449 | }; |
| 1450 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1451 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1452 | list_getattr(PyListObject *f, char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1453 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1454 | return Py_FindMethod(list_methods, (PyObject *)f, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1457 | static PySequenceMethods list_as_sequence = { |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1458 | (inquiry)list_length, /*sq_length*/ |
| 1459 | (binaryfunc)list_concat, /*sq_concat*/ |
| 1460 | (intargfunc)list_repeat, /*sq_repeat*/ |
| 1461 | (intargfunc)list_item, /*sq_item*/ |
| 1462 | (intintargfunc)list_slice, /*sq_slice*/ |
| 1463 | (intobjargproc)list_ass_item, /*sq_ass_item*/ |
| 1464 | (intintobjargproc)list_ass_slice, /*sq_ass_slice*/ |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 1465 | (objobjproc)list_contains, /*sq_contains*/ |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1466 | (binaryfunc)list_inplace_concat, /*sq_inplace_concat*/ |
| 1467 | (intargfunc)list_inplace_repeat, /*sq_inplace_repeat*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1468 | }; |
| 1469 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1470 | PyTypeObject PyList_Type = { |
| 1471 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1472 | 0, |
| 1473 | "list", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1474 | sizeof(PyListObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1475 | 0, |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1476 | (destructor)list_dealloc, /*tp_dealloc*/ |
| 1477 | (printfunc)list_print, /*tp_print*/ |
| 1478 | (getattrfunc)list_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1479 | 0, /*tp_setattr*/ |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1480 | (cmpfunc)list_compare, /*tp_compare*/ |
| 1481 | (reprfunc)list_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1482 | 0, /*tp_as_number*/ |
| 1483 | &list_as_sequence, /*tp_as_sequence*/ |
| 1484 | 0, /*tp_as_mapping*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 1485 | 0, /*tp_hash*/ |
| 1486 | 0, /*tp_call*/ |
| 1487 | 0, /*tp_str*/ |
| 1488 | 0, /*tp_getattro*/ |
| 1489 | 0, /*tp_setattro*/ |
| 1490 | 0, /*tp_as_buffer*/ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 1491 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1492 | 0, /* tp_doc */ |
| 1493 | (traverseproc)list_traverse, /* tp_traverse */ |
| 1494 | (inquiry)list_clear, /* tp_clear */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1495 | }; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1496 | |
| 1497 | |
| 1498 | /* During a sort, we really can't have anyone modifying the list; it could |
| 1499 | cause core dumps. Thus, we substitute a dummy type that raises an |
| 1500 | explanatory exception when a modifying operation is used. Caveat: |
| 1501 | comparisons may behave differently; but I guess it's a bad idea anyway to |
| 1502 | compare a list that's being sorted... */ |
| 1503 | |
| 1504 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1505 | immutable_list_op(void) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1506 | { |
| 1507 | PyErr_SetString(PyExc_TypeError, |
| 1508 | "a list cannot be modified while it is being sorted"); |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | |
| 1512 | static PyMethodDef immutable_list_methods[] = { |
| 1513 | {"append", (PyCFunction)immutable_list_op}, |
| 1514 | {"insert", (PyCFunction)immutable_list_op}, |
| 1515 | {"remove", (PyCFunction)immutable_list_op}, |
| 1516 | {"index", (PyCFunction)listindex}, |
| 1517 | {"count", (PyCFunction)listcount}, |
| 1518 | {"reverse", (PyCFunction)immutable_list_op}, |
| 1519 | {"sort", (PyCFunction)immutable_list_op}, |
| 1520 | {NULL, NULL} /* sentinel */ |
| 1521 | }; |
| 1522 | |
| 1523 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1524 | immutable_list_getattr(PyListObject *f, char *name) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1525 | { |
| 1526 | return Py_FindMethod(immutable_list_methods, (PyObject *)f, name); |
| 1527 | } |
| 1528 | |
| 1529 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1530 | immutable_list_ass(void) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1531 | { |
| 1532 | immutable_list_op(); |
| 1533 | return -1; |
| 1534 | } |
| 1535 | |
| 1536 | static PySequenceMethods immutable_list_as_sequence = { |
| 1537 | (inquiry)list_length, /*sq_length*/ |
| 1538 | (binaryfunc)list_concat, /*sq_concat*/ |
| 1539 | (intargfunc)list_repeat, /*sq_repeat*/ |
| 1540 | (intargfunc)list_item, /*sq_item*/ |
| 1541 | (intintargfunc)list_slice, /*sq_slice*/ |
| 1542 | (intobjargproc)immutable_list_ass, /*sq_ass_item*/ |
| 1543 | (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 1544 | (objobjproc)list_contains, /*sq_contains*/ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1545 | }; |
| 1546 | |
| 1547 | static PyTypeObject immutable_list_type = { |
| 1548 | PyObject_HEAD_INIT(&PyType_Type) |
| 1549 | 0, |
| 1550 | "list (immutable, during sort)", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1551 | sizeof(PyListObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1552 | 0, |
| 1553 | 0, /*tp_dealloc*/ /* Cannot happen */ |
| 1554 | (printfunc)list_print, /*tp_print*/ |
| 1555 | (getattrfunc)immutable_list_getattr, /*tp_getattr*/ |
| 1556 | 0, /*tp_setattr*/ |
| 1557 | 0, /*tp_compare*/ /* Won't be called */ |
| 1558 | (reprfunc)list_repr, /*tp_repr*/ |
| 1559 | 0, /*tp_as_number*/ |
| 1560 | &immutable_list_as_sequence, /*tp_as_sequence*/ |
| 1561 | 0, /*tp_as_mapping*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 1562 | 0, /*tp_hash*/ |
| 1563 | 0, /*tp_call*/ |
| 1564 | 0, /*tp_str*/ |
| 1565 | 0, /*tp_getattro*/ |
| 1566 | 0, /*tp_setattro*/ |
| 1567 | 0, /*tp_as_buffer*/ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 1568 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1569 | 0, /* tp_doc */ |
| 1570 | (traverseproc)list_traverse, /* tp_traverse */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1571 | }; |