blob: ca24be7300f70fbfb400c6b44553f7c8d7904c03 [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 Hettinger68995862007-10-10 00:26:46 +0000601 if (((dequeobject *)deque)->maxlen == -1)
602 return PyObject_CallFunction((PyObject *)(Py_Type(deque)), "O", deque, NULL);
603 else
604 return PyObject_CallFunction((PyObject *)(Py_Type(deque)), "Oi",
605 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000606}
607
608PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
609
610static PyObject *
611deque_reduce(dequeobject *deque)
612{
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000613 PyObject *dict, *result, *aslist;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000614
Raymond Hettinger952f8802004-11-09 07:27:35 +0000615 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000616 if (dict == NULL)
Raymond Hettinger952f8802004-11-09 07:27:35 +0000617 PyErr_Clear();
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000618 aslist = PySequence_List((PyObject *)deque);
619 if (aslist == NULL) {
Neal Norwitzc47cf7d2007-10-05 03:39:17 +0000620 Py_XDECREF(dict);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000621 return NULL;
622 }
Raymond Hettinger68995862007-10-10 00:26:46 +0000623 if (dict == NULL) {
624 if (deque->maxlen == -1)
625 result = Py_BuildValue("O(O)", Py_Type(deque), aslist);
626 else
627 result = Py_BuildValue("O(Oi)", Py_Type(deque), aslist, deque->maxlen);
628 } else {
629 if (deque->maxlen == -1)
630 result = Py_BuildValue("O(OO)O", Py_Type(deque), aslist, Py_None, dict);
631 else
632 result = Py_BuildValue("O(Oi)O", Py_Type(deque), aslist, deque->maxlen, dict);
633 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000634 Py_XDECREF(dict);
635 Py_DECREF(aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000636 return result;
637}
638
639PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
640
641static PyObject *
642deque_repr(PyObject *deque)
643{
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000644 PyObject *aslist, *result, *fmt;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000645 int i;
646
647 i = Py_ReprEnter(deque);
648 if (i != 0) {
649 if (i < 0)
650 return NULL;
651 return PyString_FromString("[...]");
652 }
653
654 aslist = PySequence_List(deque);
655 if (aslist == NULL) {
656 Py_ReprLeave(deque);
657 return NULL;
658 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000659 if (((dequeobject *)deque)->maxlen != -1)
660 fmt = PyString_FromFormat("deque(%%r, maxlen=%i)",
661 ((dequeobject *)deque)->maxlen);
662 else
663 fmt = PyString_FromString("deque(%r)");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000664 if (fmt == NULL) {
665 Py_DECREF(aslist);
666 Py_ReprLeave(deque);
667 return NULL;
668 }
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000669 result = PyString_Format(fmt, aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000670 Py_DECREF(fmt);
671 Py_DECREF(aslist);
672 Py_ReprLeave(deque);
673 return result;
674}
675
676static int
677deque_tp_print(PyObject *deque, FILE *fp, int flags)
678{
679 PyObject *it, *item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000680 char *emit = ""; /* No separator emitted on first pass */
681 char *separator = ", ";
682 int i;
683
684 i = Py_ReprEnter(deque);
685 if (i != 0) {
686 if (i < 0)
687 return i;
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000688 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000689 fputs("[...]", fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000690 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000691 return 0;
692 }
693
694 it = PyObject_GetIter(deque);
695 if (it == NULL)
696 return -1;
697
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000698 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000699 fputs("deque([", fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000700 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000701 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000702 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000703 fputs(emit, fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000704 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000705 emit = separator;
706 if (PyObject_Print(item, fp, 0) != 0) {
707 Py_DECREF(item);
708 Py_DECREF(it);
709 Py_ReprLeave(deque);
710 return -1;
711 }
712 Py_DECREF(item);
713 }
714 Py_ReprLeave(deque);
715 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000716 if (PyErr_Occurred())
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000717 return -1;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000718
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000719 Py_BEGIN_ALLOW_THREADS
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000720 if (((dequeobject *)deque)->maxlen == -1)
721 fputs("])", fp);
722 else
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000723 fprintf(fp, "], maxlen=%d)", ((dequeobject *)deque)->maxlen);
724 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000725 return 0;
726}
727
Raymond Hettinger738ec902004-02-29 02:15:56 +0000728static PyObject *
729deque_richcompare(PyObject *v, PyObject *w, int op)
730{
731 PyObject *it1=NULL, *it2=NULL, *x, *y;
Armin Rigo974d7572004-10-02 13:59:34 +0000732 int b, vs, ws, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000733
Tim Peters1065f752004-10-01 01:03:29 +0000734 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000735 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000736 Py_INCREF(Py_NotImplemented);
737 return Py_NotImplemented;
738 }
739
740 /* Shortcuts */
741 vs = ((dequeobject *)v)->len;
742 ws = ((dequeobject *)w)->len;
743 if (op == Py_EQ) {
744 if (v == w)
745 Py_RETURN_TRUE;
746 if (vs != ws)
747 Py_RETURN_FALSE;
748 }
749 if (op == Py_NE) {
750 if (v == w)
751 Py_RETURN_FALSE;
752 if (vs != ws)
753 Py_RETURN_TRUE;
754 }
755
756 /* Search for the first index where items are different */
757 it1 = PyObject_GetIter(v);
758 if (it1 == NULL)
759 goto done;
760 it2 = PyObject_GetIter(w);
761 if (it2 == NULL)
762 goto done;
Armin Rigo974d7572004-10-02 13:59:34 +0000763 for (;;) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000764 x = PyIter_Next(it1);
Armin Rigo974d7572004-10-02 13:59:34 +0000765 if (x == NULL && PyErr_Occurred())
Raymond Hettinger738ec902004-02-29 02:15:56 +0000766 goto done;
767 y = PyIter_Next(it2);
Armin Rigo974d7572004-10-02 13:59:34 +0000768 if (x == NULL || y == NULL)
769 break;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000770 b = PyObject_RichCompareBool(x, y, Py_EQ);
771 if (b == 0) {
772 cmp = PyObject_RichCompareBool(x, y, op);
773 Py_DECREF(x);
774 Py_DECREF(y);
775 goto done;
776 }
777 Py_DECREF(x);
778 Py_DECREF(y);
779 if (b == -1)
780 goto done;
781 }
Armin Rigo974d7572004-10-02 13:59:34 +0000782 /* We reached the end of one deque or both */
783 Py_XDECREF(x);
784 Py_XDECREF(y);
785 if (PyErr_Occurred())
786 goto done;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000787 switch (op) {
Armin Rigo974d7572004-10-02 13:59:34 +0000788 case Py_LT: cmp = y != NULL; break; /* if w was longer */
789 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
790 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
791 case Py_NE: cmp = x != y; break; /* if one deque continues */
792 case Py_GT: cmp = x != NULL; break; /* if v was longer */
793 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000794 }
Tim Peters1065f752004-10-01 01:03:29 +0000795
Raymond Hettinger738ec902004-02-29 02:15:56 +0000796done:
797 Py_XDECREF(it1);
798 Py_XDECREF(it2);
799 if (cmp == 1)
800 Py_RETURN_TRUE;
801 if (cmp == 0)
802 Py_RETURN_FALSE;
803 return NULL;
804}
805
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000806static int
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000807deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000808{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000809 PyObject *iterable = NULL;
Raymond Hettinger68995862007-10-10 00:26:46 +0000810 PyObject *maxlenobj = NULL;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000811 int maxlen = -1;
812 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000813
Raymond Hettinger68995862007-10-10 00:26:46 +0000814 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000815 return -1;
Raymond Hettinger68995862007-10-10 00:26:46 +0000816 if (maxlenobj != NULL && maxlenobj != Py_None) {
817 maxlen = PyInt_AsLong(maxlenobj);
818 if (maxlen == -1 && PyErr_Occurred())
819 return -1;
820 if (maxlen < 0) {
821 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
822 return -1;
823 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000824 }
825 deque->maxlen = maxlen;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000826 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000827 PyObject *rv = deque_extend(deque, iterable);
828 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000829 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000830 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000831 }
832 return 0;
833}
834
835static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000836 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000837 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000838 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000839 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000840 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000842};
843
844/* deque object ********************************************************/
845
846static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000847static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000848PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000849 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000850
851static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000852 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000853 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000854 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000855 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000856 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000857 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000858 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000859 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000860 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000861 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000862 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000863 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000864 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000865 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000866 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000867 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000868 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000869 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000870 {"remove", (PyCFunction)deque_remove,
871 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000872 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000873 METH_NOARGS, reversed_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000874 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000875 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000876 {NULL, NULL} /* sentinel */
877};
878
879PyDoc_STRVAR(deque_doc,
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000880"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000881\n\
882Build an ordered collection accessible from endpoints only.");
883
Neal Norwitz87f10132004-02-29 15:40:53 +0000884static PyTypeObject deque_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000885 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000886 "collections.deque", /* tp_name */
887 sizeof(dequeobject), /* tp_basicsize */
888 0, /* tp_itemsize */
889 /* methods */
890 (destructor)deque_dealloc, /* tp_dealloc */
Georg Brandld37ac692006-03-30 11:58:57 +0000891 deque_tp_print, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000892 0, /* tp_getattr */
893 0, /* tp_setattr */
894 0, /* tp_compare */
Georg Brandld37ac692006-03-30 11:58:57 +0000895 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000896 0, /* tp_as_number */
897 &deque_as_sequence, /* tp_as_sequence */
898 0, /* tp_as_mapping */
899 deque_nohash, /* tp_hash */
900 0, /* tp_call */
901 0, /* tp_str */
902 PyObject_GenericGetAttr, /* tp_getattro */
903 0, /* tp_setattro */
904 0, /* tp_as_buffer */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000905 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
906 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000907 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000908 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000909 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000910 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000911 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000912 (getiterfunc)deque_iter, /* tp_iter */
913 0, /* tp_iternext */
914 deque_methods, /* tp_methods */
915 0, /* tp_members */
916 0, /* tp_getset */
917 0, /* tp_base */
918 0, /* tp_dict */
919 0, /* tp_descr_get */
920 0, /* tp_descr_set */
921 0, /* tp_dictoffset */
922 (initproc)deque_init, /* tp_init */
923 PyType_GenericAlloc, /* tp_alloc */
924 deque_new, /* tp_new */
925 PyObject_GC_Del, /* tp_free */
926};
927
928/*********************** Deque Iterator **************************/
929
930typedef struct {
931 PyObject_HEAD
932 int index;
933 block *b;
934 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000935 long state; /* state when the iterator is created */
936 int counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000937} dequeiterobject;
938
939PyTypeObject dequeiter_type;
940
941static PyObject *
942deque_iter(dequeobject *deque)
943{
944 dequeiterobject *it;
945
946 it = PyObject_New(dequeiterobject, &dequeiter_type);
947 if (it == NULL)
948 return NULL;
949 it->b = deque->leftblock;
950 it->index = deque->leftindex;
951 Py_INCREF(deque);
952 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000953 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000954 it->counter = deque->len;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000955 return (PyObject *)it;
956}
957
958static void
959dequeiter_dealloc(dequeiterobject *dio)
960{
961 Py_XDECREF(dio->deque);
Martin v. Löwis68192102007-07-21 06:55:02 +0000962 Py_Type(dio)->tp_free(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000963}
964
965static PyObject *
966dequeiter_next(dequeiterobject *it)
967{
968 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000969
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000970 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000971 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000972 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000973 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000974 return NULL;
975 }
Raymond Hettinger51c2f6c2007-01-08 18:09:20 +0000976 if (it->counter == 0)
977 return NULL;
Tim Peters5566e962006-07-28 00:23:15 +0000978 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000979 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000980
981 item = it->b->data[it->index];
982 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000983 it->counter--;
984 if (it->index == BLOCKLEN && it->counter > 0) {
985 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000986 it->b = it->b->rightlink;
987 it->index = 0;
988 }
989 Py_INCREF(item);
990 return item;
991}
992
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000993static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000994dequeiter_len(dequeiterobject *it)
995{
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000996 return PyInt_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000997}
998
Armin Rigof5b3e362006-02-11 21:32:43 +0000999PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001000
1001static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00001002 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001003 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001004};
1005
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001006PyTypeObject dequeiter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001007 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001008 "deque_iterator", /* tp_name */
1009 sizeof(dequeiterobject), /* tp_basicsize */
1010 0, /* tp_itemsize */
1011 /* methods */
1012 (destructor)dequeiter_dealloc, /* tp_dealloc */
1013 0, /* tp_print */
1014 0, /* tp_getattr */
1015 0, /* tp_setattr */
1016 0, /* tp_compare */
1017 0, /* tp_repr */
1018 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001019 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001020 0, /* tp_as_mapping */
1021 0, /* tp_hash */
1022 0, /* tp_call */
1023 0, /* tp_str */
1024 PyObject_GenericGetAttr, /* tp_getattro */
1025 0, /* tp_setattro */
1026 0, /* tp_as_buffer */
1027 Py_TPFLAGS_DEFAULT, /* tp_flags */
1028 0, /* tp_doc */
1029 0, /* tp_traverse */
1030 0, /* tp_clear */
1031 0, /* tp_richcompare */
1032 0, /* tp_weaklistoffset */
1033 PyObject_SelfIter, /* tp_iter */
1034 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001035 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001036 0,
1037};
1038
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001039/*********************** Deque Reverse Iterator **************************/
1040
1041PyTypeObject dequereviter_type;
1042
1043static PyObject *
1044deque_reviter(dequeobject *deque)
1045{
1046 dequeiterobject *it;
1047
1048 it = PyObject_New(dequeiterobject, &dequereviter_type);
1049 if (it == NULL)
1050 return NULL;
1051 it->b = deque->rightblock;
1052 it->index = deque->rightindex;
1053 Py_INCREF(deque);
1054 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001055 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001056 it->counter = deque->len;
1057 return (PyObject *)it;
1058}
1059
1060static PyObject *
1061dequereviter_next(dequeiterobject *it)
1062{
1063 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001064 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001065 return NULL;
1066
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001067 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001068 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001069 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001070 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001071 return NULL;
1072 }
Tim Peters5566e962006-07-28 00:23:15 +00001073 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001074 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001075
1076 item = it->b->data[it->index];
1077 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001078 it->counter--;
1079 if (it->index == -1 && it->counter > 0) {
1080 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001081 it->b = it->b->leftlink;
1082 it->index = BLOCKLEN - 1;
1083 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001084 Py_INCREF(item);
1085 return item;
1086}
1087
1088PyTypeObject dequereviter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001089 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001090 "deque_reverse_iterator", /* tp_name */
1091 sizeof(dequeiterobject), /* tp_basicsize */
1092 0, /* tp_itemsize */
1093 /* methods */
1094 (destructor)dequeiter_dealloc, /* tp_dealloc */
1095 0, /* tp_print */
1096 0, /* tp_getattr */
1097 0, /* tp_setattr */
1098 0, /* tp_compare */
1099 0, /* tp_repr */
1100 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001101 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001102 0, /* tp_as_mapping */
1103 0, /* tp_hash */
1104 0, /* tp_call */
1105 0, /* tp_str */
1106 PyObject_GenericGetAttr, /* tp_getattro */
1107 0, /* tp_setattro */
1108 0, /* tp_as_buffer */
1109 Py_TPFLAGS_DEFAULT, /* tp_flags */
1110 0, /* tp_doc */
1111 0, /* tp_traverse */
1112 0, /* tp_clear */
1113 0, /* tp_richcompare */
1114 0, /* tp_weaklistoffset */
1115 PyObject_SelfIter, /* tp_iter */
1116 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001117 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001118 0,
1119};
1120
Guido van Rossum1968ad32006-02-25 22:38:04 +00001121/* defaultdict type *********************************************************/
1122
1123typedef struct {
1124 PyDictObject dict;
1125 PyObject *default_factory;
1126} defdictobject;
1127
1128static PyTypeObject defdict_type; /* Forward */
1129
1130PyDoc_STRVAR(defdict_missing_doc,
1131"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Georg Brandlb51a57e2007-03-06 13:32:52 +00001132 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001133 self[key] = value = self.default_factory()\n\
1134 return value\n\
1135");
1136
1137static PyObject *
1138defdict_missing(defdictobject *dd, PyObject *key)
1139{
1140 PyObject *factory = dd->default_factory;
1141 PyObject *value;
1142 if (factory == NULL || factory == Py_None) {
1143 /* XXX Call dict.__missing__(key) */
Georg Brandlb51a57e2007-03-06 13:32:52 +00001144 PyObject *tup;
1145 tup = PyTuple_Pack(1, key);
1146 if (!tup) return NULL;
1147 PyErr_SetObject(PyExc_KeyError, tup);
1148 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001149 return NULL;
1150 }
1151 value = PyEval_CallObject(factory, NULL);
1152 if (value == NULL)
1153 return value;
1154 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1155 Py_DECREF(value);
1156 return NULL;
1157 }
1158 return value;
1159}
1160
1161PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1162
1163static PyObject *
1164defdict_copy(defdictobject *dd)
1165{
1166 /* This calls the object's class. That only works for subclasses
1167 whose class constructor has the same signature. Subclasses that
1168 define a different constructor signature must override copy().
1169 */
Neal Norwitzc47cf7d2007-10-05 03:39:17 +00001170 return PyObject_CallFunctionObjArgs((PyObject*)Py_Type(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001171 dd->default_factory, dd, NULL);
1172}
1173
1174static PyObject *
1175defdict_reduce(defdictobject *dd)
1176{
Tim Peters5566e962006-07-28 00:23:15 +00001177 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001178
1179 - factory function
1180 - tuple of args for the factory function
1181 - additional state (here None)
1182 - sequence iterator (here None)
1183 - dictionary iterator (yielding successive (key, value) pairs
1184
1185 This API is used by pickle.py and copy.py.
1186
1187 For this to be useful with pickle.py, the default_factory
1188 must be picklable; e.g., None, a built-in, or a global
1189 function in a module or package.
1190
1191 Both shallow and deep copying are supported, but for deep
1192 copying, the default_factory must be deep-copyable; e.g. None,
1193 or a built-in (functions are not copyable at this time).
1194
1195 This only works for subclasses as long as their constructor
1196 signature is compatible; the first argument must be the
1197 optional default_factory, defaulting to None.
1198 */
1199 PyObject *args;
1200 PyObject *items;
1201 PyObject *result;
1202 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1203 args = PyTuple_New(0);
1204 else
1205 args = PyTuple_Pack(1, dd->default_factory);
1206 if (args == NULL)
1207 return NULL;
1208 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
1209 if (items == NULL) {
1210 Py_DECREF(args);
1211 return NULL;
1212 }
Martin v. Löwis68192102007-07-21 06:55:02 +00001213 result = PyTuple_Pack(5, Py_Type(dd), args,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001214 Py_None, Py_None, items);
Tim Peters5566e962006-07-28 00:23:15 +00001215 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001216 Py_DECREF(args);
1217 return result;
1218}
1219
1220static PyMethodDef defdict_methods[] = {
1221 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1222 defdict_missing_doc},
1223 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1224 defdict_copy_doc},
1225 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1226 defdict_copy_doc},
1227 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1228 reduce_doc},
1229 {NULL}
1230};
1231
1232static PyMemberDef defdict_members[] = {
1233 {"default_factory", T_OBJECT,
1234 offsetof(defdictobject, default_factory), 0,
1235 PyDoc_STR("Factory for default value called by __missing__().")},
1236 {NULL}
1237};
1238
1239static void
1240defdict_dealloc(defdictobject *dd)
1241{
1242 Py_CLEAR(dd->default_factory);
1243 PyDict_Type.tp_dealloc((PyObject *)dd);
1244}
1245
1246static int
1247defdict_print(defdictobject *dd, FILE *fp, int flags)
1248{
1249 int sts;
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001250 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001251 fprintf(fp, "defaultdict(");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001252 Py_END_ALLOW_THREADS
1253 if (dd->default_factory == NULL) {
1254 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001255 fprintf(fp, "None");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001256 Py_END_ALLOW_THREADS
1257 } else {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001258 PyObject_Print(dd->default_factory, fp, 0);
1259 }
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001260 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001261 fprintf(fp, ", ");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001262 Py_END_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001263 sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001264 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001265 fprintf(fp, ")");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001266 Py_END_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001267 return sts;
1268}
1269
1270static PyObject *
1271defdict_repr(defdictobject *dd)
1272{
1273 PyObject *defrepr;
1274 PyObject *baserepr;
1275 PyObject *result;
1276 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1277 if (baserepr == NULL)
1278 return NULL;
1279 if (dd->default_factory == NULL)
1280 defrepr = PyString_FromString("None");
1281 else
1282 defrepr = PyObject_Repr(dd->default_factory);
1283 if (defrepr == NULL) {
1284 Py_DECREF(baserepr);
1285 return NULL;
1286 }
1287 result = PyString_FromFormat("defaultdict(%s, %s)",
1288 PyString_AS_STRING(defrepr),
1289 PyString_AS_STRING(baserepr));
1290 Py_DECREF(defrepr);
1291 Py_DECREF(baserepr);
1292 return result;
1293}
1294
1295static int
1296defdict_traverse(PyObject *self, visitproc visit, void *arg)
1297{
1298 Py_VISIT(((defdictobject *)self)->default_factory);
1299 return PyDict_Type.tp_traverse(self, visit, arg);
1300}
1301
1302static int
1303defdict_tp_clear(defdictobject *dd)
1304{
Thomas Woutersedf17d82006-04-15 17:28:34 +00001305 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001306 return PyDict_Type.tp_clear((PyObject *)dd);
1307}
1308
1309static int
1310defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1311{
1312 defdictobject *dd = (defdictobject *)self;
1313 PyObject *olddefault = dd->default_factory;
1314 PyObject *newdefault = NULL;
1315 PyObject *newargs;
1316 int result;
1317 if (args == NULL || !PyTuple_Check(args))
1318 newargs = PyTuple_New(0);
1319 else {
1320 Py_ssize_t n = PyTuple_GET_SIZE(args);
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001321 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001322 newdefault = PyTuple_GET_ITEM(args, 0);
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001323 if (!PyCallable_Check(newdefault)) {
1324 PyErr_SetString(PyExc_TypeError,
1325 "first argument must be callable");
1326 return -1;
1327 }
1328 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001329 newargs = PySequence_GetSlice(args, 1, n);
1330 }
1331 if (newargs == NULL)
1332 return -1;
1333 Py_XINCREF(newdefault);
1334 dd->default_factory = newdefault;
1335 result = PyDict_Type.tp_init(self, newargs, kwds);
1336 Py_DECREF(newargs);
1337 Py_XDECREF(olddefault);
1338 return result;
1339}
1340
1341PyDoc_STRVAR(defdict_doc,
1342"defaultdict(default_factory) --> dict with default factory\n\
1343\n\
1344The default factory is called without arguments to produce\n\
1345a new value when a key is not present, in __getitem__ only.\n\
1346A defaultdict compares equal to a dict with the same items.\n\
1347");
1348
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001349/* See comment in xxsubtype.c */
1350#define DEFERRED_ADDRESS(ADDR) 0
1351
Guido van Rossum1968ad32006-02-25 22:38:04 +00001352static PyTypeObject defdict_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001353 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001354 "collections.defaultdict", /* tp_name */
1355 sizeof(defdictobject), /* tp_basicsize */
1356 0, /* tp_itemsize */
1357 /* methods */
1358 (destructor)defdict_dealloc, /* tp_dealloc */
1359 (printfunc)defdict_print, /* tp_print */
1360 0, /* tp_getattr */
1361 0, /* tp_setattr */
1362 0, /* tp_compare */
1363 (reprfunc)defdict_repr, /* tp_repr */
1364 0, /* tp_as_number */
1365 0, /* tp_as_sequence */
1366 0, /* tp_as_mapping */
1367 0, /* tp_hash */
1368 0, /* tp_call */
1369 0, /* tp_str */
1370 PyObject_GenericGetAttr, /* tp_getattro */
1371 0, /* tp_setattro */
1372 0, /* tp_as_buffer */
1373 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1374 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
1375 defdict_doc, /* tp_doc */
Georg Brandld37ac692006-03-30 11:58:57 +00001376 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001377 (inquiry)defdict_tp_clear, /* tp_clear */
1378 0, /* tp_richcompare */
1379 0, /* tp_weaklistoffset*/
1380 0, /* tp_iter */
1381 0, /* tp_iternext */
1382 defdict_methods, /* tp_methods */
1383 defdict_members, /* tp_members */
1384 0, /* tp_getset */
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001385 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001386 0, /* tp_dict */
1387 0, /* tp_descr_get */
1388 0, /* tp_descr_set */
1389 0, /* tp_dictoffset */
Georg Brandld37ac692006-03-30 11:58:57 +00001390 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001391 PyType_GenericAlloc, /* tp_alloc */
1392 0, /* tp_new */
1393 PyObject_GC_Del, /* tp_free */
1394};
1395
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001396/* module level code ********************************************************/
1397
1398PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001399"High performance data structures.\n\
1400- deque: ordered collection accessible from endpoints only\n\
1401- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001402");
1403
1404PyMODINIT_FUNC
Raymond Hettingereb979882007-02-28 18:37:52 +00001405init_collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001406{
1407 PyObject *m;
1408
Raymond Hettingereb979882007-02-28 18:37:52 +00001409 m = Py_InitModule3("_collections", NULL, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001410 if (m == NULL)
1411 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001412
1413 if (PyType_Ready(&deque_type) < 0)
1414 return;
1415 Py_INCREF(&deque_type);
1416 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1417
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001418 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001419 if (PyType_Ready(&defdict_type) < 0)
1420 return;
1421 Py_INCREF(&defdict_type);
1422 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1423
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001424 if (PyType_Ready(&dequeiter_type) < 0)
Tim Peters1065f752004-10-01 01:03:29 +00001425 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001426
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001427 if (PyType_Ready(&dequereviter_type) < 0)
1428 return;
1429
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001430 return;
1431}