blob: cd8ddca97c8aaa6bb9c85ea5830b0a8b961d5739 [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 Hettinger556b43d2007-10-05 19:07:31 +0000634 PyObject *aslist, *result, *fmt;
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 Hettinger556b43d2007-10-05 19:07:31 +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;
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000678 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000679 fputs("[...]", fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000680 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000681 return 0;
682 }
683
684 it = PyObject_GetIter(deque);
685 if (it == NULL)
686 return -1;
687
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000688 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000689 fputs("deque([", fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000690 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000691 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000692 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000693 fputs(emit, fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000694 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000695 emit = separator;
696 if (PyObject_Print(item, fp, 0) != 0) {
697 Py_DECREF(item);
698 Py_DECREF(it);
699 Py_ReprLeave(deque);
700 return -1;
701 }
702 Py_DECREF(item);
703 }
704 Py_ReprLeave(deque);
705 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000706 if (PyErr_Occurred())
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000707 return -1;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000708
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000709 Py_BEGIN_ALLOW_THREADS
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000710 if (((dequeobject *)deque)->maxlen == -1)
711 fputs("])", fp);
712 else
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000713 fprintf(fp, "], maxlen=%d)", ((dequeobject *)deque)->maxlen);
714 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000715 return 0;
716}
717
Raymond Hettinger738ec902004-02-29 02:15:56 +0000718static PyObject *
719deque_richcompare(PyObject *v, PyObject *w, int op)
720{
721 PyObject *it1=NULL, *it2=NULL, *x, *y;
Armin Rigo974d7572004-10-02 13:59:34 +0000722 int b, vs, ws, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000723
Tim Peters1065f752004-10-01 01:03:29 +0000724 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000725 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000726 Py_INCREF(Py_NotImplemented);
727 return Py_NotImplemented;
728 }
729
730 /* Shortcuts */
731 vs = ((dequeobject *)v)->len;
732 ws = ((dequeobject *)w)->len;
733 if (op == Py_EQ) {
734 if (v == w)
735 Py_RETURN_TRUE;
736 if (vs != ws)
737 Py_RETURN_FALSE;
738 }
739 if (op == Py_NE) {
740 if (v == w)
741 Py_RETURN_FALSE;
742 if (vs != ws)
743 Py_RETURN_TRUE;
744 }
745
746 /* Search for the first index where items are different */
747 it1 = PyObject_GetIter(v);
748 if (it1 == NULL)
749 goto done;
750 it2 = PyObject_GetIter(w);
751 if (it2 == NULL)
752 goto done;
Armin Rigo974d7572004-10-02 13:59:34 +0000753 for (;;) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000754 x = PyIter_Next(it1);
Armin Rigo974d7572004-10-02 13:59:34 +0000755 if (x == NULL && PyErr_Occurred())
Raymond Hettinger738ec902004-02-29 02:15:56 +0000756 goto done;
757 y = PyIter_Next(it2);
Armin Rigo974d7572004-10-02 13:59:34 +0000758 if (x == NULL || y == NULL)
759 break;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000760 b = PyObject_RichCompareBool(x, y, Py_EQ);
761 if (b == 0) {
762 cmp = PyObject_RichCompareBool(x, y, op);
763 Py_DECREF(x);
764 Py_DECREF(y);
765 goto done;
766 }
767 Py_DECREF(x);
768 Py_DECREF(y);
769 if (b == -1)
770 goto done;
771 }
Armin Rigo974d7572004-10-02 13:59:34 +0000772 /* We reached the end of one deque or both */
773 Py_XDECREF(x);
774 Py_XDECREF(y);
775 if (PyErr_Occurred())
776 goto done;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000777 switch (op) {
Armin Rigo974d7572004-10-02 13:59:34 +0000778 case Py_LT: cmp = y != NULL; break; /* if w was longer */
779 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
780 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
781 case Py_NE: cmp = x != y; break; /* if one deque continues */
782 case Py_GT: cmp = x != NULL; break; /* if v was longer */
783 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000784 }
Tim Peters1065f752004-10-01 01:03:29 +0000785
Raymond Hettinger738ec902004-02-29 02:15:56 +0000786done:
787 Py_XDECREF(it1);
788 Py_XDECREF(it2);
789 if (cmp == 1)
790 Py_RETURN_TRUE;
791 if (cmp == 0)
792 Py_RETURN_FALSE;
793 return NULL;
794}
795
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000796static int
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000797deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000798{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000799 PyObject *iterable = NULL;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000800 int maxlen = -1;
801 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000802
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000803 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|Oi:deque", kwlist, &iterable, &maxlen))
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000804 return -1;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000805 if (maxlen < -1) {
806 PyErr_SetString(PyExc_ValueError, "maxlen must be -1 or greater");
807 return -1;
808 }
809 deque->maxlen = maxlen;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000810 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000811 PyObject *rv = deque_extend(deque, iterable);
812 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000813 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000814 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000815 }
816 return 0;
817}
818
819static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000820 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000821 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000822 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000823 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000824 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000825 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000826};
827
828/* deque object ********************************************************/
829
830static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000831static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000832PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000833 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000834
835static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000836 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000837 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000838 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000839 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000840 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000841 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000842 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000843 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000844 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000845 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000846 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000847 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000848 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000849 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000850 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000851 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000852 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000853 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000854 {"remove", (PyCFunction)deque_remove,
855 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000856 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000857 METH_NOARGS, reversed_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000858 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000859 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000860 {NULL, NULL} /* sentinel */
861};
862
863PyDoc_STRVAR(deque_doc,
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000864"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000865\n\
866Build an ordered collection accessible from endpoints only.");
867
Neal Norwitz87f10132004-02-29 15:40:53 +0000868static PyTypeObject deque_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000869 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000870 "collections.deque", /* tp_name */
871 sizeof(dequeobject), /* tp_basicsize */
872 0, /* tp_itemsize */
873 /* methods */
874 (destructor)deque_dealloc, /* tp_dealloc */
Georg Brandld37ac692006-03-30 11:58:57 +0000875 deque_tp_print, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000876 0, /* tp_getattr */
877 0, /* tp_setattr */
878 0, /* tp_compare */
Georg Brandld37ac692006-03-30 11:58:57 +0000879 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000880 0, /* tp_as_number */
881 &deque_as_sequence, /* tp_as_sequence */
882 0, /* tp_as_mapping */
883 deque_nohash, /* tp_hash */
884 0, /* tp_call */
885 0, /* tp_str */
886 PyObject_GenericGetAttr, /* tp_getattro */
887 0, /* tp_setattro */
888 0, /* tp_as_buffer */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000889 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
890 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000891 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000892 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000893 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000894 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000895 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000896 (getiterfunc)deque_iter, /* tp_iter */
897 0, /* tp_iternext */
898 deque_methods, /* tp_methods */
899 0, /* tp_members */
900 0, /* tp_getset */
901 0, /* tp_base */
902 0, /* tp_dict */
903 0, /* tp_descr_get */
904 0, /* tp_descr_set */
905 0, /* tp_dictoffset */
906 (initproc)deque_init, /* tp_init */
907 PyType_GenericAlloc, /* tp_alloc */
908 deque_new, /* tp_new */
909 PyObject_GC_Del, /* tp_free */
910};
911
912/*********************** Deque Iterator **************************/
913
914typedef struct {
915 PyObject_HEAD
916 int index;
917 block *b;
918 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000919 long state; /* state when the iterator is created */
920 int counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000921} dequeiterobject;
922
923PyTypeObject dequeiter_type;
924
925static PyObject *
926deque_iter(dequeobject *deque)
927{
928 dequeiterobject *it;
929
930 it = PyObject_New(dequeiterobject, &dequeiter_type);
931 if (it == NULL)
932 return NULL;
933 it->b = deque->leftblock;
934 it->index = deque->leftindex;
935 Py_INCREF(deque);
936 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000937 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000938 it->counter = deque->len;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000939 return (PyObject *)it;
940}
941
942static void
943dequeiter_dealloc(dequeiterobject *dio)
944{
945 Py_XDECREF(dio->deque);
Martin v. Löwis68192102007-07-21 06:55:02 +0000946 Py_Type(dio)->tp_free(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000947}
948
949static PyObject *
950dequeiter_next(dequeiterobject *it)
951{
952 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000953
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000954 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000955 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000956 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000957 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000958 return NULL;
959 }
Raymond Hettinger51c2f6c2007-01-08 18:09:20 +0000960 if (it->counter == 0)
961 return NULL;
Tim Peters5566e962006-07-28 00:23:15 +0000962 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000963 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000964
965 item = it->b->data[it->index];
966 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000967 it->counter--;
968 if (it->index == BLOCKLEN && it->counter > 0) {
969 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000970 it->b = it->b->rightlink;
971 it->index = 0;
972 }
973 Py_INCREF(item);
974 return item;
975}
976
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000977static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000978dequeiter_len(dequeiterobject *it)
979{
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000980 return PyInt_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000981}
982
Armin Rigof5b3e362006-02-11 21:32:43 +0000983PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000984
985static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000986 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000987 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000988};
989
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000990PyTypeObject dequeiter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000991 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000992 "deque_iterator", /* tp_name */
993 sizeof(dequeiterobject), /* tp_basicsize */
994 0, /* tp_itemsize */
995 /* methods */
996 (destructor)dequeiter_dealloc, /* tp_dealloc */
997 0, /* tp_print */
998 0, /* tp_getattr */
999 0, /* tp_setattr */
1000 0, /* tp_compare */
1001 0, /* tp_repr */
1002 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001003 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001004 0, /* tp_as_mapping */
1005 0, /* tp_hash */
1006 0, /* tp_call */
1007 0, /* tp_str */
1008 PyObject_GenericGetAttr, /* tp_getattro */
1009 0, /* tp_setattro */
1010 0, /* tp_as_buffer */
1011 Py_TPFLAGS_DEFAULT, /* tp_flags */
1012 0, /* tp_doc */
1013 0, /* tp_traverse */
1014 0, /* tp_clear */
1015 0, /* tp_richcompare */
1016 0, /* tp_weaklistoffset */
1017 PyObject_SelfIter, /* tp_iter */
1018 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001019 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001020 0,
1021};
1022
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001023/*********************** Deque Reverse Iterator **************************/
1024
1025PyTypeObject dequereviter_type;
1026
1027static PyObject *
1028deque_reviter(dequeobject *deque)
1029{
1030 dequeiterobject *it;
1031
1032 it = PyObject_New(dequeiterobject, &dequereviter_type);
1033 if (it == NULL)
1034 return NULL;
1035 it->b = deque->rightblock;
1036 it->index = deque->rightindex;
1037 Py_INCREF(deque);
1038 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001039 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001040 it->counter = deque->len;
1041 return (PyObject *)it;
1042}
1043
1044static PyObject *
1045dequereviter_next(dequeiterobject *it)
1046{
1047 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001048 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001049 return NULL;
1050
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001051 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001052 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001053 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001054 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001055 return NULL;
1056 }
Tim Peters5566e962006-07-28 00:23:15 +00001057 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001058 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001059
1060 item = it->b->data[it->index];
1061 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001062 it->counter--;
1063 if (it->index == -1 && it->counter > 0) {
1064 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001065 it->b = it->b->leftlink;
1066 it->index = BLOCKLEN - 1;
1067 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001068 Py_INCREF(item);
1069 return item;
1070}
1071
1072PyTypeObject dequereviter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001073 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001074 "deque_reverse_iterator", /* tp_name */
1075 sizeof(dequeiterobject), /* tp_basicsize */
1076 0, /* tp_itemsize */
1077 /* methods */
1078 (destructor)dequeiter_dealloc, /* tp_dealloc */
1079 0, /* tp_print */
1080 0, /* tp_getattr */
1081 0, /* tp_setattr */
1082 0, /* tp_compare */
1083 0, /* tp_repr */
1084 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001085 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001086 0, /* tp_as_mapping */
1087 0, /* tp_hash */
1088 0, /* tp_call */
1089 0, /* tp_str */
1090 PyObject_GenericGetAttr, /* tp_getattro */
1091 0, /* tp_setattro */
1092 0, /* tp_as_buffer */
1093 Py_TPFLAGS_DEFAULT, /* tp_flags */
1094 0, /* tp_doc */
1095 0, /* tp_traverse */
1096 0, /* tp_clear */
1097 0, /* tp_richcompare */
1098 0, /* tp_weaklistoffset */
1099 PyObject_SelfIter, /* tp_iter */
1100 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001101 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001102 0,
1103};
1104
Guido van Rossum1968ad32006-02-25 22:38:04 +00001105/* defaultdict type *********************************************************/
1106
1107typedef struct {
1108 PyDictObject dict;
1109 PyObject *default_factory;
1110} defdictobject;
1111
1112static PyTypeObject defdict_type; /* Forward */
1113
1114PyDoc_STRVAR(defdict_missing_doc,
1115"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Georg Brandlb51a57e2007-03-06 13:32:52 +00001116 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001117 self[key] = value = self.default_factory()\n\
1118 return value\n\
1119");
1120
1121static PyObject *
1122defdict_missing(defdictobject *dd, PyObject *key)
1123{
1124 PyObject *factory = dd->default_factory;
1125 PyObject *value;
1126 if (factory == NULL || factory == Py_None) {
1127 /* XXX Call dict.__missing__(key) */
Georg Brandlb51a57e2007-03-06 13:32:52 +00001128 PyObject *tup;
1129 tup = PyTuple_Pack(1, key);
1130 if (!tup) return NULL;
1131 PyErr_SetObject(PyExc_KeyError, tup);
1132 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001133 return NULL;
1134 }
1135 value = PyEval_CallObject(factory, NULL);
1136 if (value == NULL)
1137 return value;
1138 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1139 Py_DECREF(value);
1140 return NULL;
1141 }
1142 return value;
1143}
1144
1145PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1146
1147static PyObject *
1148defdict_copy(defdictobject *dd)
1149{
1150 /* This calls the object's class. That only works for subclasses
1151 whose class constructor has the same signature. Subclasses that
1152 define a different constructor signature must override copy().
1153 */
Neal Norwitzc47cf7d2007-10-05 03:39:17 +00001154 return PyObject_CallFunctionObjArgs((PyObject*)Py_Type(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001155 dd->default_factory, dd, NULL);
1156}
1157
1158static PyObject *
1159defdict_reduce(defdictobject *dd)
1160{
Tim Peters5566e962006-07-28 00:23:15 +00001161 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001162
1163 - factory function
1164 - tuple of args for the factory function
1165 - additional state (here None)
1166 - sequence iterator (here None)
1167 - dictionary iterator (yielding successive (key, value) pairs
1168
1169 This API is used by pickle.py and copy.py.
1170
1171 For this to be useful with pickle.py, the default_factory
1172 must be picklable; e.g., None, a built-in, or a global
1173 function in a module or package.
1174
1175 Both shallow and deep copying are supported, but for deep
1176 copying, the default_factory must be deep-copyable; e.g. None,
1177 or a built-in (functions are not copyable at this time).
1178
1179 This only works for subclasses as long as their constructor
1180 signature is compatible; the first argument must be the
1181 optional default_factory, defaulting to None.
1182 */
1183 PyObject *args;
1184 PyObject *items;
1185 PyObject *result;
1186 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1187 args = PyTuple_New(0);
1188 else
1189 args = PyTuple_Pack(1, dd->default_factory);
1190 if (args == NULL)
1191 return NULL;
1192 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
1193 if (items == NULL) {
1194 Py_DECREF(args);
1195 return NULL;
1196 }
Martin v. Löwis68192102007-07-21 06:55:02 +00001197 result = PyTuple_Pack(5, Py_Type(dd), args,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001198 Py_None, Py_None, items);
Tim Peters5566e962006-07-28 00:23:15 +00001199 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001200 Py_DECREF(args);
1201 return result;
1202}
1203
1204static PyMethodDef defdict_methods[] = {
1205 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1206 defdict_missing_doc},
1207 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1208 defdict_copy_doc},
1209 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1210 defdict_copy_doc},
1211 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1212 reduce_doc},
1213 {NULL}
1214};
1215
1216static PyMemberDef defdict_members[] = {
1217 {"default_factory", T_OBJECT,
1218 offsetof(defdictobject, default_factory), 0,
1219 PyDoc_STR("Factory for default value called by __missing__().")},
1220 {NULL}
1221};
1222
1223static void
1224defdict_dealloc(defdictobject *dd)
1225{
1226 Py_CLEAR(dd->default_factory);
1227 PyDict_Type.tp_dealloc((PyObject *)dd);
1228}
1229
1230static int
1231defdict_print(defdictobject *dd, FILE *fp, int flags)
1232{
1233 int sts;
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001234 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001235 fprintf(fp, "defaultdict(");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001236 Py_END_ALLOW_THREADS
1237 if (dd->default_factory == NULL) {
1238 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001239 fprintf(fp, "None");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001240 Py_END_ALLOW_THREADS
1241 } else {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001242 PyObject_Print(dd->default_factory, fp, 0);
1243 }
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001244 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001245 fprintf(fp, ", ");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001246 Py_END_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001247 sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001248 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001249 fprintf(fp, ")");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001250 Py_END_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001251 return sts;
1252}
1253
1254static PyObject *
1255defdict_repr(defdictobject *dd)
1256{
1257 PyObject *defrepr;
1258 PyObject *baserepr;
1259 PyObject *result;
1260 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1261 if (baserepr == NULL)
1262 return NULL;
1263 if (dd->default_factory == NULL)
1264 defrepr = PyString_FromString("None");
1265 else
1266 defrepr = PyObject_Repr(dd->default_factory);
1267 if (defrepr == NULL) {
1268 Py_DECREF(baserepr);
1269 return NULL;
1270 }
1271 result = PyString_FromFormat("defaultdict(%s, %s)",
1272 PyString_AS_STRING(defrepr),
1273 PyString_AS_STRING(baserepr));
1274 Py_DECREF(defrepr);
1275 Py_DECREF(baserepr);
1276 return result;
1277}
1278
1279static int
1280defdict_traverse(PyObject *self, visitproc visit, void *arg)
1281{
1282 Py_VISIT(((defdictobject *)self)->default_factory);
1283 return PyDict_Type.tp_traverse(self, visit, arg);
1284}
1285
1286static int
1287defdict_tp_clear(defdictobject *dd)
1288{
Thomas Woutersedf17d82006-04-15 17:28:34 +00001289 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001290 return PyDict_Type.tp_clear((PyObject *)dd);
1291}
1292
1293static int
1294defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1295{
1296 defdictobject *dd = (defdictobject *)self;
1297 PyObject *olddefault = dd->default_factory;
1298 PyObject *newdefault = NULL;
1299 PyObject *newargs;
1300 int result;
1301 if (args == NULL || !PyTuple_Check(args))
1302 newargs = PyTuple_New(0);
1303 else {
1304 Py_ssize_t n = PyTuple_GET_SIZE(args);
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001305 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001306 newdefault = PyTuple_GET_ITEM(args, 0);
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001307 if (!PyCallable_Check(newdefault)) {
1308 PyErr_SetString(PyExc_TypeError,
1309 "first argument must be callable");
1310 return -1;
1311 }
1312 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001313 newargs = PySequence_GetSlice(args, 1, n);
1314 }
1315 if (newargs == NULL)
1316 return -1;
1317 Py_XINCREF(newdefault);
1318 dd->default_factory = newdefault;
1319 result = PyDict_Type.tp_init(self, newargs, kwds);
1320 Py_DECREF(newargs);
1321 Py_XDECREF(olddefault);
1322 return result;
1323}
1324
1325PyDoc_STRVAR(defdict_doc,
1326"defaultdict(default_factory) --> dict with default factory\n\
1327\n\
1328The default factory is called without arguments to produce\n\
1329a new value when a key is not present, in __getitem__ only.\n\
1330A defaultdict compares equal to a dict with the same items.\n\
1331");
1332
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001333/* See comment in xxsubtype.c */
1334#define DEFERRED_ADDRESS(ADDR) 0
1335
Guido van Rossum1968ad32006-02-25 22:38:04 +00001336static PyTypeObject defdict_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001337 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001338 "collections.defaultdict", /* tp_name */
1339 sizeof(defdictobject), /* tp_basicsize */
1340 0, /* tp_itemsize */
1341 /* methods */
1342 (destructor)defdict_dealloc, /* tp_dealloc */
1343 (printfunc)defdict_print, /* tp_print */
1344 0, /* tp_getattr */
1345 0, /* tp_setattr */
1346 0, /* tp_compare */
1347 (reprfunc)defdict_repr, /* tp_repr */
1348 0, /* tp_as_number */
1349 0, /* tp_as_sequence */
1350 0, /* tp_as_mapping */
1351 0, /* tp_hash */
1352 0, /* tp_call */
1353 0, /* tp_str */
1354 PyObject_GenericGetAttr, /* tp_getattro */
1355 0, /* tp_setattro */
1356 0, /* tp_as_buffer */
1357 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1358 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
1359 defdict_doc, /* tp_doc */
Georg Brandld37ac692006-03-30 11:58:57 +00001360 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001361 (inquiry)defdict_tp_clear, /* tp_clear */
1362 0, /* tp_richcompare */
1363 0, /* tp_weaklistoffset*/
1364 0, /* tp_iter */
1365 0, /* tp_iternext */
1366 defdict_methods, /* tp_methods */
1367 defdict_members, /* tp_members */
1368 0, /* tp_getset */
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001369 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001370 0, /* tp_dict */
1371 0, /* tp_descr_get */
1372 0, /* tp_descr_set */
1373 0, /* tp_dictoffset */
Georg Brandld37ac692006-03-30 11:58:57 +00001374 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001375 PyType_GenericAlloc, /* tp_alloc */
1376 0, /* tp_new */
1377 PyObject_GC_Del, /* tp_free */
1378};
1379
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001380/* module level code ********************************************************/
1381
1382PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001383"High performance data structures.\n\
1384- deque: ordered collection accessible from endpoints only\n\
1385- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001386");
1387
1388PyMODINIT_FUNC
Raymond Hettingereb979882007-02-28 18:37:52 +00001389init_collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001390{
1391 PyObject *m;
1392
Raymond Hettingereb979882007-02-28 18:37:52 +00001393 m = Py_InitModule3("_collections", NULL, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001394 if (m == NULL)
1395 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001396
1397 if (PyType_Ready(&deque_type) < 0)
1398 return;
1399 Py_INCREF(&deque_type);
1400 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1401
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001402 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001403 if (PyType_Ready(&defdict_type) < 0)
1404 return;
1405 Py_INCREF(&defdict_type);
1406 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1407
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001408 if (PyType_Ready(&dequeiter_type) < 0)
Tim Peters1065f752004-10-01 01:03:29 +00001409 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001410
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001411 if (PyType_Ready(&dequereviter_type) < 0)
1412 return;
1413
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001414 return;
1415}