[2] | 1 | #include "Python.h"
|
---|
| 2 | #include "structmember.h"
|
---|
| 3 |
|
---|
| 4 | /* collections module implementation of a deque() datatype
|
---|
| 5 | Written and maintained by Raymond D. Hettinger <python@rcn.com>
|
---|
| 6 | Copyright (c) 2004 Python Software Foundation.
|
---|
| 7 | All rights reserved.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /* The block length may be set to any number over 1. Larger numbers
|
---|
[391] | 11 | * reduce the number of calls to the memory allocator, give faster
|
---|
| 12 | * indexing and rotation, and reduce the link::data overhead ratio.
|
---|
| 13 | *
|
---|
| 14 | * Ideally, the block length will be set to two less than some
|
---|
| 15 | * multiple of the cache-line length (so that the full block
|
---|
| 16 | * including the leftlink and rightlink will fit neatly into
|
---|
| 17 | * cache lines).
|
---|
[2] | 18 | */
|
---|
| 19 |
|
---|
| 20 | #define BLOCKLEN 62
|
---|
| 21 | #define CENTER ((BLOCKLEN - 1) / 2)
|
---|
| 22 |
|
---|
| 23 | /* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
|
---|
| 24 | * This list is not circular (the leftmost block has leftlink==NULL,
|
---|
| 25 | * and the rightmost block has rightlink==NULL). A deque d's first
|
---|
| 26 | * element is at d.leftblock[leftindex] and its last element is at
|
---|
| 27 | * d.rightblock[rightindex]; note that, unlike as for Python slice
|
---|
| 28 | * indices, these indices are inclusive on both ends. By being inclusive
|
---|
| 29 | * on both ends, algorithms for left and right operations become
|
---|
| 30 | * symmetrical which simplifies the design.
|
---|
| 31 | *
|
---|
| 32 | * The list of blocks is never empty, so d.leftblock and d.rightblock
|
---|
| 33 | * are never equal to NULL.
|
---|
| 34 | *
|
---|
| 35 | * The indices, d.leftindex and d.rightindex are always in the range
|
---|
| 36 | * 0 <= index < BLOCKLEN.
|
---|
| 37 | * Their exact relationship is:
|
---|
| 38 | * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
|
---|
| 39 | *
|
---|
| 40 | * Empty deques have d.len == 0; d.leftblock==d.rightblock;
|
---|
| 41 | * d.leftindex == CENTER+1; and d.rightindex == CENTER.
|
---|
| 42 | * Checking for d.len == 0 is the intended way to see whether d is empty.
|
---|
| 43 | *
|
---|
| 44 | * Whenever d.leftblock == d.rightblock,
|
---|
| 45 | * d.leftindex + d.len - 1 == d.rightindex.
|
---|
| 46 | *
|
---|
| 47 | * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
|
---|
| 48 | * become indices into distinct blocks and either may be larger than the
|
---|
| 49 | * other.
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 52 | typedef struct BLOCK {
|
---|
[391] | 53 | PyObject *data[BLOCKLEN];
|
---|
| 54 | struct BLOCK *rightlink;
|
---|
| 55 | struct BLOCK *leftlink;
|
---|
[2] | 56 | } block;
|
---|
| 57 |
|
---|
| 58 | #define MAXFREEBLOCKS 10
|
---|
| 59 | static Py_ssize_t numfreeblocks = 0;
|
---|
| 60 | static block *freeblocks[MAXFREEBLOCKS];
|
---|
| 61 |
|
---|
| 62 | static block *
|
---|
| 63 | newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
|
---|
[391] | 64 | block *b;
|
---|
| 65 | /* To prevent len from overflowing PY_SSIZE_T_MAX on 32-bit machines, we
|
---|
| 66 | * refuse to allocate new blocks if the current len is nearing overflow. */
|
---|
| 67 | if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
|
---|
| 68 | PyErr_SetString(PyExc_OverflowError,
|
---|
| 69 | "cannot add more blocks to the deque");
|
---|
| 70 | return NULL;
|
---|
| 71 | }
|
---|
| 72 | if (numfreeblocks) {
|
---|
| 73 | numfreeblocks -= 1;
|
---|
| 74 | b = freeblocks[numfreeblocks];
|
---|
| 75 | } else {
|
---|
| 76 | b = PyMem_Malloc(sizeof(block));
|
---|
| 77 | if (b == NULL) {
|
---|
| 78 | PyErr_NoMemory();
|
---|
| 79 | return NULL;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | b->leftlink = leftlink;
|
---|
| 83 | b->rightlink = rightlink;
|
---|
| 84 | return b;
|
---|
[2] | 85 | }
|
---|
| 86 |
|
---|
| 87 | static void
|
---|
| 88 | freeblock(block *b)
|
---|
| 89 | {
|
---|
[391] | 90 | if (numfreeblocks < MAXFREEBLOCKS) {
|
---|
| 91 | freeblocks[numfreeblocks] = b;
|
---|
| 92 | numfreeblocks++;
|
---|
| 93 | } else {
|
---|
| 94 | PyMem_Free(b);
|
---|
| 95 | }
|
---|
[2] | 96 | }
|
---|
| 97 |
|
---|
| 98 | typedef struct {
|
---|
[391] | 99 | PyObject_HEAD
|
---|
| 100 | block *leftblock;
|
---|
| 101 | block *rightblock;
|
---|
| 102 | Py_ssize_t leftindex; /* in range(BLOCKLEN) */
|
---|
| 103 | Py_ssize_t rightindex; /* in range(BLOCKLEN) */
|
---|
| 104 | Py_ssize_t len;
|
---|
| 105 | long state; /* incremented whenever the indices move */
|
---|
| 106 | Py_ssize_t maxlen;
|
---|
| 107 | PyObject *weakreflist; /* List of weak references */
|
---|
[2] | 108 | } dequeobject;
|
---|
| 109 |
|
---|
| 110 | /* The deque's size limit is d.maxlen. The limit can be zero or positive.
|
---|
| 111 | * If there is no limit, then d.maxlen == -1.
|
---|
[391] | 112 | *
|
---|
[2] | 113 | * After an item is added to a deque, we check to see if the size has grown past
|
---|
| 114 | * the limit. If it has, we get the size back down to the limit by popping an
|
---|
| 115 | * item off of the opposite end. The methods that can trigger this are append(),
|
---|
| 116 | * appendleft(), extend(), and extendleft().
|
---|
| 117 | */
|
---|
| 118 |
|
---|
[391] | 119 | #define TRIM(d, popfunction) \
|
---|
| 120 | if (d->maxlen != -1 && d->len > d->maxlen) { \
|
---|
| 121 | PyObject *rv = popfunction(d, NULL); \
|
---|
| 122 | assert(rv != NULL && d->len <= d->maxlen); \
|
---|
| 123 | Py_DECREF(rv); \
|
---|
[2] | 124 | }
|
---|
| 125 |
|
---|
| 126 | static PyTypeObject deque_type;
|
---|
| 127 |
|
---|
| 128 | static PyObject *
|
---|
| 129 | deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
---|
| 130 | {
|
---|
[391] | 131 | dequeobject *deque;
|
---|
| 132 | block *b;
|
---|
[2] | 133 |
|
---|
[391] | 134 | /* create dequeobject structure */
|
---|
| 135 | deque = (dequeobject *)type->tp_alloc(type, 0);
|
---|
| 136 | if (deque == NULL)
|
---|
| 137 | return NULL;
|
---|
[2] | 138 |
|
---|
[391] | 139 | b = newblock(NULL, NULL, 0);
|
---|
| 140 | if (b == NULL) {
|
---|
| 141 | Py_DECREF(deque);
|
---|
| 142 | return NULL;
|
---|
| 143 | }
|
---|
[2] | 144 |
|
---|
[391] | 145 | assert(BLOCKLEN >= 2);
|
---|
| 146 | deque->leftblock = b;
|
---|
| 147 | deque->rightblock = b;
|
---|
| 148 | deque->leftindex = CENTER + 1;
|
---|
| 149 | deque->rightindex = CENTER;
|
---|
| 150 | deque->len = 0;
|
---|
| 151 | deque->state = 0;
|
---|
| 152 | deque->weakreflist = NULL;
|
---|
| 153 | deque->maxlen = -1;
|
---|
[2] | 154 |
|
---|
[391] | 155 | return (PyObject *)deque;
|
---|
[2] | 156 | }
|
---|
| 157 |
|
---|
| 158 | static PyObject *
|
---|
| 159 | deque_pop(dequeobject *deque, PyObject *unused)
|
---|
| 160 | {
|
---|
[391] | 161 | PyObject *item;
|
---|
| 162 | block *prevblock;
|
---|
[2] | 163 |
|
---|
[391] | 164 | if (deque->len == 0) {
|
---|
| 165 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
|
---|
| 166 | return NULL;
|
---|
| 167 | }
|
---|
| 168 | item = deque->rightblock->data[deque->rightindex];
|
---|
| 169 | deque->rightindex--;
|
---|
| 170 | deque->len--;
|
---|
| 171 | deque->state++;
|
---|
[2] | 172 |
|
---|
[391] | 173 | if (deque->rightindex == -1) {
|
---|
| 174 | if (deque->len == 0) {
|
---|
| 175 | assert(deque->leftblock == deque->rightblock);
|
---|
| 176 | assert(deque->leftindex == deque->rightindex+1);
|
---|
| 177 | /* re-center instead of freeing a block */
|
---|
| 178 | deque->leftindex = CENTER + 1;
|
---|
| 179 | deque->rightindex = CENTER;
|
---|
| 180 | } else {
|
---|
| 181 | prevblock = deque->rightblock->leftlink;
|
---|
| 182 | assert(deque->leftblock != deque->rightblock);
|
---|
| 183 | freeblock(deque->rightblock);
|
---|
| 184 | prevblock->rightlink = NULL;
|
---|
| 185 | deque->rightblock = prevblock;
|
---|
| 186 | deque->rightindex = BLOCKLEN - 1;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | return item;
|
---|
[2] | 190 | }
|
---|
| 191 |
|
---|
| 192 | PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
|
---|
| 193 |
|
---|
| 194 | static PyObject *
|
---|
| 195 | deque_popleft(dequeobject *deque, PyObject *unused)
|
---|
| 196 | {
|
---|
[391] | 197 | PyObject *item;
|
---|
| 198 | block *prevblock;
|
---|
[2] | 199 |
|
---|
[391] | 200 | if (deque->len == 0) {
|
---|
| 201 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
|
---|
| 202 | return NULL;
|
---|
| 203 | }
|
---|
| 204 | assert(deque->leftblock != NULL);
|
---|
| 205 | item = deque->leftblock->data[deque->leftindex];
|
---|
| 206 | deque->leftindex++;
|
---|
| 207 | deque->len--;
|
---|
| 208 | deque->state++;
|
---|
[2] | 209 |
|
---|
[391] | 210 | if (deque->leftindex == BLOCKLEN) {
|
---|
| 211 | if (deque->len == 0) {
|
---|
| 212 | assert(deque->leftblock == deque->rightblock);
|
---|
| 213 | assert(deque->leftindex == deque->rightindex+1);
|
---|
| 214 | /* re-center instead of freeing a block */
|
---|
| 215 | deque->leftindex = CENTER + 1;
|
---|
| 216 | deque->rightindex = CENTER;
|
---|
| 217 | } else {
|
---|
| 218 | assert(deque->leftblock != deque->rightblock);
|
---|
| 219 | prevblock = deque->leftblock->rightlink;
|
---|
| 220 | freeblock(deque->leftblock);
|
---|
| 221 | assert(prevblock != NULL);
|
---|
| 222 | prevblock->leftlink = NULL;
|
---|
| 223 | deque->leftblock = prevblock;
|
---|
| 224 | deque->leftindex = 0;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | return item;
|
---|
[2] | 228 | }
|
---|
| 229 |
|
---|
| 230 | PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
|
---|
| 231 |
|
---|
| 232 | static PyObject *
|
---|
| 233 | deque_append(dequeobject *deque, PyObject *item)
|
---|
| 234 | {
|
---|
[391] | 235 | deque->state++;
|
---|
| 236 | if (deque->rightindex == BLOCKLEN-1) {
|
---|
| 237 | block *b = newblock(deque->rightblock, NULL, deque->len);
|
---|
| 238 | if (b == NULL)
|
---|
| 239 | return NULL;
|
---|
| 240 | assert(deque->rightblock->rightlink == NULL);
|
---|
| 241 | deque->rightblock->rightlink = b;
|
---|
| 242 | deque->rightblock = b;
|
---|
| 243 | deque->rightindex = -1;
|
---|
| 244 | }
|
---|
| 245 | Py_INCREF(item);
|
---|
| 246 | deque->len++;
|
---|
| 247 | deque->rightindex++;
|
---|
| 248 | deque->rightblock->data[deque->rightindex] = item;
|
---|
| 249 | TRIM(deque, deque_popleft);
|
---|
| 250 | Py_RETURN_NONE;
|
---|
[2] | 251 | }
|
---|
| 252 |
|
---|
| 253 | PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
|
---|
| 254 |
|
---|
| 255 | static PyObject *
|
---|
| 256 | deque_appendleft(dequeobject *deque, PyObject *item)
|
---|
| 257 | {
|
---|
[391] | 258 | deque->state++;
|
---|
| 259 | if (deque->leftindex == 0) {
|
---|
| 260 | block *b = newblock(NULL, deque->leftblock, deque->len);
|
---|
| 261 | if (b == NULL)
|
---|
| 262 | return NULL;
|
---|
| 263 | assert(deque->leftblock->leftlink == NULL);
|
---|
| 264 | deque->leftblock->leftlink = b;
|
---|
| 265 | deque->leftblock = b;
|
---|
| 266 | deque->leftindex = BLOCKLEN;
|
---|
| 267 | }
|
---|
| 268 | Py_INCREF(item);
|
---|
| 269 | deque->len++;
|
---|
| 270 | deque->leftindex--;
|
---|
| 271 | deque->leftblock->data[deque->leftindex] = item;
|
---|
| 272 | TRIM(deque, deque_pop);
|
---|
| 273 | Py_RETURN_NONE;
|
---|
[2] | 274 | }
|
---|
| 275 |
|
---|
| 276 | PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
|
---|
| 277 |
|
---|
[391] | 278 |
|
---|
| 279 | /* Run an iterator to exhaustion. Shortcut for
|
---|
| 280 | the extend/extendleft methods when maxlen == 0. */
|
---|
| 281 | static PyObject*
|
---|
| 282 | consume_iterator(PyObject *it)
|
---|
| 283 | {
|
---|
| 284 | PyObject *item;
|
---|
| 285 |
|
---|
| 286 | while ((item = PyIter_Next(it)) != NULL) {
|
---|
| 287 | Py_DECREF(item);
|
---|
| 288 | }
|
---|
| 289 | Py_DECREF(it);
|
---|
| 290 | if (PyErr_Occurred())
|
---|
| 291 | return NULL;
|
---|
| 292 | Py_RETURN_NONE;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[2] | 295 | static PyObject *
|
---|
| 296 | deque_extend(dequeobject *deque, PyObject *iterable)
|
---|
| 297 | {
|
---|
[391] | 298 | PyObject *it, *item;
|
---|
[2] | 299 |
|
---|
[391] | 300 | /* Handle case where id(deque) == id(iterable) */
|
---|
| 301 | if ((PyObject *)deque == iterable) {
|
---|
| 302 | PyObject *result;
|
---|
| 303 | PyObject *s = PySequence_List(iterable);
|
---|
| 304 | if (s == NULL)
|
---|
| 305 | return NULL;
|
---|
| 306 | result = deque_extend(deque, s);
|
---|
| 307 | Py_DECREF(s);
|
---|
| 308 | return result;
|
---|
| 309 | }
|
---|
[2] | 310 |
|
---|
[391] | 311 | it = PyObject_GetIter(iterable);
|
---|
| 312 | if (it == NULL)
|
---|
| 313 | return NULL;
|
---|
[2] | 314 |
|
---|
[391] | 315 | if (deque->maxlen == 0)
|
---|
| 316 | return consume_iterator(it);
|
---|
| 317 |
|
---|
| 318 | while ((item = PyIter_Next(it)) != NULL) {
|
---|
| 319 | deque->state++;
|
---|
| 320 | if (deque->rightindex == BLOCKLEN-1) {
|
---|
| 321 | block *b = newblock(deque->rightblock, NULL,
|
---|
| 322 | deque->len);
|
---|
| 323 | if (b == NULL) {
|
---|
| 324 | Py_DECREF(item);
|
---|
| 325 | Py_DECREF(it);
|
---|
| 326 | return NULL;
|
---|
| 327 | }
|
---|
| 328 | assert(deque->rightblock->rightlink == NULL);
|
---|
| 329 | deque->rightblock->rightlink = b;
|
---|
| 330 | deque->rightblock = b;
|
---|
| 331 | deque->rightindex = -1;
|
---|
| 332 | }
|
---|
| 333 | deque->len++;
|
---|
| 334 | deque->rightindex++;
|
---|
| 335 | deque->rightblock->data[deque->rightindex] = item;
|
---|
| 336 | TRIM(deque, deque_popleft);
|
---|
| 337 | }
|
---|
| 338 | Py_DECREF(it);
|
---|
| 339 | if (PyErr_Occurred())
|
---|
| 340 | return NULL;
|
---|
| 341 | Py_RETURN_NONE;
|
---|
[2] | 342 | }
|
---|
| 343 |
|
---|
| 344 | PyDoc_STRVAR(extend_doc,
|
---|
| 345 | "Extend the right side of the deque with elements from the iterable");
|
---|
| 346 |
|
---|
| 347 | static PyObject *
|
---|
| 348 | deque_extendleft(dequeobject *deque, PyObject *iterable)
|
---|
| 349 | {
|
---|
[391] | 350 | PyObject *it, *item;
|
---|
[2] | 351 |
|
---|
[391] | 352 | /* Handle case where id(deque) == id(iterable) */
|
---|
| 353 | if ((PyObject *)deque == iterable) {
|
---|
| 354 | PyObject *result;
|
---|
| 355 | PyObject *s = PySequence_List(iterable);
|
---|
| 356 | if (s == NULL)
|
---|
| 357 | return NULL;
|
---|
| 358 | result = deque_extendleft(deque, s);
|
---|
| 359 | Py_DECREF(s);
|
---|
| 360 | return result;
|
---|
| 361 | }
|
---|
[2] | 362 |
|
---|
[391] | 363 | it = PyObject_GetIter(iterable);
|
---|
| 364 | if (it == NULL)
|
---|
| 365 | return NULL;
|
---|
[2] | 366 |
|
---|
[391] | 367 | if (deque->maxlen == 0)
|
---|
| 368 | return consume_iterator(it);
|
---|
| 369 |
|
---|
| 370 | while ((item = PyIter_Next(it)) != NULL) {
|
---|
| 371 | deque->state++;
|
---|
| 372 | if (deque->leftindex == 0) {
|
---|
| 373 | block *b = newblock(NULL, deque->leftblock,
|
---|
| 374 | deque->len);
|
---|
| 375 | if (b == NULL) {
|
---|
| 376 | Py_DECREF(item);
|
---|
| 377 | Py_DECREF(it);
|
---|
| 378 | return NULL;
|
---|
| 379 | }
|
---|
| 380 | assert(deque->leftblock->leftlink == NULL);
|
---|
| 381 | deque->leftblock->leftlink = b;
|
---|
| 382 | deque->leftblock = b;
|
---|
| 383 | deque->leftindex = BLOCKLEN;
|
---|
| 384 | }
|
---|
| 385 | deque->len++;
|
---|
| 386 | deque->leftindex--;
|
---|
| 387 | deque->leftblock->data[deque->leftindex] = item;
|
---|
| 388 | TRIM(deque, deque_pop);
|
---|
| 389 | }
|
---|
| 390 | Py_DECREF(it);
|
---|
| 391 | if (PyErr_Occurred())
|
---|
| 392 | return NULL;
|
---|
| 393 | Py_RETURN_NONE;
|
---|
[2] | 394 | }
|
---|
| 395 |
|
---|
| 396 | PyDoc_STRVAR(extendleft_doc,
|
---|
| 397 | "Extend the left side of the deque with elements from the iterable");
|
---|
| 398 |
|
---|
| 399 | static PyObject *
|
---|
| 400 | deque_inplace_concat(dequeobject *deque, PyObject *other)
|
---|
| 401 | {
|
---|
[391] | 402 | PyObject *result;
|
---|
[2] | 403 |
|
---|
[391] | 404 | result = deque_extend(deque, other);
|
---|
| 405 | if (result == NULL)
|
---|
| 406 | return result;
|
---|
| 407 | Py_DECREF(result);
|
---|
| 408 | Py_INCREF(deque);
|
---|
| 409 | return (PyObject *)deque;
|
---|
[2] | 410 | }
|
---|
| 411 |
|
---|
| 412 | static int
|
---|
| 413 | _deque_rotate(dequeobject *deque, Py_ssize_t n)
|
---|
| 414 | {
|
---|
[391] | 415 | Py_ssize_t m, len=deque->len, halflen=len>>1;
|
---|
[2] | 416 |
|
---|
[391] | 417 | if (len <= 1)
|
---|
| 418 | return 0;
|
---|
| 419 | if (n > halflen || n < -halflen) {
|
---|
| 420 | n %= len;
|
---|
| 421 | if (n > halflen)
|
---|
| 422 | n -= len;
|
---|
| 423 | else if (n < -halflen)
|
---|
| 424 | n += len;
|
---|
| 425 | }
|
---|
| 426 | assert(len > 1);
|
---|
| 427 | assert(-halflen <= n && n <= halflen);
|
---|
[2] | 428 |
|
---|
[391] | 429 | deque->state++;
|
---|
| 430 | while (n > 0) {
|
---|
| 431 | if (deque->leftindex == 0) {
|
---|
| 432 | block *b = newblock(NULL, deque->leftblock, len);
|
---|
| 433 | if (b == NULL)
|
---|
| 434 | return -1;
|
---|
| 435 | assert(deque->leftblock->leftlink == NULL);
|
---|
| 436 | deque->leftblock->leftlink = b;
|
---|
| 437 | deque->leftblock = b;
|
---|
| 438 | deque->leftindex = BLOCKLEN;
|
---|
| 439 | }
|
---|
| 440 | assert(deque->leftindex > 0);
|
---|
| 441 |
|
---|
| 442 | m = n;
|
---|
| 443 | if (m > deque->rightindex + 1)
|
---|
| 444 | m = deque->rightindex + 1;
|
---|
| 445 | if (m > deque->leftindex)
|
---|
| 446 | m = deque->leftindex;
|
---|
| 447 | assert (m > 0 && m <= len);
|
---|
| 448 | memcpy(&deque->leftblock->data[deque->leftindex - m],
|
---|
| 449 | &deque->rightblock->data[deque->rightindex + 1 - m],
|
---|
| 450 | m * sizeof(PyObject *));
|
---|
| 451 | deque->rightindex -= m;
|
---|
| 452 | deque->leftindex -= m;
|
---|
| 453 | n -= m;
|
---|
| 454 |
|
---|
| 455 | if (deque->rightindex == -1) {
|
---|
| 456 | block *prevblock = deque->rightblock->leftlink;
|
---|
| 457 | assert(deque->rightblock != NULL);
|
---|
| 458 | assert(deque->leftblock != deque->rightblock);
|
---|
| 459 | freeblock(deque->rightblock);
|
---|
| 460 | prevblock->rightlink = NULL;
|
---|
| 461 | deque->rightblock = prevblock;
|
---|
| 462 | deque->rightindex = BLOCKLEN - 1;
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 | while (n < 0) {
|
---|
| 466 | if (deque->rightindex == BLOCKLEN - 1) {
|
---|
| 467 | block *b = newblock(deque->rightblock, NULL, len);
|
---|
| 468 | if (b == NULL)
|
---|
| 469 | return -1;
|
---|
| 470 | assert(deque->rightblock->rightlink == NULL);
|
---|
| 471 | deque->rightblock->rightlink = b;
|
---|
| 472 | deque->rightblock = b;
|
---|
| 473 | deque->rightindex = -1;
|
---|
| 474 | }
|
---|
| 475 | assert (deque->rightindex < BLOCKLEN - 1);
|
---|
| 476 |
|
---|
| 477 | m = -n;
|
---|
| 478 | if (m > BLOCKLEN - deque->leftindex)
|
---|
| 479 | m = BLOCKLEN - deque->leftindex;
|
---|
| 480 | if (m > BLOCKLEN - 1 - deque->rightindex)
|
---|
| 481 | m = BLOCKLEN - 1 - deque->rightindex;
|
---|
| 482 | assert (m > 0 && m <= len);
|
---|
| 483 | memcpy(&deque->rightblock->data[deque->rightindex + 1],
|
---|
| 484 | &deque->leftblock->data[deque->leftindex],
|
---|
| 485 | m * sizeof(PyObject *));
|
---|
| 486 | deque->leftindex += m;
|
---|
| 487 | deque->rightindex += m;
|
---|
| 488 | n += m;
|
---|
| 489 |
|
---|
| 490 | if (deque->leftindex == BLOCKLEN) {
|
---|
| 491 | block *nextblock = deque->leftblock->rightlink;
|
---|
| 492 | assert(deque->leftblock != deque->rightblock);
|
---|
| 493 | freeblock(deque->leftblock);
|
---|
| 494 | assert(nextblock != NULL);
|
---|
| 495 | nextblock->leftlink = NULL;
|
---|
| 496 | deque->leftblock = nextblock;
|
---|
| 497 | deque->leftindex = 0;
|
---|
| 498 | }
|
---|
| 499 | }
|
---|
| 500 | return 0;
|
---|
[2] | 501 | }
|
---|
| 502 |
|
---|
| 503 | static PyObject *
|
---|
| 504 | deque_rotate(dequeobject *deque, PyObject *args)
|
---|
| 505 | {
|
---|
[391] | 506 | Py_ssize_t n=1;
|
---|
[2] | 507 |
|
---|
[391] | 508 | if (!PyArg_ParseTuple(args, "|n:rotate", &n))
|
---|
| 509 | return NULL;
|
---|
| 510 | if (_deque_rotate(deque, n) == 0)
|
---|
| 511 | Py_RETURN_NONE;
|
---|
| 512 | return NULL;
|
---|
[2] | 513 | }
|
---|
| 514 |
|
---|
| 515 | PyDoc_STRVAR(rotate_doc,
|
---|
| 516 | "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
|
---|
| 517 |
|
---|
[391] | 518 | static PyObject *
|
---|
| 519 | deque_reverse(dequeobject *deque, PyObject *unused)
|
---|
| 520 | {
|
---|
| 521 | block *leftblock = deque->leftblock;
|
---|
| 522 | block *rightblock = deque->rightblock;
|
---|
| 523 | Py_ssize_t leftindex = deque->leftindex;
|
---|
| 524 | Py_ssize_t rightindex = deque->rightindex;
|
---|
| 525 | Py_ssize_t n = (deque->len)/2;
|
---|
| 526 | Py_ssize_t i;
|
---|
| 527 | PyObject *tmp;
|
---|
| 528 |
|
---|
| 529 | for (i=0 ; i<n ; i++) {
|
---|
| 530 | /* Validate that pointers haven't met in the middle */
|
---|
| 531 | assert(leftblock != rightblock || leftindex < rightindex);
|
---|
| 532 |
|
---|
| 533 | /* Swap */
|
---|
| 534 | tmp = leftblock->data[leftindex];
|
---|
| 535 | leftblock->data[leftindex] = rightblock->data[rightindex];
|
---|
| 536 | rightblock->data[rightindex] = tmp;
|
---|
| 537 |
|
---|
| 538 | /* Advance left block/index pair */
|
---|
| 539 | leftindex++;
|
---|
| 540 | if (leftindex == BLOCKLEN) {
|
---|
| 541 | if (leftblock->rightlink == NULL)
|
---|
| 542 | break;
|
---|
| 543 | leftblock = leftblock->rightlink;
|
---|
| 544 | leftindex = 0;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /* Step backwards with the right block/index pair */
|
---|
| 548 | rightindex--;
|
---|
| 549 | if (rightindex == -1) {
|
---|
| 550 | if (rightblock->leftlink == NULL)
|
---|
| 551 | break;
|
---|
| 552 | rightblock = rightblock->leftlink;
|
---|
| 553 | rightindex = BLOCKLEN - 1;
|
---|
| 554 | }
|
---|
| 555 | }
|
---|
| 556 | Py_RETURN_NONE;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | PyDoc_STRVAR(reverse_doc,
|
---|
| 560 | "D.reverse() -- reverse *IN PLACE*");
|
---|
| 561 |
|
---|
| 562 | static PyObject *
|
---|
| 563 | deque_count(dequeobject *deque, PyObject *v)
|
---|
| 564 | {
|
---|
| 565 | block *leftblock = deque->leftblock;
|
---|
| 566 | Py_ssize_t leftindex = deque->leftindex;
|
---|
| 567 | Py_ssize_t n = deque->len;
|
---|
| 568 | Py_ssize_t i;
|
---|
| 569 | Py_ssize_t count = 0;
|
---|
| 570 | PyObject *item;
|
---|
| 571 | long start_state = deque->state;
|
---|
| 572 | int cmp;
|
---|
| 573 |
|
---|
| 574 | for (i=0 ; i<n ; i++) {
|
---|
| 575 | item = leftblock->data[leftindex];
|
---|
| 576 | cmp = PyObject_RichCompareBool(item, v, Py_EQ);
|
---|
| 577 | if (cmp > 0)
|
---|
| 578 | count++;
|
---|
| 579 | else if (cmp < 0)
|
---|
| 580 | return NULL;
|
---|
| 581 |
|
---|
| 582 | if (start_state != deque->state) {
|
---|
| 583 | PyErr_SetString(PyExc_RuntimeError,
|
---|
| 584 | "deque mutated during iteration");
|
---|
| 585 | return NULL;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | /* Advance left block/index pair */
|
---|
| 589 | leftindex++;
|
---|
| 590 | if (leftindex == BLOCKLEN) {
|
---|
| 591 | if (leftblock->rightlink == NULL) /* can occur when i==n-1 */
|
---|
| 592 | break;
|
---|
| 593 | leftblock = leftblock->rightlink;
|
---|
| 594 | leftindex = 0;
|
---|
| 595 | }
|
---|
| 596 | }
|
---|
| 597 | return PyInt_FromSsize_t(count);
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | PyDoc_STRVAR(count_doc,
|
---|
| 601 | "D.count(value) -> integer -- return number of occurrences of value");
|
---|
| 602 |
|
---|
[2] | 603 | static Py_ssize_t
|
---|
| 604 | deque_len(dequeobject *deque)
|
---|
| 605 | {
|
---|
[391] | 606 | return deque->len;
|
---|
[2] | 607 | }
|
---|
| 608 |
|
---|
| 609 | static PyObject *
|
---|
| 610 | deque_remove(dequeobject *deque, PyObject *value)
|
---|
| 611 | {
|
---|
[391] | 612 | Py_ssize_t i, n=deque->len;
|
---|
[2] | 613 |
|
---|
[391] | 614 | for (i=0 ; i<n ; i++) {
|
---|
| 615 | PyObject *item = deque->leftblock->data[deque->leftindex];
|
---|
| 616 | int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
|
---|
[2] | 617 |
|
---|
[391] | 618 | if (deque->len != n) {
|
---|
| 619 | PyErr_SetString(PyExc_IndexError,
|
---|
| 620 | "deque mutated during remove().");
|
---|
| 621 | return NULL;
|
---|
| 622 | }
|
---|
| 623 | if (cmp > 0) {
|
---|
| 624 | PyObject *tgt = deque_popleft(deque, NULL);
|
---|
| 625 | assert (tgt != NULL);
|
---|
| 626 | Py_DECREF(tgt);
|
---|
| 627 | if (_deque_rotate(deque, i) == -1)
|
---|
| 628 | return NULL;
|
---|
| 629 | Py_RETURN_NONE;
|
---|
| 630 | }
|
---|
| 631 | else if (cmp < 0) {
|
---|
| 632 | _deque_rotate(deque, i);
|
---|
| 633 | return NULL;
|
---|
| 634 | }
|
---|
| 635 | _deque_rotate(deque, -1);
|
---|
| 636 | }
|
---|
| 637 | PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
|
---|
| 638 | return NULL;
|
---|
[2] | 639 | }
|
---|
| 640 |
|
---|
| 641 | PyDoc_STRVAR(remove_doc,
|
---|
| 642 | "D.remove(value) -- remove first occurrence of value.");
|
---|
| 643 |
|
---|
[391] | 644 | static void
|
---|
[2] | 645 | deque_clear(dequeobject *deque)
|
---|
| 646 | {
|
---|
[391] | 647 | PyObject *item;
|
---|
[2] | 648 |
|
---|
[391] | 649 | while (deque->len) {
|
---|
| 650 | item = deque_pop(deque, NULL);
|
---|
| 651 | assert (item != NULL);
|
---|
| 652 | Py_DECREF(item);
|
---|
| 653 | }
|
---|
| 654 | assert(deque->leftblock == deque->rightblock &&
|
---|
| 655 | deque->leftindex - 1 == deque->rightindex &&
|
---|
| 656 | deque->len == 0);
|
---|
[2] | 657 | }
|
---|
| 658 |
|
---|
| 659 | static PyObject *
|
---|
| 660 | deque_item(dequeobject *deque, Py_ssize_t i)
|
---|
| 661 | {
|
---|
[391] | 662 | block *b;
|
---|
| 663 | PyObject *item;
|
---|
| 664 | Py_ssize_t n, index=i;
|
---|
[2] | 665 |
|
---|
[391] | 666 | if (i < 0 || i >= deque->len) {
|
---|
| 667 | PyErr_SetString(PyExc_IndexError,
|
---|
| 668 | "deque index out of range");
|
---|
| 669 | return NULL;
|
---|
| 670 | }
|
---|
[2] | 671 |
|
---|
[391] | 672 | if (i == 0) {
|
---|
| 673 | i = deque->leftindex;
|
---|
| 674 | b = deque->leftblock;
|
---|
| 675 | } else if (i == deque->len - 1) {
|
---|
| 676 | i = deque->rightindex;
|
---|
| 677 | b = deque->rightblock;
|
---|
| 678 | } else {
|
---|
| 679 | i += deque->leftindex;
|
---|
| 680 | n = i / BLOCKLEN;
|
---|
| 681 | i %= BLOCKLEN;
|
---|
| 682 | if (index < (deque->len >> 1)) {
|
---|
| 683 | b = deque->leftblock;
|
---|
| 684 | while (n--)
|
---|
| 685 | b = b->rightlink;
|
---|
| 686 | } else {
|
---|
| 687 | n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
|
---|
| 688 | b = deque->rightblock;
|
---|
| 689 | while (n--)
|
---|
| 690 | b = b->leftlink;
|
---|
| 691 | }
|
---|
| 692 | }
|
---|
| 693 | item = b->data[i];
|
---|
| 694 | Py_INCREF(item);
|
---|
| 695 | return item;
|
---|
[2] | 696 | }
|
---|
| 697 |
|
---|
| 698 | /* delitem() implemented in terms of rotate for simplicity and reasonable
|
---|
| 699 | performance near the end points. If for some reason this method becomes
|
---|
| 700 | popular, it is not hard to re-implement this using direct data movement
|
---|
| 701 | (similar to code in list slice assignment) and achieve a two or threefold
|
---|
| 702 | performance boost.
|
---|
| 703 | */
|
---|
| 704 |
|
---|
| 705 | static int
|
---|
| 706 | deque_del_item(dequeobject *deque, Py_ssize_t i)
|
---|
| 707 | {
|
---|
[391] | 708 | PyObject *item;
|
---|
[2] | 709 |
|
---|
[391] | 710 | assert (i >= 0 && i < deque->len);
|
---|
| 711 | if (_deque_rotate(deque, -i) == -1)
|
---|
| 712 | return -1;
|
---|
[2] | 713 |
|
---|
[391] | 714 | item = deque_popleft(deque, NULL);
|
---|
| 715 | assert (item != NULL);
|
---|
| 716 | Py_DECREF(item);
|
---|
[2] | 717 |
|
---|
[391] | 718 | return _deque_rotate(deque, i);
|
---|
[2] | 719 | }
|
---|
| 720 |
|
---|
| 721 | static int
|
---|
| 722 | deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
|
---|
| 723 | {
|
---|
[391] | 724 | PyObject *old_value;
|
---|
| 725 | block *b;
|
---|
| 726 | Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
|
---|
[2] | 727 |
|
---|
[391] | 728 | if (i < 0 || i >= len) {
|
---|
| 729 | PyErr_SetString(PyExc_IndexError,
|
---|
| 730 | "deque index out of range");
|
---|
| 731 | return -1;
|
---|
| 732 | }
|
---|
| 733 | if (v == NULL)
|
---|
| 734 | return deque_del_item(deque, i);
|
---|
[2] | 735 |
|
---|
[391] | 736 | i += deque->leftindex;
|
---|
| 737 | n = i / BLOCKLEN;
|
---|
| 738 | i %= BLOCKLEN;
|
---|
| 739 | if (index <= halflen) {
|
---|
| 740 | b = deque->leftblock;
|
---|
| 741 | while (n--)
|
---|
| 742 | b = b->rightlink;
|
---|
| 743 | } else {
|
---|
| 744 | n = (deque->leftindex + len - 1) / BLOCKLEN - n;
|
---|
| 745 | b = deque->rightblock;
|
---|
| 746 | while (n--)
|
---|
| 747 | b = b->leftlink;
|
---|
| 748 | }
|
---|
| 749 | Py_INCREF(v);
|
---|
| 750 | old_value = b->data[i];
|
---|
| 751 | b->data[i] = v;
|
---|
| 752 | Py_DECREF(old_value);
|
---|
| 753 | return 0;
|
---|
[2] | 754 | }
|
---|
| 755 |
|
---|
| 756 | static PyObject *
|
---|
| 757 | deque_clearmethod(dequeobject *deque)
|
---|
| 758 | {
|
---|
[391] | 759 | deque_clear(deque);
|
---|
| 760 | Py_RETURN_NONE;
|
---|
[2] | 761 | }
|
---|
| 762 |
|
---|
| 763 | PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
|
---|
| 764 |
|
---|
| 765 | static void
|
---|
| 766 | deque_dealloc(dequeobject *deque)
|
---|
| 767 | {
|
---|
[391] | 768 | PyObject_GC_UnTrack(deque);
|
---|
| 769 | if (deque->weakreflist != NULL)
|
---|
| 770 | PyObject_ClearWeakRefs((PyObject *) deque);
|
---|
| 771 | if (deque->leftblock != NULL) {
|
---|
| 772 | deque_clear(deque);
|
---|
| 773 | assert(deque->leftblock != NULL);
|
---|
| 774 | freeblock(deque->leftblock);
|
---|
| 775 | }
|
---|
| 776 | deque->leftblock = NULL;
|
---|
| 777 | deque->rightblock = NULL;
|
---|
| 778 | Py_TYPE(deque)->tp_free(deque);
|
---|
[2] | 779 | }
|
---|
| 780 |
|
---|
| 781 | static int
|
---|
| 782 | deque_traverse(dequeobject *deque, visitproc visit, void *arg)
|
---|
| 783 | {
|
---|
[391] | 784 | block *b;
|
---|
| 785 | PyObject *item;
|
---|
| 786 | Py_ssize_t index;
|
---|
| 787 | Py_ssize_t indexlo = deque->leftindex;
|
---|
[2] | 788 |
|
---|
[391] | 789 | for (b = deque->leftblock; b != NULL; b = b->rightlink) {
|
---|
| 790 | const Py_ssize_t indexhi = b == deque->rightblock ?
|
---|
| 791 | deque->rightindex :
|
---|
| 792 | BLOCKLEN - 1;
|
---|
[2] | 793 |
|
---|
[391] | 794 | for (index = indexlo; index <= indexhi; ++index) {
|
---|
| 795 | item = b->data[index];
|
---|
| 796 | Py_VISIT(item);
|
---|
| 797 | }
|
---|
| 798 | indexlo = 0;
|
---|
| 799 | }
|
---|
| 800 | return 0;
|
---|
[2] | 801 | }
|
---|
| 802 |
|
---|
| 803 | static PyObject *
|
---|
| 804 | deque_copy(PyObject *deque)
|
---|
| 805 | {
|
---|
[391] | 806 | if (((dequeobject *)deque)->maxlen == -1)
|
---|
| 807 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
|
---|
| 808 | else
|
---|
| 809 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
|
---|
| 810 | deque, ((dequeobject *)deque)->maxlen, NULL);
|
---|
[2] | 811 | }
|
---|
| 812 |
|
---|
| 813 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
|
---|
| 814 |
|
---|
| 815 | static PyObject *
|
---|
| 816 | deque_reduce(dequeobject *deque)
|
---|
| 817 | {
|
---|
[391] | 818 | PyObject *dict, *result, *aslist;
|
---|
[2] | 819 |
|
---|
[391] | 820 | dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
|
---|
| 821 | if (dict == NULL)
|
---|
| 822 | PyErr_Clear();
|
---|
| 823 | aslist = PySequence_List((PyObject *)deque);
|
---|
| 824 | if (aslist == NULL) {
|
---|
| 825 | Py_XDECREF(dict);
|
---|
| 826 | return NULL;
|
---|
| 827 | }
|
---|
| 828 | if (dict == NULL) {
|
---|
| 829 | if (deque->maxlen == -1)
|
---|
| 830 | result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
|
---|
| 831 | else
|
---|
| 832 | result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
|
---|
| 833 | } else {
|
---|
| 834 | if (deque->maxlen == -1)
|
---|
| 835 | result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
|
---|
| 836 | else
|
---|
| 837 | result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
|
---|
| 838 | }
|
---|
| 839 | Py_XDECREF(dict);
|
---|
| 840 | Py_DECREF(aslist);
|
---|
| 841 | return result;
|
---|
[2] | 842 | }
|
---|
| 843 |
|
---|
| 844 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
|
---|
| 845 |
|
---|
| 846 | static PyObject *
|
---|
| 847 | deque_repr(PyObject *deque)
|
---|
| 848 | {
|
---|
[391] | 849 | PyObject *aslist, *result, *fmt;
|
---|
| 850 | int i;
|
---|
[2] | 851 |
|
---|
[391] | 852 | i = Py_ReprEnter(deque);
|
---|
| 853 | if (i != 0) {
|
---|
| 854 | if (i < 0)
|
---|
| 855 | return NULL;
|
---|
| 856 | return PyString_FromString("[...]");
|
---|
| 857 | }
|
---|
[2] | 858 |
|
---|
[391] | 859 | aslist = PySequence_List(deque);
|
---|
| 860 | if (aslist == NULL) {
|
---|
| 861 | Py_ReprLeave(deque);
|
---|
| 862 | return NULL;
|
---|
| 863 | }
|
---|
| 864 | if (((dequeobject *)deque)->maxlen != -1)
|
---|
| 865 | fmt = PyString_FromFormat("deque(%%r, maxlen=%zd)",
|
---|
| 866 | ((dequeobject *)deque)->maxlen);
|
---|
| 867 | else
|
---|
| 868 | fmt = PyString_FromString("deque(%r)");
|
---|
| 869 | if (fmt == NULL) {
|
---|
| 870 | Py_DECREF(aslist);
|
---|
| 871 | Py_ReprLeave(deque);
|
---|
| 872 | return NULL;
|
---|
| 873 | }
|
---|
| 874 | result = PyString_Format(fmt, aslist);
|
---|
| 875 | Py_DECREF(fmt);
|
---|
| 876 | Py_DECREF(aslist);
|
---|
| 877 | Py_ReprLeave(deque);
|
---|
| 878 | return result;
|
---|
[2] | 879 | }
|
---|
| 880 |
|
---|
| 881 | static int
|
---|
| 882 | deque_tp_print(PyObject *deque, FILE *fp, int flags)
|
---|
| 883 | {
|
---|
[391] | 884 | PyObject *it, *item;
|
---|
| 885 | char *emit = ""; /* No separator emitted on first pass */
|
---|
| 886 | char *separator = ", ";
|
---|
| 887 | int i;
|
---|
[2] | 888 |
|
---|
[391] | 889 | i = Py_ReprEnter(deque);
|
---|
| 890 | if (i != 0) {
|
---|
| 891 | if (i < 0)
|
---|
| 892 | return i;
|
---|
| 893 | Py_BEGIN_ALLOW_THREADS
|
---|
| 894 | fputs("[...]", fp);
|
---|
| 895 | Py_END_ALLOW_THREADS
|
---|
| 896 | return 0;
|
---|
| 897 | }
|
---|
[2] | 898 |
|
---|
[391] | 899 | it = PyObject_GetIter(deque);
|
---|
| 900 | if (it == NULL)
|
---|
| 901 | return -1;
|
---|
[2] | 902 |
|
---|
[391] | 903 | Py_BEGIN_ALLOW_THREADS
|
---|
| 904 | fputs("deque([", fp);
|
---|
| 905 | Py_END_ALLOW_THREADS
|
---|
| 906 | while ((item = PyIter_Next(it)) != NULL) {
|
---|
| 907 | Py_BEGIN_ALLOW_THREADS
|
---|
| 908 | fputs(emit, fp);
|
---|
| 909 | Py_END_ALLOW_THREADS
|
---|
| 910 | emit = separator;
|
---|
| 911 | if (PyObject_Print(item, fp, 0) != 0) {
|
---|
| 912 | Py_DECREF(item);
|
---|
| 913 | Py_DECREF(it);
|
---|
| 914 | Py_ReprLeave(deque);
|
---|
| 915 | return -1;
|
---|
| 916 | }
|
---|
| 917 | Py_DECREF(item);
|
---|
| 918 | }
|
---|
| 919 | Py_ReprLeave(deque);
|
---|
| 920 | Py_DECREF(it);
|
---|
| 921 | if (PyErr_Occurred())
|
---|
| 922 | return -1;
|
---|
[2] | 923 |
|
---|
[391] | 924 | Py_BEGIN_ALLOW_THREADS
|
---|
| 925 | if (((dequeobject *)deque)->maxlen == -1)
|
---|
| 926 | fputs("])", fp);
|
---|
| 927 | else
|
---|
| 928 | fprintf(fp, "], maxlen=%" PY_FORMAT_SIZE_T "d)", ((dequeobject *)deque)->maxlen);
|
---|
| 929 | Py_END_ALLOW_THREADS
|
---|
| 930 | return 0;
|
---|
[2] | 931 | }
|
---|
| 932 |
|
---|
| 933 | static PyObject *
|
---|
| 934 | deque_richcompare(PyObject *v, PyObject *w, int op)
|
---|
| 935 | {
|
---|
[391] | 936 | PyObject *it1=NULL, *it2=NULL, *x, *y;
|
---|
| 937 | Py_ssize_t vs, ws;
|
---|
| 938 | int b, cmp=-1;
|
---|
[2] | 939 |
|
---|
[391] | 940 | if (!PyObject_TypeCheck(v, &deque_type) ||
|
---|
| 941 | !PyObject_TypeCheck(w, &deque_type)) {
|
---|
| 942 | Py_INCREF(Py_NotImplemented);
|
---|
| 943 | return Py_NotImplemented;
|
---|
| 944 | }
|
---|
[2] | 945 |
|
---|
[391] | 946 | /* Shortcuts */
|
---|
| 947 | vs = ((dequeobject *)v)->len;
|
---|
| 948 | ws = ((dequeobject *)w)->len;
|
---|
| 949 | if (op == Py_EQ) {
|
---|
| 950 | if (v == w)
|
---|
| 951 | Py_RETURN_TRUE;
|
---|
| 952 | if (vs != ws)
|
---|
| 953 | Py_RETURN_FALSE;
|
---|
| 954 | }
|
---|
| 955 | if (op == Py_NE) {
|
---|
| 956 | if (v == w)
|
---|
| 957 | Py_RETURN_FALSE;
|
---|
| 958 | if (vs != ws)
|
---|
| 959 | Py_RETURN_TRUE;
|
---|
| 960 | }
|
---|
[2] | 961 |
|
---|
[391] | 962 | /* Search for the first index where items are different */
|
---|
| 963 | it1 = PyObject_GetIter(v);
|
---|
| 964 | if (it1 == NULL)
|
---|
| 965 | goto done;
|
---|
| 966 | it2 = PyObject_GetIter(w);
|
---|
| 967 | if (it2 == NULL)
|
---|
| 968 | goto done;
|
---|
| 969 | for (;;) {
|
---|
| 970 | x = PyIter_Next(it1);
|
---|
| 971 | if (x == NULL && PyErr_Occurred())
|
---|
| 972 | goto done;
|
---|
| 973 | y = PyIter_Next(it2);
|
---|
| 974 | if (x == NULL || y == NULL)
|
---|
| 975 | break;
|
---|
| 976 | b = PyObject_RichCompareBool(x, y, Py_EQ);
|
---|
| 977 | if (b == 0) {
|
---|
| 978 | cmp = PyObject_RichCompareBool(x, y, op);
|
---|
| 979 | Py_DECREF(x);
|
---|
| 980 | Py_DECREF(y);
|
---|
| 981 | goto done;
|
---|
| 982 | }
|
---|
| 983 | Py_DECREF(x);
|
---|
| 984 | Py_DECREF(y);
|
---|
| 985 | if (b == -1)
|
---|
| 986 | goto done;
|
---|
| 987 | }
|
---|
| 988 | /* We reached the end of one deque or both */
|
---|
| 989 | Py_XDECREF(x);
|
---|
| 990 | Py_XDECREF(y);
|
---|
| 991 | if (PyErr_Occurred())
|
---|
| 992 | goto done;
|
---|
| 993 | switch (op) {
|
---|
| 994 | case Py_LT: cmp = y != NULL; break; /* if w was longer */
|
---|
| 995 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */
|
---|
| 996 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
|
---|
| 997 | case Py_NE: cmp = x != y; break; /* if one deque continues */
|
---|
| 998 | case Py_GT: cmp = x != NULL; break; /* if v was longer */
|
---|
| 999 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */
|
---|
| 1000 | }
|
---|
[2] | 1001 |
|
---|
| 1002 | done:
|
---|
[391] | 1003 | Py_XDECREF(it1);
|
---|
| 1004 | Py_XDECREF(it2);
|
---|
| 1005 | if (cmp == 1)
|
---|
| 1006 | Py_RETURN_TRUE;
|
---|
| 1007 | if (cmp == 0)
|
---|
| 1008 | Py_RETURN_FALSE;
|
---|
| 1009 | return NULL;
|
---|
[2] | 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | static int
|
---|
| 1013 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
|
---|
| 1014 | {
|
---|
[391] | 1015 | PyObject *iterable = NULL;
|
---|
| 1016 | PyObject *maxlenobj = NULL;
|
---|
| 1017 | Py_ssize_t maxlen = -1;
|
---|
| 1018 | char *kwlist[] = {"iterable", "maxlen", 0};
|
---|
[2] | 1019 |
|
---|
[391] | 1020 | if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
|
---|
| 1021 | return -1;
|
---|
| 1022 | if (maxlenobj != NULL && maxlenobj != Py_None) {
|
---|
| 1023 | maxlen = PyInt_AsSsize_t(maxlenobj);
|
---|
| 1024 | if (maxlen == -1 && PyErr_Occurred())
|
---|
| 1025 | return -1;
|
---|
| 1026 | if (maxlen < 0) {
|
---|
| 1027 | PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
|
---|
| 1028 | return -1;
|
---|
| 1029 | }
|
---|
| 1030 | }
|
---|
| 1031 | deque->maxlen = maxlen;
|
---|
| 1032 | deque_clear(deque);
|
---|
| 1033 | if (iterable != NULL) {
|
---|
| 1034 | PyObject *rv = deque_extend(deque, iterable);
|
---|
| 1035 | if (rv == NULL)
|
---|
| 1036 | return -1;
|
---|
| 1037 | Py_DECREF(rv);
|
---|
| 1038 | }
|
---|
| 1039 | return 0;
|
---|
[2] | 1040 | }
|
---|
| 1041 |
|
---|
[391] | 1042 | static PyObject *
|
---|
| 1043 | deque_sizeof(dequeobject *deque, void *unused)
|
---|
| 1044 | {
|
---|
| 1045 | Py_ssize_t res;
|
---|
| 1046 | Py_ssize_t blocks;
|
---|
| 1047 |
|
---|
| 1048 | res = sizeof(dequeobject);
|
---|
| 1049 | blocks = (deque->leftindex + deque->len + BLOCKLEN - 1) / BLOCKLEN;
|
---|
| 1050 | assert(deque->leftindex + deque->len - 1 ==
|
---|
| 1051 | (blocks - 1) * BLOCKLEN + deque->rightindex);
|
---|
| 1052 | res += blocks * sizeof(block);
|
---|
| 1053 | return PyLong_FromSsize_t(res);
|
---|
| 1054 | }
|
---|
| 1055 |
|
---|
| 1056 | PyDoc_STRVAR(sizeof_doc,
|
---|
| 1057 | "D.__sizeof__() -- size of D in memory, in bytes");
|
---|
| 1058 |
|
---|
| 1059 | static PyObject *
|
---|
| 1060 | deque_get_maxlen(dequeobject *deque)
|
---|
| 1061 | {
|
---|
| 1062 | if (deque->maxlen == -1)
|
---|
| 1063 | Py_RETURN_NONE;
|
---|
| 1064 | return PyInt_FromSsize_t(deque->maxlen);
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | static PyGetSetDef deque_getset[] = {
|
---|
| 1068 | {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
|
---|
| 1069 | "maximum size of a deque or None if unbounded"},
|
---|
| 1070 | {0}
|
---|
| 1071 | };
|
---|
| 1072 |
|
---|
[2] | 1073 | static PySequenceMethods deque_as_sequence = {
|
---|
[391] | 1074 | (lenfunc)deque_len, /* sq_length */
|
---|
| 1075 | 0, /* sq_concat */
|
---|
| 1076 | 0, /* sq_repeat */
|
---|
| 1077 | (ssizeargfunc)deque_item, /* sq_item */
|
---|
| 1078 | 0, /* sq_slice */
|
---|
| 1079 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
|
---|
| 1080 | 0, /* sq_ass_slice */
|
---|
| 1081 | 0, /* sq_contains */
|
---|
| 1082 | (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
|
---|
| 1083 | 0, /* sq_inplace_repeat */
|
---|
[2] | 1084 |
|
---|
| 1085 | };
|
---|
| 1086 |
|
---|
| 1087 | /* deque object ********************************************************/
|
---|
| 1088 |
|
---|
| 1089 | static PyObject *deque_iter(dequeobject *deque);
|
---|
| 1090 | static PyObject *deque_reviter(dequeobject *deque);
|
---|
| 1091 | PyDoc_STRVAR(reversed_doc,
|
---|
[391] | 1092 | "D.__reversed__() -- return a reverse iterator over the deque");
|
---|
[2] | 1093 |
|
---|
| 1094 | static PyMethodDef deque_methods[] = {
|
---|
[391] | 1095 | {"append", (PyCFunction)deque_append,
|
---|
| 1096 | METH_O, append_doc},
|
---|
| 1097 | {"appendleft", (PyCFunction)deque_appendleft,
|
---|
| 1098 | METH_O, appendleft_doc},
|
---|
| 1099 | {"clear", (PyCFunction)deque_clearmethod,
|
---|
| 1100 | METH_NOARGS, clear_doc},
|
---|
| 1101 | {"__copy__", (PyCFunction)deque_copy,
|
---|
| 1102 | METH_NOARGS, copy_doc},
|
---|
| 1103 | {"count", (PyCFunction)deque_count,
|
---|
| 1104 | METH_O, count_doc},
|
---|
| 1105 | {"extend", (PyCFunction)deque_extend,
|
---|
| 1106 | METH_O, extend_doc},
|
---|
| 1107 | {"extendleft", (PyCFunction)deque_extendleft,
|
---|
| 1108 | METH_O, extendleft_doc},
|
---|
| 1109 | {"pop", (PyCFunction)deque_pop,
|
---|
| 1110 | METH_NOARGS, pop_doc},
|
---|
| 1111 | {"popleft", (PyCFunction)deque_popleft,
|
---|
| 1112 | METH_NOARGS, popleft_doc},
|
---|
| 1113 | {"__reduce__", (PyCFunction)deque_reduce,
|
---|
| 1114 | METH_NOARGS, reduce_doc},
|
---|
| 1115 | {"remove", (PyCFunction)deque_remove,
|
---|
| 1116 | METH_O, remove_doc},
|
---|
| 1117 | {"__reversed__", (PyCFunction)deque_reviter,
|
---|
| 1118 | METH_NOARGS, reversed_doc},
|
---|
| 1119 | {"reverse", (PyCFunction)deque_reverse,
|
---|
| 1120 | METH_NOARGS, reverse_doc},
|
---|
| 1121 | {"rotate", (PyCFunction)deque_rotate,
|
---|
| 1122 | METH_VARARGS, rotate_doc},
|
---|
| 1123 | {"__sizeof__", (PyCFunction)deque_sizeof,
|
---|
| 1124 | METH_NOARGS, sizeof_doc},
|
---|
| 1125 | {NULL, NULL} /* sentinel */
|
---|
[2] | 1126 | };
|
---|
| 1127 |
|
---|
| 1128 | PyDoc_STRVAR(deque_doc,
|
---|
[391] | 1129 | "deque([iterable[, maxlen]]) --> deque object\n\
|
---|
[2] | 1130 | \n\
|
---|
[391] | 1131 | Build an ordered collection with optimized access from its endpoints.");
|
---|
[2] | 1132 |
|
---|
| 1133 | static PyTypeObject deque_type = {
|
---|
[391] | 1134 | PyVarObject_HEAD_INIT(NULL, 0)
|
---|
| 1135 | "collections.deque", /* tp_name */
|
---|
| 1136 | sizeof(dequeobject), /* tp_basicsize */
|
---|
| 1137 | 0, /* tp_itemsize */
|
---|
| 1138 | /* methods */
|
---|
| 1139 | (destructor)deque_dealloc, /* tp_dealloc */
|
---|
| 1140 | deque_tp_print, /* tp_print */
|
---|
| 1141 | 0, /* tp_getattr */
|
---|
| 1142 | 0, /* tp_setattr */
|
---|
| 1143 | 0, /* tp_compare */
|
---|
| 1144 | deque_repr, /* tp_repr */
|
---|
| 1145 | 0, /* tp_as_number */
|
---|
| 1146 | &deque_as_sequence, /* tp_as_sequence */
|
---|
| 1147 | 0, /* tp_as_mapping */
|
---|
| 1148 | (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
|
---|
| 1149 | 0, /* tp_call */
|
---|
| 1150 | 0, /* tp_str */
|
---|
| 1151 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
| 1152 | 0, /* tp_setattro */
|
---|
| 1153 | 0, /* tp_as_buffer */
|
---|
| 1154 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
|
---|
| 1155 | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
|
---|
| 1156 | deque_doc, /* tp_doc */
|
---|
| 1157 | (traverseproc)deque_traverse, /* tp_traverse */
|
---|
| 1158 | (inquiry)deque_clear, /* tp_clear */
|
---|
| 1159 | (richcmpfunc)deque_richcompare, /* tp_richcompare */
|
---|
| 1160 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
|
---|
| 1161 | (getiterfunc)deque_iter, /* tp_iter */
|
---|
| 1162 | 0, /* tp_iternext */
|
---|
| 1163 | deque_methods, /* tp_methods */
|
---|
| 1164 | 0, /* tp_members */
|
---|
| 1165 | deque_getset, /* tp_getset */
|
---|
| 1166 | 0, /* tp_base */
|
---|
| 1167 | 0, /* tp_dict */
|
---|
| 1168 | 0, /* tp_descr_get */
|
---|
| 1169 | 0, /* tp_descr_set */
|
---|
| 1170 | 0, /* tp_dictoffset */
|
---|
| 1171 | (initproc)deque_init, /* tp_init */
|
---|
| 1172 | PyType_GenericAlloc, /* tp_alloc */
|
---|
| 1173 | deque_new, /* tp_new */
|
---|
| 1174 | PyObject_GC_Del, /* tp_free */
|
---|
[2] | 1175 | };
|
---|
| 1176 |
|
---|
| 1177 | /*********************** Deque Iterator **************************/
|
---|
| 1178 |
|
---|
| 1179 | typedef struct {
|
---|
[391] | 1180 | PyObject_HEAD
|
---|
| 1181 | Py_ssize_t index;
|
---|
| 1182 | block *b;
|
---|
| 1183 | dequeobject *deque;
|
---|
| 1184 | long state; /* state when the iterator is created */
|
---|
| 1185 | Py_ssize_t counter; /* number of items remaining for iteration */
|
---|
[2] | 1186 | } dequeiterobject;
|
---|
| 1187 |
|
---|
| 1188 | static PyTypeObject dequeiter_type;
|
---|
| 1189 |
|
---|
| 1190 | static PyObject *
|
---|
| 1191 | deque_iter(dequeobject *deque)
|
---|
| 1192 | {
|
---|
[391] | 1193 | dequeiterobject *it;
|
---|
[2] | 1194 |
|
---|
[391] | 1195 | it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
|
---|
| 1196 | if (it == NULL)
|
---|
| 1197 | return NULL;
|
---|
| 1198 | it->b = deque->leftblock;
|
---|
| 1199 | it->index = deque->leftindex;
|
---|
| 1200 | Py_INCREF(deque);
|
---|
| 1201 | it->deque = deque;
|
---|
| 1202 | it->state = deque->state;
|
---|
| 1203 | it->counter = deque->len;
|
---|
| 1204 | PyObject_GC_Track(it);
|
---|
| 1205 | return (PyObject *)it;
|
---|
[2] | 1206 | }
|
---|
| 1207 |
|
---|
| 1208 | static int
|
---|
| 1209 | dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
|
---|
| 1210 | {
|
---|
[391] | 1211 | Py_VISIT(dio->deque);
|
---|
| 1212 | return 0;
|
---|
[2] | 1213 | }
|
---|
| 1214 |
|
---|
| 1215 | static void
|
---|
| 1216 | dequeiter_dealloc(dequeiterobject *dio)
|
---|
| 1217 | {
|
---|
[391] | 1218 | Py_XDECREF(dio->deque);
|
---|
| 1219 | PyObject_GC_Del(dio);
|
---|
[2] | 1220 | }
|
---|
| 1221 |
|
---|
| 1222 | static PyObject *
|
---|
| 1223 | dequeiter_next(dequeiterobject *it)
|
---|
| 1224 | {
|
---|
[391] | 1225 | PyObject *item;
|
---|
[2] | 1226 |
|
---|
[391] | 1227 | if (it->deque->state != it->state) {
|
---|
| 1228 | it->counter = 0;
|
---|
| 1229 | PyErr_SetString(PyExc_RuntimeError,
|
---|
| 1230 | "deque mutated during iteration");
|
---|
| 1231 | return NULL;
|
---|
| 1232 | }
|
---|
| 1233 | if (it->counter == 0)
|
---|
| 1234 | return NULL;
|
---|
| 1235 | assert (!(it->b == it->deque->rightblock &&
|
---|
| 1236 | it->index > it->deque->rightindex));
|
---|
[2] | 1237 |
|
---|
[391] | 1238 | item = it->b->data[it->index];
|
---|
| 1239 | it->index++;
|
---|
| 1240 | it->counter--;
|
---|
| 1241 | if (it->index == BLOCKLEN && it->counter > 0) {
|
---|
| 1242 | assert (it->b->rightlink != NULL);
|
---|
| 1243 | it->b = it->b->rightlink;
|
---|
| 1244 | it->index = 0;
|
---|
| 1245 | }
|
---|
| 1246 | Py_INCREF(item);
|
---|
| 1247 | return item;
|
---|
[2] | 1248 | }
|
---|
| 1249 |
|
---|
| 1250 | static PyObject *
|
---|
| 1251 | dequeiter_len(dequeiterobject *it)
|
---|
| 1252 | {
|
---|
[391] | 1253 | return PyInt_FromLong(it->counter);
|
---|
[2] | 1254 | }
|
---|
| 1255 |
|
---|
| 1256 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
|
---|
| 1257 |
|
---|
| 1258 | static PyMethodDef dequeiter_methods[] = {
|
---|
[391] | 1259 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
|
---|
| 1260 | {NULL, NULL} /* sentinel */
|
---|
[2] | 1261 | };
|
---|
| 1262 |
|
---|
| 1263 | static PyTypeObject dequeiter_type = {
|
---|
[391] | 1264 | PyVarObject_HEAD_INIT(NULL, 0)
|
---|
| 1265 | "deque_iterator", /* tp_name */
|
---|
| 1266 | sizeof(dequeiterobject), /* tp_basicsize */
|
---|
| 1267 | 0, /* tp_itemsize */
|
---|
| 1268 | /* methods */
|
---|
| 1269 | (destructor)dequeiter_dealloc, /* tp_dealloc */
|
---|
| 1270 | 0, /* tp_print */
|
---|
| 1271 | 0, /* tp_getattr */
|
---|
| 1272 | 0, /* tp_setattr */
|
---|
| 1273 | 0, /* tp_compare */
|
---|
| 1274 | 0, /* tp_repr */
|
---|
| 1275 | 0, /* tp_as_number */
|
---|
| 1276 | 0, /* tp_as_sequence */
|
---|
| 1277 | 0, /* tp_as_mapping */
|
---|
| 1278 | 0, /* tp_hash */
|
---|
| 1279 | 0, /* tp_call */
|
---|
| 1280 | 0, /* tp_str */
|
---|
| 1281 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
| 1282 | 0, /* tp_setattro */
|
---|
| 1283 | 0, /* tp_as_buffer */
|
---|
| 1284 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
---|
| 1285 | 0, /* tp_doc */
|
---|
| 1286 | (traverseproc)dequeiter_traverse, /* tp_traverse */
|
---|
| 1287 | 0, /* tp_clear */
|
---|
| 1288 | 0, /* tp_richcompare */
|
---|
| 1289 | 0, /* tp_weaklistoffset */
|
---|
| 1290 | PyObject_SelfIter, /* tp_iter */
|
---|
| 1291 | (iternextfunc)dequeiter_next, /* tp_iternext */
|
---|
| 1292 | dequeiter_methods, /* tp_methods */
|
---|
| 1293 | 0,
|
---|
[2] | 1294 | };
|
---|
| 1295 |
|
---|
| 1296 | /*********************** Deque Reverse Iterator **************************/
|
---|
| 1297 |
|
---|
| 1298 | static PyTypeObject dequereviter_type;
|
---|
| 1299 |
|
---|
| 1300 | static PyObject *
|
---|
| 1301 | deque_reviter(dequeobject *deque)
|
---|
| 1302 | {
|
---|
[391] | 1303 | dequeiterobject *it;
|
---|
[2] | 1304 |
|
---|
[391] | 1305 | it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
|
---|
| 1306 | if (it == NULL)
|
---|
| 1307 | return NULL;
|
---|
| 1308 | it->b = deque->rightblock;
|
---|
| 1309 | it->index = deque->rightindex;
|
---|
| 1310 | Py_INCREF(deque);
|
---|
| 1311 | it->deque = deque;
|
---|
| 1312 | it->state = deque->state;
|
---|
| 1313 | it->counter = deque->len;
|
---|
| 1314 | PyObject_GC_Track(it);
|
---|
| 1315 | return (PyObject *)it;
|
---|
[2] | 1316 | }
|
---|
| 1317 |
|
---|
| 1318 | static PyObject *
|
---|
| 1319 | dequereviter_next(dequeiterobject *it)
|
---|
| 1320 | {
|
---|
[391] | 1321 | PyObject *item;
|
---|
| 1322 | if (it->counter == 0)
|
---|
| 1323 | return NULL;
|
---|
[2] | 1324 |
|
---|
[391] | 1325 | if (it->deque->state != it->state) {
|
---|
| 1326 | it->counter = 0;
|
---|
| 1327 | PyErr_SetString(PyExc_RuntimeError,
|
---|
| 1328 | "deque mutated during iteration");
|
---|
| 1329 | return NULL;
|
---|
| 1330 | }
|
---|
| 1331 | assert (!(it->b == it->deque->leftblock &&
|
---|
| 1332 | it->index < it->deque->leftindex));
|
---|
[2] | 1333 |
|
---|
[391] | 1334 | item = it->b->data[it->index];
|
---|
| 1335 | it->index--;
|
---|
| 1336 | it->counter--;
|
---|
| 1337 | if (it->index == -1 && it->counter > 0) {
|
---|
| 1338 | assert (it->b->leftlink != NULL);
|
---|
| 1339 | it->b = it->b->leftlink;
|
---|
| 1340 | it->index = BLOCKLEN - 1;
|
---|
| 1341 | }
|
---|
| 1342 | Py_INCREF(item);
|
---|
| 1343 | return item;
|
---|
[2] | 1344 | }
|
---|
| 1345 |
|
---|
| 1346 | static PyTypeObject dequereviter_type = {
|
---|
[391] | 1347 | PyVarObject_HEAD_INIT(NULL, 0)
|
---|
| 1348 | "deque_reverse_iterator", /* tp_name */
|
---|
| 1349 | sizeof(dequeiterobject), /* tp_basicsize */
|
---|
| 1350 | 0, /* tp_itemsize */
|
---|
| 1351 | /* methods */
|
---|
| 1352 | (destructor)dequeiter_dealloc, /* tp_dealloc */
|
---|
| 1353 | 0, /* tp_print */
|
---|
| 1354 | 0, /* tp_getattr */
|
---|
| 1355 | 0, /* tp_setattr */
|
---|
| 1356 | 0, /* tp_compare */
|
---|
| 1357 | 0, /* tp_repr */
|
---|
| 1358 | 0, /* tp_as_number */
|
---|
| 1359 | 0, /* tp_as_sequence */
|
---|
| 1360 | 0, /* tp_as_mapping */
|
---|
| 1361 | 0, /* tp_hash */
|
---|
| 1362 | 0, /* tp_call */
|
---|
| 1363 | 0, /* tp_str */
|
---|
| 1364 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
| 1365 | 0, /* tp_setattro */
|
---|
| 1366 | 0, /* tp_as_buffer */
|
---|
| 1367 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
---|
| 1368 | 0, /* tp_doc */
|
---|
| 1369 | (traverseproc)dequeiter_traverse, /* tp_traverse */
|
---|
| 1370 | 0, /* tp_clear */
|
---|
| 1371 | 0, /* tp_richcompare */
|
---|
| 1372 | 0, /* tp_weaklistoffset */
|
---|
| 1373 | PyObject_SelfIter, /* tp_iter */
|
---|
| 1374 | (iternextfunc)dequereviter_next, /* tp_iternext */
|
---|
| 1375 | dequeiter_methods, /* tp_methods */
|
---|
| 1376 | 0,
|
---|
[2] | 1377 | };
|
---|
| 1378 |
|
---|
| 1379 | /* defaultdict type *********************************************************/
|
---|
| 1380 |
|
---|
| 1381 | typedef struct {
|
---|
[391] | 1382 | PyDictObject dict;
|
---|
| 1383 | PyObject *default_factory;
|
---|
[2] | 1384 | } defdictobject;
|
---|
| 1385 |
|
---|
| 1386 | static PyTypeObject defdict_type; /* Forward */
|
---|
| 1387 |
|
---|
| 1388 | PyDoc_STRVAR(defdict_missing_doc,
|
---|
| 1389 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
|
---|
| 1390 | if self.default_factory is None: raise KeyError((key,))\n\
|
---|
| 1391 | self[key] = value = self.default_factory()\n\
|
---|
| 1392 | return value\n\
|
---|
| 1393 | ");
|
---|
| 1394 |
|
---|
| 1395 | static PyObject *
|
---|
| 1396 | defdict_missing(defdictobject *dd, PyObject *key)
|
---|
| 1397 | {
|
---|
[391] | 1398 | PyObject *factory = dd->default_factory;
|
---|
| 1399 | PyObject *value;
|
---|
| 1400 | if (factory == NULL || factory == Py_None) {
|
---|
| 1401 | /* XXX Call dict.__missing__(key) */
|
---|
| 1402 | PyObject *tup;
|
---|
| 1403 | tup = PyTuple_Pack(1, key);
|
---|
| 1404 | if (!tup) return NULL;
|
---|
| 1405 | PyErr_SetObject(PyExc_KeyError, tup);
|
---|
| 1406 | Py_DECREF(tup);
|
---|
| 1407 | return NULL;
|
---|
| 1408 | }
|
---|
| 1409 | value = PyEval_CallObject(factory, NULL);
|
---|
| 1410 | if (value == NULL)
|
---|
| 1411 | return value;
|
---|
| 1412 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
|
---|
| 1413 | Py_DECREF(value);
|
---|
| 1414 | return NULL;
|
---|
| 1415 | }
|
---|
| 1416 | return value;
|
---|
[2] | 1417 | }
|
---|
| 1418 |
|
---|
| 1419 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
|
---|
| 1420 |
|
---|
| 1421 | static PyObject *
|
---|
| 1422 | defdict_copy(defdictobject *dd)
|
---|
| 1423 | {
|
---|
[391] | 1424 | /* This calls the object's class. That only works for subclasses
|
---|
| 1425 | whose class constructor has the same signature. Subclasses that
|
---|
| 1426 | define a different constructor signature must override copy().
|
---|
| 1427 | */
|
---|
[2] | 1428 |
|
---|
[391] | 1429 | if (dd->default_factory == NULL)
|
---|
| 1430 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
|
---|
| 1431 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
|
---|
| 1432 | dd->default_factory, dd, NULL);
|
---|
[2] | 1433 | }
|
---|
| 1434 |
|
---|
| 1435 | static PyObject *
|
---|
| 1436 | defdict_reduce(defdictobject *dd)
|
---|
| 1437 | {
|
---|
[391] | 1438 | /* __reduce__ must return a 5-tuple as follows:
|
---|
[2] | 1439 |
|
---|
[391] | 1440 | - factory function
|
---|
| 1441 | - tuple of args for the factory function
|
---|
| 1442 | - additional state (here None)
|
---|
| 1443 | - sequence iterator (here None)
|
---|
| 1444 | - dictionary iterator (yielding successive (key, value) pairs
|
---|
[2] | 1445 |
|
---|
[391] | 1446 | This API is used by pickle.py and copy.py.
|
---|
[2] | 1447 |
|
---|
[391] | 1448 | For this to be useful with pickle.py, the default_factory
|
---|
| 1449 | must be picklable; e.g., None, a built-in, or a global
|
---|
| 1450 | function in a module or package.
|
---|
[2] | 1451 |
|
---|
[391] | 1452 | Both shallow and deep copying are supported, but for deep
|
---|
| 1453 | copying, the default_factory must be deep-copyable; e.g. None,
|
---|
| 1454 | or a built-in (functions are not copyable at this time).
|
---|
[2] | 1455 |
|
---|
[391] | 1456 | This only works for subclasses as long as their constructor
|
---|
| 1457 | signature is compatible; the first argument must be the
|
---|
| 1458 | optional default_factory, defaulting to None.
|
---|
| 1459 | */
|
---|
| 1460 | PyObject *args;
|
---|
| 1461 | PyObject *items;
|
---|
| 1462 | PyObject *result;
|
---|
| 1463 | if (dd->default_factory == NULL || dd->default_factory == Py_None)
|
---|
| 1464 | args = PyTuple_New(0);
|
---|
| 1465 | else
|
---|
| 1466 | args = PyTuple_Pack(1, dd->default_factory);
|
---|
| 1467 | if (args == NULL)
|
---|
| 1468 | return NULL;
|
---|
| 1469 | items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
|
---|
| 1470 | if (items == NULL) {
|
---|
| 1471 | Py_DECREF(args);
|
---|
| 1472 | return NULL;
|
---|
| 1473 | }
|
---|
| 1474 | result = PyTuple_Pack(5, Py_TYPE(dd), args,
|
---|
| 1475 | Py_None, Py_None, items);
|
---|
| 1476 | Py_DECREF(items);
|
---|
| 1477 | Py_DECREF(args);
|
---|
| 1478 | return result;
|
---|
[2] | 1479 | }
|
---|
| 1480 |
|
---|
| 1481 | static PyMethodDef defdict_methods[] = {
|
---|
[391] | 1482 | {"__missing__", (PyCFunction)defdict_missing, METH_O,
|
---|
| 1483 | defdict_missing_doc},
|
---|
| 1484 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
|
---|
| 1485 | defdict_copy_doc},
|
---|
| 1486 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
|
---|
| 1487 | defdict_copy_doc},
|
---|
| 1488 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
|
---|
| 1489 | reduce_doc},
|
---|
| 1490 | {NULL}
|
---|
[2] | 1491 | };
|
---|
| 1492 |
|
---|
| 1493 | static PyMemberDef defdict_members[] = {
|
---|
[391] | 1494 | {"default_factory", T_OBJECT,
|
---|
| 1495 | offsetof(defdictobject, default_factory), 0,
|
---|
| 1496 | PyDoc_STR("Factory for default value called by __missing__().")},
|
---|
| 1497 | {NULL}
|
---|
[2] | 1498 | };
|
---|
| 1499 |
|
---|
| 1500 | static void
|
---|
| 1501 | defdict_dealloc(defdictobject *dd)
|
---|
| 1502 | {
|
---|
[391] | 1503 | Py_CLEAR(dd->default_factory);
|
---|
| 1504 | PyDict_Type.tp_dealloc((PyObject *)dd);
|
---|
[2] | 1505 | }
|
---|
| 1506 |
|
---|
| 1507 | static int
|
---|
| 1508 | defdict_print(defdictobject *dd, FILE *fp, int flags)
|
---|
| 1509 | {
|
---|
[391] | 1510 | int sts;
|
---|
| 1511 | Py_BEGIN_ALLOW_THREADS
|
---|
| 1512 | fprintf(fp, "defaultdict(");
|
---|
| 1513 | Py_END_ALLOW_THREADS
|
---|
| 1514 | if (dd->default_factory == NULL) {
|
---|
| 1515 | Py_BEGIN_ALLOW_THREADS
|
---|
| 1516 | fprintf(fp, "None");
|
---|
| 1517 | Py_END_ALLOW_THREADS
|
---|
| 1518 | } else {
|
---|
| 1519 | PyObject_Print(dd->default_factory, fp, 0);
|
---|
| 1520 | }
|
---|
| 1521 | Py_BEGIN_ALLOW_THREADS
|
---|
| 1522 | fprintf(fp, ", ");
|
---|
| 1523 | Py_END_ALLOW_THREADS
|
---|
| 1524 | sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
|
---|
| 1525 | Py_BEGIN_ALLOW_THREADS
|
---|
| 1526 | fprintf(fp, ")");
|
---|
| 1527 | Py_END_ALLOW_THREADS
|
---|
| 1528 | return sts;
|
---|
[2] | 1529 | }
|
---|
| 1530 |
|
---|
| 1531 | static PyObject *
|
---|
| 1532 | defdict_repr(defdictobject *dd)
|
---|
| 1533 | {
|
---|
[391] | 1534 | PyObject *defrepr;
|
---|
| 1535 | PyObject *baserepr;
|
---|
| 1536 | PyObject *result;
|
---|
| 1537 | baserepr = PyDict_Type.tp_repr((PyObject *)dd);
|
---|
| 1538 | if (baserepr == NULL)
|
---|
| 1539 | return NULL;
|
---|
| 1540 | if (dd->default_factory == NULL)
|
---|
| 1541 | defrepr = PyString_FromString("None");
|
---|
| 1542 | else
|
---|
| 1543 | {
|
---|
| 1544 | int status = Py_ReprEnter(dd->default_factory);
|
---|
| 1545 | if (status != 0) {
|
---|
| 1546 | if (status < 0) {
|
---|
| 1547 | Py_DECREF(baserepr);
|
---|
| 1548 | return NULL;
|
---|
| 1549 | }
|
---|
| 1550 | defrepr = PyString_FromString("...");
|
---|
| 1551 | }
|
---|
| 1552 | else
|
---|
| 1553 | defrepr = PyObject_Repr(dd->default_factory);
|
---|
| 1554 | Py_ReprLeave(dd->default_factory);
|
---|
| 1555 | }
|
---|
| 1556 | if (defrepr == NULL) {
|
---|
| 1557 | Py_DECREF(baserepr);
|
---|
| 1558 | return NULL;
|
---|
| 1559 | }
|
---|
| 1560 | result = PyString_FromFormat("defaultdict(%s, %s)",
|
---|
| 1561 | PyString_AS_STRING(defrepr),
|
---|
| 1562 | PyString_AS_STRING(baserepr));
|
---|
| 1563 | Py_DECREF(defrepr);
|
---|
| 1564 | Py_DECREF(baserepr);
|
---|
| 1565 | return result;
|
---|
[2] | 1566 | }
|
---|
| 1567 |
|
---|
| 1568 | static int
|
---|
| 1569 | defdict_traverse(PyObject *self, visitproc visit, void *arg)
|
---|
| 1570 | {
|
---|
[391] | 1571 | Py_VISIT(((defdictobject *)self)->default_factory);
|
---|
| 1572 | return PyDict_Type.tp_traverse(self, visit, arg);
|
---|
[2] | 1573 | }
|
---|
| 1574 |
|
---|
| 1575 | static int
|
---|
| 1576 | defdict_tp_clear(defdictobject *dd)
|
---|
| 1577 | {
|
---|
[391] | 1578 | Py_CLEAR(dd->default_factory);
|
---|
| 1579 | return PyDict_Type.tp_clear((PyObject *)dd);
|
---|
[2] | 1580 | }
|
---|
| 1581 |
|
---|
| 1582 | static int
|
---|
| 1583 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
|
---|
| 1584 | {
|
---|
[391] | 1585 | defdictobject *dd = (defdictobject *)self;
|
---|
| 1586 | PyObject *olddefault = dd->default_factory;
|
---|
| 1587 | PyObject *newdefault = NULL;
|
---|
| 1588 | PyObject *newargs;
|
---|
| 1589 | int result;
|
---|
| 1590 | if (args == NULL || !PyTuple_Check(args))
|
---|
| 1591 | newargs = PyTuple_New(0);
|
---|
| 1592 | else {
|
---|
| 1593 | Py_ssize_t n = PyTuple_GET_SIZE(args);
|
---|
| 1594 | if (n > 0) {
|
---|
| 1595 | newdefault = PyTuple_GET_ITEM(args, 0);
|
---|
| 1596 | if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
|
---|
| 1597 | PyErr_SetString(PyExc_TypeError,
|
---|
| 1598 | "first argument must be callable");
|
---|
| 1599 | return -1;
|
---|
| 1600 | }
|
---|
| 1601 | }
|
---|
| 1602 | newargs = PySequence_GetSlice(args, 1, n);
|
---|
| 1603 | }
|
---|
| 1604 | if (newargs == NULL)
|
---|
| 1605 | return -1;
|
---|
| 1606 | Py_XINCREF(newdefault);
|
---|
| 1607 | dd->default_factory = newdefault;
|
---|
| 1608 | result = PyDict_Type.tp_init(self, newargs, kwds);
|
---|
| 1609 | Py_DECREF(newargs);
|
---|
| 1610 | Py_XDECREF(olddefault);
|
---|
| 1611 | return result;
|
---|
[2] | 1612 | }
|
---|
| 1613 |
|
---|
| 1614 | PyDoc_STRVAR(defdict_doc,
|
---|
| 1615 | "defaultdict(default_factory) --> dict with default factory\n\
|
---|
| 1616 | \n\
|
---|
| 1617 | The default factory is called without arguments to produce\n\
|
---|
| 1618 | a new value when a key is not present, in __getitem__ only.\n\
|
---|
| 1619 | A defaultdict compares equal to a dict with the same items.\n\
|
---|
| 1620 | ");
|
---|
| 1621 |
|
---|
| 1622 | /* See comment in xxsubtype.c */
|
---|
| 1623 | #define DEFERRED_ADDRESS(ADDR) 0
|
---|
| 1624 |
|
---|
| 1625 | static PyTypeObject defdict_type = {
|
---|
[391] | 1626 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
---|
| 1627 | "collections.defaultdict", /* tp_name */
|
---|
| 1628 | sizeof(defdictobject), /* tp_basicsize */
|
---|
| 1629 | 0, /* tp_itemsize */
|
---|
| 1630 | /* methods */
|
---|
| 1631 | (destructor)defdict_dealloc, /* tp_dealloc */
|
---|
| 1632 | (printfunc)defdict_print, /* tp_print */
|
---|
| 1633 | 0, /* tp_getattr */
|
---|
| 1634 | 0, /* tp_setattr */
|
---|
| 1635 | 0, /* tp_compare */
|
---|
| 1636 | (reprfunc)defdict_repr, /* tp_repr */
|
---|
| 1637 | 0, /* tp_as_number */
|
---|
| 1638 | 0, /* tp_as_sequence */
|
---|
| 1639 | 0, /* tp_as_mapping */
|
---|
| 1640 | 0, /* tp_hash */
|
---|
| 1641 | 0, /* tp_call */
|
---|
| 1642 | 0, /* tp_str */
|
---|
| 1643 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
| 1644 | 0, /* tp_setattro */
|
---|
| 1645 | 0, /* tp_as_buffer */
|
---|
| 1646 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
|
---|
| 1647 | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
|
---|
| 1648 | defdict_doc, /* tp_doc */
|
---|
| 1649 | defdict_traverse, /* tp_traverse */
|
---|
| 1650 | (inquiry)defdict_tp_clear, /* tp_clear */
|
---|
| 1651 | 0, /* tp_richcompare */
|
---|
| 1652 | 0, /* tp_weaklistoffset*/
|
---|
| 1653 | 0, /* tp_iter */
|
---|
| 1654 | 0, /* tp_iternext */
|
---|
| 1655 | defdict_methods, /* tp_methods */
|
---|
| 1656 | defdict_members, /* tp_members */
|
---|
| 1657 | 0, /* tp_getset */
|
---|
| 1658 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
|
---|
| 1659 | 0, /* tp_dict */
|
---|
| 1660 | 0, /* tp_descr_get */
|
---|
| 1661 | 0, /* tp_descr_set */
|
---|
| 1662 | 0, /* tp_dictoffset */
|
---|
| 1663 | defdict_init, /* tp_init */
|
---|
| 1664 | PyType_GenericAlloc, /* tp_alloc */
|
---|
| 1665 | 0, /* tp_new */
|
---|
| 1666 | PyObject_GC_Del, /* tp_free */
|
---|
[2] | 1667 | };
|
---|
| 1668 |
|
---|
| 1669 | /* module level code ********************************************************/
|
---|
| 1670 |
|
---|
| 1671 | PyDoc_STRVAR(module_doc,
|
---|
| 1672 | "High performance data structures.\n\
|
---|
| 1673 | - deque: ordered collection accessible from endpoints only\n\
|
---|
| 1674 | - defaultdict: dict subclass with a default value factory\n\
|
---|
| 1675 | ");
|
---|
| 1676 |
|
---|
| 1677 | PyMODINIT_FUNC
|
---|
| 1678 | init_collections(void)
|
---|
| 1679 | {
|
---|
[391] | 1680 | PyObject *m;
|
---|
[2] | 1681 |
|
---|
[391] | 1682 | m = Py_InitModule3("_collections", NULL, module_doc);
|
---|
| 1683 | if (m == NULL)
|
---|
| 1684 | return;
|
---|
[2] | 1685 |
|
---|
[391] | 1686 | if (PyType_Ready(&deque_type) < 0)
|
---|
| 1687 | return;
|
---|
| 1688 | Py_INCREF(&deque_type);
|
---|
| 1689 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
|
---|
[2] | 1690 |
|
---|
[391] | 1691 | defdict_type.tp_base = &PyDict_Type;
|
---|
| 1692 | if (PyType_Ready(&defdict_type) < 0)
|
---|
| 1693 | return;
|
---|
| 1694 | Py_INCREF(&defdict_type);
|
---|
| 1695 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
|
---|
[2] | 1696 |
|
---|
[391] | 1697 | if (PyType_Ready(&dequeiter_type) < 0)
|
---|
| 1698 | return;
|
---|
[2] | 1699 |
|
---|
[391] | 1700 | if (PyType_Ready(&dequereviter_type) < 0)
|
---|
| 1701 | return;
|
---|
[2] | 1702 |
|
---|
[391] | 1703 | return;
|
---|
[2] | 1704 | }
|
---|