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