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