blob: f8d656cccbfa58328760b04349279f8f3ef9b379 [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
4/* collections module implementation of a deque() datatype
5 Written and maintained by Raymond D. Hettinger <python@rcn.com>
6 Copyright (c) 2004 Python Software Foundation.
7 All rights reserved.
8*/
9
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000010/* The block length may be set to any number over 1. Larger numbers
11 * reduce the number of calls to the memory allocator but take more
12 * memory. Ideally, BLOCKLEN should be set with an eye to the
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013 * length of a cache line.
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000014 */
15
Raymond Hettinger7d112df2004-11-02 02:11:35 +000016#define BLOCKLEN 62
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000017#define CENTER ((BLOCKLEN - 1) / 2)
Raymond Hettinger756b3f32004-01-29 06:37:52 +000018
Tim Petersd8768d32004-10-01 01:32:53 +000019/* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
20 * This list is not circular (the leftmost block has leftlink==NULL,
21 * and the rightmost block has rightlink==NULL). A deque d's first
22 * element is at d.leftblock[leftindex] and its last element is at
23 * d.rightblock[rightindex]; note that, unlike as for Python slice
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000024 * indices, these indices are inclusive on both ends. By being inclusive
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 * on both ends, algorithms for left and right operations become
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000026 * symmetrical which simplifies the design.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027 *
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000028 * The list of blocks is never empty, so d.leftblock and d.rightblock
29 * are never equal to NULL.
30 *
31 * The indices, d.leftindex and d.rightindex are always in the range
32 * 0 <= index < BLOCKLEN.
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000033 * Their exact relationship is:
34 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000035 *
36 * Empty deques have d.len == 0; d.leftblock==d.rightblock;
37 * d.leftindex == CENTER+1; and d.rightindex == CENTER.
38 * Checking for d.len == 0 is the intended way to see whether d is empty.
39 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000040 * Whenever d.leftblock == d.rightblock,
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000041 * d.leftindex + d.len - 1 == d.rightindex.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042 *
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000043 * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044 * become indices into distinct blocks and either may be larger than the
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000045 * other.
Tim Petersd8768d32004-10-01 01:32:53 +000046 */
47
Raymond Hettinger756b3f32004-01-29 06:37:52 +000048typedef struct BLOCK {
49 struct BLOCK *leftlink;
50 struct BLOCK *rightlink;
51 PyObject *data[BLOCKLEN];
52} block;
53
Guido van Rossum58da9312007-11-10 23:39:45 +000054#define MAXFREEBLOCKS 10
Benjamin Petersond6313712008-07-31 16:23:04 +000055static Py_ssize_t numfreeblocks = 0;
Guido van Rossum58da9312007-11-10 23:39:45 +000056static block *freeblocks[MAXFREEBLOCKS];
57
Tim Peters6f853562004-10-01 01:04:50 +000058static block *
Benjamin Petersond6313712008-07-31 16:23:04 +000059newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000060 block *b;
Benjamin Petersond6313712008-07-31 16:23:04 +000061 /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000062 * refuse to allocate new blocks if the current len is dangerously
63 * close. There is some extra margin to prevent spurious arithmetic
64 * overflows at various places. The following check ensures that
65 * the blocks allocated to the deque, in the worst case, can only
Benjamin Petersond6313712008-07-31 16:23:04 +000066 * have PY_SSIZE_T_MAX-2 entries in total.
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000067 */
Benjamin Petersond6313712008-07-31 16:23:04 +000068 if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000069 PyErr_SetString(PyExc_OverflowError,
70 "cannot add more blocks to the deque");
71 return NULL;
72 }
Guido van Rossum58da9312007-11-10 23:39:45 +000073 if (numfreeblocks) {
74 numfreeblocks -= 1;
75 b = freeblocks[numfreeblocks];
76 } else {
77 b = PyMem_Malloc(sizeof(block));
78 if (b == NULL) {
79 PyErr_NoMemory();
80 return NULL;
81 }
Raymond Hettinger756b3f32004-01-29 06:37:52 +000082 }
83 b->leftlink = leftlink;
84 b->rightlink = rightlink;
85 return b;
86}
87
Martin v. Löwis59683e82008-06-13 07:50:45 +000088static void
Guido van Rossum58da9312007-11-10 23:39:45 +000089freeblock(block *b)
90{
91 if (numfreeblocks < MAXFREEBLOCKS) {
92 freeblocks[numfreeblocks] = b;
93 numfreeblocks++;
94 } else {
95 PyMem_Free(b);
96 }
97}
98
Raymond Hettinger756b3f32004-01-29 06:37:52 +000099typedef struct {
100 PyObject_HEAD
101 block *leftblock;
102 block *rightblock;
Benjamin Petersond6313712008-07-31 16:23:04 +0000103 Py_ssize_t leftindex; /* in range(BLOCKLEN) */
104 Py_ssize_t rightindex; /* in range(BLOCKLEN) */
105 Py_ssize_t len;
106 Py_ssize_t maxlen;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000107 long state; /* incremented whenever the indices move */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000108 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000109} dequeobject;
110
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000111/* The deque's size limit is d.maxlen. The limit can be zero or positive.
112 * If there is no limit, then d.maxlen == -1.
113 *
114 * After an item is added to a deque, we check to see if the size has grown past
115 * the limit. If it has, we get the size back down to the limit by popping an
116 * item off of the opposite end. The methods that can trigger this are append(),
117 * appendleft(), extend(), and extendleft().
118 */
119
120#define TRIM(d, popfunction) \
121 if (d->maxlen != -1 && d->len > d->maxlen) { \
122 PyObject *rv = popfunction(d, NULL); \
123 assert(rv != NULL && d->len <= d->maxlen); \
124 Py_DECREF(rv); \
125 }
126
Neal Norwitz87f10132004-02-29 15:40:53 +0000127static PyTypeObject deque_type;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000128
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000129static PyObject *
130deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
131{
132 dequeobject *deque;
133 block *b;
134
135 /* create dequeobject structure */
136 deque = (dequeobject *)type->tp_alloc(type, 0);
137 if (deque == NULL)
138 return NULL;
Tim Peters1065f752004-10-01 01:03:29 +0000139
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000140 b = newblock(NULL, NULL, 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000141 if (b == NULL) {
142 Py_DECREF(deque);
143 return NULL;
144 }
145
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000146 assert(BLOCKLEN >= 2);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000147 deque->leftblock = b;
148 deque->rightblock = b;
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000149 deque->leftindex = CENTER + 1;
150 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000151 deque->len = 0;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000152 deque->state = 0;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000153 deque->weakreflist = NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000154 deque->maxlen = -1;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000155
156 return (PyObject *)deque;
157}
158
159static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000160deque_pop(dequeobject *deque, PyObject *unused)
161{
162 PyObject *item;
163 block *prevblock;
164
165 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000166 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000167 return NULL;
168 }
169 item = deque->rightblock->data[deque->rightindex];
170 deque->rightindex--;
171 deque->len--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000172 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000173
174 if (deque->rightindex == -1) {
175 if (deque->len == 0) {
176 assert(deque->leftblock == deque->rightblock);
177 assert(deque->leftindex == deque->rightindex+1);
178 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000179 deque->leftindex = CENTER + 1;
180 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000181 } else {
182 prevblock = deque->rightblock->leftlink;
183 assert(deque->leftblock != deque->rightblock);
Guido van Rossum58da9312007-11-10 23:39:45 +0000184 freeblock(deque->rightblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000185 prevblock->rightlink = NULL;
186 deque->rightblock = prevblock;
187 deque->rightindex = BLOCKLEN - 1;
188 }
189 }
190 return item;
191}
192
193PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
194
195static PyObject *
196deque_popleft(dequeobject *deque, PyObject *unused)
197{
198 PyObject *item;
199 block *prevblock;
200
201 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000202 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000203 return NULL;
204 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000205 assert(deque->leftblock != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000206 item = deque->leftblock->data[deque->leftindex];
207 deque->leftindex++;
208 deque->len--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000209 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000210
211 if (deque->leftindex == BLOCKLEN) {
212 if (deque->len == 0) {
213 assert(deque->leftblock == deque->rightblock);
214 assert(deque->leftindex == deque->rightindex+1);
215 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000216 deque->leftindex = CENTER + 1;
217 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000218 } else {
219 assert(deque->leftblock != deque->rightblock);
220 prevblock = deque->leftblock->rightlink;
Guido van Rossum58da9312007-11-10 23:39:45 +0000221 freeblock(deque->leftblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000222 assert(prevblock != NULL);
223 prevblock->leftlink = NULL;
224 deque->leftblock = prevblock;
225 deque->leftindex = 0;
226 }
227 }
228 return item;
229}
230
231PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
232
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000233static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000234deque_append(dequeobject *deque, PyObject *item)
235{
236 deque->state++;
237 if (deque->rightindex == BLOCKLEN-1) {
238 block *b = newblock(deque->rightblock, NULL, deque->len);
239 if (b == NULL)
240 return NULL;
241 assert(deque->rightblock->rightlink == NULL);
242 deque->rightblock->rightlink = b;
243 deque->rightblock = b;
244 deque->rightindex = -1;
245 }
246 Py_INCREF(item);
247 deque->len++;
248 deque->rightindex++;
249 deque->rightblock->data[deque->rightindex] = item;
250 TRIM(deque, deque_popleft);
251 Py_RETURN_NONE;
252}
253
254PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
255
256static PyObject *
257deque_appendleft(dequeobject *deque, PyObject *item)
258{
259 deque->state++;
260 if (deque->leftindex == 0) {
261 block *b = newblock(NULL, deque->leftblock, deque->len);
262 if (b == NULL)
263 return NULL;
264 assert(deque->leftblock->leftlink == NULL);
265 deque->leftblock->leftlink = b;
266 deque->leftblock = b;
267 deque->leftindex = BLOCKLEN;
268 }
269 Py_INCREF(item);
270 deque->len++;
271 deque->leftindex--;
272 deque->leftblock->data[deque->leftindex] = item;
273 TRIM(deque, deque_pop);
274 Py_RETURN_NONE;
275}
276
277PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
278
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000279
280/* Run an iterator to exhaustion. Shortcut for
281 the extend/extendleft methods when maxlen == 0. */
282static PyObject*
283consume_iterator(PyObject *it)
284{
285 PyObject *item;
286
287 while ((item = PyIter_Next(it)) != NULL) {
288 Py_DECREF(item);
289 }
290 Py_DECREF(it);
291 if (PyErr_Occurred())
292 return NULL;
293 Py_RETURN_NONE;
294}
295
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000296static PyObject *
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000297deque_extend(dequeobject *deque, PyObject *iterable)
298{
299 PyObject *it, *item;
300
301 it = PyObject_GetIter(iterable);
302 if (it == NULL)
303 return NULL;
304
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000305 if (deque->maxlen == 0)
306 return consume_iterator(it);
307
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000308 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000309 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000310 if (deque->rightindex == BLOCKLEN-1) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000311 block *b = newblock(deque->rightblock, NULL,
312 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000313 if (b == NULL) {
314 Py_DECREF(item);
315 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000316 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000317 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000318 assert(deque->rightblock->rightlink == NULL);
319 deque->rightblock->rightlink = b;
320 deque->rightblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000321 deque->rightindex = -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000322 }
Armin Rigo974d7572004-10-02 13:59:34 +0000323 deque->len++;
324 deque->rightindex++;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000325 deque->rightblock->data[deque->rightindex] = item;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000326 TRIM(deque, deque_popleft);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000327 }
328 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000329 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000330 return NULL;
331 Py_RETURN_NONE;
332}
333
Tim Peters1065f752004-10-01 01:03:29 +0000334PyDoc_STRVAR(extend_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000335"Extend the right side of the deque with elements from the iterable");
336
337static PyObject *
338deque_extendleft(dequeobject *deque, PyObject *iterable)
339{
340 PyObject *it, *item;
341
342 it = PyObject_GetIter(iterable);
343 if (it == NULL)
344 return NULL;
345
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000346 if (deque->maxlen == 0)
347 return consume_iterator(it);
348
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000349 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000350 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000351 if (deque->leftindex == 0) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000352 block *b = newblock(NULL, deque->leftblock,
353 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000354 if (b == NULL) {
355 Py_DECREF(item);
356 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000357 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000358 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000359 assert(deque->leftblock->leftlink == NULL);
360 deque->leftblock->leftlink = b;
361 deque->leftblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000362 deque->leftindex = BLOCKLEN;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000363 }
Armin Rigo974d7572004-10-02 13:59:34 +0000364 deque->len++;
365 deque->leftindex--;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000366 deque->leftblock->data[deque->leftindex] = item;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000367 TRIM(deque, deque_pop);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000368 }
369 Py_DECREF(it);
Raymond Hettingera435c532004-07-09 04:10:20 +0000370 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000371 return NULL;
372 Py_RETURN_NONE;
373}
374
Tim Peters1065f752004-10-01 01:03:29 +0000375PyDoc_STRVAR(extendleft_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000376"Extend the left side of the deque with elements from the iterable");
377
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000378static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379_deque_rotate(dequeobject *deque, Py_ssize_t n)
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000380{
Benjamin Petersond6313712008-07-31 16:23:04 +0000381 Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000382 PyObject *item, *rv;
383
Raymond Hettingeree33b272004-02-08 04:05:26 +0000384 if (len == 0)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000385 return 0;
Raymond Hettingeree33b272004-02-08 04:05:26 +0000386 if (n > halflen || n < -halflen) {
387 n %= len;
388 if (n > halflen)
389 n -= len;
390 else if (n < -halflen)
391 n += len;
392 }
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000393
394 for (i=0 ; i<n ; i++) {
395 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000396 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000397 rv = deque_appendleft(deque, item);
398 Py_DECREF(item);
399 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000400 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000401 Py_DECREF(rv);
402 }
403 for (i=0 ; i>n ; i--) {
404 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000405 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000406 rv = deque_append(deque, item);
407 Py_DECREF(item);
408 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000409 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000410 Py_DECREF(rv);
411 }
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000412 return 0;
413}
414
415static PyObject *
416deque_rotate(dequeobject *deque, PyObject *args)
417{
Benjamin Petersond6313712008-07-31 16:23:04 +0000418 Py_ssize_t n=1;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000419
Benjamin Petersond6313712008-07-31 16:23:04 +0000420 if (!PyArg_ParseTuple(args, "|n:rotate", &n))
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000421 return NULL;
422 if (_deque_rotate(deque, n) == 0)
423 Py_RETURN_NONE;
424 return NULL;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000425}
426
Tim Peters1065f752004-10-01 01:03:29 +0000427PyDoc_STRVAR(rotate_doc,
Raymond Hettingeree33b272004-02-08 04:05:26 +0000428"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000429
Martin v. Löwis18e16552006-02-15 17:27:45 +0000430static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000431deque_len(dequeobject *deque)
432{
433 return deque->len;
434}
435
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000436static PyObject *
437deque_remove(dequeobject *deque, PyObject *value)
438{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439 Py_ssize_t i, n=deque->len;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000440
441 for (i=0 ; i<n ; i++) {
442 PyObject *item = deque->leftblock->data[deque->leftindex];
443 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000444
445 if (deque->len != n) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000446 PyErr_SetString(PyExc_IndexError,
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000447 "deque mutated during remove().");
448 return NULL;
449 }
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000450 if (cmp > 0) {
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000451 PyObject *tgt = deque_popleft(deque, NULL);
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000452 assert (tgt != NULL);
453 Py_DECREF(tgt);
454 if (_deque_rotate(deque, i) == -1)
455 return NULL;
456 Py_RETURN_NONE;
457 }
458 else if (cmp < 0) {
459 _deque_rotate(deque, i);
460 return NULL;
461 }
462 _deque_rotate(deque, -1);
463 }
464 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
465 return NULL;
466}
467
468PyDoc_STRVAR(remove_doc,
469"D.remove(value) -- remove first occurrence of value.");
470
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000471static int
472deque_clear(dequeobject *deque)
473{
474 PyObject *item;
475
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000476 while (deque->len) {
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000477 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000478 assert (item != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000479 Py_DECREF(item);
480 }
481 assert(deque->leftblock == deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000482 deque->leftindex - 1 == deque->rightindex &&
483 deque->len == 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000484 return 0;
485}
486
487static PyObject *
Benjamin Petersond6313712008-07-31 16:23:04 +0000488deque_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000489{
490 block *b;
491 PyObject *item;
Benjamin Petersond6313712008-07-31 16:23:04 +0000492 Py_ssize_t n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000493
494 if (i < 0 || i >= deque->len) {
495 PyErr_SetString(PyExc_IndexError,
496 "deque index out of range");
497 return NULL;
498 }
499
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000500 if (i == 0) {
501 i = deque->leftindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000502 b = deque->leftblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000503 } else if (i == deque->len - 1) {
504 i = deque->rightindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000505 b = deque->rightblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000506 } else {
507 i += deque->leftindex;
508 n = i / BLOCKLEN;
509 i %= BLOCKLEN;
Armin Rigo974d7572004-10-02 13:59:34 +0000510 if (index < (deque->len >> 1)) {
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000511 b = deque->leftblock;
512 while (n--)
513 b = b->rightlink;
514 } else {
515 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
516 b = deque->rightblock;
517 while (n--)
518 b = b->leftlink;
519 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000520 }
521 item = b->data[i];
522 Py_INCREF(item);
523 return item;
524}
525
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000526/* delitem() implemented in terms of rotate for simplicity and reasonable
527 performance near the end points. If for some reason this method becomes
Tim Peters1065f752004-10-01 01:03:29 +0000528 popular, it is not hard to re-implement this using direct data movement
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000529 (similar to code in list slice assignment) and achieve a two or threefold
530 performance boost.
531*/
532
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000533static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000534deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000535{
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000536 PyObject *item;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000537
Tim Peters1065f752004-10-01 01:03:29 +0000538 assert (i >= 0 && i < deque->len);
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000539 if (_deque_rotate(deque, -i) == -1)
540 return -1;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000541
542 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000543 assert (item != NULL);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000544 Py_DECREF(item);
545
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000546 return _deque_rotate(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000547}
548
549static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000550deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000551{
552 PyObject *old_value;
553 block *b;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000554 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000555
Raymond Hettingera435c532004-07-09 04:10:20 +0000556 if (i < 0 || i >= len) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000557 PyErr_SetString(PyExc_IndexError,
558 "deque index out of range");
559 return -1;
560 }
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000561 if (v == NULL)
562 return deque_del_item(deque, i);
563
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000564 i += deque->leftindex;
565 n = i / BLOCKLEN;
566 i %= BLOCKLEN;
Raymond Hettingera435c532004-07-09 04:10:20 +0000567 if (index <= halflen) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000568 b = deque->leftblock;
569 while (n--)
570 b = b->rightlink;
571 } else {
Raymond Hettingera435c532004-07-09 04:10:20 +0000572 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000573 b = deque->rightblock;
574 while (n--)
575 b = b->leftlink;
576 }
577 Py_INCREF(v);
578 old_value = b->data[i];
579 b->data[i] = v;
580 Py_DECREF(old_value);
581 return 0;
582}
583
584static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000585deque_clearmethod(dequeobject *deque)
586{
Raymond Hettingera435c532004-07-09 04:10:20 +0000587 int rv;
588
589 rv = deque_clear(deque);
590 assert (rv != -1);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000591 Py_RETURN_NONE;
592}
593
594PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
595
596static void
597deque_dealloc(dequeobject *deque)
598{
599 PyObject_GC_UnTrack(deque);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000600 if (deque->weakreflist != NULL)
601 PyObject_ClearWeakRefs((PyObject *) deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000602 if (deque->leftblock != NULL) {
Raymond Hettingere9c89e82004-07-19 00:10:24 +0000603 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000604 assert(deque->leftblock != NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +0000605 freeblock(deque->leftblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000606 }
607 deque->leftblock = NULL;
608 deque->rightblock = NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000609 Py_TYPE(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000610}
611
612static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000613deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000614{
Tim Peters10c7e862004-10-01 02:01:04 +0000615 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000616 PyObject *item;
Benjamin Petersond6313712008-07-31 16:23:04 +0000617 Py_ssize_t index;
618 Py_ssize_t indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000619
Tim Peters10c7e862004-10-01 02:01:04 +0000620 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
Benjamin Petersond6313712008-07-31 16:23:04 +0000621 const Py_ssize_t indexhi = b == deque->rightblock ?
Tim Peters10c7e862004-10-01 02:01:04 +0000622 deque->rightindex :
623 BLOCKLEN - 1;
624
625 for (index = indexlo; index <= indexhi; ++index) {
626 item = b->data[index];
627 Py_VISIT(item);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000628 }
Tim Peters10c7e862004-10-01 02:01:04 +0000629 indexlo = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000630 }
631 return 0;
632}
633
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000634static PyObject *
635deque_copy(PyObject *deque)
636{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000637 if (((dequeobject *)deque)->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000638 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000639 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000640 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000641 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000642}
643
644PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
645
646static PyObject *
647deque_reduce(dequeobject *deque)
648{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000649 PyObject *dict, *result, *aslist;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000650
Raymond Hettinger952f8802004-11-09 07:27:35 +0000651 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000652 if (dict == NULL)
Raymond Hettinger952f8802004-11-09 07:27:35 +0000653 PyErr_Clear();
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000654 aslist = PySequence_List((PyObject *)deque);
655 if (aslist == NULL) {
656 Py_XDECREF(dict);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000657 return NULL;
658 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000659 if (dict == NULL) {
660 if (deque->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000661 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000662 else
Benjamin Petersond6313712008-07-31 16:23:04 +0000663 result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000664 } else {
665 if (deque->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000666 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000667 else
Benjamin Petersond6313712008-07-31 16:23:04 +0000668 result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000669 }
670 Py_XDECREF(dict);
671 Py_DECREF(aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000672 return result;
673}
674
675PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
676
677static PyObject *
678deque_repr(PyObject *deque)
679{
Walter Dörwald1ab83302007-05-18 17:15:44 +0000680 PyObject *aslist, *result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000681 int i;
682
683 i = Py_ReprEnter(deque);
684 if (i != 0) {
685 if (i < 0)
686 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000687 return PyUnicode_FromString("[...]");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000688 }
689
690 aslist = PySequence_List(deque);
691 if (aslist == NULL) {
692 Py_ReprLeave(deque);
693 return NULL;
694 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000695 if (((dequeobject *)deque)->maxlen != -1)
Benjamin Petersona786b022008-08-25 21:05:21 +0000696
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000697 result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
Benjamin Petersona786b022008-08-25 21:05:21 +0000698 aslist, ((dequeobject *)deque)->maxlen);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000699 else
700 result = PyUnicode_FromFormat("deque(%R)", aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000701 Py_DECREF(aslist);
702 Py_ReprLeave(deque);
703 return result;
704}
705
Raymond Hettinger738ec902004-02-29 02:15:56 +0000706static PyObject *
707deque_richcompare(PyObject *v, PyObject *w, int op)
708{
709 PyObject *it1=NULL, *it2=NULL, *x, *y;
Benjamin Petersond6313712008-07-31 16:23:04 +0000710 Py_ssize_t vs, ws;
711 int b, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000712
Tim Peters1065f752004-10-01 01:03:29 +0000713 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000714 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000715 Py_INCREF(Py_NotImplemented);
716 return Py_NotImplemented;
717 }
718
719 /* Shortcuts */
720 vs = ((dequeobject *)v)->len;
721 ws = ((dequeobject *)w)->len;
722 if (op == Py_EQ) {
723 if (v == w)
724 Py_RETURN_TRUE;
725 if (vs != ws)
726 Py_RETURN_FALSE;
727 }
728 if (op == Py_NE) {
729 if (v == w)
730 Py_RETURN_FALSE;
731 if (vs != ws)
732 Py_RETURN_TRUE;
733 }
734
735 /* Search for the first index where items are different */
736 it1 = PyObject_GetIter(v);
737 if (it1 == NULL)
738 goto done;
739 it2 = PyObject_GetIter(w);
740 if (it2 == NULL)
741 goto done;
Armin Rigo974d7572004-10-02 13:59:34 +0000742 for (;;) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000743 x = PyIter_Next(it1);
Armin Rigo974d7572004-10-02 13:59:34 +0000744 if (x == NULL && PyErr_Occurred())
Raymond Hettinger738ec902004-02-29 02:15:56 +0000745 goto done;
746 y = PyIter_Next(it2);
Armin Rigo974d7572004-10-02 13:59:34 +0000747 if (x == NULL || y == NULL)
748 break;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000749 b = PyObject_RichCompareBool(x, y, Py_EQ);
750 if (b == 0) {
751 cmp = PyObject_RichCompareBool(x, y, op);
752 Py_DECREF(x);
753 Py_DECREF(y);
754 goto done;
755 }
756 Py_DECREF(x);
757 Py_DECREF(y);
758 if (b == -1)
759 goto done;
760 }
Armin Rigo974d7572004-10-02 13:59:34 +0000761 /* We reached the end of one deque or both */
762 Py_XDECREF(x);
763 Py_XDECREF(y);
764 if (PyErr_Occurred())
765 goto done;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000766 switch (op) {
Armin Rigo974d7572004-10-02 13:59:34 +0000767 case Py_LT: cmp = y != NULL; break; /* if w was longer */
768 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
769 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
770 case Py_NE: cmp = x != y; break; /* if one deque continues */
771 case Py_GT: cmp = x != NULL; break; /* if v was longer */
772 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000773 }
Tim Peters1065f752004-10-01 01:03:29 +0000774
Raymond Hettinger738ec902004-02-29 02:15:56 +0000775done:
776 Py_XDECREF(it1);
777 Py_XDECREF(it2);
778 if (cmp == 1)
779 Py_RETURN_TRUE;
780 if (cmp == 0)
781 Py_RETURN_FALSE;
782 return NULL;
783}
784
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000785static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000786deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000787{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000788 PyObject *iterable = NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000789 PyObject *maxlenobj = NULL;
Benjamin Petersond6313712008-07-31 16:23:04 +0000790 Py_ssize_t maxlen = -1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000791 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000792
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000793 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000794 return -1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000795 if (maxlenobj != NULL && maxlenobj != Py_None) {
Benjamin Petersond6313712008-07-31 16:23:04 +0000796 maxlen = PyLong_AsSsize_t(maxlenobj);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000797 if (maxlen == -1 && PyErr_Occurred())
798 return -1;
799 if (maxlen < 0) {
800 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
801 return -1;
802 }
803 }
804 deque->maxlen = maxlen;
Christian Heimes38053212007-12-14 01:24:44 +0000805 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000806 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000807 PyObject *rv = deque_extend(deque, iterable);
808 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000809 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000810 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000811 }
812 return 0;
813}
814
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000815static PyObject *
816deque_get_maxlen(dequeobject *deque)
817{
818 if (deque->maxlen == -1)
819 Py_RETURN_NONE;
820 return PyLong_FromSsize_t(deque->maxlen);
821}
822
823static PyGetSetDef deque_getset[] = {
824 {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
825 "maximum size of a deque or None if unbounded"},
826 {0}
827};
828
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000829static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000830 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000831 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000832 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000833 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000834 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000835 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000836};
837
838/* deque object ********************************************************/
839
840static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000841static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000842PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000843 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000844
845static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000846 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000847 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000848 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000849 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000850 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000851 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000852 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000853 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000854 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000855 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000856 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000857 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000858 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000859 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000860 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000861 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000862 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000863 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000864 {"remove", (PyCFunction)deque_remove,
865 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000866 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000867 METH_NOARGS, reversed_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000868 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000869 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000870 {NULL, NULL} /* sentinel */
871};
872
873PyDoc_STRVAR(deque_doc,
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000874"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000875\n\
876Build an ordered collection accessible from endpoints only.");
877
Neal Norwitz87f10132004-02-29 15:40:53 +0000878static PyTypeObject deque_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000879 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000880 "collections.deque", /* tp_name */
881 sizeof(dequeobject), /* tp_basicsize */
882 0, /* tp_itemsize */
883 /* methods */
884 (destructor)deque_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000885 0, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000886 0, /* tp_getattr */
887 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000888 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000889 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000890 0, /* tp_as_number */
891 &deque_as_sequence, /* tp_as_sequence */
892 0, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +0000893 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000894 0, /* tp_call */
895 0, /* tp_str */
896 PyObject_GenericGetAttr, /* tp_getattro */
897 0, /* tp_setattro */
898 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000899 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
900 /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000901 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000902 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000903 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000904 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000905 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000906 (getiterfunc)deque_iter, /* tp_iter */
907 0, /* tp_iternext */
908 deque_methods, /* tp_methods */
909 0, /* tp_members */
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000910 deque_getset, /* tp_getset */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000911 0, /* tp_base */
912 0, /* tp_dict */
913 0, /* tp_descr_get */
914 0, /* tp_descr_set */
915 0, /* tp_dictoffset */
916 (initproc)deque_init, /* tp_init */
917 PyType_GenericAlloc, /* tp_alloc */
918 deque_new, /* tp_new */
919 PyObject_GC_Del, /* tp_free */
920};
921
922/*********************** Deque Iterator **************************/
923
924typedef struct {
925 PyObject_HEAD
Benjamin Petersond6313712008-07-31 16:23:04 +0000926 Py_ssize_t index;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000927 block *b;
928 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000929 long state; /* state when the iterator is created */
Benjamin Petersond6313712008-07-31 16:23:04 +0000930 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000931} dequeiterobject;
932
Martin v. Löwis59683e82008-06-13 07:50:45 +0000933static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000934
935static PyObject *
936deque_iter(dequeobject *deque)
937{
938 dequeiterobject *it;
939
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000940 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000941 if (it == NULL)
942 return NULL;
943 it->b = deque->leftblock;
944 it->index = deque->leftindex;
945 Py_INCREF(deque);
946 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000947 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000948 it->counter = deque->len;
Georg Brandlb1441c72009-01-03 22:33:39 +0000949 PyObject_GC_Track(it);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000950 return (PyObject *)it;
951}
952
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000953static int
954dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
955{
956 Py_VISIT(dio->deque);
957 return 0;
958}
959
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000960static void
961dequeiter_dealloc(dequeiterobject *dio)
962{
963 Py_XDECREF(dio->deque);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000964 PyObject_GC_Del(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000965}
966
967static PyObject *
968dequeiter_next(dequeiterobject *it)
969{
970 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000971
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000972 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000973 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000974 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000975 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000976 return NULL;
977 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000978 if (it->counter == 0)
979 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000980 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000981 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000982
983 item = it->b->data[it->index];
984 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000985 it->counter--;
986 if (it->index == BLOCKLEN && it->counter > 0) {
987 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000988 it->b = it->b->rightlink;
989 it->index = 0;
990 }
991 Py_INCREF(item);
992 return item;
993}
994
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000995static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000996dequeiter_len(dequeiterobject *it)
997{
Christian Heimes217cfd12007-12-02 14:31:20 +0000998 return PyLong_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000999}
1000
Armin Rigof5b3e362006-02-11 21:32:43 +00001001PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001002
1003static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00001004 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001005 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001006};
1007
Martin v. Löwis59683e82008-06-13 07:50:45 +00001008static PyTypeObject dequeiter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001009 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001010 "deque_iterator", /* tp_name */
1011 sizeof(dequeiterobject), /* tp_basicsize */
1012 0, /* tp_itemsize */
1013 /* methods */
1014 (destructor)dequeiter_dealloc, /* tp_dealloc */
1015 0, /* tp_print */
1016 0, /* tp_getattr */
1017 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001018 0, /* tp_reserved */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001019 0, /* tp_repr */
1020 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001021 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001022 0, /* tp_as_mapping */
1023 0, /* tp_hash */
1024 0, /* tp_call */
1025 0, /* tp_str */
1026 PyObject_GenericGetAttr, /* tp_getattro */
1027 0, /* tp_setattro */
1028 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001029 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001030 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001031 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001032 0, /* tp_clear */
1033 0, /* tp_richcompare */
1034 0, /* tp_weaklistoffset */
1035 PyObject_SelfIter, /* tp_iter */
1036 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001037 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001038 0,
1039};
1040
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001041/*********************** Deque Reverse Iterator **************************/
1042
Martin v. Löwis59683e82008-06-13 07:50:45 +00001043static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001044
1045static PyObject *
1046deque_reviter(dequeobject *deque)
1047{
1048 dequeiterobject *it;
1049
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001050 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001051 if (it == NULL)
1052 return NULL;
1053 it->b = deque->rightblock;
1054 it->index = deque->rightindex;
1055 Py_INCREF(deque);
1056 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001057 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001058 it->counter = deque->len;
Georg Brandlb1441c72009-01-03 22:33:39 +00001059 PyObject_GC_Track(it);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001060 return (PyObject *)it;
1061}
1062
1063static PyObject *
1064dequereviter_next(dequeiterobject *it)
1065{
1066 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001067 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001068 return NULL;
1069
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001070 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001071 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001072 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001073 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001074 return NULL;
1075 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001076 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001077 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001078
1079 item = it->b->data[it->index];
1080 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001081 it->counter--;
1082 if (it->index == -1 && it->counter > 0) {
1083 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001084 it->b = it->b->leftlink;
1085 it->index = BLOCKLEN - 1;
1086 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001087 Py_INCREF(item);
1088 return item;
1089}
1090
Martin v. Löwis59683e82008-06-13 07:50:45 +00001091static PyTypeObject dequereviter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001092 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001093 "deque_reverse_iterator", /* tp_name */
1094 sizeof(dequeiterobject), /* tp_basicsize */
1095 0, /* tp_itemsize */
1096 /* methods */
1097 (destructor)dequeiter_dealloc, /* tp_dealloc */
1098 0, /* tp_print */
1099 0, /* tp_getattr */
1100 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001101 0, /* tp_reserved */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001102 0, /* tp_repr */
1103 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001104 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001105 0, /* tp_as_mapping */
1106 0, /* tp_hash */
1107 0, /* tp_call */
1108 0, /* tp_str */
1109 PyObject_GenericGetAttr, /* tp_getattro */
1110 0, /* tp_setattro */
1111 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001112 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001113 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001114 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001115 0, /* tp_clear */
1116 0, /* tp_richcompare */
1117 0, /* tp_weaklistoffset */
1118 PyObject_SelfIter, /* tp_iter */
1119 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001120 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001121 0,
1122};
1123
Guido van Rossum1968ad32006-02-25 22:38:04 +00001124/* defaultdict type *********************************************************/
1125
1126typedef struct {
1127 PyDictObject dict;
1128 PyObject *default_factory;
1129} defdictobject;
1130
1131static PyTypeObject defdict_type; /* Forward */
1132
1133PyDoc_STRVAR(defdict_missing_doc,
1134"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Guido van Rossumd8faa362007-04-27 19:54:29 +00001135 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001136 self[key] = value = self.default_factory()\n\
1137 return value\n\
1138");
1139
1140static PyObject *
1141defdict_missing(defdictobject *dd, PyObject *key)
1142{
1143 PyObject *factory = dd->default_factory;
1144 PyObject *value;
1145 if (factory == NULL || factory == Py_None) {
1146 /* XXX Call dict.__missing__(key) */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001147 PyObject *tup;
1148 tup = PyTuple_Pack(1, key);
1149 if (!tup) return NULL;
1150 PyErr_SetObject(PyExc_KeyError, tup);
1151 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001152 return NULL;
1153 }
1154 value = PyEval_CallObject(factory, NULL);
1155 if (value == NULL)
1156 return value;
1157 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1158 Py_DECREF(value);
1159 return NULL;
1160 }
1161 return value;
1162}
1163
1164PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1165
1166static PyObject *
1167defdict_copy(defdictobject *dd)
1168{
1169 /* This calls the object's class. That only works for subclasses
1170 whose class constructor has the same signature. Subclasses that
Christian Heimes0bd4e112008-02-12 22:59:25 +00001171 define a different constructor signature must override copy().
Guido van Rossum1968ad32006-02-25 22:38:04 +00001172 */
Christian Heimes90aa7642007-12-19 02:45:37 +00001173 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001174 dd->default_factory, dd, NULL);
1175}
1176
1177static PyObject *
1178defdict_reduce(defdictobject *dd)
1179{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001180 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001181
1182 - factory function
1183 - tuple of args for the factory function
1184 - additional state (here None)
1185 - sequence iterator (here None)
1186 - dictionary iterator (yielding successive (key, value) pairs
1187
1188 This API is used by pickle.py and copy.py.
1189
1190 For this to be useful with pickle.py, the default_factory
1191 must be picklable; e.g., None, a built-in, or a global
1192 function in a module or package.
1193
1194 Both shallow and deep copying are supported, but for deep
1195 copying, the default_factory must be deep-copyable; e.g. None,
1196 or a built-in (functions are not copyable at this time).
1197
1198 This only works for subclasses as long as their constructor
1199 signature is compatible; the first argument must be the
1200 optional default_factory, defaulting to None.
1201 */
1202 PyObject *args;
1203 PyObject *items;
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001204 PyObject *iter;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001205 PyObject *result;
1206 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1207 args = PyTuple_New(0);
1208 else
1209 args = PyTuple_Pack(1, dd->default_factory);
1210 if (args == NULL)
1211 return NULL;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001212 items = PyObject_CallMethod((PyObject *)dd, "items", "()");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001213 if (items == NULL) {
1214 Py_DECREF(args);
1215 return NULL;
1216 }
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001217 iter = PyObject_GetIter(items);
1218 if (iter == NULL) {
1219 Py_DECREF(items);
1220 Py_DECREF(args);
1221 return NULL;
1222 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001223 result = PyTuple_Pack(5, Py_TYPE(dd), args,
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001224 Py_None, Py_None, iter);
1225 Py_DECREF(iter);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001226 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001227 Py_DECREF(args);
1228 return result;
1229}
1230
1231static PyMethodDef defdict_methods[] = {
1232 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1233 defdict_missing_doc},
Christian Heimes3feef612008-02-11 06:19:17 +00001234 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1235 defdict_copy_doc},
Guido van Rossum1968ad32006-02-25 22:38:04 +00001236 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1237 defdict_copy_doc},
1238 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1239 reduce_doc},
1240 {NULL}
1241};
1242
1243static PyMemberDef defdict_members[] = {
1244 {"default_factory", T_OBJECT,
1245 offsetof(defdictobject, default_factory), 0,
1246 PyDoc_STR("Factory for default value called by __missing__().")},
1247 {NULL}
1248};
1249
1250static void
1251defdict_dealloc(defdictobject *dd)
1252{
1253 Py_CLEAR(dd->default_factory);
1254 PyDict_Type.tp_dealloc((PyObject *)dd);
1255}
1256
Guido van Rossum1968ad32006-02-25 22:38:04 +00001257static PyObject *
1258defdict_repr(defdictobject *dd)
1259{
Guido van Rossum1968ad32006-02-25 22:38:04 +00001260 PyObject *baserepr;
Christian Heimes77c02eb2008-02-09 02:18:51 +00001261 PyObject *defrepr;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001262 PyObject *result;
1263 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1264 if (baserepr == NULL)
1265 return NULL;
1266 if (dd->default_factory == NULL)
Christian Heimes77c02eb2008-02-09 02:18:51 +00001267 defrepr = PyUnicode_FromString("None");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001268 else
Christian Heimes77c02eb2008-02-09 02:18:51 +00001269 {
1270 int status = Py_ReprEnter(dd->default_factory);
1271 if (status != 0) {
1272 if (status < 0)
1273 return NULL;
1274 defrepr = PyUnicode_FromString("...");
1275 }
1276 else
1277 defrepr = PyObject_Repr(dd->default_factory);
1278 Py_ReprLeave(dd->default_factory);
1279 }
1280 if (defrepr == NULL) {
1281 Py_DECREF(baserepr);
1282 return NULL;
1283 }
1284 result = PyUnicode_FromFormat("defaultdict(%U, %U)",
1285 defrepr, baserepr);
1286 Py_DECREF(defrepr);
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001287 Py_DECREF(baserepr);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001288 return result;
1289}
1290
1291static int
1292defdict_traverse(PyObject *self, visitproc visit, void *arg)
1293{
1294 Py_VISIT(((defdictobject *)self)->default_factory);
1295 return PyDict_Type.tp_traverse(self, visit, arg);
1296}
1297
1298static int
1299defdict_tp_clear(defdictobject *dd)
1300{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001301 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001302 return PyDict_Type.tp_clear((PyObject *)dd);
1303}
1304
1305static int
1306defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1307{
1308 defdictobject *dd = (defdictobject *)self;
1309 PyObject *olddefault = dd->default_factory;
1310 PyObject *newdefault = NULL;
1311 PyObject *newargs;
1312 int result;
1313 if (args == NULL || !PyTuple_Check(args))
1314 newargs = PyTuple_New(0);
1315 else {
1316 Py_ssize_t n = PyTuple_GET_SIZE(args);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001317 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001318 newdefault = PyTuple_GET_ITEM(args, 0);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001319 if (!PyCallable_Check(newdefault)) {
1320 PyErr_SetString(PyExc_TypeError,
1321 "first argument must be callable");
1322 return -1;
1323 }
1324 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001325 newargs = PySequence_GetSlice(args, 1, n);
1326 }
1327 if (newargs == NULL)
1328 return -1;
1329 Py_XINCREF(newdefault);
1330 dd->default_factory = newdefault;
1331 result = PyDict_Type.tp_init(self, newargs, kwds);
1332 Py_DECREF(newargs);
1333 Py_XDECREF(olddefault);
1334 return result;
1335}
1336
1337PyDoc_STRVAR(defdict_doc,
1338"defaultdict(default_factory) --> dict with default factory\n\
1339\n\
1340The default factory is called without arguments to produce\n\
1341a new value when a key is not present, in __getitem__ only.\n\
1342A defaultdict compares equal to a dict with the same items.\n\
1343");
1344
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001345/* See comment in xxsubtype.c */
1346#define DEFERRED_ADDRESS(ADDR) 0
1347
Guido van Rossum1968ad32006-02-25 22:38:04 +00001348static PyTypeObject defdict_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001349 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001350 "collections.defaultdict", /* tp_name */
1351 sizeof(defdictobject), /* tp_basicsize */
1352 0, /* tp_itemsize */
1353 /* methods */
1354 (destructor)defdict_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +00001355 0, /* tp_print */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001356 0, /* tp_getattr */
1357 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001358 0, /* tp_reserved */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001359 (reprfunc)defdict_repr, /* tp_repr */
1360 0, /* tp_as_number */
1361 0, /* tp_as_sequence */
1362 0, /* tp_as_mapping */
1363 0, /* tp_hash */
1364 0, /* tp_call */
1365 0, /* tp_str */
1366 PyObject_GenericGetAttr, /* tp_getattro */
1367 0, /* tp_setattro */
1368 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001369 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1370 /* tp_flags */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001371 defdict_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001373 (inquiry)defdict_tp_clear, /* tp_clear */
1374 0, /* tp_richcompare */
1375 0, /* tp_weaklistoffset*/
1376 0, /* tp_iter */
1377 0, /* tp_iternext */
1378 defdict_methods, /* tp_methods */
1379 defdict_members, /* tp_members */
1380 0, /* tp_getset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001382 0, /* tp_dict */
1383 0, /* tp_descr_get */
1384 0, /* tp_descr_set */
1385 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001386 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001387 PyType_GenericAlloc, /* tp_alloc */
1388 0, /* tp_new */
1389 PyObject_GC_Del, /* tp_free */
1390};
1391
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001392/* module level code ********************************************************/
1393
1394PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001395"High performance data structures.\n\
1396- deque: ordered collection accessible from endpoints only\n\
1397- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001398");
1399
Martin v. Löwis1a214512008-06-11 05:26:20 +00001400
1401static struct PyModuleDef _collectionsmodule = {
1402 PyModuleDef_HEAD_INIT,
1403 "_collections",
1404 module_doc,
1405 -1,
1406 NULL,
1407 NULL,
1408 NULL,
1409 NULL,
1410 NULL
1411};
1412
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001413PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001414PyInit__collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001415{
1416 PyObject *m;
1417
Martin v. Löwis1a214512008-06-11 05:26:20 +00001418 m = PyModule_Create(&_collectionsmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001419 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001420 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001421
1422 if (PyType_Ready(&deque_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001423 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001424 Py_INCREF(&deque_type);
1425 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1426
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001427 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001428 if (PyType_Ready(&defdict_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001429 return NULL;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001430 Py_INCREF(&defdict_type);
1431 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1432
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001433 if (PyType_Ready(&dequeiter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001434 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001435
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001436 if (PyType_Ready(&dequereviter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001437 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001438
Martin v. Löwis1a214512008-06-11 05:26:20 +00001439 return m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001440}