2 #include "structmember.h"
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.
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.
17 #define CENTER ((BLOCKLEN - 1) / 2)
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.
28 * The list of blocks is never empty, so d.leftblock and d.rightblock
29 * are never equal to NULL.
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.
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.
40 * Whenever d.leftblock == d.rightblock,
41 * d.leftindex + d.len - 1 == d.rightindex.
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
48 typedef struct BLOCK
{
49 struct BLOCK
*leftlink
;
50 struct BLOCK
*rightlink
;
51 PyObject
*data
[BLOCKLEN
];
54 #define MAXFREEBLOCKS 10
55 static Py_ssize_t numfreeblocks
= 0;
56 static block
*freeblocks
[MAXFREEBLOCKS
];
59 newblock(block
*leftlink
, block
*rightlink
, Py_ssize_t len
) {
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.
68 if (len
>= PY_SSIZE_T_MAX
- 2*BLOCKLEN
) {
69 PyErr_SetString(PyExc_OverflowError
,
70 "cannot add more blocks to the deque");
75 b
= freeblocks
[numfreeblocks
];
77 b
= PyMem_Malloc(sizeof(block
));
83 b
->leftlink
= leftlink
;
84 b
->rightlink
= rightlink
;
91 if (numfreeblocks
< MAXFREEBLOCKS
) {
92 freeblocks
[numfreeblocks
] = b
;
103 Py_ssize_t leftindex
; /* in range(BLOCKLEN) */
104 Py_ssize_t rightindex
; /* in range(BLOCKLEN) */
107 long state
; /* incremented whenever the indices move */
108 PyObject
*weakreflist
; /* List of weak references */
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.
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().
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); \
127 static PyTypeObject deque_type
;
130 deque_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
135 /* create dequeobject structure */
136 deque
= (dequeobject
*)type
->tp_alloc(type
, 0);
140 b
= newblock(NULL
, NULL
, 0);
146 assert(BLOCKLEN
>= 2);
147 deque
->leftblock
= b
;
148 deque
->rightblock
= b
;
149 deque
->leftindex
= CENTER
+ 1;
150 deque
->rightindex
= CENTER
;
153 deque
->weakreflist
= NULL
;
156 return (PyObject
*)deque
;
160 deque_pop(dequeobject
*deque
, PyObject
*unused
)
165 if (deque
->len
== 0) {
166 PyErr_SetString(PyExc_IndexError
, "pop from an empty deque");
169 item
= deque
->rightblock
->data
[deque
->rightindex
];
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
;
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;
193 PyDoc_STRVAR(pop_doc
, "Remove and return the rightmost element.");
196 deque_popleft(dequeobject
*deque
, PyObject
*unused
)
201 if (deque
->len
== 0) {
202 PyErr_SetString(PyExc_IndexError
, "pop from an empty deque");
205 assert(deque
->leftblock
!= NULL
);
206 item
= deque
->leftblock
->data
[deque
->leftindex
];
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
;
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;
231 PyDoc_STRVAR(popleft_doc
, "Remove and return the leftmost element.");
234 deque_append(dequeobject
*deque
, PyObject
*item
)
237 if (deque
->rightindex
== BLOCKLEN
-1) {
238 block
*b
= newblock(deque
->rightblock
, NULL
, deque
->len
);
241 assert(deque
->rightblock
->rightlink
== NULL
);
242 deque
->rightblock
->rightlink
= b
;
243 deque
->rightblock
= b
;
244 deque
->rightindex
= -1;
249 deque
->rightblock
->data
[deque
->rightindex
] = item
;
250 TRIM(deque
, deque_popleft
);
254 PyDoc_STRVAR(append_doc
, "Add an element to the right side of the deque.");
257 deque_appendleft(dequeobject
*deque
, PyObject
*item
)
260 if (deque
->leftindex
== 0) {
261 block
*b
= newblock(NULL
, deque
->leftblock
, deque
->len
);
264 assert(deque
->leftblock
->leftlink
== NULL
);
265 deque
->leftblock
->leftlink
= b
;
266 deque
->leftblock
= b
;
267 deque
->leftindex
= BLOCKLEN
;
272 deque
->leftblock
->data
[deque
->leftindex
] = item
;
273 TRIM(deque
, deque_pop
);
277 PyDoc_STRVAR(appendleft_doc
, "Add an element to the left side of the deque.");
280 /* Run an iterator to exhaustion. Shortcut for
281 the extend/extendleft methods when maxlen == 0. */
283 consume_iterator(PyObject
*it
)
287 while ((item
= PyIter_Next(it
)) != NULL
) {
291 if (PyErr_Occurred())
297 deque_extend(dequeobject
*deque
, PyObject
*iterable
)
301 /* Handle case where id(deque) == id(iterable) */
302 if ((PyObject
*)deque
== iterable
) {
304 PyObject
*s
= PySequence_List(iterable
);
307 result
= deque_extend(deque
, s
);
312 it
= PyObject_GetIter(iterable
);
316 if (deque
->maxlen
== 0)
317 return consume_iterator(it
);
319 while ((item
= PyIter_Next(it
)) != NULL
) {
321 if (deque
->rightindex
== BLOCKLEN
-1) {
322 block
*b
= newblock(deque
->rightblock
, NULL
,
329 assert(deque
->rightblock
->rightlink
== NULL
);
330 deque
->rightblock
->rightlink
= b
;
331 deque
->rightblock
= b
;
332 deque
->rightindex
= -1;
336 deque
->rightblock
->data
[deque
->rightindex
] = item
;
337 TRIM(deque
, deque_popleft
);
340 if (PyErr_Occurred())
345 PyDoc_STRVAR(extend_doc
,
346 "Extend the right side of the deque with elements from the iterable");
349 deque_extendleft(dequeobject
*deque
, PyObject
*iterable
)
353 /* Handle case where id(deque) == id(iterable) */
354 if ((PyObject
*)deque
== iterable
) {
356 PyObject
*s
= PySequence_List(iterable
);
359 result
= deque_extendleft(deque
, s
);
364 it
= PyObject_GetIter(iterable
);
368 if (deque
->maxlen
== 0)
369 return consume_iterator(it
);
371 while ((item
= PyIter_Next(it
)) != NULL
) {
373 if (deque
->leftindex
== 0) {
374 block
*b
= newblock(NULL
, deque
->leftblock
,
381 assert(deque
->leftblock
->leftlink
== NULL
);
382 deque
->leftblock
->leftlink
= b
;
383 deque
->leftblock
= b
;
384 deque
->leftindex
= BLOCKLEN
;
388 deque
->leftblock
->data
[deque
->leftindex
] = item
;
389 TRIM(deque
, deque_pop
);
392 if (PyErr_Occurred())
397 PyDoc_STRVAR(extendleft_doc
,
398 "Extend the left side of the deque with elements from the iterable");
401 deque_inplace_concat(dequeobject
*deque
, PyObject
*other
)
405 result
= deque_extend(deque
, other
);
410 return (PyObject
*)deque
;
414 _deque_rotate(dequeobject
*deque
, Py_ssize_t n
)
416 Py_ssize_t i
, len
=deque
->len
, halflen
=(len
+1)>>1;
421 if (n
> halflen
|| n
< -halflen
) {
425 else if (n
< -halflen
)
429 for (i
=0 ; i
<n
; i
++) {
430 item
= deque_pop(deque
, NULL
);
431 assert (item
!= NULL
);
432 rv
= deque_appendleft(deque
, item
);
438 for (i
=0 ; i
>n
; i
--) {
439 item
= deque_popleft(deque
, NULL
);
440 assert (item
!= NULL
);
441 rv
= deque_append(deque
, item
);
451 deque_rotate(dequeobject
*deque
, PyObject
*args
)
455 if (!PyArg_ParseTuple(args
, "|n:rotate", &n
))
457 if (_deque_rotate(deque
, n
) == 0)
462 PyDoc_STRVAR(rotate_doc
,
463 "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
466 deque_reverse(dequeobject
*deque
, PyObject
*unused
)
468 block
*leftblock
= deque
->leftblock
;
469 block
*rightblock
= deque
->rightblock
;
470 Py_ssize_t leftindex
= deque
->leftindex
;
471 Py_ssize_t rightindex
= deque
->rightindex
;
472 Py_ssize_t n
= (deque
->len
)/2;
476 for (i
=0 ; i
<n
; i
++) {
477 /* Validate that pointers haven't met in the middle */
478 assert(leftblock
!= rightblock
|| leftindex
< rightindex
);
481 tmp
= leftblock
->data
[leftindex
];
482 leftblock
->data
[leftindex
] = rightblock
->data
[rightindex
];
483 rightblock
->data
[rightindex
] = tmp
;
485 /* Advance left block/index pair */
487 if (leftindex
== BLOCKLEN
) {
488 assert (leftblock
->rightlink
!= NULL
);
489 leftblock
= leftblock
->rightlink
;
493 /* Step backwards with the right block/index pair */
495 if (rightindex
== -1) {
496 assert (rightblock
->leftlink
!= NULL
);
497 rightblock
= rightblock
->leftlink
;
498 rightindex
= BLOCKLEN
- 1;
504 PyDoc_STRVAR(reverse_doc
,
505 "D.reverse() -- reverse *IN PLACE*");
508 deque_len(dequeobject
*deque
)
514 deque_remove(dequeobject
*deque
, PyObject
*value
)
516 Py_ssize_t i
, n
=deque
->len
;
518 for (i
=0 ; i
<n
; i
++) {
519 PyObject
*item
= deque
->leftblock
->data
[deque
->leftindex
];
520 int cmp
= PyObject_RichCompareBool(item
, value
, Py_EQ
);
522 if (deque
->len
!= n
) {
523 PyErr_SetString(PyExc_IndexError
,
524 "deque mutated during remove().");
528 PyObject
*tgt
= deque_popleft(deque
, NULL
);
529 assert (tgt
!= NULL
);
531 if (_deque_rotate(deque
, i
) == -1)
536 _deque_rotate(deque
, i
);
539 _deque_rotate(deque
, -1);
541 PyErr_SetString(PyExc_ValueError
, "deque.remove(x): x not in deque");
545 PyDoc_STRVAR(remove_doc
,
546 "D.remove(value) -- remove first occurrence of value.");
549 deque_clear(dequeobject
*deque
)
554 item
= deque_pop(deque
, NULL
);
555 assert (item
!= NULL
);
558 assert(deque
->leftblock
== deque
->rightblock
&&
559 deque
->leftindex
- 1 == deque
->rightindex
&&
565 deque_item(dequeobject
*deque
, Py_ssize_t i
)
569 Py_ssize_t n
, index
=i
;
571 if (i
< 0 || i
>= deque
->len
) {
572 PyErr_SetString(PyExc_IndexError
,
573 "deque index out of range");
578 i
= deque
->leftindex
;
579 b
= deque
->leftblock
;
580 } else if (i
== deque
->len
- 1) {
581 i
= deque
->rightindex
;
582 b
= deque
->rightblock
;
584 i
+= deque
->leftindex
;
587 if (index
< (deque
->len
>> 1)) {
588 b
= deque
->leftblock
;
592 n
= (deque
->leftindex
+ deque
->len
- 1) / BLOCKLEN
- n
;
593 b
= deque
->rightblock
;
603 /* delitem() implemented in terms of rotate for simplicity and reasonable
604 performance near the end points. If for some reason this method becomes
605 popular, it is not hard to re-implement this using direct data movement
606 (similar to code in list slice assignment) and achieve a two or threefold
611 deque_del_item(dequeobject
*deque
, Py_ssize_t i
)
615 assert (i
>= 0 && i
< deque
->len
);
616 if (_deque_rotate(deque
, -i
) == -1)
619 item
= deque_popleft(deque
, NULL
);
620 assert (item
!= NULL
);
623 return _deque_rotate(deque
, i
);
627 deque_ass_item(dequeobject
*deque
, Py_ssize_t i
, PyObject
*v
)
631 Py_ssize_t n
, len
=deque
->len
, halflen
=(len
+1)>>1, index
=i
;
633 if (i
< 0 || i
>= len
) {
634 PyErr_SetString(PyExc_IndexError
,
635 "deque index out of range");
639 return deque_del_item(deque
, i
);
641 i
+= deque
->leftindex
;
644 if (index
<= halflen
) {
645 b
= deque
->leftblock
;
649 n
= (deque
->leftindex
+ len
- 1) / BLOCKLEN
- n
;
650 b
= deque
->rightblock
;
655 old_value
= b
->data
[i
];
657 Py_DECREF(old_value
);
662 deque_clearmethod(dequeobject
*deque
)
666 rv
= deque_clear(deque
);
671 PyDoc_STRVAR(clear_doc
, "Remove all elements from the deque.");
674 deque_dealloc(dequeobject
*deque
)
676 PyObject_GC_UnTrack(deque
);
677 if (deque
->weakreflist
!= NULL
)
678 PyObject_ClearWeakRefs((PyObject
*) deque
);
679 if (deque
->leftblock
!= NULL
) {
681 assert(deque
->leftblock
!= NULL
);
682 freeblock(deque
->leftblock
);
684 deque
->leftblock
= NULL
;
685 deque
->rightblock
= NULL
;
686 Py_TYPE(deque
)->tp_free(deque
);
690 deque_traverse(dequeobject
*deque
, visitproc visit
, void *arg
)
695 Py_ssize_t indexlo
= deque
->leftindex
;
697 for (b
= deque
->leftblock
; b
!= NULL
; b
= b
->rightlink
) {
698 const Py_ssize_t indexhi
= b
== deque
->rightblock
?
702 for (index
= indexlo
; index
<= indexhi
; ++index
) {
703 item
= b
->data
[index
];
712 deque_copy(PyObject
*deque
)
714 if (((dequeobject
*)deque
)->maxlen
== -1)
715 return PyObject_CallFunction((PyObject
*)(Py_TYPE(deque
)), "O", deque
, NULL
);
717 return PyObject_CallFunction((PyObject
*)(Py_TYPE(deque
)), "Oi",
718 deque
, ((dequeobject
*)deque
)->maxlen
, NULL
);
721 PyDoc_STRVAR(copy_doc
, "Return a shallow copy of a deque.");
724 deque_reduce(dequeobject
*deque
)
726 PyObject
*dict
, *result
, *aslist
;
728 dict
= PyObject_GetAttrString((PyObject
*)deque
, "__dict__");
731 aslist
= PySequence_List((PyObject
*)deque
);
732 if (aslist
== NULL
) {
737 if (deque
->maxlen
== -1)
738 result
= Py_BuildValue("O(O)", Py_TYPE(deque
), aslist
);
740 result
= Py_BuildValue("O(On)", Py_TYPE(deque
), aslist
, deque
->maxlen
);
742 if (deque
->maxlen
== -1)
743 result
= Py_BuildValue("O(OO)O", Py_TYPE(deque
), aslist
, Py_None
, dict
);
745 result
= Py_BuildValue("O(On)O", Py_TYPE(deque
), aslist
, deque
->maxlen
, dict
);
752 PyDoc_STRVAR(reduce_doc
, "Return state information for pickling.");
755 deque_repr(PyObject
*deque
)
757 PyObject
*aslist
, *result
, *fmt
;
760 i
= Py_ReprEnter(deque
);
764 return PyString_FromString("[...]");
767 aslist
= PySequence_List(deque
);
768 if (aslist
== NULL
) {
772 if (((dequeobject
*)deque
)->maxlen
!= -1)
773 fmt
= PyString_FromFormat("deque(%%r, maxlen=%zd)",
774 ((dequeobject
*)deque
)->maxlen
);
776 fmt
= PyString_FromString("deque(%r)");
782 result
= PyString_Format(fmt
, aslist
);
790 deque_tp_print(PyObject
*deque
, FILE *fp
, int flags
)
793 char *emit
= ""; /* No separator emitted on first pass */
794 char *separator
= ", ";
797 i
= Py_ReprEnter(deque
);
801 Py_BEGIN_ALLOW_THREADS
807 it
= PyObject_GetIter(deque
);
811 Py_BEGIN_ALLOW_THREADS
812 fputs("deque([", fp
);
814 while ((item
= PyIter_Next(it
)) != NULL
) {
815 Py_BEGIN_ALLOW_THREADS
819 if (PyObject_Print(item
, fp
, 0) != 0) {
829 if (PyErr_Occurred())
832 Py_BEGIN_ALLOW_THREADS
833 if (((dequeobject
*)deque
)->maxlen
== -1)
836 fprintf(fp
, "], maxlen=%" PY_FORMAT_SIZE_T
"d)", ((dequeobject
*)deque
)->maxlen
);
842 deque_richcompare(PyObject
*v
, PyObject
*w
, int op
)
844 PyObject
*it1
=NULL
, *it2
=NULL
, *x
, *y
;
848 if (!PyObject_TypeCheck(v
, &deque_type
) ||
849 !PyObject_TypeCheck(w
, &deque_type
)) {
850 Py_INCREF(Py_NotImplemented
);
851 return Py_NotImplemented
;
855 vs
= ((dequeobject
*)v
)->len
;
856 ws
= ((dequeobject
*)w
)->len
;
870 /* Search for the first index where items are different */
871 it1
= PyObject_GetIter(v
);
874 it2
= PyObject_GetIter(w
);
878 x
= PyIter_Next(it1
);
879 if (x
== NULL
&& PyErr_Occurred())
881 y
= PyIter_Next(it2
);
882 if (x
== NULL
|| y
== NULL
)
884 b
= PyObject_RichCompareBool(x
, y
, Py_EQ
);
886 cmp
= PyObject_RichCompareBool(x
, y
, op
);
896 /* We reached the end of one deque or both */
899 if (PyErr_Occurred())
902 case Py_LT
: cmp
= y
!= NULL
; break; /* if w was longer */
903 case Py_LE
: cmp
= x
== NULL
; break; /* if v was not longer */
904 case Py_EQ
: cmp
= x
== y
; break; /* if we reached the end of both */
905 case Py_NE
: cmp
= x
!= y
; break; /* if one deque continues */
906 case Py_GT
: cmp
= x
!= NULL
; break; /* if v was longer */
907 case Py_GE
: cmp
= y
== NULL
; break; /* if w was not longer */
921 deque_init(dequeobject
*deque
, PyObject
*args
, PyObject
*kwdargs
)
923 PyObject
*iterable
= NULL
;
924 PyObject
*maxlenobj
= NULL
;
925 Py_ssize_t maxlen
= -1;
926 char *kwlist
[] = {"iterable", "maxlen", 0};
928 if (!PyArg_ParseTupleAndKeywords(args
, kwdargs
, "|OO:deque", kwlist
, &iterable
, &maxlenobj
))
930 if (maxlenobj
!= NULL
&& maxlenobj
!= Py_None
) {
931 maxlen
= PyInt_AsSsize_t(maxlenobj
);
932 if (maxlen
== -1 && PyErr_Occurred())
935 PyErr_SetString(PyExc_ValueError
, "maxlen must be non-negative");
939 deque
->maxlen
= maxlen
;
941 if (iterable
!= NULL
) {
942 PyObject
*rv
= deque_extend(deque
, iterable
);
951 deque_get_maxlen(dequeobject
*deque
)
953 if (deque
->maxlen
== -1)
955 return PyInt_FromSsize_t(deque
->maxlen
);
958 static PyGetSetDef deque_getset
[] = {
959 {"maxlen", (getter
)deque_get_maxlen
, (setter
)NULL
,
960 "maximum size of a deque or None if unbounded"},
964 static PySequenceMethods deque_as_sequence
= {
965 (lenfunc
)deque_len
, /* sq_length */
968 (ssizeargfunc
)deque_item
, /* sq_item */
970 (ssizeobjargproc
)deque_ass_item
, /* sq_ass_item */
971 0, /* sq_ass_slice */
973 (binaryfunc
)deque_inplace_concat
, /* sq_inplace_concat */
974 0, /* sq_inplace_repeat */
978 /* deque object ********************************************************/
980 static PyObject
*deque_iter(dequeobject
*deque
);
981 static PyObject
*deque_reviter(dequeobject
*deque
);
982 PyDoc_STRVAR(reversed_doc
,
983 "D.__reversed__() -- return a reverse iterator over the deque");
985 static PyMethodDef deque_methods
[] = {
986 {"append", (PyCFunction
)deque_append
,
988 {"appendleft", (PyCFunction
)deque_appendleft
,
989 METH_O
, appendleft_doc
},
990 {"clear", (PyCFunction
)deque_clearmethod
,
991 METH_NOARGS
, clear_doc
},
992 {"__copy__", (PyCFunction
)deque_copy
,
993 METH_NOARGS
, copy_doc
},
994 {"extend", (PyCFunction
)deque_extend
,
996 {"extendleft", (PyCFunction
)deque_extendleft
,
997 METH_O
, extendleft_doc
},
998 {"pop", (PyCFunction
)deque_pop
,
999 METH_NOARGS
, pop_doc
},
1000 {"popleft", (PyCFunction
)deque_popleft
,
1001 METH_NOARGS
, popleft_doc
},
1002 {"__reduce__", (PyCFunction
)deque_reduce
,
1003 METH_NOARGS
, reduce_doc
},
1004 {"remove", (PyCFunction
)deque_remove
,
1005 METH_O
, remove_doc
},
1006 {"__reversed__", (PyCFunction
)deque_reviter
,
1007 METH_NOARGS
, reversed_doc
},
1008 {"reverse", (PyCFunction
)deque_reverse
,
1009 METH_NOARGS
, reverse_doc
},
1010 {"rotate", (PyCFunction
)deque_rotate
,
1011 METH_VARARGS
, rotate_doc
},
1012 {NULL
, NULL
} /* sentinel */
1015 PyDoc_STRVAR(deque_doc
,
1016 "deque(iterable[, maxlen]) --> deque object\n\
1018 Build an ordered collection accessible from endpoints only.");
1020 static PyTypeObject deque_type
= {
1021 PyVarObject_HEAD_INIT(NULL
, 0)
1022 "collections.deque", /* tp_name */
1023 sizeof(dequeobject
), /* tp_basicsize */
1024 0, /* tp_itemsize */
1026 (destructor
)deque_dealloc
, /* tp_dealloc */
1027 deque_tp_print
, /* tp_print */
1031 deque_repr
, /* tp_repr */
1032 0, /* tp_as_number */
1033 &deque_as_sequence
, /* tp_as_sequence */
1034 0, /* tp_as_mapping */
1035 (hashfunc
)PyObject_HashNotImplemented
, /* tp_hash */
1038 PyObject_GenericGetAttr
, /* tp_getattro */
1039 0, /* tp_setattro */
1040 0, /* tp_as_buffer */
1041 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
|
1042 Py_TPFLAGS_HAVE_WEAKREFS
, /* tp_flags */
1043 deque_doc
, /* tp_doc */
1044 (traverseproc
)deque_traverse
, /* tp_traverse */
1045 (inquiry
)deque_clear
, /* tp_clear */
1046 (richcmpfunc
)deque_richcompare
, /* tp_richcompare */
1047 offsetof(dequeobject
, weakreflist
), /* tp_weaklistoffset*/
1048 (getiterfunc
)deque_iter
, /* tp_iter */
1049 0, /* tp_iternext */
1050 deque_methods
, /* tp_methods */
1052 deque_getset
, /* tp_getset */
1055 0, /* tp_descr_get */
1056 0, /* tp_descr_set */
1057 0, /* tp_dictoffset */
1058 (initproc
)deque_init
, /* tp_init */
1059 PyType_GenericAlloc
, /* tp_alloc */
1060 deque_new
, /* tp_new */
1061 PyObject_GC_Del
, /* tp_free */
1064 /*********************** Deque Iterator **************************/
1071 long state
; /* state when the iterator is created */
1072 Py_ssize_t counter
; /* number of items remaining for iteration */
1075 static PyTypeObject dequeiter_type
;
1078 deque_iter(dequeobject
*deque
)
1080 dequeiterobject
*it
;
1082 it
= PyObject_GC_New(dequeiterobject
, &dequeiter_type
);
1085 it
->b
= deque
->leftblock
;
1086 it
->index
= deque
->leftindex
;
1089 it
->state
= deque
->state
;
1090 it
->counter
= deque
->len
;
1091 PyObject_GC_Track(it
);
1092 return (PyObject
*)it
;
1096 dequeiter_traverse(dequeiterobject
*dio
, visitproc visit
, void *arg
)
1098 Py_VISIT(dio
->deque
);
1103 dequeiter_dealloc(dequeiterobject
*dio
)
1105 Py_XDECREF(dio
->deque
);
1106 PyObject_GC_Del(dio
);
1110 dequeiter_next(dequeiterobject
*it
)
1114 if (it
->deque
->state
!= it
->state
) {
1116 PyErr_SetString(PyExc_RuntimeError
,
1117 "deque mutated during iteration");
1120 if (it
->counter
== 0)
1122 assert (!(it
->b
== it
->deque
->rightblock
&&
1123 it
->index
> it
->deque
->rightindex
));
1125 item
= it
->b
->data
[it
->index
];
1128 if (it
->index
== BLOCKLEN
&& it
->counter
> 0) {
1129 assert (it
->b
->rightlink
!= NULL
);
1130 it
->b
= it
->b
->rightlink
;
1138 dequeiter_len(dequeiterobject
*it
)
1140 return PyInt_FromLong(it
->counter
);
1143 PyDoc_STRVAR(length_hint_doc
, "Private method returning an estimate of len(list(it)).");
1145 static PyMethodDef dequeiter_methods
[] = {
1146 {"__length_hint__", (PyCFunction
)dequeiter_len
, METH_NOARGS
, length_hint_doc
},
1147 {NULL
, NULL
} /* sentinel */
1150 static PyTypeObject dequeiter_type
= {
1151 PyVarObject_HEAD_INIT(NULL
, 0)
1152 "deque_iterator", /* tp_name */
1153 sizeof(dequeiterobject
), /* tp_basicsize */
1154 0, /* tp_itemsize */
1156 (destructor
)dequeiter_dealloc
, /* tp_dealloc */
1162 0, /* tp_as_number */
1163 0, /* tp_as_sequence */
1164 0, /* tp_as_mapping */
1168 PyObject_GenericGetAttr
, /* tp_getattro */
1169 0, /* tp_setattro */
1170 0, /* tp_as_buffer */
1171 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
1173 (traverseproc
)dequeiter_traverse
, /* tp_traverse */
1175 0, /* tp_richcompare */
1176 0, /* tp_weaklistoffset */
1177 PyObject_SelfIter
, /* tp_iter */
1178 (iternextfunc
)dequeiter_next
, /* tp_iternext */
1179 dequeiter_methods
, /* tp_methods */
1183 /*********************** Deque Reverse Iterator **************************/
1185 static PyTypeObject dequereviter_type
;
1188 deque_reviter(dequeobject
*deque
)
1190 dequeiterobject
*it
;
1192 it
= PyObject_GC_New(dequeiterobject
, &dequereviter_type
);
1195 it
->b
= deque
->rightblock
;
1196 it
->index
= deque
->rightindex
;
1199 it
->state
= deque
->state
;
1200 it
->counter
= deque
->len
;
1201 PyObject_GC_Track(it
);
1202 return (PyObject
*)it
;
1206 dequereviter_next(dequeiterobject
*it
)
1209 if (it
->counter
== 0)
1212 if (it
->deque
->state
!= it
->state
) {
1214 PyErr_SetString(PyExc_RuntimeError
,
1215 "deque mutated during iteration");
1218 assert (!(it
->b
== it
->deque
->leftblock
&&
1219 it
->index
< it
->deque
->leftindex
));
1221 item
= it
->b
->data
[it
->index
];
1224 if (it
->index
== -1 && it
->counter
> 0) {
1225 assert (it
->b
->leftlink
!= NULL
);
1226 it
->b
= it
->b
->leftlink
;
1227 it
->index
= BLOCKLEN
- 1;
1233 static PyTypeObject dequereviter_type
= {
1234 PyVarObject_HEAD_INIT(NULL
, 0)
1235 "deque_reverse_iterator", /* tp_name */
1236 sizeof(dequeiterobject
), /* tp_basicsize */
1237 0, /* tp_itemsize */
1239 (destructor
)dequeiter_dealloc
, /* tp_dealloc */
1245 0, /* tp_as_number */
1246 0, /* tp_as_sequence */
1247 0, /* tp_as_mapping */
1251 PyObject_GenericGetAttr
, /* tp_getattro */
1252 0, /* tp_setattro */
1253 0, /* tp_as_buffer */
1254 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
1256 (traverseproc
)dequeiter_traverse
, /* tp_traverse */
1258 0, /* tp_richcompare */
1259 0, /* tp_weaklistoffset */
1260 PyObject_SelfIter
, /* tp_iter */
1261 (iternextfunc
)dequereviter_next
, /* tp_iternext */
1262 dequeiter_methods
, /* tp_methods */
1266 /* defaultdict type *********************************************************/
1270 PyObject
*default_factory
;
1273 static PyTypeObject defdict_type
; /* Forward */
1275 PyDoc_STRVAR(defdict_missing_doc
,
1276 "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
1277 if self.default_factory is None: raise KeyError((key,))\n\
1278 self[key] = value = self.default_factory()\n\
1283 defdict_missing(defdictobject
*dd
, PyObject
*key
)
1285 PyObject
*factory
= dd
->default_factory
;
1287 if (factory
== NULL
|| factory
== Py_None
) {
1288 /* XXX Call dict.__missing__(key) */
1290 tup
= PyTuple_Pack(1, key
);
1291 if (!tup
) return NULL
;
1292 PyErr_SetObject(PyExc_KeyError
, tup
);
1296 value
= PyEval_CallObject(factory
, NULL
);
1299 if (PyObject_SetItem((PyObject
*)dd
, key
, value
) < 0) {
1306 PyDoc_STRVAR(defdict_copy_doc
, "D.copy() -> a shallow copy of D.");
1309 defdict_copy(defdictobject
*dd
)
1311 /* This calls the object's class. That only works for subclasses
1312 whose class constructor has the same signature. Subclasses that
1313 define a different constructor signature must override copy().
1316 if (dd
->default_factory
== NULL
)
1317 return PyObject_CallFunctionObjArgs((PyObject
*)Py_TYPE(dd
), Py_None
, dd
, NULL
);
1318 return PyObject_CallFunctionObjArgs((PyObject
*)Py_TYPE(dd
),
1319 dd
->default_factory
, dd
, NULL
);
1323 defdict_reduce(defdictobject
*dd
)
1325 /* __reduce__ must return a 5-tuple as follows:
1328 - tuple of args for the factory function
1329 - additional state (here None)
1330 - sequence iterator (here None)
1331 - dictionary iterator (yielding successive (key, value) pairs
1333 This API is used by pickle.py and copy.py.
1335 For this to be useful with pickle.py, the default_factory
1336 must be picklable; e.g., None, a built-in, or a global
1337 function in a module or package.
1339 Both shallow and deep copying are supported, but for deep
1340 copying, the default_factory must be deep-copyable; e.g. None,
1341 or a built-in (functions are not copyable at this time).
1343 This only works for subclasses as long as their constructor
1344 signature is compatible; the first argument must be the
1345 optional default_factory, defaulting to None.
1350 if (dd
->default_factory
== NULL
|| dd
->default_factory
== Py_None
)
1351 args
= PyTuple_New(0);
1353 args
= PyTuple_Pack(1, dd
->default_factory
);
1356 items
= PyObject_CallMethod((PyObject
*)dd
, "iteritems", "()");
1357 if (items
== NULL
) {
1361 result
= PyTuple_Pack(5, Py_TYPE(dd
), args
,
1362 Py_None
, Py_None
, items
);
1368 static PyMethodDef defdict_methods
[] = {
1369 {"__missing__", (PyCFunction
)defdict_missing
, METH_O
,
1370 defdict_missing_doc
},
1371 {"copy", (PyCFunction
)defdict_copy
, METH_NOARGS
,
1373 {"__copy__", (PyCFunction
)defdict_copy
, METH_NOARGS
,
1375 {"__reduce__", (PyCFunction
)defdict_reduce
, METH_NOARGS
,
1380 static PyMemberDef defdict_members
[] = {
1381 {"default_factory", T_OBJECT
,
1382 offsetof(defdictobject
, default_factory
), 0,
1383 PyDoc_STR("Factory for default value called by __missing__().")},
1388 defdict_dealloc(defdictobject
*dd
)
1390 Py_CLEAR(dd
->default_factory
);
1391 PyDict_Type
.tp_dealloc((PyObject
*)dd
);
1395 defdict_print(defdictobject
*dd
, FILE *fp
, int flags
)
1398 Py_BEGIN_ALLOW_THREADS
1399 fprintf(fp
, "defaultdict(");
1400 Py_END_ALLOW_THREADS
1401 if (dd
->default_factory
== NULL
) {
1402 Py_BEGIN_ALLOW_THREADS
1403 fprintf(fp
, "None");
1404 Py_END_ALLOW_THREADS
1406 PyObject_Print(dd
->default_factory
, fp
, 0);
1408 Py_BEGIN_ALLOW_THREADS
1410 Py_END_ALLOW_THREADS
1411 sts
= PyDict_Type
.tp_print((PyObject
*)dd
, fp
, 0);
1412 Py_BEGIN_ALLOW_THREADS
1414 Py_END_ALLOW_THREADS
1419 defdict_repr(defdictobject
*dd
)
1424 baserepr
= PyDict_Type
.tp_repr((PyObject
*)dd
);
1425 if (baserepr
== NULL
)
1427 if (dd
->default_factory
== NULL
)
1428 defrepr
= PyString_FromString("None");
1431 int status
= Py_ReprEnter(dd
->default_factory
);
1435 defrepr
= PyString_FromString("...");
1438 defrepr
= PyObject_Repr(dd
->default_factory
);
1439 Py_ReprLeave(dd
->default_factory
);
1441 if (defrepr
== NULL
) {
1442 Py_DECREF(baserepr
);
1445 result
= PyString_FromFormat("defaultdict(%s, %s)",
1446 PyString_AS_STRING(defrepr
),
1447 PyString_AS_STRING(baserepr
));
1449 Py_DECREF(baserepr
);
1454 defdict_traverse(PyObject
*self
, visitproc visit
, void *arg
)
1456 Py_VISIT(((defdictobject
*)self
)->default_factory
);
1457 return PyDict_Type
.tp_traverse(self
, visit
, arg
);
1461 defdict_tp_clear(defdictobject
*dd
)
1463 Py_CLEAR(dd
->default_factory
);
1464 return PyDict_Type
.tp_clear((PyObject
*)dd
);
1468 defdict_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1470 defdictobject
*dd
= (defdictobject
*)self
;
1471 PyObject
*olddefault
= dd
->default_factory
;
1472 PyObject
*newdefault
= NULL
;
1475 if (args
== NULL
|| !PyTuple_Check(args
))
1476 newargs
= PyTuple_New(0);
1478 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1480 newdefault
= PyTuple_GET_ITEM(args
, 0);
1481 if (!PyCallable_Check(newdefault
) && newdefault
!= Py_None
) {
1482 PyErr_SetString(PyExc_TypeError
,
1483 "first argument must be callable");
1487 newargs
= PySequence_GetSlice(args
, 1, n
);
1489 if (newargs
== NULL
)
1491 Py_XINCREF(newdefault
);
1492 dd
->default_factory
= newdefault
;
1493 result
= PyDict_Type
.tp_init(self
, newargs
, kwds
);
1495 Py_XDECREF(olddefault
);
1499 PyDoc_STRVAR(defdict_doc
,
1500 "defaultdict(default_factory) --> dict with default factory\n\
1502 The default factory is called without arguments to produce\n\
1503 a new value when a key is not present, in __getitem__ only.\n\
1504 A defaultdict compares equal to a dict with the same items.\n\
1507 /* See comment in xxsubtype.c */
1508 #define DEFERRED_ADDRESS(ADDR) 0
1510 static PyTypeObject defdict_type
= {
1511 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
), 0)
1512 "collections.defaultdict", /* tp_name */
1513 sizeof(defdictobject
), /* tp_basicsize */
1514 0, /* tp_itemsize */
1516 (destructor
)defdict_dealloc
, /* tp_dealloc */
1517 (printfunc
)defdict_print
, /* tp_print */
1521 (reprfunc
)defdict_repr
, /* tp_repr */
1522 0, /* tp_as_number */
1523 0, /* tp_as_sequence */
1524 0, /* tp_as_mapping */
1528 PyObject_GenericGetAttr
, /* tp_getattro */
1529 0, /* tp_setattro */
1530 0, /* tp_as_buffer */
1531 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
|
1532 Py_TPFLAGS_HAVE_WEAKREFS
, /* tp_flags */
1533 defdict_doc
, /* tp_doc */
1534 defdict_traverse
, /* tp_traverse */
1535 (inquiry
)defdict_tp_clear
, /* tp_clear */
1536 0, /* tp_richcompare */
1537 0, /* tp_weaklistoffset*/
1539 0, /* tp_iternext */
1540 defdict_methods
, /* tp_methods */
1541 defdict_members
, /* tp_members */
1543 DEFERRED_ADDRESS(&PyDict_Type
), /* tp_base */
1545 0, /* tp_descr_get */
1546 0, /* tp_descr_set */
1547 0, /* tp_dictoffset */
1548 defdict_init
, /* tp_init */
1549 PyType_GenericAlloc
, /* tp_alloc */
1551 PyObject_GC_Del
, /* tp_free */
1554 /* module level code ********************************************************/
1556 PyDoc_STRVAR(module_doc
,
1557 "High performance data structures.\n\
1558 - deque: ordered collection accessible from endpoints only\n\
1559 - defaultdict: dict subclass with a default value factory\n\
1563 init_collections(void)
1567 m
= Py_InitModule3("_collections", NULL
, module_doc
);
1571 if (PyType_Ready(&deque_type
) < 0)
1573 Py_INCREF(&deque_type
);
1574 PyModule_AddObject(m
, "deque", (PyObject
*)&deque_type
);
1576 defdict_type
.tp_base
= &PyDict_Type
;
1577 if (PyType_Ready(&defdict_type
) < 0)
1579 Py_INCREF(&defdict_type
);
1580 PyModule_AddObject(m
, "defaultdict", (PyObject
*)&defdict_type
);
1582 if (PyType_Ready(&dequeiter_type
) < 0)
1585 if (PyType_Ready(&dequereviter_type
) < 0)