blob: 78a71bf077e59929e5ae0bfaf0c3e0f660a1baf7 [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
Tim Peters5566e962006-07-28 00:23:15 +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
Tim Peters5566e962006-07-28 00:23:15 +000025 * on both ends, algorithms for left and right operations become
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000026 * symmetrical which simplifies the design.
Tim Peters5566e962006-07-28 00:23:15 +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 *
Tim Peters5566e962006-07-28 00:23:15 +000040 * Whenever d.leftblock == d.rightblock,
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000041 * d.leftindex + d.len - 1 == d.rightindex.
Tim Peters5566e962006-07-28 00:23:15 +000042 *
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000043 * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
Tim Peters5566e962006-07-28 00:23:15 +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
Tim Peters6f853562004-10-01 01:04:50 +000054static block *
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000055newblock(block *leftlink, block *rightlink, int len) {
56 block *b;
57 /* To prevent len from overflowing INT_MAX on 64-bit machines, we
58 * refuse to allocate new blocks if the current len is dangerously
59 * close. There is some extra margin to prevent spurious arithmetic
60 * overflows at various places. The following check ensures that
61 * the blocks allocated to the deque, in the worst case, can only
62 * have INT_MAX-2 entries in total.
63 */
64 if (len >= INT_MAX - 2*BLOCKLEN) {
65 PyErr_SetString(PyExc_OverflowError,
66 "cannot add more blocks to the deque");
67 return NULL;
68 }
69 b = PyMem_Malloc(sizeof(block));
Raymond Hettinger756b3f32004-01-29 06:37:52 +000070 if (b == NULL) {
71 PyErr_NoMemory();
72 return NULL;
73 }
74 b->leftlink = leftlink;
75 b->rightlink = rightlink;
76 return b;
77}
78
79typedef struct {
80 PyObject_HEAD
81 block *leftblock;
82 block *rightblock;
Tim Petersd8768d32004-10-01 01:32:53 +000083 int leftindex; /* in range(BLOCKLEN) */
84 int rightindex; /* in range(BLOCKLEN) */
Raymond Hettinger756b3f32004-01-29 06:37:52 +000085 int len;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +000086 int maxlen;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +000087 long state; /* incremented whenever the indices move */
Raymond Hettinger691d8052004-05-30 07:26:47 +000088 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger756b3f32004-01-29 06:37:52 +000089} dequeobject;
90
Raymond Hettingera7fc4b12007-10-05 02:47:07 +000091/* The deque's size limit is d.maxlen. The limit can be zero or positive.
92 * If there is no limit, then d.maxlen == -1.
93 *
94 * After an item is added to a deque, we check to see if the size has grown past
95 * the limit. If it has, we get the size back down to the limit by popping an
96 * item off of the opposite end. The methods that can trigger this are append(),
97 * appendleft(), extend(), and extendleft().
98 */
99
100#define TRIM(d, popfunction) \
101 if (d->maxlen != -1 && d->len > d->maxlen) { \
102 PyObject *rv = popfunction(d, NULL); \
103 assert(rv != NULL && d->len <= d->maxlen); \
104 Py_DECREF(rv); \
105 }
106
Neal Norwitz87f10132004-02-29 15:40:53 +0000107static PyTypeObject deque_type;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000108
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000109static PyObject *
110deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
111{
112 dequeobject *deque;
113 block *b;
114
115 /* create dequeobject structure */
116 deque = (dequeobject *)type->tp_alloc(type, 0);
117 if (deque == NULL)
118 return NULL;
Tim Peters1065f752004-10-01 01:03:29 +0000119
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000120 b = newblock(NULL, NULL, 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000121 if (b == NULL) {
122 Py_DECREF(deque);
123 return NULL;
124 }
125
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000126 assert(BLOCKLEN >= 2);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000127 deque->leftblock = b;
128 deque->rightblock = b;
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000129 deque->leftindex = CENTER + 1;
130 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000131 deque->len = 0;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000132 deque->state = 0;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000133 deque->weakreflist = NULL;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000134 deque->maxlen = -1;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000135
136 return (PyObject *)deque;
137}
138
139static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000140deque_pop(dequeobject *deque, PyObject *unused)
141{
142 PyObject *item;
143 block *prevblock;
144
145 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000146 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000147 return NULL;
148 }
149 item = deque->rightblock->data[deque->rightindex];
150 deque->rightindex--;
151 deque->len--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000152 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000153
154 if (deque->rightindex == -1) {
155 if (deque->len == 0) {
156 assert(deque->leftblock == deque->rightblock);
157 assert(deque->leftindex == deque->rightindex+1);
158 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000159 deque->leftindex = CENTER + 1;
160 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000161 } else {
162 prevblock = deque->rightblock->leftlink;
163 assert(deque->leftblock != deque->rightblock);
164 PyMem_Free(deque->rightblock);
165 prevblock->rightlink = NULL;
166 deque->rightblock = prevblock;
167 deque->rightindex = BLOCKLEN - 1;
168 }
169 }
170 return item;
171}
172
173PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
174
175static PyObject *
176deque_popleft(dequeobject *deque, PyObject *unused)
177{
178 PyObject *item;
179 block *prevblock;
180
181 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000182 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000183 return NULL;
184 }
Neal Norwitzccc56c72006-08-13 18:13:02 +0000185 assert(deque->leftblock != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000186 item = deque->leftblock->data[deque->leftindex];
187 deque->leftindex++;
188 deque->len--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000189 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000190
191 if (deque->leftindex == BLOCKLEN) {
192 if (deque->len == 0) {
193 assert(deque->leftblock == deque->rightblock);
194 assert(deque->leftindex == deque->rightindex+1);
195 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000196 deque->leftindex = CENTER + 1;
197 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000198 } else {
199 assert(deque->leftblock != deque->rightblock);
200 prevblock = deque->leftblock->rightlink;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000201 PyMem_Free(deque->leftblock);
202 assert(prevblock != NULL);
203 prevblock->leftlink = NULL;
204 deque->leftblock = prevblock;
205 deque->leftindex = 0;
206 }
207 }
208 return item;
209}
210
211PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
212
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000213static PyObject *
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000214deque_append(dequeobject *deque, PyObject *item)
215{
216 deque->state++;
217 if (deque->rightindex == BLOCKLEN-1) {
218 block *b = newblock(deque->rightblock, NULL, deque->len);
219 if (b == NULL)
220 return NULL;
221 assert(deque->rightblock->rightlink == NULL);
222 deque->rightblock->rightlink = b;
223 deque->rightblock = b;
224 deque->rightindex = -1;
225 }
226 Py_INCREF(item);
227 deque->len++;
228 deque->rightindex++;
229 deque->rightblock->data[deque->rightindex] = item;
230 TRIM(deque, deque_popleft);
231 Py_RETURN_NONE;
232}
233
234PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
235
236static PyObject *
237deque_appendleft(dequeobject *deque, PyObject *item)
238{
239 deque->state++;
240 if (deque->leftindex == 0) {
241 block *b = newblock(NULL, deque->leftblock, deque->len);
242 if (b == NULL)
243 return NULL;
244 assert(deque->leftblock->leftlink == NULL);
245 deque->leftblock->leftlink = b;
246 deque->leftblock = b;
247 deque->leftindex = BLOCKLEN;
248 }
249 Py_INCREF(item);
250 deque->len++;
251 deque->leftindex--;
252 deque->leftblock->data[deque->leftindex] = item;
253 TRIM(deque, deque_pop);
254 Py_RETURN_NONE;
255}
256
257PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
258
259static PyObject *
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000260deque_extend(dequeobject *deque, PyObject *iterable)
261{
262 PyObject *it, *item;
263
264 it = PyObject_GetIter(iterable);
265 if (it == NULL)
266 return NULL;
267
268 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000269 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000270 if (deque->rightindex == BLOCKLEN-1) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000271 block *b = newblock(deque->rightblock, NULL,
272 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000273 if (b == NULL) {
274 Py_DECREF(item);
275 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000276 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000277 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000278 assert(deque->rightblock->rightlink == NULL);
279 deque->rightblock->rightlink = b;
280 deque->rightblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000281 deque->rightindex = -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000282 }
Armin Rigo974d7572004-10-02 13:59:34 +0000283 deque->len++;
284 deque->rightindex++;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000285 deque->rightblock->data[deque->rightindex] = item;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000286 TRIM(deque, deque_popleft);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000287 }
288 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000289 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000290 return NULL;
291 Py_RETURN_NONE;
292}
293
Tim Peters1065f752004-10-01 01:03:29 +0000294PyDoc_STRVAR(extend_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000295"Extend the right side of the deque with elements from the iterable");
296
297static PyObject *
298deque_extendleft(dequeobject *deque, PyObject *iterable)
299{
300 PyObject *it, *item;
301
302 it = PyObject_GetIter(iterable);
303 if (it == NULL)
304 return NULL;
305
306 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000307 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000308 if (deque->leftindex == 0) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000309 block *b = newblock(NULL, deque->leftblock,
310 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000311 if (b == NULL) {
312 Py_DECREF(item);
313 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000314 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000315 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000316 assert(deque->leftblock->leftlink == NULL);
317 deque->leftblock->leftlink = b;
318 deque->leftblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000319 deque->leftindex = BLOCKLEN;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000320 }
Armin Rigo974d7572004-10-02 13:59:34 +0000321 deque->len++;
322 deque->leftindex--;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000323 deque->leftblock->data[deque->leftindex] = item;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000324 TRIM(deque, deque_pop);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000325 }
326 Py_DECREF(it);
Raymond Hettingera435c532004-07-09 04:10:20 +0000327 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000328 return NULL;
329 Py_RETURN_NONE;
330}
331
Tim Peters1065f752004-10-01 01:03:29 +0000332PyDoc_STRVAR(extendleft_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000333"Extend the left side of the deque with elements from the iterable");
334
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000335static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000336_deque_rotate(dequeobject *deque, Py_ssize_t n)
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000337{
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000338 int i, len=deque->len, halflen=(len+1)>>1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000339 PyObject *item, *rv;
340
Raymond Hettingeree33b272004-02-08 04:05:26 +0000341 if (len == 0)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000342 return 0;
Raymond Hettingeree33b272004-02-08 04:05:26 +0000343 if (n > halflen || n < -halflen) {
344 n %= len;
345 if (n > halflen)
346 n -= len;
347 else if (n < -halflen)
348 n += len;
349 }
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000350
351 for (i=0 ; i<n ; i++) {
352 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000353 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000354 rv = deque_appendleft(deque, item);
355 Py_DECREF(item);
356 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000357 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000358 Py_DECREF(rv);
359 }
360 for (i=0 ; i>n ; i--) {
361 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000362 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000363 rv = deque_append(deque, item);
364 Py_DECREF(item);
365 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000366 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000367 Py_DECREF(rv);
368 }
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000369 return 0;
370}
371
372static PyObject *
373deque_rotate(dequeobject *deque, PyObject *args)
374{
375 int n=1;
376
377 if (!PyArg_ParseTuple(args, "|i:rotate", &n))
378 return NULL;
379 if (_deque_rotate(deque, n) == 0)
380 Py_RETURN_NONE;
381 return NULL;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000382}
383
Tim Peters1065f752004-10-01 01:03:29 +0000384PyDoc_STRVAR(rotate_doc,
Raymond Hettingeree33b272004-02-08 04:05:26 +0000385"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000386
Martin v. Löwis18e16552006-02-15 17:27:45 +0000387static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000388deque_len(dequeobject *deque)
389{
390 return deque->len;
391}
392
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000393static PyObject *
394deque_remove(dequeobject *deque, PyObject *value)
395{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000396 Py_ssize_t i, n=deque->len;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000397
398 for (i=0 ; i<n ; i++) {
399 PyObject *item = deque->leftblock->data[deque->leftindex];
400 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000401
402 if (deque->len != n) {
Tim Peters5566e962006-07-28 00:23:15 +0000403 PyErr_SetString(PyExc_IndexError,
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000404 "deque mutated during remove().");
405 return NULL;
406 }
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000407 if (cmp > 0) {
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000408 PyObject *tgt = deque_popleft(deque, NULL);
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000409 assert (tgt != NULL);
410 Py_DECREF(tgt);
411 if (_deque_rotate(deque, i) == -1)
412 return NULL;
413 Py_RETURN_NONE;
414 }
415 else if (cmp < 0) {
416 _deque_rotate(deque, i);
417 return NULL;
418 }
419 _deque_rotate(deque, -1);
420 }
421 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
422 return NULL;
423}
424
425PyDoc_STRVAR(remove_doc,
426"D.remove(value) -- remove first occurrence of value.");
427
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000428static int
429deque_clear(dequeobject *deque)
430{
431 PyObject *item;
432
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000433 while (deque->len) {
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000434 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000435 assert (item != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000436 Py_DECREF(item);
437 }
438 assert(deque->leftblock == deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000439 deque->leftindex - 1 == deque->rightindex &&
440 deque->len == 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000441 return 0;
442}
443
444static PyObject *
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000445deque_item(dequeobject *deque, int i)
446{
447 block *b;
448 PyObject *item;
Armin Rigo974d7572004-10-02 13:59:34 +0000449 int n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000450
451 if (i < 0 || i >= deque->len) {
452 PyErr_SetString(PyExc_IndexError,
453 "deque index out of range");
454 return NULL;
455 }
456
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000457 if (i == 0) {
458 i = deque->leftindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000459 b = deque->leftblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000460 } else if (i == deque->len - 1) {
461 i = deque->rightindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000462 b = deque->rightblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000463 } else {
464 i += deque->leftindex;
465 n = i / BLOCKLEN;
466 i %= BLOCKLEN;
Armin Rigo974d7572004-10-02 13:59:34 +0000467 if (index < (deque->len >> 1)) {
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000468 b = deque->leftblock;
469 while (n--)
470 b = b->rightlink;
471 } else {
472 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
473 b = deque->rightblock;
474 while (n--)
475 b = b->leftlink;
476 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000477 }
478 item = b->data[i];
479 Py_INCREF(item);
480 return item;
481}
482
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000483/* delitem() implemented in terms of rotate for simplicity and reasonable
484 performance near the end points. If for some reason this method becomes
Tim Peters1065f752004-10-01 01:03:29 +0000485 popular, it is not hard to re-implement this using direct data movement
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000486 (similar to code in list slice assignment) and achieve a two or threefold
487 performance boost.
488*/
489
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000490static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000491deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000492{
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000493 PyObject *item;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000494
Tim Peters1065f752004-10-01 01:03:29 +0000495 assert (i >= 0 && i < deque->len);
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000496 if (_deque_rotate(deque, -i) == -1)
497 return -1;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000498
499 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000500 assert (item != NULL);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000501 Py_DECREF(item);
502
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000503 return _deque_rotate(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000504}
505
506static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000507deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000508{
509 PyObject *old_value;
510 block *b;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000511 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000512
Raymond Hettingera435c532004-07-09 04:10:20 +0000513 if (i < 0 || i >= len) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000514 PyErr_SetString(PyExc_IndexError,
515 "deque index out of range");
516 return -1;
517 }
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000518 if (v == NULL)
519 return deque_del_item(deque, i);
520
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000521 i += deque->leftindex;
522 n = i / BLOCKLEN;
523 i %= BLOCKLEN;
Raymond Hettingera435c532004-07-09 04:10:20 +0000524 if (index <= halflen) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000525 b = deque->leftblock;
526 while (n--)
527 b = b->rightlink;
528 } else {
Raymond Hettingera435c532004-07-09 04:10:20 +0000529 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000530 b = deque->rightblock;
531 while (n--)
532 b = b->leftlink;
533 }
534 Py_INCREF(v);
535 old_value = b->data[i];
536 b->data[i] = v;
537 Py_DECREF(old_value);
538 return 0;
539}
540
541static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000542deque_clearmethod(dequeobject *deque)
543{
Raymond Hettingera435c532004-07-09 04:10:20 +0000544 int rv;
545
546 rv = deque_clear(deque);
547 assert (rv != -1);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000548 Py_RETURN_NONE;
549}
550
551PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
552
553static void
554deque_dealloc(dequeobject *deque)
555{
556 PyObject_GC_UnTrack(deque);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000557 if (deque->weakreflist != NULL)
558 PyObject_ClearWeakRefs((PyObject *) deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000559 if (deque->leftblock != NULL) {
Raymond Hettingere9c89e82004-07-19 00:10:24 +0000560 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000561 assert(deque->leftblock != NULL);
562 PyMem_Free(deque->leftblock);
563 }
564 deque->leftblock = NULL;
565 deque->rightblock = NULL;
Martin v. Löwis68192102007-07-21 06:55:02 +0000566 Py_Type(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000567}
568
569static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000570deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000571{
Tim Peters10c7e862004-10-01 02:01:04 +0000572 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000573 PyObject *item;
Tim Peters10c7e862004-10-01 02:01:04 +0000574 int index;
575 int indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000576
Tim Peters10c7e862004-10-01 02:01:04 +0000577 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
578 const int indexhi = b == deque->rightblock ?
579 deque->rightindex :
580 BLOCKLEN - 1;
581
582 for (index = indexlo; index <= indexhi; ++index) {
583 item = b->data[index];
584 Py_VISIT(item);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000585 }
Tim Peters10c7e862004-10-01 02:01:04 +0000586 indexlo = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000587 }
588 return 0;
589}
590
591static long
592deque_nohash(PyObject *self)
593{
594 PyErr_SetString(PyExc_TypeError, "deque objects are unhashable");
595 return -1;
596}
597
598static PyObject *
599deque_copy(PyObject *deque)
600{
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000601 return PyObject_CallFunction((PyObject *)(Py_Type(deque)), "Oi",
602 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000603}
604
605PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
606
607static PyObject *
608deque_reduce(dequeobject *deque)
609{
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000610 PyObject *dict, *result, *aslist;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000611
Raymond Hettinger952f8802004-11-09 07:27:35 +0000612 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000613 if (dict == NULL)
Raymond Hettinger952f8802004-11-09 07:27:35 +0000614 PyErr_Clear();
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000615 aslist = PySequence_List((PyObject *)deque);
616 if (aslist == NULL) {
Neal Norwitzc47cf7d2007-10-05 03:39:17 +0000617 Py_XDECREF(dict);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000618 return NULL;
619 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000620 if (dict == NULL)
621 result = Py_BuildValue("O(Oi)", Py_Type(deque), aslist, deque->maxlen);
622 else
623 result = Py_BuildValue("O(Oi)O", Py_Type(deque), aslist, deque->maxlen, dict);
624 Py_XDECREF(dict);
625 Py_DECREF(aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000626 return result;
627}
628
629PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
630
631static PyObject *
632deque_repr(PyObject *deque)
633{
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000634 PyObject *aslist, *result, *fmt; /*, *limit; */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000635 int i;
636
637 i = Py_ReprEnter(deque);
638 if (i != 0) {
639 if (i < 0)
640 return NULL;
641 return PyString_FromString("[...]");
642 }
643
644 aslist = PySequence_List(deque);
645 if (aslist == NULL) {
646 Py_ReprLeave(deque);
647 return NULL;
648 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000649 if (((dequeobject *)deque)->maxlen != -1)
650 fmt = PyString_FromFormat("deque(%%r, maxlen=%i)",
651 ((dequeobject *)deque)->maxlen);
652 else
653 fmt = PyString_FromString("deque(%r)");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000654 if (fmt == NULL) {
655 Py_DECREF(aslist);
656 Py_ReprLeave(deque);
657 return NULL;
658 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000659 result = PyString_Format(fmt, aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000660 Py_DECREF(fmt);
661 Py_DECREF(aslist);
662 Py_ReprLeave(deque);
663 return result;
664}
665
666static int
667deque_tp_print(PyObject *deque, FILE *fp, int flags)
668{
669 PyObject *it, *item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000670 char *emit = ""; /* No separator emitted on first pass */
671 char *separator = ", ";
672 int i;
673
674 i = Py_ReprEnter(deque);
675 if (i != 0) {
676 if (i < 0)
677 return i;
678 fputs("[...]", fp);
679 return 0;
680 }
681
682 it = PyObject_GetIter(deque);
683 if (it == NULL)
684 return -1;
685
686 fputs("deque([", fp);
687 while ((item = PyIter_Next(it)) != NULL) {
688 fputs(emit, fp);
689 emit = separator;
690 if (PyObject_Print(item, fp, 0) != 0) {
691 Py_DECREF(item);
692 Py_DECREF(it);
693 Py_ReprLeave(deque);
694 return -1;
695 }
696 Py_DECREF(item);
697 }
698 Py_ReprLeave(deque);
699 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000700 if (PyErr_Occurred())
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000701 return -1;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000702
703 if (((dequeobject *)deque)->maxlen == -1)
704 fputs("])", fp);
705 else
706 fprintf(fp, "], maxlen=%d)", ((dequeobject *)deque)->maxlen);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000707 return 0;
708}
709
Raymond Hettinger738ec902004-02-29 02:15:56 +0000710static PyObject *
711deque_richcompare(PyObject *v, PyObject *w, int op)
712{
713 PyObject *it1=NULL, *it2=NULL, *x, *y;
Armin Rigo974d7572004-10-02 13:59:34 +0000714 int b, vs, ws, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000715
Tim Peters1065f752004-10-01 01:03:29 +0000716 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000717 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000718 Py_INCREF(Py_NotImplemented);
719 return Py_NotImplemented;
720 }
721
722 /* Shortcuts */
723 vs = ((dequeobject *)v)->len;
724 ws = ((dequeobject *)w)->len;
725 if (op == Py_EQ) {
726 if (v == w)
727 Py_RETURN_TRUE;
728 if (vs != ws)
729 Py_RETURN_FALSE;
730 }
731 if (op == Py_NE) {
732 if (v == w)
733 Py_RETURN_FALSE;
734 if (vs != ws)
735 Py_RETURN_TRUE;
736 }
737
738 /* Search for the first index where items are different */
739 it1 = PyObject_GetIter(v);
740 if (it1 == NULL)
741 goto done;
742 it2 = PyObject_GetIter(w);
743 if (it2 == NULL)
744 goto done;
Armin Rigo974d7572004-10-02 13:59:34 +0000745 for (;;) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000746 x = PyIter_Next(it1);
Armin Rigo974d7572004-10-02 13:59:34 +0000747 if (x == NULL && PyErr_Occurred())
Raymond Hettinger738ec902004-02-29 02:15:56 +0000748 goto done;
749 y = PyIter_Next(it2);
Armin Rigo974d7572004-10-02 13:59:34 +0000750 if (x == NULL || y == NULL)
751 break;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000752 b = PyObject_RichCompareBool(x, y, Py_EQ);
753 if (b == 0) {
754 cmp = PyObject_RichCompareBool(x, y, op);
755 Py_DECREF(x);
756 Py_DECREF(y);
757 goto done;
758 }
759 Py_DECREF(x);
760 Py_DECREF(y);
761 if (b == -1)
762 goto done;
763 }
Armin Rigo974d7572004-10-02 13:59:34 +0000764 /* We reached the end of one deque or both */
765 Py_XDECREF(x);
766 Py_XDECREF(y);
767 if (PyErr_Occurred())
768 goto done;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000769 switch (op) {
Armin Rigo974d7572004-10-02 13:59:34 +0000770 case Py_LT: cmp = y != NULL; break; /* if w was longer */
771 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
772 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
773 case Py_NE: cmp = x != y; break; /* if one deque continues */
774 case Py_GT: cmp = x != NULL; break; /* if v was longer */
775 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000776 }
Tim Peters1065f752004-10-01 01:03:29 +0000777
Raymond Hettinger738ec902004-02-29 02:15:56 +0000778done:
779 Py_XDECREF(it1);
780 Py_XDECREF(it2);
781 if (cmp == 1)
782 Py_RETURN_TRUE;
783 if (cmp == 0)
784 Py_RETURN_FALSE;
785 return NULL;
786}
787
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000788static int
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000789deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000790{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000791 PyObject *iterable = NULL;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000792 int maxlen = -1;
793 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000794
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000795 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|Oi:deque", kwlist, &iterable, &maxlen))
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000796 return -1;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000797 if (maxlen < -1) {
798 PyErr_SetString(PyExc_ValueError, "maxlen must be -1 or greater");
799 return -1;
800 }
801 deque->maxlen = maxlen;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000802 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000803 PyObject *rv = deque_extend(deque, iterable);
804 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000805 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000806 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000807 }
808 return 0;
809}
810
811static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000812 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000813 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000814 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000815 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000816 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000817 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000818};
819
820/* deque object ********************************************************/
821
822static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000823static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000824PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000825 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000826
827static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000828 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000829 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000830 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000831 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000832 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000833 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000834 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000835 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000836 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000837 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000838 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000839 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000840 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000841 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000842 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000843 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000844 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000845 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000846 {"remove", (PyCFunction)deque_remove,
847 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000848 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000849 METH_NOARGS, reversed_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000850 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000851 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000852 {NULL, NULL} /* sentinel */
853};
854
855PyDoc_STRVAR(deque_doc,
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000856"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000857\n\
858Build an ordered collection accessible from endpoints only.");
859
Neal Norwitz87f10132004-02-29 15:40:53 +0000860static PyTypeObject deque_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000861 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000862 "collections.deque", /* tp_name */
863 sizeof(dequeobject), /* tp_basicsize */
864 0, /* tp_itemsize */
865 /* methods */
866 (destructor)deque_dealloc, /* tp_dealloc */
Georg Brandld37ac692006-03-30 11:58:57 +0000867 deque_tp_print, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000868 0, /* tp_getattr */
869 0, /* tp_setattr */
870 0, /* tp_compare */
Georg Brandld37ac692006-03-30 11:58:57 +0000871 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000872 0, /* tp_as_number */
873 &deque_as_sequence, /* tp_as_sequence */
874 0, /* tp_as_mapping */
875 deque_nohash, /* tp_hash */
876 0, /* tp_call */
877 0, /* tp_str */
878 PyObject_GenericGetAttr, /* tp_getattro */
879 0, /* tp_setattro */
880 0, /* tp_as_buffer */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000881 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
882 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000883 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000884 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000885 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000886 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000887 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000888 (getiterfunc)deque_iter, /* tp_iter */
889 0, /* tp_iternext */
890 deque_methods, /* tp_methods */
891 0, /* tp_members */
892 0, /* tp_getset */
893 0, /* tp_base */
894 0, /* tp_dict */
895 0, /* tp_descr_get */
896 0, /* tp_descr_set */
897 0, /* tp_dictoffset */
898 (initproc)deque_init, /* tp_init */
899 PyType_GenericAlloc, /* tp_alloc */
900 deque_new, /* tp_new */
901 PyObject_GC_Del, /* tp_free */
902};
903
904/*********************** Deque Iterator **************************/
905
906typedef struct {
907 PyObject_HEAD
908 int index;
909 block *b;
910 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000911 long state; /* state when the iterator is created */
912 int counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000913} dequeiterobject;
914
915PyTypeObject dequeiter_type;
916
917static PyObject *
918deque_iter(dequeobject *deque)
919{
920 dequeiterobject *it;
921
922 it = PyObject_New(dequeiterobject, &dequeiter_type);
923 if (it == NULL)
924 return NULL;
925 it->b = deque->leftblock;
926 it->index = deque->leftindex;
927 Py_INCREF(deque);
928 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000929 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000930 it->counter = deque->len;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000931 return (PyObject *)it;
932}
933
934static void
935dequeiter_dealloc(dequeiterobject *dio)
936{
937 Py_XDECREF(dio->deque);
Martin v. Löwis68192102007-07-21 06:55:02 +0000938 Py_Type(dio)->tp_free(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000939}
940
941static PyObject *
942dequeiter_next(dequeiterobject *it)
943{
944 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000945
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000946 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000947 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000948 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000949 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000950 return NULL;
951 }
Raymond Hettinger51c2f6c2007-01-08 18:09:20 +0000952 if (it->counter == 0)
953 return NULL;
Tim Peters5566e962006-07-28 00:23:15 +0000954 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000955 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000956
957 item = it->b->data[it->index];
958 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000959 it->counter--;
960 if (it->index == BLOCKLEN && it->counter > 0) {
961 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000962 it->b = it->b->rightlink;
963 it->index = 0;
964 }
965 Py_INCREF(item);
966 return item;
967}
968
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000969static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000970dequeiter_len(dequeiterobject *it)
971{
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000972 return PyInt_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000973}
974
Armin Rigof5b3e362006-02-11 21:32:43 +0000975PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000976
977static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000978 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000979 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000980};
981
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000982PyTypeObject dequeiter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000983 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000984 "deque_iterator", /* tp_name */
985 sizeof(dequeiterobject), /* tp_basicsize */
986 0, /* tp_itemsize */
987 /* methods */
988 (destructor)dequeiter_dealloc, /* tp_dealloc */
989 0, /* tp_print */
990 0, /* tp_getattr */
991 0, /* tp_setattr */
992 0, /* tp_compare */
993 0, /* tp_repr */
994 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000995 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000996 0, /* tp_as_mapping */
997 0, /* tp_hash */
998 0, /* tp_call */
999 0, /* tp_str */
1000 PyObject_GenericGetAttr, /* tp_getattro */
1001 0, /* tp_setattro */
1002 0, /* tp_as_buffer */
1003 Py_TPFLAGS_DEFAULT, /* tp_flags */
1004 0, /* tp_doc */
1005 0, /* tp_traverse */
1006 0, /* tp_clear */
1007 0, /* tp_richcompare */
1008 0, /* tp_weaklistoffset */
1009 PyObject_SelfIter, /* tp_iter */
1010 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001011 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001012 0,
1013};
1014
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001015/*********************** Deque Reverse Iterator **************************/
1016
1017PyTypeObject dequereviter_type;
1018
1019static PyObject *
1020deque_reviter(dequeobject *deque)
1021{
1022 dequeiterobject *it;
1023
1024 it = PyObject_New(dequeiterobject, &dequereviter_type);
1025 if (it == NULL)
1026 return NULL;
1027 it->b = deque->rightblock;
1028 it->index = deque->rightindex;
1029 Py_INCREF(deque);
1030 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001031 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001032 it->counter = deque->len;
1033 return (PyObject *)it;
1034}
1035
1036static PyObject *
1037dequereviter_next(dequeiterobject *it)
1038{
1039 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001040 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001041 return NULL;
1042
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001043 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001044 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001045 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001046 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001047 return NULL;
1048 }
Tim Peters5566e962006-07-28 00:23:15 +00001049 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001050 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001051
1052 item = it->b->data[it->index];
1053 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001054 it->counter--;
1055 if (it->index == -1 && it->counter > 0) {
1056 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001057 it->b = it->b->leftlink;
1058 it->index = BLOCKLEN - 1;
1059 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001060 Py_INCREF(item);
1061 return item;
1062}
1063
1064PyTypeObject dequereviter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001065 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001066 "deque_reverse_iterator", /* tp_name */
1067 sizeof(dequeiterobject), /* tp_basicsize */
1068 0, /* tp_itemsize */
1069 /* methods */
1070 (destructor)dequeiter_dealloc, /* tp_dealloc */
1071 0, /* tp_print */
1072 0, /* tp_getattr */
1073 0, /* tp_setattr */
1074 0, /* tp_compare */
1075 0, /* tp_repr */
1076 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001077 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001078 0, /* tp_as_mapping */
1079 0, /* tp_hash */
1080 0, /* tp_call */
1081 0, /* tp_str */
1082 PyObject_GenericGetAttr, /* tp_getattro */
1083 0, /* tp_setattro */
1084 0, /* tp_as_buffer */
1085 Py_TPFLAGS_DEFAULT, /* tp_flags */
1086 0, /* tp_doc */
1087 0, /* tp_traverse */
1088 0, /* tp_clear */
1089 0, /* tp_richcompare */
1090 0, /* tp_weaklistoffset */
1091 PyObject_SelfIter, /* tp_iter */
1092 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001093 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001094 0,
1095};
1096
Guido van Rossum1968ad32006-02-25 22:38:04 +00001097/* defaultdict type *********************************************************/
1098
1099typedef struct {
1100 PyDictObject dict;
1101 PyObject *default_factory;
1102} defdictobject;
1103
1104static PyTypeObject defdict_type; /* Forward */
1105
1106PyDoc_STRVAR(defdict_missing_doc,
1107"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Georg Brandlb51a57e2007-03-06 13:32:52 +00001108 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001109 self[key] = value = self.default_factory()\n\
1110 return value\n\
1111");
1112
1113static PyObject *
1114defdict_missing(defdictobject *dd, PyObject *key)
1115{
1116 PyObject *factory = dd->default_factory;
1117 PyObject *value;
1118 if (factory == NULL || factory == Py_None) {
1119 /* XXX Call dict.__missing__(key) */
Georg Brandlb51a57e2007-03-06 13:32:52 +00001120 PyObject *tup;
1121 tup = PyTuple_Pack(1, key);
1122 if (!tup) return NULL;
1123 PyErr_SetObject(PyExc_KeyError, tup);
1124 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001125 return NULL;
1126 }
1127 value = PyEval_CallObject(factory, NULL);
1128 if (value == NULL)
1129 return value;
1130 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1131 Py_DECREF(value);
1132 return NULL;
1133 }
1134 return value;
1135}
1136
1137PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1138
1139static PyObject *
1140defdict_copy(defdictobject *dd)
1141{
1142 /* This calls the object's class. That only works for subclasses
1143 whose class constructor has the same signature. Subclasses that
1144 define a different constructor signature must override copy().
1145 */
Neal Norwitzc47cf7d2007-10-05 03:39:17 +00001146 return PyObject_CallFunctionObjArgs((PyObject*)Py_Type(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001147 dd->default_factory, dd, NULL);
1148}
1149
1150static PyObject *
1151defdict_reduce(defdictobject *dd)
1152{
Tim Peters5566e962006-07-28 00:23:15 +00001153 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001154
1155 - factory function
1156 - tuple of args for the factory function
1157 - additional state (here None)
1158 - sequence iterator (here None)
1159 - dictionary iterator (yielding successive (key, value) pairs
1160
1161 This API is used by pickle.py and copy.py.
1162
1163 For this to be useful with pickle.py, the default_factory
1164 must be picklable; e.g., None, a built-in, or a global
1165 function in a module or package.
1166
1167 Both shallow and deep copying are supported, but for deep
1168 copying, the default_factory must be deep-copyable; e.g. None,
1169 or a built-in (functions are not copyable at this time).
1170
1171 This only works for subclasses as long as their constructor
1172 signature is compatible; the first argument must be the
1173 optional default_factory, defaulting to None.
1174 */
1175 PyObject *args;
1176 PyObject *items;
1177 PyObject *result;
1178 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1179 args = PyTuple_New(0);
1180 else
1181 args = PyTuple_Pack(1, dd->default_factory);
1182 if (args == NULL)
1183 return NULL;
1184 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
1185 if (items == NULL) {
1186 Py_DECREF(args);
1187 return NULL;
1188 }
Martin v. Löwis68192102007-07-21 06:55:02 +00001189 result = PyTuple_Pack(5, Py_Type(dd), args,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001190 Py_None, Py_None, items);
Tim Peters5566e962006-07-28 00:23:15 +00001191 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001192 Py_DECREF(args);
1193 return result;
1194}
1195
1196static PyMethodDef defdict_methods[] = {
1197 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1198 defdict_missing_doc},
1199 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1200 defdict_copy_doc},
1201 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1202 defdict_copy_doc},
1203 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1204 reduce_doc},
1205 {NULL}
1206};
1207
1208static PyMemberDef defdict_members[] = {
1209 {"default_factory", T_OBJECT,
1210 offsetof(defdictobject, default_factory), 0,
1211 PyDoc_STR("Factory for default value called by __missing__().")},
1212 {NULL}
1213};
1214
1215static void
1216defdict_dealloc(defdictobject *dd)
1217{
1218 Py_CLEAR(dd->default_factory);
1219 PyDict_Type.tp_dealloc((PyObject *)dd);
1220}
1221
1222static int
1223defdict_print(defdictobject *dd, FILE *fp, int flags)
1224{
1225 int sts;
1226 fprintf(fp, "defaultdict(");
Raymond Hettingera7fc4b12007-10-05 02:47:07 +00001227 if (dd->default_factory == NULL)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001228 fprintf(fp, "None");
1229 else {
1230 PyObject_Print(dd->default_factory, fp, 0);
1231 }
1232 fprintf(fp, ", ");
1233 sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
1234 fprintf(fp, ")");
1235 return sts;
1236}
1237
1238static PyObject *
1239defdict_repr(defdictobject *dd)
1240{
1241 PyObject *defrepr;
1242 PyObject *baserepr;
1243 PyObject *result;
1244 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1245 if (baserepr == NULL)
1246 return NULL;
1247 if (dd->default_factory == NULL)
1248 defrepr = PyString_FromString("None");
1249 else
1250 defrepr = PyObject_Repr(dd->default_factory);
1251 if (defrepr == NULL) {
1252 Py_DECREF(baserepr);
1253 return NULL;
1254 }
1255 result = PyString_FromFormat("defaultdict(%s, %s)",
1256 PyString_AS_STRING(defrepr),
1257 PyString_AS_STRING(baserepr));
1258 Py_DECREF(defrepr);
1259 Py_DECREF(baserepr);
1260 return result;
1261}
1262
1263static int
1264defdict_traverse(PyObject *self, visitproc visit, void *arg)
1265{
1266 Py_VISIT(((defdictobject *)self)->default_factory);
1267 return PyDict_Type.tp_traverse(self, visit, arg);
1268}
1269
1270static int
1271defdict_tp_clear(defdictobject *dd)
1272{
Thomas Woutersedf17d82006-04-15 17:28:34 +00001273 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001274 return PyDict_Type.tp_clear((PyObject *)dd);
1275}
1276
1277static int
1278defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1279{
1280 defdictobject *dd = (defdictobject *)self;
1281 PyObject *olddefault = dd->default_factory;
1282 PyObject *newdefault = NULL;
1283 PyObject *newargs;
1284 int result;
1285 if (args == NULL || !PyTuple_Check(args))
1286 newargs = PyTuple_New(0);
1287 else {
1288 Py_ssize_t n = PyTuple_GET_SIZE(args);
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001289 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001290 newdefault = PyTuple_GET_ITEM(args, 0);
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001291 if (!PyCallable_Check(newdefault)) {
1292 PyErr_SetString(PyExc_TypeError,
1293 "first argument must be callable");
1294 return -1;
1295 }
1296 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001297 newargs = PySequence_GetSlice(args, 1, n);
1298 }
1299 if (newargs == NULL)
1300 return -1;
1301 Py_XINCREF(newdefault);
1302 dd->default_factory = newdefault;
1303 result = PyDict_Type.tp_init(self, newargs, kwds);
1304 Py_DECREF(newargs);
1305 Py_XDECREF(olddefault);
1306 return result;
1307}
1308
1309PyDoc_STRVAR(defdict_doc,
1310"defaultdict(default_factory) --> dict with default factory\n\
1311\n\
1312The default factory is called without arguments to produce\n\
1313a new value when a key is not present, in __getitem__ only.\n\
1314A defaultdict compares equal to a dict with the same items.\n\
1315");
1316
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001317/* See comment in xxsubtype.c */
1318#define DEFERRED_ADDRESS(ADDR) 0
1319
Guido van Rossum1968ad32006-02-25 22:38:04 +00001320static PyTypeObject defdict_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001321 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001322 "collections.defaultdict", /* tp_name */
1323 sizeof(defdictobject), /* tp_basicsize */
1324 0, /* tp_itemsize */
1325 /* methods */
1326 (destructor)defdict_dealloc, /* tp_dealloc */
1327 (printfunc)defdict_print, /* tp_print */
1328 0, /* tp_getattr */
1329 0, /* tp_setattr */
1330 0, /* tp_compare */
1331 (reprfunc)defdict_repr, /* tp_repr */
1332 0, /* tp_as_number */
1333 0, /* tp_as_sequence */
1334 0, /* tp_as_mapping */
1335 0, /* tp_hash */
1336 0, /* tp_call */
1337 0, /* tp_str */
1338 PyObject_GenericGetAttr, /* tp_getattro */
1339 0, /* tp_setattro */
1340 0, /* tp_as_buffer */
1341 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1342 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
1343 defdict_doc, /* tp_doc */
Georg Brandld37ac692006-03-30 11:58:57 +00001344 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001345 (inquiry)defdict_tp_clear, /* tp_clear */
1346 0, /* tp_richcompare */
1347 0, /* tp_weaklistoffset*/
1348 0, /* tp_iter */
1349 0, /* tp_iternext */
1350 defdict_methods, /* tp_methods */
1351 defdict_members, /* tp_members */
1352 0, /* tp_getset */
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001353 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001354 0, /* tp_dict */
1355 0, /* tp_descr_get */
1356 0, /* tp_descr_set */
1357 0, /* tp_dictoffset */
Georg Brandld37ac692006-03-30 11:58:57 +00001358 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001359 PyType_GenericAlloc, /* tp_alloc */
1360 0, /* tp_new */
1361 PyObject_GC_Del, /* tp_free */
1362};
1363
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001364/* module level code ********************************************************/
1365
1366PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001367"High performance data structures.\n\
1368- deque: ordered collection accessible from endpoints only\n\
1369- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001370");
1371
1372PyMODINIT_FUNC
Raymond Hettingereb979882007-02-28 18:37:52 +00001373init_collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001374{
1375 PyObject *m;
1376
Raymond Hettingereb979882007-02-28 18:37:52 +00001377 m = Py_InitModule3("_collections", NULL, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001378 if (m == NULL)
1379 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001380
1381 if (PyType_Ready(&deque_type) < 0)
1382 return;
1383 Py_INCREF(&deque_type);
1384 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1385
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001386 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001387 if (PyType_Ready(&defdict_type) < 0)
1388 return;
1389 Py_INCREF(&defdict_type);
1390 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1391
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001392 if (PyType_Ready(&dequeiter_type) < 0)
Tim Peters1065f752004-10-01 01:03:29 +00001393 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001394
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001395 if (PyType_Ready(&dequereviter_type) < 0)
1396 return;
1397
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001398 return;
1399}