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