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