blob: cdcf4ef9884661acf0c47244f67a2b509a14bb0f [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
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000430static PyObject *
431deque_reverse(dequeobject *deque, PyObject *unused)
432{
433 block *leftblock = deque->leftblock;
434 block *rightblock = deque->rightblock;
435 Py_ssize_t leftindex = deque->leftindex;
436 Py_ssize_t rightindex = deque->rightindex;
437 Py_ssize_t n = (deque->len)/2;
438 Py_ssize_t i;
439 PyObject *tmp;
440
441 for (i=0 ; i<n ; i++) {
442 /* Validate that pointers haven't met in the middle */
443 assert(leftblock != rightblock || leftindex < rightindex);
444
445 /* Swap */
446 tmp = leftblock->data[leftindex];
447 leftblock->data[leftindex] = rightblock->data[rightindex];
448 rightblock->data[rightindex] = tmp;
449
450 /* Advance left block/index pair */
451 leftindex++;
452 if (leftindex == BLOCKLEN) {
453 assert (leftblock->rightlink != NULL);
454 leftblock = leftblock->rightlink;
455 leftindex = 0;
456 }
457
458 /* Step backwards with the right block/index pair */
459 rightindex--;
460 if (rightindex == -1) {
461 assert (rightblock->leftlink != NULL);
462 rightblock = rightblock->leftlink;
463 rightindex = BLOCKLEN - 1;
464 }
465 }
466 Py_RETURN_NONE;
467}
468
469PyDoc_STRVAR(reverse_doc,
470"D.reverse() -- reverse *IN PLACE*");
471
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000473deque_len(dequeobject *deque)
474{
475 return deque->len;
476}
477
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000478static PyObject *
479deque_remove(dequeobject *deque, PyObject *value)
480{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000481 Py_ssize_t i, n=deque->len;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000482
483 for (i=0 ; i<n ; i++) {
484 PyObject *item = deque->leftblock->data[deque->leftindex];
485 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000486
487 if (deque->len != n) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000488 PyErr_SetString(PyExc_IndexError,
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000489 "deque mutated during remove().");
490 return NULL;
491 }
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000492 if (cmp > 0) {
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000493 PyObject *tgt = deque_popleft(deque, NULL);
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000494 assert (tgt != NULL);
495 Py_DECREF(tgt);
496 if (_deque_rotate(deque, i) == -1)
497 return NULL;
498 Py_RETURN_NONE;
499 }
500 else if (cmp < 0) {
501 _deque_rotate(deque, i);
502 return NULL;
503 }
504 _deque_rotate(deque, -1);
505 }
506 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
507 return NULL;
508}
509
510PyDoc_STRVAR(remove_doc,
511"D.remove(value) -- remove first occurrence of value.");
512
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000513static int
514deque_clear(dequeobject *deque)
515{
516 PyObject *item;
517
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000518 while (deque->len) {
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000519 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000520 assert (item != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000521 Py_DECREF(item);
522 }
523 assert(deque->leftblock == deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000524 deque->leftindex - 1 == deque->rightindex &&
525 deque->len == 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000526 return 0;
527}
528
529static PyObject *
Benjamin Petersond6313712008-07-31 16:23:04 +0000530deque_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000531{
532 block *b;
533 PyObject *item;
Benjamin Petersond6313712008-07-31 16:23:04 +0000534 Py_ssize_t n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000535
536 if (i < 0 || i >= deque->len) {
537 PyErr_SetString(PyExc_IndexError,
538 "deque index out of range");
539 return NULL;
540 }
541
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000542 if (i == 0) {
543 i = deque->leftindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000544 b = deque->leftblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000545 } else if (i == deque->len - 1) {
546 i = deque->rightindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000547 b = deque->rightblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000548 } else {
549 i += deque->leftindex;
550 n = i / BLOCKLEN;
551 i %= BLOCKLEN;
Armin Rigo974d7572004-10-02 13:59:34 +0000552 if (index < (deque->len >> 1)) {
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000553 b = deque->leftblock;
554 while (n--)
555 b = b->rightlink;
556 } else {
557 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
558 b = deque->rightblock;
559 while (n--)
560 b = b->leftlink;
561 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000562 }
563 item = b->data[i];
564 Py_INCREF(item);
565 return item;
566}
567
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000568/* delitem() implemented in terms of rotate for simplicity and reasonable
569 performance near the end points. If for some reason this method becomes
Tim Peters1065f752004-10-01 01:03:29 +0000570 popular, it is not hard to re-implement this using direct data movement
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000571 (similar to code in list slice assignment) and achieve a two or threefold
572 performance boost.
573*/
574
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000575static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000576deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000577{
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000578 PyObject *item;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000579
Tim Peters1065f752004-10-01 01:03:29 +0000580 assert (i >= 0 && i < deque->len);
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000581 if (_deque_rotate(deque, -i) == -1)
582 return -1;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000583
584 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000585 assert (item != NULL);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000586 Py_DECREF(item);
587
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000588 return _deque_rotate(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000589}
590
591static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000592deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000593{
594 PyObject *old_value;
595 block *b;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000596 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000597
Raymond Hettingera435c532004-07-09 04:10:20 +0000598 if (i < 0 || i >= len) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000599 PyErr_SetString(PyExc_IndexError,
600 "deque index out of range");
601 return -1;
602 }
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000603 if (v == NULL)
604 return deque_del_item(deque, i);
605
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000606 i += deque->leftindex;
607 n = i / BLOCKLEN;
608 i %= BLOCKLEN;
Raymond Hettingera435c532004-07-09 04:10:20 +0000609 if (index <= halflen) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000610 b = deque->leftblock;
611 while (n--)
612 b = b->rightlink;
613 } else {
Raymond Hettingera435c532004-07-09 04:10:20 +0000614 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000615 b = deque->rightblock;
616 while (n--)
617 b = b->leftlink;
618 }
619 Py_INCREF(v);
620 old_value = b->data[i];
621 b->data[i] = v;
622 Py_DECREF(old_value);
623 return 0;
624}
625
626static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000627deque_clearmethod(dequeobject *deque)
628{
Raymond Hettingera435c532004-07-09 04:10:20 +0000629 int rv;
630
631 rv = deque_clear(deque);
632 assert (rv != -1);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000633 Py_RETURN_NONE;
634}
635
636PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
637
638static void
639deque_dealloc(dequeobject *deque)
640{
641 PyObject_GC_UnTrack(deque);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000642 if (deque->weakreflist != NULL)
643 PyObject_ClearWeakRefs((PyObject *) deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000644 if (deque->leftblock != NULL) {
Raymond Hettingere9c89e82004-07-19 00:10:24 +0000645 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000646 assert(deque->leftblock != NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +0000647 freeblock(deque->leftblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000648 }
649 deque->leftblock = NULL;
650 deque->rightblock = NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000651 Py_TYPE(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000652}
653
654static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000655deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000656{
Tim Peters10c7e862004-10-01 02:01:04 +0000657 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000658 PyObject *item;
Benjamin Petersond6313712008-07-31 16:23:04 +0000659 Py_ssize_t index;
660 Py_ssize_t indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000661
Tim Peters10c7e862004-10-01 02:01:04 +0000662 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
Benjamin Petersond6313712008-07-31 16:23:04 +0000663 const Py_ssize_t indexhi = b == deque->rightblock ?
Tim Peters10c7e862004-10-01 02:01:04 +0000664 deque->rightindex :
665 BLOCKLEN - 1;
666
667 for (index = indexlo; index <= indexhi; ++index) {
668 item = b->data[index];
669 Py_VISIT(item);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000670 }
Tim Peters10c7e862004-10-01 02:01:04 +0000671 indexlo = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000672 }
673 return 0;
674}
675
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000676static PyObject *
677deque_copy(PyObject *deque)
678{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000679 if (((dequeobject *)deque)->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000680 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000681 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000682 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000683 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000684}
685
686PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
687
688static PyObject *
689deque_reduce(dequeobject *deque)
690{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000691 PyObject *dict, *result, *aslist;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000692
Raymond Hettinger952f8802004-11-09 07:27:35 +0000693 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000694 if (dict == NULL)
Raymond Hettinger952f8802004-11-09 07:27:35 +0000695 PyErr_Clear();
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000696 aslist = PySequence_List((PyObject *)deque);
697 if (aslist == NULL) {
698 Py_XDECREF(dict);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000699 return NULL;
700 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000701 if (dict == NULL) {
702 if (deque->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000703 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000704 else
Benjamin Petersond6313712008-07-31 16:23:04 +0000705 result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000706 } else {
707 if (deque->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000708 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000709 else
Benjamin Petersond6313712008-07-31 16:23:04 +0000710 result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000711 }
712 Py_XDECREF(dict);
713 Py_DECREF(aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000714 return result;
715}
716
717PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
718
719static PyObject *
720deque_repr(PyObject *deque)
721{
Walter Dörwald1ab83302007-05-18 17:15:44 +0000722 PyObject *aslist, *result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000723 int i;
724
725 i = Py_ReprEnter(deque);
726 if (i != 0) {
727 if (i < 0)
728 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000729 return PyUnicode_FromString("[...]");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000730 }
731
732 aslist = PySequence_List(deque);
733 if (aslist == NULL) {
734 Py_ReprLeave(deque);
735 return NULL;
736 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000737 if (((dequeobject *)deque)->maxlen != -1)
Benjamin Petersona786b022008-08-25 21:05:21 +0000738
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000739 result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
Benjamin Petersona786b022008-08-25 21:05:21 +0000740 aslist, ((dequeobject *)deque)->maxlen);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000741 else
742 result = PyUnicode_FromFormat("deque(%R)", aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000743 Py_DECREF(aslist);
744 Py_ReprLeave(deque);
745 return result;
746}
747
Raymond Hettinger738ec902004-02-29 02:15:56 +0000748static PyObject *
749deque_richcompare(PyObject *v, PyObject *w, int op)
750{
751 PyObject *it1=NULL, *it2=NULL, *x, *y;
Benjamin Petersond6313712008-07-31 16:23:04 +0000752 Py_ssize_t vs, ws;
753 int b, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000754
Tim Peters1065f752004-10-01 01:03:29 +0000755 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000756 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000757 Py_INCREF(Py_NotImplemented);
758 return Py_NotImplemented;
759 }
760
761 /* Shortcuts */
762 vs = ((dequeobject *)v)->len;
763 ws = ((dequeobject *)w)->len;
764 if (op == Py_EQ) {
765 if (v == w)
766 Py_RETURN_TRUE;
767 if (vs != ws)
768 Py_RETURN_FALSE;
769 }
770 if (op == Py_NE) {
771 if (v == w)
772 Py_RETURN_FALSE;
773 if (vs != ws)
774 Py_RETURN_TRUE;
775 }
776
777 /* Search for the first index where items are different */
778 it1 = PyObject_GetIter(v);
779 if (it1 == NULL)
780 goto done;
781 it2 = PyObject_GetIter(w);
782 if (it2 == NULL)
783 goto done;
Armin Rigo974d7572004-10-02 13:59:34 +0000784 for (;;) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000785 x = PyIter_Next(it1);
Armin Rigo974d7572004-10-02 13:59:34 +0000786 if (x == NULL && PyErr_Occurred())
Raymond Hettinger738ec902004-02-29 02:15:56 +0000787 goto done;
788 y = PyIter_Next(it2);
Armin Rigo974d7572004-10-02 13:59:34 +0000789 if (x == NULL || y == NULL)
790 break;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000791 b = PyObject_RichCompareBool(x, y, Py_EQ);
792 if (b == 0) {
793 cmp = PyObject_RichCompareBool(x, y, op);
794 Py_DECREF(x);
795 Py_DECREF(y);
796 goto done;
797 }
798 Py_DECREF(x);
799 Py_DECREF(y);
800 if (b == -1)
801 goto done;
802 }
Armin Rigo974d7572004-10-02 13:59:34 +0000803 /* We reached the end of one deque or both */
804 Py_XDECREF(x);
805 Py_XDECREF(y);
806 if (PyErr_Occurred())
807 goto done;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000808 switch (op) {
Armin Rigo974d7572004-10-02 13:59:34 +0000809 case Py_LT: cmp = y != NULL; break; /* if w was longer */
810 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
811 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
812 case Py_NE: cmp = x != y; break; /* if one deque continues */
813 case Py_GT: cmp = x != NULL; break; /* if v was longer */
814 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000815 }
Tim Peters1065f752004-10-01 01:03:29 +0000816
Raymond Hettinger738ec902004-02-29 02:15:56 +0000817done:
818 Py_XDECREF(it1);
819 Py_XDECREF(it2);
820 if (cmp == 1)
821 Py_RETURN_TRUE;
822 if (cmp == 0)
823 Py_RETURN_FALSE;
824 return NULL;
825}
826
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000827static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000828deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000829{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000830 PyObject *iterable = NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000831 PyObject *maxlenobj = NULL;
Benjamin Petersond6313712008-07-31 16:23:04 +0000832 Py_ssize_t maxlen = -1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000833 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000834
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000835 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000836 return -1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000837 if (maxlenobj != NULL && maxlenobj != Py_None) {
Benjamin Petersond6313712008-07-31 16:23:04 +0000838 maxlen = PyLong_AsSsize_t(maxlenobj);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000839 if (maxlen == -1 && PyErr_Occurred())
840 return -1;
841 if (maxlen < 0) {
842 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
843 return -1;
844 }
845 }
846 deque->maxlen = maxlen;
Christian Heimes38053212007-12-14 01:24:44 +0000847 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000848 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000849 PyObject *rv = deque_extend(deque, iterable);
850 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000851 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000852 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000853 }
854 return 0;
855}
856
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000857static PyObject *
858deque_get_maxlen(dequeobject *deque)
859{
860 if (deque->maxlen == -1)
861 Py_RETURN_NONE;
862 return PyLong_FromSsize_t(deque->maxlen);
863}
864
865static PyGetSetDef deque_getset[] = {
866 {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
867 "maximum size of a deque or None if unbounded"},
868 {0}
869};
870
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000871static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000872 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000873 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000874 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000875 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000876 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000877 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000878};
879
880/* deque object ********************************************************/
881
882static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000883static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000884PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000885 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000886
887static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000888 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000889 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000890 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000891 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000892 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000893 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000894 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000895 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000896 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000897 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000898 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000899 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000900 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000901 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000902 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000903 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000904 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000905 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000906 {"remove", (PyCFunction)deque_remove,
907 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000908 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000909 METH_NOARGS, reversed_doc},
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000910 {"reverse", (PyCFunction)deque_reverse,
911 METH_NOARGS, reverse_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000912 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000913 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000914 {NULL, NULL} /* sentinel */
915};
916
917PyDoc_STRVAR(deque_doc,
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000918"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000919\n\
920Build an ordered collection accessible from endpoints only.");
921
Neal Norwitz87f10132004-02-29 15:40:53 +0000922static PyTypeObject deque_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000923 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000924 "collections.deque", /* tp_name */
925 sizeof(dequeobject), /* tp_basicsize */
926 0, /* tp_itemsize */
927 /* methods */
928 (destructor)deque_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000929 0, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000930 0, /* tp_getattr */
931 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000932 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000934 0, /* tp_as_number */
935 &deque_as_sequence, /* tp_as_sequence */
936 0, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +0000937 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000938 0, /* tp_call */
939 0, /* tp_str */
940 PyObject_GenericGetAttr, /* tp_getattro */
941 0, /* tp_setattro */
942 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000943 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
944 /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000945 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000946 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000947 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000948 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000949 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000950 (getiterfunc)deque_iter, /* tp_iter */
951 0, /* tp_iternext */
952 deque_methods, /* tp_methods */
953 0, /* tp_members */
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000954 deque_getset, /* tp_getset */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000955 0, /* tp_base */
956 0, /* tp_dict */
957 0, /* tp_descr_get */
958 0, /* tp_descr_set */
959 0, /* tp_dictoffset */
960 (initproc)deque_init, /* tp_init */
961 PyType_GenericAlloc, /* tp_alloc */
962 deque_new, /* tp_new */
963 PyObject_GC_Del, /* tp_free */
964};
965
966/*********************** Deque Iterator **************************/
967
968typedef struct {
969 PyObject_HEAD
Benjamin Petersond6313712008-07-31 16:23:04 +0000970 Py_ssize_t index;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000971 block *b;
972 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000973 long state; /* state when the iterator is created */
Benjamin Petersond6313712008-07-31 16:23:04 +0000974 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000975} dequeiterobject;
976
Martin v. Löwis59683e82008-06-13 07:50:45 +0000977static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000978
979static PyObject *
980deque_iter(dequeobject *deque)
981{
982 dequeiterobject *it;
983
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000984 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000985 if (it == NULL)
986 return NULL;
987 it->b = deque->leftblock;
988 it->index = deque->leftindex;
989 Py_INCREF(deque);
990 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000991 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000992 it->counter = deque->len;
Georg Brandlb1441c72009-01-03 22:33:39 +0000993 PyObject_GC_Track(it);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000994 return (PyObject *)it;
995}
996
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000997static int
998dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
999{
1000 Py_VISIT(dio->deque);
1001 return 0;
1002}
1003
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001004static void
1005dequeiter_dealloc(dequeiterobject *dio)
1006{
1007 Py_XDECREF(dio->deque);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001008 PyObject_GC_Del(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001009}
1010
1011static PyObject *
1012dequeiter_next(dequeiterobject *it)
1013{
1014 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001015
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001016 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001017 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001018 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001019 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001020 return NULL;
1021 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001022 if (it->counter == 0)
1023 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001024 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001025 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001026
1027 item = it->b->data[it->index];
1028 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001029 it->counter--;
1030 if (it->index == BLOCKLEN && it->counter > 0) {
1031 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001032 it->b = it->b->rightlink;
1033 it->index = 0;
1034 }
1035 Py_INCREF(item);
1036 return item;
1037}
1038
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001039static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001040dequeiter_len(dequeiterobject *it)
1041{
Christian Heimes217cfd12007-12-02 14:31:20 +00001042 return PyLong_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001043}
1044
Armin Rigof5b3e362006-02-11 21:32:43 +00001045PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001046
1047static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00001048 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001049 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001050};
1051
Martin v. Löwis59683e82008-06-13 07:50:45 +00001052static PyTypeObject dequeiter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001053 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001054 "deque_iterator", /* tp_name */
1055 sizeof(dequeiterobject), /* tp_basicsize */
1056 0, /* tp_itemsize */
1057 /* methods */
1058 (destructor)dequeiter_dealloc, /* tp_dealloc */
1059 0, /* tp_print */
1060 0, /* tp_getattr */
1061 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001062 0, /* tp_reserved */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001063 0, /* tp_repr */
1064 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001065 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001066 0, /* tp_as_mapping */
1067 0, /* tp_hash */
1068 0, /* tp_call */
1069 0, /* tp_str */
1070 PyObject_GenericGetAttr, /* tp_getattro */
1071 0, /* tp_setattro */
1072 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001073 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001074 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001075 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001076 0, /* tp_clear */
1077 0, /* tp_richcompare */
1078 0, /* tp_weaklistoffset */
1079 PyObject_SelfIter, /* tp_iter */
1080 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001081 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001082 0,
1083};
1084
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001085/*********************** Deque Reverse Iterator **************************/
1086
Martin v. Löwis59683e82008-06-13 07:50:45 +00001087static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001088
1089static PyObject *
1090deque_reviter(dequeobject *deque)
1091{
1092 dequeiterobject *it;
1093
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001094 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001095 if (it == NULL)
1096 return NULL;
1097 it->b = deque->rightblock;
1098 it->index = deque->rightindex;
1099 Py_INCREF(deque);
1100 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001101 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001102 it->counter = deque->len;
Georg Brandlb1441c72009-01-03 22:33:39 +00001103 PyObject_GC_Track(it);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001104 return (PyObject *)it;
1105}
1106
1107static PyObject *
1108dequereviter_next(dequeiterobject *it)
1109{
1110 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001111 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001112 return NULL;
1113
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001114 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001115 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001116 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001117 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001118 return NULL;
1119 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001120 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001121 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001122
1123 item = it->b->data[it->index];
1124 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001125 it->counter--;
1126 if (it->index == -1 && it->counter > 0) {
1127 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001128 it->b = it->b->leftlink;
1129 it->index = BLOCKLEN - 1;
1130 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001131 Py_INCREF(item);
1132 return item;
1133}
1134
Martin v. Löwis59683e82008-06-13 07:50:45 +00001135static PyTypeObject dequereviter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001136 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001137 "deque_reverse_iterator", /* tp_name */
1138 sizeof(dequeiterobject), /* tp_basicsize */
1139 0, /* tp_itemsize */
1140 /* methods */
1141 (destructor)dequeiter_dealloc, /* tp_dealloc */
1142 0, /* tp_print */
1143 0, /* tp_getattr */
1144 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001145 0, /* tp_reserved */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001146 0, /* tp_repr */
1147 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001148 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001149 0, /* tp_as_mapping */
1150 0, /* tp_hash */
1151 0, /* tp_call */
1152 0, /* tp_str */
1153 PyObject_GenericGetAttr, /* tp_getattro */
1154 0, /* tp_setattro */
1155 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001156 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001157 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001158 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001159 0, /* tp_clear */
1160 0, /* tp_richcompare */
1161 0, /* tp_weaklistoffset */
1162 PyObject_SelfIter, /* tp_iter */
1163 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001164 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001165 0,
1166};
1167
Guido van Rossum1968ad32006-02-25 22:38:04 +00001168/* defaultdict type *********************************************************/
1169
1170typedef struct {
1171 PyDictObject dict;
1172 PyObject *default_factory;
1173} defdictobject;
1174
1175static PyTypeObject defdict_type; /* Forward */
1176
1177PyDoc_STRVAR(defdict_missing_doc,
1178"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Guido van Rossumd8faa362007-04-27 19:54:29 +00001179 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001180 self[key] = value = self.default_factory()\n\
1181 return value\n\
1182");
1183
1184static PyObject *
1185defdict_missing(defdictobject *dd, PyObject *key)
1186{
1187 PyObject *factory = dd->default_factory;
1188 PyObject *value;
1189 if (factory == NULL || factory == Py_None) {
1190 /* XXX Call dict.__missing__(key) */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001191 PyObject *tup;
1192 tup = PyTuple_Pack(1, key);
1193 if (!tup) return NULL;
1194 PyErr_SetObject(PyExc_KeyError, tup);
1195 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001196 return NULL;
1197 }
1198 value = PyEval_CallObject(factory, NULL);
1199 if (value == NULL)
1200 return value;
1201 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1202 Py_DECREF(value);
1203 return NULL;
1204 }
1205 return value;
1206}
1207
1208PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1209
1210static PyObject *
1211defdict_copy(defdictobject *dd)
1212{
1213 /* This calls the object's class. That only works for subclasses
1214 whose class constructor has the same signature. Subclasses that
Christian Heimes0bd4e112008-02-12 22:59:25 +00001215 define a different constructor signature must override copy().
Guido van Rossum1968ad32006-02-25 22:38:04 +00001216 */
Raymond Hettinger54628fa2009-08-04 19:16:39 +00001217
1218 if (dd->default_factory == NULL)
1219 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +00001220 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001221 dd->default_factory, dd, NULL);
1222}
1223
1224static PyObject *
1225defdict_reduce(defdictobject *dd)
1226{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001227 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001228
1229 - factory function
1230 - tuple of args for the factory function
1231 - additional state (here None)
1232 - sequence iterator (here None)
1233 - dictionary iterator (yielding successive (key, value) pairs
1234
1235 This API is used by pickle.py and copy.py.
1236
1237 For this to be useful with pickle.py, the default_factory
1238 must be picklable; e.g., None, a built-in, or a global
1239 function in a module or package.
1240
1241 Both shallow and deep copying are supported, but for deep
1242 copying, the default_factory must be deep-copyable; e.g. None,
1243 or a built-in (functions are not copyable at this time).
1244
1245 This only works for subclasses as long as their constructor
1246 signature is compatible; the first argument must be the
1247 optional default_factory, defaulting to None.
1248 */
1249 PyObject *args;
1250 PyObject *items;
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001251 PyObject *iter;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001252 PyObject *result;
1253 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1254 args = PyTuple_New(0);
1255 else
1256 args = PyTuple_Pack(1, dd->default_factory);
1257 if (args == NULL)
1258 return NULL;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001259 items = PyObject_CallMethod((PyObject *)dd, "items", "()");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001260 if (items == NULL) {
1261 Py_DECREF(args);
1262 return NULL;
1263 }
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001264 iter = PyObject_GetIter(items);
1265 if (iter == NULL) {
1266 Py_DECREF(items);
1267 Py_DECREF(args);
1268 return NULL;
1269 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001270 result = PyTuple_Pack(5, Py_TYPE(dd), args,
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001271 Py_None, Py_None, iter);
1272 Py_DECREF(iter);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001273 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001274 Py_DECREF(args);
1275 return result;
1276}
1277
1278static PyMethodDef defdict_methods[] = {
1279 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1280 defdict_missing_doc},
Christian Heimes3feef612008-02-11 06:19:17 +00001281 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1282 defdict_copy_doc},
Guido van Rossum1968ad32006-02-25 22:38:04 +00001283 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1284 defdict_copy_doc},
1285 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1286 reduce_doc},
1287 {NULL}
1288};
1289
1290static PyMemberDef defdict_members[] = {
1291 {"default_factory", T_OBJECT,
1292 offsetof(defdictobject, default_factory), 0,
1293 PyDoc_STR("Factory for default value called by __missing__().")},
1294 {NULL}
1295};
1296
1297static void
1298defdict_dealloc(defdictobject *dd)
1299{
1300 Py_CLEAR(dd->default_factory);
1301 PyDict_Type.tp_dealloc((PyObject *)dd);
1302}
1303
Guido van Rossum1968ad32006-02-25 22:38:04 +00001304static PyObject *
1305defdict_repr(defdictobject *dd)
1306{
Guido van Rossum1968ad32006-02-25 22:38:04 +00001307 PyObject *baserepr;
Christian Heimes77c02eb2008-02-09 02:18:51 +00001308 PyObject *defrepr;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001309 PyObject *result;
1310 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1311 if (baserepr == NULL)
1312 return NULL;
1313 if (dd->default_factory == NULL)
Christian Heimes77c02eb2008-02-09 02:18:51 +00001314 defrepr = PyUnicode_FromString("None");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001315 else
Christian Heimes77c02eb2008-02-09 02:18:51 +00001316 {
1317 int status = Py_ReprEnter(dd->default_factory);
1318 if (status != 0) {
1319 if (status < 0)
1320 return NULL;
1321 defrepr = PyUnicode_FromString("...");
1322 }
1323 else
1324 defrepr = PyObject_Repr(dd->default_factory);
1325 Py_ReprLeave(dd->default_factory);
1326 }
1327 if (defrepr == NULL) {
1328 Py_DECREF(baserepr);
1329 return NULL;
1330 }
1331 result = PyUnicode_FromFormat("defaultdict(%U, %U)",
1332 defrepr, baserepr);
1333 Py_DECREF(defrepr);
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001334 Py_DECREF(baserepr);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001335 return result;
1336}
1337
1338static int
1339defdict_traverse(PyObject *self, visitproc visit, void *arg)
1340{
1341 Py_VISIT(((defdictobject *)self)->default_factory);
1342 return PyDict_Type.tp_traverse(self, visit, arg);
1343}
1344
1345static int
1346defdict_tp_clear(defdictobject *dd)
1347{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001349 return PyDict_Type.tp_clear((PyObject *)dd);
1350}
1351
1352static int
1353defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1354{
1355 defdictobject *dd = (defdictobject *)self;
1356 PyObject *olddefault = dd->default_factory;
1357 PyObject *newdefault = NULL;
1358 PyObject *newargs;
1359 int result;
1360 if (args == NULL || !PyTuple_Check(args))
1361 newargs = PyTuple_New(0);
1362 else {
1363 Py_ssize_t n = PyTuple_GET_SIZE(args);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001364 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001365 newdefault = PyTuple_GET_ITEM(args, 0);
Raymond Hettinger54628fa2009-08-04 19:16:39 +00001366 if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
Thomas Wouterscf297e42007-02-23 15:07:44 +00001367 PyErr_SetString(PyExc_TypeError,
1368 "first argument must be callable");
1369 return -1;
1370 }
1371 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001372 newargs = PySequence_GetSlice(args, 1, n);
1373 }
1374 if (newargs == NULL)
1375 return -1;
1376 Py_XINCREF(newdefault);
1377 dd->default_factory = newdefault;
1378 result = PyDict_Type.tp_init(self, newargs, kwds);
1379 Py_DECREF(newargs);
1380 Py_XDECREF(olddefault);
1381 return result;
1382}
1383
1384PyDoc_STRVAR(defdict_doc,
1385"defaultdict(default_factory) --> dict with default factory\n\
1386\n\
1387The default factory is called without arguments to produce\n\
1388a new value when a key is not present, in __getitem__ only.\n\
1389A defaultdict compares equal to a dict with the same items.\n\
1390");
1391
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001392/* See comment in xxsubtype.c */
1393#define DEFERRED_ADDRESS(ADDR) 0
1394
Guido van Rossum1968ad32006-02-25 22:38:04 +00001395static PyTypeObject defdict_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001396 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001397 "collections.defaultdict", /* tp_name */
1398 sizeof(defdictobject), /* tp_basicsize */
1399 0, /* tp_itemsize */
1400 /* methods */
1401 (destructor)defdict_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +00001402 0, /* tp_print */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001403 0, /* tp_getattr */
1404 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001405 0, /* tp_reserved */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001406 (reprfunc)defdict_repr, /* tp_repr */
1407 0, /* tp_as_number */
1408 0, /* tp_as_sequence */
1409 0, /* tp_as_mapping */
1410 0, /* tp_hash */
1411 0, /* tp_call */
1412 0, /* tp_str */
1413 PyObject_GenericGetAttr, /* tp_getattro */
1414 0, /* tp_setattro */
1415 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001416 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1417 /* tp_flags */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001418 defdict_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001419 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001420 (inquiry)defdict_tp_clear, /* tp_clear */
1421 0, /* tp_richcompare */
1422 0, /* tp_weaklistoffset*/
1423 0, /* tp_iter */
1424 0, /* tp_iternext */
1425 defdict_methods, /* tp_methods */
1426 defdict_members, /* tp_members */
1427 0, /* tp_getset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001428 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001429 0, /* tp_dict */
1430 0, /* tp_descr_get */
1431 0, /* tp_descr_set */
1432 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001434 PyType_GenericAlloc, /* tp_alloc */
1435 0, /* tp_new */
1436 PyObject_GC_Del, /* tp_free */
1437};
1438
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001439/* module level code ********************************************************/
1440
1441PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001442"High performance data structures.\n\
1443- deque: ordered collection accessible from endpoints only\n\
1444- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001445");
1446
Martin v. Löwis1a214512008-06-11 05:26:20 +00001447
1448static struct PyModuleDef _collectionsmodule = {
1449 PyModuleDef_HEAD_INIT,
1450 "_collections",
1451 module_doc,
1452 -1,
1453 NULL,
1454 NULL,
1455 NULL,
1456 NULL,
1457 NULL
1458};
1459
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001460PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001461PyInit__collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001462{
1463 PyObject *m;
1464
Martin v. Löwis1a214512008-06-11 05:26:20 +00001465 m = PyModule_Create(&_collectionsmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001466 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001467 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001468
1469 if (PyType_Ready(&deque_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001470 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001471 Py_INCREF(&deque_type);
1472 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1473
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001474 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001475 if (PyType_Ready(&defdict_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001476 return NULL;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001477 Py_INCREF(&defdict_type);
1478 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1479
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001480 if (PyType_Ready(&dequeiter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001481 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001482
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001483 if (PyType_Ready(&dequereviter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001484 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001485
Martin v. Löwis1a214512008-06-11 05:26:20 +00001486 return m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001487}