| 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 <[email protected]>
|
|---|
| 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
|
|---|
| 11 | * reduce the number of calls to the memory allocator but take more
|
|---|
| 12 | * memory. Ideally, BLOCKLEN should be set with an eye to the
|
|---|
| 13 | * length of a cache line.
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | #define BLOCKLEN 62
|
|---|
| 17 | #define CENTER ((BLOCKLEN - 1) / 2)
|
|---|
| 18 |
|
|---|
| 19 | /* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
|
|---|
| 20 | * This list is not circular (the leftmost block has leftlink==NULL,
|
|---|
| 21 | * and the rightmost block has rightlink==NULL). A deque d's first
|
|---|
| 22 | * element is at d.leftblock[leftindex] and its last element is at
|
|---|
| 23 | * d.rightblock[rightindex]; note that, unlike as for Python slice
|
|---|
| 24 | * indices, these indices are inclusive on both ends. By being inclusive
|
|---|
| 25 | * on both ends, algorithms for left and right operations become
|
|---|
| 26 | * symmetrical which simplifies the design.
|
|---|
| 27 | *
|
|---|
| 28 | * The list of blocks is never empty, so d.leftblock and d.rightblock
|
|---|
| 29 | * are never equal to NULL.
|
|---|
| 30 | *
|
|---|
| 31 | * The indices, d.leftindex and d.rightindex are always in the range
|
|---|
| 32 | * 0 <= index < BLOCKLEN.
|
|---|
| 33 | * Their exact relationship is:
|
|---|
| 34 | * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
|
|---|
| 35 | *
|
|---|
| 36 | * Empty deques have d.len == 0; d.leftblock==d.rightblock;
|
|---|
| 37 | * d.leftindex == CENTER+1; and d.rightindex == CENTER.
|
|---|
| 38 | * Checking for d.len == 0 is the intended way to see whether d is empty.
|
|---|
| 39 | *
|
|---|
| 40 | * Whenever d.leftblock == d.rightblock,
|
|---|
| 41 | * d.leftindex + d.len - 1 == d.rightindex.
|
|---|
| 42 | *
|
|---|
| 43 | * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
|
|---|
| 44 | * become indices into distinct blocks and either may be larger than the
|
|---|
| 45 | * other.
|
|---|
| 46 | */
|
|---|
| 47 |
|
|---|
| 48 | typedef struct BLOCK {
|
|---|
| 49 | struct BLOCK *leftlink;
|
|---|
| 50 | struct BLOCK *rightlink;
|
|---|
| 51 | PyObject *data[BLOCKLEN];
|
|---|
| 52 | } block;
|
|---|
| 53 |
|
|---|
| 54 | static block *
|
|---|
| 55 | newblock(block *leftlink, block *rightlink, int len) {
|
|---|
| 56 | block *b;
|
|---|
| 57 | /* To prevent len from overflowing INT_MAX on 64-bit machines, we
|
|---|
| 58 | * refuse to allocate new blocks if the current len is dangerously
|
|---|
| 59 | * close. There is some extra margin to prevent spurious arithmetic
|
|---|
| 60 | * overflows at various places. The following check ensures that
|
|---|
| 61 | * the blocks allocated to the deque, in the worst case, can only
|
|---|
| 62 | * have INT_MAX-2 entries in total.
|
|---|
| 63 | */
|
|---|
| 64 | if (len >= INT_MAX - 2*BLOCKLEN) {
|
|---|
| 65 | PyErr_SetString(PyExc_OverflowError,
|
|---|
| 66 | "cannot add more blocks to the deque");
|
|---|
| 67 | return NULL;
|
|---|
| 68 | }
|
|---|
| 69 | b = PyMem_Malloc(sizeof(block));
|
|---|
| 70 | if (b == NULL) {
|
|---|
| 71 | PyErr_NoMemory();
|
|---|
| 72 | return NULL;
|
|---|
| 73 | }
|
|---|
| 74 | b->leftlink = leftlink;
|
|---|
| 75 | b->rightlink = rightlink;
|
|---|
| 76 | return b;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | typedef struct {
|
|---|
| 80 | PyObject_HEAD
|
|---|
| 81 | block *leftblock;
|
|---|
| 82 | block *rightblock;
|
|---|
| 83 | int leftindex; /* in range(BLOCKLEN) */
|
|---|
| 84 | int rightindex; /* in range(BLOCKLEN) */
|
|---|
| 85 | int len;
|
|---|
| 86 | long state; /* incremented whenever the indices move */
|
|---|
| 87 | PyObject *weakreflist; /* List of weak references */
|
|---|
| 88 | } dequeobject;
|
|---|
| 89 |
|
|---|
| 90 | static PyTypeObject deque_type;
|
|---|
| 91 |
|
|---|
| 92 | static PyObject *
|
|---|
| 93 | deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|---|
| 94 | {
|
|---|
| 95 | dequeobject *deque;
|
|---|
| 96 | block *b;
|
|---|
| 97 |
|
|---|
| 98 | if (!_PyArg_NoKeywords("deque()", kwds))
|
|---|
| 99 | return NULL;
|
|---|
| 100 |
|
|---|
| 101 | /* create dequeobject structure */
|
|---|
| 102 | deque = (dequeobject *)type->tp_alloc(type, 0);
|
|---|
| 103 | if (deque == NULL)
|
|---|
| 104 | return NULL;
|
|---|
| 105 |
|
|---|
| 106 | b = newblock(NULL, NULL, 0);
|
|---|
| 107 | if (b == NULL) {
|
|---|
| 108 | Py_DECREF(deque);
|
|---|
| 109 | return NULL;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | assert(BLOCKLEN >= 2);
|
|---|
| 113 | deque->leftblock = b;
|
|---|
| 114 | deque->rightblock = b;
|
|---|
| 115 | deque->leftindex = CENTER + 1;
|
|---|
| 116 | deque->rightindex = CENTER;
|
|---|
| 117 | deque->len = 0;
|
|---|
| 118 | deque->state = 0;
|
|---|
| 119 | deque->weakreflist = NULL;
|
|---|
| 120 |
|
|---|
| 121 | return (PyObject *)deque;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | static PyObject *
|
|---|
| 125 | deque_append(dequeobject *deque, PyObject *item)
|
|---|
| 126 | {
|
|---|
| 127 | deque->state++;
|
|---|
| 128 | if (deque->rightindex == BLOCKLEN-1) {
|
|---|
| 129 | block *b = newblock(deque->rightblock, NULL, deque->len);
|
|---|
| 130 | if (b == NULL)
|
|---|
| 131 | return NULL;
|
|---|
| 132 | assert(deque->rightblock->rightlink == NULL);
|
|---|
| 133 | deque->rightblock->rightlink = b;
|
|---|
| 134 | deque->rightblock = b;
|
|---|
| 135 | deque->rightindex = -1;
|
|---|
| 136 | }
|
|---|
| 137 | Py_INCREF(item);
|
|---|
| 138 | deque->len++;
|
|---|
| 139 | deque->rightindex++;
|
|---|
| 140 | deque->rightblock->data[deque->rightindex] = item;
|
|---|
| 141 | Py_RETURN_NONE;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
|
|---|
| 145 |
|
|---|
| 146 | static PyObject *
|
|---|
| 147 | deque_appendleft(dequeobject *deque, PyObject *item)
|
|---|
| 148 | {
|
|---|
| 149 | deque->state++;
|
|---|
| 150 | if (deque->leftindex == 0) {
|
|---|
| 151 | block *b = newblock(NULL, deque->leftblock, deque->len);
|
|---|
| 152 | if (b == NULL)
|
|---|
| 153 | return NULL;
|
|---|
| 154 | assert(deque->leftblock->leftlink == NULL);
|
|---|
| 155 | deque->leftblock->leftlink = b;
|
|---|
| 156 | deque->leftblock = b;
|
|---|
| 157 | deque->leftindex = BLOCKLEN;
|
|---|
| 158 | }
|
|---|
| 159 | Py_INCREF(item);
|
|---|
| 160 | deque->len++;
|
|---|
| 161 | deque->leftindex--;
|
|---|
| 162 | deque->leftblock->data[deque->leftindex] = item;
|
|---|
| 163 | Py_RETURN_NONE;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
|
|---|
| 167 |
|
|---|
| 168 | static PyObject *
|
|---|
| 169 | deque_pop(dequeobject *deque, PyObject *unused)
|
|---|
| 170 | {
|
|---|
| 171 | PyObject *item;
|
|---|
| 172 | block *prevblock;
|
|---|
| 173 |
|
|---|
| 174 | if (deque->len == 0) {
|
|---|
| 175 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
|
|---|
| 176 | return NULL;
|
|---|
| 177 | }
|
|---|
| 178 | item = deque->rightblock->data[deque->rightindex];
|
|---|
| 179 | deque->rightindex--;
|
|---|
| 180 | deque->len--;
|
|---|
| 181 | deque->state++;
|
|---|
| 182 |
|
|---|
| 183 | if (deque->rightindex == -1) {
|
|---|
| 184 | if (deque->len == 0) {
|
|---|
| 185 | assert(deque->leftblock == deque->rightblock);
|
|---|
| 186 | assert(deque->leftindex == deque->rightindex+1);
|
|---|
| 187 | /* re-center instead of freeing a block */
|
|---|
| 188 | deque->leftindex = CENTER + 1;
|
|---|
| 189 | deque->rightindex = CENTER;
|
|---|
| 190 | } else {
|
|---|
| 191 | prevblock = deque->rightblock->leftlink;
|
|---|
| 192 | assert(deque->leftblock != deque->rightblock);
|
|---|
| 193 | PyMem_Free(deque->rightblock);
|
|---|
| 194 | prevblock->rightlink = NULL;
|
|---|
| 195 | deque->rightblock = prevblock;
|
|---|
| 196 | deque->rightindex = BLOCKLEN - 1;
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | return item;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
|
|---|
| 203 |
|
|---|
| 204 | static PyObject *
|
|---|
| 205 | deque_popleft(dequeobject *deque, PyObject *unused)
|
|---|
| 206 | {
|
|---|
| 207 | PyObject *item;
|
|---|
| 208 | block *prevblock;
|
|---|
| 209 |
|
|---|
| 210 | if (deque->len == 0) {
|
|---|
| 211 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
|
|---|
| 212 | return NULL;
|
|---|
| 213 | }
|
|---|
| 214 | assert(deque->leftblock != NULL);
|
|---|
| 215 | item = deque->leftblock->data[deque->leftindex];
|
|---|
| 216 | deque->leftindex++;
|
|---|
| 217 | deque->len--;
|
|---|
| 218 | deque->state++;
|
|---|
| 219 |
|
|---|
| 220 | if (deque->leftindex == BLOCKLEN) {
|
|---|
| 221 | if (deque->len == 0) {
|
|---|
| 222 | assert(deque->leftblock == deque->rightblock);
|
|---|
| 223 | assert(deque->leftindex == deque->rightindex+1);
|
|---|
| 224 | /* re-center instead of freeing a block */
|
|---|
| 225 | deque->leftindex = CENTER + 1;
|
|---|
| 226 | deque->rightindex = CENTER;
|
|---|
| 227 | } else {
|
|---|
| 228 | assert(deque->leftblock != deque->rightblock);
|
|---|
| 229 | prevblock = deque->leftblock->rightlink;
|
|---|
| 230 | PyMem_Free(deque->leftblock);
|
|---|
| 231 | assert(prevblock != NULL);
|
|---|
| 232 | prevblock->leftlink = NULL;
|
|---|
| 233 | deque->leftblock = prevblock;
|
|---|
| 234 | deque->leftindex = 0;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 | return item;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
|
|---|
| 241 |
|
|---|
| 242 | static PyObject *
|
|---|
| 243 | deque_extend(dequeobject *deque, PyObject *iterable)
|
|---|
| 244 | {
|
|---|
| 245 | PyObject *it, *item;
|
|---|
| 246 |
|
|---|
| 247 | it = PyObject_GetIter(iterable);
|
|---|
| 248 | if (it == NULL)
|
|---|
| 249 | return NULL;
|
|---|
| 250 |
|
|---|
| 251 | while ((item = PyIter_Next(it)) != NULL) {
|
|---|
| 252 | deque->state++;
|
|---|
| 253 | if (deque->rightindex == BLOCKLEN-1) {
|
|---|
| 254 | block *b = newblock(deque->rightblock, NULL,
|
|---|
| 255 | deque->len);
|
|---|
| 256 | if (b == NULL) {
|
|---|
| 257 | Py_DECREF(item);
|
|---|
| 258 | Py_DECREF(it);
|
|---|
| 259 | return NULL;
|
|---|
| 260 | }
|
|---|
| 261 | assert(deque->rightblock->rightlink == NULL);
|
|---|
| 262 | deque->rightblock->rightlink = b;
|
|---|
| 263 | deque->rightblock = b;
|
|---|
| 264 | deque->rightindex = -1;
|
|---|
| 265 | }
|
|---|
| 266 | deque->len++;
|
|---|
| 267 | deque->rightindex++;
|
|---|
| 268 | deque->rightblock->data[deque->rightindex] = item;
|
|---|
| 269 | }
|
|---|
| 270 | Py_DECREF(it);
|
|---|
| 271 | if (PyErr_Occurred())
|
|---|
| 272 | return NULL;
|
|---|
| 273 | Py_RETURN_NONE;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | PyDoc_STRVAR(extend_doc,
|
|---|
| 277 | "Extend the right side of the deque with elements from the iterable");
|
|---|
| 278 |
|
|---|
| 279 | static PyObject *
|
|---|
| 280 | deque_extendleft(dequeobject *deque, PyObject *iterable)
|
|---|
| 281 | {
|
|---|
| 282 | PyObject *it, *item;
|
|---|
| 283 |
|
|---|
| 284 | it = PyObject_GetIter(iterable);
|
|---|
| 285 | if (it == NULL)
|
|---|
| 286 | return NULL;
|
|---|
| 287 |
|
|---|
| 288 | while ((item = PyIter_Next(it)) != NULL) {
|
|---|
| 289 | deque->state++;
|
|---|
| 290 | if (deque->leftindex == 0) {
|
|---|
| 291 | block *b = newblock(NULL, deque->leftblock,
|
|---|
| 292 | deque->len);
|
|---|
| 293 | if (b == NULL) {
|
|---|
| 294 | Py_DECREF(item);
|
|---|
| 295 | Py_DECREF(it);
|
|---|
| 296 | return NULL;
|
|---|
| 297 | }
|
|---|
| 298 | assert(deque->leftblock->leftlink == NULL);
|
|---|
| 299 | deque->leftblock->leftlink = b;
|
|---|
| 300 | deque->leftblock = b;
|
|---|
| 301 | deque->leftindex = BLOCKLEN;
|
|---|
| 302 | }
|
|---|
| 303 | deque->len++;
|
|---|
| 304 | deque->leftindex--;
|
|---|
| 305 | deque->leftblock->data[deque->leftindex] = item;
|
|---|
| 306 | }
|
|---|
| 307 | Py_DECREF(it);
|
|---|
| 308 | if (PyErr_Occurred())
|
|---|
| 309 | return NULL;
|
|---|
| 310 | Py_RETURN_NONE;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | PyDoc_STRVAR(extendleft_doc,
|
|---|
| 314 | "Extend the left side of the deque with elements from the iterable");
|
|---|
| 315 |
|
|---|
| 316 | static int
|
|---|
| 317 | _deque_rotate(dequeobject *deque, Py_ssize_t n)
|
|---|
| 318 | {
|
|---|
| 319 | int i, len=deque->len, halflen=(len+1)>>1;
|
|---|
| 320 | PyObject *item, *rv;
|
|---|
| 321 |
|
|---|
| 322 | if (len == 0)
|
|---|
| 323 | return 0;
|
|---|
| 324 | if (n > halflen || n < -halflen) {
|
|---|
| 325 | n %= len;
|
|---|
| 326 | if (n > halflen)
|
|---|
| 327 | n -= len;
|
|---|
| 328 | else if (n < -halflen)
|
|---|
| 329 | n += len;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | for (i=0 ; i<n ; i++) {
|
|---|
| 333 | item = deque_pop(deque, NULL);
|
|---|
| 334 | assert (item != NULL);
|
|---|
| 335 | rv = deque_appendleft(deque, item);
|
|---|
| 336 | Py_DECREF(item);
|
|---|
| 337 | if (rv == NULL)
|
|---|
| 338 | return -1;
|
|---|
| 339 | Py_DECREF(rv);
|
|---|
| 340 | }
|
|---|
| 341 | for (i=0 ; i>n ; i--) {
|
|---|
| 342 | item = deque_popleft(deque, NULL);
|
|---|
| 343 | assert (item != NULL);
|
|---|
| 344 | rv = deque_append(deque, item);
|
|---|
| 345 | Py_DECREF(item);
|
|---|
| 346 | if (rv == NULL)
|
|---|
| 347 | return -1;
|
|---|
| 348 | Py_DECREF(rv);
|
|---|
| 349 | }
|
|---|
| 350 | return 0;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | static PyObject *
|
|---|
| 354 | deque_rotate(dequeobject *deque, PyObject *args)
|
|---|
| 355 | {
|
|---|
| 356 | int n=1;
|
|---|
| 357 |
|
|---|
| 358 | if (!PyArg_ParseTuple(args, "|i:rotate", &n))
|
|---|
| 359 | return NULL;
|
|---|
| 360 | if (_deque_rotate(deque, n) == 0)
|
|---|
| 361 | Py_RETURN_NONE;
|
|---|
| 362 | return NULL;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | PyDoc_STRVAR(rotate_doc,
|
|---|
| 366 | "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
|
|---|
| 367 |
|
|---|
| 368 | static Py_ssize_t
|
|---|
| 369 | deque_len(dequeobject *deque)
|
|---|
| 370 | {
|
|---|
| 371 | return deque->len;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | static PyObject *
|
|---|
| 375 | deque_remove(dequeobject *deque, PyObject *value)
|
|---|
| 376 | {
|
|---|
| 377 | Py_ssize_t i, n=deque->len;
|
|---|
| 378 |
|
|---|
| 379 | for (i=0 ; i<n ; i++) {
|
|---|
| 380 | PyObject *item = deque->leftblock->data[deque->leftindex];
|
|---|
| 381 | int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
|
|---|
| 382 |
|
|---|
| 383 | if (deque->len != n) {
|
|---|
| 384 | PyErr_SetString(PyExc_IndexError,
|
|---|
| 385 | "deque mutated during remove().");
|
|---|
| 386 | return NULL;
|
|---|
| 387 | }
|
|---|
| 388 | if (cmp > 0) {
|
|---|
| 389 | PyObject *tgt = deque_popleft(deque, NULL);
|
|---|
| 390 | assert (tgt != NULL);
|
|---|
| 391 | Py_DECREF(tgt);
|
|---|
| 392 | if (_deque_rotate(deque, i) == -1)
|
|---|
| 393 | return NULL;
|
|---|
| 394 | Py_RETURN_NONE;
|
|---|
| 395 | }
|
|---|
| 396 | else if (cmp < 0) {
|
|---|
| 397 | _deque_rotate(deque, i);
|
|---|
| 398 | return NULL;
|
|---|
| 399 | }
|
|---|
| 400 | _deque_rotate(deque, -1);
|
|---|
| 401 | }
|
|---|
| 402 | PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
|
|---|
| 403 | return NULL;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | PyDoc_STRVAR(remove_doc,
|
|---|
| 407 | "D.remove(value) -- remove first occurrence of value.");
|
|---|
| 408 |
|
|---|
| 409 | static int
|
|---|
| 410 | deque_clear(dequeobject *deque)
|
|---|
| 411 | {
|
|---|
| 412 | PyObject *item;
|
|---|
| 413 |
|
|---|
| 414 | while (deque->len) {
|
|---|
| 415 | item = deque_pop(deque, NULL);
|
|---|
| 416 | assert (item != NULL);
|
|---|
| 417 | Py_DECREF(item);
|
|---|
| 418 | }
|
|---|
| 419 | assert(deque->leftblock == deque->rightblock &&
|
|---|
| 420 | deque->leftindex - 1 == deque->rightindex &&
|
|---|
| 421 | deque->len == 0);
|
|---|
| 422 | return 0;
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | static PyObject *
|
|---|
| 426 | deque_item(dequeobject *deque, int i)
|
|---|
| 427 | {
|
|---|
| 428 | block *b;
|
|---|
| 429 | PyObject *item;
|
|---|
| 430 | int n, index=i;
|
|---|
| 431 |
|
|---|
| 432 | if (i < 0 || i >= deque->len) {
|
|---|
| 433 | PyErr_SetString(PyExc_IndexError,
|
|---|
| 434 | "deque index out of range");
|
|---|
| 435 | return NULL;
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | if (i == 0) {
|
|---|
| 439 | i = deque->leftindex;
|
|---|
| 440 | b = deque->leftblock;
|
|---|
| 441 | } else if (i == deque->len - 1) {
|
|---|
| 442 | i = deque->rightindex;
|
|---|
| 443 | b = deque->rightblock;
|
|---|
| 444 | } else {
|
|---|
| 445 | i += deque->leftindex;
|
|---|
| 446 | n = i / BLOCKLEN;
|
|---|
| 447 | i %= BLOCKLEN;
|
|---|
| 448 | if (index < (deque->len >> 1)) {
|
|---|
| 449 | b = deque->leftblock;
|
|---|
| 450 | while (n--)
|
|---|
| 451 | b = b->rightlink;
|
|---|
| 452 | } else {
|
|---|
| 453 | n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
|
|---|
| 454 | b = deque->rightblock;
|
|---|
| 455 | while (n--)
|
|---|
| 456 | b = b->leftlink;
|
|---|
| 457 | }
|
|---|
| 458 | }
|
|---|
| 459 | item = b->data[i];
|
|---|
| 460 | Py_INCREF(item);
|
|---|
| 461 | return item;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /* delitem() implemented in terms of rotate for simplicity and reasonable
|
|---|
| 465 | performance near the end points. If for some reason this method becomes
|
|---|
| 466 | popular, it is not hard to re-implement this using direct data movement
|
|---|
| 467 | (similar to code in list slice assignment) and achieve a two or threefold
|
|---|
| 468 | performance boost.
|
|---|
| 469 | */
|
|---|
| 470 |
|
|---|
| 471 | static int
|
|---|
| 472 | deque_del_item(dequeobject *deque, Py_ssize_t i)
|
|---|
| 473 | {
|
|---|
| 474 | PyObject *item;
|
|---|
| 475 |
|
|---|
| 476 | assert (i >= 0 && i < deque->len);
|
|---|
| 477 | if (_deque_rotate(deque, -i) == -1)
|
|---|
| 478 | return -1;
|
|---|
| 479 |
|
|---|
| 480 | item = deque_popleft(deque, NULL);
|
|---|
| 481 | assert (item != NULL);
|
|---|
| 482 | Py_DECREF(item);
|
|---|
| 483 |
|
|---|
| 484 | return _deque_rotate(deque, i);
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | static int
|
|---|
| 488 | deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
|
|---|
| 489 | {
|
|---|
| 490 | PyObject *old_value;
|
|---|
| 491 | block *b;
|
|---|
| 492 | Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
|
|---|
| 493 |
|
|---|
| 494 | if (i < 0 || i >= len) {
|
|---|
| 495 | PyErr_SetString(PyExc_IndexError,
|
|---|
| 496 | "deque index out of range");
|
|---|
| 497 | return -1;
|
|---|
| 498 | }
|
|---|
| 499 | if (v == NULL)
|
|---|
| 500 | return deque_del_item(deque, i);
|
|---|
| 501 |
|
|---|
| 502 | i += deque->leftindex;
|
|---|
| 503 | n = i / BLOCKLEN;
|
|---|
| 504 | i %= BLOCKLEN;
|
|---|
| 505 | if (index <= halflen) {
|
|---|
| 506 | b = deque->leftblock;
|
|---|
| 507 | while (n--)
|
|---|
| 508 | b = b->rightlink;
|
|---|
| 509 | } else {
|
|---|
| 510 | n = (deque->leftindex + len - 1) / BLOCKLEN - n;
|
|---|
| 511 | b = deque->rightblock;
|
|---|
| 512 | while (n--)
|
|---|
| 513 | b = b->leftlink;
|
|---|
| 514 | }
|
|---|
| 515 | Py_INCREF(v);
|
|---|
| 516 | old_value = b->data[i];
|
|---|
| 517 | b->data[i] = v;
|
|---|
| 518 | Py_DECREF(old_value);
|
|---|
| 519 | return 0;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | static PyObject *
|
|---|
| 523 | deque_clearmethod(dequeobject *deque)
|
|---|
| 524 | {
|
|---|
| 525 | int rv;
|
|---|
| 526 |
|
|---|
| 527 | rv = deque_clear(deque);
|
|---|
| 528 | assert (rv != -1);
|
|---|
| 529 | Py_RETURN_NONE;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
|
|---|
| 533 |
|
|---|
| 534 | static void
|
|---|
| 535 | deque_dealloc(dequeobject *deque)
|
|---|
| 536 | {
|
|---|
| 537 | PyObject_GC_UnTrack(deque);
|
|---|
| 538 | if (deque->weakreflist != NULL)
|
|---|
| 539 | PyObject_ClearWeakRefs((PyObject *) deque);
|
|---|
| 540 | if (deque->leftblock != NULL) {
|
|---|
| 541 | deque_clear(deque);
|
|---|
| 542 | assert(deque->leftblock != NULL);
|
|---|
| 543 | PyMem_Free(deque->leftblock);
|
|---|
| 544 | }
|
|---|
| 545 | deque->leftblock = NULL;
|
|---|
| 546 | deque->rightblock = NULL;
|
|---|
| 547 | deque->ob_type->tp_free(deque);
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | static int
|
|---|
| 551 | deque_traverse(dequeobject *deque, visitproc visit, void *arg)
|
|---|
| 552 | {
|
|---|
| 553 | block *b;
|
|---|
| 554 | PyObject *item;
|
|---|
| 555 | int index;
|
|---|
| 556 | int indexlo = deque->leftindex;
|
|---|
| 557 |
|
|---|
| 558 | for (b = deque->leftblock; b != NULL; b = b->rightlink) {
|
|---|
| 559 | const int indexhi = b == deque->rightblock ?
|
|---|
| 560 | deque->rightindex :
|
|---|
| 561 | BLOCKLEN - 1;
|
|---|
| 562 |
|
|---|
| 563 | for (index = indexlo; index <= indexhi; ++index) {
|
|---|
| 564 | item = b->data[index];
|
|---|
| 565 | Py_VISIT(item);
|
|---|
| 566 | }
|
|---|
| 567 | indexlo = 0;
|
|---|
| 568 | }
|
|---|
| 569 | return 0;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | static long
|
|---|
| 573 | deque_nohash(PyObject *self)
|
|---|
| 574 | {
|
|---|
| 575 | PyErr_SetString(PyExc_TypeError, "deque objects are unhashable");
|
|---|
| 576 | return -1;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | static PyObject *
|
|---|
| 580 | deque_copy(PyObject *deque)
|
|---|
| 581 | {
|
|---|
| 582 | return PyObject_CallFunctionObjArgs((PyObject *)(deque->ob_type),
|
|---|
| 583 | deque, NULL);
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
|
|---|
| 587 |
|
|---|
| 588 | static PyObject *
|
|---|
| 589 | deque_reduce(dequeobject *deque)
|
|---|
| 590 | {
|
|---|
| 591 | PyObject *dict, *result, *it;
|
|---|
| 592 |
|
|---|
| 593 | dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
|
|---|
| 594 | if (dict == NULL) {
|
|---|
| 595 | PyErr_Clear();
|
|---|
| 596 | dict = Py_None;
|
|---|
| 597 | Py_INCREF(dict);
|
|---|
| 598 | }
|
|---|
| 599 | it = PyObject_GetIter((PyObject *)deque);
|
|---|
| 600 | if (it == NULL) {
|
|---|
| 601 | Py_DECREF(dict);
|
|---|
| 602 | return NULL;
|
|---|
| 603 | }
|
|---|
| 604 | result = Py_BuildValue("O()ON", deque->ob_type, dict, it);
|
|---|
| 605 | Py_DECREF(dict);
|
|---|
| 606 | return result;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
|
|---|
| 610 |
|
|---|
| 611 | static PyObject *
|
|---|
| 612 | deque_repr(PyObject *deque)
|
|---|
| 613 | {
|
|---|
| 614 | PyObject *aslist, *result, *fmt;
|
|---|
| 615 | int i;
|
|---|
| 616 |
|
|---|
| 617 | i = Py_ReprEnter(deque);
|
|---|
| 618 | if (i != 0) {
|
|---|
| 619 | if (i < 0)
|
|---|
| 620 | return NULL;
|
|---|
| 621 | return PyString_FromString("[...]");
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | aslist = PySequence_List(deque);
|
|---|
| 625 | if (aslist == NULL) {
|
|---|
| 626 | Py_ReprLeave(deque);
|
|---|
| 627 | return NULL;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | fmt = PyString_FromString("deque(%r)");
|
|---|
| 631 | if (fmt == NULL) {
|
|---|
| 632 | Py_DECREF(aslist);
|
|---|
| 633 | Py_ReprLeave(deque);
|
|---|
| 634 | return NULL;
|
|---|
| 635 | }
|
|---|
| 636 | result = PyString_Format(fmt, aslist);
|
|---|
| 637 | Py_DECREF(fmt);
|
|---|
| 638 | Py_DECREF(aslist);
|
|---|
| 639 | Py_ReprLeave(deque);
|
|---|
| 640 | return result;
|
|---|
| 641 | }
|
|---|
| 642 |
|
|---|
| 643 | static int
|
|---|
| 644 | deque_tp_print(PyObject *deque, FILE *fp, int flags)
|
|---|
| 645 | {
|
|---|
| 646 | PyObject *it, *item;
|
|---|
| 647 | char *emit = ""; /* No separator emitted on first pass */
|
|---|
| 648 | char *separator = ", ";
|
|---|
| 649 | int i;
|
|---|
| 650 |
|
|---|
| 651 | i = Py_ReprEnter(deque);
|
|---|
| 652 | if (i != 0) {
|
|---|
| 653 | if (i < 0)
|
|---|
| 654 | return i;
|
|---|
| 655 | fputs("[...]", fp);
|
|---|
| 656 | return 0;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | it = PyObject_GetIter(deque);
|
|---|
| 660 | if (it == NULL)
|
|---|
| 661 | return -1;
|
|---|
| 662 |
|
|---|
| 663 | fputs("deque([", fp);
|
|---|
| 664 | while ((item = PyIter_Next(it)) != NULL) {
|
|---|
| 665 | fputs(emit, fp);
|
|---|
| 666 | emit = separator;
|
|---|
| 667 | if (PyObject_Print(item, fp, 0) != 0) {
|
|---|
| 668 | Py_DECREF(item);
|
|---|
| 669 | Py_DECREF(it);
|
|---|
| 670 | Py_ReprLeave(deque);
|
|---|
| 671 | return -1;
|
|---|
| 672 | }
|
|---|
| 673 | Py_DECREF(item);
|
|---|
| 674 | }
|
|---|
| 675 | Py_ReprLeave(deque);
|
|---|
| 676 | Py_DECREF(it);
|
|---|
| 677 | if (PyErr_Occurred())
|
|---|
| 678 | return -1;
|
|---|
| 679 | fputs("])", fp);
|
|---|
| 680 | return 0;
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | static PyObject *
|
|---|
| 684 | deque_richcompare(PyObject *v, PyObject *w, int op)
|
|---|
| 685 | {
|
|---|
| 686 | PyObject *it1=NULL, *it2=NULL, *x, *y;
|
|---|
| 687 | int b, vs, ws, cmp=-1;
|
|---|
| 688 |
|
|---|
| 689 | if (!PyObject_TypeCheck(v, &deque_type) ||
|
|---|
| 690 | !PyObject_TypeCheck(w, &deque_type)) {
|
|---|
| 691 | Py_INCREF(Py_NotImplemented);
|
|---|
| 692 | return Py_NotImplemented;
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | /* Shortcuts */
|
|---|
| 696 | vs = ((dequeobject *)v)->len;
|
|---|
| 697 | ws = ((dequeobject *)w)->len;
|
|---|
| 698 | if (op == Py_EQ) {
|
|---|
| 699 | if (v == w)
|
|---|
| 700 | Py_RETURN_TRUE;
|
|---|
| 701 | if (vs != ws)
|
|---|
| 702 | Py_RETURN_FALSE;
|
|---|
| 703 | }
|
|---|
| 704 | if (op == Py_NE) {
|
|---|
| 705 | if (v == w)
|
|---|
| 706 | Py_RETURN_FALSE;
|
|---|
| 707 | if (vs != ws)
|
|---|
| 708 | Py_RETURN_TRUE;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | /* Search for the first index where items are different */
|
|---|
| 712 | it1 = PyObject_GetIter(v);
|
|---|
| 713 | if (it1 == NULL)
|
|---|
| 714 | goto done;
|
|---|
| 715 | it2 = PyObject_GetIter(w);
|
|---|
| 716 | if (it2 == NULL)
|
|---|
| 717 | goto done;
|
|---|
| 718 | for (;;) {
|
|---|
| 719 | x = PyIter_Next(it1);
|
|---|
| 720 | if (x == NULL && PyErr_Occurred())
|
|---|
| 721 | goto done;
|
|---|
| 722 | y = PyIter_Next(it2);
|
|---|
| 723 | if (x == NULL || y == NULL)
|
|---|
| 724 | break;
|
|---|
| 725 | b = PyObject_RichCompareBool(x, y, Py_EQ);
|
|---|
| 726 | if (b == 0) {
|
|---|
| 727 | cmp = PyObject_RichCompareBool(x, y, op);
|
|---|
| 728 | Py_DECREF(x);
|
|---|
| 729 | Py_DECREF(y);
|
|---|
| 730 | goto done;
|
|---|
| 731 | }
|
|---|
| 732 | Py_DECREF(x);
|
|---|
| 733 | Py_DECREF(y);
|
|---|
| 734 | if (b == -1)
|
|---|
| 735 | goto done;
|
|---|
| 736 | }
|
|---|
| 737 | /* We reached the end of one deque or both */
|
|---|
| 738 | Py_XDECREF(x);
|
|---|
| 739 | Py_XDECREF(y);
|
|---|
| 740 | if (PyErr_Occurred())
|
|---|
| 741 | goto done;
|
|---|
| 742 | switch (op) {
|
|---|
| 743 | case Py_LT: cmp = y != NULL; break; /* if w was longer */
|
|---|
| 744 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */
|
|---|
| 745 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
|
|---|
| 746 | case Py_NE: cmp = x != y; break; /* if one deque continues */
|
|---|
| 747 | case Py_GT: cmp = x != NULL; break; /* if v was longer */
|
|---|
| 748 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | done:
|
|---|
| 752 | Py_XDECREF(it1);
|
|---|
| 753 | Py_XDECREF(it2);
|
|---|
| 754 | if (cmp == 1)
|
|---|
| 755 | Py_RETURN_TRUE;
|
|---|
| 756 | if (cmp == 0)
|
|---|
| 757 | Py_RETURN_FALSE;
|
|---|
| 758 | return NULL;
|
|---|
| 759 | }
|
|---|
| 760 |
|
|---|
| 761 | static int
|
|---|
| 762 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwds)
|
|---|
| 763 | {
|
|---|
| 764 | PyObject *iterable = NULL;
|
|---|
| 765 |
|
|---|
| 766 | if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable))
|
|---|
| 767 | return -1;
|
|---|
| 768 |
|
|---|
| 769 | if (iterable != NULL) {
|
|---|
| 770 | PyObject *rv = deque_extend(deque, iterable);
|
|---|
| 771 | if (rv == NULL)
|
|---|
| 772 | return -1;
|
|---|
| 773 | Py_DECREF(rv);
|
|---|
| 774 | }
|
|---|
| 775 | return 0;
|
|---|
| 776 | }
|
|---|
| 777 |
|
|---|
| 778 | static PySequenceMethods deque_as_sequence = {
|
|---|
| 779 | (lenfunc)deque_len, /* sq_length */
|
|---|
| 780 | 0, /* sq_concat */
|
|---|
| 781 | 0, /* sq_repeat */
|
|---|
| 782 | (ssizeargfunc)deque_item, /* sq_item */
|
|---|
| 783 | 0, /* sq_slice */
|
|---|
| 784 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
|
|---|
| 785 | };
|
|---|
| 786 |
|
|---|
| 787 | /* deque object ********************************************************/
|
|---|
| 788 |
|
|---|
| 789 | static PyObject *deque_iter(dequeobject *deque);
|
|---|
| 790 | static PyObject *deque_reviter(dequeobject *deque);
|
|---|
| 791 | PyDoc_STRVAR(reversed_doc,
|
|---|
| 792 | "D.__reversed__() -- return a reverse iterator over the deque");
|
|---|
| 793 |
|
|---|
| 794 | static PyMethodDef deque_methods[] = {
|
|---|
| 795 | {"append", (PyCFunction)deque_append,
|
|---|
| 796 | METH_O, append_doc},
|
|---|
| 797 | {"appendleft", (PyCFunction)deque_appendleft,
|
|---|
| 798 | METH_O, appendleft_doc},
|
|---|
| 799 | {"clear", (PyCFunction)deque_clearmethod,
|
|---|
| 800 | METH_NOARGS, clear_doc},
|
|---|
| 801 | {"__copy__", (PyCFunction)deque_copy,
|
|---|
| 802 | METH_NOARGS, copy_doc},
|
|---|
| 803 | {"extend", (PyCFunction)deque_extend,
|
|---|
| 804 | METH_O, extend_doc},
|
|---|
| 805 | {"extendleft", (PyCFunction)deque_extendleft,
|
|---|
| 806 | METH_O, extendleft_doc},
|
|---|
| 807 | {"pop", (PyCFunction)deque_pop,
|
|---|
| 808 | METH_NOARGS, pop_doc},
|
|---|
| 809 | {"popleft", (PyCFunction)deque_popleft,
|
|---|
| 810 | METH_NOARGS, popleft_doc},
|
|---|
| 811 | {"__reduce__", (PyCFunction)deque_reduce,
|
|---|
| 812 | METH_NOARGS, reduce_doc},
|
|---|
| 813 | {"remove", (PyCFunction)deque_remove,
|
|---|
| 814 | METH_O, remove_doc},
|
|---|
| 815 | {"__reversed__", (PyCFunction)deque_reviter,
|
|---|
| 816 | METH_NOARGS, reversed_doc},
|
|---|
| 817 | {"rotate", (PyCFunction)deque_rotate,
|
|---|
| 818 | METH_VARARGS, rotate_doc},
|
|---|
| 819 | {NULL, NULL} /* sentinel */
|
|---|
| 820 | };
|
|---|
| 821 |
|
|---|
| 822 | PyDoc_STRVAR(deque_doc,
|
|---|
| 823 | "deque(iterable) --> deque object\n\
|
|---|
| 824 | \n\
|
|---|
| 825 | Build an ordered collection accessible from endpoints only.");
|
|---|
| 826 |
|
|---|
| 827 | static PyTypeObject deque_type = {
|
|---|
| 828 | PyObject_HEAD_INIT(NULL)
|
|---|
| 829 | 0, /* ob_size */
|
|---|
| 830 | "collections.deque", /* tp_name */
|
|---|
| 831 | sizeof(dequeobject), /* tp_basicsize */
|
|---|
| 832 | 0, /* tp_itemsize */
|
|---|
| 833 | /* methods */
|
|---|
| 834 | (destructor)deque_dealloc, /* tp_dealloc */
|
|---|
| 835 | deque_tp_print, /* tp_print */
|
|---|
| 836 | 0, /* tp_getattr */
|
|---|
| 837 | 0, /* tp_setattr */
|
|---|
| 838 | 0, /* tp_compare */
|
|---|
| 839 | deque_repr, /* tp_repr */
|
|---|
| 840 | 0, /* tp_as_number */
|
|---|
| 841 | &deque_as_sequence, /* tp_as_sequence */
|
|---|
| 842 | 0, /* tp_as_mapping */
|
|---|
| 843 | deque_nohash, /* tp_hash */
|
|---|
| 844 | 0, /* tp_call */
|
|---|
| 845 | 0, /* tp_str */
|
|---|
| 846 | PyObject_GenericGetAttr, /* tp_getattro */
|
|---|
| 847 | 0, /* tp_setattro */
|
|---|
| 848 | 0, /* tp_as_buffer */
|
|---|
| 849 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
|
|---|
| 850 | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
|
|---|
| 851 | deque_doc, /* tp_doc */
|
|---|
| 852 | (traverseproc)deque_traverse, /* tp_traverse */
|
|---|
| 853 | (inquiry)deque_clear, /* tp_clear */
|
|---|
| 854 | (richcmpfunc)deque_richcompare, /* tp_richcompare */
|
|---|
| 855 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
|
|---|
| 856 | (getiterfunc)deque_iter, /* tp_iter */
|
|---|
| 857 | 0, /* tp_iternext */
|
|---|
| 858 | deque_methods, /* tp_methods */
|
|---|
| 859 | 0, /* tp_members */
|
|---|
| 860 | 0, /* tp_getset */
|
|---|
| 861 | 0, /* tp_base */
|
|---|
| 862 | 0, /* tp_dict */
|
|---|
| 863 | 0, /* tp_descr_get */
|
|---|
| 864 | 0, /* tp_descr_set */
|
|---|
| 865 | 0, /* tp_dictoffset */
|
|---|
| 866 | (initproc)deque_init, /* tp_init */
|
|---|
| 867 | PyType_GenericAlloc, /* tp_alloc */
|
|---|
| 868 | deque_new, /* tp_new */
|
|---|
| 869 | PyObject_GC_Del, /* tp_free */
|
|---|
| 870 | };
|
|---|
| 871 |
|
|---|
| 872 | /*********************** Deque Iterator **************************/
|
|---|
| 873 |
|
|---|
| 874 | typedef struct {
|
|---|
| 875 | PyObject_HEAD
|
|---|
| 876 | int index;
|
|---|
| 877 | block *b;
|
|---|
| 878 | dequeobject *deque;
|
|---|
| 879 | long state; /* state when the iterator is created */
|
|---|
| 880 | int counter; /* number of items remaining for iteration */
|
|---|
| 881 | } dequeiterobject;
|
|---|
| 882 |
|
|---|
| 883 | PyTypeObject dequeiter_type;
|
|---|
| 884 |
|
|---|
| 885 | static PyObject *
|
|---|
| 886 | deque_iter(dequeobject *deque)
|
|---|
| 887 | {
|
|---|
| 888 | dequeiterobject *it;
|
|---|
| 889 |
|
|---|
| 890 | it = PyObject_New(dequeiterobject, &dequeiter_type);
|
|---|
| 891 | if (it == NULL)
|
|---|
| 892 | return NULL;
|
|---|
| 893 | it->b = deque->leftblock;
|
|---|
| 894 | it->index = deque->leftindex;
|
|---|
| 895 | Py_INCREF(deque);
|
|---|
| 896 | it->deque = deque;
|
|---|
| 897 | it->state = deque->state;
|
|---|
| 898 | it->counter = deque->len;
|
|---|
| 899 | return (PyObject *)it;
|
|---|
| 900 | }
|
|---|
| 901 |
|
|---|
| 902 | static void
|
|---|
| 903 | dequeiter_dealloc(dequeiterobject *dio)
|
|---|
| 904 | {
|
|---|
| 905 | Py_XDECREF(dio->deque);
|
|---|
| 906 | dio->ob_type->tp_free(dio);
|
|---|
| 907 | }
|
|---|
| 908 |
|
|---|
| 909 | static PyObject *
|
|---|
| 910 | dequeiter_next(dequeiterobject *it)
|
|---|
| 911 | {
|
|---|
| 912 | PyObject *item;
|
|---|
| 913 |
|
|---|
| 914 | if (it->counter == 0)
|
|---|
| 915 | return NULL;
|
|---|
| 916 |
|
|---|
| 917 | if (it->deque->state != it->state) {
|
|---|
| 918 | it->counter = 0;
|
|---|
| 919 | PyErr_SetString(PyExc_RuntimeError,
|
|---|
| 920 | "deque mutated during iteration");
|
|---|
| 921 | return NULL;
|
|---|
| 922 | }
|
|---|
| 923 | assert (!(it->b == it->deque->rightblock &&
|
|---|
| 924 | it->index > it->deque->rightindex));
|
|---|
| 925 |
|
|---|
| 926 | item = it->b->data[it->index];
|
|---|
| 927 | it->index++;
|
|---|
| 928 | it->counter--;
|
|---|
| 929 | if (it->index == BLOCKLEN && it->counter > 0) {
|
|---|
| 930 | assert (it->b->rightlink != NULL);
|
|---|
| 931 | it->b = it->b->rightlink;
|
|---|
| 932 | it->index = 0;
|
|---|
| 933 | }
|
|---|
| 934 | Py_INCREF(item);
|
|---|
| 935 | return item;
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | static PyObject *
|
|---|
| 939 | dequeiter_len(dequeiterobject *it)
|
|---|
| 940 | {
|
|---|
| 941 | return PyInt_FromLong(it->counter);
|
|---|
| 942 | }
|
|---|
| 943 |
|
|---|
| 944 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
|
|---|
| 945 |
|
|---|
| 946 | static PyMethodDef dequeiter_methods[] = {
|
|---|
| 947 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
|
|---|
| 948 | {NULL, NULL} /* sentinel */
|
|---|
| 949 | };
|
|---|
| 950 |
|
|---|
| 951 | PyTypeObject dequeiter_type = {
|
|---|
| 952 | PyObject_HEAD_INIT(NULL)
|
|---|
| 953 | 0, /* ob_size */
|
|---|
| 954 | "deque_iterator", /* tp_name */
|
|---|
| 955 | sizeof(dequeiterobject), /* tp_basicsize */
|
|---|
| 956 | 0, /* tp_itemsize */
|
|---|
| 957 | /* methods */
|
|---|
| 958 | (destructor)dequeiter_dealloc, /* tp_dealloc */
|
|---|
| 959 | 0, /* tp_print */
|
|---|
| 960 | 0, /* tp_getattr */
|
|---|
| 961 | 0, /* tp_setattr */
|
|---|
| 962 | 0, /* tp_compare */
|
|---|
| 963 | 0, /* tp_repr */
|
|---|
| 964 | 0, /* tp_as_number */
|
|---|
| 965 | 0, /* tp_as_sequence */
|
|---|
| 966 | 0, /* tp_as_mapping */
|
|---|
| 967 | 0, /* tp_hash */
|
|---|
| 968 | 0, /* tp_call */
|
|---|
| 969 | 0, /* tp_str */
|
|---|
| 970 | PyObject_GenericGetAttr, /* tp_getattro */
|
|---|
| 971 | 0, /* tp_setattro */
|
|---|
| 972 | 0, /* tp_as_buffer */
|
|---|
| 973 | Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|---|
| 974 | 0, /* tp_doc */
|
|---|
| 975 | 0, /* tp_traverse */
|
|---|
| 976 | 0, /* tp_clear */
|
|---|
| 977 | 0, /* tp_richcompare */
|
|---|
| 978 | 0, /* tp_weaklistoffset */
|
|---|
| 979 | PyObject_SelfIter, /* tp_iter */
|
|---|
| 980 | (iternextfunc)dequeiter_next, /* tp_iternext */
|
|---|
| 981 | dequeiter_methods, /* tp_methods */
|
|---|
| 982 | 0,
|
|---|
| 983 | };
|
|---|
| 984 |
|
|---|
| 985 | /*********************** Deque Reverse Iterator **************************/
|
|---|
| 986 |
|
|---|
| 987 | PyTypeObject dequereviter_type;
|
|---|
| 988 |
|
|---|
| 989 | static PyObject *
|
|---|
| 990 | deque_reviter(dequeobject *deque)
|
|---|
| 991 | {
|
|---|
| 992 | dequeiterobject *it;
|
|---|
| 993 |
|
|---|
| 994 | it = PyObject_New(dequeiterobject, &dequereviter_type);
|
|---|
| 995 | if (it == NULL)
|
|---|
| 996 | return NULL;
|
|---|
| 997 | it->b = deque->rightblock;
|
|---|
| 998 | it->index = deque->rightindex;
|
|---|
| 999 | Py_INCREF(deque);
|
|---|
| 1000 | it->deque = deque;
|
|---|
| 1001 | it->state = deque->state;
|
|---|
| 1002 | it->counter = deque->len;
|
|---|
| 1003 | return (PyObject *)it;
|
|---|
| 1004 | }
|
|---|
| 1005 |
|
|---|
| 1006 | static PyObject *
|
|---|
| 1007 | dequereviter_next(dequeiterobject *it)
|
|---|
| 1008 | {
|
|---|
| 1009 | PyObject *item;
|
|---|
| 1010 | if (it->counter == 0)
|
|---|
| 1011 | return NULL;
|
|---|
| 1012 |
|
|---|
| 1013 | if (it->deque->state != it->state) {
|
|---|
| 1014 | it->counter = 0;
|
|---|
| 1015 | PyErr_SetString(PyExc_RuntimeError,
|
|---|
| 1016 | "deque mutated during iteration");
|
|---|
| 1017 | return NULL;
|
|---|
| 1018 | }
|
|---|
| 1019 | assert (!(it->b == it->deque->leftblock &&
|
|---|
| 1020 | it->index < it->deque->leftindex));
|
|---|
| 1021 |
|
|---|
| 1022 | item = it->b->data[it->index];
|
|---|
| 1023 | it->index--;
|
|---|
| 1024 | it->counter--;
|
|---|
| 1025 | if (it->index == -1 && it->counter > 0) {
|
|---|
| 1026 | assert (it->b->leftlink != NULL);
|
|---|
| 1027 | it->b = it->b->leftlink;
|
|---|
| 1028 | it->index = BLOCKLEN - 1;
|
|---|
| 1029 | }
|
|---|
| 1030 | Py_INCREF(item);
|
|---|
| 1031 | return item;
|
|---|
| 1032 | }
|
|---|
| 1033 |
|
|---|
| 1034 | PyTypeObject dequereviter_type = {
|
|---|
| 1035 | PyObject_HEAD_INIT(NULL)
|
|---|
| 1036 | 0, /* ob_size */
|
|---|
| 1037 | "deque_reverse_iterator", /* tp_name */
|
|---|
| 1038 | sizeof(dequeiterobject), /* tp_basicsize */
|
|---|
| 1039 | 0, /* tp_itemsize */
|
|---|
| 1040 | /* methods */
|
|---|
| 1041 | (destructor)dequeiter_dealloc, /* tp_dealloc */
|
|---|
| 1042 | 0, /* tp_print */
|
|---|
| 1043 | 0, /* tp_getattr */
|
|---|
| 1044 | 0, /* tp_setattr */
|
|---|
| 1045 | 0, /* tp_compare */
|
|---|
| 1046 | 0, /* tp_repr */
|
|---|
| 1047 | 0, /* tp_as_number */
|
|---|
| 1048 | 0, /* tp_as_sequence */
|
|---|
| 1049 | 0, /* tp_as_mapping */
|
|---|
| 1050 | 0, /* tp_hash */
|
|---|
| 1051 | 0, /* tp_call */
|
|---|
| 1052 | 0, /* tp_str */
|
|---|
| 1053 | PyObject_GenericGetAttr, /* tp_getattro */
|
|---|
| 1054 | 0, /* tp_setattro */
|
|---|
| 1055 | 0, /* tp_as_buffer */
|
|---|
| 1056 | Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|---|
| 1057 | 0, /* tp_doc */
|
|---|
| 1058 | 0, /* tp_traverse */
|
|---|
| 1059 | 0, /* tp_clear */
|
|---|
| 1060 | 0, /* tp_richcompare */
|
|---|
| 1061 | 0, /* tp_weaklistoffset */
|
|---|
| 1062 | PyObject_SelfIter, /* tp_iter */
|
|---|
| 1063 | (iternextfunc)dequereviter_next, /* tp_iternext */
|
|---|
| 1064 | dequeiter_methods, /* tp_methods */
|
|---|
| 1065 | 0,
|
|---|
| 1066 | };
|
|---|
| 1067 |
|
|---|
| 1068 | /* defaultdict type *********************************************************/
|
|---|
| 1069 |
|
|---|
| 1070 | typedef struct {
|
|---|
| 1071 | PyDictObject dict;
|
|---|
| 1072 | PyObject *default_factory;
|
|---|
| 1073 | } defdictobject;
|
|---|
| 1074 |
|
|---|
| 1075 | static PyTypeObject defdict_type; /* Forward */
|
|---|
| 1076 |
|
|---|
| 1077 | PyDoc_STRVAR(defdict_missing_doc,
|
|---|
| 1078 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
|
|---|
| 1079 | if self.default_factory is None: raise KeyError(key)\n\
|
|---|
| 1080 | self[key] = value = self.default_factory()\n\
|
|---|
| 1081 | return value\n\
|
|---|
| 1082 | ");
|
|---|
| 1083 |
|
|---|
| 1084 | static PyObject *
|
|---|
| 1085 | defdict_missing(defdictobject *dd, PyObject *key)
|
|---|
| 1086 | {
|
|---|
| 1087 | PyObject *factory = dd->default_factory;
|
|---|
| 1088 | PyObject *value;
|
|---|
| 1089 | if (factory == NULL || factory == Py_None) {
|
|---|
| 1090 | /* XXX Call dict.__missing__(key) */
|
|---|
| 1091 | PyErr_SetObject(PyExc_KeyError, key);
|
|---|
| 1092 | return NULL;
|
|---|
| 1093 | }
|
|---|
| 1094 | value = PyEval_CallObject(factory, NULL);
|
|---|
| 1095 | if (value == NULL)
|
|---|
| 1096 | return value;
|
|---|
| 1097 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
|
|---|
| 1098 | Py_DECREF(value);
|
|---|
| 1099 | return NULL;
|
|---|
| 1100 | }
|
|---|
| 1101 | return value;
|
|---|
| 1102 | }
|
|---|
| 1103 |
|
|---|
| 1104 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
|
|---|
| 1105 |
|
|---|
| 1106 | static PyObject *
|
|---|
| 1107 | defdict_copy(defdictobject *dd)
|
|---|
| 1108 | {
|
|---|
| 1109 | /* This calls the object's class. That only works for subclasses
|
|---|
| 1110 | whose class constructor has the same signature. Subclasses that
|
|---|
| 1111 | define a different constructor signature must override copy().
|
|---|
| 1112 | */
|
|---|
| 1113 | return PyObject_CallFunctionObjArgs((PyObject *)dd->dict.ob_type,
|
|---|
| 1114 | dd->default_factory, dd, NULL);
|
|---|
| 1115 | }
|
|---|
| 1116 |
|
|---|
| 1117 | static PyObject *
|
|---|
| 1118 | defdict_reduce(defdictobject *dd)
|
|---|
| 1119 | {
|
|---|
| 1120 | /* __reduce__ must return a 5-tuple as follows:
|
|---|
| 1121 |
|
|---|
| 1122 | - factory function
|
|---|
| 1123 | - tuple of args for the factory function
|
|---|
| 1124 | - additional state (here None)
|
|---|
| 1125 | - sequence iterator (here None)
|
|---|
| 1126 | - dictionary iterator (yielding successive (key, value) pairs
|
|---|
| 1127 |
|
|---|
| 1128 | This API is used by pickle.py and copy.py.
|
|---|
| 1129 |
|
|---|
| 1130 | For this to be useful with pickle.py, the default_factory
|
|---|
| 1131 | must be picklable; e.g., None, a built-in, or a global
|
|---|
| 1132 | function in a module or package.
|
|---|
| 1133 |
|
|---|
| 1134 | Both shallow and deep copying are supported, but for deep
|
|---|
| 1135 | copying, the default_factory must be deep-copyable; e.g. None,
|
|---|
| 1136 | or a built-in (functions are not copyable at this time).
|
|---|
| 1137 |
|
|---|
| 1138 | This only works for subclasses as long as their constructor
|
|---|
| 1139 | signature is compatible; the first argument must be the
|
|---|
| 1140 | optional default_factory, defaulting to None.
|
|---|
| 1141 | */
|
|---|
| 1142 | PyObject *args;
|
|---|
| 1143 | PyObject *items;
|
|---|
| 1144 | PyObject *result;
|
|---|
| 1145 | if (dd->default_factory == NULL || dd->default_factory == Py_None)
|
|---|
| 1146 | args = PyTuple_New(0);
|
|---|
| 1147 | else
|
|---|
| 1148 | args = PyTuple_Pack(1, dd->default_factory);
|
|---|
| 1149 | if (args == NULL)
|
|---|
| 1150 | return NULL;
|
|---|
| 1151 | items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
|
|---|
| 1152 | if (items == NULL) {
|
|---|
| 1153 | Py_DECREF(args);
|
|---|
| 1154 | return NULL;
|
|---|
| 1155 | }
|
|---|
| 1156 | result = PyTuple_Pack(5, dd->dict.ob_type, args,
|
|---|
| 1157 | Py_None, Py_None, items);
|
|---|
| 1158 | Py_DECREF(items);
|
|---|
| 1159 | Py_DECREF(args);
|
|---|
| 1160 | return result;
|
|---|
| 1161 | }
|
|---|
| 1162 |
|
|---|
| 1163 | static PyMethodDef defdict_methods[] = {
|
|---|
| 1164 | {"__missing__", (PyCFunction)defdict_missing, METH_O,
|
|---|
| 1165 | defdict_missing_doc},
|
|---|
| 1166 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
|
|---|
| 1167 | defdict_copy_doc},
|
|---|
| 1168 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
|
|---|
| 1169 | defdict_copy_doc},
|
|---|
| 1170 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
|
|---|
| 1171 | reduce_doc},
|
|---|
| 1172 | {NULL}
|
|---|
| 1173 | };
|
|---|
| 1174 |
|
|---|
| 1175 | static PyMemberDef defdict_members[] = {
|
|---|
| 1176 | {"default_factory", T_OBJECT,
|
|---|
| 1177 | offsetof(defdictobject, default_factory), 0,
|
|---|
| 1178 | PyDoc_STR("Factory for default value called by __missing__().")},
|
|---|
| 1179 | {NULL}
|
|---|
| 1180 | };
|
|---|
| 1181 |
|
|---|
| 1182 | static void
|
|---|
| 1183 | defdict_dealloc(defdictobject *dd)
|
|---|
| 1184 | {
|
|---|
| 1185 | Py_CLEAR(dd->default_factory);
|
|---|
| 1186 | PyDict_Type.tp_dealloc((PyObject *)dd);
|
|---|
| 1187 | }
|
|---|
| 1188 |
|
|---|
| 1189 | static int
|
|---|
| 1190 | defdict_print(defdictobject *dd, FILE *fp, int flags)
|
|---|
| 1191 | {
|
|---|
| 1192 | int sts;
|
|---|
| 1193 | fprintf(fp, "defaultdict(");
|
|---|
| 1194 | if (dd->default_factory == NULL)
|
|---|
| 1195 | fprintf(fp, "None");
|
|---|
| 1196 | else {
|
|---|
| 1197 | PyObject_Print(dd->default_factory, fp, 0);
|
|---|
| 1198 | }
|
|---|
| 1199 | fprintf(fp, ", ");
|
|---|
| 1200 | sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
|
|---|
| 1201 | fprintf(fp, ")");
|
|---|
| 1202 | return sts;
|
|---|
| 1203 | }
|
|---|
| 1204 |
|
|---|
| 1205 | static PyObject *
|
|---|
| 1206 | defdict_repr(defdictobject *dd)
|
|---|
| 1207 | {
|
|---|
| 1208 | PyObject *defrepr;
|
|---|
| 1209 | PyObject *baserepr;
|
|---|
| 1210 | PyObject *result;
|
|---|
| 1211 | baserepr = PyDict_Type.tp_repr((PyObject *)dd);
|
|---|
| 1212 | if (baserepr == NULL)
|
|---|
| 1213 | return NULL;
|
|---|
| 1214 | if (dd->default_factory == NULL)
|
|---|
| 1215 | defrepr = PyString_FromString("None");
|
|---|
| 1216 | else
|
|---|
| 1217 | defrepr = PyObject_Repr(dd->default_factory);
|
|---|
| 1218 | if (defrepr == NULL) {
|
|---|
| 1219 | Py_DECREF(baserepr);
|
|---|
| 1220 | return NULL;
|
|---|
| 1221 | }
|
|---|
| 1222 | result = PyString_FromFormat("defaultdict(%s, %s)",
|
|---|
| 1223 | PyString_AS_STRING(defrepr),
|
|---|
| 1224 | PyString_AS_STRING(baserepr));
|
|---|
| 1225 | Py_DECREF(defrepr);
|
|---|
| 1226 | Py_DECREF(baserepr);
|
|---|
| 1227 | return result;
|
|---|
| 1228 | }
|
|---|
| 1229 |
|
|---|
| 1230 | static int
|
|---|
| 1231 | defdict_traverse(PyObject *self, visitproc visit, void *arg)
|
|---|
| 1232 | {
|
|---|
| 1233 | Py_VISIT(((defdictobject *)self)->default_factory);
|
|---|
| 1234 | return PyDict_Type.tp_traverse(self, visit, arg);
|
|---|
| 1235 | }
|
|---|
| 1236 |
|
|---|
| 1237 | static int
|
|---|
| 1238 | defdict_tp_clear(defdictobject *dd)
|
|---|
| 1239 | {
|
|---|
| 1240 | Py_CLEAR(dd->default_factory);
|
|---|
| 1241 | return PyDict_Type.tp_clear((PyObject *)dd);
|
|---|
| 1242 | }
|
|---|
| 1243 |
|
|---|
| 1244 | static int
|
|---|
| 1245 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|---|
| 1246 | {
|
|---|
| 1247 | defdictobject *dd = (defdictobject *)self;
|
|---|
| 1248 | PyObject *olddefault = dd->default_factory;
|
|---|
| 1249 | PyObject *newdefault = NULL;
|
|---|
| 1250 | PyObject *newargs;
|
|---|
| 1251 | int result;
|
|---|
| 1252 | if (args == NULL || !PyTuple_Check(args))
|
|---|
| 1253 | newargs = PyTuple_New(0);
|
|---|
| 1254 | else {
|
|---|
| 1255 | Py_ssize_t n = PyTuple_GET_SIZE(args);
|
|---|
| 1256 | if (n > 0)
|
|---|
| 1257 | newdefault = PyTuple_GET_ITEM(args, 0);
|
|---|
| 1258 | newargs = PySequence_GetSlice(args, 1, n);
|
|---|
| 1259 | }
|
|---|
| 1260 | if (newargs == NULL)
|
|---|
| 1261 | return -1;
|
|---|
| 1262 | Py_XINCREF(newdefault);
|
|---|
| 1263 | dd->default_factory = newdefault;
|
|---|
| 1264 | result = PyDict_Type.tp_init(self, newargs, kwds);
|
|---|
| 1265 | Py_DECREF(newargs);
|
|---|
| 1266 | Py_XDECREF(olddefault);
|
|---|
| 1267 | return result;
|
|---|
| 1268 | }
|
|---|
| 1269 |
|
|---|
| 1270 | PyDoc_STRVAR(defdict_doc,
|
|---|
| 1271 | "defaultdict(default_factory) --> dict with default factory\n\
|
|---|
| 1272 | \n\
|
|---|
| 1273 | The default factory is called without arguments to produce\n\
|
|---|
| 1274 | a new value when a key is not present, in __getitem__ only.\n\
|
|---|
| 1275 | A defaultdict compares equal to a dict with the same items.\n\
|
|---|
| 1276 | ");
|
|---|
| 1277 |
|
|---|
| 1278 | /* See comment in xxsubtype.c */
|
|---|
| 1279 | #define DEFERRED_ADDRESS(ADDR) 0
|
|---|
| 1280 |
|
|---|
| 1281 | static PyTypeObject defdict_type = {
|
|---|
| 1282 | PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
|
|---|
| 1283 | 0, /* ob_size */
|
|---|
| 1284 | "collections.defaultdict", /* tp_name */
|
|---|
| 1285 | sizeof(defdictobject), /* tp_basicsize */
|
|---|
| 1286 | 0, /* tp_itemsize */
|
|---|
| 1287 | /* methods */
|
|---|
| 1288 | (destructor)defdict_dealloc, /* tp_dealloc */
|
|---|
| 1289 | (printfunc)defdict_print, /* tp_print */
|
|---|
| 1290 | 0, /* tp_getattr */
|
|---|
| 1291 | 0, /* tp_setattr */
|
|---|
| 1292 | 0, /* tp_compare */
|
|---|
| 1293 | (reprfunc)defdict_repr, /* tp_repr */
|
|---|
| 1294 | 0, /* tp_as_number */
|
|---|
| 1295 | 0, /* tp_as_sequence */
|
|---|
| 1296 | 0, /* tp_as_mapping */
|
|---|
| 1297 | 0, /* tp_hash */
|
|---|
| 1298 | 0, /* tp_call */
|
|---|
| 1299 | 0, /* tp_str */
|
|---|
| 1300 | PyObject_GenericGetAttr, /* tp_getattro */
|
|---|
| 1301 | 0, /* tp_setattro */
|
|---|
| 1302 | 0, /* tp_as_buffer */
|
|---|
| 1303 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
|
|---|
| 1304 | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
|
|---|
| 1305 | defdict_doc, /* tp_doc */
|
|---|
| 1306 | defdict_traverse, /* tp_traverse */
|
|---|
| 1307 | (inquiry)defdict_tp_clear, /* tp_clear */
|
|---|
| 1308 | 0, /* tp_richcompare */
|
|---|
| 1309 | 0, /* tp_weaklistoffset*/
|
|---|
| 1310 | 0, /* tp_iter */
|
|---|
| 1311 | 0, /* tp_iternext */
|
|---|
| 1312 | defdict_methods, /* tp_methods */
|
|---|
| 1313 | defdict_members, /* tp_members */
|
|---|
| 1314 | 0, /* tp_getset */
|
|---|
| 1315 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
|
|---|
| 1316 | 0, /* tp_dict */
|
|---|
| 1317 | 0, /* tp_descr_get */
|
|---|
| 1318 | 0, /* tp_descr_set */
|
|---|
| 1319 | 0, /* tp_dictoffset */
|
|---|
| 1320 | defdict_init, /* tp_init */
|
|---|
| 1321 | PyType_GenericAlloc, /* tp_alloc */
|
|---|
| 1322 | 0, /* tp_new */
|
|---|
| 1323 | PyObject_GC_Del, /* tp_free */
|
|---|
| 1324 | };
|
|---|
| 1325 |
|
|---|
| 1326 | /* module level code ********************************************************/
|
|---|
| 1327 |
|
|---|
| 1328 | PyDoc_STRVAR(module_doc,
|
|---|
| 1329 | "High performance data structures.\n\
|
|---|
| 1330 | - deque: ordered collection accessible from endpoints only\n\
|
|---|
| 1331 | - defaultdict: dict subclass with a default value factory\n\
|
|---|
| 1332 | ");
|
|---|
| 1333 |
|
|---|
| 1334 | PyMODINIT_FUNC
|
|---|
| 1335 | initcollections(void)
|
|---|
| 1336 | {
|
|---|
| 1337 | PyObject *m;
|
|---|
| 1338 |
|
|---|
| 1339 | m = Py_InitModule3("collections", NULL, module_doc);
|
|---|
| 1340 | if (m == NULL)
|
|---|
| 1341 | return;
|
|---|
| 1342 |
|
|---|
| 1343 | if (PyType_Ready(&deque_type) < 0)
|
|---|
| 1344 | return;
|
|---|
| 1345 | Py_INCREF(&deque_type);
|
|---|
| 1346 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
|
|---|
| 1347 |
|
|---|
| 1348 | defdict_type.tp_base = &PyDict_Type;
|
|---|
| 1349 | if (PyType_Ready(&defdict_type) < 0)
|
|---|
| 1350 | return;
|
|---|
| 1351 | Py_INCREF(&defdict_type);
|
|---|
| 1352 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
|
|---|
| 1353 |
|
|---|
| 1354 | if (PyType_Ready(&dequeiter_type) < 0)
|
|---|
| 1355 | return;
|
|---|
| 1356 |
|
|---|
| 1357 | if (PyType_Ready(&dequereviter_type) < 0)
|
|---|
| 1358 | return;
|
|---|
| 1359 |
|
|---|
| 1360 | return;
|
|---|
| 1361 | }
|
|---|