| 1 |
|
| 2 |
|
| 3 |
#include "Python.h" |
| 4 |
|
| 5 |
#ifdef STDC_HEADERS |
| 6 |
#include <stddef.h> |
| 7 |
#else |
| 8 |
#include <sys/types.h> |
| 9 |
#endif |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
if (allocated >= newsize && newsize >= (allocated >> 1)) { |
| 36 |
assert(self->ob_item != NULL || newsize == 0); |
| 37 |
Py_SIZE(self) = newsize; |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6); |
| 49 |
|
| 50 |
|
| 51 |
if (new_allocated > PY_SIZE_MAX - newsize) { |
| 52 |
PyErr_NoMemory(); |
| 53 |
return -1; |
| 54 |
} else { |
| 55 |
new_allocated += newsize; |
| 56 |
} |
| 57 |
|
| 58 |
if (newsize == 0) |
| 59 |
new_allocated = 0; |
| 60 |
items = self->ob_item; |
| 61 |
if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *))) |
| 62 |
PyMem_RESIZE(items, PyObject *, new_allocated); |
| 63 |
else |
| 64 |
items = NULL; |
| 65 |
if (items == NULL) { |
| 66 |
PyErr_NoMemory(); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
self->ob_item = items; |
| 70 |
Py_SIZE(self) = newsize; |
| 71 |
self->allocated = new_allocated; |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
#undef SHOW_ALLOC_COUNT |
| 77 |
#ifdef SHOW_ALLOC_COUNT |
| 78 |
static size_t count_alloc = 0; |
| 79 |
static size_t count_reuse = 0; |
| 80 |
|
| 81 |
static void |
| 82 |
show_alloc(void) |
| 83 |
{ |
| 84 |
fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n", |
| 85 |
count_alloc); |
| 86 |
fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T |
| 87 |
"d\n", count_reuse); |
| 88 |
fprintf(stderr, "%.2f%% reuse rate\n\n", |
| 89 |
(100.0*count_reuse/(count_alloc+count_reuse))); |
| 90 |
} |
| 91 |
#endif |
| 92 |
|
| 93 |
|
| 94 |
#ifndef PyList_MAXFREELIST |
| 95 |
#define PyList_MAXFREELIST 80 |
| 96 |
#endif |
| 97 |
static PyListObject *free_list[PyList_MAXFREELIST]; |
| 98 |
static int numfree = 0; |
| 99 |
|
| 100 |
void |
| 101 |
PyList_Fini(void) |
| 102 |
{ |
| 103 |
PyListObject *op; |
| 104 |
|
| 105 |
while (numfree) { |
| 106 |
op = free_list[--numfree]; |
| 107 |
assert(PyList_CheckExact(op)); |
| 108 |
PyObject_GC_Del(op); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
PyObject * |
| 113 |
PyList_New(Py_ssize_t size) |
| 114 |
{ |
| 115 |
PyListObject *op; |
| 116 |
size_t nbytes; |
| 117 |
#ifdef SHOW_ALLOC_COUNT |
| 118 |
static int initialized = 0; |
| 119 |
if (!initialized) { |
| 120 |
Py_AtExit(show_alloc); |
| 121 |
initialized = 1; |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
if (size < 0) { |
| 126 |
PyErr_BadInternalCall(); |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *)) |
| 132 |
return PyErr_NoMemory(); |
| 133 |
nbytes = size * sizeof(PyObject *); |
| 134 |
if (numfree) { |
| 135 |
numfree--; |
| 136 |
op = free_list[numfree]; |
| 137 |
_Py_NewReference((PyObject *)op); |
| 138 |
#ifdef SHOW_ALLOC_COUNT |
| 139 |
count_reuse++; |
| 140 |
#endif |
| 141 |
} else { |
| 142 |
op = PyObject_GC_New(PyListObject, &PyList_Type); |
| 143 |
if (op == NULL) |
| 144 |
return NULL; |
| 145 |
#ifdef SHOW_ALLOC_COUNT |
| 146 |
count_alloc++; |
| 147 |
#endif |
| 148 |
} |
| 149 |
if (size <= 0) |
| 150 |
op->ob_item = NULL; |
| 151 |
else { |
| 152 |
op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); |
| 153 |
if (op->ob_item == NULL) { |
| 154 |
Py_DECREF(op); |
| 155 |
return PyErr_NoMemory(); |
| 156 |
} |
| 157 |
memset(op->ob_item, 0, nbytes); |
| 158 |
} |
| 159 |
Py_SIZE(op) = size; |
| 160 |
op->allocated = size; |
| 161 |
_PyObject_GC_TRACK(op); |
| 162 |
return (PyObject *) op; |
| 163 |
} |
| 164 |
|
| 165 |
Py_ssize_t |
| 166 |
PyList_Size(PyObject *op) |
| 167 |
{ |
| 168 |
if (!PyList_Check(op)) { |
| 169 |
PyErr_BadInternalCall(); |
| 170 |
return -1; |
| 171 |
} |
| 172 |
else |
| 173 |
return Py_SIZE(op); |
| 174 |
} |
| 175 |
|
| 176 |
static PyObject *indexerr = NULL; |
| 177 |
|
| 178 |
PyObject * |
| 179 |
PyList_GetItem(PyObject *op, Py_ssize_t i) |
| 180 |
{ |
| 181 |
if (!PyList_Check(op)) { |
| 182 |
PyErr_BadInternalCall(); |
| 183 |
return NULL; |
| 184 |
} |
| 185 |
if (i < 0 || i >= Py_SIZE(op)) { |
| 186 |
if (indexerr == NULL) { |
| 187 |
indexerr = PyString_FromString( |
| 188 |
"list index out of range"); |
| 189 |
if (indexerr == NULL) |
| 190 |
return NULL; |
| 191 |
} |
| 192 |
PyErr_SetObject(PyExc_IndexError, indexerr); |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
return ((PyListObject *)op) -> ob_item[i]; |
| 196 |
} |
| 197 |
|
| 198 |
int |
| 199 |
PyList_SetItem(register PyObject *op, register Py_ssize_t i, |
| 200 |
register PyObject *newitem) |
| 201 |
{ |
| 202 |
register PyObject *olditem; |
| 203 |
register PyObject **p; |
| 204 |
if (!PyList_Check(op)) { |
| 205 |
Py_XDECREF(newitem); |
| 206 |
PyErr_BadInternalCall(); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
if (i < 0 || i >= Py_SIZE(op)) { |
| 210 |
Py_XDECREF(newitem); |
| 211 |
PyErr_SetString(PyExc_IndexError, |
| 212 |
"list assignment index out of range"); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
p = ((PyListObject *)op) -> ob_item + i; |
| 216 |
olditem = *p; |
| 217 |
*p = newitem; |
| 218 |
Py_XDECREF(olditem); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int |
| 223 |
ins1(PyListObject *self, Py_ssize_t where, PyObject *v) |
| 224 |
{ |
| 225 |
Py_ssize_t i, n = Py_SIZE(self); |
| 226 |
PyObject **items; |
| 227 |
if (v == NULL) { |
| 228 |
PyErr_BadInternalCall(); |
| 229 |
return -1; |
| 230 |
} |
| 231 |
if (n == PY_SSIZE_T_MAX) { |
| 232 |
PyErr_SetString(PyExc_OverflowError, |
| 233 |
"cannot add more objects to list"); |
| 234 |
return -1; |
| 235 |
} |
| 236 |
|
| 237 |
if (list_resize(self, n+1) == -1) |
| 238 |
return -1; |
| 239 |
|
| 240 |
if (where < 0) { |
| 241 |
where += n; |
| 242 |
if (where < 0) |
| 243 |
where = 0; |
| 244 |
} |
| 245 |
if (where > n) |
| 246 |
where = n; |
| 247 |
items = self->ob_item; |
| 248 |
for (i = n; --i >= where; ) |
| 249 |
items[i+1] = items[i]; |
| 250 |
Py_INCREF(v); |
| 251 |
items[where] = v; |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
int |
| 256 |
PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem) |
| 257 |
{ |
| 258 |
if (!PyList_Check(op)) { |
| 259 |
PyErr_BadInternalCall(); |
| 260 |
return -1; |
| 261 |
} |
| 262 |
return ins1((PyListObject *)op, where, newitem); |
| 263 |
} |
| 264 |
|
| 265 |
static int |
| 266 |
app1(PyListObject *self, PyObject *v) |
| 267 |
{ |
| 268 |
Py_ssize_t n = PyList_GET_SIZE(self); |
| 269 |
|
| 270 |
assert (v != NULL); |
| 271 |
if (n == PY_SSIZE_T_MAX) { |
| 272 |
PyErr_SetString(PyExc_OverflowError, |
| 273 |
"cannot add more objects to list"); |
| 274 |
return -1; |
| 275 |
} |
| 276 |
|
| 277 |
if (list_resize(self, n+1) == -1) |
| 278 |
return -1; |
| 279 |
|
| 280 |
Py_INCREF(v); |
| 281 |
PyList_SET_ITEM(self, n, v); |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
int |
| 286 |
PyList_Append(PyObject *op, PyObject *newitem) |
| 287 |
{ |
| 288 |
if (PyList_Check(op) && (newitem != NULL)) |
| 289 |
return app1((PyListObject *)op, newitem); |
| 290 |
PyErr_BadInternalCall(); |
| 291 |
return -1; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
static void |
| 297 |
list_dealloc(PyListObject *op) |
| 298 |
{ |
| 299 |
Py_ssize_t i; |
| 300 |
PyObject_GC_UnTrack(op); |
| 301 |
Py_TRASHCAN_SAFE_BEGIN(op) |
| 302 |
if (op->ob_item != NULL) { |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
i = Py_SIZE(op); |
| 308 |
while (--i >= 0) { |
| 309 |
Py_XDECREF(op->ob_item[i]); |
| 310 |
} |
| 311 |
PyMem_FREE(op->ob_item); |
| 312 |
} |
| 313 |
if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op)) |
| 314 |
free_list[numfree++] = op; |
| 315 |
else |
| 316 |
Py_TYPE(op)->tp_free((PyObject *)op); |
| 317 |
Py_TRASHCAN_SAFE_END(op) |
| 318 |
} |
| 319 |
|
| 320 |
static int |
| 321 |
list_print(PyListObject *op, FILE *fp, int flags) |
| 322 |
{ |
| 323 |
int rc; |
| 324 |
Py_ssize_t i; |
| 325 |
PyObject *item; |
| 326 |
|
| 327 |
rc = Py_ReprEnter((PyObject*)op); |
| 328 |
if (rc != 0) { |
| 329 |
if (rc < 0) |
| 330 |
return rc; |
| 331 |
Py_BEGIN_ALLOW_THREADS |
| 332 |
fprintf(fp, "[...]"); |
| 333 |
Py_END_ALLOW_THREADS |
| 334 |
return 0; |
| 335 |
} |
| 336 |
Py_BEGIN_ALLOW_THREADS |
| 337 |
fprintf(fp, "["); |
| 338 |
Py_END_ALLOW_THREADS |
| 339 |
for (i = 0; i < Py_SIZE(op); i++) { |
| 340 |
item = op->ob_item[i]; |
| 341 |
Py_INCREF(item); |
| 342 |
if (i > 0) { |
| 343 |
Py_BEGIN_ALLOW_THREADS |
| 344 |
fprintf(fp, ", "); |
| 345 |
Py_END_ALLOW_THREADS |
| 346 |
} |
| 347 |
if (PyObject_Print(item, fp, 0) != 0) { |
| 348 |
Py_DECREF(item); |
| 349 |
Py_ReprLeave((PyObject *)op); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
Py_DECREF(item); |
| 353 |
} |
| 354 |
Py_BEGIN_ALLOW_THREADS |
| 355 |
fprintf(fp, "]"); |
| 356 |
Py_END_ALLOW_THREADS |
| 357 |
Py_ReprLeave((PyObject *)op); |
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
static PyObject * |
| 362 |
list_repr(PyListObject *v) |
| 363 |
{ |
| 364 |
Py_ssize_t i; |
| 365 |
PyObject *s, *temp; |
| 366 |
PyObject *pieces = NULL, *result = NULL; |
| 367 |
|
| 368 |
i = Py_ReprEnter((PyObject*)v); |
| 369 |
if (i != 0) { |
| 370 |
return i > 0 ? PyString_FromString("[...]") : NULL; |
| 371 |
} |
| 372 |
|
| 373 |
if (Py_SIZE(v) == 0) { |
| 374 |
result = PyString_FromString("[]"); |
| 375 |
goto Done; |
| 376 |
} |
| 377 |
|
| 378 |
pieces = PyList_New(0); |
| 379 |
if (pieces == NULL) |
| 380 |
goto Done; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
for (i = 0; i < Py_SIZE(v); ++i) { |
| 385 |
int status; |
| 386 |
s = PyObject_Repr(v->ob_item[i]); |
| 387 |
if (s == NULL) |
| 388 |
goto Done; |
| 389 |
status = PyList_Append(pieces, s); |
| 390 |
Py_DECREF(s); |
| 391 |
if (status < 0) |
| 392 |
goto Done; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
assert(PyList_GET_SIZE(pieces) > 0); |
| 397 |
s = PyString_FromString("["); |
| 398 |
if (s == NULL) |
| 399 |
goto Done; |
| 400 |
temp = PyList_GET_ITEM(pieces, 0); |
| 401 |
PyString_ConcatAndDel(&s, temp); |
| 402 |
PyList_SET_ITEM(pieces, 0, s); |
| 403 |
if (s == NULL) |
| 404 |
goto Done; |
| 405 |
|
| 406 |
s = PyString_FromString("]"); |
| 407 |
if (s == NULL) |
| 408 |
goto Done; |
| 409 |
temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 410 |
PyString_ConcatAndDel(&temp, s); |
| 411 |
PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 412 |
if (temp == NULL) |
| 413 |
goto Done; |
| 414 |
|
| 415 |
|
| 416 |
s = PyString_FromString(", "); |
| 417 |
if (s == NULL) |
| 418 |
goto Done; |
| 419 |
result = _PyString_Join(s, pieces); |
| 420 |
Py_DECREF(s); |
| 421 |
|
| 422 |
Done: |
| 423 |
Py_XDECREF(pieces); |
| 424 |
Py_ReprLeave((PyObject *)v); |
| 425 |
return result; |
| 426 |
} |
| 427 |
|
| 428 |
static Py_ssize_t |
| 429 |
list_length(PyListObject *a) |
| 430 |
{ |
| 431 |
return Py_SIZE(a); |
| 432 |
} |
| 433 |
|
| 434 |
static int |
| 435 |
list_contains(PyListObject *a, PyObject *el) |
| 436 |
{ |
| 437 |
Py_ssize_t i; |
| 438 |
int cmp; |
| 439 |
|
| 440 |
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) |
| 441 |
cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i), |
| 442 |
Py_EQ); |
| 443 |
return cmp; |
| 444 |
} |
| 445 |
|
| 446 |
static PyObject * |
| 447 |
list_item(PyListObject *a, Py_ssize_t i) |
| 448 |
{ |
| 449 |
if (i < 0 || i >= Py_SIZE(a)) { |
| 450 |
if (indexerr == NULL) { |
| 451 |
indexerr = PyString_FromString( |
| 452 |
"list index out of range"); |
| 453 |
if (indexerr == NULL) |
| 454 |
return NULL; |
| 455 |
} |
| 456 |
PyErr_SetObject(PyExc_IndexError, indexerr); |
| 457 |
return NULL; |
| 458 |
} |
| 459 |
Py_INCREF(a->ob_item[i]); |
| 460 |
return a->ob_item[i]; |
| 461 |
} |
| 462 |
|
| 463 |
static PyObject * |
| 464 |
list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
| 465 |
{ |
| 466 |
PyListObject *np; |
| 467 |
PyObject **src, **dest; |
| 468 |
Py_ssize_t i, len; |
| 469 |
if (ilow < 0) |
| 470 |
ilow = 0; |
| 471 |
else if (ilow > Py_SIZE(a)) |
| 472 |
ilow = Py_SIZE(a); |
| 473 |
if (ihigh < ilow) |
| 474 |
ihigh = ilow; |
| 475 |
else if (ihigh > Py_SIZE(a)) |
| 476 |
ihigh = Py_SIZE(a); |
| 477 |
len = ihigh - ilow; |
| 478 |
np = (PyListObject *) PyList_New(len); |
| 479 |
if (np == NULL) |
| 480 |
return NULL; |
| 481 |
|
| 482 |
src = a->ob_item + ilow; |
| 483 |
dest = np->ob_item; |
| 484 |
for (i = 0; i < len; i++) { |
| 485 |
PyObject *v = src[i]; |
| 486 |
Py_INCREF(v); |
| 487 |
dest[i] = v; |
| 488 |
} |
| 489 |
return (PyObject *)np; |
| 490 |
} |
| 491 |
|
| 492 |
PyObject * |
| 493 |
PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
| 494 |
{ |
| 495 |
if (!PyList_Check(a)) { |
| 496 |
PyErr_BadInternalCall(); |
| 497 |
return NULL; |
| 498 |
} |
| 499 |
return list_slice((PyListObject *)a, ilow, ihigh); |
| 500 |
} |
| 501 |
|
| 502 |
static PyObject * |
| 503 |
list_concat(PyListObject *a, PyObject *bb) |
| 504 |
{ |
| 505 |
Py_ssize_t size; |
| 506 |
Py_ssize_t i; |
| 507 |
PyObject **src, **dest; |
| 508 |
PyListObject *np; |
| 509 |
if (!PyList_Check(bb)) { |
| 510 |
PyErr_Format(PyExc_TypeError, |
| 511 |
"can only concatenate list (not \"%.200s\") to list", |
| 512 |
bb->ob_type->tp_name); |
| 513 |
return NULL; |
| 514 |
} |
| 515 |
#define b ((PyListObject *)bb) |
| 516 |
size = Py_SIZE(a) + Py_SIZE(b); |
| 517 |
if (size < 0) |
| 518 |
return PyErr_NoMemory(); |
| 519 |
np = (PyListObject *) PyList_New(size); |
| 520 |
if (np == NULL) { |
| 521 |
return NULL; |
| 522 |
} |
| 523 |
src = a->ob_item; |
| 524 |
dest = np->ob_item; |
| 525 |
for (i = 0; i < Py_SIZE(a); i++) { |
| 526 |
PyObject *v = src[i]; |
| 527 |
Py_INCREF(v); |
| 528 |
dest[i] = v; |
| 529 |
} |
| 530 |
src = b->ob_item; |
| 531 |
dest = np->ob_item + Py_SIZE(a); |
| 532 |
for (i = 0; i < Py_SIZE(b); i++) { |
| 533 |
PyObject *v = src[i]; |
| 534 |
Py_INCREF(v); |
| 535 |
dest[i] = v; |
| 536 |
} |
| 537 |
return (PyObject *)np; |
| 538 |
#undef b |
| 539 |
} |
| 540 |
|
| 541 |
static PyObject * |
| 542 |
list_repeat(PyListObject *a, Py_ssize_t n) |
| 543 |
{ |
| 544 |
Py_ssize_t i, j; |
| 545 |
Py_ssize_t size; |
| 546 |
PyListObject *np; |
| 547 |
PyObject **p, **items; |
| 548 |
PyObject *elem; |
| 549 |
if (n < 0) |
| 550 |
n = 0; |
| 551 |
if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) |
| 552 |
return PyErr_NoMemory(); |
| 553 |
size = Py_SIZE(a) * n; |
| 554 |
if (size == 0) |
| 555 |
return PyList_New(0); |
| 556 |
np = (PyListObject *) PyList_New(size); |
| 557 |
if (np == NULL) |
| 558 |
return NULL; |
| 559 |
|
| 560 |
items = np->ob_item; |
| 561 |
if (Py_SIZE(a) == 1) { |
| 562 |
elem = a->ob_item[0]; |
| 563 |
for (i = 0; i < n; i++) { |
| 564 |
items[i] = elem; |
| 565 |
Py_INCREF(elem); |
| 566 |
} |
| 567 |
return (PyObject *) np; |
| 568 |
} |
| 569 |
p = np->ob_item; |
| 570 |
items = a->ob_item; |
| 571 |
for (i = 0; i < n; i++) { |
| 572 |
for (j = 0; j < Py_SIZE(a); j++) { |
| 573 |
*p = items[j]; |
| 574 |
Py_INCREF(*p); |
| 575 |
p++; |
| 576 |
} |
| 577 |
} |
| 578 |
return (PyObject *) np; |
| 579 |
} |
| 580 |
|
| 581 |
static int |
| 582 |
list_clear(PyListObject *a) |
| 583 |
{ |
| 584 |
Py_ssize_t i; |
| 585 |
PyObject **item = a->ob_item; |
| 586 |
if (item != NULL) { |
| 587 |
|
| 588 |
|
| 589 |
i = Py_SIZE(a); |
| 590 |
Py_SIZE(a) = 0; |
| 591 |
a->ob_item = NULL; |
| 592 |
a->allocated = 0; |
| 593 |
while (--i >= 0) { |
| 594 |
Py_XDECREF(item[i]); |
| 595 |
} |
| 596 |
PyMem_FREE(item); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
static int |
| 611 |
list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) |
| 612 |
{ |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
PyObject *recycle_on_stack[8]; |
| 620 |
PyObject **recycle = recycle_on_stack; |
| 621 |
PyObject **item; |
| 622 |
PyObject **vitem = NULL; |
| 623 |
PyObject *v_as_SF = NULL; |
| 624 |
Py_ssize_t n; |
| 625 |
Py_ssize_t norig; |
| 626 |
Py_ssize_t d; |
| 627 |
Py_ssize_t k; |
| 628 |
size_t s; |
| 629 |
int result = -1; |
| 630 |
#define b ((PyListObject *)v) |
| 631 |
if (v == NULL) |
| 632 |
n = 0; |
| 633 |
else { |
| 634 |
if (a == b) { |
| 635 |
|
| 636 |
v = list_slice(b, 0, Py_SIZE(b)); |
| 637 |
if (v == NULL) |
| 638 |
return result; |
| 639 |
result = list_ass_slice(a, ilow, ihigh, v); |
| 640 |
Py_DECREF(v); |
| 641 |
return result; |
| 642 |
} |
| 643 |
v_as_SF = PySequence_Fast(v, "can only assign an iterable"); |
| 644 |
if(v_as_SF == NULL) |
| 645 |
goto Error; |
| 646 |
n = PySequence_Fast_GET_SIZE(v_as_SF); |
| 647 |
vitem = PySequence_Fast_ITEMS(v_as_SF); |
| 648 |
} |
| 649 |
if (ilow < 0) |
| 650 |
ilow = 0; |
| 651 |
else if (ilow > Py_SIZE(a)) |
| 652 |
ilow = Py_SIZE(a); |
| 653 |
|
| 654 |
if (ihigh < ilow) |
| 655 |
ihigh = ilow; |
| 656 |
else if (ihigh > Py_SIZE(a)) |
| 657 |
ihigh = Py_SIZE(a); |
| 658 |
|
| 659 |
norig = ihigh - ilow; |
| 660 |
assert(norig >= 0); |
| 661 |
d = n - norig; |
| 662 |
if (Py_SIZE(a) + d == 0) { |
| 663 |
Py_XDECREF(v_as_SF); |
| 664 |
return list_clear(a); |
| 665 |
} |
| 666 |
item = a->ob_item; |
| 667 |
|
| 668 |
s = norig * sizeof(PyObject *); |
| 669 |
|
| 670 |
if (s) { |
| 671 |
if (s > sizeof(recycle_on_stack)) { |
| 672 |
recycle = (PyObject **)PyMem_MALLOC(s); |
| 673 |
if (recycle == NULL) { |
| 674 |
PyErr_NoMemory(); |
| 675 |
goto Error; |
| 676 |
} |
| 677 |
} |
| 678 |
memcpy(recycle, &item[ilow], s); |
| 679 |
} |
| 680 |
|
| 681 |
if (d < 0) { |
| 682 |
memmove(&item[ihigh+d], &item[ihigh], |
| 683 |
(Py_SIZE(a) - ihigh)*sizeof(PyObject *)); |
| 684 |
list_resize(a, Py_SIZE(a) + d); |
| 685 |
item = a->ob_item; |
| 686 |
} |
| 687 |
else if (d > 0) { |
| 688 |
k = Py_SIZE(a); |
| 689 |
if (list_resize(a, k+d) < 0) |
| 690 |
goto Error; |
| 691 |
item = a->ob_item; |
| 692 |
memmove(&item[ihigh+d], &item[ihigh], |
| 693 |
(k - ihigh)*sizeof(PyObject *)); |
| 694 |
} |
| 695 |
for (k = 0; k < n; k++, ilow++) { |
| 696 |
PyObject *w = vitem[k]; |
| 697 |
Py_XINCREF(w); |
| 698 |
item[ilow] = w; |
| 699 |
} |
| 700 |
for (k = norig - 1; k >= 0; --k) |
| 701 |
Py_XDECREF(recycle[k]); |
| 702 |
result = 0; |
| 703 |
Error: |
| 704 |
if (recycle != recycle_on_stack) |
| 705 |
PyMem_FREE(recycle); |
| 706 |
Py_XDECREF(v_as_SF); |
| 707 |
return result; |
| 708 |
#undef b |
| 709 |
} |
| 710 |
|
| 711 |
int |
| 712 |
PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) |
| 713 |
{ |
| 714 |
if (!PyList_Check(a)) { |
| 715 |
PyErr_BadInternalCall(); |
| 716 |
return -1; |
| 717 |
} |
| 718 |
return list_ass_slice((PyListObject *)a, ilow, ihigh, v); |
| 719 |
} |
| 720 |
|
| 721 |
static PyObject * |
| 722 |
list_inplace_repeat(PyListObject *self, Py_ssize_t n) |
| 723 |
{ |
| 724 |
PyObject **items; |
| 725 |
Py_ssize_t size, i, j, p; |
| 726 |
|
| 727 |
|
| 728 |
size = PyList_GET_SIZE(self); |
| 729 |
if (size == 0 || n == 1) { |
| 730 |
Py_INCREF(self); |
| 731 |
return (PyObject *)self; |
| 732 |
} |
| 733 |
|
| 734 |
if (n < 1) { |
| 735 |
(void)list_clear(self); |
| 736 |
Py_INCREF(self); |
| 737 |
return (PyObject *)self; |
| 738 |
} |
| 739 |
|
| 740 |
if (size > PY_SSIZE_T_MAX / n) { |
| 741 |
return PyErr_NoMemory(); |
| 742 |
} |
| 743 |
|
| 744 |
if (list_resize(self, size*n) == -1) |
| 745 |
return NULL; |
| 746 |
|
| 747 |
p = size; |
| 748 |
items = self->ob_item; |
| 749 |
for (i = 1; i < n; i++) { |
| 750 |
for (j = 0; j < size; j++) { |
| 751 |
PyObject *o = items[j]; |
| 752 |
Py_INCREF(o); |
| 753 |
items[p++] = o; |
| 754 |
} |
| 755 |
} |
| 756 |
Py_INCREF(self); |
| 757 |
return (PyObject *)self; |
| 758 |
} |
| 759 |
|
| 760 |
static int |
| 761 |
list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v) |
| 762 |
{ |
| 763 |
PyObject *old_value; |
| 764 |
if (i < 0 || i >= Py_SIZE(a)) { |
| 765 |
PyErr_SetString(PyExc_IndexError, |
| 766 |
"list assignment index out of range"); |
| 767 |
return -1; |
| 768 |
} |
| 769 |
if (v == NULL) |
| 770 |
return list_ass_slice(a, i, i+1, v); |
| 771 |
Py_INCREF(v); |
| 772 |
old_value = a->ob_item[i]; |
| 773 |
a->ob_item[i] = v; |
| 774 |
Py_DECREF(old_value); |
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
static PyObject * |
| 779 |
listinsert(PyListObject *self, PyObject *args) |
| 780 |
{ |
| 781 |
Py_ssize_t i; |
| 782 |
PyObject *v; |
| 783 |
if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) |
| 784 |
return NULL; |
| 785 |
if (ins1(self, i, v) == 0) |
| 786 |
Py_RETURN_NONE; |
| 787 |
return NULL; |
| 788 |
} |
| 789 |
|
| 790 |
static PyObject * |
| 791 |
listappend(PyListObject *self, PyObject *v) |
| 792 |
{ |
| 793 |
if (app1(self, v) == 0) |
| 794 |
Py_RETURN_NONE; |
| 795 |
return NULL; |
| 796 |
} |
| 797 |
|
| 798 |
static PyObject * |
| 799 |
listextend(PyListObject *self, PyObject *b) |
| 800 |
{ |
| 801 |
PyObject *it; |
| 802 |
Py_ssize_t m; |
| 803 |
Py_ssize_t n; |
| 804 |
Py_ssize_t mn; |
| 805 |
Py_ssize_t i; |
| 806 |
PyObject *(*iternext)(PyObject *); |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) { |
| 813 |
PyObject **src, **dest; |
| 814 |
b = PySequence_Fast(b, "argument must be iterable"); |
| 815 |
if (!b) |
| 816 |
return NULL; |
| 817 |
n = PySequence_Fast_GET_SIZE(b); |
| 818 |
if (n == 0) { |
| 819 |
|
| 820 |
Py_DECREF(b); |
| 821 |
Py_RETURN_NONE; |
| 822 |
} |
| 823 |
m = Py_SIZE(self); |
| 824 |
if (list_resize(self, m + n) == -1) { |
| 825 |
Py_DECREF(b); |
| 826 |
return NULL; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
src = PySequence_Fast_ITEMS(b); |
| 835 |
dest = self->ob_item + m; |
| 836 |
for (i = 0; i < n; i++) { |
| 837 |
PyObject *o = src[i]; |
| 838 |
Py_INCREF(o); |
| 839 |
dest[i] = o; |
| 840 |
} |
| 841 |
Py_DECREF(b); |
| 842 |
Py_RETURN_NONE; |
| 843 |
} |
| 844 |
|
| 845 |
it = PyObject_GetIter(b); |
| 846 |
if (it == NULL) |
| 847 |
return NULL; |
| 848 |
iternext = *it->ob_type->tp_iternext; |
| 849 |
|
| 850 |
|
| 851 |
n = _PyObject_LengthHint(b, 8); |
| 852 |
if (n == -1) { |
| 853 |
Py_DECREF(it); |
| 854 |
return NULL; |
| 855 |
} |
| 856 |
m = Py_SIZE(self); |
| 857 |
mn = m + n; |
| 858 |
if (mn >= m) { |
| 859 |
|
| 860 |
if (list_resize(self, mn) == -1) |
| 861 |
goto error; |
| 862 |
|
| 863 |
Py_SIZE(self) = m; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
for (;;) { |
| 872 |
PyObject *item = iternext(it); |
| 873 |
if (item == NULL) { |
| 874 |
if (PyErr_Occurred()) { |
| 875 |
if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 876 |
PyErr_Clear(); |
| 877 |
else |
| 878 |
goto error; |
| 879 |
} |
| 880 |
break; |
| 881 |
} |
| 882 |
if (Py_SIZE(self) < self->allocated) { |
| 883 |
|
| 884 |
PyList_SET_ITEM(self, Py_SIZE(self), item); |
| 885 |
++Py_SIZE(self); |
| 886 |
} |
| 887 |
else { |
| 888 |
int status = app1(self, item); |
| 889 |
Py_DECREF(item); |
| 890 |
if (status < 0) |
| 891 |
goto error; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
if (Py_SIZE(self) < self->allocated) |
| 897 |
list_resize(self, Py_SIZE(self)); |
| 898 |
|
| 899 |
Py_DECREF(it); |
| 900 |
Py_RETURN_NONE; |
| 901 |
|
| 902 |
error: |
| 903 |
Py_DECREF(it); |
| 904 |
return NULL; |
| 905 |
} |
| 906 |
|
| 907 |
PyObject * |
| 908 |
_PyList_Extend(PyListObject *self, PyObject *b) |
| 909 |
{ |
| 910 |
return listextend(self, b); |
| 911 |
} |
| 912 |
|
| 913 |
static PyObject * |
| 914 |
list_inplace_concat(PyListObject *self, PyObject *other) |
| 915 |
{ |
| 916 |
PyObject *result; |
| 917 |
|
| 918 |
result = listextend(self, other); |
| 919 |
if (result == NULL) |
| 920 |
return result; |
| 921 |
Py_DECREF(result); |
| 922 |
Py_INCREF(self); |
| 923 |
return (PyObject *)self; |
| 924 |
} |
| 925 |
|
| 926 |
static PyObject * |
| 927 |
listpop(PyListObject *self, PyObject *args) |
| 928 |
{ |
| 929 |
Py_ssize_t i = -1; |
| 930 |
PyObject *v; |
| 931 |
int status; |
| 932 |
|
| 933 |
if (!PyArg_ParseTuple(args, "|n:pop", &i)) |
| 934 |
return NULL; |
| 935 |
|
| 936 |
if (Py_SIZE(self) == 0) { |
| 937 |
|
| 938 |
PyErr_SetString(PyExc_IndexError, "pop from empty list"); |
| 939 |
return NULL; |
| 940 |
} |
| 941 |
if (i < 0) |
| 942 |
i += Py_SIZE(self); |
| 943 |
if (i < 0 || i >= Py_SIZE(self)) { |
| 944 |
PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 945 |
return NULL; |
| 946 |
} |
| 947 |
v = self->ob_item[i]; |
| 948 |
if (i == Py_SIZE(self) - 1) { |
| 949 |
status = list_resize(self, Py_SIZE(self) - 1); |
| 950 |
assert(status >= 0); |
| 951 |
return v; |
| 952 |
} |
| 953 |
Py_INCREF(v); |
| 954 |
status = list_ass_slice(self, i, i+1, (PyObject *)NULL); |
| 955 |
assert(status >= 0); |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
(void) status; |
| 960 |
|
| 961 |
return v; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
static void |
| 966 |
reverse_slice(PyObject **lo, PyObject **hi) |
| 967 |
{ |
| 968 |
assert(lo && hi); |
| 969 |
|
| 970 |
--hi; |
| 971 |
while (lo < hi) { |
| 972 |
PyObject *t = *lo; |
| 973 |
*lo = *hi; |
| 974 |
*hi = t; |
| 975 |
++lo; |
| 976 |
--hi; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
static int |
| 991 |
islt(PyObject *x, PyObject *y, PyObject *compare) |
| 992 |
{ |
| 993 |
PyObject *res; |
| 994 |
PyObject *args; |
| 995 |
Py_ssize_t i; |
| 996 |
|
| 997 |
assert(compare != NULL); |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
args = PyTuple_New(2); |
| 1002 |
if (args == NULL) |
| 1003 |
return -1; |
| 1004 |
Py_INCREF(x); |
| 1005 |
Py_INCREF(y); |
| 1006 |
PyTuple_SET_ITEM(args, 0, x); |
| 1007 |
PyTuple_SET_ITEM(args, 1, y); |
| 1008 |
res = PyObject_Call(compare, args, NULL); |
| 1009 |
Py_DECREF(args); |
| 1010 |
if (res == NULL) |
| 1011 |
return -1; |
| 1012 |
if (!PyInt_Check(res)) { |
| 1013 |
PyErr_Format(PyExc_TypeError, |
| 1014 |
"comparison function must return int, not %.200s", |
| 1015 |
res->ob_type->tp_name); |
| 1016 |
Py_DECREF(res); |
| 1017 |
return -1; |
| 1018 |
} |
| 1019 |
i = PyInt_AsLong(res); |
| 1020 |
Py_DECREF(res); |
| 1021 |
return i < 0; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
#define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \ |
| 1030 |
PyObject_RichCompareBool(X, Y, Py_LT) : \ |
| 1031 |
islt(X, Y, COMPARE)) |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
#define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \ |
| 1038 |
if (k) |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
static int |
| 1052 |
binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare) |
| 1053 |
|
| 1054 |
{ |
| 1055 |
register Py_ssize_t k; |
| 1056 |
register PyObject **l, **p, **r; |
| 1057 |
register PyObject *pivot; |
| 1058 |
|
| 1059 |
assert(lo <= start && start <= hi); |
| 1060 |
|
| 1061 |
if (lo == start) |
| 1062 |
++start; |
| 1063 |
for (; start < hi; ++start) { |
| 1064 |
|
| 1065 |
l = lo; |
| 1066 |
r = start; |
| 1067 |
pivot = *r; |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
assert(l < r); |
| 1074 |
do { |
| 1075 |
p = l + ((r - l) >> 1); |
| 1076 |
IFLT(pivot, *p) |
| 1077 |
r = p; |
| 1078 |
else |
| 1079 |
l = p+1; |
| 1080 |
} while (l < r); |
| 1081 |
assert(l == r); |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
for (p = start; p > l; --p) |
| 1090 |
*p = *(p-1); |
| 1091 |
*l = pivot; |
| 1092 |
} |
| 1093 |
return 0; |
| 1094 |
|
| 1095 |
fail: |
| 1096 |
return -1; |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
static Py_ssize_t |
| 1118 |
count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending) |
| 1119 |
{ |
| 1120 |
Py_ssize_t k; |
| 1121 |
Py_ssize_t n; |
| 1122 |
|
| 1123 |
assert(lo < hi); |
| 1124 |
*descending = 0; |
| 1125 |
++lo; |
| 1126 |
if (lo == hi) |
| 1127 |
return 1; |
| 1128 |
|
| 1129 |
n = 2; |
| 1130 |
IFLT(*lo, *(lo-1)) { |
| 1131 |
*descending = 1; |
| 1132 |
for (lo = lo+1; lo < hi; ++lo, ++n) { |
| 1133 |
IFLT(*lo, *(lo-1)) |
| 1134 |
; |
| 1135 |
else |
| 1136 |
break; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
else { |
| 1140 |
for (lo = lo+1; lo < hi; ++lo, ++n) { |
| 1141 |
IFLT(*lo, *(lo-1)) |
| 1142 |
break; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
return n; |
| 1147 |
fail: |
| 1148 |
return -1; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
static Py_ssize_t |
| 1173 |
gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare) |
| 1174 |
{ |
| 1175 |
Py_ssize_t ofs; |
| 1176 |
Py_ssize_t lastofs; |
| 1177 |
Py_ssize_t k; |
| 1178 |
|
| 1179 |
assert(key && a && n > 0 && hint >= 0 && hint < n); |
| 1180 |
|
| 1181 |
a += hint; |
| 1182 |
lastofs = 0; |
| 1183 |
ofs = 1; |
| 1184 |
IFLT(*a, key) { |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
const Py_ssize_t maxofs = n - hint; |
| 1189 |
while (ofs < maxofs) { |
| 1190 |
IFLT(a[ofs], key) { |
| 1191 |
lastofs = ofs; |
| 1192 |
ofs = (ofs << 1) + 1; |
| 1193 |
if (ofs <= 0) |
| 1194 |
ofs = maxofs; |
| 1195 |
} |
| 1196 |
else |
| 1197 |
break; |
| 1198 |
} |
| 1199 |
if (ofs > maxofs) |
| 1200 |
ofs = maxofs; |
| 1201 |
|
| 1202 |
lastofs += hint; |
| 1203 |
ofs += hint; |
| 1204 |
} |
| 1205 |
else { |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
const Py_ssize_t maxofs = hint + 1; |
| 1210 |
while (ofs < maxofs) { |
| 1211 |
IFLT(*(a-ofs), key) |
| 1212 |
break; |
| 1213 |
|
| 1214 |
lastofs = ofs; |
| 1215 |
ofs = (ofs << 1) + 1; |
| 1216 |
if (ofs <= 0) |
| 1217 |
ofs = maxofs; |
| 1218 |
} |
| 1219 |
if (ofs > maxofs) |
| 1220 |
ofs = maxofs; |
| 1221 |
|
| 1222 |
k = lastofs; |
| 1223 |
lastofs = hint - ofs; |
| 1224 |
ofs = hint - k; |
| 1225 |
} |
| 1226 |
a -= hint; |
| 1227 |
|
| 1228 |
assert(-1 <= lastofs && lastofs < ofs && ofs <= n); |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
++lastofs; |
| 1234 |
while (lastofs < ofs) { |
| 1235 |
Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1); |
| 1236 |
|
| 1237 |
IFLT(a[m], key) |
| 1238 |
lastofs = m+1; |
| 1239 |
else |
| 1240 |
ofs = m; |
| 1241 |
} |
| 1242 |
assert(lastofs == ofs); |
| 1243 |
return ofs; |
| 1244 |
|
| 1245 |
fail: |
| 1246 |
return -1; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
static Py_ssize_t |
| 1264 |
gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare) |
| 1265 |
{ |
| 1266 |
Py_ssize_t ofs; |
| 1267 |
Py_ssize_t lastofs; |
| 1268 |
Py_ssize_t k; |
| 1269 |
|
| 1270 |
assert(key && a && n > 0 && hint >= 0 && hint < n); |
| 1271 |
|
| 1272 |
a += hint; |
| 1273 |
lastofs = 0; |
| 1274 |
ofs = 1; |
| 1275 |
IFLT(key, *a) { |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
const Py_ssize_t maxofs = hint + 1; |
| 1280 |
while (ofs < maxofs) { |
| 1281 |
IFLT(key, *(a-ofs)) { |
| 1282 |
lastofs = ofs; |
| 1283 |
ofs = (ofs << 1) + 1; |
| 1284 |
if (ofs <= 0) |
| 1285 |
ofs = maxofs; |
| 1286 |
} |
| 1287 |
else |
| 1288 |
break; |
| 1289 |
} |
| 1290 |
if (ofs > maxofs) |
| 1291 |
ofs = maxofs; |
| 1292 |
|
| 1293 |
k = lastofs; |
| 1294 |
lastofs = hint - ofs; |
| 1295 |
ofs = hint - k; |
| 1296 |
} |
| 1297 |
else { |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
const Py_ssize_t maxofs = n - hint; |
| 1302 |
while (ofs < maxofs) { |
| 1303 |
IFLT(key, a[ofs]) |
| 1304 |
break; |
| 1305 |
|
| 1306 |
lastofs = ofs; |
| 1307 |
ofs = (ofs << 1) + 1; |
| 1308 |
if (ofs <= 0) |
| 1309 |
ofs = maxofs; |
| 1310 |
} |
| 1311 |
if (ofs > maxofs) |
| 1312 |
ofs = maxofs; |
| 1313 |
|
| 1314 |
lastofs += hint; |
| 1315 |
ofs += hint; |
| 1316 |
} |
| 1317 |
a -= hint; |
| 1318 |
|
| 1319 |
assert(-1 <= lastofs && lastofs < ofs && ofs <= n); |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
++lastofs; |
| 1325 |
while (lastofs < ofs) { |
| 1326 |
Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1); |
| 1327 |
|
| 1328 |
IFLT(key, a[m]) |
| 1329 |
ofs = m; |
| 1330 |
else |
| 1331 |
lastofs = m+1; |
| 1332 |
} |
| 1333 |
assert(lastofs == ofs); |
| 1334 |
return ofs; |
| 1335 |
|
| 1336 |
fail: |
| 1337 |
return -1; |
| 1338 |
} |
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
#define MAX_MERGE_PENDING 85 |
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
#define MIN_GALLOP 7 |
| 1352 |
|
| 1353 |
|
| 1354 |
#define MERGESTATE_TEMP_SIZE 256 |
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
struct s_slice { |
| 1360 |
PyObject **base; |
| 1361 |
Py_ssize_t len; |
| 1362 |
}; |
| 1363 |
|
| 1364 |
typedef struct s_MergeState { |
| 1365 |
|
| 1366 |
PyObject *compare; |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
Py_ssize_t min_gallop; |
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
PyObject **a; |
| 1378 |
Py_ssize_t alloced; |
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
int n; |
| 1390 |
struct s_slice pending[MAX_MERGE_PENDING]; |
| 1391 |
|
| 1392 |
|
| 1393 |
PyObject *temparray[MERGESTATE_TEMP_SIZE]; |
| 1394 |
} MergeState; |
| 1395 |
|
| 1396 |
|
| 1397 |
static void |
| 1398 |
merge_init(MergeState *ms, PyObject *compare) |
| 1399 |
{ |
| 1400 |
assert(ms != NULL); |
| 1401 |
ms->compare = compare; |
| 1402 |
ms->a = ms->temparray; |
| 1403 |
ms->alloced = MERGESTATE_TEMP_SIZE; |
| 1404 |
ms->n = 0; |
| 1405 |
ms->min_gallop = MIN_GALLOP; |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
static void |
| 1413 |
merge_freemem(MergeState *ms) |
| 1414 |
{ |
| 1415 |
assert(ms != NULL); |
| 1416 |
if (ms->a != ms->temparray) |
| 1417 |
PyMem_Free(ms->a); |
| 1418 |
ms->a = ms->temparray; |
| 1419 |
ms->alloced = MERGESTATE_TEMP_SIZE; |
| 1420 |
} |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
static int |
| 1426 |
merge_getmem(MergeState *ms, Py_ssize_t need) |
| 1427 |
{ |
| 1428 |
assert(ms != NULL); |
| 1429 |
if (need <= ms->alloced) |
| 1430 |
return 0; |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
merge_freemem(ms); |
| 1435 |
if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*)) { |
| 1436 |
PyErr_NoMemory(); |
| 1437 |
return -1; |
| 1438 |
} |
| 1439 |
ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*)); |
| 1440 |
if (ms->a) { |
| 1441 |
ms->alloced = need; |
| 1442 |
return 0; |
| 1443 |
} |
| 1444 |
PyErr_NoMemory(); |
| 1445 |
merge_freemem(ms); |
| 1446 |
return -1; |
| 1447 |
} |
| 1448 |
#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \ |
| 1449 |
merge_getmem(MS, NEED)) |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
static Py_ssize_t |
| 1458 |
merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na, |
| 1459 |
PyObject **pb, Py_ssize_t nb) |
| 1460 |
{ |
| 1461 |
Py_ssize_t k; |
| 1462 |
PyObject *compare; |
| 1463 |
PyObject **dest; |
| 1464 |
int result = -1; |
| 1465 |
Py_ssize_t min_gallop; |
| 1466 |
|
| 1467 |
assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); |
| 1468 |
if (MERGE_GETMEM(ms, na) < 0) |
| 1469 |
return -1; |
| 1470 |
memcpy(ms->a, pa, na * sizeof(PyObject*)); |
| 1471 |
dest = pa; |
| 1472 |
pa = ms->a; |
| 1473 |
|
| 1474 |
*dest++ = *pb++; |
| 1475 |
--nb; |
| 1476 |
if (nb == 0) |
| 1477 |
goto Succeed; |
| 1478 |
if (na == 1) |
| 1479 |
goto CopyB; |
| 1480 |
|
| 1481 |
min_gallop = ms->min_gallop; |
| 1482 |
compare = ms->compare; |
| 1483 |
for (;;) { |
| 1484 |
Py_ssize_t acount = 0; |
| 1485 |
Py_ssize_t bcount = 0; |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
for (;;) { |
| 1491 |
assert(na > 1 && nb > 0); |
| 1492 |
k = ISLT(*pb, *pa, compare); |
| 1493 |
if (k) { |
| 1494 |
if (k < 0) |
| 1495 |
goto Fail; |
| 1496 |
*dest++ = *pb++; |
| 1497 |
++bcount; |
| 1498 |
acount = 0; |
| 1499 |
--nb; |
| 1500 |
if (nb == 0) |
| 1501 |
goto Succeed; |
| 1502 |
if (bcount >= min_gallop) |
| 1503 |
break; |
| 1504 |
} |
| 1505 |
else { |
| 1506 |
*dest++ = *pa++; |
| 1507 |
++acount; |
| 1508 |
bcount = 0; |
| 1509 |
--na; |
| 1510 |
if (na == 1) |
| 1511 |
goto CopyB; |
| 1512 |
if (acount >= min_gallop) |
| 1513 |
break; |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
++min_gallop; |
| 1523 |
do { |
| 1524 |
assert(na > 1 && nb > 0); |
| 1525 |
min_gallop -= min_gallop > 1; |
| 1526 |
ms->min_gallop = min_gallop; |
| 1527 |
k = gallop_right(*pb, pa, na, 0, compare); |
| 1528 |
acount = k; |
| 1529 |
if (k) { |
| 1530 |
if (k < 0) |
| 1531 |
goto Fail; |
| 1532 |
memcpy(dest, pa, k * sizeof(PyObject *)); |
| 1533 |
dest += k; |
| 1534 |
pa += k; |
| 1535 |
na -= k; |
| 1536 |
if (na == 1) |
| 1537 |
goto CopyB; |
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
if (na == 0) |
| 1543 |
goto Succeed; |
| 1544 |
} |
| 1545 |
*dest++ = *pb++; |
| 1546 |
--nb; |
| 1547 |
if (nb == 0) |
| 1548 |
goto Succeed; |
| 1549 |
|
| 1550 |
k = gallop_left(*pa, pb, nb, 0, compare); |
| 1551 |
bcount = k; |
| 1552 |
if (k) { |
| 1553 |
if (k < 0) |
| 1554 |
goto Fail; |
| 1555 |
memmove(dest, pb, k * sizeof(PyObject *)); |
| 1556 |
dest += k; |
| 1557 |
pb += k; |
| 1558 |
nb -= k; |
| 1559 |
if (nb == 0) |
| 1560 |
goto Succeed; |
| 1561 |
} |
| 1562 |
*dest++ = *pa++; |
| 1563 |
--na; |
| 1564 |
if (na == 1) |
| 1565 |
goto CopyB; |
| 1566 |
} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
| 1567 |
++min_gallop; |
| 1568 |
ms->min_gallop = min_gallop; |
| 1569 |
} |
| 1570 |
Succeed: |
| 1571 |
result = 0; |
| 1572 |
Fail: |
| 1573 |
if (na) |
| 1574 |
memcpy(dest, pa, na * sizeof(PyObject*)); |
| 1575 |
return result; |
| 1576 |
CopyB: |
| 1577 |
assert(na == 1 && nb > 0); |
| 1578 |
|
| 1579 |
memmove(dest, pb, nb * sizeof(PyObject *)); |
| 1580 |
dest[nb] = *pa; |
| 1581 |
return 0; |
| 1582 |
} |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
static Py_ssize_t |
| 1591 |
merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb) |
| 1592 |
{ |
| 1593 |
Py_ssize_t k; |
| 1594 |
PyObject *compare; |
| 1595 |
PyObject **dest; |
| 1596 |
int result = -1; |
| 1597 |
PyObject **basea; |
| 1598 |
PyObject **baseb; |
| 1599 |
Py_ssize_t min_gallop; |
| 1600 |
|
| 1601 |
assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); |
| 1602 |
if (MERGE_GETMEM(ms, nb) < 0) |
| 1603 |
return -1; |
| 1604 |
dest = pb + nb - 1; |
| 1605 |
memcpy(ms->a, pb, nb * sizeof(PyObject*)); |
| 1606 |
basea = pa; |
| 1607 |
baseb = ms->a; |
| 1608 |
pb = ms->a + nb - 1; |
| 1609 |
pa += na - 1; |
| 1610 |
|
| 1611 |
*dest-- = *pa--; |
| 1612 |
--na; |
| 1613 |
if (na == 0) |
| 1614 |
goto Succeed; |
| 1615 |
if (nb == 1) |
| 1616 |
goto CopyA; |
| 1617 |
|
| 1618 |
min_gallop = ms->min_gallop; |
| 1619 |
compare = ms->compare; |
| 1620 |
for (;;) { |
| 1621 |
Py_ssize_t acount = 0; |
| 1622 |
Py_ssize_t bcount = 0; |
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
for (;;) { |
| 1628 |
assert(na > 0 && nb > 1); |
| 1629 |
k = ISLT(*pb, *pa, compare); |
| 1630 |
if (k) { |
| 1631 |
if (k < 0) |
| 1632 |
goto Fail; |
| 1633 |
*dest-- = *pa--; |
| 1634 |
++acount; |
| 1635 |
bcount = 0; |
| 1636 |
--na; |
| 1637 |
if (na == 0) |
| 1638 |
goto Succeed; |
| 1639 |
if (acount >= min_gallop) |
| 1640 |
break; |
| 1641 |
} |
| 1642 |
else { |
| 1643 |
*dest-- = *pb--; |
| 1644 |
++bcount; |
| 1645 |
acount = 0; |
| 1646 |
--nb; |
| 1647 |
if (nb == 1) |
| 1648 |
goto CopyA; |
| 1649 |
if (bcount >= min_gallop) |
| 1650 |
break; |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
++min_gallop; |
| 1660 |
do { |
| 1661 |
assert(na > 0 && nb > 1); |
| 1662 |
min_gallop -= min_gallop > 1; |
| 1663 |
ms->min_gallop = min_gallop; |
| 1664 |
k = gallop_right(*pb, basea, na, na-1, compare); |
| 1665 |
if (k < 0) |
| 1666 |
goto Fail; |
| 1667 |
k = na - k; |
| 1668 |
acount = k; |
| 1669 |
if (k) { |
| 1670 |
dest -= k; |
| 1671 |
pa -= k; |
| 1672 |
memmove(dest+1, pa+1, k * sizeof(PyObject *)); |
| 1673 |
na -= k; |
| 1674 |
if (na == 0) |
| 1675 |
goto Succeed; |
| 1676 |
} |
| 1677 |
*dest-- = *pb--; |
| 1678 |
--nb; |
| 1679 |
if (nb == 1) |
| 1680 |
goto CopyA; |
| 1681 |
|
| 1682 |
k = gallop_left(*pa, baseb, nb, nb-1, compare); |
| 1683 |
if (k < 0) |
| 1684 |
goto Fail; |
| 1685 |
k = nb - k; |
| 1686 |
bcount = k; |
| 1687 |
if (k) { |
| 1688 |
dest -= k; |
| 1689 |
pb -= k; |
| 1690 |
memcpy(dest+1, pb+1, k * sizeof(PyObject *)); |
| 1691 |
nb -= k; |
| 1692 |
if (nb == 1) |
| 1693 |
goto CopyA; |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
if (nb == 0) |
| 1699 |
goto Succeed; |
| 1700 |
} |
| 1701 |
*dest-- = *pa--; |
| 1702 |
--na; |
| 1703 |
if (na == 0) |
| 1704 |
goto Succeed; |
| 1705 |
} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
| 1706 |
++min_gallop; |
| 1707 |
ms->min_gallop = min_gallop; |
| 1708 |
} |
| 1709 |
Succeed: |
| 1710 |
result = 0; |
| 1711 |
Fail: |
| 1712 |
if (nb) |
| 1713 |
memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*)); |
| 1714 |
return result; |
| 1715 |
CopyA: |
| 1716 |
assert(nb == 1 && na > 0); |
| 1717 |
|
| 1718 |
dest -= na; |
| 1719 |
pa -= na; |
| 1720 |
memmove(dest+1, pa+1, na * sizeof(PyObject *)); |
| 1721 |
*dest = *pb; |
| 1722 |
return 0; |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
static Py_ssize_t |
| 1729 |
merge_at(MergeState *ms, Py_ssize_t i) |
| 1730 |
{ |
| 1731 |
PyObject **pa, **pb; |
| 1732 |
Py_ssize_t na, nb; |
| 1733 |
Py_ssize_t k; |
| 1734 |
PyObject *compare; |
| 1735 |
|
| 1736 |
assert(ms != NULL); |
| 1737 |
assert(ms->n >= 2); |
| 1738 |
assert(i >= 0); |
| 1739 |
assert(i == ms->n - 2 || i == ms->n - 3); |
| 1740 |
|
| 1741 |
pa = ms->pending[i].base; |
| 1742 |
na = ms->pending[i].len; |
| 1743 |
pb = ms->pending[i+1].base; |
| 1744 |
nb = ms->pending[i+1].len; |
| 1745 |
assert(na > 0 && nb > 0); |
| 1746 |
assert(pa + na == pb); |
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
ms->pending[i].len = na + nb; |
| 1753 |
if (i == ms->n - 3) |
| 1754 |
ms->pending[i+1] = ms->pending[i+2]; |
| 1755 |
--ms->n; |
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
compare = ms->compare; |
| 1761 |
k = gallop_right(*pb, pa, na, 0, compare); |
| 1762 |
if (k < 0) |
| 1763 |
return -1; |
| 1764 |
pa += k; |
| 1765 |
na -= k; |
| 1766 |
if (na == 0) |
| 1767 |
return 0; |
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
nb = gallop_left(pa[na-1], pb, nb, nb-1, compare); |
| 1773 |
if (nb <= 0) |
| 1774 |
return nb; |
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
if (na <= nb) |
| 1780 |
return merge_lo(ms, pa, na, pb, nb); |
| 1781 |
else |
| 1782 |
return merge_hi(ms, pa, na, pb, nb); |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
static int |
| 1796 |
merge_collapse(MergeState *ms) |
| 1797 |
{ |
| 1798 |
struct s_slice *p = ms->pending; |
| 1799 |
|
| 1800 |
assert(ms); |
| 1801 |
while (ms->n > 1) { |
| 1802 |
Py_ssize_t n = ms->n - 2; |
| 1803 |
if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) || |
| 1804 |
(n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) { |
| 1805 |
if (p[n-1].len < p[n+1].len) |
| 1806 |
--n; |
| 1807 |
if (merge_at(ms, n) < 0) |
| 1808 |
return -1; |
| 1809 |
} |
| 1810 |
else if (p[n].len <= p[n+1].len) { |
| 1811 |
if (merge_at(ms, n) < 0) |
| 1812 |
return -1; |
| 1813 |
} |
| 1814 |
else |
| 1815 |
break; |
| 1816 |
} |
| 1817 |
return 0; |
| 1818 |
} |
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
static int |
| 1826 |
merge_force_collapse(MergeState *ms) |
| 1827 |
{ |
| 1828 |
struct s_slice *p = ms->pending; |
| 1829 |
|
| 1830 |
assert(ms); |
| 1831 |
while (ms->n > 1) { |
| 1832 |
Py_ssize_t n = ms->n - 2; |
| 1833 |
if (n > 0 && p[n-1].len < p[n+1].len) |
| 1834 |
--n; |
| 1835 |
if (merge_at(ms, n) < 0) |
| 1836 |
return -1; |
| 1837 |
} |
| 1838 |
return 0; |
| 1839 |
} |
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
static Py_ssize_t |
| 1852 |
merge_compute_minrun(Py_ssize_t n) |
| 1853 |
{ |
| 1854 |
Py_ssize_t r = 0; |
| 1855 |
|
| 1856 |
assert(n >= 0); |
| 1857 |
while (n >= 64) { |
| 1858 |
r |= n & 1; |
| 1859 |
n >>= 1; |
| 1860 |
} |
| 1861 |
return n + r; |
| 1862 |
} |
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
typedef struct { |
| 1872 |
PyObject_HEAD |
| 1873 |
PyObject *key; |
| 1874 |
PyObject *value; |
| 1875 |
} sortwrapperobject; |
| 1876 |
|
| 1877 |
PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key."); |
| 1878 |
static PyObject * |
| 1879 |
sortwrapper_richcompare(sortwrapperobject *, sortwrapperobject *, int); |
| 1880 |
static void |
| 1881 |
sortwrapper_dealloc(sortwrapperobject *); |
| 1882 |
|
| 1883 |
static PyTypeObject sortwrapper_type = { |
| 1884 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1885 |
"sortwrapper", |
| 1886 |
sizeof(sortwrapperobject), |
| 1887 |
0, |
| 1888 |
|
| 1889 |
(destructor)sortwrapper_dealloc, |
| 1890 |
0, |
| 1891 |
0, |
| 1892 |
0, |
| 1893 |
0, |
| 1894 |
0, |
| 1895 |
0, |
| 1896 |
0, |
| 1897 |
0, |
| 1898 |
0, |
| 1899 |
0, |
| 1900 |
0, |
| 1901 |
PyObject_GenericGetAttr, |
| 1902 |
0, |
| 1903 |
0, |
| 1904 |
Py_TPFLAGS_DEFAULT | |
| 1905 |
Py_TPFLAGS_HAVE_RICHCOMPARE, |
| 1906 |
sortwrapper_doc, |
| 1907 |
0, |
| 1908 |
0, |
| 1909 |
(richcmpfunc)sortwrapper_richcompare, |
| 1910 |
}; |
| 1911 |
|
| 1912 |
|
| 1913 |
static PyObject * |
| 1914 |
sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op) |
| 1915 |
{ |
| 1916 |
if (!PyObject_TypeCheck(b, &sortwrapper_type)) { |
| 1917 |
PyErr_SetString(PyExc_TypeError, |
| 1918 |
"expected a sortwrapperobject"); |
| 1919 |
return NULL; |
| 1920 |
} |
| 1921 |
return PyObject_RichCompare(a->key, b->key, op); |
| 1922 |
} |
| 1923 |
|
| 1924 |
static void |
| 1925 |
sortwrapper_dealloc(sortwrapperobject *so) |
| 1926 |
{ |
| 1927 |
Py_XDECREF(so->key); |
| 1928 |
Py_XDECREF(so->value); |
| 1929 |
PyObject_Del(so); |
| 1930 |
} |
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
static PyObject * |
| 1936 |
build_sortwrapper(PyObject *key, PyObject *value) |
| 1937 |
{ |
| 1938 |
sortwrapperobject *so; |
| 1939 |
|
| 1940 |
so = PyObject_New(sortwrapperobject, &sortwrapper_type); |
| 1941 |
if (so == NULL) |
| 1942 |
return NULL; |
| 1943 |
so->key = key; |
| 1944 |
so->value = value; |
| 1945 |
return (PyObject *)so; |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
static PyObject * |
| 1950 |
sortwrapper_getvalue(PyObject *so) |
| 1951 |
{ |
| 1952 |
PyObject *value; |
| 1953 |
|
| 1954 |
if (!PyObject_TypeCheck(so, &sortwrapper_type)) { |
| 1955 |
PyErr_SetString(PyExc_TypeError, |
| 1956 |
"expected a sortwrapperobject"); |
| 1957 |
return NULL; |
| 1958 |
} |
| 1959 |
value = ((sortwrapperobject *)so)->value; |
| 1960 |
Py_INCREF(value); |
| 1961 |
return value; |
| 1962 |
} |
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
typedef struct { |
| 1969 |
PyObject_HEAD |
| 1970 |
PyObject *func; |
| 1971 |
} cmpwrapperobject; |
| 1972 |
|
| 1973 |
static void |
| 1974 |
cmpwrapper_dealloc(cmpwrapperobject *co) |
| 1975 |
{ |
| 1976 |
Py_XDECREF(co->func); |
| 1977 |
PyObject_Del(co); |
| 1978 |
} |
| 1979 |
|
| 1980 |
static PyObject * |
| 1981 |
cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds) |
| 1982 |
{ |
| 1983 |
PyObject *x, *y, *xx, *yy; |
| 1984 |
|
| 1985 |
if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y)) |
| 1986 |
return NULL; |
| 1987 |
if (!PyObject_TypeCheck(x, &sortwrapper_type) || |
| 1988 |
!PyObject_TypeCheck(y, &sortwrapper_type)) { |
| 1989 |
PyErr_SetString(PyExc_TypeError, |
| 1990 |
"expected a sortwrapperobject"); |
| 1991 |
return NULL; |
| 1992 |
} |
| 1993 |
xx = ((sortwrapperobject *)x)->key; |
| 1994 |
yy = ((sortwrapperobject *)y)->key; |
| 1995 |
return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL); |
| 1996 |
} |
| 1997 |
|
| 1998 |
PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys."); |
| 1999 |
|
| 2000 |
static PyTypeObject cmpwrapper_type = { |
| 2001 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2002 |
"cmpwrapper", |
| 2003 |
sizeof(cmpwrapperobject), |
| 2004 |
0, |
| 2005 |
|
| 2006 |
(destructor)cmpwrapper_dealloc, |
| 2007 |
0, |
| 2008 |
0, |
| 2009 |
0, |
| 2010 |
0, |
| 2011 |
0, |
| 2012 |
0, |
| 2013 |
0, |
| 2014 |
0, |
| 2015 |
0, |
| 2016 |
(ternaryfunc)cmpwrapper_call, |
| 2017 |
0, |
| 2018 |
PyObject_GenericGetAttr, |
| 2019 |
0, |
| 2020 |
0, |
| 2021 |
Py_TPFLAGS_DEFAULT, |
| 2022 |
cmpwrapper_doc, |
| 2023 |
}; |
| 2024 |
|
| 2025 |
static PyObject * |
| 2026 |
build_cmpwrapper(PyObject *cmpfunc) |
| 2027 |
{ |
| 2028 |
cmpwrapperobject *co; |
| 2029 |
|
| 2030 |
co = PyObject_New(cmpwrapperobject, &cmpwrapper_type); |
| 2031 |
if (co == NULL) |
| 2032 |
return NULL; |
| 2033 |
Py_INCREF(cmpfunc); |
| 2034 |
co->func = cmpfunc; |
| 2035 |
return (PyObject *)co; |
| 2036 |
} |
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
static PyObject * |
| 2044 |
listsort(PyListObject *self, PyObject *args, PyObject *kwds) |
| 2045 |
{ |
| 2046 |
MergeState ms; |
| 2047 |
PyObject **lo, **hi; |
| 2048 |
Py_ssize_t nremaining; |
| 2049 |
Py_ssize_t minrun; |
| 2050 |
Py_ssize_t saved_ob_size, saved_allocated; |
| 2051 |
PyObject **saved_ob_item; |
| 2052 |
PyObject **final_ob_item; |
| 2053 |
PyObject *compare = NULL; |
| 2054 |
PyObject *result = NULL; |
| 2055 |
int reverse = 0; |
| 2056 |
PyObject *keyfunc = NULL; |
| 2057 |
Py_ssize_t i; |
| 2058 |
PyObject *key, *value, *kvpair; |
| 2059 |
static char *kwlist[] = {"cmp", "key", "reverse", 0}; |
| 2060 |
|
| 2061 |
assert(self != NULL); |
| 2062 |
assert (PyList_Check(self)); |
| 2063 |
if (args != NULL) { |
| 2064 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort", |
| 2065 |
kwlist, &compare, &keyfunc, &reverse)) |
| 2066 |
return NULL; |
| 2067 |
} |
| 2068 |
if (compare == Py_None) |
| 2069 |
compare = NULL; |
| 2070 |
if (compare != NULL && |
| 2071 |
PyErr_WarnPy3k("the cmp argument is not supported in 3.x", 1) < 0) |
| 2072 |
return NULL; |
| 2073 |
if (keyfunc == Py_None) |
| 2074 |
keyfunc = NULL; |
| 2075 |
if (compare != NULL && keyfunc != NULL) { |
| 2076 |
compare = build_cmpwrapper(compare); |
| 2077 |
if (compare == NULL) |
| 2078 |
return NULL; |
| 2079 |
} else |
| 2080 |
Py_XINCREF(compare); |
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
|
| 2087 |
saved_ob_size = Py_SIZE(self); |
| 2088 |
saved_ob_item = self->ob_item; |
| 2089 |
saved_allocated = self->allocated; |
| 2090 |
Py_SIZE(self) = 0; |
| 2091 |
self->ob_item = NULL; |
| 2092 |
self->allocated = -1; |
| 2093 |
|
| 2094 |
if (keyfunc != NULL) { |
| 2095 |
for (i=0 ; i < saved_ob_size ; i++) { |
| 2096 |
value = saved_ob_item[i]; |
| 2097 |
key = PyObject_CallFunctionObjArgs(keyfunc, value, |
| 2098 |
NULL); |
| 2099 |
if (key == NULL) { |
| 2100 |
for (i=i-1 ; i>=0 ; i--) { |
| 2101 |
kvpair = saved_ob_item[i]; |
| 2102 |
value = sortwrapper_getvalue(kvpair); |
| 2103 |
saved_ob_item[i] = value; |
| 2104 |
Py_DECREF(kvpair); |
| 2105 |
} |
| 2106 |
goto dsu_fail; |
| 2107 |
} |
| 2108 |
kvpair = build_sortwrapper(key, value); |
| 2109 |
if (kvpair == NULL) |
| 2110 |
goto dsu_fail; |
| 2111 |
saved_ob_item[i] = kvpair; |
| 2112 |
} |
| 2113 |
} |
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
if (reverse && saved_ob_size > 1) |
| 2118 |
reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size); |
| 2119 |
|
| 2120 |
merge_init(&ms, compare); |
| 2121 |
|
| 2122 |
nremaining = saved_ob_size; |
| 2123 |
if (nremaining < 2) |
| 2124 |
goto succeed; |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
lo = saved_ob_item; |
| 2130 |
hi = lo + nremaining; |
| 2131 |
minrun = merge_compute_minrun(nremaining); |
| 2132 |
do { |
| 2133 |
int descending; |
| 2134 |
Py_ssize_t n; |
| 2135 |
|
| 2136 |
|
| 2137 |
n = count_run(lo, hi, compare, &descending); |
| 2138 |
if (n < 0) |
| 2139 |
goto fail; |
| 2140 |
if (descending) |
| 2141 |
reverse_slice(lo, lo + n); |
| 2142 |
|
| 2143 |
if (n < minrun) { |
| 2144 |
const Py_ssize_t force = nremaining <= minrun ? |
| 2145 |
nremaining : minrun; |
| 2146 |
if (binarysort(lo, lo + force, lo + n, compare) < 0) |
| 2147 |
goto fail; |
| 2148 |
n = force; |
| 2149 |
} |
| 2150 |
|
| 2151 |
assert(ms.n < MAX_MERGE_PENDING); |
| 2152 |
ms.pending[ms.n].base = lo; |
| 2153 |
ms.pending[ms.n].len = n; |
| 2154 |
++ms.n; |
| 2155 |
if (merge_collapse(&ms) < 0) |
| 2156 |
goto fail; |
| 2157 |
|
| 2158 |
lo += n; |
| 2159 |
nremaining -= n; |
| 2160 |
} while (nremaining); |
| 2161 |
assert(lo == hi); |
| 2162 |
|
| 2163 |
if (merge_force_collapse(&ms) < 0) |
| 2164 |
goto fail; |
| 2165 |
assert(ms.n == 1); |
| 2166 |
assert(ms.pending[0].base == saved_ob_item); |
| 2167 |
assert(ms.pending[0].len == saved_ob_size); |
| 2168 |
|
| 2169 |
succeed: |
| 2170 |
result = Py_None; |
| 2171 |
fail: |
| 2172 |
if (keyfunc != NULL) { |
| 2173 |
for (i=0 ; i < saved_ob_size ; i++) { |
| 2174 |
kvpair = saved_ob_item[i]; |
| 2175 |
value = sortwrapper_getvalue(kvpair); |
| 2176 |
saved_ob_item[i] = value; |
| 2177 |
Py_DECREF(kvpair); |
| 2178 |
} |
| 2179 |
} |
| 2180 |
|
| 2181 |
if (self->allocated != -1 && result != NULL) { |
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
PyErr_SetString(PyExc_ValueError, "list modified during sort"); |
| 2186 |
result = NULL; |
| 2187 |
} |
| 2188 |
|
| 2189 |
if (reverse && saved_ob_size > 1) |
| 2190 |
reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size); |
| 2191 |
|
| 2192 |
merge_freemem(&ms); |
| 2193 |
|
| 2194 |
dsu_fail: |
| 2195 |
final_ob_item = self->ob_item; |
| 2196 |
i = Py_SIZE(self); |
| 2197 |
Py_SIZE(self) = saved_ob_size; |
| 2198 |
self->ob_item = saved_ob_item; |
| 2199 |
self->allocated = saved_allocated; |
| 2200 |
if (final_ob_item != NULL) { |
| 2201 |
|
| 2202 |
|
| 2203 |
while (--i >= 0) { |
| 2204 |
Py_XDECREF(final_ob_item[i]); |
| 2205 |
} |
| 2206 |
PyMem_FREE(final_ob_item); |
| 2207 |
} |
| 2208 |
Py_XDECREF(compare); |
| 2209 |
Py_XINCREF(result); |
| 2210 |
return result; |
| 2211 |
} |
| 2212 |
#undef IFLT |
| 2213 |
#undef ISLT |
| 2214 |
|
| 2215 |
int |
| 2216 |
PyList_Sort(PyObject *v) |
| 2217 |
{ |
| 2218 |
if (v == NULL || !PyList_Check(v)) { |
| 2219 |
PyErr_BadInternalCall(); |
| 2220 |
return -1; |
| 2221 |
} |
| 2222 |
v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL); |
| 2223 |
if (v == NULL) |
| 2224 |
return -1; |
| 2225 |
Py_DECREF(v); |
| 2226 |
return 0; |
| 2227 |
} |
| 2228 |
|
| 2229 |
static PyObject * |
| 2230 |
listreverse(PyListObject *self) |
| 2231 |
{ |
| 2232 |
if (Py_SIZE(self) > 1) |
| 2233 |
reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self)); |
| 2234 |
Py_RETURN_NONE; |
| 2235 |
} |
| 2236 |
|
| 2237 |
int |
| 2238 |
PyList_Reverse(PyObject *v) |
| 2239 |
{ |
| 2240 |
PyListObject *self = (PyListObject *)v; |
| 2241 |
|
| 2242 |
if (v == NULL || !PyList_Check(v)) { |
| 2243 |
PyErr_BadInternalCall(); |
| 2244 |
return -1; |
| 2245 |
} |
| 2246 |
if (Py_SIZE(self) > 1) |
| 2247 |
reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self)); |
| 2248 |
return 0; |
| 2249 |
} |
| 2250 |
|
| 2251 |
PyObject * |
| 2252 |
PyList_AsTuple(PyObject *v) |
| 2253 |
{ |
| 2254 |
PyObject *w; |
| 2255 |
PyObject **p, **q; |
| 2256 |
Py_ssize_t n; |
| 2257 |
if (v == NULL || !PyList_Check(v)) { |
| 2258 |
PyErr_BadInternalCall(); |
| 2259 |
return NULL; |
| 2260 |
} |
| 2261 |
n = Py_SIZE(v); |
| 2262 |
w = PyTuple_New(n); |
| 2263 |
if (w == NULL) |
| 2264 |
return NULL; |
| 2265 |
p = ((PyTupleObject *)w)->ob_item; |
| 2266 |
q = ((PyListObject *)v)->ob_item; |
| 2267 |
while (--n >= 0) { |
| 2268 |
Py_INCREF(*q); |
| 2269 |
*p = *q; |
| 2270 |
p++; |
| 2271 |
q++; |
| 2272 |
} |
| 2273 |
return w; |
| 2274 |
} |
| 2275 |
|
| 2276 |
static PyObject * |
| 2277 |
listindex(PyListObject *self, PyObject *args) |
| 2278 |
{ |
| 2279 |
Py_ssize_t i, start=0, stop=Py_SIZE(self); |
| 2280 |
PyObject *v, *format_tuple, *err_string; |
| 2281 |
static PyObject *err_format = NULL; |
| 2282 |
|
| 2283 |
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, |
| 2284 |
_PyEval_SliceIndexNotNone, &start, |
| 2285 |
_PyEval_SliceIndexNotNone, &stop)) |
| 2286 |
return NULL; |
| 2287 |
if (start < 0) { |
| 2288 |
start += Py_SIZE(self); |
| 2289 |
if (start < 0) |
| 2290 |
start = 0; |
| 2291 |
} |
| 2292 |
if (stop < 0) { |
| 2293 |
stop += Py_SIZE(self); |
| 2294 |
if (stop < 0) |
| 2295 |
stop = 0; |
| 2296 |
} |
| 2297 |
for (i = start; i < stop && i < Py_SIZE(self); i++) { |
| 2298 |
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 2299 |
if (cmp > 0) |
| 2300 |
return PyInt_FromSsize_t(i); |
| 2301 |
else if (cmp < 0) |
| 2302 |
return NULL; |
| 2303 |
} |
| 2304 |
if (err_format == NULL) { |
| 2305 |
err_format = PyString_FromString("%r is not in list"); |
| 2306 |
if (err_format == NULL) |
| 2307 |
return NULL; |
| 2308 |
} |
| 2309 |
format_tuple = PyTuple_Pack(1, v); |
| 2310 |
if (format_tuple == NULL) |
| 2311 |
return NULL; |
| 2312 |
err_string = PyString_Format(err_format, format_tuple); |
| 2313 |
Py_DECREF(format_tuple); |
| 2314 |
if (err_string == NULL) |
| 2315 |
return NULL; |
| 2316 |
PyErr_SetObject(PyExc_ValueError, err_string); |
| 2317 |
Py_DECREF(err_string); |
| 2318 |
return NULL; |
| 2319 |
} |
| 2320 |
|
| 2321 |
static PyObject * |
| 2322 |
listcount(PyListObject *self, PyObject *v) |
| 2323 |
{ |
| 2324 |
Py_ssize_t count = 0; |
| 2325 |
Py_ssize_t i; |
| 2326 |
|
| 2327 |
for (i = 0; i < Py_SIZE(self); i++) { |
| 2328 |
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 2329 |
if (cmp > 0) |
| 2330 |
count++; |
| 2331 |
else if (cmp < 0) |
| 2332 |
return NULL; |
| 2333 |
} |
| 2334 |
return PyInt_FromSsize_t(count); |
| 2335 |
} |
| 2336 |
|
| 2337 |
static PyObject * |
| 2338 |
listremove(PyListObject *self, PyObject *v) |
| 2339 |
{ |
| 2340 |
Py_ssize_t i; |
| 2341 |
|
| 2342 |
for (i = 0; i < Py_SIZE(self); i++) { |
| 2343 |
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 2344 |
if (cmp > 0) { |
| 2345 |
if (list_ass_slice(self, i, i+1, |
| 2346 |
(PyObject *)NULL) == 0) |
| 2347 |
Py_RETURN_NONE; |
| 2348 |
return NULL; |
| 2349 |
} |
| 2350 |
else if (cmp < 0) |
| 2351 |
return NULL; |
| 2352 |
} |
| 2353 |
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); |
| 2354 |
return NULL; |
| 2355 |
} |
| 2356 |
|
| 2357 |
static int |
| 2358 |
list_traverse(PyListObject *o, visitproc visit, void *arg) |
| 2359 |
{ |
| 2360 |
Py_ssize_t i; |
| 2361 |
|
| 2362 |
for (i = Py_SIZE(o); --i >= 0; ) |
| 2363 |
Py_VISIT(o->ob_item[i]); |
| 2364 |
return 0; |
| 2365 |
} |
| 2366 |
|
| 2367 |
static PyObject * |
| 2368 |
list_richcompare(PyObject *v, PyObject *w, int op) |
| 2369 |
{ |
| 2370 |
PyListObject *vl, *wl; |
| 2371 |
Py_ssize_t i; |
| 2372 |
|
| 2373 |
if (!PyList_Check(v) || !PyList_Check(w)) { |
| 2374 |
Py_INCREF(Py_NotImplemented); |
| 2375 |
return Py_NotImplemented; |
| 2376 |
} |
| 2377 |
|
| 2378 |
vl = (PyListObject *)v; |
| 2379 |
wl = (PyListObject *)w; |
| 2380 |
|
| 2381 |
if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) { |
| 2382 |
|
| 2383 |
PyObject *res; |
| 2384 |
if (op == Py_EQ) |
| 2385 |
res = Py_False; |
| 2386 |
else |
| 2387 |
res = Py_True; |
| 2388 |
Py_INCREF(res); |
| 2389 |
return res; |
| 2390 |
} |
| 2391 |
|
| 2392 |
|
| 2393 |
for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) { |
| 2394 |
int k = PyObject_RichCompareBool(vl->ob_item[i], |
| 2395 |
wl->ob_item[i], Py_EQ); |
| 2396 |
if (k < 0) |
| 2397 |
return NULL; |
| 2398 |
if (!k) |
| 2399 |
break; |
| 2400 |
} |
| 2401 |
|
| 2402 |
if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) { |
| 2403 |
|
| 2404 |
Py_ssize_t vs = Py_SIZE(vl); |
| 2405 |
Py_ssize_t ws = Py_SIZE(wl); |
| 2406 |
int cmp; |
| 2407 |
PyObject *res; |
| 2408 |
switch (op) { |
| 2409 |
case Py_LT: cmp = vs < ws; break; |
| 2410 |
case Py_LE: cmp = vs <= ws; break; |
| 2411 |
case Py_EQ: cmp = vs == ws; break; |
| 2412 |
case Py_NE: cmp = vs != ws; break; |
| 2413 |
case Py_GT: cmp = vs > ws; break; |
| 2414 |
case Py_GE: cmp = vs >= ws; break; |
| 2415 |
default: return NULL; |
| 2416 |
} |
| 2417 |
if (cmp) |
| 2418 |
res = Py_True; |
| 2419 |
else |
| 2420 |
res = Py_False; |
| 2421 |
Py_INCREF(res); |
| 2422 |
return res; |
| 2423 |
} |
| 2424 |
|
| 2425 |
|
| 2426 |
if (op == Py_EQ) { |
| 2427 |
Py_INCREF(Py_False); |
| 2428 |
return Py_False; |
| 2429 |
} |
| 2430 |
if (op == Py_NE) { |
| 2431 |
Py_INCREF(Py_True); |
| 2432 |
return Py_True; |
| 2433 |
} |
| 2434 |
|
| 2435 |
|
| 2436 |
return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op); |
| 2437 |
} |
| 2438 |
|
| 2439 |
static int |
| 2440 |
list_init(PyListObject *self, PyObject *args, PyObject *kw) |
| 2441 |
{ |
| 2442 |
PyObject *arg = NULL; |
| 2443 |
static char *kwlist[] = {"sequence", 0}; |
| 2444 |
|
| 2445 |
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg)) |
| 2446 |
return -1; |
| 2447 |
|
| 2448 |
|
| 2449 |
assert(0 <= Py_SIZE(self)); |
| 2450 |
assert(Py_SIZE(self) <= self->allocated || self->allocated == -1); |
| 2451 |
assert(self->ob_item != NULL || |
| 2452 |
self->allocated == 0 || self->allocated == -1); |
| 2453 |
|
| 2454 |
|
| 2455 |
if (self->ob_item != NULL) { |
| 2456 |
(void)list_clear(self); |
| 2457 |
} |
| 2458 |
if (arg != NULL) { |
| 2459 |
PyObject *rv = listextend(self, arg); |
| 2460 |
if (rv == NULL) |
| 2461 |
return -1; |
| 2462 |
Py_DECREF(rv); |
| 2463 |
} |
| 2464 |
return 0; |
| 2465 |
} |
| 2466 |
|
| 2467 |
static PyObject * |
| 2468 |
list_sizeof(PyListObject *self) |
| 2469 |
{ |
| 2470 |
Py_ssize_t res; |
| 2471 |
|
| 2472 |
res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*); |
| 2473 |
return PyInt_FromSsize_t(res); |
| 2474 |
} |
| 2475 |
|
| 2476 |
static PyObject *list_iter(PyObject *seq); |
| 2477 |
static PyObject *list_reversed(PyListObject* seq, PyObject* unused); |
| 2478 |
|
| 2479 |
PyDoc_STRVAR(getitem_doc, |
| 2480 |
"x.__getitem__(y) <==> x[y]"); |
| 2481 |
PyDoc_STRVAR(reversed_doc, |
| 2482 |
"L.__reversed__() -- return a reverse iterator over the list"); |
| 2483 |
PyDoc_STRVAR(sizeof_doc, |
| 2484 |
"L.__sizeof__() -- size of L in memory, in bytes"); |
| 2485 |
PyDoc_STRVAR(append_doc, |
| 2486 |
"L.append(object) -- append object to end"); |
| 2487 |
PyDoc_STRVAR(extend_doc, |
| 2488 |
"L.extend(iterable) -- extend list by appending elements from the iterable"); |
| 2489 |
PyDoc_STRVAR(insert_doc, |
| 2490 |
"L.insert(index, object) -- insert object before index"); |
| 2491 |
PyDoc_STRVAR(pop_doc, |
| 2492 |
"L.pop([index]) -> item -- remove and return item at index (default last).\n" |
| 2493 |
"Raises IndexError if list is empty or index is out of range."); |
| 2494 |
PyDoc_STRVAR(remove_doc, |
| 2495 |
"L.remove(value) -- remove first occurrence of value.\n" |
| 2496 |
"Raises ValueError if the value is not present."); |
| 2497 |
PyDoc_STRVAR(index_doc, |
| 2498 |
"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n" |
| 2499 |
"Raises ValueError if the value is not present."); |
| 2500 |
PyDoc_STRVAR(count_doc, |
| 2501 |
"L.count(value) -> integer -- return number of occurrences of value"); |
| 2502 |
PyDoc_STRVAR(reverse_doc, |
| 2503 |
"L.reverse() -- reverse *IN PLACE*"); |
| 2504 |
PyDoc_STRVAR(sort_doc, |
| 2505 |
"L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\ |
| 2506 |
cmp(x, y) -> -1, 0, 1"); |
| 2507 |
|
| 2508 |
static PyObject *list_subscript(PyListObject*, PyObject*); |
| 2509 |
|
| 2510 |
static PyMethodDef list_methods[] = { |
| 2511 |
{"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc}, |
| 2512 |
{"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc}, |
| 2513 |
{"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc}, |
| 2514 |
{"append", (PyCFunction)listappend, METH_O, append_doc}, |
| 2515 |
{"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, |
| 2516 |
{"extend", (PyCFunction)listextend, METH_O, extend_doc}, |
| 2517 |
{"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc}, |
| 2518 |
{"remove", (PyCFunction)listremove, METH_O, remove_doc}, |
| 2519 |
{"index", (PyCFunction)listindex, METH_VARARGS, index_doc}, |
| 2520 |
{"count", (PyCFunction)listcount, METH_O, count_doc}, |
| 2521 |
{"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc}, |
| 2522 |
{"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc}, |
| 2523 |
{NULL, NULL} |
| 2524 |
}; |
| 2525 |
|
| 2526 |
static PySequenceMethods list_as_sequence = { |
| 2527 |
(lenfunc)list_length, |
| 2528 |
(binaryfunc)list_concat, |
| 2529 |
(ssizeargfunc)list_repeat, |
| 2530 |
(ssizeargfunc)list_item, |
| 2531 |
(ssizessizeargfunc)list_slice, |
| 2532 |
(ssizeobjargproc)list_ass_item, |
| 2533 |
(ssizessizeobjargproc)list_ass_slice, |
| 2534 |
(objobjproc)list_contains, |
| 2535 |
(binaryfunc)list_inplace_concat, |
| 2536 |
(ssizeargfunc)list_inplace_repeat, |
| 2537 |
}; |
| 2538 |
|
| 2539 |
PyDoc_STRVAR(list_doc, |
| 2540 |
"list() -> new empty list\n" |
| 2541 |
"list(iterable) -> new list initialized from iterable's items"); |
| 2542 |
|
| 2543 |
|
| 2544 |
static PyObject * |
| 2545 |
list_subscript(PyListObject* self, PyObject* item) |
| 2546 |
{ |
| 2547 |
if (PyIndex_Check(item)) { |
| 2548 |
Py_ssize_t i; |
| 2549 |
i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 2550 |
if (i == -1 && PyErr_Occurred()) |
| 2551 |
return NULL; |
| 2552 |
if (i < 0) |
| 2553 |
i += PyList_GET_SIZE(self); |
| 2554 |
return list_item(self, i); |
| 2555 |
} |
| 2556 |
else if (PySlice_Check(item)) { |
| 2557 |
Py_ssize_t start, stop, step, slicelength, cur, i; |
| 2558 |
PyObject* result; |
| 2559 |
PyObject* it; |
| 2560 |
PyObject **src, **dest; |
| 2561 |
|
| 2562 |
if (_PySlice_Unpack(item, &start, &stop, &step) < 0) { |
| 2563 |
return NULL; |
| 2564 |
} |
| 2565 |
slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, |
| 2566 |
step); |
| 2567 |
|
| 2568 |
if (slicelength <= 0) { |
| 2569 |
return PyList_New(0); |
| 2570 |
} |
| 2571 |
else if (step == 1) { |
| 2572 |
return list_slice(self, start, stop); |
| 2573 |
} |
| 2574 |
else { |
| 2575 |
result = PyList_New(slicelength); |
| 2576 |
if (!result) return NULL; |
| 2577 |
|
| 2578 |
src = self->ob_item; |
| 2579 |
dest = ((PyListObject *)result)->ob_item; |
| 2580 |
for (cur = start, i = 0; i < slicelength; |
| 2581 |
cur += step, i++) { |
| 2582 |
it = src[cur]; |
| 2583 |
Py_INCREF(it); |
| 2584 |
dest[i] = it; |
| 2585 |
} |
| 2586 |
|
| 2587 |
return result; |
| 2588 |
} |
| 2589 |
} |
| 2590 |
else { |
| 2591 |
PyErr_Format(PyExc_TypeError, |
| 2592 |
"list indices must be integers, not %.200s", |
| 2593 |
item->ob_type->tp_name); |
| 2594 |
return NULL; |
| 2595 |
} |
| 2596 |
} |
| 2597 |
|
| 2598 |
static int |
| 2599 |
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) |
| 2600 |
{ |
| 2601 |
if (PyIndex_Check(item)) { |
| 2602 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 2603 |
if (i == -1 && PyErr_Occurred()) |
| 2604 |
return -1; |
| 2605 |
if (i < 0) |
| 2606 |
i += PyList_GET_SIZE(self); |
| 2607 |
return list_ass_item(self, i, value); |
| 2608 |
} |
| 2609 |
else if (PySlice_Check(item)) { |
| 2610 |
Py_ssize_t start, stop, step, slicelength; |
| 2611 |
|
| 2612 |
if (_PySlice_Unpack(item, &start, &stop, &step) < 0) { |
| 2613 |
return -1; |
| 2614 |
} |
| 2615 |
slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, |
| 2616 |
step); |
| 2617 |
|
| 2618 |
if (step == 1) |
| 2619 |
return list_ass_slice(self, start, stop, value); |
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
if ((step < 0 && start < stop) || |
| 2624 |
(step > 0 && start > stop)) |
| 2625 |
stop = start; |
| 2626 |
|
| 2627 |
if (value == NULL) { |
| 2628 |
|
| 2629 |
PyObject **garbage; |
| 2630 |
size_t cur; |
| 2631 |
Py_ssize_t i; |
| 2632 |
|
| 2633 |
if (slicelength <= 0) |
| 2634 |
return 0; |
| 2635 |
|
| 2636 |
if (step < 0) { |
| 2637 |
stop = start + 1; |
| 2638 |
start = stop + step*(slicelength - 1) - 1; |
| 2639 |
step = -step; |
| 2640 |
} |
| 2641 |
|
| 2642 |
assert((size_t)slicelength <= |
| 2643 |
PY_SIZE_MAX / sizeof(PyObject*)); |
| 2644 |
|
| 2645 |
garbage = (PyObject**) |
| 2646 |
PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
| 2647 |
if (!garbage) { |
| 2648 |
PyErr_NoMemory(); |
| 2649 |
return -1; |
| 2650 |
} |
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
for (cur = start, i = 0; |
| 2659 |
cur < (size_t)stop; |
| 2660 |
cur += step, i++) { |
| 2661 |
Py_ssize_t lim = step - 1; |
| 2662 |
|
| 2663 |
garbage[i] = PyList_GET_ITEM(self, cur); |
| 2664 |
|
| 2665 |
if (cur + step >= (size_t)Py_SIZE(self)) { |
| 2666 |
lim = Py_SIZE(self) - cur - 1; |
| 2667 |
} |
| 2668 |
|
| 2669 |
memmove(self->ob_item + cur - i, |
| 2670 |
self->ob_item + cur + 1, |
| 2671 |
lim * sizeof(PyObject *)); |
| 2672 |
} |
| 2673 |
cur = start + slicelength*step; |
| 2674 |
if (cur < (size_t)Py_SIZE(self)) { |
| 2675 |
memmove(self->ob_item + cur - slicelength, |
| 2676 |
self->ob_item + cur, |
| 2677 |
(Py_SIZE(self) - cur) * |
| 2678 |
sizeof(PyObject *)); |
| 2679 |
} |
| 2680 |
|
| 2681 |
Py_SIZE(self) -= slicelength; |
| 2682 |
list_resize(self, Py_SIZE(self)); |
| 2683 |
|
| 2684 |
for (i = 0; i < slicelength; i++) { |
| 2685 |
Py_DECREF(garbage[i]); |
| 2686 |
} |
| 2687 |
PyMem_FREE(garbage); |
| 2688 |
|
| 2689 |
return 0; |
| 2690 |
} |
| 2691 |
else { |
| 2692 |
|
| 2693 |
PyObject *ins, *seq; |
| 2694 |
PyObject **garbage, **seqitems, **selfitems; |
| 2695 |
Py_ssize_t cur, i; |
| 2696 |
|
| 2697 |
|
| 2698 |
if (self == (PyListObject*)value) { |
| 2699 |
seq = list_slice((PyListObject*)value, 0, |
| 2700 |
PyList_GET_SIZE(value)); |
| 2701 |
} |
| 2702 |
else { |
| 2703 |
seq = PySequence_Fast(value, |
| 2704 |
"must assign iterable " |
| 2705 |
"to extended slice"); |
| 2706 |
} |
| 2707 |
if (!seq) |
| 2708 |
return -1; |
| 2709 |
|
| 2710 |
if (PySequence_Fast_GET_SIZE(seq) != slicelength) { |
| 2711 |
PyErr_Format(PyExc_ValueError, |
| 2712 |
"attempt to assign sequence of " |
| 2713 |
"size %zd to extended slice of " |
| 2714 |
"size %zd", |
| 2715 |
PySequence_Fast_GET_SIZE(seq), |
| 2716 |
slicelength); |
| 2717 |
Py_DECREF(seq); |
| 2718 |
return -1; |
| 2719 |
} |
| 2720 |
|
| 2721 |
if (!slicelength) { |
| 2722 |
Py_DECREF(seq); |
| 2723 |
return 0; |
| 2724 |
} |
| 2725 |
|
| 2726 |
garbage = (PyObject**) |
| 2727 |
PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
| 2728 |
if (!garbage) { |
| 2729 |
Py_DECREF(seq); |
| 2730 |
PyErr_NoMemory(); |
| 2731 |
return -1; |
| 2732 |
} |
| 2733 |
|
| 2734 |
selfitems = self->ob_item; |
| 2735 |
seqitems = PySequence_Fast_ITEMS(seq); |
| 2736 |
for (cur = start, i = 0; i < slicelength; |
| 2737 |
cur += step, i++) { |
| 2738 |
garbage[i] = selfitems[cur]; |
| 2739 |
ins = seqitems[i]; |
| 2740 |
Py_INCREF(ins); |
| 2741 |
selfitems[cur] = ins; |
| 2742 |
} |
| 2743 |
|
| 2744 |
for (i = 0; i < slicelength; i++) { |
| 2745 |
Py_DECREF(garbage[i]); |
| 2746 |
} |
| 2747 |
|
| 2748 |
PyMem_FREE(garbage); |
| 2749 |
Py_DECREF(seq); |
| 2750 |
|
| 2751 |
return 0; |
| 2752 |
} |
| 2753 |
} |
| 2754 |
else { |
| 2755 |
PyErr_Format(PyExc_TypeError, |
| 2756 |
"list indices must be integers, not %.200s", |
| 2757 |
item->ob_type->tp_name); |
| 2758 |
return -1; |
| 2759 |
} |
| 2760 |
} |
| 2761 |
|
| 2762 |
static PyMappingMethods list_as_mapping = { |
| 2763 |
(lenfunc)list_length, |
| 2764 |
(binaryfunc)list_subscript, |
| 2765 |
(objobjargproc)list_ass_subscript |
| 2766 |
}; |
| 2767 |
|
| 2768 |
PyTypeObject PyList_Type = { |
| 2769 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2770 |
"list", |
| 2771 |
sizeof(PyListObject), |
| 2772 |
0, |
| 2773 |
(destructor)list_dealloc, |
| 2774 |
(printfunc)list_print, |
| 2775 |
0, |
| 2776 |
0, |
| 2777 |
0, |
| 2778 |
(reprfunc)list_repr, |
| 2779 |
0, |
| 2780 |
&list_as_sequence, |
| 2781 |
&list_as_mapping, |
| 2782 |
(hashfunc)PyObject_HashNotImplemented, |
| 2783 |
0, |
| 2784 |
0, |
| 2785 |
PyObject_GenericGetAttr, |
| 2786 |
0, |
| 2787 |
0, |
| 2788 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2789 |
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, |
| 2790 |
list_doc, |
| 2791 |
(traverseproc)list_traverse, |
| 2792 |
(inquiry)list_clear, |
| 2793 |
list_richcompare, |
| 2794 |
0, |
| 2795 |
list_iter, |
| 2796 |
0, |
| 2797 |
list_methods, |
| 2798 |
0, |
| 2799 |
0, |
| 2800 |
0, |
| 2801 |
0, |
| 2802 |
0, |
| 2803 |
0, |
| 2804 |
0, |
| 2805 |
(initproc)list_init, |
| 2806 |
PyType_GenericAlloc, |
| 2807 |
PyType_GenericNew, |
| 2808 |
PyObject_GC_Del, |
| 2809 |
}; |
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
typedef struct { |
| 2815 |
PyObject_HEAD |
| 2816 |
long it_index; |
| 2817 |
PyListObject *it_seq; |
| 2818 |
} listiterobject; |
| 2819 |
|
| 2820 |
static PyObject *list_iter(PyObject *); |
| 2821 |
static void listiter_dealloc(listiterobject *); |
| 2822 |
static int listiter_traverse(listiterobject *, visitproc, void *); |
| 2823 |
static PyObject *listiter_next(listiterobject *); |
| 2824 |
static PyObject *listiter_len(listiterobject *); |
| 2825 |
|
| 2826 |
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 2827 |
|
| 2828 |
static PyMethodDef listiter_methods[] = { |
| 2829 |
{"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc}, |
| 2830 |
{NULL, NULL} |
| 2831 |
}; |
| 2832 |
|
| 2833 |
PyTypeObject PyListIter_Type = { |
| 2834 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2835 |
"listiterator", |
| 2836 |
sizeof(listiterobject), |
| 2837 |
0, |
| 2838 |
|
| 2839 |
(destructor)listiter_dealloc, |
| 2840 |
0, |
| 2841 |
0, |
| 2842 |
0, |
| 2843 |
0, |
| 2844 |
0, |
| 2845 |
0, |
| 2846 |
0, |
| 2847 |
0, |
| 2848 |
0, |
| 2849 |
0, |
| 2850 |
0, |
| 2851 |
PyObject_GenericGetAttr, |
| 2852 |
0, |
| 2853 |
0, |
| 2854 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 2855 |
0, |
| 2856 |
(traverseproc)listiter_traverse, |
| 2857 |
0, |
| 2858 |
0, |
| 2859 |
0, |
| 2860 |
PyObject_SelfIter, |
| 2861 |
(iternextfunc)listiter_next, |
| 2862 |
listiter_methods, |
| 2863 |
0, |
| 2864 |
}; |
| 2865 |
|
| 2866 |
|
| 2867 |
static PyObject * |
| 2868 |
list_iter(PyObject *seq) |
| 2869 |
{ |
| 2870 |
listiterobject *it; |
| 2871 |
|
| 2872 |
if (!PyList_Check(seq)) { |
| 2873 |
PyErr_BadInternalCall(); |
| 2874 |
return NULL; |
| 2875 |
} |
| 2876 |
it = PyObject_GC_New(listiterobject, &PyListIter_Type); |
| 2877 |
if (it == NULL) |
| 2878 |
return NULL; |
| 2879 |
it->it_index = 0; |
| 2880 |
Py_INCREF(seq); |
| 2881 |
it->it_seq = (PyListObject *)seq; |
| 2882 |
_PyObject_GC_TRACK(it); |
| 2883 |
return (PyObject *)it; |
| 2884 |
} |
| 2885 |
|
| 2886 |
static void |
| 2887 |
listiter_dealloc(listiterobject *it) |
| 2888 |
{ |
| 2889 |
_PyObject_GC_UNTRACK(it); |
| 2890 |
Py_XDECREF(it->it_seq); |
| 2891 |
PyObject_GC_Del(it); |
| 2892 |
} |
| 2893 |
|
| 2894 |
static int |
| 2895 |
listiter_traverse(listiterobject *it, visitproc visit, void *arg) |
| 2896 |
{ |
| 2897 |
Py_VISIT(it->it_seq); |
| 2898 |
return 0; |
| 2899 |
} |
| 2900 |
|
| 2901 |
static PyObject * |
| 2902 |
listiter_next(listiterobject *it) |
| 2903 |
{ |
| 2904 |
PyListObject *seq; |
| 2905 |
PyObject *item; |
| 2906 |
|
| 2907 |
assert(it != NULL); |
| 2908 |
seq = it->it_seq; |
| 2909 |
if (seq == NULL) |
| 2910 |
return NULL; |
| 2911 |
assert(PyList_Check(seq)); |
| 2912 |
|
| 2913 |
if (it->it_index < PyList_GET_SIZE(seq)) { |
| 2914 |
item = PyList_GET_ITEM(seq, it->it_index); |
| 2915 |
++it->it_index; |
| 2916 |
Py_INCREF(item); |
| 2917 |
return item; |
| 2918 |
} |
| 2919 |
|
| 2920 |
it->it_seq = NULL; |
| 2921 |
Py_DECREF(seq); |
| 2922 |
return NULL; |
| 2923 |
} |
| 2924 |
|
| 2925 |
static PyObject * |
| 2926 |
listiter_len(listiterobject *it) |
| 2927 |
{ |
| 2928 |
Py_ssize_t len; |
| 2929 |
if (it->it_seq) { |
| 2930 |
len = PyList_GET_SIZE(it->it_seq) - it->it_index; |
| 2931 |
if (len >= 0) |
| 2932 |
return PyInt_FromSsize_t(len); |
| 2933 |
} |
| 2934 |
return PyInt_FromLong(0); |
| 2935 |
} |
| 2936 |
|
| 2937 |
|
| 2938 |
typedef struct { |
| 2939 |
PyObject_HEAD |
| 2940 |
Py_ssize_t it_index; |
| 2941 |
PyListObject *it_seq; |
| 2942 |
} listreviterobject; |
| 2943 |
|
| 2944 |
static PyObject *list_reversed(PyListObject *, PyObject *); |
| 2945 |
static void listreviter_dealloc(listreviterobject *); |
| 2946 |
static int listreviter_traverse(listreviterobject *, visitproc, void *); |
| 2947 |
static PyObject *listreviter_next(listreviterobject *); |
| 2948 |
static PyObject *listreviter_len(listreviterobject *); |
| 2949 |
|
| 2950 |
static PyMethodDef listreviter_methods[] = { |
| 2951 |
{"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc}, |
| 2952 |
{NULL, NULL} |
| 2953 |
}; |
| 2954 |
|
| 2955 |
PyTypeObject PyListRevIter_Type = { |
| 2956 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2957 |
"listreverseiterator", |
| 2958 |
sizeof(listreviterobject), |
| 2959 |
0, |
| 2960 |
|
| 2961 |
(destructor)listreviter_dealloc, |
| 2962 |
0, |
| 2963 |
0, |
| 2964 |
0, |
| 2965 |
0, |
| 2966 |
0, |
| 2967 |
0, |
| 2968 |
0, |
| 2969 |
0, |
| 2970 |
0, |
| 2971 |
0, |
| 2972 |
0, |
| 2973 |
PyObject_GenericGetAttr, |
| 2974 |
0, |
| 2975 |
0, |
| 2976 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 2977 |
0, |
| 2978 |
(traverseproc)listreviter_traverse, |
| 2979 |
0, |
| 2980 |
0, |
| 2981 |
0, |
| 2982 |
PyObject_SelfIter, |
| 2983 |
(iternextfunc)listreviter_next, |
| 2984 |
listreviter_methods, |
| 2985 |
0, |
| 2986 |
}; |
| 2987 |
|
| 2988 |
static PyObject * |
| 2989 |
list_reversed(PyListObject *seq, PyObject *unused) |
| 2990 |
{ |
| 2991 |
listreviterobject *it; |
| 2992 |
|
| 2993 |
it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type); |
| 2994 |
if (it == NULL) |
| 2995 |
return NULL; |
| 2996 |
assert(PyList_Check(seq)); |
| 2997 |
it->it_index = PyList_GET_SIZE(seq) - 1; |
| 2998 |
Py_INCREF(seq); |
| 2999 |
it->it_seq = seq; |
| 3000 |
PyObject_GC_Track(it); |
| 3001 |
return (PyObject *)it; |
| 3002 |
} |
| 3003 |
|
| 3004 |
static void |
| 3005 |
listreviter_dealloc(listreviterobject *it) |
| 3006 |
{ |
| 3007 |
PyObject_GC_UnTrack(it); |
| 3008 |
Py_XDECREF(it->it_seq); |
| 3009 |
PyObject_GC_Del(it); |
| 3010 |
} |
| 3011 |
|
| 3012 |
static int |
| 3013 |
listreviter_traverse(listreviterobject *it, visitproc visit, void *arg) |
| 3014 |
{ |
| 3015 |
Py_VISIT(it->it_seq); |
| 3016 |
return 0; |
| 3017 |
} |
| 3018 |
|
| 3019 |
static PyObject * |
| 3020 |
listreviter_next(listreviterobject *it) |
| 3021 |
{ |
| 3022 |
PyObject *item; |
| 3023 |
Py_ssize_t index; |
| 3024 |
PyListObject *seq; |
| 3025 |
|
| 3026 |
assert(it != NULL); |
| 3027 |
seq = it->it_seq; |
| 3028 |
if (seq == NULL) { |
| 3029 |
return NULL; |
| 3030 |
} |
| 3031 |
assert(PyList_Check(seq)); |
| 3032 |
|
| 3033 |
index = it->it_index; |
| 3034 |
if (index>=0 && index < PyList_GET_SIZE(seq)) { |
| 3035 |
item = PyList_GET_ITEM(seq, index); |
| 3036 |
it->it_index--; |
| 3037 |
Py_INCREF(item); |
| 3038 |
return item; |
| 3039 |
} |
| 3040 |
it->it_index = -1; |
| 3041 |
it->it_seq = NULL; |
| 3042 |
Py_DECREF(seq); |
| 3043 |
return NULL; |
| 3044 |
} |
| 3045 |
|
| 3046 |
static PyObject * |
| 3047 |
listreviter_len(listreviterobject *it) |
| 3048 |
{ |
| 3049 |
Py_ssize_t len = it->it_index + 1; |
| 3050 |
if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len) |
| 3051 |
len = 0; |
| 3052 |
return PyLong_FromSsize_t(len); |
| 3053 |
} |