blob: 8d27b88ef62ec273a374ebf1a5430e8da3dcd5a6 [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
815static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000816 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000817 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000818 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000819 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000820 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000821 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000822};
823
824/* deque object ********************************************************/
825
826static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000827static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000828PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000829 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000830
831static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000832 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000833 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000834 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000835 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000836 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000837 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000838 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000839 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000840 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000841 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000842 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000843 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000844 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000845 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000846 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000847 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000848 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000849 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000850 {"remove", (PyCFunction)deque_remove,
851 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000852 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000853 METH_NOARGS, reversed_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000854 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000855 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000856 {NULL, NULL} /* sentinel */
857};
858
859PyDoc_STRVAR(deque_doc,
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000860"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000861\n\
862Build an ordered collection accessible from endpoints only.");
863
Neal Norwitz87f10132004-02-29 15:40:53 +0000864static PyTypeObject deque_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000865 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000866 "collections.deque", /* tp_name */
867 sizeof(dequeobject), /* tp_basicsize */
868 0, /* tp_itemsize */
869 /* methods */
870 (destructor)deque_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000871 0, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000872 0, /* tp_getattr */
873 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000874 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000876 0, /* tp_as_number */
877 &deque_as_sequence, /* tp_as_sequence */
878 0, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +0000879 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000880 0, /* tp_call */
881 0, /* tp_str */
882 PyObject_GenericGetAttr, /* tp_getattro */
883 0, /* tp_setattro */
884 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000885 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
886 /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000887 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000888 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000889 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000890 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000891 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000892 (getiterfunc)deque_iter, /* tp_iter */
893 0, /* tp_iternext */
894 deque_methods, /* tp_methods */
895 0, /* tp_members */
896 0, /* tp_getset */
897 0, /* tp_base */
898 0, /* tp_dict */
899 0, /* tp_descr_get */
900 0, /* tp_descr_set */
901 0, /* tp_dictoffset */
902 (initproc)deque_init, /* tp_init */
903 PyType_GenericAlloc, /* tp_alloc */
904 deque_new, /* tp_new */
905 PyObject_GC_Del, /* tp_free */
906};
907
908/*********************** Deque Iterator **************************/
909
910typedef struct {
911 PyObject_HEAD
Benjamin Petersond6313712008-07-31 16:23:04 +0000912 Py_ssize_t index;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000913 block *b;
914 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000915 long state; /* state when the iterator is created */
Benjamin Petersond6313712008-07-31 16:23:04 +0000916 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000917} dequeiterobject;
918
Martin v. Löwis59683e82008-06-13 07:50:45 +0000919static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000920
921static PyObject *
922deque_iter(dequeobject *deque)
923{
924 dequeiterobject *it;
925
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000926 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000927 if (it == NULL)
928 return NULL;
929 it->b = deque->leftblock;
930 it->index = deque->leftindex;
931 Py_INCREF(deque);
932 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000933 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000934 it->counter = deque->len;
Georg Brandlb1441c72009-01-03 22:33:39 +0000935 PyObject_GC_Track(it);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000936 return (PyObject *)it;
937}
938
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000939static int
940dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
941{
942 Py_VISIT(dio->deque);
943 return 0;
944}
945
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000946static void
947dequeiter_dealloc(dequeiterobject *dio)
948{
949 Py_XDECREF(dio->deque);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000950 PyObject_GC_Del(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000951}
952
953static PyObject *
954dequeiter_next(dequeiterobject *it)
955{
956 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000957
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000958 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000959 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000960 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000961 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000962 return NULL;
963 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000964 if (it->counter == 0)
965 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000967 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000968
969 item = it->b->data[it->index];
970 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000971 it->counter--;
972 if (it->index == BLOCKLEN && it->counter > 0) {
973 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000974 it->b = it->b->rightlink;
975 it->index = 0;
976 }
977 Py_INCREF(item);
978 return item;
979}
980
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000981static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000982dequeiter_len(dequeiterobject *it)
983{
Christian Heimes217cfd12007-12-02 14:31:20 +0000984 return PyLong_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000985}
986
Armin Rigof5b3e362006-02-11 21:32:43 +0000987PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000988
989static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000990 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000991 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000992};
993
Martin v. Löwis59683e82008-06-13 07:50:45 +0000994static PyTypeObject dequeiter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000995 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000996 "deque_iterator", /* tp_name */
997 sizeof(dequeiterobject), /* tp_basicsize */
998 0, /* tp_itemsize */
999 /* methods */
1000 (destructor)dequeiter_dealloc, /* tp_dealloc */
1001 0, /* tp_print */
1002 0, /* tp_getattr */
1003 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001004 0, /* tp_reserved */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001005 0, /* tp_repr */
1006 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001007 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001008 0, /* tp_as_mapping */
1009 0, /* tp_hash */
1010 0, /* tp_call */
1011 0, /* tp_str */
1012 PyObject_GenericGetAttr, /* tp_getattro */
1013 0, /* tp_setattro */
1014 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001015 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001016 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001017 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001018 0, /* tp_clear */
1019 0, /* tp_richcompare */
1020 0, /* tp_weaklistoffset */
1021 PyObject_SelfIter, /* tp_iter */
1022 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001023 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001024 0,
1025};
1026
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001027/*********************** Deque Reverse Iterator **************************/
1028
Martin v. Löwis59683e82008-06-13 07:50:45 +00001029static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001030
1031static PyObject *
1032deque_reviter(dequeobject *deque)
1033{
1034 dequeiterobject *it;
1035
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001036 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001037 if (it == NULL)
1038 return NULL;
1039 it->b = deque->rightblock;
1040 it->index = deque->rightindex;
1041 Py_INCREF(deque);
1042 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001043 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001044 it->counter = deque->len;
Georg Brandlb1441c72009-01-03 22:33:39 +00001045 PyObject_GC_Track(it);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001046 return (PyObject *)it;
1047}
1048
1049static PyObject *
1050dequereviter_next(dequeiterobject *it)
1051{
1052 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001053 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001054 return NULL;
1055
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001056 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001057 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001058 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001059 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001060 return NULL;
1061 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001062 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001063 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001064
1065 item = it->b->data[it->index];
1066 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001067 it->counter--;
1068 if (it->index == -1 && it->counter > 0) {
1069 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001070 it->b = it->b->leftlink;
1071 it->index = BLOCKLEN - 1;
1072 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001073 Py_INCREF(item);
1074 return item;
1075}
1076
Martin v. Löwis59683e82008-06-13 07:50:45 +00001077static PyTypeObject dequereviter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001078 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001079 "deque_reverse_iterator", /* tp_name */
1080 sizeof(dequeiterobject), /* tp_basicsize */
1081 0, /* tp_itemsize */
1082 /* methods */
1083 (destructor)dequeiter_dealloc, /* tp_dealloc */
1084 0, /* tp_print */
1085 0, /* tp_getattr */
1086 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001087 0, /* tp_reserved */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001088 0, /* tp_repr */
1089 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001090 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001091 0, /* tp_as_mapping */
1092 0, /* tp_hash */
1093 0, /* tp_call */
1094 0, /* tp_str */
1095 PyObject_GenericGetAttr, /* tp_getattro */
1096 0, /* tp_setattro */
1097 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001098 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001099 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001100 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001101 0, /* tp_clear */
1102 0, /* tp_richcompare */
1103 0, /* tp_weaklistoffset */
1104 PyObject_SelfIter, /* tp_iter */
1105 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001106 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001107 0,
1108};
1109
Guido van Rossum1968ad32006-02-25 22:38:04 +00001110/* defaultdict type *********************************************************/
1111
1112typedef struct {
1113 PyDictObject dict;
1114 PyObject *default_factory;
1115} defdictobject;
1116
1117static PyTypeObject defdict_type; /* Forward */
1118
1119PyDoc_STRVAR(defdict_missing_doc,
1120"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Guido van Rossumd8faa362007-04-27 19:54:29 +00001121 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001122 self[key] = value = self.default_factory()\n\
1123 return value\n\
1124");
1125
1126static PyObject *
1127defdict_missing(defdictobject *dd, PyObject *key)
1128{
1129 PyObject *factory = dd->default_factory;
1130 PyObject *value;
1131 if (factory == NULL || factory == Py_None) {
1132 /* XXX Call dict.__missing__(key) */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001133 PyObject *tup;
1134 tup = PyTuple_Pack(1, key);
1135 if (!tup) return NULL;
1136 PyErr_SetObject(PyExc_KeyError, tup);
1137 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001138 return NULL;
1139 }
1140 value = PyEval_CallObject(factory, NULL);
1141 if (value == NULL)
1142 return value;
1143 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1144 Py_DECREF(value);
1145 return NULL;
1146 }
1147 return value;
1148}
1149
1150PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1151
1152static PyObject *
1153defdict_copy(defdictobject *dd)
1154{
1155 /* This calls the object's class. That only works for subclasses
1156 whose class constructor has the same signature. Subclasses that
Christian Heimes0bd4e112008-02-12 22:59:25 +00001157 define a different constructor signature must override copy().
Guido van Rossum1968ad32006-02-25 22:38:04 +00001158 */
Christian Heimes90aa7642007-12-19 02:45:37 +00001159 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001160 dd->default_factory, dd, NULL);
1161}
1162
1163static PyObject *
1164defdict_reduce(defdictobject *dd)
1165{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001166 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001167
1168 - factory function
1169 - tuple of args for the factory function
1170 - additional state (here None)
1171 - sequence iterator (here None)
1172 - dictionary iterator (yielding successive (key, value) pairs
1173
1174 This API is used by pickle.py and copy.py.
1175
1176 For this to be useful with pickle.py, the default_factory
1177 must be picklable; e.g., None, a built-in, or a global
1178 function in a module or package.
1179
1180 Both shallow and deep copying are supported, but for deep
1181 copying, the default_factory must be deep-copyable; e.g. None,
1182 or a built-in (functions are not copyable at this time).
1183
1184 This only works for subclasses as long as their constructor
1185 signature is compatible; the first argument must be the
1186 optional default_factory, defaulting to None.
1187 */
1188 PyObject *args;
1189 PyObject *items;
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001190 PyObject *iter;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001191 PyObject *result;
1192 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1193 args = PyTuple_New(0);
1194 else
1195 args = PyTuple_Pack(1, dd->default_factory);
1196 if (args == NULL)
1197 return NULL;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001198 items = PyObject_CallMethod((PyObject *)dd, "items", "()");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001199 if (items == NULL) {
1200 Py_DECREF(args);
1201 return NULL;
1202 }
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001203 iter = PyObject_GetIter(items);
1204 if (iter == NULL) {
1205 Py_DECREF(items);
1206 Py_DECREF(args);
1207 return NULL;
1208 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001209 result = PyTuple_Pack(5, Py_TYPE(dd), args,
Amaury Forgeot d'Arcf43ee812008-10-30 20:58:42 +00001210 Py_None, Py_None, iter);
1211 Py_DECREF(iter);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001212 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001213 Py_DECREF(args);
1214 return result;
1215}
1216
1217static PyMethodDef defdict_methods[] = {
1218 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1219 defdict_missing_doc},
Christian Heimes3feef612008-02-11 06:19:17 +00001220 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1221 defdict_copy_doc},
Guido van Rossum1968ad32006-02-25 22:38:04 +00001222 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1223 defdict_copy_doc},
1224 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1225 reduce_doc},
1226 {NULL}
1227};
1228
1229static PyMemberDef defdict_members[] = {
1230 {"default_factory", T_OBJECT,
1231 offsetof(defdictobject, default_factory), 0,
1232 PyDoc_STR("Factory for default value called by __missing__().")},
1233 {NULL}
1234};
1235
1236static void
1237defdict_dealloc(defdictobject *dd)
1238{
1239 Py_CLEAR(dd->default_factory);
1240 PyDict_Type.tp_dealloc((PyObject *)dd);
1241}
1242
Guido van Rossum1968ad32006-02-25 22:38:04 +00001243static PyObject *
1244defdict_repr(defdictobject *dd)
1245{
Guido van Rossum1968ad32006-02-25 22:38:04 +00001246 PyObject *baserepr;
Christian Heimes77c02eb2008-02-09 02:18:51 +00001247 PyObject *defrepr;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001248 PyObject *result;
1249 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1250 if (baserepr == NULL)
1251 return NULL;
1252 if (dd->default_factory == NULL)
Christian Heimes77c02eb2008-02-09 02:18:51 +00001253 defrepr = PyUnicode_FromString("None");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001254 else
Christian Heimes77c02eb2008-02-09 02:18:51 +00001255 {
1256 int status = Py_ReprEnter(dd->default_factory);
1257 if (status != 0) {
1258 if (status < 0)
1259 return NULL;
1260 defrepr = PyUnicode_FromString("...");
1261 }
1262 else
1263 defrepr = PyObject_Repr(dd->default_factory);
1264 Py_ReprLeave(dd->default_factory);
1265 }
1266 if (defrepr == NULL) {
1267 Py_DECREF(baserepr);
1268 return NULL;
1269 }
1270 result = PyUnicode_FromFormat("defaultdict(%U, %U)",
1271 defrepr, baserepr);
1272 Py_DECREF(defrepr);
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001273 Py_DECREF(baserepr);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001274 return result;
1275}
1276
1277static int
1278defdict_traverse(PyObject *self, visitproc visit, void *arg)
1279{
1280 Py_VISIT(((defdictobject *)self)->default_factory);
1281 return PyDict_Type.tp_traverse(self, visit, arg);
1282}
1283
1284static int
1285defdict_tp_clear(defdictobject *dd)
1286{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001288 return PyDict_Type.tp_clear((PyObject *)dd);
1289}
1290
1291static int
1292defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1293{
1294 defdictobject *dd = (defdictobject *)self;
1295 PyObject *olddefault = dd->default_factory;
1296 PyObject *newdefault = NULL;
1297 PyObject *newargs;
1298 int result;
1299 if (args == NULL || !PyTuple_Check(args))
1300 newargs = PyTuple_New(0);
1301 else {
1302 Py_ssize_t n = PyTuple_GET_SIZE(args);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001303 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001304 newdefault = PyTuple_GET_ITEM(args, 0);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001305 if (!PyCallable_Check(newdefault)) {
1306 PyErr_SetString(PyExc_TypeError,
1307 "first argument must be callable");
1308 return -1;
1309 }
1310 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001311 newargs = PySequence_GetSlice(args, 1, n);
1312 }
1313 if (newargs == NULL)
1314 return -1;
1315 Py_XINCREF(newdefault);
1316 dd->default_factory = newdefault;
1317 result = PyDict_Type.tp_init(self, newargs, kwds);
1318 Py_DECREF(newargs);
1319 Py_XDECREF(olddefault);
1320 return result;
1321}
1322
1323PyDoc_STRVAR(defdict_doc,
1324"defaultdict(default_factory) --> dict with default factory\n\
1325\n\
1326The default factory is called without arguments to produce\n\
1327a new value when a key is not present, in __getitem__ only.\n\
1328A defaultdict compares equal to a dict with the same items.\n\
1329");
1330
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001331/* See comment in xxsubtype.c */
1332#define DEFERRED_ADDRESS(ADDR) 0
1333
Guido van Rossum1968ad32006-02-25 22:38:04 +00001334static PyTypeObject defdict_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001335 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001336 "collections.defaultdict", /* tp_name */
1337 sizeof(defdictobject), /* tp_basicsize */
1338 0, /* tp_itemsize */
1339 /* methods */
1340 (destructor)defdict_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +00001341 0, /* tp_print */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001342 0, /* tp_getattr */
1343 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001344 0, /* tp_reserved */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001345 (reprfunc)defdict_repr, /* tp_repr */
1346 0, /* tp_as_number */
1347 0, /* tp_as_sequence */
1348 0, /* tp_as_mapping */
1349 0, /* tp_hash */
1350 0, /* tp_call */
1351 0, /* tp_str */
1352 PyObject_GenericGetAttr, /* tp_getattro */
1353 0, /* tp_setattro */
1354 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001355 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1356 /* tp_flags */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001357 defdict_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001358 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001359 (inquiry)defdict_tp_clear, /* tp_clear */
1360 0, /* tp_richcompare */
1361 0, /* tp_weaklistoffset*/
1362 0, /* tp_iter */
1363 0, /* tp_iternext */
1364 defdict_methods, /* tp_methods */
1365 defdict_members, /* tp_members */
1366 0, /* tp_getset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001367 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001368 0, /* tp_dict */
1369 0, /* tp_descr_get */
1370 0, /* tp_descr_set */
1371 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001373 PyType_GenericAlloc, /* tp_alloc */
1374 0, /* tp_new */
1375 PyObject_GC_Del, /* tp_free */
1376};
1377
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001378/* module level code ********************************************************/
1379
1380PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001381"High performance data structures.\n\
1382- deque: ordered collection accessible from endpoints only\n\
1383- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001384");
1385
Martin v. Löwis1a214512008-06-11 05:26:20 +00001386
1387static struct PyModuleDef _collectionsmodule = {
1388 PyModuleDef_HEAD_INIT,
1389 "_collections",
1390 module_doc,
1391 -1,
1392 NULL,
1393 NULL,
1394 NULL,
1395 NULL,
1396 NULL
1397};
1398
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001399PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001400PyInit__collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001401{
1402 PyObject *m;
1403
Martin v. Löwis1a214512008-06-11 05:26:20 +00001404 m = PyModule_Create(&_collectionsmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001405 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001406 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001407
1408 if (PyType_Ready(&deque_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001409 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001410 Py_INCREF(&deque_type);
1411 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1412
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001413 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001414 if (PyType_Ready(&defdict_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001415 return NULL;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001416 Py_INCREF(&defdict_type);
1417 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1418
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001419 if (PyType_Ready(&dequeiter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001420 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001421
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001422 if (PyType_Ready(&dequereviter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001423 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001424
Martin v. Löwis1a214512008-06-11 05:26:20 +00001425 return m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001426}