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