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