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