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