blob: dbb2f8a07f6f668793c86a6d7e05ed1f24111680 [file] [log] [blame]
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001#include "Python.h"
Raymond Hettinger691d8052004-05-30 07:26:47 +00002#include "structmember.h"
Raymond Hettinger756b3f32004-01-29 06:37:52 +00003
Raymond Hettingerc2083082015-02-28 23:29:16 -08004#ifdef STDC_HEADERS
5#include <stddef.h>
6#else
7#include <sys/types.h> /* For size_t */
8#endif
9
Raymond Hettinger756b3f32004-01-29 06:37:52 +000010/* collections module implementation of a deque() datatype
11 Written and maintained by Raymond D. Hettinger <python@rcn.com>
Raymond Hettingereb6b5542015-02-10 22:37:22 -060012 Copyright (c) 2004-2015 Python Software Foundation.
Raymond Hettinger756b3f32004-01-29 06:37:52 +000013 All rights reserved.
14*/
15
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000016/* The block length may be set to any number over 1. Larger numbers
Raymond Hettinger20b0f872013-06-23 15:44:33 -070017 * reduce the number of calls to the memory allocator, give faster
Raymond Hettinger30c90742015-03-02 22:31:35 -080018 * indexing and rotation, and reduce the link to data overhead ratio.
Raymond Hettingerdaf57f22015-02-26 23:21:29 -080019 * Making the block length a power of two speeds-up the modulo
Raymond Hettinger30c90742015-03-02 22:31:35 -080020 * and division calculations in deque_item() and deque_ass_item().
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000021 */
22
Raymond Hettingerdaf57f22015-02-26 23:21:29 -080023#define BLOCKLEN 64
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000024#define CENTER ((BLOCKLEN - 1) / 2)
Raymond Hettinger756b3f32004-01-29 06:37:52 +000025
Raymond Hettinger551350a2015-03-24 00:19:53 -070026/* Data for deque objects is stored in a doubly-linked list of fixed
27 * length blocks. This assures that appends or pops never move any
28 * other data elements besides the one being appended or popped.
29 *
30 * Another advantage is that it completely avoids use of realloc(),
31 * resulting in more predictable performance.
32 *
33 * Textbook implementations of doubly-linked lists store one datum
34 * per link, but that gives them a 200% memory overhead (a prev and
35 * next link for each datum) and it costs one malloc() call per data
36 * element. By using fixed-length blocks, the link to data ratio is
37 * significantly improved and there are proportionally fewer calls
38 * to malloc() and free(). The data blocks of consecutive pointers
39 * also improve cache locality.
40 *
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000041 * The list of blocks is never empty, so d.leftblock and d.rightblock
Raymond Hettinger82df9252013-07-07 01:43:42 -100042 * are never equal to NULL. The list is not circular.
43 *
44 * A deque d's first element is at d.leftblock[leftindex]
45 * and its last element is at d.rightblock[rightindex].
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000046 *
Raymond Hettinger551350a2015-03-24 00:19:53 -070047 * Unlike Python slice indices, these indices are inclusive on both
48 * ends. This makes the algorithms for left and right operations
49 * more symmetrical and it simplifies the design.
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000050 *
Raymond Hettinger551350a2015-03-24 00:19:53 -070051 * The indices, d.leftindex and d.rightindex are always in the range:
52 * 0 <= index < BLOCKLEN
53 *
54 * And their exact relationship is:
55 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000056 *
Raymond Hettinger8dbbae22015-03-24 21:01:50 -070057 * Whenever d.leftblock == d.rightblock, then:
Raymond Hettinger551350a2015-03-24 00:19:53 -070058 * d.leftindex + d.len - 1 == d.rightindex
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059 *
Raymond Hettinger551350a2015-03-24 00:19:53 -070060 * However, when d.leftblock != d.rightblock, the d.leftindex and
61 * d.rightindex become indices into distinct blocks and either may
62 * be larger than the other.
63 *
64 * Empty deques have:
65 * d.len == 0
66 * d.leftblock == d.rightblock
67 * d.leftindex == CENTER + 1
68 * d.rightindex == CENTER
69 *
70 * Checking for d.len == 0 is the intended way to see whether d is empty.
Tim Petersd8768d32004-10-01 01:32:53 +000071 */
72
Raymond Hettinger756b3f32004-01-29 06:37:52 +000073typedef struct BLOCK {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 struct BLOCK *leftlink;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 PyObject *data[BLOCKLEN];
Raymond Hettinger20b0f872013-06-23 15:44:33 -070076 struct BLOCK *rightlink;
Raymond Hettinger756b3f32004-01-29 06:37:52 +000077} block;
78
Raymond Hettinger30c90742015-03-02 22:31:35 -080079typedef struct {
80 PyObject_VAR_HEAD
81 block *leftblock;
82 block *rightblock;
Raymond Hettinger551350a2015-03-24 00:19:53 -070083 Py_ssize_t leftindex; /* 0 <= leftindex < BLOCKLEN */
84 Py_ssize_t rightindex; /* 0 <= rightindex < BLOCKLEN */
Raymond Hettingerf9d9c792015-03-02 22:47:46 -080085 size_t state; /* incremented whenever the indices move */
Raymond Hettinger30c90742015-03-02 22:31:35 -080086 Py_ssize_t maxlen;
Raymond Hettinger0f6f9472015-03-21 01:42:10 -070087 PyObject *weakreflist;
Raymond Hettinger30c90742015-03-02 22:31:35 -080088} dequeobject;
89
90static PyTypeObject deque_type;
91
Raymond Hettinger82df9252013-07-07 01:43:42 -100092/* For debug builds, add error checking to track the endpoints
93 * in the chain of links. The goal is to make sure that link
94 * assignments only take place at endpoints so that links already
95 * in use do not get overwritten.
96 *
97 * CHECK_END should happen before each assignment to a block's link field.
98 * MARK_END should happen whenever a link field becomes a new endpoint.
99 * This happens when new blocks are added or whenever an existing
100 * block is freed leaving another existing block as the new endpoint.
101 */
102
Raymond Hettinger3223dd52013-07-26 23:14:22 -0700103#ifndef NDEBUG
Raymond Hettinger82df9252013-07-07 01:43:42 -1000104#define MARK_END(link) link = NULL;
105#define CHECK_END(link) assert(link == NULL);
106#define CHECK_NOT_END(link) assert(link != NULL);
107#else
108#define MARK_END(link)
109#define CHECK_END(link)
110#define CHECK_NOT_END(link)
111#endif
112
113/* A simple freelisting scheme is used to minimize calls to the memory
Raymond Hettinger551350a2015-03-24 00:19:53 -0700114 allocator. It accommodates common use cases where new blocks are being
Raymond Hettinger82df9252013-07-07 01:43:42 -1000115 added at about the same rate as old blocks are being freed.
116 */
117
Guido van Rossum58da9312007-11-10 23:39:45 +0000118#define MAXFREEBLOCKS 10
Benjamin Petersond6313712008-07-31 16:23:04 +0000119static Py_ssize_t numfreeblocks = 0;
Guido van Rossum58da9312007-11-10 23:39:45 +0000120static block *freeblocks[MAXFREEBLOCKS];
121
Tim Peters6f853562004-10-01 01:04:50 +0000122static block *
Raymond Hettinger82df9252013-07-07 01:43:42 -1000123newblock(Py_ssize_t len) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 block *b;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700125 /* To prevent len from overflowing PY_SSIZE_T_MAX, we refuse to
126 * allocate new blocks if the current len is nearing overflow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
128 PyErr_SetString(PyExc_OverflowError,
129 "cannot add more blocks to the deque");
130 return NULL;
131 }
132 if (numfreeblocks) {
Raymond Hettinger59cf23a2013-02-07 00:57:19 -0500133 numfreeblocks--;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000134 return freeblocks[numfreeblocks];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000136 b = PyMem_Malloc(sizeof(block));
137 if (b != NULL) {
138 return b;
139 }
140 PyErr_NoMemory();
141 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000142}
143
Martin v. Löwis59683e82008-06-13 07:50:45 +0000144static void
Guido van Rossum58da9312007-11-10 23:39:45 +0000145freeblock(block *b)
146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (numfreeblocks < MAXFREEBLOCKS) {
148 freeblocks[numfreeblocks] = b;
149 numfreeblocks++;
150 } else {
151 PyMem_Free(b);
152 }
Guido van Rossum58da9312007-11-10 23:39:45 +0000153}
154
Raymond Hettingerda2850f2015-02-27 12:42:54 -0800155/* XXX Todo:
Raymond Hettinger54023152014-04-23 00:58:48 -0700156 If aligned memory allocations become available, make the
157 deque object 64 byte aligned so that all of the fields
158 can be retrieved or updated in a single cache line.
159*/
160
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000161static PyObject *
162deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 dequeobject *deque;
165 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 /* create dequeobject structure */
168 deque = (dequeobject *)type->tp_alloc(type, 0);
169 if (deque == NULL)
170 return NULL;
Tim Peters1065f752004-10-01 01:03:29 +0000171
Raymond Hettinger82df9252013-07-07 01:43:42 -1000172 b = newblock(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 if (b == NULL) {
174 Py_DECREF(deque);
175 return NULL;
176 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000177 MARK_END(b->leftlink);
178 MARK_END(b->rightlink);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 assert(BLOCKLEN >= 2);
Raymond Hettinger87e69122015-03-02 23:32:02 -0800181 Py_SIZE(deque) = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 deque->leftblock = b;
183 deque->rightblock = b;
184 deque->leftindex = CENTER + 1;
185 deque->rightindex = CENTER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 deque->state = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 deque->maxlen = -1;
Raymond Hettinger87e69122015-03-02 23:32:02 -0800188 deque->weakreflist = NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 return (PyObject *)deque;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000191}
192
193static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000194deque_pop(dequeobject *deque, PyObject *unused)
195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 PyObject *item;
197 block *prevblock;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000198
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000199 if (Py_SIZE(deque) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
201 return NULL;
202 }
203 item = deque->rightblock->data[deque->rightindex];
204 deque->rightindex--;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000205 Py_SIZE(deque)--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if (deque->rightindex == -1) {
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700209 if (Py_SIZE(deque)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 prevblock = deque->rightblock->leftlink;
211 assert(deque->leftblock != deque->rightblock);
212 freeblock(deque->rightblock);
Raymond Hettinger82df9252013-07-07 01:43:42 -1000213 CHECK_NOT_END(prevblock);
214 MARK_END(prevblock->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 deque->rightblock = prevblock;
216 deque->rightindex = BLOCKLEN - 1;
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700217 } else {
218 assert(deque->leftblock == deque->rightblock);
219 assert(deque->leftindex == deque->rightindex+1);
220 /* re-center instead of freeing a block */
221 deque->leftindex = CENTER + 1;
222 deque->rightindex = CENTER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 }
224 }
225 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000226}
227
228PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
229
230static PyObject *
231deque_popleft(dequeobject *deque, PyObject *unused)
232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 PyObject *item;
234 block *prevblock;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000235
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000236 if (Py_SIZE(deque) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
238 return NULL;
239 }
240 assert(deque->leftblock != NULL);
241 item = deque->leftblock->data[deque->leftindex];
242 deque->leftindex++;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000243 Py_SIZE(deque)--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (deque->leftindex == BLOCKLEN) {
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700247 if (Py_SIZE(deque)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 assert(deque->leftblock != deque->rightblock);
249 prevblock = deque->leftblock->rightlink;
250 freeblock(deque->leftblock);
Raymond Hettinger82df9252013-07-07 01:43:42 -1000251 CHECK_NOT_END(prevblock);
252 MARK_END(prevblock->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 deque->leftblock = prevblock;
254 deque->leftindex = 0;
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700255 } else {
256 assert(deque->leftblock == deque->rightblock);
257 assert(deque->leftindex == deque->rightindex+1);
258 /* re-center instead of freeing a block */
259 deque->leftindex = CENTER + 1;
260 deque->rightindex = CENTER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 }
262 }
263 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000264}
265
266PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
267
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800268/* The deque's size limit is d.maxlen. The limit can be zero or positive.
269 * If there is no limit, then d.maxlen == -1.
270 *
271 * After an item is added to a deque, we check to see if the size has grown past
272 * the limit. If it has, we get the size back down to the limit by popping an
273 * item off of the opposite end. The methods that can trigger this are append(),
274 * appendleft(), extend(), and extendleft().
275 */
276
277static void
278deque_trim_right(dequeobject *deque)
279{
280 if (deque->maxlen != -1 && Py_SIZE(deque) > deque->maxlen) {
281 PyObject *rv = deque_pop(deque, NULL);
282 assert(rv != NULL);
283 assert(Py_SIZE(deque) <= deque->maxlen);
284 Py_DECREF(rv);
285 }
286}
287
288static void
289deque_trim_left(dequeobject *deque)
290{
291 if (deque->maxlen != -1 && Py_SIZE(deque) > deque->maxlen) {
292 PyObject *rv = deque_popleft(deque, NULL);
293 assert(rv != NULL);
294 assert(Py_SIZE(deque) <= deque->maxlen);
295 Py_DECREF(rv);
296 }
297}
298
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000299static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000300deque_append(dequeobject *deque, PyObject *item)
301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 deque->state++;
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700303 if (deque->rightindex == BLOCKLEN - 1) {
Raymond Hettinger82df9252013-07-07 01:43:42 -1000304 block *b = newblock(Py_SIZE(deque));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (b == NULL)
306 return NULL;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000307 b->leftlink = deque->rightblock;
308 CHECK_END(deque->rightblock->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 deque->rightblock->rightlink = b;
310 deque->rightblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000311 MARK_END(b->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 deque->rightindex = -1;
313 }
314 Py_INCREF(item);
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000315 Py_SIZE(deque)++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 deque->rightindex++;
317 deque->rightblock->data[deque->rightindex] = item;
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800318 deque_trim_left(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 Py_RETURN_NONE;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000320}
321
322PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
323
324static PyObject *
325deque_appendleft(dequeobject *deque, PyObject *item)
326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 deque->state++;
328 if (deque->leftindex == 0) {
Raymond Hettinger82df9252013-07-07 01:43:42 -1000329 block *b = newblock(Py_SIZE(deque));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (b == NULL)
331 return NULL;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000332 b->rightlink = deque->leftblock;
333 CHECK_END(deque->leftblock->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 deque->leftblock->leftlink = b;
335 deque->leftblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000336 MARK_END(b->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 deque->leftindex = BLOCKLEN;
338 }
339 Py_INCREF(item);
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000340 Py_SIZE(deque)++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 deque->leftindex--;
342 deque->leftblock->data[deque->leftindex] = item;
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800343 deque_trim_right(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_RETURN_NONE;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000345}
346
347PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
348
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350/* Run an iterator to exhaustion. Shortcut for
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000351 the extend/extendleft methods when maxlen == 0. */
352static PyObject*
353consume_iterator(PyObject *it)
354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 PyObject *item;
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 while ((item = PyIter_Next(it)) != NULL) {
358 Py_DECREF(item);
359 }
360 Py_DECREF(it);
361 if (PyErr_Occurred())
362 return NULL;
363 Py_RETURN_NONE;
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000364}
365
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000366static PyObject *
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000367deque_extend(dequeobject *deque, PyObject *iterable)
368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 PyObject *it, *item;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 /* Handle case where id(deque) == id(iterable) */
372 if ((PyObject *)deque == iterable) {
373 PyObject *result;
374 PyObject *s = PySequence_List(iterable);
375 if (s == NULL)
376 return NULL;
377 result = deque_extend(deque, s);
378 Py_DECREF(s);
379 return result;
380 }
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000381
Raymond Hettingerd9c116c2013-07-09 00:13:21 -0700382 /* Space saving heuristic. Start filling from the left */
383 if (Py_SIZE(deque) == 0) {
384 assert(deque->leftblock == deque->rightblock);
385 assert(deque->leftindex == deque->rightindex+1);
386 deque->leftindex = 1;
387 deque->rightindex = 0;
388 }
389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 it = PyObject_GetIter(iterable);
391 if (it == NULL)
392 return NULL;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (deque->maxlen == 0)
395 return consume_iterator(it);
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 while ((item = PyIter_Next(it)) != NULL) {
398 deque->state++;
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700399 if (deque->rightindex == BLOCKLEN - 1) {
Raymond Hettinger82df9252013-07-07 01:43:42 -1000400 block *b = newblock(Py_SIZE(deque));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (b == NULL) {
402 Py_DECREF(item);
403 Py_DECREF(it);
404 return NULL;
405 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000406 b->leftlink = deque->rightblock;
407 CHECK_END(deque->rightblock->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 deque->rightblock->rightlink = b;
409 deque->rightblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000410 MARK_END(b->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 deque->rightindex = -1;
412 }
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000413 Py_SIZE(deque)++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 deque->rightindex++;
415 deque->rightblock->data[deque->rightindex] = item;
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800416 deque_trim_left(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 }
418 Py_DECREF(it);
419 if (PyErr_Occurred())
420 return NULL;
421 Py_RETURN_NONE;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000422}
423
Tim Peters1065f752004-10-01 01:03:29 +0000424PyDoc_STRVAR(extend_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000425"Extend the right side of the deque with elements from the iterable");
426
427static PyObject *
428deque_extendleft(dequeobject *deque, PyObject *iterable)
429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyObject *it, *item;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 /* Handle case where id(deque) == id(iterable) */
433 if ((PyObject *)deque == iterable) {
434 PyObject *result;
435 PyObject *s = PySequence_List(iterable);
436 if (s == NULL)
437 return NULL;
438 result = deque_extendleft(deque, s);
439 Py_DECREF(s);
440 return result;
441 }
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000442
Raymond Hettingerd9c116c2013-07-09 00:13:21 -0700443 /* Space saving heuristic. Start filling from the right */
444 if (Py_SIZE(deque) == 0) {
445 assert(deque->leftblock == deque->rightblock);
446 assert(deque->leftindex == deque->rightindex+1);
447 deque->leftindex = BLOCKLEN - 1;
448 deque->rightindex = BLOCKLEN - 2;
449 }
450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 it = PyObject_GetIter(iterable);
452 if (it == NULL)
453 return NULL;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (deque->maxlen == 0)
456 return consume_iterator(it);
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 while ((item = PyIter_Next(it)) != NULL) {
459 deque->state++;
460 if (deque->leftindex == 0) {
Raymond Hettinger82df9252013-07-07 01:43:42 -1000461 block *b = newblock(Py_SIZE(deque));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 if (b == NULL) {
463 Py_DECREF(item);
464 Py_DECREF(it);
465 return NULL;
466 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000467 b->rightlink = deque->leftblock;
468 CHECK_END(deque->leftblock->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 deque->leftblock->leftlink = b;
470 deque->leftblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000471 MARK_END(b->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 deque->leftindex = BLOCKLEN;
473 }
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000474 Py_SIZE(deque)++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 deque->leftindex--;
476 deque->leftblock->data[deque->leftindex] = item;
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800477 deque_trim_right(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 }
479 Py_DECREF(it);
480 if (PyErr_Occurred())
481 return NULL;
482 Py_RETURN_NONE;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000483}
484
Tim Peters1065f752004-10-01 01:03:29 +0000485PyDoc_STRVAR(extendleft_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000486"Extend the left side of the deque with elements from the iterable");
487
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000488static PyObject *
489deque_inplace_concat(dequeobject *deque, PyObject *other)
490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyObject *result;
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 result = deque_extend(deque, other);
494 if (result == NULL)
495 return result;
496 Py_DECREF(result);
497 Py_INCREF(deque);
498 return (PyObject *)deque;
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000499}
500
Raymond Hettinger54023152014-04-23 00:58:48 -0700501/* The rotate() method is part of the public API and is used internally
502as a primitive for other methods.
503
504Rotation by 1 or -1 is a common case, so any optimizations for high
505volume rotations should take care not to penalize the common case.
506
507Conceptually, a rotate by one is equivalent to a pop on one side and an
508append on the other. However, a pop/append pair is unnecessarily slow
509because it requires a incref/decref pair for an object located randomly
510in memory. It is better to just move the object pointer from one block
511to the next without changing the reference count.
512
513When moving batches of pointers, it is tempting to use memcpy() but that
514proved to be slower than a simple loop for a variety of reasons.
515Memcpy() cannot know in advance that we're copying pointers instead of
516bytes, that the source and destination are pointer aligned and
517non-overlapping, that moving just one pointer is a common case, that we
518never need to move more than BLOCKLEN pointers, and that at least one
519pointer is always moved.
520
521For high volume rotations, newblock() and freeblock() are never called
522more than once. Previously emptied blocks are immediately reused as a
523destination block. If a block is left-over at the end, it is freed.
524*/
525
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000526static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527_deque_rotate(dequeobject *deque, Py_ssize_t n)
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000528{
Raymond Hettinger3959af92013-07-13 02:34:08 -0700529 block *b = NULL;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700530 block *leftblock = deque->leftblock;
531 block *rightblock = deque->rightblock;
532 Py_ssize_t leftindex = deque->leftindex;
533 Py_ssize_t rightindex = deque->rightindex;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000534 Py_ssize_t len=Py_SIZE(deque), halflen=len>>1;
Raymond Hettinger3959af92013-07-13 02:34:08 -0700535 int rv = -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000536
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800537 if (len <= 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return 0;
539 if (n > halflen || n < -halflen) {
540 n %= len;
541 if (n > halflen)
542 n -= len;
543 else if (n < -halflen)
544 n += len;
545 }
Raymond Hettinger1f0044c2013-02-05 01:30:46 -0500546 assert(len > 1);
Raymond Hettingera4409c12013-02-04 00:08:12 -0500547 assert(-halflen <= n && n <= halflen);
Raymond Hettinger231ee4d2013-02-02 11:24:43 -0800548
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800549 deque->state++;
Raymond Hettinger1f0044c2013-02-05 01:30:46 -0500550 while (n > 0) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700551 if (leftindex == 0) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700552 if (b == NULL) {
Raymond Hettinger3959af92013-07-13 02:34:08 -0700553 b = newblock(len);
554 if (b == NULL)
555 goto done;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700556 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000557 b->rightlink = leftblock;
558 CHECK_END(leftblock->leftlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700559 leftblock->leftlink = b;
560 leftblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000561 MARK_END(b->leftlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700562 leftindex = BLOCKLEN;
Raymond Hettinger3959af92013-07-13 02:34:08 -0700563 b = NULL;
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800564 }
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700565 assert(leftindex > 0);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700566 {
567 PyObject **src, **dest;
568 Py_ssize_t m = n;
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800569
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700570 if (m > rightindex + 1)
571 m = rightindex + 1;
572 if (m > leftindex)
573 m = leftindex;
574 assert (m > 0 && m <= len);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700575 rightindex -= m;
576 leftindex -= m;
Raymond Hettinger0e259f12015-02-01 22:53:41 -0800577 src = &rightblock->data[rightindex + 1];
578 dest = &leftblock->data[leftindex];
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700579 n -= m;
Raymond Hettinger840533b2013-07-13 17:03:58 -0700580 do {
Raymond Hettinger0e259f12015-02-01 22:53:41 -0800581 *(dest++) = *(src++);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700582 } while (--m);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700583 }
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700584 if (rightindex == -1) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700585 assert(leftblock != rightblock);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700586 assert(b == NULL);
Raymond Hettinger3959af92013-07-13 02:34:08 -0700587 b = rightblock;
Raymond Hettingerb97cc492013-07-21 01:51:07 -0700588 CHECK_NOT_END(rightblock->leftlink);
589 rightblock = rightblock->leftlink;
590 MARK_END(rightblock->rightlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700591 rightindex = BLOCKLEN - 1;
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800592 }
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800593 }
Raymond Hettinger1f0044c2013-02-05 01:30:46 -0500594 while (n < 0) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700595 if (rightindex == BLOCKLEN - 1) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700596 if (b == NULL) {
Raymond Hettinger3959af92013-07-13 02:34:08 -0700597 b = newblock(len);
598 if (b == NULL)
599 goto done;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700600 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000601 b->leftlink = rightblock;
602 CHECK_END(rightblock->rightlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700603 rightblock->rightlink = b;
604 rightblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000605 MARK_END(b->rightlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700606 rightindex = -1;
Raymond Hettinger3959af92013-07-13 02:34:08 -0700607 b = NULL;
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800608 }
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700609 assert (rightindex < BLOCKLEN - 1);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700610 {
611 PyObject **src, **dest;
612 Py_ssize_t m = -n;
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800613
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700614 if (m > BLOCKLEN - leftindex)
615 m = BLOCKLEN - leftindex;
616 if (m > BLOCKLEN - 1 - rightindex)
617 m = BLOCKLEN - 1 - rightindex;
618 assert (m > 0 && m <= len);
619 src = &leftblock->data[leftindex];
620 dest = &rightblock->data[rightindex + 1];
621 leftindex += m;
622 rightindex += m;
623 n += m;
Raymond Hettinger840533b2013-07-13 17:03:58 -0700624 do {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700625 *(dest++) = *(src++);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700626 } while (--m);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700627 }
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700628 if (leftindex == BLOCKLEN) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700629 assert(leftblock != rightblock);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700630 assert(b == NULL);
Raymond Hettinger3959af92013-07-13 02:34:08 -0700631 b = leftblock;
Raymond Hettingerb97cc492013-07-21 01:51:07 -0700632 CHECK_NOT_END(leftblock->rightlink);
633 leftblock = leftblock->rightlink;
634 MARK_END(leftblock->leftlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700635 leftindex = 0;
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 }
Raymond Hettinger3959af92013-07-13 02:34:08 -0700638 rv = 0;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700639done:
Raymond Hettinger3959af92013-07-13 02:34:08 -0700640 if (b != NULL)
641 freeblock(b);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700642 deque->leftblock = leftblock;
643 deque->rightblock = rightblock;
644 deque->leftindex = leftindex;
645 deque->rightindex = rightindex;
646
647 return rv;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000648}
649
650static PyObject *
651deque_rotate(dequeobject *deque, PyObject *args)
652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 Py_ssize_t n=1;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (!PyArg_ParseTuple(args, "|n:rotate", &n))
656 return NULL;
Raymond Hettinger6921c132015-03-21 02:03:40 -0700657 if (!_deque_rotate(deque, n))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 Py_RETURN_NONE;
659 return NULL;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000660}
661
Tim Peters1065f752004-10-01 01:03:29 +0000662PyDoc_STRVAR(rotate_doc,
Raymond Hettingeree33b272004-02-08 04:05:26 +0000663"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000664
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000665static PyObject *
666deque_reverse(dequeobject *deque, PyObject *unused)
667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 block *leftblock = deque->leftblock;
669 block *rightblock = deque->rightblock;
670 Py_ssize_t leftindex = deque->leftindex;
671 Py_ssize_t rightindex = deque->rightindex;
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700672 Py_ssize_t n = Py_SIZE(deque) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 Py_ssize_t i;
674 PyObject *tmp;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 for (i=0 ; i<n ; i++) {
677 /* Validate that pointers haven't met in the middle */
678 assert(leftblock != rightblock || leftindex < rightindex);
Raymond Hettinger82df9252013-07-07 01:43:42 -1000679 CHECK_NOT_END(leftblock);
680 CHECK_NOT_END(rightblock);
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* Swap */
683 tmp = leftblock->data[leftindex];
684 leftblock->data[leftindex] = rightblock->data[rightindex];
685 rightblock->data[rightindex] = tmp;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 /* Advance left block/index pair */
688 leftindex++;
689 if (leftindex == BLOCKLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 leftblock = leftblock->rightlink;
691 leftindex = 0;
692 }
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 /* Step backwards with the right block/index pair */
695 rightindex--;
696 if (rightindex == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 rightblock = rightblock->leftlink;
698 rightindex = BLOCKLEN - 1;
699 }
700 }
701 Py_RETURN_NONE;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000702}
703
704PyDoc_STRVAR(reverse_doc,
705"D.reverse() -- reverse *IN PLACE*");
706
Raymond Hettinger44459de2010-04-03 23:20:46 +0000707static PyObject *
708deque_count(dequeobject *deque, PyObject *v)
709{
Raymond Hettingerf3a67b72013-07-06 17:49:06 -1000710 block *b = deque->leftblock;
711 Py_ssize_t index = deque->leftindex;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000712 Py_ssize_t n = Py_SIZE(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 Py_ssize_t i;
714 Py_ssize_t count = 0;
Raymond Hettingerf9d9c792015-03-02 22:47:46 -0800715 size_t start_state = deque->state;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyObject *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 int cmp;
Raymond Hettinger44459de2010-04-03 23:20:46 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 for (i=0 ; i<n ; i++) {
Raymond Hettinger82df9252013-07-07 01:43:42 -1000720 CHECK_NOT_END(b);
Raymond Hettingerf3a67b72013-07-06 17:49:06 -1000721 item = b->data[index];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
723 if (cmp > 0)
724 count++;
725 else if (cmp < 0)
726 return NULL;
Raymond Hettinger44459de2010-04-03 23:20:46 +0000727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 if (start_state != deque->state) {
729 PyErr_SetString(PyExc_RuntimeError,
730 "deque mutated during iteration");
731 return NULL;
732 }
Raymond Hettinger44459de2010-04-03 23:20:46 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* Advance left block/index pair */
Raymond Hettingerf3a67b72013-07-06 17:49:06 -1000735 index++;
736 if (index == BLOCKLEN) {
737 b = b->rightlink;
738 index = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 }
740 }
741 return PyLong_FromSsize_t(count);
Raymond Hettinger44459de2010-04-03 23:20:46 +0000742}
743
744PyDoc_STRVAR(count_doc,
745"D.count(value) -> integer -- return number of occurrences of value");
746
Raymond Hettinger39dadf72015-03-20 16:38:56 -0700747static int
748deque_contains(dequeobject *deque, PyObject *v)
749{
750 block *b = deque->leftblock;
751 Py_ssize_t index = deque->leftindex;
752 Py_ssize_t n = Py_SIZE(deque);
753 Py_ssize_t i;
754 size_t start_state = deque->state;
755 PyObject *item;
756 int cmp;
757
758 for (i=0 ; i<n ; i++) {
759 CHECK_NOT_END(b);
760 item = b->data[index];
761 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
762 if (cmp) {
763 return cmp;
764 }
765 if (start_state != deque->state) {
766 PyErr_SetString(PyExc_RuntimeError,
767 "deque mutated during iteration");
768 return -1;
769 }
770 index++;
771 if (index == BLOCKLEN) {
772 b = b->rightlink;
773 index = 0;
774 }
775 }
776 return 0;
777}
778
Martin v. Löwis18e16552006-02-15 17:27:45 +0000779static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000780deque_len(dequeobject *deque)
781{
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000782 return Py_SIZE(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000783}
784
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000785static PyObject *
Raymond Hettinger32ea1652015-03-21 01:37:37 -0700786deque_index(dequeobject *deque, PyObject *args)
787{
788 Py_ssize_t i, start=0, stop=Py_SIZE(deque);
789 PyObject *v, *item;
790 block *b = deque->leftblock;
791 Py_ssize_t index = deque->leftindex;
792 size_t start_state = deque->state;
793
794 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
795 _PyEval_SliceIndex, &start,
796 _PyEval_SliceIndex, &stop))
797 return NULL;
798 if (start < 0) {
799 start += Py_SIZE(deque);
800 if (start < 0)
801 start = 0;
802 }
803 if (stop < 0) {
804 stop += Py_SIZE(deque);
805 if (stop < 0)
806 stop = 0;
807 }
808
809 for (i=0 ; i<stop ; i++) {
810 if (i >= start) {
811 int cmp;
812 CHECK_NOT_END(b);
813 item = b->data[index];
814 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
815 if (cmp > 0)
816 return PyLong_FromSsize_t(i);
817 else if (cmp < 0)
818 return NULL;
819 if (start_state != deque->state) {
820 PyErr_SetString(PyExc_RuntimeError,
821 "deque mutated during iteration");
822 return NULL;
823 }
824 }
825 index++;
826 if (index == BLOCKLEN) {
827 b = b->rightlink;
828 index = 0;
829 }
830 }
831 PyErr_Format(PyExc_ValueError, "%R is not in deque", v);
832 return NULL;
833}
834
835PyDoc_STRVAR(index_doc,
836"D.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
837"Raises ValueError if the value is not present.");
838
Raymond Hettinger551350a2015-03-24 00:19:53 -0700839/* insert(), remove(), and delitem() are implemented in terms of
840 rotate() for simplicity and reasonable performance near the end
841 points. If for some reason these methods become popular, it is not
842 hard to re-implement this using direct data movement (similar to
843 the code used in list slice assignments) and achieve a performance
Raymond Hettingerfef9c1b2015-03-24 21:12:57 -0700844 boost (by moving each pointer only once instead of twice).
Raymond Hettinger551350a2015-03-24 00:19:53 -0700845*/
846
Raymond Hettinger32ea1652015-03-21 01:37:37 -0700847static PyObject *
848deque_insert(dequeobject *deque, PyObject *args)
849{
850 Py_ssize_t index;
851 Py_ssize_t n = Py_SIZE(deque);
852 PyObject *value;
853 PyObject *rv;
854
855 if (!PyArg_ParseTuple(args, "nO:insert", &index, &value))
856 return NULL;
857 if (index >= n)
858 return deque_append(deque, value);
859 if (index <= -n || index == 0)
860 return deque_appendleft(deque, value);
861 if (_deque_rotate(deque, -index))
862 return NULL;
863 if (index < 0)
864 rv = deque_append(deque, value);
865 else
866 rv = deque_appendleft(deque, value);
867 if (rv == NULL)
868 return NULL;
869 Py_DECREF(rv);
870 if (_deque_rotate(deque, index))
871 return NULL;
872 Py_RETURN_NONE;
873}
874
875PyDoc_STRVAR(insert_doc,
876"D.insert(index, object) -- insert object before index");
877
878static PyObject *
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000879deque_remove(dequeobject *deque, PyObject *value)
880{
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000881 Py_ssize_t i, n=Py_SIZE(deque);
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 for (i=0 ; i<n ; i++) {
884 PyObject *item = deque->leftblock->data[deque->leftindex];
885 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000886
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000887 if (Py_SIZE(deque) != n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyErr_SetString(PyExc_IndexError,
889 "deque mutated during remove().");
890 return NULL;
891 }
892 if (cmp > 0) {
893 PyObject *tgt = deque_popleft(deque, NULL);
894 assert (tgt != NULL);
Raymond Hettinger6921c132015-03-21 02:03:40 -0700895 if (_deque_rotate(deque, i))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 return NULL;
Raymond Hettingerac13ad62015-03-21 01:53:16 -0700897 Py_DECREF(tgt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Py_RETURN_NONE;
899 }
900 else if (cmp < 0) {
901 _deque_rotate(deque, i);
902 return NULL;
903 }
904 _deque_rotate(deque, -1);
905 }
906 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
907 return NULL;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000908}
909
910PyDoc_STRVAR(remove_doc,
911"D.remove(value) -- remove first occurrence of value.");
912
Benjamin Peterson0e5c48a2013-01-12 21:22:18 -0500913static void
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000914deque_clear(dequeobject *deque)
915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyObject *item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000917
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000918 while (Py_SIZE(deque)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 item = deque_pop(deque, NULL);
920 assert (item != NULL);
921 Py_DECREF(item);
922 }
Raymond Hettinger87e69122015-03-02 23:32:02 -0800923 assert(deque->leftblock == deque->rightblock);
924 assert(deque->leftindex - 1 == deque->rightindex);
925 assert(Py_SIZE(deque) == 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000926}
927
Raymond Hettinger3c186ba2015-03-02 21:45:02 -0800928static int
929valid_index(Py_ssize_t i, Py_ssize_t limit)
930{
931 /* The cast to size_t let us use just a single comparison
932 to check whether i is in the range: 0 <= i < limit */
933 return (size_t) i < (size_t) limit;
934}
935
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000936static PyObject *
Benjamin Petersond6313712008-07-31 16:23:04 +0000937deque_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 block *b;
940 PyObject *item;
941 Py_ssize_t n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000942
Raymond Hettinger3c186ba2015-03-02 21:45:02 -0800943 if (!valid_index(i, Py_SIZE(deque))) {
944 PyErr_SetString(PyExc_IndexError, "deque index out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 return NULL;
946 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 if (i == 0) {
949 i = deque->leftindex;
950 b = deque->leftblock;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000951 } else if (i == Py_SIZE(deque) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 i = deque->rightindex;
953 b = deque->rightblock;
954 } else {
955 i += deque->leftindex;
Raymond Hettingerc2083082015-02-28 23:29:16 -0800956 n = (Py_ssize_t)((size_t) i / BLOCKLEN);
957 i = (Py_ssize_t)((size_t) i % BLOCKLEN);
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000958 if (index < (Py_SIZE(deque) >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 b = deque->leftblock;
960 while (n--)
961 b = b->rightlink;
962 } else {
Raymond Hettinger63d1ff22015-02-28 07:41:30 -0800963 n = (Py_ssize_t)(
Raymond Hettingerc2083082015-02-28 23:29:16 -0800964 ((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
Raymond Hettinger63d1ff22015-02-28 07:41:30 -0800965 / BLOCKLEN - n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 b = deque->rightblock;
967 while (n--)
968 b = b->leftlink;
969 }
970 }
971 item = b->data[i];
972 Py_INCREF(item);
973 return item;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000974}
975
976static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000977deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyObject *item;
Raymond Hettingerac13ad62015-03-21 01:53:16 -0700980 int rv;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000981
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000982 assert (i >= 0 && i < Py_SIZE(deque));
Raymond Hettinger6921c132015-03-21 02:03:40 -0700983 if (_deque_rotate(deque, -i))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 item = deque_popleft(deque, NULL);
Raymond Hettingerac13ad62015-03-21 01:53:16 -0700986 rv = _deque_rotate(deque, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 assert (item != NULL);
988 Py_DECREF(item);
Raymond Hettingerac13ad62015-03-21 01:53:16 -0700989 return rv;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000990}
991
992static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000993deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyObject *old_value;
996 block *b;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000997 Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000998
Raymond Hettinger3c186ba2015-03-02 21:45:02 -0800999 if (!valid_index(i, len)) {
1000 PyErr_SetString(PyExc_IndexError, "deque index out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 return -1;
1002 }
1003 if (v == NULL)
1004 return deque_del_item(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 i += deque->leftindex;
Raymond Hettingerc2083082015-02-28 23:29:16 -08001007 n = (Py_ssize_t)((size_t) i / BLOCKLEN);
1008 i = (Py_ssize_t)((size_t) i % BLOCKLEN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 if (index <= halflen) {
1010 b = deque->leftblock;
1011 while (n--)
1012 b = b->rightlink;
1013 } else {
Raymond Hettingera473b9d2015-02-28 17:49:47 -08001014 n = (Py_ssize_t)(
Raymond Hettingerc2083082015-02-28 23:29:16 -08001015 ((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
Raymond Hettingera473b9d2015-02-28 17:49:47 -08001016 / BLOCKLEN - n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 b = deque->rightblock;
1018 while (n--)
1019 b = b->leftlink;
1020 }
1021 Py_INCREF(v);
1022 old_value = b->data[i];
1023 b->data[i] = v;
1024 Py_DECREF(old_value);
1025 return 0;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001026}
1027
1028static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001029deque_clearmethod(dequeobject *deque)
1030{
Benjamin Peterson0e5c48a2013-01-12 21:22:18 -05001031 deque_clear(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 Py_RETURN_NONE;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001033}
1034
1035PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
1036
1037static void
1038deque_dealloc(dequeobject *deque)
1039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyObject_GC_UnTrack(deque);
1041 if (deque->weakreflist != NULL)
1042 PyObject_ClearWeakRefs((PyObject *) deque);
1043 if (deque->leftblock != NULL) {
1044 deque_clear(deque);
1045 assert(deque->leftblock != NULL);
1046 freeblock(deque->leftblock);
1047 }
1048 deque->leftblock = NULL;
1049 deque->rightblock = NULL;
1050 Py_TYPE(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001051}
1052
1053static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001054deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 block *b;
1057 PyObject *item;
1058 Py_ssize_t index;
1059 Py_ssize_t indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001060
Raymond Hettinger5bfa8672013-07-06 11:58:09 -10001061 for (b = deque->leftblock; b != deque->rightblock; b = b->rightlink) {
1062 for (index = indexlo; index < BLOCKLEN ; index++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 item = b->data[index];
1064 Py_VISIT(item);
1065 }
1066 indexlo = 0;
1067 }
Raymond Hettinger5bfa8672013-07-06 11:58:09 -10001068 for (index = indexlo; index <= deque->rightindex; index++) {
1069 item = b->data[index];
1070 Py_VISIT(item);
1071 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 return 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001073}
1074
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001075static PyObject *
1076deque_copy(PyObject *deque)
1077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (((dequeobject *)deque)->maxlen == -1)
1079 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
1080 else
1081 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
1082 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001083}
1084
1085PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
1086
1087static PyObject *
1088deque_reduce(dequeobject *deque)
1089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 PyObject *dict, *result, *aslist;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001091 _Py_IDENTIFIER(__dict__);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001092
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001093 dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (dict == NULL)
1095 PyErr_Clear();
1096 aslist = PySequence_List((PyObject *)deque);
1097 if (aslist == NULL) {
1098 Py_XDECREF(dict);
1099 return NULL;
1100 }
1101 if (dict == NULL) {
1102 if (deque->maxlen == -1)
1103 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
1104 else
1105 result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
1106 } else {
1107 if (deque->maxlen == -1)
1108 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
1109 else
1110 result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
1111 }
1112 Py_XDECREF(dict);
1113 Py_DECREF(aslist);
1114 return result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001115}
1116
1117PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1118
1119static PyObject *
1120deque_repr(PyObject *deque)
1121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 PyObject *aslist, *result;
1123 int i;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 i = Py_ReprEnter(deque);
1126 if (i != 0) {
1127 if (i < 0)
1128 return NULL;
1129 return PyUnicode_FromString("[...]");
1130 }
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 aslist = PySequence_List(deque);
1133 if (aslist == NULL) {
1134 Py_ReprLeave(deque);
1135 return NULL;
1136 }
1137 if (((dequeobject *)deque)->maxlen != -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
1139 aslist, ((dequeobject *)deque)->maxlen);
1140 else
1141 result = PyUnicode_FromFormat("deque(%R)", aslist);
1142 Py_DECREF(aslist);
1143 Py_ReprLeave(deque);
1144 return result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001145}
1146
Raymond Hettinger738ec902004-02-29 02:15:56 +00001147static PyObject *
1148deque_richcompare(PyObject *v, PyObject *w, int op)
1149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 PyObject *it1=NULL, *it2=NULL, *x, *y;
1151 Py_ssize_t vs, ws;
1152 int b, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (!PyObject_TypeCheck(v, &deque_type) ||
1155 !PyObject_TypeCheck(w, &deque_type)) {
Brian Curtindfc80e32011-08-10 20:28:54 -05001156 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 }
Raymond Hettinger738ec902004-02-29 02:15:56 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 /* Shortcuts */
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001160 vs = Py_SIZE((dequeobject *)v);
1161 ws = Py_SIZE((dequeobject *)w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (op == Py_EQ) {
1163 if (v == w)
1164 Py_RETURN_TRUE;
1165 if (vs != ws)
1166 Py_RETURN_FALSE;
1167 }
1168 if (op == Py_NE) {
1169 if (v == w)
1170 Py_RETURN_FALSE;
1171 if (vs != ws)
1172 Py_RETURN_TRUE;
1173 }
Raymond Hettinger738ec902004-02-29 02:15:56 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 /* Search for the first index where items are different */
1176 it1 = PyObject_GetIter(v);
1177 if (it1 == NULL)
1178 goto done;
1179 it2 = PyObject_GetIter(w);
1180 if (it2 == NULL)
1181 goto done;
1182 for (;;) {
1183 x = PyIter_Next(it1);
1184 if (x == NULL && PyErr_Occurred())
1185 goto done;
1186 y = PyIter_Next(it2);
1187 if (x == NULL || y == NULL)
1188 break;
1189 b = PyObject_RichCompareBool(x, y, Py_EQ);
1190 if (b == 0) {
1191 cmp = PyObject_RichCompareBool(x, y, op);
1192 Py_DECREF(x);
1193 Py_DECREF(y);
1194 goto done;
1195 }
1196 Py_DECREF(x);
1197 Py_DECREF(y);
1198 if (b == -1)
1199 goto done;
1200 }
1201 /* We reached the end of one deque or both */
1202 Py_XDECREF(x);
1203 Py_XDECREF(y);
1204 if (PyErr_Occurred())
1205 goto done;
1206 switch (op) {
1207 case Py_LT: cmp = y != NULL; break; /* if w was longer */
1208 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
1209 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
1210 case Py_NE: cmp = x != y; break; /* if one deque continues */
1211 case Py_GT: cmp = x != NULL; break; /* if v was longer */
1212 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
1213 }
Tim Peters1065f752004-10-01 01:03:29 +00001214
Raymond Hettinger738ec902004-02-29 02:15:56 +00001215done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 Py_XDECREF(it1);
1217 Py_XDECREF(it2);
1218 if (cmp == 1)
1219 Py_RETURN_TRUE;
1220 if (cmp == 0)
1221 Py_RETURN_FALSE;
1222 return NULL;
Raymond Hettinger738ec902004-02-29 02:15:56 +00001223}
1224
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001225static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001226deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 PyObject *iterable = NULL;
1229 PyObject *maxlenobj = NULL;
1230 Py_ssize_t maxlen = -1;
1231 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
1234 return -1;
1235 if (maxlenobj != NULL && maxlenobj != Py_None) {
1236 maxlen = PyLong_AsSsize_t(maxlenobj);
1237 if (maxlen == -1 && PyErr_Occurred())
1238 return -1;
1239 if (maxlen < 0) {
1240 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
1241 return -1;
1242 }
1243 }
1244 deque->maxlen = maxlen;
1245 deque_clear(deque);
1246 if (iterable != NULL) {
1247 PyObject *rv = deque_extend(deque, iterable);
1248 if (rv == NULL)
1249 return -1;
1250 Py_DECREF(rv);
1251 }
1252 return 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001253}
1254
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001255static PyObject *
Jesus Cea16e2fca2012-08-03 14:49:42 +02001256deque_sizeof(dequeobject *deque, void *unused)
1257{
1258 Py_ssize_t res;
1259 Py_ssize_t blocks;
1260
1261 res = sizeof(dequeobject);
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001262 blocks = (deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
1263 assert(deque->leftindex + Py_SIZE(deque) - 1 ==
Jesus Cea16e2fca2012-08-03 14:49:42 +02001264 (blocks - 1) * BLOCKLEN + deque->rightindex);
1265 res += blocks * sizeof(block);
1266 return PyLong_FromSsize_t(res);
1267}
1268
1269PyDoc_STRVAR(sizeof_doc,
1270"D.__sizeof__() -- size of D in memory, in bytes");
1271
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001272static int
1273deque_bool(dequeobject *deque)
1274{
1275 return Py_SIZE(deque) != 0;
1276}
1277
Jesus Cea16e2fca2012-08-03 14:49:42 +02001278static PyObject *
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001279deque_get_maxlen(dequeobject *deque)
1280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (deque->maxlen == -1)
1282 Py_RETURN_NONE;
1283 return PyLong_FromSsize_t(deque->maxlen);
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001284}
1285
1286static PyGetSetDef deque_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
1288 "maximum size of a deque or None if unbounded"},
1289 {0}
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001290};
1291
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001292static PySequenceMethods deque_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 (lenfunc)deque_len, /* sq_length */
1294 0, /* sq_concat */
1295 0, /* sq_repeat */
1296 (ssizeargfunc)deque_item, /* sq_item */
1297 0, /* sq_slice */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001298 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 0, /* sq_ass_slice */
Raymond Hettinger39dadf72015-03-20 16:38:56 -07001300 (objobjproc)deque_contains, /* sq_contains */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001301 (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 0, /* sq_inplace_repeat */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001303};
1304
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001305static PyNumberMethods deque_as_number = {
1306 0, /* nb_add */
1307 0, /* nb_subtract */
1308 0, /* nb_multiply */
1309 0, /* nb_remainder */
1310 0, /* nb_divmod */
1311 0, /* nb_power */
1312 0, /* nb_negative */
1313 0, /* nb_positive */
1314 0, /* nb_absolute */
1315 (inquiry)deque_bool, /* nb_bool */
1316 0, /* nb_invert */
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001317 };
1318
1319
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001320/* deque object ********************************************************/
1321
1322static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001323static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +00001324PyDoc_STRVAR(reversed_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001326
1327static PyMethodDef deque_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 {"append", (PyCFunction)deque_append,
1329 METH_O, append_doc},
1330 {"appendleft", (PyCFunction)deque_appendleft,
1331 METH_O, appendleft_doc},
1332 {"clear", (PyCFunction)deque_clearmethod,
1333 METH_NOARGS, clear_doc},
1334 {"__copy__", (PyCFunction)deque_copy,
1335 METH_NOARGS, copy_doc},
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001336 {"copy", (PyCFunction)deque_copy,
1337 METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 {"count", (PyCFunction)deque_count,
Raymond Hettinger0f6f9472015-03-21 01:42:10 -07001339 METH_O, count_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 {"extend", (PyCFunction)deque_extend,
1341 METH_O, extend_doc},
1342 {"extendleft", (PyCFunction)deque_extendleft,
1343 METH_O, extendleft_doc},
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001344 {"index", (PyCFunction)deque_index,
1345 METH_VARARGS, index_doc},
1346 {"insert", (PyCFunction)deque_insert,
1347 METH_VARARGS, insert_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 {"pop", (PyCFunction)deque_pop,
1349 METH_NOARGS, pop_doc},
1350 {"popleft", (PyCFunction)deque_popleft,
1351 METH_NOARGS, popleft_doc},
Raymond Hettinger0f6f9472015-03-21 01:42:10 -07001352 {"__reduce__", (PyCFunction)deque_reduce,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 METH_NOARGS, reduce_doc},
1354 {"remove", (PyCFunction)deque_remove,
1355 METH_O, remove_doc},
1356 {"__reversed__", (PyCFunction)deque_reviter,
1357 METH_NOARGS, reversed_doc},
1358 {"reverse", (PyCFunction)deque_reverse,
1359 METH_NOARGS, reverse_doc},
1360 {"rotate", (PyCFunction)deque_rotate,
Jesus Cea16e2fca2012-08-03 14:49:42 +02001361 METH_VARARGS, rotate_doc},
1362 {"__sizeof__", (PyCFunction)deque_sizeof,
1363 METH_NOARGS, sizeof_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 {NULL, NULL} /* sentinel */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001365};
1366
1367PyDoc_STRVAR(deque_doc,
Andrew Svetlov6a5c7c32012-10-31 11:50:40 +02001368"deque([iterable[, maxlen]]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001369\n\
Raymond Hettinger49747052011-03-29 17:36:31 -07001370Build an ordered collection with optimized access from its endpoints.");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001371
Neal Norwitz87f10132004-02-29 15:40:53 +00001372static PyTypeObject deque_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 PyVarObject_HEAD_INIT(NULL, 0)
1374 "collections.deque", /* tp_name */
1375 sizeof(dequeobject), /* tp_basicsize */
1376 0, /* tp_itemsize */
1377 /* methods */
1378 (destructor)deque_dealloc, /* tp_dealloc */
1379 0, /* tp_print */
1380 0, /* tp_getattr */
1381 0, /* tp_setattr */
1382 0, /* tp_reserved */
1383 deque_repr, /* tp_repr */
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001384 &deque_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 &deque_as_sequence, /* tp_as_sequence */
1386 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00001387 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 0, /* tp_call */
1389 0, /* tp_str */
1390 PyObject_GenericGetAttr, /* tp_getattro */
1391 0, /* tp_setattro */
1392 0, /* tp_as_buffer */
1393 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandlf038b322010-10-18 07:35:09 +00001394 /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 deque_doc, /* tp_doc */
1396 (traverseproc)deque_traverse, /* tp_traverse */
1397 (inquiry)deque_clear, /* tp_clear */
1398 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Georg Brandlf038b322010-10-18 07:35:09 +00001399 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 (getiterfunc)deque_iter, /* tp_iter */
1401 0, /* tp_iternext */
1402 deque_methods, /* tp_methods */
1403 0, /* tp_members */
Georg Brandlf038b322010-10-18 07:35:09 +00001404 deque_getset, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 0, /* tp_base */
1406 0, /* tp_dict */
1407 0, /* tp_descr_get */
1408 0, /* tp_descr_set */
1409 0, /* tp_dictoffset */
1410 (initproc)deque_init, /* tp_init */
1411 PyType_GenericAlloc, /* tp_alloc */
1412 deque_new, /* tp_new */
1413 PyObject_GC_Del, /* tp_free */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001414};
1415
1416/*********************** Deque Iterator **************************/
1417
1418typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 block *b;
Raymond Hettinger87e69122015-03-02 23:32:02 -08001421 Py_ssize_t index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 dequeobject *deque;
Raymond Hettingerf9d9c792015-03-02 22:47:46 -08001423 size_t state; /* state when the iterator is created */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001425} dequeiterobject;
1426
Martin v. Löwis59683e82008-06-13 07:50:45 +00001427static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001428
1429static PyObject *
1430deque_iter(dequeobject *deque)
1431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 dequeiterobject *it;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
1435 if (it == NULL)
1436 return NULL;
1437 it->b = deque->leftblock;
1438 it->index = deque->leftindex;
1439 Py_INCREF(deque);
1440 it->deque = deque;
1441 it->state = deque->state;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001442 it->counter = Py_SIZE(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyObject_GC_Track(it);
1444 return (PyObject *)it;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001445}
1446
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001447static int
1448dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
1449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 Py_VISIT(dio->deque);
1451 return 0;
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001452}
1453
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001454static void
1455dequeiter_dealloc(dequeiterobject *dio)
1456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 Py_XDECREF(dio->deque);
1458 PyObject_GC_Del(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001459}
1460
1461static PyObject *
1462dequeiter_next(dequeiterobject *it)
1463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 if (it->deque->state != it->state) {
1467 it->counter = 0;
1468 PyErr_SetString(PyExc_RuntimeError,
1469 "deque mutated during iteration");
1470 return NULL;
1471 }
1472 if (it->counter == 0)
1473 return NULL;
1474 assert (!(it->b == it->deque->rightblock &&
1475 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 item = it->b->data[it->index];
1478 it->index++;
1479 it->counter--;
1480 if (it->index == BLOCKLEN && it->counter > 0) {
Raymond Hettinger82df9252013-07-07 01:43:42 -10001481 CHECK_NOT_END(it->b->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 it->b = it->b->rightlink;
1483 it->index = 0;
1484 }
1485 Py_INCREF(item);
1486 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001487}
1488
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001489static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001490dequeiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1491{
1492 Py_ssize_t i, index=0;
1493 PyObject *deque;
1494 dequeiterobject *it;
1495 if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index))
1496 return NULL;
1497 assert(type == &dequeiter_type);
1498
1499 it = (dequeiterobject*)deque_iter((dequeobject *)deque);
1500 if (!it)
1501 return NULL;
1502 /* consume items from the queue */
1503 for(i=0; i<index; i++) {
1504 PyObject *item = dequeiter_next(it);
1505 if (item) {
1506 Py_DECREF(item);
1507 } else {
1508 if (it->counter) {
1509 Py_DECREF(it);
1510 return NULL;
1511 } else
1512 break;
1513 }
1514 }
1515 return (PyObject*)it;
1516}
1517
1518static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001519dequeiter_len(dequeiterobject *it)
1520{
Antoine Pitrou554f3342010-08-17 18:30:06 +00001521 return PyLong_FromSsize_t(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001522}
1523
Armin Rigof5b3e362006-02-11 21:32:43 +00001524PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001525
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001526static PyObject *
1527dequeiter_reduce(dequeiterobject *it)
1528{
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001529 return Py_BuildValue("O(On)", Py_TYPE(it), it->deque, Py_SIZE(it->deque) - it->counter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001530}
1531
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001532static PyMethodDef dequeiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001534 {"__reduce__", (PyCFunction)dequeiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001536};
1537
Martin v. Löwis59683e82008-06-13 07:50:45 +00001538static PyTypeObject dequeiter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger87e69122015-03-02 23:32:02 -08001540 "_collections._deque_iterator", /* tp_name */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 sizeof(dequeiterobject), /* tp_basicsize */
1542 0, /* tp_itemsize */
1543 /* methods */
1544 (destructor)dequeiter_dealloc, /* tp_dealloc */
1545 0, /* tp_print */
1546 0, /* tp_getattr */
1547 0, /* tp_setattr */
1548 0, /* tp_reserved */
1549 0, /* tp_repr */
1550 0, /* tp_as_number */
1551 0, /* tp_as_sequence */
1552 0, /* tp_as_mapping */
1553 0, /* tp_hash */
1554 0, /* tp_call */
1555 0, /* tp_str */
1556 PyObject_GenericGetAttr, /* tp_getattro */
1557 0, /* tp_setattro */
1558 0, /* tp_as_buffer */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001559 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 0, /* tp_doc */
1561 (traverseproc)dequeiter_traverse, /* tp_traverse */
1562 0, /* tp_clear */
1563 0, /* tp_richcompare */
1564 0, /* tp_weaklistoffset */
1565 PyObject_SelfIter, /* tp_iter */
1566 (iternextfunc)dequeiter_next, /* tp_iternext */
1567 dequeiter_methods, /* tp_methods */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001568 0, /* tp_members */
1569 0, /* tp_getset */
1570 0, /* tp_base */
1571 0, /* tp_dict */
1572 0, /* tp_descr_get */
1573 0, /* tp_descr_set */
1574 0, /* tp_dictoffset */
1575 0, /* tp_init */
1576 0, /* tp_alloc */
1577 dequeiter_new, /* tp_new */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 0,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001579};
1580
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001581/*********************** Deque Reverse Iterator **************************/
1582
Martin v. Löwis59683e82008-06-13 07:50:45 +00001583static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001584
1585static PyObject *
1586deque_reviter(dequeobject *deque)
1587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 dequeiterobject *it;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
1591 if (it == NULL)
1592 return NULL;
1593 it->b = deque->rightblock;
1594 it->index = deque->rightindex;
1595 Py_INCREF(deque);
1596 it->deque = deque;
1597 it->state = deque->state;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001598 it->counter = Py_SIZE(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 PyObject_GC_Track(it);
1600 return (PyObject *)it;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001601}
1602
1603static PyObject *
1604dequereviter_next(dequeiterobject *it)
1605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 PyObject *item;
1607 if (it->counter == 0)
1608 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (it->deque->state != it->state) {
1611 it->counter = 0;
1612 PyErr_SetString(PyExc_RuntimeError,
1613 "deque mutated during iteration");
1614 return NULL;
1615 }
1616 assert (!(it->b == it->deque->leftblock &&
1617 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 item = it->b->data[it->index];
1620 it->index--;
1621 it->counter--;
1622 if (it->index == -1 && it->counter > 0) {
Raymond Hettinger82df9252013-07-07 01:43:42 -10001623 CHECK_NOT_END(it->b->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 it->b = it->b->leftlink;
1625 it->index = BLOCKLEN - 1;
1626 }
1627 Py_INCREF(item);
1628 return item;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001629}
1630
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001631static PyObject *
1632dequereviter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1633{
1634 Py_ssize_t i, index=0;
1635 PyObject *deque;
1636 dequeiterobject *it;
1637 if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index))
1638 return NULL;
1639 assert(type == &dequereviter_type);
1640
1641 it = (dequeiterobject*)deque_reviter((dequeobject *)deque);
1642 if (!it)
1643 return NULL;
1644 /* consume items from the queue */
1645 for(i=0; i<index; i++) {
1646 PyObject *item = dequereviter_next(it);
1647 if (item) {
1648 Py_DECREF(item);
1649 } else {
1650 if (it->counter) {
1651 Py_DECREF(it);
1652 return NULL;
1653 } else
1654 break;
1655 }
1656 }
1657 return (PyObject*)it;
1658}
1659
Martin v. Löwis59683e82008-06-13 07:50:45 +00001660static PyTypeObject dequereviter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger87e69122015-03-02 23:32:02 -08001662 "_collections._deque_reverse_iterator", /* tp_name */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 sizeof(dequeiterobject), /* tp_basicsize */
1664 0, /* tp_itemsize */
1665 /* methods */
1666 (destructor)dequeiter_dealloc, /* tp_dealloc */
1667 0, /* tp_print */
1668 0, /* tp_getattr */
1669 0, /* tp_setattr */
1670 0, /* tp_reserved */
1671 0, /* tp_repr */
1672 0, /* tp_as_number */
1673 0, /* tp_as_sequence */
1674 0, /* tp_as_mapping */
1675 0, /* tp_hash */
1676 0, /* tp_call */
1677 0, /* tp_str */
1678 PyObject_GenericGetAttr, /* tp_getattro */
1679 0, /* tp_setattro */
1680 0, /* tp_as_buffer */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001681 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 0, /* tp_doc */
1683 (traverseproc)dequeiter_traverse, /* tp_traverse */
1684 0, /* tp_clear */
1685 0, /* tp_richcompare */
1686 0, /* tp_weaklistoffset */
1687 PyObject_SelfIter, /* tp_iter */
1688 (iternextfunc)dequereviter_next, /* tp_iternext */
1689 dequeiter_methods, /* tp_methods */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001690 0, /* tp_members */
1691 0, /* tp_getset */
1692 0, /* tp_base */
1693 0, /* tp_dict */
1694 0, /* tp_descr_get */
1695 0, /* tp_descr_set */
1696 0, /* tp_dictoffset */
1697 0, /* tp_init */
1698 0, /* tp_alloc */
1699 dequereviter_new, /* tp_new */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 0,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001701};
1702
Guido van Rossum1968ad32006-02-25 22:38:04 +00001703/* defaultdict type *********************************************************/
1704
1705typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 PyDictObject dict;
1707 PyObject *default_factory;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001708} defdictobject;
1709
1710static PyTypeObject defdict_type; /* Forward */
1711
1712PyDoc_STRVAR(defdict_missing_doc,
1713"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Guido van Rossumd8faa362007-04-27 19:54:29 +00001714 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001715 self[key] = value = self.default_factory()\n\
1716 return value\n\
1717");
1718
1719static PyObject *
1720defdict_missing(defdictobject *dd, PyObject *key)
1721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 PyObject *factory = dd->default_factory;
1723 PyObject *value;
1724 if (factory == NULL || factory == Py_None) {
1725 /* XXX Call dict.__missing__(key) */
1726 PyObject *tup;
1727 tup = PyTuple_Pack(1, key);
1728 if (!tup) return NULL;
1729 PyErr_SetObject(PyExc_KeyError, tup);
1730 Py_DECREF(tup);
1731 return NULL;
1732 }
1733 value = PyEval_CallObject(factory, NULL);
1734 if (value == NULL)
1735 return value;
1736 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1737 Py_DECREF(value);
1738 return NULL;
1739 }
1740 return value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001741}
1742
1743PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1744
1745static PyObject *
1746defdict_copy(defdictobject *dd)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 /* This calls the object's class. That only works for subclasses
1749 whose class constructor has the same signature. Subclasses that
1750 define a different constructor signature must override copy().
1751 */
Raymond Hettinger54628fa2009-08-04 19:16:39 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (dd->default_factory == NULL)
1754 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
1755 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
1756 dd->default_factory, dd, NULL);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001757}
1758
1759static PyObject *
1760defdict_reduce(defdictobject *dd)
1761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 - factory function
1765 - tuple of args for the factory function
1766 - additional state (here None)
1767 - sequence iterator (here None)
1768 - dictionary iterator (yielding successive (key, value) pairs
Guido van Rossum1968ad32006-02-25 22:38:04 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 This API is used by pickle.py and copy.py.
Guido van Rossum1968ad32006-02-25 22:38:04 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 For this to be useful with pickle.py, the default_factory
1773 must be picklable; e.g., None, a built-in, or a global
1774 function in a module or package.
Guido van Rossum1968ad32006-02-25 22:38:04 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 Both shallow and deep copying are supported, but for deep
1777 copying, the default_factory must be deep-copyable; e.g. None,
1778 or a built-in (functions are not copyable at this time).
Guido van Rossum1968ad32006-02-25 22:38:04 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 This only works for subclasses as long as their constructor
1781 signature is compatible; the first argument must be the
1782 optional default_factory, defaulting to None.
1783 */
1784 PyObject *args;
1785 PyObject *items;
1786 PyObject *iter;
1787 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001788 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1791 args = PyTuple_New(0);
1792 else
1793 args = PyTuple_Pack(1, dd->default_factory);
1794 if (args == NULL)
1795 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001796 items = _PyObject_CallMethodId((PyObject *)dd, &PyId_items, "()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (items == NULL) {
1798 Py_DECREF(args);
1799 return NULL;
1800 }
1801 iter = PyObject_GetIter(items);
1802 if (iter == NULL) {
1803 Py_DECREF(items);
1804 Py_DECREF(args);
1805 return NULL;
1806 }
1807 result = PyTuple_Pack(5, Py_TYPE(dd), args,
1808 Py_None, Py_None, iter);
1809 Py_DECREF(iter);
1810 Py_DECREF(items);
1811 Py_DECREF(args);
1812 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001813}
1814
1815static PyMethodDef defdict_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1817 defdict_missing_doc},
1818 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1819 defdict_copy_doc},
1820 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1821 defdict_copy_doc},
1822 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1823 reduce_doc},
1824 {NULL}
Guido van Rossum1968ad32006-02-25 22:38:04 +00001825};
1826
1827static PyMemberDef defdict_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 {"default_factory", T_OBJECT,
1829 offsetof(defdictobject, default_factory), 0,
1830 PyDoc_STR("Factory for default value called by __missing__().")},
1831 {NULL}
Guido van Rossum1968ad32006-02-25 22:38:04 +00001832};
1833
1834static void
1835defdict_dealloc(defdictobject *dd)
1836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 Py_CLEAR(dd->default_factory);
1838 PyDict_Type.tp_dealloc((PyObject *)dd);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001839}
1840
Guido van Rossum1968ad32006-02-25 22:38:04 +00001841static PyObject *
1842defdict_repr(defdictobject *dd)
1843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 PyObject *baserepr;
1845 PyObject *defrepr;
1846 PyObject *result;
1847 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1848 if (baserepr == NULL)
1849 return NULL;
1850 if (dd->default_factory == NULL)
1851 defrepr = PyUnicode_FromString("None");
1852 else
1853 {
1854 int status = Py_ReprEnter(dd->default_factory);
1855 if (status != 0) {
Antoine Pitrouf5f1fe02012-02-15 02:42:46 +01001856 if (status < 0) {
1857 Py_DECREF(baserepr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 return NULL;
Antoine Pitrouf5f1fe02012-02-15 02:42:46 +01001859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 defrepr = PyUnicode_FromString("...");
1861 }
1862 else
1863 defrepr = PyObject_Repr(dd->default_factory);
1864 Py_ReprLeave(dd->default_factory);
1865 }
1866 if (defrepr == NULL) {
1867 Py_DECREF(baserepr);
1868 return NULL;
1869 }
1870 result = PyUnicode_FromFormat("defaultdict(%U, %U)",
1871 defrepr, baserepr);
1872 Py_DECREF(defrepr);
1873 Py_DECREF(baserepr);
1874 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001875}
1876
1877static int
1878defdict_traverse(PyObject *self, visitproc visit, void *arg)
1879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 Py_VISIT(((defdictobject *)self)->default_factory);
1881 return PyDict_Type.tp_traverse(self, visit, arg);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001882}
1883
1884static int
1885defdict_tp_clear(defdictobject *dd)
1886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 Py_CLEAR(dd->default_factory);
1888 return PyDict_Type.tp_clear((PyObject *)dd);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001889}
1890
1891static int
1892defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 defdictobject *dd = (defdictobject *)self;
1895 PyObject *olddefault = dd->default_factory;
1896 PyObject *newdefault = NULL;
1897 PyObject *newargs;
1898 int result;
1899 if (args == NULL || !PyTuple_Check(args))
1900 newargs = PyTuple_New(0);
1901 else {
1902 Py_ssize_t n = PyTuple_GET_SIZE(args);
1903 if (n > 0) {
1904 newdefault = PyTuple_GET_ITEM(args, 0);
1905 if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
1906 PyErr_SetString(PyExc_TypeError,
1907 "first argument must be callable");
1908 return -1;
1909 }
1910 }
1911 newargs = PySequence_GetSlice(args, 1, n);
1912 }
1913 if (newargs == NULL)
1914 return -1;
1915 Py_XINCREF(newdefault);
1916 dd->default_factory = newdefault;
1917 result = PyDict_Type.tp_init(self, newargs, kwds);
1918 Py_DECREF(newargs);
1919 Py_XDECREF(olddefault);
1920 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001921}
1922
1923PyDoc_STRVAR(defdict_doc,
Benjamin Peterson9cb33b72014-01-13 23:56:05 -05001924"defaultdict(default_factory[, ...]) --> dict with default factory\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001925\n\
1926The default factory is called without arguments to produce\n\
1927a new value when a key is not present, in __getitem__ only.\n\
1928A defaultdict compares equal to a dict with the same items.\n\
Benjamin Peterson9cb33b72014-01-13 23:56:05 -05001929All remaining arguments are treated the same as if they were\n\
1930passed to the dict constructor, including keyword arguments.\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001931");
1932
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001933/* See comment in xxsubtype.c */
1934#define DEFERRED_ADDRESS(ADDR) 0
1935
Guido van Rossum1968ad32006-02-25 22:38:04 +00001936static PyTypeObject defdict_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
1938 "collections.defaultdict", /* tp_name */
1939 sizeof(defdictobject), /* tp_basicsize */
1940 0, /* tp_itemsize */
1941 /* methods */
1942 (destructor)defdict_dealloc, /* tp_dealloc */
1943 0, /* tp_print */
1944 0, /* tp_getattr */
1945 0, /* tp_setattr */
1946 0, /* tp_reserved */
1947 (reprfunc)defdict_repr, /* tp_repr */
1948 0, /* tp_as_number */
1949 0, /* tp_as_sequence */
1950 0, /* tp_as_mapping */
1951 0, /* tp_hash */
1952 0, /* tp_call */
1953 0, /* tp_str */
1954 PyObject_GenericGetAttr, /* tp_getattro */
1955 0, /* tp_setattro */
1956 0, /* tp_as_buffer */
1957 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1958 /* tp_flags */
1959 defdict_doc, /* tp_doc */
1960 defdict_traverse, /* tp_traverse */
1961 (inquiry)defdict_tp_clear, /* tp_clear */
1962 0, /* tp_richcompare */
1963 0, /* tp_weaklistoffset*/
1964 0, /* tp_iter */
1965 0, /* tp_iternext */
1966 defdict_methods, /* tp_methods */
1967 defdict_members, /* tp_members */
1968 0, /* tp_getset */
1969 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
1970 0, /* tp_dict */
1971 0, /* tp_descr_get */
1972 0, /* tp_descr_set */
1973 0, /* tp_dictoffset */
1974 defdict_init, /* tp_init */
1975 PyType_GenericAlloc, /* tp_alloc */
1976 0, /* tp_new */
1977 PyObject_GC_Del, /* tp_free */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001978};
1979
Raymond Hettinger96f34102010-12-15 16:30:37 +00001980/* helper function for Counter *********************************************/
1981
1982PyDoc_STRVAR(_count_elements_doc,
1983"_count_elements(mapping, iterable) -> None\n\
1984\n\
1985Count elements in the iterable, updating the mappping");
1986
1987static PyObject *
1988_count_elements(PyObject *self, PyObject *args)
1989{
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07001990 _Py_IDENTIFIER(get);
Raymond Hettinger2ff21902013-10-01 00:55:43 -07001991 _Py_IDENTIFIER(__setitem__);
Raymond Hettinger96f34102010-12-15 16:30:37 +00001992 PyObject *it, *iterable, *mapping, *oldval;
1993 PyObject *newval = NULL;
1994 PyObject *key = NULL;
Raymond Hettinger224c87d2013-10-01 21:36:09 -07001995 PyObject *zero = NULL;
Raymond Hettinger96f34102010-12-15 16:30:37 +00001996 PyObject *one = NULL;
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07001997 PyObject *bound_get = NULL;
1998 PyObject *mapping_get;
1999 PyObject *dict_get;
Raymond Hettinger2ff21902013-10-01 00:55:43 -07002000 PyObject *mapping_setitem;
Raymond Hettinger2ff21902013-10-01 00:55:43 -07002001 PyObject *dict_setitem;
Raymond Hettinger96f34102010-12-15 16:30:37 +00002002
2003 if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable))
2004 return NULL;
2005
Raymond Hettinger96f34102010-12-15 16:30:37 +00002006 it = PyObject_GetIter(iterable);
2007 if (it == NULL)
2008 return NULL;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002009
Raymond Hettinger96f34102010-12-15 16:30:37 +00002010 one = PyLong_FromLong(1);
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002011 if (one == NULL)
2012 goto done;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002013
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002014 /* Only take the fast path when get() and __setitem__()
2015 * have not been overridden.
2016 */
2017 mapping_get = _PyType_LookupId(Py_TYPE(mapping), &PyId_get);
2018 dict_get = _PyType_LookupId(&PyDict_Type, &PyId_get);
Raymond Hettinger2ff21902013-10-01 00:55:43 -07002019 mapping_setitem = _PyType_LookupId(Py_TYPE(mapping), &PyId___setitem__);
2020 dict_setitem = _PyType_LookupId(&PyDict_Type, &PyId___setitem__);
2021
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002022 if (mapping_get != NULL && mapping_get == dict_get &&
2023 mapping_setitem != NULL && mapping_setitem == dict_setitem) {
Raymond Hettinger426e0522011-01-03 02:12:02 +00002024 while (1) {
Raymond Hettinger507d9972014-05-18 21:32:40 +01002025 /* Fast path advantages:
2026 1. Eliminate double hashing
2027 (by re-using the same hash for both the get and set)
2028 2. Avoid argument overhead of PyObject_CallFunctionObjArgs
2029 (argument tuple creation and parsing)
2030 3. Avoid indirection through a bound method object
2031 (creates another argument tuple)
2032 4. Avoid initial increment from zero
2033 (reuse an existing one-object instead)
2034 */
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002035 Py_hash_t hash;
2036
Raymond Hettinger426e0522011-01-03 02:12:02 +00002037 key = PyIter_Next(it);
Victor Stinnera154b5c2011-04-20 23:23:52 +02002038 if (key == NULL)
2039 break;
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002040
2041 if (!PyUnicode_CheckExact(key) ||
2042 (hash = ((PyASCIIObject *) key)->hash) == -1)
2043 {
2044 hash = PyObject_Hash(key);
Raymond Hettingerda2850f2015-02-27 12:42:54 -08002045 if (hash == -1)
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002046 goto done;
2047 }
2048
2049 oldval = _PyDict_GetItem_KnownHash(mapping, key, hash);
Raymond Hettinger426e0522011-01-03 02:12:02 +00002050 if (oldval == NULL) {
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002051 if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) == -1)
Raymond Hettinger507d9972014-05-18 21:32:40 +01002052 goto done;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002053 } else {
2054 newval = PyNumber_Add(oldval, one);
2055 if (newval == NULL)
Raymond Hettinger507d9972014-05-18 21:32:40 +01002056 goto done;
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002057 if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) == -1)
Raymond Hettinger507d9972014-05-18 21:32:40 +01002058 goto done;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002059 Py_CLEAR(newval);
2060 }
2061 Py_DECREF(key);
Raymond Hettinger96f34102010-12-15 16:30:37 +00002062 }
Raymond Hettinger426e0522011-01-03 02:12:02 +00002063 } else {
Victor Stinnere7f516c2013-11-06 23:52:55 +01002064 bound_get = _PyObject_GetAttrId(mapping, &PyId_get);
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002065 if (bound_get == NULL)
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002066 goto done;
2067
2068 zero = PyLong_FromLong(0);
2069 if (zero == NULL)
2070 goto done;
2071
Raymond Hettinger426e0522011-01-03 02:12:02 +00002072 while (1) {
2073 key = PyIter_Next(it);
Victor Stinnera154b5c2011-04-20 23:23:52 +02002074 if (key == NULL)
2075 break;
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002076 oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002077 if (oldval == NULL)
2078 break;
2079 newval = PyNumber_Add(oldval, one);
2080 Py_DECREF(oldval);
2081 if (newval == NULL)
2082 break;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002083 if (PyObject_SetItem(mapping, key, newval) == -1)
Raymond Hettinger96f34102010-12-15 16:30:37 +00002084 break;
2085 Py_CLEAR(newval);
Raymond Hettinger426e0522011-01-03 02:12:02 +00002086 Py_DECREF(key);
Raymond Hettinger96f34102010-12-15 16:30:37 +00002087 }
Raymond Hettinger96f34102010-12-15 16:30:37 +00002088 }
Raymond Hettinger426e0522011-01-03 02:12:02 +00002089
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002090done:
Raymond Hettinger96f34102010-12-15 16:30:37 +00002091 Py_DECREF(it);
2092 Py_XDECREF(key);
2093 Py_XDECREF(newval);
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002094 Py_XDECREF(bound_get);
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002095 Py_XDECREF(zero);
2096 Py_XDECREF(one);
Raymond Hettinger96f34102010-12-15 16:30:37 +00002097 if (PyErr_Occurred())
2098 return NULL;
2099 Py_RETURN_NONE;
2100}
2101
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002102/* module level code ********************************************************/
2103
2104PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00002105"High performance data structures.\n\
2106- deque: ordered collection accessible from endpoints only\n\
2107- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002108");
2109
Raymond Hettinger96f34102010-12-15 16:30:37 +00002110static struct PyMethodDef module_functions[] = {
2111 {"_count_elements", _count_elements, METH_VARARGS, _count_elements_doc},
2112 {NULL, NULL} /* sentinel */
2113};
Martin v. Löwis1a214512008-06-11 05:26:20 +00002114
2115static struct PyModuleDef _collectionsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 PyModuleDef_HEAD_INIT,
2117 "_collections",
2118 module_doc,
2119 -1,
Raymond Hettinger96f34102010-12-15 16:30:37 +00002120 module_functions,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 NULL,
2122 NULL,
2123 NULL,
2124 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002125};
2126
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002127PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002128PyInit__collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 PyObject *m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 m = PyModule_Create(&_collectionsmodule);
2133 if (m == NULL)
2134 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 if (PyType_Ready(&deque_type) < 0)
2137 return NULL;
2138 Py_INCREF(&deque_type);
2139 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 defdict_type.tp_base = &PyDict_Type;
2142 if (PyType_Ready(&defdict_type) < 0)
2143 return NULL;
2144 Py_INCREF(&defdict_type);
2145 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
Guido van Rossum1968ad32006-02-25 22:38:04 +00002146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 if (PyType_Ready(&dequeiter_type) < 0)
2148 return NULL;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002149 Py_INCREF(&dequeiter_type);
2150 PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 if (PyType_Ready(&dequereviter_type) < 0)
2153 return NULL;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002154 Py_INCREF(&dequereviter_type);
2155 PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 return m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002158}