ยปCore Development>Code coverage>Modules/_collectionsmodule.c

Python code coverage for Modules/_collectionsmodule.c

#countcontent
1n/a#include "Python.h"
2n/a#include "structmember.h"
3n/a
4n/a#ifdef STDC_HEADERS
5n/a#include <stddef.h>
6n/a#else
7n/a#include <sys/types.h> /* For size_t */
8n/a#endif
9n/a
10n/a/* collections module implementation of a deque() datatype
11n/a Written and maintained by Raymond D. Hettinger <python@rcn.com>
12n/a*/
13n/a
14n/a/* The block length may be set to any number over 1. Larger numbers
15n/a * reduce the number of calls to the memory allocator, give faster
16n/a * indexing and rotation, and reduce the link to data overhead ratio.
17n/a * Making the block length a power of two speeds-up the modulo
18n/a * and division calculations in deque_item() and deque_ass_item().
19n/a */
20n/a
21n/a#define BLOCKLEN 64
22n/a#define CENTER ((BLOCKLEN - 1) / 2)
23n/a
24n/a/* Data for deque objects is stored in a doubly-linked list of fixed
25n/a * length blocks. This assures that appends or pops never move any
26n/a * other data elements besides the one being appended or popped.
27n/a *
28n/a * Another advantage is that it completely avoids use of realloc(),
29n/a * resulting in more predictable performance.
30n/a *
31n/a * Textbook implementations of doubly-linked lists store one datum
32n/a * per link, but that gives them a 200% memory overhead (a prev and
33n/a * next link for each datum) and it costs one malloc() call per data
34n/a * element. By using fixed-length blocks, the link to data ratio is
35n/a * significantly improved and there are proportionally fewer calls
36n/a * to malloc() and free(). The data blocks of consecutive pointers
37n/a * also improve cache locality.
38n/a *
39n/a * The list of blocks is never empty, so d.leftblock and d.rightblock
40n/a * are never equal to NULL. The list is not circular.
41n/a *
42n/a * A deque d's first element is at d.leftblock[leftindex]
43n/a * and its last element is at d.rightblock[rightindex].
44n/a *
45n/a * Unlike Python slice indices, these indices are inclusive on both
46n/a * ends. This makes the algorithms for left and right operations
47n/a * more symmetrical and it simplifies the design.
48n/a *
49n/a * The indices, d.leftindex and d.rightindex are always in the range:
50n/a * 0 <= index < BLOCKLEN
51n/a *
52n/a * And their exact relationship is:
53n/a * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex
54n/a *
55n/a * Whenever d.leftblock == d.rightblock, then:
56n/a * d.leftindex + d.len - 1 == d.rightindex
57n/a *
58n/a * However, when d.leftblock != d.rightblock, the d.leftindex and
59n/a * d.rightindex become indices into distinct blocks and either may
60n/a * be larger than the other.
61n/a *
62n/a * Empty deques have:
63n/a * d.len == 0
64n/a * d.leftblock == d.rightblock
65n/a * d.leftindex == CENTER + 1
66n/a * d.rightindex == CENTER
67n/a *
68n/a * Checking for d.len == 0 is the intended way to see whether d is empty.
69n/a */
70n/a
71n/atypedef struct BLOCK {
72n/a struct BLOCK *leftlink;
73n/a PyObject *data[BLOCKLEN];
74n/a struct BLOCK *rightlink;
75n/a} block;
76n/a
77n/atypedef struct {
78n/a PyObject_VAR_HEAD
79n/a block *leftblock;
80n/a block *rightblock;
81n/a Py_ssize_t leftindex; /* 0 <= leftindex < BLOCKLEN */
82n/a Py_ssize_t rightindex; /* 0 <= rightindex < BLOCKLEN */
83n/a size_t state; /* incremented whenever the indices move */
84n/a Py_ssize_t maxlen; /* maxlen is -1 for unbounded deques */
85n/a PyObject *weakreflist;
86n/a} dequeobject;
87n/a
88n/astatic PyTypeObject deque_type;
89n/a
90n/a/* For debug builds, add error checking to track the endpoints
91n/a * in the chain of links. The goal is to make sure that link
92n/a * assignments only take place at endpoints so that links already
93n/a * in use do not get overwritten.
94n/a *
95n/a * CHECK_END should happen before each assignment to a block's link field.
96n/a * MARK_END should happen whenever a link field becomes a new endpoint.
97n/a * This happens when new blocks are added or whenever an existing
98n/a * block is freed leaving another existing block as the new endpoint.
99n/a */
100n/a
101n/a#ifndef NDEBUG
102n/a#define MARK_END(link) link = NULL;
103n/a#define CHECK_END(link) assert(link == NULL);
104n/a#define CHECK_NOT_END(link) assert(link != NULL);
105n/a#else
106n/a#define MARK_END(link)
107n/a#define CHECK_END(link)
108n/a#define CHECK_NOT_END(link)
109n/a#endif
110n/a
111n/a/* A simple freelisting scheme is used to minimize calls to the memory
112n/a allocator. It accommodates common use cases where new blocks are being
113n/a added at about the same rate as old blocks are being freed.
114n/a */
115n/a
116n/a#define MAXFREEBLOCKS 16
117n/astatic Py_ssize_t numfreeblocks = 0;
118n/astatic block *freeblocks[MAXFREEBLOCKS];
119n/a
120n/astatic block *
121n/anewblock(void) {
122n/a block *b;
123n/a if (numfreeblocks) {
124n/a numfreeblocks--;
125n/a return freeblocks[numfreeblocks];
126n/a }
127n/a b = PyMem_Malloc(sizeof(block));
128n/a if (b != NULL) {
129n/a return b;
130n/a }
131n/a PyErr_NoMemory();
132n/a return NULL;
133n/a}
134n/a
135n/astatic void
136n/afreeblock(block *b)
137n/a{
138n/a if (numfreeblocks < MAXFREEBLOCKS) {
139n/a freeblocks[numfreeblocks] = b;
140n/a numfreeblocks++;
141n/a } else {
142n/a PyMem_Free(b);
143n/a }
144n/a}
145n/a
146n/astatic PyObject *
147n/adeque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
148n/a{
149n/a dequeobject *deque;
150n/a block *b;
151n/a
152n/a /* create dequeobject structure */
153n/a deque = (dequeobject *)type->tp_alloc(type, 0);
154n/a if (deque == NULL)
155n/a return NULL;
156n/a
157n/a b = newblock();
158n/a if (b == NULL) {
159n/a Py_DECREF(deque);
160n/a return NULL;
161n/a }
162n/a MARK_END(b->leftlink);
163n/a MARK_END(b->rightlink);
164n/a
165n/a assert(BLOCKLEN >= 2);
166n/a Py_SIZE(deque) = 0;
167n/a deque->leftblock = b;
168n/a deque->rightblock = b;
169n/a deque->leftindex = CENTER + 1;
170n/a deque->rightindex = CENTER;
171n/a deque->state = 0;
172n/a deque->maxlen = -1;
173n/a deque->weakreflist = NULL;
174n/a
175n/a return (PyObject *)deque;
176n/a}
177n/a
178n/astatic PyObject *
179n/adeque_pop(dequeobject *deque, PyObject *unused)
180n/a{
181n/a PyObject *item;
182n/a block *prevblock;
183n/a
184n/a if (Py_SIZE(deque) == 0) {
185n/a PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
186n/a return NULL;
187n/a }
188n/a item = deque->rightblock->data[deque->rightindex];
189n/a deque->rightindex--;
190n/a Py_SIZE(deque)--;
191n/a deque->state++;
192n/a
193n/a if (deque->rightindex < 0) {
194n/a if (Py_SIZE(deque)) {
195n/a prevblock = deque->rightblock->leftlink;
196n/a assert(deque->leftblock != deque->rightblock);
197n/a freeblock(deque->rightblock);
198n/a CHECK_NOT_END(prevblock);
199n/a MARK_END(prevblock->rightlink);
200n/a deque->rightblock = prevblock;
201n/a deque->rightindex = BLOCKLEN - 1;
202n/a } else {
203n/a assert(deque->leftblock == deque->rightblock);
204n/a assert(deque->leftindex == deque->rightindex+1);
205n/a /* re-center instead of freeing a block */
206n/a deque->leftindex = CENTER + 1;
207n/a deque->rightindex = CENTER;
208n/a }
209n/a }
210n/a return item;
211n/a}
212n/a
213n/aPyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
214n/a
215n/astatic PyObject *
216n/adeque_popleft(dequeobject *deque, PyObject *unused)
217n/a{
218n/a PyObject *item;
219n/a block *prevblock;
220n/a
221n/a if (Py_SIZE(deque) == 0) {
222n/a PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
223n/a return NULL;
224n/a }
225n/a assert(deque->leftblock != NULL);
226n/a item = deque->leftblock->data[deque->leftindex];
227n/a deque->leftindex++;
228n/a Py_SIZE(deque)--;
229n/a deque->state++;
230n/a
231n/a if (deque->leftindex == BLOCKLEN) {
232n/a if (Py_SIZE(deque)) {
233n/a assert(deque->leftblock != deque->rightblock);
234n/a prevblock = deque->leftblock->rightlink;
235n/a freeblock(deque->leftblock);
236n/a CHECK_NOT_END(prevblock);
237n/a MARK_END(prevblock->leftlink);
238n/a deque->leftblock = prevblock;
239n/a deque->leftindex = 0;
240n/a } else {
241n/a assert(deque->leftblock == deque->rightblock);
242n/a assert(deque->leftindex == deque->rightindex+1);
243n/a /* re-center instead of freeing a block */
244n/a deque->leftindex = CENTER + 1;
245n/a deque->rightindex = CENTER;
246n/a }
247n/a }
248n/a return item;
249n/a}
250n/a
251n/aPyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
252n/a
253n/a/* The deque's size limit is d.maxlen. The limit can be zero or positive.
254n/a * If there is no limit, then d.maxlen == -1.
255n/a *
256n/a * After an item is added to a deque, we check to see if the size has
257n/a * grown past the limit. If it has, we get the size back down to the limit
258n/a * by popping an item off of the opposite end. The methods that can
259n/a * trigger this are append(), appendleft(), extend(), and extendleft().
260n/a *
261n/a * The macro to check whether a deque needs to be trimmed uses a single
262n/a * unsigned test that returns true whenever 0 <= maxlen < Py_SIZE(deque).
263n/a */
264n/a
265n/a#define NEEDS_TRIM(deque, maxlen) ((size_t)(maxlen) < (size_t)(Py_SIZE(deque)))
266n/a
267n/astatic int
268n/adeque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
269n/a{
270n/a if (deque->rightindex == BLOCKLEN - 1) {
271n/a block *b = newblock();
272n/a if (b == NULL)
273n/a return -1;
274n/a b->leftlink = deque->rightblock;
275n/a CHECK_END(deque->rightblock->rightlink);
276n/a deque->rightblock->rightlink = b;
277n/a deque->rightblock = b;
278n/a MARK_END(b->rightlink);
279n/a deque->rightindex = -1;
280n/a }
281n/a Py_SIZE(deque)++;
282n/a deque->rightindex++;
283n/a deque->rightblock->data[deque->rightindex] = item;
284n/a if (NEEDS_TRIM(deque, maxlen)) {
285n/a PyObject *olditem = deque_popleft(deque, NULL);
286n/a Py_DECREF(olditem);
287n/a } else {
288n/a deque->state++;
289n/a }
290n/a return 0;
291n/a}
292n/a
293n/astatic PyObject *
294n/adeque_append(dequeobject *deque, PyObject *item)
295n/a{
296n/a Py_INCREF(item);
297n/a if (deque_append_internal(deque, item, deque->maxlen) < 0)
298n/a return NULL;
299n/a Py_RETURN_NONE;
300n/a}
301n/a
302n/aPyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
303n/a
304n/astatic int
305n/adeque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
306n/a{
307n/a if (deque->leftindex == 0) {
308n/a block *b = newblock();
309n/a if (b == NULL)
310n/a return -1;
311n/a b->rightlink = deque->leftblock;
312n/a CHECK_END(deque->leftblock->leftlink);
313n/a deque->leftblock->leftlink = b;
314n/a deque->leftblock = b;
315n/a MARK_END(b->leftlink);
316n/a deque->leftindex = BLOCKLEN;
317n/a }
318n/a Py_SIZE(deque)++;
319n/a deque->leftindex--;
320n/a deque->leftblock->data[deque->leftindex] = item;
321n/a if (NEEDS_TRIM(deque, deque->maxlen)) {
322n/a PyObject *olditem = deque_pop(deque, NULL);
323n/a Py_DECREF(olditem);
324n/a } else {
325n/a deque->state++;
326n/a }
327n/a return 0;
328n/a}
329n/a
330n/astatic PyObject *
331n/adeque_appendleft(dequeobject *deque, PyObject *item)
332n/a{
333n/a Py_INCREF(item);
334n/a if (deque_appendleft_internal(deque, item, deque->maxlen) < 0)
335n/a return NULL;
336n/a Py_RETURN_NONE;
337n/a}
338n/a
339n/aPyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
340n/a
341n/astatic PyObject*
342n/afinalize_iterator(PyObject *it)
343n/a{
344n/a if (PyErr_Occurred()) {
345n/a if (PyErr_ExceptionMatches(PyExc_StopIteration))
346n/a PyErr_Clear();
347n/a else {
348n/a Py_DECREF(it);
349n/a return NULL;
350n/a }
351n/a }
352n/a Py_DECREF(it);
353n/a Py_RETURN_NONE;
354n/a}
355n/a
356n/a/* Run an iterator to exhaustion. Shortcut for
357n/a the extend/extendleft methods when maxlen == 0. */
358n/astatic PyObject*
359n/aconsume_iterator(PyObject *it)
360n/a{
361n/a PyObject *(*iternext)(PyObject *);
362n/a PyObject *item;
363n/a
364n/a iternext = *Py_TYPE(it)->tp_iternext;
365n/a while ((item = iternext(it)) != NULL) {
366n/a Py_DECREF(item);
367n/a }
368n/a return finalize_iterator(it);
369n/a}
370n/a
371n/astatic PyObject *
372n/adeque_extend(dequeobject *deque, PyObject *iterable)
373n/a{
374n/a PyObject *it, *item;
375n/a PyObject *(*iternext)(PyObject *);
376n/a Py_ssize_t maxlen = deque->maxlen;
377n/a
378n/a /* Handle case where id(deque) == id(iterable) */
379n/a if ((PyObject *)deque == iterable) {
380n/a PyObject *result;
381n/a PyObject *s = PySequence_List(iterable);
382n/a if (s == NULL)
383n/a return NULL;
384n/a result = deque_extend(deque, s);
385n/a Py_DECREF(s);
386n/a return result;
387n/a }
388n/a
389n/a it = PyObject_GetIter(iterable);
390n/a if (it == NULL)
391n/a return NULL;
392n/a
393n/a if (maxlen == 0)
394n/a return consume_iterator(it);
395n/a
396n/a /* Space saving heuristic. Start filling from the left */
397n/a if (Py_SIZE(deque) == 0) {
398n/a assert(deque->leftblock == deque->rightblock);
399n/a assert(deque->leftindex == deque->rightindex+1);
400n/a deque->leftindex = 1;
401n/a deque->rightindex = 0;
402n/a }
403n/a
404n/a iternext = *Py_TYPE(it)->tp_iternext;
405n/a while ((item = iternext(it)) != NULL) {
406n/a if (deque->rightindex == BLOCKLEN - 1) {
407n/a block *b = newblock();
408n/a if (b == NULL) {
409n/a Py_DECREF(item);
410n/a Py_DECREF(it);
411n/a return NULL;
412n/a }
413n/a b->leftlink = deque->rightblock;
414n/a CHECK_END(deque->rightblock->rightlink);
415n/a deque->rightblock->rightlink = b;
416n/a deque->rightblock = b;
417n/a MARK_END(b->rightlink);
418n/a deque->rightindex = -1;
419n/a }
420n/a Py_SIZE(deque)++;
421n/a deque->rightindex++;
422n/a deque->rightblock->data[deque->rightindex] = item;
423n/a if (NEEDS_TRIM(deque, maxlen)) {
424n/a PyObject *olditem = deque_popleft(deque, NULL);
425n/a Py_DECREF(olditem);
426n/a } else {
427n/a deque->state++;
428n/a }
429n/a }
430n/a return finalize_iterator(it);
431n/a}
432n/a
433n/aPyDoc_STRVAR(extend_doc,
434n/a"Extend the right side of the deque with elements from the iterable");
435n/a
436n/astatic PyObject *
437n/adeque_extendleft(dequeobject *deque, PyObject *iterable)
438n/a{
439n/a PyObject *it, *item;
440n/a PyObject *(*iternext)(PyObject *);
441n/a Py_ssize_t maxlen = deque->maxlen;
442n/a
443n/a /* Handle case where id(deque) == id(iterable) */
444n/a if ((PyObject *)deque == iterable) {
445n/a PyObject *result;
446n/a PyObject *s = PySequence_List(iterable);
447n/a if (s == NULL)
448n/a return NULL;
449n/a result = deque_extendleft(deque, s);
450n/a Py_DECREF(s);
451n/a return result;
452n/a }
453n/a
454n/a it = PyObject_GetIter(iterable);
455n/a if (it == NULL)
456n/a return NULL;
457n/a
458n/a if (maxlen == 0)
459n/a return consume_iterator(it);
460n/a
461n/a /* Space saving heuristic. Start filling from the right */
462n/a if (Py_SIZE(deque) == 0) {
463n/a assert(deque->leftblock == deque->rightblock);
464n/a assert(deque->leftindex == deque->rightindex+1);
465n/a deque->leftindex = BLOCKLEN - 1;
466n/a deque->rightindex = BLOCKLEN - 2;
467n/a }
468n/a
469n/a iternext = *Py_TYPE(it)->tp_iternext;
470n/a while ((item = iternext(it)) != NULL) {
471n/a if (deque->leftindex == 0) {
472n/a block *b = newblock();
473n/a if (b == NULL) {
474n/a Py_DECREF(item);
475n/a Py_DECREF(it);
476n/a return NULL;
477n/a }
478n/a b->rightlink = deque->leftblock;
479n/a CHECK_END(deque->leftblock->leftlink);
480n/a deque->leftblock->leftlink = b;
481n/a deque->leftblock = b;
482n/a MARK_END(b->leftlink);
483n/a deque->leftindex = BLOCKLEN;
484n/a }
485n/a Py_SIZE(deque)++;
486n/a deque->leftindex--;
487n/a deque->leftblock->data[deque->leftindex] = item;
488n/a if (NEEDS_TRIM(deque, maxlen)) {
489n/a PyObject *olditem = deque_pop(deque, NULL);
490n/a Py_DECREF(olditem);
491n/a } else {
492n/a deque->state++;
493n/a }
494n/a }
495n/a return finalize_iterator(it);
496n/a}
497n/a
498n/aPyDoc_STRVAR(extendleft_doc,
499n/a"Extend the left side of the deque with elements from the iterable");
500n/a
501n/astatic PyObject *
502n/adeque_inplace_concat(dequeobject *deque, PyObject *other)
503n/a{
504n/a PyObject *result;
505n/a
506n/a result = deque_extend(deque, other);
507n/a if (result == NULL)
508n/a return result;
509n/a Py_INCREF(deque);
510n/a Py_DECREF(result);
511n/a return (PyObject *)deque;
512n/a}
513n/a
514n/astatic PyObject *
515n/adeque_copy(PyObject *deque)
516n/a{
517n/a dequeobject *old_deque = (dequeobject *)deque;
518n/a if (Py_TYPE(deque) == &deque_type) {
519n/a dequeobject *new_deque;
520n/a PyObject *rv;
521n/a
522n/a new_deque = (dequeobject *)deque_new(&deque_type, (PyObject *)NULL, (PyObject *)NULL);
523n/a if (new_deque == NULL)
524n/a return NULL;
525n/a new_deque->maxlen = old_deque->maxlen;
526n/a /* Fast path for the deque_repeat() common case where len(deque) == 1 */
527n/a if (Py_SIZE(deque) == 1) {
528n/a PyObject *item = old_deque->leftblock->data[old_deque->leftindex];
529n/a rv = deque_append(new_deque, item);
530n/a } else {
531n/a rv = deque_extend(new_deque, deque);
532n/a }
533n/a if (rv != NULL) {
534n/a Py_DECREF(rv);
535n/a return (PyObject *)new_deque;
536n/a }
537n/a Py_DECREF(new_deque);
538n/a return NULL;
539n/a }
540n/a if (old_deque->maxlen < 0)
541n/a return PyObject_CallFunctionObjArgs((PyObject *)(Py_TYPE(deque)),
542n/a deque, NULL);
543n/a else
544n/a return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
545n/a deque, old_deque->maxlen, NULL);
546n/a}
547n/a
548n/aPyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
549n/a
550n/astatic PyObject *
551n/adeque_concat(dequeobject *deque, PyObject *other)
552n/a{
553n/a PyObject *new_deque, *result;
554n/a int rv;
555n/a
556n/a rv = PyObject_IsInstance(other, (PyObject *)&deque_type);
557n/a if (rv <= 0) {
558n/a if (rv == 0) {
559n/a PyErr_Format(PyExc_TypeError,
560n/a "can only concatenate deque (not \"%.200s\") to deque",
561n/a other->ob_type->tp_name);
562n/a }
563n/a return NULL;
564n/a }
565n/a
566n/a new_deque = deque_copy((PyObject *)deque);
567n/a if (new_deque == NULL)
568n/a return NULL;
569n/a result = deque_extend((dequeobject *)new_deque, other);
570n/a if (result == NULL) {
571n/a Py_DECREF(new_deque);
572n/a return NULL;
573n/a }
574n/a Py_DECREF(result);
575n/a return new_deque;
576n/a}
577n/a
578n/astatic void
579n/adeque_clear(dequeobject *deque)
580n/a{
581n/a block *b;
582n/a block *prevblock;
583n/a block *leftblock;
584n/a Py_ssize_t leftindex;
585n/a Py_ssize_t n, m;
586n/a PyObject *item;
587n/a PyObject **itemptr, **limit;
588n/a
589n/a if (Py_SIZE(deque) == 0)
590n/a return;
591n/a
592n/a /* During the process of clearing a deque, decrefs can cause the
593n/a deque to mutate. To avoid fatal confusion, we have to make the
594n/a deque empty before clearing the blocks and never refer to
595n/a anything via deque->ref while clearing. (This is the same
596n/a technique used for clearing lists, sets, and dicts.)
597n/a
598n/a Making the deque empty requires allocating a new empty block. In
599n/a the unlikely event that memory is full, we fall back to an
600n/a alternate method that doesn't require a new block. Repeating
601n/a pops in a while-loop is slower, possibly re-entrant (and a clever
602n/a adversary could cause it to never terminate).
603n/a */
604n/a
605n/a b = newblock();
606n/a if (b == NULL) {
607n/a PyErr_Clear();
608n/a goto alternate_method;
609n/a }
610n/a
611n/a /* Remember the old size, leftblock, and leftindex */
612n/a n = Py_SIZE(deque);
613n/a leftblock = deque->leftblock;
614n/a leftindex = deque->leftindex;
615n/a
616n/a /* Set the deque to be empty using the newly allocated block */
617n/a MARK_END(b->leftlink);
618n/a MARK_END(b->rightlink);
619n/a Py_SIZE(deque) = 0;
620n/a deque->leftblock = b;
621n/a deque->rightblock = b;
622n/a deque->leftindex = CENTER + 1;
623n/a deque->rightindex = CENTER;
624n/a deque->state++;
625n/a
626n/a /* Now the old size, leftblock, and leftindex are disconnected from
627n/a the empty deque and we can use them to decref the pointers.
628n/a */
629n/a m = (BLOCKLEN - leftindex > n) ? n : BLOCKLEN - leftindex;
630n/a itemptr = &leftblock->data[leftindex];
631n/a limit = itemptr + m;
632n/a n -= m;
633n/a while (1) {
634n/a if (itemptr == limit) {
635n/a if (n == 0)
636n/a break;
637n/a CHECK_NOT_END(leftblock->rightlink);
638n/a prevblock = leftblock;
639n/a leftblock = leftblock->rightlink;
640n/a m = (n > BLOCKLEN) ? BLOCKLEN : n;
641n/a itemptr = leftblock->data;
642n/a limit = itemptr + m;
643n/a n -= m;
644n/a freeblock(prevblock);
645n/a }
646n/a item = *(itemptr++);
647n/a Py_DECREF(item);
648n/a }
649n/a CHECK_END(leftblock->rightlink);
650n/a freeblock(leftblock);
651n/a return;
652n/a
653n/a alternate_method:
654n/a while (Py_SIZE(deque)) {
655n/a item = deque_pop(deque, NULL);
656n/a assert (item != NULL);
657n/a Py_DECREF(item);
658n/a }
659n/a}
660n/a
661n/astatic PyObject *
662n/adeque_clearmethod(dequeobject *deque)
663n/a{
664n/a deque_clear(deque);
665n/a Py_RETURN_NONE;
666n/a}
667n/a
668n/aPyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
669n/a
670n/astatic PyObject *
671n/adeque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
672n/a{
673n/a Py_ssize_t i, m, size;
674n/a PyObject *seq;
675n/a PyObject *rv;
676n/a
677n/a size = Py_SIZE(deque);
678n/a if (size == 0 || n == 1) {
679n/a Py_INCREF(deque);
680n/a return (PyObject *)deque;
681n/a }
682n/a
683n/a if (n <= 0) {
684n/a deque_clear(deque);
685n/a Py_INCREF(deque);
686n/a return (PyObject *)deque;
687n/a }
688n/a
689n/a if (size == 1) {
690n/a /* common case, repeating a single element */
691n/a PyObject *item = deque->leftblock->data[deque->leftindex];
692n/a
693n/a if (deque->maxlen >= 0 && n > deque->maxlen)
694n/a n = deque->maxlen;
695n/a
696n/a deque->state++;
697n/a for (i = 0 ; i < n-1 ; ) {
698n/a if (deque->rightindex == BLOCKLEN - 1) {
699n/a block *b = newblock();
700n/a if (b == NULL) {
701n/a Py_SIZE(deque) += i;
702n/a return NULL;
703n/a }
704n/a b->leftlink = deque->rightblock;
705n/a CHECK_END(deque->rightblock->rightlink);
706n/a deque->rightblock->rightlink = b;
707n/a deque->rightblock = b;
708n/a MARK_END(b->rightlink);
709n/a deque->rightindex = -1;
710n/a }
711n/a m = n - 1 - i;
712n/a if (m > BLOCKLEN - 1 - deque->rightindex)
713n/a m = BLOCKLEN - 1 - deque->rightindex;
714n/a i += m;
715n/a while (m--) {
716n/a deque->rightindex++;
717n/a Py_INCREF(item);
718n/a deque->rightblock->data[deque->rightindex] = item;
719n/a }
720n/a }
721n/a Py_SIZE(deque) += i;
722n/a Py_INCREF(deque);
723n/a return (PyObject *)deque;
724n/a }
725n/a
726n/a if ((size_t)size > PY_SSIZE_T_MAX / (size_t)n) {
727n/a return PyErr_NoMemory();
728n/a }
729n/a
730n/a seq = PySequence_List((PyObject *)deque);
731n/a if (seq == NULL)
732n/a return seq;
733n/a
734n/a for (i = 0 ; i < n-1 ; i++) {
735n/a rv = deque_extend(deque, seq);
736n/a if (rv == NULL) {
737n/a Py_DECREF(seq);
738n/a return NULL;
739n/a }
740n/a Py_DECREF(rv);
741n/a }
742n/a Py_INCREF(deque);
743n/a Py_DECREF(seq);
744n/a return (PyObject *)deque;
745n/a}
746n/a
747n/astatic PyObject *
748n/adeque_repeat(dequeobject *deque, Py_ssize_t n)
749n/a{
750n/a dequeobject *new_deque;
751n/a PyObject *rv;
752n/a
753n/a new_deque = (dequeobject *)deque_copy((PyObject *) deque);
754n/a if (new_deque == NULL)
755n/a return NULL;
756n/a rv = deque_inplace_repeat(new_deque, n);
757n/a Py_DECREF(new_deque);
758n/a return rv;
759n/a}
760n/a
761n/a/* The rotate() method is part of the public API and is used internally
762n/aas a primitive for other methods.
763n/a
764n/aRotation by 1 or -1 is a common case, so any optimizations for high
765n/avolume rotations should take care not to penalize the common case.
766n/a
767n/aConceptually, a rotate by one is equivalent to a pop on one side and an
768n/aappend on the other. However, a pop/append pair is unnecessarily slow
769n/abecause it requires an incref/decref pair for an object located randomly
770n/ain memory. It is better to just move the object pointer from one block
771n/ato the next without changing the reference count.
772n/a
773n/aWhen moving batches of pointers, it is tempting to use memcpy() but that
774n/aproved to be slower than a simple loop for a variety of reasons.
775n/aMemcpy() cannot know in advance that we're copying pointers instead of
776n/abytes, that the source and destination are pointer aligned and
777n/anon-overlapping, that moving just one pointer is a common case, that we
778n/anever need to move more than BLOCKLEN pointers, and that at least one
779n/apointer is always moved.
780n/a
781n/aFor high volume rotations, newblock() and freeblock() are never called
782n/amore than once. Previously emptied blocks are immediately reused as a
783n/adestination block. If a block is left-over at the end, it is freed.
784n/a*/
785n/a
786n/astatic int
787n/a_deque_rotate(dequeobject *deque, Py_ssize_t n)
788n/a{
789n/a block *b = NULL;
790n/a block *leftblock = deque->leftblock;
791n/a block *rightblock = deque->rightblock;
792n/a Py_ssize_t leftindex = deque->leftindex;
793n/a Py_ssize_t rightindex = deque->rightindex;
794n/a Py_ssize_t len=Py_SIZE(deque), halflen=len>>1;
795n/a int rv = -1;
796n/a
797n/a if (len <= 1)
798n/a return 0;
799n/a if (n > halflen || n < -halflen) {
800n/a n %= len;
801n/a if (n > halflen)
802n/a n -= len;
803n/a else if (n < -halflen)
804n/a n += len;
805n/a }
806n/a assert(len > 1);
807n/a assert(-halflen <= n && n <= halflen);
808n/a
809n/a deque->state++;
810n/a while (n > 0) {
811n/a if (leftindex == 0) {
812n/a if (b == NULL) {
813n/a b = newblock();
814n/a if (b == NULL)
815n/a goto done;
816n/a }
817n/a b->rightlink = leftblock;
818n/a CHECK_END(leftblock->leftlink);
819n/a leftblock->leftlink = b;
820n/a leftblock = b;
821n/a MARK_END(b->leftlink);
822n/a leftindex = BLOCKLEN;
823n/a b = NULL;
824n/a }
825n/a assert(leftindex > 0);
826n/a {
827n/a PyObject **src, **dest;
828n/a Py_ssize_t m = n;
829n/a
830n/a if (m > rightindex + 1)
831n/a m = rightindex + 1;
832n/a if (m > leftindex)
833n/a m = leftindex;
834n/a assert (m > 0 && m <= len);
835n/a rightindex -= m;
836n/a leftindex -= m;
837n/a src = &rightblock->data[rightindex + 1];
838n/a dest = &leftblock->data[leftindex];
839n/a n -= m;
840n/a do {
841n/a *(dest++) = *(src++);
842n/a } while (--m);
843n/a }
844n/a if (rightindex < 0) {
845n/a assert(leftblock != rightblock);
846n/a assert(b == NULL);
847n/a b = rightblock;
848n/a CHECK_NOT_END(rightblock->leftlink);
849n/a rightblock = rightblock->leftlink;
850n/a MARK_END(rightblock->rightlink);
851n/a rightindex = BLOCKLEN - 1;
852n/a }
853n/a }
854n/a while (n < 0) {
855n/a if (rightindex == BLOCKLEN - 1) {
856n/a if (b == NULL) {
857n/a b = newblock();
858n/a if (b == NULL)
859n/a goto done;
860n/a }
861n/a b->leftlink = rightblock;
862n/a CHECK_END(rightblock->rightlink);
863n/a rightblock->rightlink = b;
864n/a rightblock = b;
865n/a MARK_END(b->rightlink);
866n/a rightindex = -1;
867n/a b = NULL;
868n/a }
869n/a assert (rightindex < BLOCKLEN - 1);
870n/a {
871n/a PyObject **src, **dest;
872n/a Py_ssize_t m = -n;
873n/a
874n/a if (m > BLOCKLEN - leftindex)
875n/a m = BLOCKLEN - leftindex;
876n/a if (m > BLOCKLEN - 1 - rightindex)
877n/a m = BLOCKLEN - 1 - rightindex;
878n/a assert (m > 0 && m <= len);
879n/a src = &leftblock->data[leftindex];
880n/a dest = &rightblock->data[rightindex + 1];
881n/a leftindex += m;
882n/a rightindex += m;
883n/a n += m;
884n/a do {
885n/a *(dest++) = *(src++);
886n/a } while (--m);
887n/a }
888n/a if (leftindex == BLOCKLEN) {
889n/a assert(leftblock != rightblock);
890n/a assert(b == NULL);
891n/a b = leftblock;
892n/a CHECK_NOT_END(leftblock->rightlink);
893n/a leftblock = leftblock->rightlink;
894n/a MARK_END(leftblock->leftlink);
895n/a leftindex = 0;
896n/a }
897n/a }
898n/a rv = 0;
899n/adone:
900n/a if (b != NULL)
901n/a freeblock(b);
902n/a deque->leftblock = leftblock;
903n/a deque->rightblock = rightblock;
904n/a deque->leftindex = leftindex;
905n/a deque->rightindex = rightindex;
906n/a
907n/a return rv;
908n/a}
909n/a
910n/astatic PyObject *
911n/adeque_rotate(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
912n/a PyObject *kwnames)
913n/a{
914n/a Py_ssize_t n=1;
915n/a
916n/a if (!_PyArg_NoStackKeywords("rotate", kwnames)) {
917n/a return NULL;
918n/a }
919n/a if (!_PyArg_ParseStack(args, nargs, "|n:rotate", &n)) {
920n/a return NULL;
921n/a }
922n/a
923n/a if (!_deque_rotate(deque, n))
924n/a Py_RETURN_NONE;
925n/a return NULL;
926n/a}
927n/a
928n/aPyDoc_STRVAR(rotate_doc,
929n/a"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
930n/a
931n/astatic PyObject *
932n/adeque_reverse(dequeobject *deque, PyObject *unused)
933n/a{
934n/a block *leftblock = deque->leftblock;
935n/a block *rightblock = deque->rightblock;
936n/a Py_ssize_t leftindex = deque->leftindex;
937n/a Py_ssize_t rightindex = deque->rightindex;
938n/a Py_ssize_t n = Py_SIZE(deque) >> 1;
939n/a PyObject *tmp;
940n/a
941n/a n++;
942n/a while (--n) {
943n/a /* Validate that pointers haven't met in the middle */
944n/a assert(leftblock != rightblock || leftindex < rightindex);
945n/a CHECK_NOT_END(leftblock);
946n/a CHECK_NOT_END(rightblock);
947n/a
948n/a /* Swap */
949n/a tmp = leftblock->data[leftindex];
950n/a leftblock->data[leftindex] = rightblock->data[rightindex];
951n/a rightblock->data[rightindex] = tmp;
952n/a
953n/a /* Advance left block/index pair */
954n/a leftindex++;
955n/a if (leftindex == BLOCKLEN) {
956n/a leftblock = leftblock->rightlink;
957n/a leftindex = 0;
958n/a }
959n/a
960n/a /* Step backwards with the right block/index pair */
961n/a rightindex--;
962n/a if (rightindex < 0) {
963n/a rightblock = rightblock->leftlink;
964n/a rightindex = BLOCKLEN - 1;
965n/a }
966n/a }
967n/a Py_RETURN_NONE;
968n/a}
969n/a
970n/aPyDoc_STRVAR(reverse_doc,
971n/a"D.reverse() -- reverse *IN PLACE*");
972n/a
973n/astatic PyObject *
974n/adeque_count(dequeobject *deque, PyObject *v)
975n/a{
976n/a block *b = deque->leftblock;
977n/a Py_ssize_t index = deque->leftindex;
978n/a Py_ssize_t n = Py_SIZE(deque);
979n/a Py_ssize_t count = 0;
980n/a size_t start_state = deque->state;
981n/a PyObject *item;
982n/a int cmp;
983n/a
984n/a n++;
985n/a while (--n) {
986n/a CHECK_NOT_END(b);
987n/a item = b->data[index];
988n/a cmp = PyObject_RichCompareBool(item, v, Py_EQ);
989n/a if (cmp < 0)
990n/a return NULL;
991n/a count += cmp;
992n/a
993n/a if (start_state != deque->state) {
994n/a PyErr_SetString(PyExc_RuntimeError,
995n/a "deque mutated during iteration");
996n/a return NULL;
997n/a }
998n/a
999n/a /* Advance left block/index pair */
1000n/a index++;
1001n/a if (index == BLOCKLEN) {
1002n/a b = b->rightlink;
1003n/a index = 0;
1004n/a }
1005n/a }
1006n/a return PyLong_FromSsize_t(count);
1007n/a}
1008n/a
1009n/aPyDoc_STRVAR(count_doc,
1010n/a"D.count(value) -> integer -- return number of occurrences of value");
1011n/a
1012n/astatic int
1013n/adeque_contains(dequeobject *deque, PyObject *v)
1014n/a{
1015n/a block *b = deque->leftblock;
1016n/a Py_ssize_t index = deque->leftindex;
1017n/a Py_ssize_t n = Py_SIZE(deque);
1018n/a size_t start_state = deque->state;
1019n/a PyObject *item;
1020n/a int cmp;
1021n/a
1022n/a n++;
1023n/a while (--n) {
1024n/a CHECK_NOT_END(b);
1025n/a item = b->data[index];
1026n/a cmp = PyObject_RichCompareBool(item, v, Py_EQ);
1027n/a if (cmp) {
1028n/a return cmp;
1029n/a }
1030n/a if (start_state != deque->state) {
1031n/a PyErr_SetString(PyExc_RuntimeError,
1032n/a "deque mutated during iteration");
1033n/a return -1;
1034n/a }
1035n/a index++;
1036n/a if (index == BLOCKLEN) {
1037n/a b = b->rightlink;
1038n/a index = 0;
1039n/a }
1040n/a }
1041n/a return 0;
1042n/a}
1043n/a
1044n/astatic Py_ssize_t
1045n/adeque_len(dequeobject *deque)
1046n/a{
1047n/a return Py_SIZE(deque);
1048n/a}
1049n/a
1050n/astatic PyObject *
1051n/adeque_index(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1052n/a PyObject *kwnames)
1053n/a{
1054n/a Py_ssize_t i, n, start=0, stop=Py_SIZE(deque);
1055n/a PyObject *v, *item;
1056n/a block *b = deque->leftblock;
1057n/a Py_ssize_t index = deque->leftindex;
1058n/a size_t start_state = deque->state;
1059n/a int cmp;
1060n/a
1061n/a if (!_PyArg_NoStackKeywords("index", kwnames)) {
1062n/a return NULL;
1063n/a }
1064n/a if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index", &v,
1065n/a _PyEval_SliceIndex, &start,
1066n/a _PyEval_SliceIndex, &stop)) {
1067n/a return NULL;
1068n/a }
1069n/a
1070n/a if (start < 0) {
1071n/a start += Py_SIZE(deque);
1072n/a if (start < 0)
1073n/a start = 0;
1074n/a }
1075n/a if (stop < 0) {
1076n/a stop += Py_SIZE(deque);
1077n/a if (stop < 0)
1078n/a stop = 0;
1079n/a }
1080n/a if (stop > Py_SIZE(deque))
1081n/a stop = Py_SIZE(deque);
1082n/a if (start > stop)
1083n/a start = stop;
1084n/a assert(0 <= start && start <= stop && stop <= Py_SIZE(deque));
1085n/a
1086n/a /* XXX Replace this loop with faster code from deque_item() */
1087n/a for (i=0 ; i<start ; i++) {
1088n/a index++;
1089n/a if (index == BLOCKLEN) {
1090n/a b = b->rightlink;
1091n/a index = 0;
1092n/a }
1093n/a }
1094n/a
1095n/a n = stop - i + 1;
1096n/a while (--n) {
1097n/a CHECK_NOT_END(b);
1098n/a item = b->data[index];
1099n/a cmp = PyObject_RichCompareBool(item, v, Py_EQ);
1100n/a if (cmp > 0)
1101n/a return PyLong_FromSsize_t(stop - n);
1102n/a if (cmp < 0)
1103n/a return NULL;
1104n/a if (start_state != deque->state) {
1105n/a PyErr_SetString(PyExc_RuntimeError,
1106n/a "deque mutated during iteration");
1107n/a return NULL;
1108n/a }
1109n/a index++;
1110n/a if (index == BLOCKLEN) {
1111n/a b = b->rightlink;
1112n/a index = 0;
1113n/a }
1114n/a }
1115n/a PyErr_Format(PyExc_ValueError, "%R is not in deque", v);
1116n/a return NULL;
1117n/a}
1118n/a
1119n/aPyDoc_STRVAR(index_doc,
1120n/a"D.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
1121n/a"Raises ValueError if the value is not present.");
1122n/a
1123n/a/* insert(), remove(), and delitem() are implemented in terms of
1124n/a rotate() for simplicity and reasonable performance near the end
1125n/a points. If for some reason these methods become popular, it is not
1126n/a hard to re-implement this using direct data movement (similar to
1127n/a the code used in list slice assignments) and achieve a performance
1128n/a boost (by moving each pointer only once instead of twice).
1129n/a*/
1130n/a
1131n/astatic PyObject *
1132n/adeque_insert(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1133n/a PyObject *kwnames)
1134n/a{
1135n/a Py_ssize_t index;
1136n/a Py_ssize_t n = Py_SIZE(deque);
1137n/a PyObject *value;
1138n/a PyObject *rv;
1139n/a
1140n/a if (!_PyArg_NoStackKeywords("insert", kwnames)) {
1141n/a return NULL;
1142n/a }
1143n/a if (!_PyArg_ParseStack(args, nargs, "nO:insert", &index, &value)) {
1144n/a return NULL;
1145n/a }
1146n/a
1147n/a if (deque->maxlen == Py_SIZE(deque)) {
1148n/a PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
1149n/a return NULL;
1150n/a }
1151n/a if (index >= n)
1152n/a return deque_append(deque, value);
1153n/a if (index <= -n || index == 0)
1154n/a return deque_appendleft(deque, value);
1155n/a if (_deque_rotate(deque, -index))
1156n/a return NULL;
1157n/a if (index < 0)
1158n/a rv = deque_append(deque, value);
1159n/a else
1160n/a rv = deque_appendleft(deque, value);
1161n/a if (rv == NULL)
1162n/a return NULL;
1163n/a Py_DECREF(rv);
1164n/a if (_deque_rotate(deque, index))
1165n/a return NULL;
1166n/a Py_RETURN_NONE;
1167n/a}
1168n/a
1169n/aPyDoc_STRVAR(insert_doc,
1170n/a"D.insert(index, object) -- insert object before index");
1171n/a
1172n/astatic PyObject *
1173n/adeque_remove(dequeobject *deque, PyObject *value)
1174n/a{
1175n/a Py_ssize_t i, n=Py_SIZE(deque);
1176n/a
1177n/a for (i=0 ; i<n ; i++) {
1178n/a PyObject *item = deque->leftblock->data[deque->leftindex];
1179n/a int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
1180n/a
1181n/a if (Py_SIZE(deque) != n) {
1182n/a PyErr_SetString(PyExc_IndexError,
1183n/a "deque mutated during remove().");
1184n/a return NULL;
1185n/a }
1186n/a if (cmp > 0) {
1187n/a PyObject *tgt = deque_popleft(deque, NULL);
1188n/a assert (tgt != NULL);
1189n/a if (_deque_rotate(deque, i))
1190n/a return NULL;
1191n/a Py_DECREF(tgt);
1192n/a Py_RETURN_NONE;
1193n/a }
1194n/a else if (cmp < 0) {
1195n/a _deque_rotate(deque, i);
1196n/a return NULL;
1197n/a }
1198n/a _deque_rotate(deque, -1);
1199n/a }
1200n/a PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
1201n/a return NULL;
1202n/a}
1203n/a
1204n/aPyDoc_STRVAR(remove_doc,
1205n/a"D.remove(value) -- remove first occurrence of value.");
1206n/a
1207n/astatic int
1208n/avalid_index(Py_ssize_t i, Py_ssize_t limit)
1209n/a{
1210n/a /* The cast to size_t lets us use just a single comparison
1211n/a to check whether i is in the range: 0 <= i < limit */
1212n/a return (size_t) i < (size_t) limit;
1213n/a}
1214n/a
1215n/astatic PyObject *
1216n/adeque_item(dequeobject *deque, Py_ssize_t i)
1217n/a{
1218n/a block *b;
1219n/a PyObject *item;
1220n/a Py_ssize_t n, index=i;
1221n/a
1222n/a if (!valid_index(i, Py_SIZE(deque))) {
1223n/a PyErr_SetString(PyExc_IndexError, "deque index out of range");
1224n/a return NULL;
1225n/a }
1226n/a
1227n/a if (i == 0) {
1228n/a i = deque->leftindex;
1229n/a b = deque->leftblock;
1230n/a } else if (i == Py_SIZE(deque) - 1) {
1231n/a i = deque->rightindex;
1232n/a b = deque->rightblock;
1233n/a } else {
1234n/a i += deque->leftindex;
1235n/a n = (Py_ssize_t)((size_t) i / BLOCKLEN);
1236n/a i = (Py_ssize_t)((size_t) i % BLOCKLEN);
1237n/a if (index < (Py_SIZE(deque) >> 1)) {
1238n/a b = deque->leftblock;
1239n/a n++;
1240n/a while (--n)
1241n/a b = b->rightlink;
1242n/a } else {
1243n/a n = (Py_ssize_t)(
1244n/a ((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
1245n/a / BLOCKLEN - n);
1246n/a b = deque->rightblock;
1247n/a n++;
1248n/a while (--n)
1249n/a b = b->leftlink;
1250n/a }
1251n/a }
1252n/a item = b->data[i];
1253n/a Py_INCREF(item);
1254n/a return item;
1255n/a}
1256n/a
1257n/astatic int
1258n/adeque_del_item(dequeobject *deque, Py_ssize_t i)
1259n/a{
1260n/a PyObject *item;
1261n/a int rv;
1262n/a
1263n/a assert (i >= 0 && i < Py_SIZE(deque));
1264n/a if (_deque_rotate(deque, -i))
1265n/a return -1;
1266n/a item = deque_popleft(deque, NULL);
1267n/a rv = _deque_rotate(deque, i);
1268n/a assert (item != NULL);
1269n/a Py_DECREF(item);
1270n/a return rv;
1271n/a}
1272n/a
1273n/astatic int
1274n/adeque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
1275n/a{
1276n/a PyObject *old_value;
1277n/a block *b;
1278n/a Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i;
1279n/a
1280n/a if (!valid_index(i, len)) {
1281n/a PyErr_SetString(PyExc_IndexError, "deque index out of range");
1282n/a return -1;
1283n/a }
1284n/a if (v == NULL)
1285n/a return deque_del_item(deque, i);
1286n/a
1287n/a i += deque->leftindex;
1288n/a n = (Py_ssize_t)((size_t) i / BLOCKLEN);
1289n/a i = (Py_ssize_t)((size_t) i % BLOCKLEN);
1290n/a if (index <= halflen) {
1291n/a b = deque->leftblock;
1292n/a n++;
1293n/a while (--n)
1294n/a b = b->rightlink;
1295n/a } else {
1296n/a n = (Py_ssize_t)(
1297n/a ((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
1298n/a / BLOCKLEN - n);
1299n/a b = deque->rightblock;
1300n/a n++;
1301n/a while (--n)
1302n/a b = b->leftlink;
1303n/a }
1304n/a Py_INCREF(v);
1305n/a old_value = b->data[i];
1306n/a b->data[i] = v;
1307n/a Py_DECREF(old_value);
1308n/a return 0;
1309n/a}
1310n/a
1311n/astatic void
1312n/adeque_dealloc(dequeobject *deque)
1313n/a{
1314n/a PyObject_GC_UnTrack(deque);
1315n/a if (deque->weakreflist != NULL)
1316n/a PyObject_ClearWeakRefs((PyObject *) deque);
1317n/a if (deque->leftblock != NULL) {
1318n/a deque_clear(deque);
1319n/a assert(deque->leftblock != NULL);
1320n/a freeblock(deque->leftblock);
1321n/a }
1322n/a deque->leftblock = NULL;
1323n/a deque->rightblock = NULL;
1324n/a Py_TYPE(deque)->tp_free(deque);
1325n/a}
1326n/a
1327n/astatic int
1328n/adeque_traverse(dequeobject *deque, visitproc visit, void *arg)
1329n/a{
1330n/a block *b;
1331n/a PyObject *item;
1332n/a Py_ssize_t index;
1333n/a Py_ssize_t indexlo = deque->leftindex;
1334n/a Py_ssize_t indexhigh;
1335n/a
1336n/a for (b = deque->leftblock; b != deque->rightblock; b = b->rightlink) {
1337n/a for (index = indexlo; index < BLOCKLEN ; index++) {
1338n/a item = b->data[index];
1339n/a Py_VISIT(item);
1340n/a }
1341n/a indexlo = 0;
1342n/a }
1343n/a indexhigh = deque->rightindex;
1344n/a for (index = indexlo; index <= indexhigh; index++) {
1345n/a item = b->data[index];
1346n/a Py_VISIT(item);
1347n/a }
1348n/a return 0;
1349n/a}
1350n/a
1351n/astatic PyObject *
1352n/adeque_reduce(dequeobject *deque)
1353n/a{
1354n/a PyObject *dict, *it;
1355n/a _Py_IDENTIFIER(__dict__);
1356n/a
1357n/a dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__);
1358n/a if (dict == NULL) {
1359n/a if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1360n/a return NULL;
1361n/a }
1362n/a PyErr_Clear();
1363n/a dict = Py_None;
1364n/a Py_INCREF(dict);
1365n/a }
1366n/a
1367n/a it = PyObject_GetIter((PyObject *)deque);
1368n/a if (it == NULL) {
1369n/a Py_DECREF(dict);
1370n/a return NULL;
1371n/a }
1372n/a
1373n/a if (deque->maxlen < 0) {
1374n/a return Py_BuildValue("O()NN", Py_TYPE(deque), dict, it);
1375n/a }
1376n/a else {
1377n/a return Py_BuildValue("O(()n)NN", Py_TYPE(deque), deque->maxlen, dict, it);
1378n/a }
1379n/a}
1380n/a
1381n/aPyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1382n/a
1383n/astatic PyObject *
1384n/adeque_repr(PyObject *deque)
1385n/a{
1386n/a PyObject *aslist, *result;
1387n/a int i;
1388n/a
1389n/a i = Py_ReprEnter(deque);
1390n/a if (i != 0) {
1391n/a if (i < 0)
1392n/a return NULL;
1393n/a return PyUnicode_FromString("[...]");
1394n/a }
1395n/a
1396n/a aslist = PySequence_List(deque);
1397n/a if (aslist == NULL) {
1398n/a Py_ReprLeave(deque);
1399n/a return NULL;
1400n/a }
1401n/a if (((dequeobject *)deque)->maxlen >= 0)
1402n/a result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
1403n/a aslist, ((dequeobject *)deque)->maxlen);
1404n/a else
1405n/a result = PyUnicode_FromFormat("deque(%R)", aslist);
1406n/a Py_ReprLeave(deque);
1407n/a Py_DECREF(aslist);
1408n/a return result;
1409n/a}
1410n/a
1411n/astatic PyObject *
1412n/adeque_richcompare(PyObject *v, PyObject *w, int op)
1413n/a{
1414n/a PyObject *it1=NULL, *it2=NULL, *x, *y;
1415n/a Py_ssize_t vs, ws;
1416n/a int b, cmp=-1;
1417n/a
1418n/a if (!PyObject_TypeCheck(v, &deque_type) ||
1419n/a !PyObject_TypeCheck(w, &deque_type)) {
1420n/a Py_RETURN_NOTIMPLEMENTED;
1421n/a }
1422n/a
1423n/a /* Shortcuts */
1424n/a vs = Py_SIZE((dequeobject *)v);
1425n/a ws = Py_SIZE((dequeobject *)w);
1426n/a if (op == Py_EQ) {
1427n/a if (v == w)
1428n/a Py_RETURN_TRUE;
1429n/a if (vs != ws)
1430n/a Py_RETURN_FALSE;
1431n/a }
1432n/a if (op == Py_NE) {
1433n/a if (v == w)
1434n/a Py_RETURN_FALSE;
1435n/a if (vs != ws)
1436n/a Py_RETURN_TRUE;
1437n/a }
1438n/a
1439n/a /* Search for the first index where items are different */
1440n/a it1 = PyObject_GetIter(v);
1441n/a if (it1 == NULL)
1442n/a goto done;
1443n/a it2 = PyObject_GetIter(w);
1444n/a if (it2 == NULL)
1445n/a goto done;
1446n/a for (;;) {
1447n/a x = PyIter_Next(it1);
1448n/a if (x == NULL && PyErr_Occurred())
1449n/a goto done;
1450n/a y = PyIter_Next(it2);
1451n/a if (x == NULL || y == NULL)
1452n/a break;
1453n/a b = PyObject_RichCompareBool(x, y, Py_EQ);
1454n/a if (b == 0) {
1455n/a cmp = PyObject_RichCompareBool(x, y, op);
1456n/a Py_DECREF(x);
1457n/a Py_DECREF(y);
1458n/a goto done;
1459n/a }
1460n/a Py_DECREF(x);
1461n/a Py_DECREF(y);
1462n/a if (b < 0)
1463n/a goto done;
1464n/a }
1465n/a /* We reached the end of one deque or both */
1466n/a Py_XDECREF(x);
1467n/a Py_XDECREF(y);
1468n/a if (PyErr_Occurred())
1469n/a goto done;
1470n/a switch (op) {
1471n/a case Py_LT: cmp = y != NULL; break; /* if w was longer */
1472n/a case Py_LE: cmp = x == NULL; break; /* if v was not longer */
1473n/a case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
1474n/a case Py_NE: cmp = x != y; break; /* if one deque continues */
1475n/a case Py_GT: cmp = x != NULL; break; /* if v was longer */
1476n/a case Py_GE: cmp = y == NULL; break; /* if w was not longer */
1477n/a }
1478n/a
1479n/adone:
1480n/a Py_XDECREF(it1);
1481n/a Py_XDECREF(it2);
1482n/a if (cmp == 1)
1483n/a Py_RETURN_TRUE;
1484n/a if (cmp == 0)
1485n/a Py_RETURN_FALSE;
1486n/a return NULL;
1487n/a}
1488n/a
1489n/astatic int
1490n/adeque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
1491n/a{
1492n/a PyObject *iterable = NULL;
1493n/a PyObject *maxlenobj = NULL;
1494n/a Py_ssize_t maxlen = -1;
1495n/a char *kwlist[] = {"iterable", "maxlen", 0};
1496n/a
1497n/a if (kwdargs == NULL) {
1498n/a if (!PyArg_UnpackTuple(args, "deque()", 0, 2, &iterable, &maxlenobj))
1499n/a return -1;
1500n/a } else {
1501n/a if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist,
1502n/a &iterable, &maxlenobj))
1503n/a return -1;
1504n/a }
1505n/a if (maxlenobj != NULL && maxlenobj != Py_None) {
1506n/a maxlen = PyLong_AsSsize_t(maxlenobj);
1507n/a if (maxlen == -1 && PyErr_Occurred())
1508n/a return -1;
1509n/a if (maxlen < 0) {
1510n/a PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
1511n/a return -1;
1512n/a }
1513n/a }
1514n/a deque->maxlen = maxlen;
1515n/a if (Py_SIZE(deque) > 0)
1516n/a deque_clear(deque);
1517n/a if (iterable != NULL) {
1518n/a PyObject *rv = deque_extend(deque, iterable);
1519n/a if (rv == NULL)
1520n/a return -1;
1521n/a Py_DECREF(rv);
1522n/a }
1523n/a return 0;
1524n/a}
1525n/a
1526n/astatic PyObject *
1527n/adeque_sizeof(dequeobject *deque, void *unused)
1528n/a{
1529n/a Py_ssize_t res;
1530n/a Py_ssize_t blocks;
1531n/a
1532n/a res = _PyObject_SIZE(Py_TYPE(deque));
1533n/a blocks = (size_t)(deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
1534n/a assert(deque->leftindex + Py_SIZE(deque) - 1 ==
1535n/a (blocks - 1) * BLOCKLEN + deque->rightindex);
1536n/a res += blocks * sizeof(block);
1537n/a return PyLong_FromSsize_t(res);
1538n/a}
1539n/a
1540n/aPyDoc_STRVAR(sizeof_doc,
1541n/a"D.__sizeof__() -- size of D in memory, in bytes");
1542n/a
1543n/astatic int
1544n/adeque_bool(dequeobject *deque)
1545n/a{
1546n/a return Py_SIZE(deque) != 0;
1547n/a}
1548n/a
1549n/astatic PyObject *
1550n/adeque_get_maxlen(dequeobject *deque)
1551n/a{
1552n/a if (deque->maxlen < 0)
1553n/a Py_RETURN_NONE;
1554n/a return PyLong_FromSsize_t(deque->maxlen);
1555n/a}
1556n/a
1557n/a
1558n/a/* deque object ********************************************************/
1559n/a
1560n/astatic PyGetSetDef deque_getset[] = {
1561n/a {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
1562n/a "maximum size of a deque or None if unbounded"},
1563n/a {0}
1564n/a};
1565n/a
1566n/astatic PySequenceMethods deque_as_sequence = {
1567n/a (lenfunc)deque_len, /* sq_length */
1568n/a (binaryfunc)deque_concat, /* sq_concat */
1569n/a (ssizeargfunc)deque_repeat, /* sq_repeat */
1570n/a (ssizeargfunc)deque_item, /* sq_item */
1571n/a 0, /* sq_slice */
1572n/a (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
1573n/a 0, /* sq_ass_slice */
1574n/a (objobjproc)deque_contains, /* sq_contains */
1575n/a (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
1576n/a (ssizeargfunc)deque_inplace_repeat, /* sq_inplace_repeat */
1577n/a};
1578n/a
1579n/astatic PyNumberMethods deque_as_number = {
1580n/a 0, /* nb_add */
1581n/a 0, /* nb_subtract */
1582n/a 0, /* nb_multiply */
1583n/a 0, /* nb_remainder */
1584n/a 0, /* nb_divmod */
1585n/a 0, /* nb_power */
1586n/a 0, /* nb_negative */
1587n/a 0, /* nb_positive */
1588n/a 0, /* nb_absolute */
1589n/a (inquiry)deque_bool, /* nb_bool */
1590n/a 0, /* nb_invert */
1591n/a };
1592n/a
1593n/astatic PyObject *deque_iter(dequeobject *deque);
1594n/astatic PyObject *deque_reviter(dequeobject *deque);
1595n/aPyDoc_STRVAR(reversed_doc,
1596n/a "D.__reversed__() -- return a reverse iterator over the deque");
1597n/a
1598n/astatic PyMethodDef deque_methods[] = {
1599n/a {"append", (PyCFunction)deque_append,
1600n/a METH_O, append_doc},
1601n/a {"appendleft", (PyCFunction)deque_appendleft,
1602n/a METH_O, appendleft_doc},
1603n/a {"clear", (PyCFunction)deque_clearmethod,
1604n/a METH_NOARGS, clear_doc},
1605n/a {"__copy__", (PyCFunction)deque_copy,
1606n/a METH_NOARGS, copy_doc},
1607n/a {"copy", (PyCFunction)deque_copy,
1608n/a METH_NOARGS, copy_doc},
1609n/a {"count", (PyCFunction)deque_count,
1610n/a METH_O, count_doc},
1611n/a {"extend", (PyCFunction)deque_extend,
1612n/a METH_O, extend_doc},
1613n/a {"extendleft", (PyCFunction)deque_extendleft,
1614n/a METH_O, extendleft_doc},
1615n/a {"index", (PyCFunction)deque_index,
1616n/a METH_FASTCALL, index_doc},
1617n/a {"insert", (PyCFunction)deque_insert,
1618n/a METH_FASTCALL, insert_doc},
1619n/a {"pop", (PyCFunction)deque_pop,
1620n/a METH_NOARGS, pop_doc},
1621n/a {"popleft", (PyCFunction)deque_popleft,
1622n/a METH_NOARGS, popleft_doc},
1623n/a {"__reduce__", (PyCFunction)deque_reduce,
1624n/a METH_NOARGS, reduce_doc},
1625n/a {"remove", (PyCFunction)deque_remove,
1626n/a METH_O, remove_doc},
1627n/a {"__reversed__", (PyCFunction)deque_reviter,
1628n/a METH_NOARGS, reversed_doc},
1629n/a {"reverse", (PyCFunction)deque_reverse,
1630n/a METH_NOARGS, reverse_doc},
1631n/a {"rotate", (PyCFunction)deque_rotate,
1632n/a METH_FASTCALL, rotate_doc},
1633n/a {"__sizeof__", (PyCFunction)deque_sizeof,
1634n/a METH_NOARGS, sizeof_doc},
1635n/a {NULL, NULL} /* sentinel */
1636n/a};
1637n/a
1638n/aPyDoc_STRVAR(deque_doc,
1639n/a"deque([iterable[, maxlen]]) --> deque object\n\
1640n/a\n\
1641n/aA list-like sequence optimized for data accesses near its endpoints.");
1642n/a
1643n/astatic PyTypeObject deque_type = {
1644n/a PyVarObject_HEAD_INIT(NULL, 0)
1645n/a "collections.deque", /* tp_name */
1646n/a sizeof(dequeobject), /* tp_basicsize */
1647n/a 0, /* tp_itemsize */
1648n/a /* methods */
1649n/a (destructor)deque_dealloc, /* tp_dealloc */
1650n/a 0, /* tp_print */
1651n/a 0, /* tp_getattr */
1652n/a 0, /* tp_setattr */
1653n/a 0, /* tp_reserved */
1654n/a deque_repr, /* tp_repr */
1655n/a &deque_as_number, /* tp_as_number */
1656n/a &deque_as_sequence, /* tp_as_sequence */
1657n/a 0, /* tp_as_mapping */
1658n/a PyObject_HashNotImplemented, /* tp_hash */
1659n/a 0, /* tp_call */
1660n/a 0, /* tp_str */
1661n/a PyObject_GenericGetAttr, /* tp_getattro */
1662n/a 0, /* tp_setattro */
1663n/a 0, /* tp_as_buffer */
1664n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1665n/a /* tp_flags */
1666n/a deque_doc, /* tp_doc */
1667n/a (traverseproc)deque_traverse, /* tp_traverse */
1668n/a (inquiry)deque_clear, /* tp_clear */
1669n/a (richcmpfunc)deque_richcompare, /* tp_richcompare */
1670n/a offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
1671n/a (getiterfunc)deque_iter, /* tp_iter */
1672n/a 0, /* tp_iternext */
1673n/a deque_methods, /* tp_methods */
1674n/a 0, /* tp_members */
1675n/a deque_getset, /* tp_getset */
1676n/a 0, /* tp_base */
1677n/a 0, /* tp_dict */
1678n/a 0, /* tp_descr_get */
1679n/a 0, /* tp_descr_set */
1680n/a 0, /* tp_dictoffset */
1681n/a (initproc)deque_init, /* tp_init */
1682n/a PyType_GenericAlloc, /* tp_alloc */
1683n/a deque_new, /* tp_new */
1684n/a PyObject_GC_Del, /* tp_free */
1685n/a};
1686n/a
1687n/a/*********************** Deque Iterator **************************/
1688n/a
1689n/atypedef struct {
1690n/a PyObject_HEAD
1691n/a block *b;
1692n/a Py_ssize_t index;
1693n/a dequeobject *deque;
1694n/a size_t state; /* state when the iterator is created */
1695n/a Py_ssize_t counter; /* number of items remaining for iteration */
1696n/a} dequeiterobject;
1697n/a
1698n/astatic PyTypeObject dequeiter_type;
1699n/a
1700n/astatic PyObject *
1701n/adeque_iter(dequeobject *deque)
1702n/a{
1703n/a dequeiterobject *it;
1704n/a
1705n/a it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
1706n/a if (it == NULL)
1707n/a return NULL;
1708n/a it->b = deque->leftblock;
1709n/a it->index = deque->leftindex;
1710n/a Py_INCREF(deque);
1711n/a it->deque = deque;
1712n/a it->state = deque->state;
1713n/a it->counter = Py_SIZE(deque);
1714n/a PyObject_GC_Track(it);
1715n/a return (PyObject *)it;
1716n/a}
1717n/a
1718n/astatic int
1719n/adequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
1720n/a{
1721n/a Py_VISIT(dio->deque);
1722n/a return 0;
1723n/a}
1724n/a
1725n/astatic void
1726n/adequeiter_dealloc(dequeiterobject *dio)
1727n/a{
1728n/a Py_XDECREF(dio->deque);
1729n/a PyObject_GC_Del(dio);
1730n/a}
1731n/a
1732n/astatic PyObject *
1733n/adequeiter_next(dequeiterobject *it)
1734n/a{
1735n/a PyObject *item;
1736n/a
1737n/a if (it->deque->state != it->state) {
1738n/a it->counter = 0;
1739n/a PyErr_SetString(PyExc_RuntimeError,
1740n/a "deque mutated during iteration");
1741n/a return NULL;
1742n/a }
1743n/a if (it->counter == 0)
1744n/a return NULL;
1745n/a assert (!(it->b == it->deque->rightblock &&
1746n/a it->index > it->deque->rightindex));
1747n/a
1748n/a item = it->b->data[it->index];
1749n/a it->index++;
1750n/a it->counter--;
1751n/a if (it->index == BLOCKLEN && it->counter > 0) {
1752n/a CHECK_NOT_END(it->b->rightlink);
1753n/a it->b = it->b->rightlink;
1754n/a it->index = 0;
1755n/a }
1756n/a Py_INCREF(item);
1757n/a return item;
1758n/a}
1759n/a
1760n/astatic PyObject *
1761n/adequeiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1762n/a{
1763n/a Py_ssize_t i, index=0;
1764n/a PyObject *deque;
1765n/a dequeiterobject *it;
1766n/a if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index))
1767n/a return NULL;
1768n/a assert(type == &dequeiter_type);
1769n/a
1770n/a it = (dequeiterobject*)deque_iter((dequeobject *)deque);
1771n/a if (!it)
1772n/a return NULL;
1773n/a /* consume items from the queue */
1774n/a for(i=0; i<index; i++) {
1775n/a PyObject *item = dequeiter_next(it);
1776n/a if (item) {
1777n/a Py_DECREF(item);
1778n/a } else {
1779n/a if (it->counter) {
1780n/a Py_DECREF(it);
1781n/a return NULL;
1782n/a } else
1783n/a break;
1784n/a }
1785n/a }
1786n/a return (PyObject*)it;
1787n/a}
1788n/a
1789n/astatic PyObject *
1790n/adequeiter_len(dequeiterobject *it)
1791n/a{
1792n/a return PyLong_FromSsize_t(it->counter);
1793n/a}
1794n/a
1795n/aPyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
1796n/a
1797n/astatic PyObject *
1798n/adequeiter_reduce(dequeiterobject *it)
1799n/a{
1800n/a return Py_BuildValue("O(On)", Py_TYPE(it), it->deque, Py_SIZE(it->deque) - it->counter);
1801n/a}
1802n/a
1803n/astatic PyMethodDef dequeiter_methods[] = {
1804n/a {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
1805n/a {"__reduce__", (PyCFunction)dequeiter_reduce, METH_NOARGS, reduce_doc},
1806n/a {NULL, NULL} /* sentinel */
1807n/a};
1808n/a
1809n/astatic PyTypeObject dequeiter_type = {
1810n/a PyVarObject_HEAD_INIT(NULL, 0)
1811n/a "_collections._deque_iterator", /* tp_name */
1812n/a sizeof(dequeiterobject), /* tp_basicsize */
1813n/a 0, /* tp_itemsize */
1814n/a /* methods */
1815n/a (destructor)dequeiter_dealloc, /* tp_dealloc */
1816n/a 0, /* tp_print */
1817n/a 0, /* tp_getattr */
1818n/a 0, /* tp_setattr */
1819n/a 0, /* tp_reserved */
1820n/a 0, /* tp_repr */
1821n/a 0, /* tp_as_number */
1822n/a 0, /* tp_as_sequence */
1823n/a 0, /* tp_as_mapping */
1824n/a 0, /* tp_hash */
1825n/a 0, /* tp_call */
1826n/a 0, /* tp_str */
1827n/a PyObject_GenericGetAttr, /* tp_getattro */
1828n/a 0, /* tp_setattro */
1829n/a 0, /* tp_as_buffer */
1830n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1831n/a 0, /* tp_doc */
1832n/a (traverseproc)dequeiter_traverse, /* tp_traverse */
1833n/a 0, /* tp_clear */
1834n/a 0, /* tp_richcompare */
1835n/a 0, /* tp_weaklistoffset */
1836n/a PyObject_SelfIter, /* tp_iter */
1837n/a (iternextfunc)dequeiter_next, /* tp_iternext */
1838n/a dequeiter_methods, /* tp_methods */
1839n/a 0, /* tp_members */
1840n/a 0, /* tp_getset */
1841n/a 0, /* tp_base */
1842n/a 0, /* tp_dict */
1843n/a 0, /* tp_descr_get */
1844n/a 0, /* tp_descr_set */
1845n/a 0, /* tp_dictoffset */
1846n/a 0, /* tp_init */
1847n/a 0, /* tp_alloc */
1848n/a dequeiter_new, /* tp_new */
1849n/a 0,
1850n/a};
1851n/a
1852n/a/*********************** Deque Reverse Iterator **************************/
1853n/a
1854n/astatic PyTypeObject dequereviter_type;
1855n/a
1856n/astatic PyObject *
1857n/adeque_reviter(dequeobject *deque)
1858n/a{
1859n/a dequeiterobject *it;
1860n/a
1861n/a it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
1862n/a if (it == NULL)
1863n/a return NULL;
1864n/a it->b = deque->rightblock;
1865n/a it->index = deque->rightindex;
1866n/a Py_INCREF(deque);
1867n/a it->deque = deque;
1868n/a it->state = deque->state;
1869n/a it->counter = Py_SIZE(deque);
1870n/a PyObject_GC_Track(it);
1871n/a return (PyObject *)it;
1872n/a}
1873n/a
1874n/astatic PyObject *
1875n/adequereviter_next(dequeiterobject *it)
1876n/a{
1877n/a PyObject *item;
1878n/a if (it->counter == 0)
1879n/a return NULL;
1880n/a
1881n/a if (it->deque->state != it->state) {
1882n/a it->counter = 0;
1883n/a PyErr_SetString(PyExc_RuntimeError,
1884n/a "deque mutated during iteration");
1885n/a return NULL;
1886n/a }
1887n/a assert (!(it->b == it->deque->leftblock &&
1888n/a it->index < it->deque->leftindex));
1889n/a
1890n/a item = it->b->data[it->index];
1891n/a it->index--;
1892n/a it->counter--;
1893n/a if (it->index < 0 && it->counter > 0) {
1894n/a CHECK_NOT_END(it->b->leftlink);
1895n/a it->b = it->b->leftlink;
1896n/a it->index = BLOCKLEN - 1;
1897n/a }
1898n/a Py_INCREF(item);
1899n/a return item;
1900n/a}
1901n/a
1902n/astatic PyObject *
1903n/adequereviter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1904n/a{
1905n/a Py_ssize_t i, index=0;
1906n/a PyObject *deque;
1907n/a dequeiterobject *it;
1908n/a if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index))
1909n/a return NULL;
1910n/a assert(type == &dequereviter_type);
1911n/a
1912n/a it = (dequeiterobject*)deque_reviter((dequeobject *)deque);
1913n/a if (!it)
1914n/a return NULL;
1915n/a /* consume items from the queue */
1916n/a for(i=0; i<index; i++) {
1917n/a PyObject *item = dequereviter_next(it);
1918n/a if (item) {
1919n/a Py_DECREF(item);
1920n/a } else {
1921n/a if (it->counter) {
1922n/a Py_DECREF(it);
1923n/a return NULL;
1924n/a } else
1925n/a break;
1926n/a }
1927n/a }
1928n/a return (PyObject*)it;
1929n/a}
1930n/a
1931n/astatic PyTypeObject dequereviter_type = {
1932n/a PyVarObject_HEAD_INIT(NULL, 0)
1933n/a "_collections._deque_reverse_iterator", /* tp_name */
1934n/a sizeof(dequeiterobject), /* tp_basicsize */
1935n/a 0, /* tp_itemsize */
1936n/a /* methods */
1937n/a (destructor)dequeiter_dealloc, /* tp_dealloc */
1938n/a 0, /* tp_print */
1939n/a 0, /* tp_getattr */
1940n/a 0, /* tp_setattr */
1941n/a 0, /* tp_reserved */
1942n/a 0, /* tp_repr */
1943n/a 0, /* tp_as_number */
1944n/a 0, /* tp_as_sequence */
1945n/a 0, /* tp_as_mapping */
1946n/a 0, /* tp_hash */
1947n/a 0, /* tp_call */
1948n/a 0, /* tp_str */
1949n/a PyObject_GenericGetAttr, /* tp_getattro */
1950n/a 0, /* tp_setattro */
1951n/a 0, /* tp_as_buffer */
1952n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1953n/a 0, /* tp_doc */
1954n/a (traverseproc)dequeiter_traverse, /* tp_traverse */
1955n/a 0, /* tp_clear */
1956n/a 0, /* tp_richcompare */
1957n/a 0, /* tp_weaklistoffset */
1958n/a PyObject_SelfIter, /* tp_iter */
1959n/a (iternextfunc)dequereviter_next, /* tp_iternext */
1960n/a dequeiter_methods, /* tp_methods */
1961n/a 0, /* tp_members */
1962n/a 0, /* tp_getset */
1963n/a 0, /* tp_base */
1964n/a 0, /* tp_dict */
1965n/a 0, /* tp_descr_get */
1966n/a 0, /* tp_descr_set */
1967n/a 0, /* tp_dictoffset */
1968n/a 0, /* tp_init */
1969n/a 0, /* tp_alloc */
1970n/a dequereviter_new, /* tp_new */
1971n/a 0,
1972n/a};
1973n/a
1974n/a/* defaultdict type *********************************************************/
1975n/a
1976n/atypedef struct {
1977n/a PyDictObject dict;
1978n/a PyObject *default_factory;
1979n/a} defdictobject;
1980n/a
1981n/astatic PyTypeObject defdict_type; /* Forward */
1982n/a
1983n/aPyDoc_STRVAR(defdict_missing_doc,
1984n/a"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
1985n/a if self.default_factory is None: raise KeyError((key,))\n\
1986n/a self[key] = value = self.default_factory()\n\
1987n/a return value\n\
1988n/a");
1989n/a
1990n/astatic PyObject *
1991n/adefdict_missing(defdictobject *dd, PyObject *key)
1992n/a{
1993n/a PyObject *factory = dd->default_factory;
1994n/a PyObject *value;
1995n/a if (factory == NULL || factory == Py_None) {
1996n/a /* XXX Call dict.__missing__(key) */
1997n/a PyObject *tup;
1998n/a tup = PyTuple_Pack(1, key);
1999n/a if (!tup) return NULL;
2000n/a PyErr_SetObject(PyExc_KeyError, tup);
2001n/a Py_DECREF(tup);
2002n/a return NULL;
2003n/a }
2004n/a value = PyEval_CallObject(factory, NULL);
2005n/a if (value == NULL)
2006n/a return value;
2007n/a if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
2008n/a Py_DECREF(value);
2009n/a return NULL;
2010n/a }
2011n/a return value;
2012n/a}
2013n/a
2014n/aPyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
2015n/a
2016n/astatic PyObject *
2017n/adefdict_copy(defdictobject *dd)
2018n/a{
2019n/a /* This calls the object's class. That only works for subclasses
2020n/a whose class constructor has the same signature. Subclasses that
2021n/a define a different constructor signature must override copy().
2022n/a */
2023n/a
2024n/a if (dd->default_factory == NULL)
2025n/a return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
2026n/a return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
2027n/a dd->default_factory, dd, NULL);
2028n/a}
2029n/a
2030n/astatic PyObject *
2031n/adefdict_reduce(defdictobject *dd)
2032n/a{
2033n/a /* __reduce__ must return a 5-tuple as follows:
2034n/a
2035n/a - factory function
2036n/a - tuple of args for the factory function
2037n/a - additional state (here None)
2038n/a - sequence iterator (here None)
2039n/a - dictionary iterator (yielding successive (key, value) pairs
2040n/a
2041n/a This API is used by pickle.py and copy.py.
2042n/a
2043n/a For this to be useful with pickle.py, the default_factory
2044n/a must be picklable; e.g., None, a built-in, or a global
2045n/a function in a module or package.
2046n/a
2047n/a Both shallow and deep copying are supported, but for deep
2048n/a copying, the default_factory must be deep-copyable; e.g. None,
2049n/a or a built-in (functions are not copyable at this time).
2050n/a
2051n/a This only works for subclasses as long as their constructor
2052n/a signature is compatible; the first argument must be the
2053n/a optional default_factory, defaulting to None.
2054n/a */
2055n/a PyObject *args;
2056n/a PyObject *items;
2057n/a PyObject *iter;
2058n/a PyObject *result;
2059n/a _Py_IDENTIFIER(items);
2060n/a
2061n/a if (dd->default_factory == NULL || dd->default_factory == Py_None)
2062n/a args = PyTuple_New(0);
2063n/a else
2064n/a args = PyTuple_Pack(1, dd->default_factory);
2065n/a if (args == NULL)
2066n/a return NULL;
2067n/a items = _PyObject_CallMethodId((PyObject *)dd, &PyId_items, NULL);
2068n/a if (items == NULL) {
2069n/a Py_DECREF(args);
2070n/a return NULL;
2071n/a }
2072n/a iter = PyObject_GetIter(items);
2073n/a if (iter == NULL) {
2074n/a Py_DECREF(items);
2075n/a Py_DECREF(args);
2076n/a return NULL;
2077n/a }
2078n/a result = PyTuple_Pack(5, Py_TYPE(dd), args,
2079n/a Py_None, Py_None, iter);
2080n/a Py_DECREF(iter);
2081n/a Py_DECREF(items);
2082n/a Py_DECREF(args);
2083n/a return result;
2084n/a}
2085n/a
2086n/astatic PyMethodDef defdict_methods[] = {
2087n/a {"__missing__", (PyCFunction)defdict_missing, METH_O,
2088n/a defdict_missing_doc},
2089n/a {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
2090n/a defdict_copy_doc},
2091n/a {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
2092n/a defdict_copy_doc},
2093n/a {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
2094n/a reduce_doc},
2095n/a {NULL}
2096n/a};
2097n/a
2098n/astatic PyMemberDef defdict_members[] = {
2099n/a {"default_factory", T_OBJECT,
2100n/a offsetof(defdictobject, default_factory), 0,
2101n/a PyDoc_STR("Factory for default value called by __missing__().")},
2102n/a {NULL}
2103n/a};
2104n/a
2105n/astatic void
2106n/adefdict_dealloc(defdictobject *dd)
2107n/a{
2108n/a Py_CLEAR(dd->default_factory);
2109n/a PyDict_Type.tp_dealloc((PyObject *)dd);
2110n/a}
2111n/a
2112n/astatic PyObject *
2113n/adefdict_repr(defdictobject *dd)
2114n/a{
2115n/a PyObject *baserepr;
2116n/a PyObject *defrepr;
2117n/a PyObject *result;
2118n/a baserepr = PyDict_Type.tp_repr((PyObject *)dd);
2119n/a if (baserepr == NULL)
2120n/a return NULL;
2121n/a if (dd->default_factory == NULL)
2122n/a defrepr = PyUnicode_FromString("None");
2123n/a else
2124n/a {
2125n/a int status = Py_ReprEnter(dd->default_factory);
2126n/a if (status != 0) {
2127n/a if (status < 0) {
2128n/a Py_DECREF(baserepr);
2129n/a return NULL;
2130n/a }
2131n/a defrepr = PyUnicode_FromString("...");
2132n/a }
2133n/a else
2134n/a defrepr = PyObject_Repr(dd->default_factory);
2135n/a Py_ReprLeave(dd->default_factory);
2136n/a }
2137n/a if (defrepr == NULL) {
2138n/a Py_DECREF(baserepr);
2139n/a return NULL;
2140n/a }
2141n/a result = PyUnicode_FromFormat("defaultdict(%U, %U)",
2142n/a defrepr, baserepr);
2143n/a Py_DECREF(defrepr);
2144n/a Py_DECREF(baserepr);
2145n/a return result;
2146n/a}
2147n/a
2148n/astatic int
2149n/adefdict_traverse(PyObject *self, visitproc visit, void *arg)
2150n/a{
2151n/a Py_VISIT(((defdictobject *)self)->default_factory);
2152n/a return PyDict_Type.tp_traverse(self, visit, arg);
2153n/a}
2154n/a
2155n/astatic int
2156n/adefdict_tp_clear(defdictobject *dd)
2157n/a{
2158n/a Py_CLEAR(dd->default_factory);
2159n/a return PyDict_Type.tp_clear((PyObject *)dd);
2160n/a}
2161n/a
2162n/astatic int
2163n/adefdict_init(PyObject *self, PyObject *args, PyObject *kwds)
2164n/a{
2165n/a defdictobject *dd = (defdictobject *)self;
2166n/a PyObject *olddefault = dd->default_factory;
2167n/a PyObject *newdefault = NULL;
2168n/a PyObject *newargs;
2169n/a int result;
2170n/a if (args == NULL || !PyTuple_Check(args))
2171n/a newargs = PyTuple_New(0);
2172n/a else {
2173n/a Py_ssize_t n = PyTuple_GET_SIZE(args);
2174n/a if (n > 0) {
2175n/a newdefault = PyTuple_GET_ITEM(args, 0);
2176n/a if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
2177n/a PyErr_SetString(PyExc_TypeError,
2178n/a "first argument must be callable or None");
2179n/a return -1;
2180n/a }
2181n/a }
2182n/a newargs = PySequence_GetSlice(args, 1, n);
2183n/a }
2184n/a if (newargs == NULL)
2185n/a return -1;
2186n/a Py_XINCREF(newdefault);
2187n/a dd->default_factory = newdefault;
2188n/a result = PyDict_Type.tp_init(self, newargs, kwds);
2189n/a Py_DECREF(newargs);
2190n/a Py_XDECREF(olddefault);
2191n/a return result;
2192n/a}
2193n/a
2194n/aPyDoc_STRVAR(defdict_doc,
2195n/a"defaultdict(default_factory[, ...]) --> dict with default factory\n\
2196n/a\n\
2197n/aThe default factory is called without arguments to produce\n\
2198n/aa new value when a key is not present, in __getitem__ only.\n\
2199n/aA defaultdict compares equal to a dict with the same items.\n\
2200n/aAll remaining arguments are treated the same as if they were\n\
2201n/apassed to the dict constructor, including keyword arguments.\n\
2202n/a");
2203n/a
2204n/a/* See comment in xxsubtype.c */
2205n/a#define DEFERRED_ADDRESS(ADDR) 0
2206n/a
2207n/astatic PyTypeObject defdict_type = {
2208n/a PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
2209n/a "collections.defaultdict", /* tp_name */
2210n/a sizeof(defdictobject), /* tp_basicsize */
2211n/a 0, /* tp_itemsize */
2212n/a /* methods */
2213n/a (destructor)defdict_dealloc, /* tp_dealloc */
2214n/a 0, /* tp_print */
2215n/a 0, /* tp_getattr */
2216n/a 0, /* tp_setattr */
2217n/a 0, /* tp_reserved */
2218n/a (reprfunc)defdict_repr, /* tp_repr */
2219n/a 0, /* tp_as_number */
2220n/a 0, /* tp_as_sequence */
2221n/a 0, /* tp_as_mapping */
2222n/a 0, /* tp_hash */
2223n/a 0, /* tp_call */
2224n/a 0, /* tp_str */
2225n/a PyObject_GenericGetAttr, /* tp_getattro */
2226n/a 0, /* tp_setattro */
2227n/a 0, /* tp_as_buffer */
2228n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
2229n/a /* tp_flags */
2230n/a defdict_doc, /* tp_doc */
2231n/a defdict_traverse, /* tp_traverse */
2232n/a (inquiry)defdict_tp_clear, /* tp_clear */
2233n/a 0, /* tp_richcompare */
2234n/a 0, /* tp_weaklistoffset*/
2235n/a 0, /* tp_iter */
2236n/a 0, /* tp_iternext */
2237n/a defdict_methods, /* tp_methods */
2238n/a defdict_members, /* tp_members */
2239n/a 0, /* tp_getset */
2240n/a DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
2241n/a 0, /* tp_dict */
2242n/a 0, /* tp_descr_get */
2243n/a 0, /* tp_descr_set */
2244n/a 0, /* tp_dictoffset */
2245n/a defdict_init, /* tp_init */
2246n/a PyType_GenericAlloc, /* tp_alloc */
2247n/a 0, /* tp_new */
2248n/a PyObject_GC_Del, /* tp_free */
2249n/a};
2250n/a
2251n/a/* helper function for Counter *********************************************/
2252n/a
2253n/aPyDoc_STRVAR(_count_elements_doc,
2254n/a"_count_elements(mapping, iterable) -> None\n\
2255n/a\n\
2256n/aCount elements in the iterable, updating the mapping");
2257n/a
2258n/astatic PyObject *
2259n/a_count_elements(PyObject *self, PyObject *args)
2260n/a{
2261n/a _Py_IDENTIFIER(get);
2262n/a _Py_IDENTIFIER(__setitem__);
2263n/a PyObject *it, *iterable, *mapping, *oldval;
2264n/a PyObject *newval = NULL;
2265n/a PyObject *key = NULL;
2266n/a PyObject *zero = NULL;
2267n/a PyObject *one = NULL;
2268n/a PyObject *bound_get = NULL;
2269n/a PyObject *mapping_get;
2270n/a PyObject *dict_get;
2271n/a PyObject *mapping_setitem;
2272n/a PyObject *dict_setitem;
2273n/a
2274n/a if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable))
2275n/a return NULL;
2276n/a
2277n/a it = PyObject_GetIter(iterable);
2278n/a if (it == NULL)
2279n/a return NULL;
2280n/a
2281n/a one = PyLong_FromLong(1);
2282n/a if (one == NULL)
2283n/a goto done;
2284n/a
2285n/a /* Only take the fast path when get() and __setitem__()
2286n/a * have not been overridden.
2287n/a */
2288n/a mapping_get = _PyType_LookupId(Py_TYPE(mapping), &PyId_get);
2289n/a dict_get = _PyType_LookupId(&PyDict_Type, &PyId_get);
2290n/a mapping_setitem = _PyType_LookupId(Py_TYPE(mapping), &PyId___setitem__);
2291n/a dict_setitem = _PyType_LookupId(&PyDict_Type, &PyId___setitem__);
2292n/a
2293n/a if (mapping_get != NULL && mapping_get == dict_get &&
2294n/a mapping_setitem != NULL && mapping_setitem == dict_setitem) {
2295n/a while (1) {
2296n/a /* Fast path advantages:
2297n/a 1. Eliminate double hashing
2298n/a (by re-using the same hash for both the get and set)
2299n/a 2. Avoid argument overhead of PyObject_CallFunctionObjArgs
2300n/a (argument tuple creation and parsing)
2301n/a 3. Avoid indirection through a bound method object
2302n/a (creates another argument tuple)
2303n/a 4. Avoid initial increment from zero
2304n/a (reuse an existing one-object instead)
2305n/a */
2306n/a Py_hash_t hash;
2307n/a
2308n/a key = PyIter_Next(it);
2309n/a if (key == NULL)
2310n/a break;
2311n/a
2312n/a if (!PyUnicode_CheckExact(key) ||
2313n/a (hash = ((PyASCIIObject *) key)->hash) == -1)
2314n/a {
2315n/a hash = PyObject_Hash(key);
2316n/a if (hash == -1)
2317n/a goto done;
2318n/a }
2319n/a
2320n/a oldval = _PyDict_GetItem_KnownHash(mapping, key, hash);
2321n/a if (oldval == NULL) {
2322n/a if (PyErr_Occurred())
2323n/a goto done;
2324n/a if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) < 0)
2325n/a goto done;
2326n/a } else {
2327n/a newval = PyNumber_Add(oldval, one);
2328n/a if (newval == NULL)
2329n/a goto done;
2330n/a if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
2331n/a goto done;
2332n/a Py_CLEAR(newval);
2333n/a }
2334n/a Py_DECREF(key);
2335n/a }
2336n/a } else {
2337n/a bound_get = _PyObject_GetAttrId(mapping, &PyId_get);
2338n/a if (bound_get == NULL)
2339n/a goto done;
2340n/a
2341n/a zero = PyLong_FromLong(0);
2342n/a if (zero == NULL)
2343n/a goto done;
2344n/a
2345n/a while (1) {
2346n/a key = PyIter_Next(it);
2347n/a if (key == NULL)
2348n/a break;
2349n/a oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
2350n/a if (oldval == NULL)
2351n/a break;
2352n/a newval = PyNumber_Add(oldval, one);
2353n/a Py_DECREF(oldval);
2354n/a if (newval == NULL)
2355n/a break;
2356n/a if (PyObject_SetItem(mapping, key, newval) < 0)
2357n/a break;
2358n/a Py_CLEAR(newval);
2359n/a Py_DECREF(key);
2360n/a }
2361n/a }
2362n/a
2363n/adone:
2364n/a Py_DECREF(it);
2365n/a Py_XDECREF(key);
2366n/a Py_XDECREF(newval);
2367n/a Py_XDECREF(bound_get);
2368n/a Py_XDECREF(zero);
2369n/a Py_XDECREF(one);
2370n/a if (PyErr_Occurred())
2371n/a return NULL;
2372n/a Py_RETURN_NONE;
2373n/a}
2374n/a
2375n/a/* module level code ********************************************************/
2376n/a
2377n/aPyDoc_STRVAR(module_doc,
2378n/a"High performance data structures.\n\
2379n/a- deque: ordered collection accessible from endpoints only\n\
2380n/a- defaultdict: dict subclass with a default value factory\n\
2381n/a");
2382n/a
2383n/astatic struct PyMethodDef module_functions[] = {
2384n/a {"_count_elements", _count_elements, METH_VARARGS, _count_elements_doc},
2385n/a {NULL, NULL} /* sentinel */
2386n/a};
2387n/a
2388n/astatic struct PyModuleDef _collectionsmodule = {
2389n/a PyModuleDef_HEAD_INIT,
2390n/a "_collections",
2391n/a module_doc,
2392n/a -1,
2393n/a module_functions,
2394n/a NULL,
2395n/a NULL,
2396n/a NULL,
2397n/a NULL
2398n/a};
2399n/a
2400n/aPyMODINIT_FUNC
2401n/aPyInit__collections(void)
2402n/a{
2403n/a PyObject *m;
2404n/a
2405n/a m = PyModule_Create(&_collectionsmodule);
2406n/a if (m == NULL)
2407n/a return NULL;
2408n/a
2409n/a if (PyType_Ready(&deque_type) < 0)
2410n/a return NULL;
2411n/a Py_INCREF(&deque_type);
2412n/a PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
2413n/a
2414n/a defdict_type.tp_base = &PyDict_Type;
2415n/a if (PyType_Ready(&defdict_type) < 0)
2416n/a return NULL;
2417n/a Py_INCREF(&defdict_type);
2418n/a PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
2419n/a
2420n/a Py_INCREF(&PyODict_Type);
2421n/a PyModule_AddObject(m, "OrderedDict", (PyObject *)&PyODict_Type);
2422n/a
2423n/a if (PyType_Ready(&dequeiter_type) < 0)
2424n/a return NULL;
2425n/a Py_INCREF(&dequeiter_type);
2426n/a PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type);
2427n/a
2428n/a if (PyType_Ready(&dequereviter_type) < 0)
2429n/a return NULL;
2430n/a Py_INCREF(&dequereviter_type);
2431n/a PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type);
2432n/a
2433n/a return m;
2434n/a}