blob: b26470ea59257ffd7bc805c28c6e4b9ab92e22c3 [file] [log] [blame]
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001#include "Python.h"
Raymond Hettinger691d8052004-05-30 07:26:47 +00002#include "structmember.h"
Raymond Hettinger756b3f32004-01-29 06:37:52 +00003
4/* collections module implementation of a deque() datatype
5 Written and maintained by Raymond D. Hettinger <python@rcn.com>
6 Copyright (c) 2004 Python Software Foundation.
7 All rights reserved.
8*/
9
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000010/* The block length may be set to any number over 1. Larger numbers
11 * reduce the number of calls to the memory allocator but take more
12 * memory. Ideally, BLOCKLEN should be set with an eye to the
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013 * length of a cache line.
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000014 */
15
Raymond Hettinger7d112df2004-11-02 02:11:35 +000016#define BLOCKLEN 62
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000017#define CENTER ((BLOCKLEN - 1) / 2)
Raymond Hettinger756b3f32004-01-29 06:37:52 +000018
Tim Petersd8768d32004-10-01 01:32:53 +000019/* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
20 * This list is not circular (the leftmost block has leftlink==NULL,
21 * and the rightmost block has rightlink==NULL). A deque d's first
22 * element is at d.leftblock[leftindex] and its last element is at
23 * d.rightblock[rightindex]; note that, unlike as for Python slice
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000024 * indices, these indices are inclusive on both ends. By being inclusive
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 * on both ends, algorithms for left and right operations become
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000026 * symmetrical which simplifies the design.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027 *
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000028 * The list of blocks is never empty, so d.leftblock and d.rightblock
29 * are never equal to NULL.
30 *
31 * The indices, d.leftindex and d.rightindex are always in the range
32 * 0 <= index < BLOCKLEN.
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000033 * Their exact relationship is:
34 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000035 *
36 * Empty deques have d.len == 0; d.leftblock==d.rightblock;
37 * d.leftindex == CENTER+1; and d.rightindex == CENTER.
38 * Checking for d.len == 0 is the intended way to see whether d is empty.
39 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000040 * Whenever d.leftblock == d.rightblock,
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000041 * d.leftindex + d.len - 1 == d.rightindex.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042 *
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000043 * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044 * become indices into distinct blocks and either may be larger than the
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000045 * other.
Tim Petersd8768d32004-10-01 01:32:53 +000046 */
47
Raymond Hettinger756b3f32004-01-29 06:37:52 +000048typedef struct BLOCK {
49 struct BLOCK *leftlink;
50 struct BLOCK *rightlink;
51 PyObject *data[BLOCKLEN];
52} block;
53
Guido van Rossum58da9312007-11-10 23:39:45 +000054#define MAXFREEBLOCKS 10
Benjamin Petersond6313712008-07-31 16:23:04 +000055static Py_ssize_t numfreeblocks = 0;
Guido van Rossum58da9312007-11-10 23:39:45 +000056static block *freeblocks[MAXFREEBLOCKS];
57
Tim Peters6f853562004-10-01 01:04:50 +000058static block *
Benjamin Petersond6313712008-07-31 16:23:04 +000059newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000060 block *b;
Benjamin Petersond6313712008-07-31 16:23:04 +000061 /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000062 * refuse to allocate new blocks if the current len is dangerously
63 * close. There is some extra margin to prevent spurious arithmetic
64 * overflows at various places. The following check ensures that
65 * the blocks allocated to the deque, in the worst case, can only
Benjamin Petersond6313712008-07-31 16:23:04 +000066 * have PY_SSIZE_T_MAX-2 entries in total.
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000067 */
Benjamin Petersond6313712008-07-31 16:23:04 +000068 if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000069 PyErr_SetString(PyExc_OverflowError,
70 "cannot add more blocks to the deque");
71 return NULL;
72 }
Guido van Rossum58da9312007-11-10 23:39:45 +000073 if (numfreeblocks) {
74 numfreeblocks -= 1;
75 b = freeblocks[numfreeblocks];
76 } else {
77 b = PyMem_Malloc(sizeof(block));
78 if (b == NULL) {
79 PyErr_NoMemory();
80 return NULL;
81 }
Raymond Hettinger756b3f32004-01-29 06:37:52 +000082 }
83 b->leftlink = leftlink;
84 b->rightlink = rightlink;
85 return b;
86}
87
Martin v. Löwis59683e82008-06-13 07:50:45 +000088static void
Guido van Rossum58da9312007-11-10 23:39:45 +000089freeblock(block *b)
90{
91 if (numfreeblocks < MAXFREEBLOCKS) {
92 freeblocks[numfreeblocks] = b;
93 numfreeblocks++;
94 } else {
95 PyMem_Free(b);
96 }
97}
98
Raymond Hettinger756b3f32004-01-29 06:37:52 +000099typedef struct {
100 PyObject_HEAD
101 block *leftblock;
102 block *rightblock;
Benjamin Petersond6313712008-07-31 16:23:04 +0000103 Py_ssize_t leftindex; /* in range(BLOCKLEN) */
104 Py_ssize_t rightindex; /* in range(BLOCKLEN) */
105 Py_ssize_t len;
106 Py_ssize_t maxlen;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000107 long state; /* incremented whenever the indices move */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000108 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000109} dequeobject;
110
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000111/* The deque's size limit is d.maxlen. The limit can be zero or positive.
112 * If there is no limit, then d.maxlen == -1.
113 *
114 * After an item is added to a deque, we check to see if the size has grown past
115 * the limit. If it has, we get the size back down to the limit by popping an
116 * item off of the opposite end. The methods that can trigger this are append(),
117 * appendleft(), extend(), and extendleft().
118 */
119
120#define TRIM(d, popfunction) \
121 if (d->maxlen != -1 && d->len > d->maxlen) { \
122 PyObject *rv = popfunction(d, NULL); \
123 assert(rv != NULL && d->len <= d->maxlen); \
124 Py_DECREF(rv); \
125 }
126
Neal Norwitz87f10132004-02-29 15:40:53 +0000127static PyTypeObject deque_type;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000128
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000129static PyObject *
130deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
131{
132 dequeobject *deque;
133 block *b;
134
135 /* create dequeobject structure */
136 deque = (dequeobject *)type->tp_alloc(type, 0);
137 if (deque == NULL)
138 return NULL;
Tim Peters1065f752004-10-01 01:03:29 +0000139
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000140 b = newblock(NULL, NULL, 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000141 if (b == NULL) {
142 Py_DECREF(deque);
143 return NULL;
144 }
145
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000146 assert(BLOCKLEN >= 2);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000147 deque->leftblock = b;
148 deque->rightblock = b;
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000149 deque->leftindex = CENTER + 1;
150 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000151 deque->len = 0;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000152 deque->state = 0;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000153 deque->weakreflist = NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000154 deque->maxlen = -1;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000155
156 return (PyObject *)deque;
157}
158
159static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000160deque_pop(dequeobject *deque, PyObject *unused)
161{
162 PyObject *item;
163 block *prevblock;
164
165 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000166 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000167 return NULL;
168 }
169 item = deque->rightblock->data[deque->rightindex];
170 deque->rightindex--;
171 deque->len--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000172 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000173
174 if (deque->rightindex == -1) {
175 if (deque->len == 0) {
176 assert(deque->leftblock == deque->rightblock);
177 assert(deque->leftindex == deque->rightindex+1);
178 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000179 deque->leftindex = CENTER + 1;
180 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000181 } else {
182 prevblock = deque->rightblock->leftlink;
183 assert(deque->leftblock != deque->rightblock);
Guido van Rossum58da9312007-11-10 23:39:45 +0000184 freeblock(deque->rightblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000185 prevblock->rightlink = NULL;
186 deque->rightblock = prevblock;
187 deque->rightindex = BLOCKLEN - 1;
188 }
189 }
190 return item;
191}
192
193PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
194
195static PyObject *
196deque_popleft(dequeobject *deque, PyObject *unused)
197{
198 PyObject *item;
199 block *prevblock;
200
201 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000202 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000203 return NULL;
204 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000205 assert(deque->leftblock != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000206 item = deque->leftblock->data[deque->leftindex];
207 deque->leftindex++;
208 deque->len--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000209 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000210
211 if (deque->leftindex == BLOCKLEN) {
212 if (deque->len == 0) {
213 assert(deque->leftblock == deque->rightblock);
214 assert(deque->leftindex == deque->rightindex+1);
215 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000216 deque->leftindex = CENTER + 1;
217 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000218 } else {
219 assert(deque->leftblock != deque->rightblock);
220 prevblock = deque->leftblock->rightlink;
Guido van Rossum58da9312007-11-10 23:39:45 +0000221 freeblock(deque->leftblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000222 assert(prevblock != NULL);
223 prevblock->leftlink = NULL;
224 deque->leftblock = prevblock;
225 deque->leftindex = 0;
226 }
227 }
228 return item;
229}
230
231PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
232
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000233static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000234deque_append(dequeobject *deque, PyObject *item)
235{
236 deque->state++;
237 if (deque->rightindex == BLOCKLEN-1) {
238 block *b = newblock(deque->rightblock, NULL, deque->len);
239 if (b == NULL)
240 return NULL;
241 assert(deque->rightblock->rightlink == NULL);
242 deque->rightblock->rightlink = b;
243 deque->rightblock = b;
244 deque->rightindex = -1;
245 }
246 Py_INCREF(item);
247 deque->len++;
248 deque->rightindex++;
249 deque->rightblock->data[deque->rightindex] = item;
250 TRIM(deque, deque_popleft);
251 Py_RETURN_NONE;
252}
253
254PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
255
256static PyObject *
257deque_appendleft(dequeobject *deque, PyObject *item)
258{
259 deque->state++;
260 if (deque->leftindex == 0) {
261 block *b = newblock(NULL, deque->leftblock, deque->len);
262 if (b == NULL)
263 return NULL;
264 assert(deque->leftblock->leftlink == NULL);
265 deque->leftblock->leftlink = b;
266 deque->leftblock = b;
267 deque->leftindex = BLOCKLEN;
268 }
269 Py_INCREF(item);
270 deque->len++;
271 deque->leftindex--;
272 deque->leftblock->data[deque->leftindex] = item;
273 TRIM(deque, deque_pop);
274 Py_RETURN_NONE;
275}
276
277PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
278
279static PyObject *
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000280deque_extend(dequeobject *deque, PyObject *iterable)
281{
282 PyObject *it, *item;
283
284 it = PyObject_GetIter(iterable);
285 if (it == NULL)
286 return NULL;
287
288 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000289 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000290 if (deque->rightindex == BLOCKLEN-1) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000291 block *b = newblock(deque->rightblock, NULL,
292 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000293 if (b == NULL) {
294 Py_DECREF(item);
295 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000296 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000297 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000298 assert(deque->rightblock->rightlink == NULL);
299 deque->rightblock->rightlink = b;
300 deque->rightblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000301 deque->rightindex = -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000302 }
Armin Rigo974d7572004-10-02 13:59:34 +0000303 deque->len++;
304 deque->rightindex++;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000305 deque->rightblock->data[deque->rightindex] = item;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000306 TRIM(deque, deque_popleft);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000307 }
308 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000309 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000310 return NULL;
311 Py_RETURN_NONE;
312}
313
Tim Peters1065f752004-10-01 01:03:29 +0000314PyDoc_STRVAR(extend_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000315"Extend the right side of the deque with elements from the iterable");
316
317static PyObject *
318deque_extendleft(dequeobject *deque, PyObject *iterable)
319{
320 PyObject *it, *item;
321
322 it = PyObject_GetIter(iterable);
323 if (it == NULL)
324 return NULL;
325
326 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000327 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000328 if (deque->leftindex == 0) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000329 block *b = newblock(NULL, deque->leftblock,
330 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000331 if (b == NULL) {
332 Py_DECREF(item);
333 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000334 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000335 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000336 assert(deque->leftblock->leftlink == NULL);
337 deque->leftblock->leftlink = b;
338 deque->leftblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000339 deque->leftindex = BLOCKLEN;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000340 }
Armin Rigo974d7572004-10-02 13:59:34 +0000341 deque->len++;
342 deque->leftindex--;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000343 deque->leftblock->data[deque->leftindex] = item;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000344 TRIM(deque, deque_pop);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000345 }
346 Py_DECREF(it);
Raymond Hettingera435c532004-07-09 04:10:20 +0000347 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000348 return NULL;
349 Py_RETURN_NONE;
350}
351
Tim Peters1065f752004-10-01 01:03:29 +0000352PyDoc_STRVAR(extendleft_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000353"Extend the left side of the deque with elements from the iterable");
354
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000355static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000356_deque_rotate(dequeobject *deque, Py_ssize_t n)
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000357{
Benjamin Petersond6313712008-07-31 16:23:04 +0000358 Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000359 PyObject *item, *rv;
360
Raymond Hettingeree33b272004-02-08 04:05:26 +0000361 if (len == 0)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000362 return 0;
Raymond Hettingeree33b272004-02-08 04:05:26 +0000363 if (n > halflen || n < -halflen) {
364 n %= len;
365 if (n > halflen)
366 n -= len;
367 else if (n < -halflen)
368 n += len;
369 }
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000370
371 for (i=0 ; i<n ; i++) {
372 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000373 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000374 rv = deque_appendleft(deque, item);
375 Py_DECREF(item);
376 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000377 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000378 Py_DECREF(rv);
379 }
380 for (i=0 ; i>n ; i--) {
381 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000382 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000383 rv = deque_append(deque, item);
384 Py_DECREF(item);
385 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000386 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000387 Py_DECREF(rv);
388 }
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000389 return 0;
390}
391
392static PyObject *
393deque_rotate(dequeobject *deque, PyObject *args)
394{
Benjamin Petersond6313712008-07-31 16:23:04 +0000395 Py_ssize_t n=1;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000396
Benjamin Petersond6313712008-07-31 16:23:04 +0000397 if (!PyArg_ParseTuple(args, "|n:rotate", &n))
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000398 return NULL;
399 if (_deque_rotate(deque, n) == 0)
400 Py_RETURN_NONE;
401 return NULL;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000402}
403
Tim Peters1065f752004-10-01 01:03:29 +0000404PyDoc_STRVAR(rotate_doc,
Raymond Hettingeree33b272004-02-08 04:05:26 +0000405"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000406
Martin v. Löwis18e16552006-02-15 17:27:45 +0000407static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000408deque_len(dequeobject *deque)
409{
410 return deque->len;
411}
412
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000413static PyObject *
414deque_remove(dequeobject *deque, PyObject *value)
415{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000416 Py_ssize_t i, n=deque->len;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000417
418 for (i=0 ; i<n ; i++) {
419 PyObject *item = deque->leftblock->data[deque->leftindex];
420 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000421
422 if (deque->len != n) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000423 PyErr_SetString(PyExc_IndexError,
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000424 "deque mutated during remove().");
425 return NULL;
426 }
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000427 if (cmp > 0) {
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000428 PyObject *tgt = deque_popleft(deque, NULL);
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000429 assert (tgt != NULL);
430 Py_DECREF(tgt);
431 if (_deque_rotate(deque, i) == -1)
432 return NULL;
433 Py_RETURN_NONE;
434 }
435 else if (cmp < 0) {
436 _deque_rotate(deque, i);
437 return NULL;
438 }
439 _deque_rotate(deque, -1);
440 }
441 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
442 return NULL;
443}
444
445PyDoc_STRVAR(remove_doc,
446"D.remove(value) -- remove first occurrence of value.");
447
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000448static int
449deque_clear(dequeobject *deque)
450{
451 PyObject *item;
452
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000453 while (deque->len) {
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000454 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000455 assert (item != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000456 Py_DECREF(item);
457 }
458 assert(deque->leftblock == deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000459 deque->leftindex - 1 == deque->rightindex &&
460 deque->len == 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000461 return 0;
462}
463
464static PyObject *
Benjamin Petersond6313712008-07-31 16:23:04 +0000465deque_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000466{
467 block *b;
468 PyObject *item;
Benjamin Petersond6313712008-07-31 16:23:04 +0000469 Py_ssize_t n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000470
471 if (i < 0 || i >= deque->len) {
472 PyErr_SetString(PyExc_IndexError,
473 "deque index out of range");
474 return NULL;
475 }
476
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000477 if (i == 0) {
478 i = deque->leftindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000479 b = deque->leftblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000480 } else if (i == deque->len - 1) {
481 i = deque->rightindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000482 b = deque->rightblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000483 } else {
484 i += deque->leftindex;
485 n = i / BLOCKLEN;
486 i %= BLOCKLEN;
Armin Rigo974d7572004-10-02 13:59:34 +0000487 if (index < (deque->len >> 1)) {
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000488 b = deque->leftblock;
489 while (n--)
490 b = b->rightlink;
491 } else {
492 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
493 b = deque->rightblock;
494 while (n--)
495 b = b->leftlink;
496 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000497 }
498 item = b->data[i];
499 Py_INCREF(item);
500 return item;
501}
502
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000503/* delitem() implemented in terms of rotate for simplicity and reasonable
504 performance near the end points. If for some reason this method becomes
Tim Peters1065f752004-10-01 01:03:29 +0000505 popular, it is not hard to re-implement this using direct data movement
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000506 (similar to code in list slice assignment) and achieve a two or threefold
507 performance boost.
508*/
509
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000510static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000511deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000512{
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000513 PyObject *item;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000514
Tim Peters1065f752004-10-01 01:03:29 +0000515 assert (i >= 0 && i < deque->len);
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000516 if (_deque_rotate(deque, -i) == -1)
517 return -1;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000518
519 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000520 assert (item != NULL);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000521 Py_DECREF(item);
522
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000523 return _deque_rotate(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000524}
525
526static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000528{
529 PyObject *old_value;
530 block *b;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000531 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000532
Raymond Hettingera435c532004-07-09 04:10:20 +0000533 if (i < 0 || i >= len) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000534 PyErr_SetString(PyExc_IndexError,
535 "deque index out of range");
536 return -1;
537 }
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000538 if (v == NULL)
539 return deque_del_item(deque, i);
540
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000541 i += deque->leftindex;
542 n = i / BLOCKLEN;
543 i %= BLOCKLEN;
Raymond Hettingera435c532004-07-09 04:10:20 +0000544 if (index <= halflen) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000545 b = deque->leftblock;
546 while (n--)
547 b = b->rightlink;
548 } else {
Raymond Hettingera435c532004-07-09 04:10:20 +0000549 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000550 b = deque->rightblock;
551 while (n--)
552 b = b->leftlink;
553 }
554 Py_INCREF(v);
555 old_value = b->data[i];
556 b->data[i] = v;
557 Py_DECREF(old_value);
558 return 0;
559}
560
561static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000562deque_clearmethod(dequeobject *deque)
563{
Raymond Hettingera435c532004-07-09 04:10:20 +0000564 int rv;
565
566 rv = deque_clear(deque);
567 assert (rv != -1);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000568 Py_RETURN_NONE;
569}
570
571PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
572
573static void
574deque_dealloc(dequeobject *deque)
575{
576 PyObject_GC_UnTrack(deque);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000577 if (deque->weakreflist != NULL)
578 PyObject_ClearWeakRefs((PyObject *) deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000579 if (deque->leftblock != NULL) {
Raymond Hettingere9c89e82004-07-19 00:10:24 +0000580 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000581 assert(deque->leftblock != NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +0000582 freeblock(deque->leftblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000583 }
584 deque->leftblock = NULL;
585 deque->rightblock = NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000586 Py_TYPE(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000587}
588
589static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000590deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000591{
Tim Peters10c7e862004-10-01 02:01:04 +0000592 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000593 PyObject *item;
Benjamin Petersond6313712008-07-31 16:23:04 +0000594 Py_ssize_t index;
595 Py_ssize_t indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000596
Tim Peters10c7e862004-10-01 02:01:04 +0000597 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
Benjamin Petersond6313712008-07-31 16:23:04 +0000598 const Py_ssize_t indexhi = b == deque->rightblock ?
Tim Peters10c7e862004-10-01 02:01:04 +0000599 deque->rightindex :
600 BLOCKLEN - 1;
601
602 for (index = indexlo; index <= indexhi; ++index) {
603 item = b->data[index];
604 Py_VISIT(item);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000605 }
Tim Peters10c7e862004-10-01 02:01:04 +0000606 indexlo = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000607 }
608 return 0;
609}
610
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000611static PyObject *
612deque_copy(PyObject *deque)
613{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000614 if (((dequeobject *)deque)->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000615 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000616 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000617 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000618 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000619}
620
621PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
622
623static PyObject *
624deque_reduce(dequeobject *deque)
625{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000626 PyObject *dict, *result, *aslist;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000627
Raymond Hettinger952f8802004-11-09 07:27:35 +0000628 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000629 if (dict == NULL)
Raymond Hettinger952f8802004-11-09 07:27:35 +0000630 PyErr_Clear();
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000631 aslist = PySequence_List((PyObject *)deque);
632 if (aslist == NULL) {
633 Py_XDECREF(dict);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000634 return NULL;
635 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000636 if (dict == NULL) {
637 if (deque->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000638 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000639 else
Benjamin Petersond6313712008-07-31 16:23:04 +0000640 result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000641 } else {
642 if (deque->maxlen == -1)
Christian Heimes90aa7642007-12-19 02:45:37 +0000643 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000644 else
Benjamin Petersond6313712008-07-31 16:23:04 +0000645 result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000646 }
647 Py_XDECREF(dict);
648 Py_DECREF(aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000649 return result;
650}
651
652PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
653
654static PyObject *
655deque_repr(PyObject *deque)
656{
Walter Dörwald1ab83302007-05-18 17:15:44 +0000657 PyObject *aslist, *result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000658 int i;
659
660 i = Py_ReprEnter(deque);
661 if (i != 0) {
662 if (i < 0)
663 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000664 return PyUnicode_FromString("[...]");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000665 }
666
667 aslist = PySequence_List(deque);
668 if (aslist == NULL) {
669 Py_ReprLeave(deque);
670 return NULL;
671 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000672 if (((dequeobject *)deque)->maxlen != -1)
Benjamin Petersona786b022008-08-25 21:05:21 +0000673
674 result = PyUnicode_FromFormat("deque(%R, maxlen=%" PY_FORMAT_SIZE_T "d)",
675 aslist, ((dequeobject *)deque)->maxlen);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000676 else
677 result = PyUnicode_FromFormat("deque(%R)", aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000678 Py_DECREF(aslist);
679 Py_ReprLeave(deque);
680 return result;
681}
682
Raymond Hettinger738ec902004-02-29 02:15:56 +0000683static PyObject *
684deque_richcompare(PyObject *v, PyObject *w, int op)
685{
686 PyObject *it1=NULL, *it2=NULL, *x, *y;
Benjamin Petersond6313712008-07-31 16:23:04 +0000687 Py_ssize_t vs, ws;
688 int b, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000689
Tim Peters1065f752004-10-01 01:03:29 +0000690 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000691 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000692 Py_INCREF(Py_NotImplemented);
693 return Py_NotImplemented;
694 }
695
696 /* Shortcuts */
697 vs = ((dequeobject *)v)->len;
698 ws = ((dequeobject *)w)->len;
699 if (op == Py_EQ) {
700 if (v == w)
701 Py_RETURN_TRUE;
702 if (vs != ws)
703 Py_RETURN_FALSE;
704 }
705 if (op == Py_NE) {
706 if (v == w)
707 Py_RETURN_FALSE;
708 if (vs != ws)
709 Py_RETURN_TRUE;
710 }
711
712 /* Search for the first index where items are different */
713 it1 = PyObject_GetIter(v);
714 if (it1 == NULL)
715 goto done;
716 it2 = PyObject_GetIter(w);
717 if (it2 == NULL)
718 goto done;
Armin Rigo974d7572004-10-02 13:59:34 +0000719 for (;;) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000720 x = PyIter_Next(it1);
Armin Rigo974d7572004-10-02 13:59:34 +0000721 if (x == NULL && PyErr_Occurred())
Raymond Hettinger738ec902004-02-29 02:15:56 +0000722 goto done;
723 y = PyIter_Next(it2);
Armin Rigo974d7572004-10-02 13:59:34 +0000724 if (x == NULL || y == NULL)
725 break;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000726 b = PyObject_RichCompareBool(x, y, Py_EQ);
727 if (b == 0) {
728 cmp = PyObject_RichCompareBool(x, y, op);
729 Py_DECREF(x);
730 Py_DECREF(y);
731 goto done;
732 }
733 Py_DECREF(x);
734 Py_DECREF(y);
735 if (b == -1)
736 goto done;
737 }
Armin Rigo974d7572004-10-02 13:59:34 +0000738 /* We reached the end of one deque or both */
739 Py_XDECREF(x);
740 Py_XDECREF(y);
741 if (PyErr_Occurred())
742 goto done;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000743 switch (op) {
Armin Rigo974d7572004-10-02 13:59:34 +0000744 case Py_LT: cmp = y != NULL; break; /* if w was longer */
745 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
746 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
747 case Py_NE: cmp = x != y; break; /* if one deque continues */
748 case Py_GT: cmp = x != NULL; break; /* if v was longer */
749 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000750 }
Tim Peters1065f752004-10-01 01:03:29 +0000751
Raymond Hettinger738ec902004-02-29 02:15:56 +0000752done:
753 Py_XDECREF(it1);
754 Py_XDECREF(it2);
755 if (cmp == 1)
756 Py_RETURN_TRUE;
757 if (cmp == 0)
758 Py_RETURN_FALSE;
759 return NULL;
760}
761
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000762static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000763deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000764{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000765 PyObject *iterable = NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000766 PyObject *maxlenobj = NULL;
Benjamin Petersond6313712008-07-31 16:23:04 +0000767 Py_ssize_t maxlen = -1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000768 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000769
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000770 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000771 return -1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000772 if (maxlenobj != NULL && maxlenobj != Py_None) {
Benjamin Petersond6313712008-07-31 16:23:04 +0000773 maxlen = PyLong_AsSsize_t(maxlenobj);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000774 if (maxlen == -1 && PyErr_Occurred())
775 return -1;
776 if (maxlen < 0) {
777 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
778 return -1;
779 }
780 }
781 deque->maxlen = maxlen;
Christian Heimes38053212007-12-14 01:24:44 +0000782 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000783 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000784 PyObject *rv = deque_extend(deque, iterable);
785 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000786 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000787 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000788 }
789 return 0;
790}
791
792static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000793 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000794 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000795 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000796 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000797 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000798 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000799};
800
801/* deque object ********************************************************/
802
803static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000804static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000805PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000806 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000807
808static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000809 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000810 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000811 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000812 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000813 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000814 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000815 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000816 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000817 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000818 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000819 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000820 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000821 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000822 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000823 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000824 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000825 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000826 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000827 {"remove", (PyCFunction)deque_remove,
828 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000829 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000830 METH_NOARGS, reversed_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000831 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000832 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000833 {NULL, NULL} /* sentinel */
834};
835
836PyDoc_STRVAR(deque_doc,
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000837"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000838\n\
839Build an ordered collection accessible from endpoints only.");
840
Neal Norwitz87f10132004-02-29 15:40:53 +0000841static PyTypeObject deque_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000842 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000843 "collections.deque", /* tp_name */
844 sizeof(dequeobject), /* tp_basicsize */
845 0, /* tp_itemsize */
846 /* methods */
847 (destructor)deque_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000848 0, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000849 0, /* tp_getattr */
850 0, /* tp_setattr */
851 0, /* tp_compare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000853 0, /* tp_as_number */
854 &deque_as_sequence, /* tp_as_sequence */
855 0, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +0000856 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000857 0, /* tp_call */
858 0, /* tp_str */
859 PyObject_GenericGetAttr, /* tp_getattro */
860 0, /* tp_setattro */
861 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
863 /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000864 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000865 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000866 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000867 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000868 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000869 (getiterfunc)deque_iter, /* tp_iter */
870 0, /* tp_iternext */
871 deque_methods, /* tp_methods */
872 0, /* tp_members */
873 0, /* tp_getset */
874 0, /* tp_base */
875 0, /* tp_dict */
876 0, /* tp_descr_get */
877 0, /* tp_descr_set */
878 0, /* tp_dictoffset */
879 (initproc)deque_init, /* tp_init */
880 PyType_GenericAlloc, /* tp_alloc */
881 deque_new, /* tp_new */
882 PyObject_GC_Del, /* tp_free */
883};
884
885/*********************** Deque Iterator **************************/
886
887typedef struct {
888 PyObject_HEAD
Benjamin Petersond6313712008-07-31 16:23:04 +0000889 Py_ssize_t index;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000890 block *b;
891 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000892 long state; /* state when the iterator is created */
Benjamin Petersond6313712008-07-31 16:23:04 +0000893 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000894} dequeiterobject;
895
Martin v. Löwis59683e82008-06-13 07:50:45 +0000896static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000897
898static PyObject *
899deque_iter(dequeobject *deque)
900{
901 dequeiterobject *it;
902
903 it = PyObject_New(dequeiterobject, &dequeiter_type);
904 if (it == NULL)
905 return NULL;
906 it->b = deque->leftblock;
907 it->index = deque->leftindex;
908 Py_INCREF(deque);
909 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000910 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000911 it->counter = deque->len;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000912 return (PyObject *)it;
913}
914
915static void
916dequeiter_dealloc(dequeiterobject *dio)
917{
918 Py_XDECREF(dio->deque);
Christian Heimes90aa7642007-12-19 02:45:37 +0000919 Py_TYPE(dio)->tp_free(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000920}
921
922static PyObject *
923dequeiter_next(dequeiterobject *it)
924{
925 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000926
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000927 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000928 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000929 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000930 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000931 return NULL;
932 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000933 if (it->counter == 0)
934 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000935 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000936 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000937
938 item = it->b->data[it->index];
939 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000940 it->counter--;
941 if (it->index == BLOCKLEN && it->counter > 0) {
942 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000943 it->b = it->b->rightlink;
944 it->index = 0;
945 }
946 Py_INCREF(item);
947 return item;
948}
949
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000950static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000951dequeiter_len(dequeiterobject *it)
952{
Christian Heimes217cfd12007-12-02 14:31:20 +0000953 return PyLong_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000954}
955
Armin Rigof5b3e362006-02-11 21:32:43 +0000956PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000957
958static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000959 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000960 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000961};
962
Martin v. Löwis59683e82008-06-13 07:50:45 +0000963static PyTypeObject dequeiter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000964 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000965 "deque_iterator", /* tp_name */
966 sizeof(dequeiterobject), /* tp_basicsize */
967 0, /* tp_itemsize */
968 /* methods */
969 (destructor)dequeiter_dealloc, /* tp_dealloc */
970 0, /* tp_print */
971 0, /* tp_getattr */
972 0, /* tp_setattr */
973 0, /* tp_compare */
974 0, /* tp_repr */
975 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000976 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000977 0, /* tp_as_mapping */
978 0, /* tp_hash */
979 0, /* tp_call */
980 0, /* tp_str */
981 PyObject_GenericGetAttr, /* tp_getattro */
982 0, /* tp_setattro */
983 0, /* tp_as_buffer */
984 Py_TPFLAGS_DEFAULT, /* tp_flags */
985 0, /* tp_doc */
986 0, /* tp_traverse */
987 0, /* tp_clear */
988 0, /* tp_richcompare */
989 0, /* tp_weaklistoffset */
990 PyObject_SelfIter, /* tp_iter */
991 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000992 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000993 0,
994};
995
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000996/*********************** Deque Reverse Iterator **************************/
997
Martin v. Löwis59683e82008-06-13 07:50:45 +0000998static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000999
1000static PyObject *
1001deque_reviter(dequeobject *deque)
1002{
1003 dequeiterobject *it;
1004
1005 it = PyObject_New(dequeiterobject, &dequereviter_type);
1006 if (it == NULL)
1007 return NULL;
1008 it->b = deque->rightblock;
1009 it->index = deque->rightindex;
1010 Py_INCREF(deque);
1011 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001012 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001013 it->counter = deque->len;
1014 return (PyObject *)it;
1015}
1016
1017static PyObject *
1018dequereviter_next(dequeiterobject *it)
1019{
1020 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001021 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001022 return NULL;
1023
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001024 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001025 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001026 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001027 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001028 return NULL;
1029 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001030 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001031 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001032
1033 item = it->b->data[it->index];
1034 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001035 it->counter--;
1036 if (it->index == -1 && it->counter > 0) {
1037 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001038 it->b = it->b->leftlink;
1039 it->index = BLOCKLEN - 1;
1040 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001041 Py_INCREF(item);
1042 return item;
1043}
1044
Martin v. Löwis59683e82008-06-13 07:50:45 +00001045static PyTypeObject dequereviter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001046 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001047 "deque_reverse_iterator", /* tp_name */
1048 sizeof(dequeiterobject), /* tp_basicsize */
1049 0, /* tp_itemsize */
1050 /* methods */
1051 (destructor)dequeiter_dealloc, /* tp_dealloc */
1052 0, /* tp_print */
1053 0, /* tp_getattr */
1054 0, /* tp_setattr */
1055 0, /* tp_compare */
1056 0, /* tp_repr */
1057 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001058 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001059 0, /* tp_as_mapping */
1060 0, /* tp_hash */
1061 0, /* tp_call */
1062 0, /* tp_str */
1063 PyObject_GenericGetAttr, /* tp_getattro */
1064 0, /* tp_setattro */
1065 0, /* tp_as_buffer */
1066 Py_TPFLAGS_DEFAULT, /* tp_flags */
1067 0, /* tp_doc */
1068 0, /* tp_traverse */
1069 0, /* tp_clear */
1070 0, /* tp_richcompare */
1071 0, /* tp_weaklistoffset */
1072 PyObject_SelfIter, /* tp_iter */
1073 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001074 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001075 0,
1076};
1077
Guido van Rossum1968ad32006-02-25 22:38:04 +00001078/* defaultdict type *********************************************************/
1079
1080typedef struct {
1081 PyDictObject dict;
1082 PyObject *default_factory;
1083} defdictobject;
1084
1085static PyTypeObject defdict_type; /* Forward */
1086
1087PyDoc_STRVAR(defdict_missing_doc,
1088"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Guido van Rossumd8faa362007-04-27 19:54:29 +00001089 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001090 self[key] = value = self.default_factory()\n\
1091 return value\n\
1092");
1093
1094static PyObject *
1095defdict_missing(defdictobject *dd, PyObject *key)
1096{
1097 PyObject *factory = dd->default_factory;
1098 PyObject *value;
1099 if (factory == NULL || factory == Py_None) {
1100 /* XXX Call dict.__missing__(key) */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001101 PyObject *tup;
1102 tup = PyTuple_Pack(1, key);
1103 if (!tup) return NULL;
1104 PyErr_SetObject(PyExc_KeyError, tup);
1105 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001106 return NULL;
1107 }
1108 value = PyEval_CallObject(factory, NULL);
1109 if (value == NULL)
1110 return value;
1111 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1112 Py_DECREF(value);
1113 return NULL;
1114 }
1115 return value;
1116}
1117
1118PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1119
1120static PyObject *
1121defdict_copy(defdictobject *dd)
1122{
1123 /* This calls the object's class. That only works for subclasses
1124 whose class constructor has the same signature. Subclasses that
Christian Heimes0bd4e112008-02-12 22:59:25 +00001125 define a different constructor signature must override copy().
Guido van Rossum1968ad32006-02-25 22:38:04 +00001126 */
Christian Heimes90aa7642007-12-19 02:45:37 +00001127 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001128 dd->default_factory, dd, NULL);
1129}
1130
1131static PyObject *
1132defdict_reduce(defdictobject *dd)
1133{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001134 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001135
1136 - factory function
1137 - tuple of args for the factory function
1138 - additional state (here None)
1139 - sequence iterator (here None)
1140 - dictionary iterator (yielding successive (key, value) pairs
1141
1142 This API is used by pickle.py and copy.py.
1143
1144 For this to be useful with pickle.py, the default_factory
1145 must be picklable; e.g., None, a built-in, or a global
1146 function in a module or package.
1147
1148 Both shallow and deep copying are supported, but for deep
1149 copying, the default_factory must be deep-copyable; e.g. None,
1150 or a built-in (functions are not copyable at this time).
1151
1152 This only works for subclasses as long as their constructor
1153 signature is compatible; the first argument must be the
1154 optional default_factory, defaulting to None.
1155 */
1156 PyObject *args;
1157 PyObject *items;
1158 PyObject *result;
1159 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1160 args = PyTuple_New(0);
1161 else
1162 args = PyTuple_Pack(1, dd->default_factory);
1163 if (args == NULL)
1164 return NULL;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001165 items = PyObject_CallMethod((PyObject *)dd, "items", "()");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001166 if (items == NULL) {
1167 Py_DECREF(args);
1168 return NULL;
1169 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001170 result = PyTuple_Pack(5, Py_TYPE(dd), args,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001171 Py_None, Py_None, items);
1172 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001173 Py_DECREF(args);
1174 return result;
1175}
1176
1177static PyMethodDef defdict_methods[] = {
1178 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1179 defdict_missing_doc},
Christian Heimes3feef612008-02-11 06:19:17 +00001180 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1181 defdict_copy_doc},
Guido van Rossum1968ad32006-02-25 22:38:04 +00001182 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1183 defdict_copy_doc},
1184 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1185 reduce_doc},
1186 {NULL}
1187};
1188
1189static PyMemberDef defdict_members[] = {
1190 {"default_factory", T_OBJECT,
1191 offsetof(defdictobject, default_factory), 0,
1192 PyDoc_STR("Factory for default value called by __missing__().")},
1193 {NULL}
1194};
1195
1196static void
1197defdict_dealloc(defdictobject *dd)
1198{
1199 Py_CLEAR(dd->default_factory);
1200 PyDict_Type.tp_dealloc((PyObject *)dd);
1201}
1202
Guido van Rossum1968ad32006-02-25 22:38:04 +00001203static PyObject *
1204defdict_repr(defdictobject *dd)
1205{
Guido van Rossum1968ad32006-02-25 22:38:04 +00001206 PyObject *baserepr;
Christian Heimes77c02eb2008-02-09 02:18:51 +00001207 PyObject *defrepr;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001208 PyObject *result;
1209 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1210 if (baserepr == NULL)
1211 return NULL;
1212 if (dd->default_factory == NULL)
Christian Heimes77c02eb2008-02-09 02:18:51 +00001213 defrepr = PyUnicode_FromString("None");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001214 else
Christian Heimes77c02eb2008-02-09 02:18:51 +00001215 {
1216 int status = Py_ReprEnter(dd->default_factory);
1217 if (status != 0) {
1218 if (status < 0)
1219 return NULL;
1220 defrepr = PyUnicode_FromString("...");
1221 }
1222 else
1223 defrepr = PyObject_Repr(dd->default_factory);
1224 Py_ReprLeave(dd->default_factory);
1225 }
1226 if (defrepr == NULL) {
1227 Py_DECREF(baserepr);
1228 return NULL;
1229 }
1230 result = PyUnicode_FromFormat("defaultdict(%U, %U)",
1231 defrepr, baserepr);
1232 Py_DECREF(defrepr);
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001233 Py_DECREF(baserepr);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001234 return result;
1235}
1236
1237static int
1238defdict_traverse(PyObject *self, visitproc visit, void *arg)
1239{
1240 Py_VISIT(((defdictobject *)self)->default_factory);
1241 return PyDict_Type.tp_traverse(self, visit, arg);
1242}
1243
1244static int
1245defdict_tp_clear(defdictobject *dd)
1246{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001247 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001248 return PyDict_Type.tp_clear((PyObject *)dd);
1249}
1250
1251static int
1252defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1253{
1254 defdictobject *dd = (defdictobject *)self;
1255 PyObject *olddefault = dd->default_factory;
1256 PyObject *newdefault = NULL;
1257 PyObject *newargs;
1258 int result;
1259 if (args == NULL || !PyTuple_Check(args))
1260 newargs = PyTuple_New(0);
1261 else {
1262 Py_ssize_t n = PyTuple_GET_SIZE(args);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001263 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001264 newdefault = PyTuple_GET_ITEM(args, 0);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001265 if (!PyCallable_Check(newdefault)) {
1266 PyErr_SetString(PyExc_TypeError,
1267 "first argument must be callable");
1268 return -1;
1269 }
1270 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001271 newargs = PySequence_GetSlice(args, 1, n);
1272 }
1273 if (newargs == NULL)
1274 return -1;
1275 Py_XINCREF(newdefault);
1276 dd->default_factory = newdefault;
1277 result = PyDict_Type.tp_init(self, newargs, kwds);
1278 Py_DECREF(newargs);
1279 Py_XDECREF(olddefault);
1280 return result;
1281}
1282
1283PyDoc_STRVAR(defdict_doc,
1284"defaultdict(default_factory) --> dict with default factory\n\
1285\n\
1286The default factory is called without arguments to produce\n\
1287a new value when a key is not present, in __getitem__ only.\n\
1288A defaultdict compares equal to a dict with the same items.\n\
1289");
1290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001291/* See comment in xxsubtype.c */
1292#define DEFERRED_ADDRESS(ADDR) 0
1293
Guido van Rossum1968ad32006-02-25 22:38:04 +00001294static PyTypeObject defdict_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001295 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001296 "collections.defaultdict", /* tp_name */
1297 sizeof(defdictobject), /* tp_basicsize */
1298 0, /* tp_itemsize */
1299 /* methods */
1300 (destructor)defdict_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +00001301 0, /* tp_print */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001302 0, /* tp_getattr */
1303 0, /* tp_setattr */
1304 0, /* tp_compare */
1305 (reprfunc)defdict_repr, /* tp_repr */
1306 0, /* tp_as_number */
1307 0, /* tp_as_sequence */
1308 0, /* tp_as_mapping */
1309 0, /* tp_hash */
1310 0, /* tp_call */
1311 0, /* tp_str */
1312 PyObject_GenericGetAttr, /* tp_getattro */
1313 0, /* tp_setattro */
1314 0, /* tp_as_buffer */
Guido van Rossumd8faa362007-04-27 19:54:29 +00001315 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1316 /* tp_flags */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001317 defdict_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001318 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001319 (inquiry)defdict_tp_clear, /* tp_clear */
1320 0, /* tp_richcompare */
1321 0, /* tp_weaklistoffset*/
1322 0, /* tp_iter */
1323 0, /* tp_iternext */
1324 defdict_methods, /* tp_methods */
1325 defdict_members, /* tp_members */
1326 0, /* tp_getset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001328 0, /* tp_dict */
1329 0, /* tp_descr_get */
1330 0, /* tp_descr_set */
1331 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001332 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001333 PyType_GenericAlloc, /* tp_alloc */
1334 0, /* tp_new */
1335 PyObject_GC_Del, /* tp_free */
1336};
1337
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001338/* module level code ********************************************************/
1339
1340PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001341"High performance data structures.\n\
1342- deque: ordered collection accessible from endpoints only\n\
1343- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001344");
1345
Martin v. Löwis1a214512008-06-11 05:26:20 +00001346
1347static struct PyModuleDef _collectionsmodule = {
1348 PyModuleDef_HEAD_INIT,
1349 "_collections",
1350 module_doc,
1351 -1,
1352 NULL,
1353 NULL,
1354 NULL,
1355 NULL,
1356 NULL
1357};
1358
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001359PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001360PyInit__collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001361{
1362 PyObject *m;
1363
Martin v. Löwis1a214512008-06-11 05:26:20 +00001364 m = PyModule_Create(&_collectionsmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001365 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001366 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001367
1368 if (PyType_Ready(&deque_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001369 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001370 Py_INCREF(&deque_type);
1371 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1372
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001374 if (PyType_Ready(&defdict_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001375 return NULL;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001376 Py_INCREF(&defdict_type);
1377 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1378
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001379 if (PyType_Ready(&dequeiter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001380 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001381
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001382 if (PyType_Ready(&dequereviter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001383 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001384
Martin v. Löwis1a214512008-06-11 05:26:20 +00001385 return m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001386}