blob: 5d3fc6be27039115a46a4edc4a94fa7f1eb7cd82 [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
Raymond Hettingerd3ffd342007-11-10 01:54:03 +000054#define MAXFREEBLOCKS 10
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +000055static Py_ssize_t numfreeblocks = 0;
Raymond Hettingerd3ffd342007-11-10 01:54:03 +000056static block *freeblocks[MAXFREEBLOCKS];
57
Tim Peters6f853562004-10-01 01:04:50 +000058static block *
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +000059newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000060 block *b;
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +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
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +000066 * have PY_SSIZE_T_MAX-2 entries in total.
Raymond Hettingerc5fa9922004-10-06 17:51:54 +000067 */
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +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 }
Raymond Hettingerd3ffd342007-11-10 01:54:03 +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öwis111c1802008-06-13 07:47:47 +000088static void
Raymond Hettingerd3ffd342007-11-10 01:54:03 +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;
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +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
Raymond Hettingera7fc4b12007-10-05 02:47:07 +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;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +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);
Raymond Hettingerd3ffd342007-11-10 01:54:03 +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 }
Neal Norwitzccc56c72006-08-13 18:13:02 +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;
Raymond Hettingerd3ffd342007-11-10 01:54:03 +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 *
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000234deque_append(dequeobject *deque, PyObject *item)
235{
236 deque->state++;
237 if (deque->rightindex == BLOCKLEN-1) {
238 block *b = newblock(deque->rightblock, NULL, deque->len);
239 if (b == NULL)
240 return NULL;
241 assert(deque->rightblock->rightlink == NULL);
242 deque->rightblock->rightlink = b;
243 deque->rightblock = b;
244 deque->rightindex = -1;
245 }
246 Py_INCREF(item);
247 deque->len++;
248 deque->rightindex++;
249 deque->rightblock->data[deque->rightindex] = item;
250 TRIM(deque, deque_popleft);
251 Py_RETURN_NONE;
252}
253
254PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
255
256static PyObject *
257deque_appendleft(dequeobject *deque, PyObject *item)
258{
259 deque->state++;
260 if (deque->leftindex == 0) {
261 block *b = newblock(NULL, deque->leftblock, deque->len);
262 if (b == NULL)
263 return NULL;
264 assert(deque->leftblock->leftlink == NULL);
265 deque->leftblock->leftlink = b;
266 deque->leftblock = b;
267 deque->leftindex = BLOCKLEN;
268 }
269 Py_INCREF(item);
270 deque->len++;
271 deque->leftindex--;
272 deque->leftblock->data[deque->leftindex] = item;
273 TRIM(deque, deque_pop);
274 Py_RETURN_NONE;
275}
276
277PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
278
Raymond Hettingerbac769b2009-03-10 09:31:48 +0000279
280/* Run an iterator to exhaustion. Shortcut for
281 the extend/extendleft methods when maxlen == 0. */
282static PyObject*
283consume_iterator(PyObject *it)
284{
285 PyObject *item;
286
287 while ((item = PyIter_Next(it)) != NULL) {
288 Py_DECREF(item);
289 }
290 Py_DECREF(it);
291 if (PyErr_Occurred())
292 return NULL;
293 Py_RETURN_NONE;
294}
295
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000296static PyObject *
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000297deque_extend(dequeobject *deque, PyObject *iterable)
298{
299 PyObject *it, *item;
300
Raymond Hettinger0b3263b2009-12-10 06:00:33 +0000301 /* Handle case where id(deque) == id(iterable) */
302 if ((PyObject *)deque == iterable) {
303 PyObject *result;
304 PyObject *s = PySequence_List(iterable);
305 if (s == NULL)
306 return NULL;
307 result = deque_extend(deque, s);
308 Py_DECREF(s);
309 return result;
310 }
311
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000312 it = PyObject_GetIter(iterable);
313 if (it == NULL)
314 return NULL;
315
Raymond Hettingerbac769b2009-03-10 09:31:48 +0000316 if (deque->maxlen == 0)
317 return consume_iterator(it);
318
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000319 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000320 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000321 if (deque->rightindex == BLOCKLEN-1) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000322 block *b = newblock(deque->rightblock, NULL,
323 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000324 if (b == NULL) {
325 Py_DECREF(item);
326 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000327 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000328 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000329 assert(deque->rightblock->rightlink == NULL);
330 deque->rightblock->rightlink = b;
331 deque->rightblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000332 deque->rightindex = -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000333 }
Armin Rigo974d7572004-10-02 13:59:34 +0000334 deque->len++;
335 deque->rightindex++;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000336 deque->rightblock->data[deque->rightindex] = item;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000337 TRIM(deque, deque_popleft);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000338 }
339 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000340 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000341 return NULL;
342 Py_RETURN_NONE;
343}
344
Tim Peters1065f752004-10-01 01:03:29 +0000345PyDoc_STRVAR(extend_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000346"Extend the right side of the deque with elements from the iterable");
347
348static PyObject *
349deque_extendleft(dequeobject *deque, PyObject *iterable)
350{
351 PyObject *it, *item;
352
Raymond Hettinger0b3263b2009-12-10 06:00:33 +0000353 /* Handle case where id(deque) == id(iterable) */
354 if ((PyObject *)deque == iterable) {
355 PyObject *result;
356 PyObject *s = PySequence_List(iterable);
357 if (s == NULL)
358 return NULL;
359 result = deque_extendleft(deque, s);
360 Py_DECREF(s);
361 return result;
362 }
363
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000364 it = PyObject_GetIter(iterable);
365 if (it == NULL)
366 return NULL;
367
Raymond Hettingerbac769b2009-03-10 09:31:48 +0000368 if (deque->maxlen == 0)
369 return consume_iterator(it);
370
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000371 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000372 deque->state++;
Armin Rigo974d7572004-10-02 13:59:34 +0000373 if (deque->leftindex == 0) {
Raymond Hettingerc5fa9922004-10-06 17:51:54 +0000374 block *b = newblock(NULL, deque->leftblock,
375 deque->len);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000376 if (b == NULL) {
377 Py_DECREF(item);
378 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000379 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000380 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000381 assert(deque->leftblock->leftlink == NULL);
382 deque->leftblock->leftlink = b;
383 deque->leftblock = b;
Armin Rigo974d7572004-10-02 13:59:34 +0000384 deque->leftindex = BLOCKLEN;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000385 }
Armin Rigo974d7572004-10-02 13:59:34 +0000386 deque->len++;
387 deque->leftindex--;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000388 deque->leftblock->data[deque->leftindex] = item;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000389 TRIM(deque, deque_pop);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000390 }
391 Py_DECREF(it);
Raymond Hettingera435c532004-07-09 04:10:20 +0000392 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000393 return NULL;
394 Py_RETURN_NONE;
395}
396
Tim Peters1065f752004-10-01 01:03:29 +0000397PyDoc_STRVAR(extendleft_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000398"Extend the left side of the deque with elements from the iterable");
399
Raymond Hettinger0b3263b2009-12-10 06:00:33 +0000400static PyObject *
401deque_inplace_concat(dequeobject *deque, PyObject *other)
402{
403 PyObject *result;
404
405 result = deque_extend(deque, other);
406 if (result == NULL)
407 return result;
408 Py_DECREF(result);
409 Py_INCREF(deque);
410 return (PyObject *)deque;
411}
412
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000413static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000414_deque_rotate(dequeobject *deque, Py_ssize_t n)
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000415{
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000416 Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000417 PyObject *item, *rv;
418
Raymond Hettingeree33b272004-02-08 04:05:26 +0000419 if (len == 0)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000420 return 0;
Raymond Hettingeree33b272004-02-08 04:05:26 +0000421 if (n > halflen || n < -halflen) {
422 n %= len;
423 if (n > halflen)
424 n -= len;
425 else if (n < -halflen)
426 n += len;
427 }
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000428
429 for (i=0 ; i<n ; i++) {
430 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000431 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000432 rv = deque_appendleft(deque, item);
433 Py_DECREF(item);
434 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000435 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000436 Py_DECREF(rv);
437 }
438 for (i=0 ; i>n ; i--) {
439 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000440 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000441 rv = deque_append(deque, item);
442 Py_DECREF(item);
443 if (rv == NULL)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000444 return -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000445 Py_DECREF(rv);
446 }
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000447 return 0;
448}
449
450static PyObject *
451deque_rotate(dequeobject *deque, PyObject *args)
452{
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000453 Py_ssize_t n=1;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000454
Raymond Hettinger723ba302008-07-24 00:53:49 +0000455 if (!PyArg_ParseTuple(args, "|n:rotate", &n))
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000456 return NULL;
457 if (_deque_rotate(deque, n) == 0)
458 Py_RETURN_NONE;
459 return NULL;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000460}
461
Tim Peters1065f752004-10-01 01:03:29 +0000462PyDoc_STRVAR(rotate_doc,
Raymond Hettingeree33b272004-02-08 04:05:26 +0000463"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000464
Raymond Hettingera5fd24e2009-12-10 06:42:54 +0000465static PyObject *
466deque_reverse(dequeobject *deque, PyObject *unused)
467{
468 block *leftblock = deque->leftblock;
469 block *rightblock = deque->rightblock;
470 Py_ssize_t leftindex = deque->leftindex;
471 Py_ssize_t rightindex = deque->rightindex;
472 Py_ssize_t n = (deque->len)/2;
473 Py_ssize_t i;
474 PyObject *tmp;
475
476 for (i=0 ; i<n ; i++) {
477 /* Validate that pointers haven't met in the middle */
478 assert(leftblock != rightblock || leftindex < rightindex);
479
480 /* Swap */
481 tmp = leftblock->data[leftindex];
482 leftblock->data[leftindex] = rightblock->data[rightindex];
483 rightblock->data[rightindex] = tmp;
484
485 /* Advance left block/index pair */
486 leftindex++;
487 if (leftindex == BLOCKLEN) {
488 assert (leftblock->rightlink != NULL);
489 leftblock = leftblock->rightlink;
490 leftindex = 0;
491 }
492
493 /* Step backwards with the right block/index pair */
494 rightindex--;
495 if (rightindex == -1) {
496 assert (rightblock->leftlink != NULL);
497 rightblock = rightblock->leftlink;
498 rightindex = BLOCKLEN - 1;
499 }
500 }
501 Py_RETURN_NONE;
502}
503
504PyDoc_STRVAR(reverse_doc,
505"D.reverse() -- reverse *IN PLACE*");
506
Raymond Hettinger5f516ed2010-04-03 18:10:37 +0000507static PyObject *
508deque_count(dequeobject *deque, PyObject *v)
509{
510 block *leftblock = deque->leftblock;
511 Py_ssize_t leftindex = deque->leftindex;
512 Py_ssize_t n = (deque->len);
513 Py_ssize_t i;
514 Py_ssize_t count = 0;
515 PyObject *item;
516 long start_state = deque->state;
517 int cmp;
518
519 for (i=0 ; i<n ; i++) {
520 item = leftblock->data[leftindex];
521 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
522 if (cmp > 0)
523 count++;
524 else if (cmp < 0)
525 return NULL;
526
527 if (start_state != deque->state) {
528 PyErr_SetString(PyExc_RuntimeError,
529 "deque mutated during iteration");
530 return NULL;
531 }
532
533 /* Advance left block/index pair */
534 leftindex++;
535 if (leftindex == BLOCKLEN) {
536 assert (leftblock->rightlink != NULL);
537 leftblock = leftblock->rightlink;
538 leftindex = 0;
539 }
540 }
541 return PyInt_FromSsize_t(count);
542}
543
544PyDoc_STRVAR(count_doc,
545"D.count(value) -> integer -- return number of occurrences of value");
546
Martin v. Löwis18e16552006-02-15 17:27:45 +0000547static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000548deque_len(dequeobject *deque)
549{
550 return deque->len;
551}
552
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000553static PyObject *
554deque_remove(dequeobject *deque, PyObject *value)
555{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000556 Py_ssize_t i, n=deque->len;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000557
558 for (i=0 ; i<n ; i++) {
559 PyObject *item = deque->leftblock->data[deque->leftindex];
560 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000561
562 if (deque->len != n) {
Tim Peters5566e962006-07-28 00:23:15 +0000563 PyErr_SetString(PyExc_IndexError,
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000564 "deque mutated during remove().");
565 return NULL;
566 }
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000567 if (cmp > 0) {
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000568 PyObject *tgt = deque_popleft(deque, NULL);
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000569 assert (tgt != NULL);
570 Py_DECREF(tgt);
571 if (_deque_rotate(deque, i) == -1)
572 return NULL;
573 Py_RETURN_NONE;
574 }
575 else if (cmp < 0) {
576 _deque_rotate(deque, i);
577 return NULL;
578 }
579 _deque_rotate(deque, -1);
580 }
581 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
582 return NULL;
583}
584
585PyDoc_STRVAR(remove_doc,
586"D.remove(value) -- remove first occurrence of value.");
587
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000588static int
589deque_clear(dequeobject *deque)
590{
591 PyObject *item;
592
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000593 while (deque->len) {
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000594 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000595 assert (item != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000596 Py_DECREF(item);
597 }
598 assert(deque->leftblock == deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +0000599 deque->leftindex - 1 == deque->rightindex &&
600 deque->len == 0);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000601 return 0;
602}
603
604static PyObject *
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000605deque_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000606{
607 block *b;
608 PyObject *item;
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000609 Py_ssize_t n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000610
611 if (i < 0 || i >= deque->len) {
612 PyErr_SetString(PyExc_IndexError,
613 "deque index out of range");
614 return NULL;
615 }
616
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000617 if (i == 0) {
618 i = deque->leftindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000619 b = deque->leftblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000620 } else if (i == deque->len - 1) {
621 i = deque->rightindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000622 b = deque->rightblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000623 } else {
624 i += deque->leftindex;
625 n = i / BLOCKLEN;
626 i %= BLOCKLEN;
Armin Rigo974d7572004-10-02 13:59:34 +0000627 if (index < (deque->len >> 1)) {
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000628 b = deque->leftblock;
629 while (n--)
630 b = b->rightlink;
631 } else {
632 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
633 b = deque->rightblock;
634 while (n--)
635 b = b->leftlink;
636 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000637 }
638 item = b->data[i];
639 Py_INCREF(item);
640 return item;
641}
642
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000643/* delitem() implemented in terms of rotate for simplicity and reasonable
644 performance near the end points. If for some reason this method becomes
Tim Peters1065f752004-10-01 01:03:29 +0000645 popular, it is not hard to re-implement this using direct data movement
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000646 (similar to code in list slice assignment) and achieve a two or threefold
647 performance boost.
648*/
649
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000650static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000651deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000652{
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000653 PyObject *item;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000654
Tim Peters1065f752004-10-01 01:03:29 +0000655 assert (i >= 0 && i < deque->len);
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000656 if (_deque_rotate(deque, -i) == -1)
657 return -1;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000658
659 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000660 assert (item != NULL);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000661 Py_DECREF(item);
662
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000663 return _deque_rotate(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000664}
665
666static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000668{
669 PyObject *old_value;
670 block *b;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000671 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000672
Raymond Hettingera435c532004-07-09 04:10:20 +0000673 if (i < 0 || i >= len) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000674 PyErr_SetString(PyExc_IndexError,
675 "deque index out of range");
676 return -1;
677 }
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000678 if (v == NULL)
679 return deque_del_item(deque, i);
680
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000681 i += deque->leftindex;
682 n = i / BLOCKLEN;
683 i %= BLOCKLEN;
Raymond Hettingera435c532004-07-09 04:10:20 +0000684 if (index <= halflen) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000685 b = deque->leftblock;
686 while (n--)
687 b = b->rightlink;
688 } else {
Raymond Hettingera435c532004-07-09 04:10:20 +0000689 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000690 b = deque->rightblock;
691 while (n--)
692 b = b->leftlink;
693 }
694 Py_INCREF(v);
695 old_value = b->data[i];
696 b->data[i] = v;
697 Py_DECREF(old_value);
698 return 0;
699}
700
701static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000702deque_clearmethod(dequeobject *deque)
703{
Raymond Hettingera435c532004-07-09 04:10:20 +0000704 int rv;
705
706 rv = deque_clear(deque);
707 assert (rv != -1);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000708 Py_RETURN_NONE;
709}
710
711PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
712
713static void
714deque_dealloc(dequeobject *deque)
715{
716 PyObject_GC_UnTrack(deque);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000717 if (deque->weakreflist != NULL)
718 PyObject_ClearWeakRefs((PyObject *) deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000719 if (deque->leftblock != NULL) {
Raymond Hettingere9c89e82004-07-19 00:10:24 +0000720 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000721 assert(deque->leftblock != NULL);
Raymond Hettingerd3ffd342007-11-10 01:54:03 +0000722 freeblock(deque->leftblock);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000723 }
724 deque->leftblock = NULL;
725 deque->rightblock = NULL;
Christian Heimese93237d2007-12-19 02:37:44 +0000726 Py_TYPE(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000727}
728
729static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000730deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000731{
Tim Peters10c7e862004-10-01 02:01:04 +0000732 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000733 PyObject *item;
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000734 Py_ssize_t index;
735 Py_ssize_t indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000736
Tim Peters10c7e862004-10-01 02:01:04 +0000737 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000738 const Py_ssize_t indexhi = b == deque->rightblock ?
Tim Peters10c7e862004-10-01 02:01:04 +0000739 deque->rightindex :
740 BLOCKLEN - 1;
741
742 for (index = indexlo; index <= indexhi; ++index) {
743 item = b->data[index];
744 Py_VISIT(item);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000745 }
Tim Peters10c7e862004-10-01 02:01:04 +0000746 indexlo = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000747 }
748 return 0;
749}
750
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000751static PyObject *
752deque_copy(PyObject *deque)
753{
Raymond Hettinger68995862007-10-10 00:26:46 +0000754 if (((dequeobject *)deque)->maxlen == -1)
Christian Heimese93237d2007-12-19 02:37:44 +0000755 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
Raymond Hettinger68995862007-10-10 00:26:46 +0000756 else
Christian Heimese93237d2007-12-19 02:37:44 +0000757 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
Raymond Hettinger68995862007-10-10 00:26:46 +0000758 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000759}
760
761PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
762
763static PyObject *
764deque_reduce(dequeobject *deque)
765{
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000766 PyObject *dict, *result, *aslist;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000767
Raymond Hettinger952f8802004-11-09 07:27:35 +0000768 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000769 if (dict == NULL)
Raymond Hettinger952f8802004-11-09 07:27:35 +0000770 PyErr_Clear();
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000771 aslist = PySequence_List((PyObject *)deque);
772 if (aslist == NULL) {
Neal Norwitzc47cf7d2007-10-05 03:39:17 +0000773 Py_XDECREF(dict);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000774 return NULL;
775 }
Raymond Hettinger68995862007-10-10 00:26:46 +0000776 if (dict == NULL) {
777 if (deque->maxlen == -1)
Christian Heimese93237d2007-12-19 02:37:44 +0000778 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
Raymond Hettinger68995862007-10-10 00:26:46 +0000779 else
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000780 result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
Raymond Hettinger68995862007-10-10 00:26:46 +0000781 } else {
782 if (deque->maxlen == -1)
Christian Heimese93237d2007-12-19 02:37:44 +0000783 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
Raymond Hettinger68995862007-10-10 00:26:46 +0000784 else
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000785 result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
Raymond Hettinger68995862007-10-10 00:26:46 +0000786 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000787 Py_XDECREF(dict);
788 Py_DECREF(aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000789 return result;
790}
791
792PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
793
794static PyObject *
795deque_repr(PyObject *deque)
796{
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000797 PyObject *aslist, *result, *fmt;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000798 int i;
799
800 i = Py_ReprEnter(deque);
801 if (i != 0) {
802 if (i < 0)
803 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000804 return PyString_FromString("[...]");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000805 }
806
807 aslist = PySequence_List(deque);
808 if (aslist == NULL) {
809 Py_ReprLeave(deque);
810 return NULL;
811 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000812 if (((dequeobject *)deque)->maxlen != -1)
Amaury Forgeot d'Arc05e34492008-09-10 22:04:45 +0000813 fmt = PyString_FromFormat("deque(%%r, maxlen=%zd)",
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000814 ((dequeobject *)deque)->maxlen);
815 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000816 fmt = PyString_FromString("deque(%r)");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000817 if (fmt == NULL) {
818 Py_DECREF(aslist);
819 Py_ReprLeave(deque);
820 return NULL;
821 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000822 result = PyString_Format(fmt, aslist);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000823 Py_DECREF(fmt);
824 Py_DECREF(aslist);
825 Py_ReprLeave(deque);
826 return result;
827}
828
829static int
830deque_tp_print(PyObject *deque, FILE *fp, int flags)
831{
832 PyObject *it, *item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000833 char *emit = ""; /* No separator emitted on first pass */
834 char *separator = ", ";
835 int i;
836
837 i = Py_ReprEnter(deque);
838 if (i != 0) {
839 if (i < 0)
840 return i;
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000841 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000842 fputs("[...]", fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000843 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000844 return 0;
845 }
846
847 it = PyObject_GetIter(deque);
848 if (it == NULL)
849 return -1;
850
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000851 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000852 fputs("deque([", fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000853 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000854 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000855 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000856 fputs(emit, fp);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000857 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000858 emit = separator;
859 if (PyObject_Print(item, fp, 0) != 0) {
860 Py_DECREF(item);
861 Py_DECREF(it);
862 Py_ReprLeave(deque);
863 return -1;
864 }
865 Py_DECREF(item);
866 }
867 Py_ReprLeave(deque);
868 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000869 if (PyErr_Occurred())
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000870 return -1;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000871
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000872 Py_BEGIN_ALLOW_THREADS
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000873 if (((dequeobject *)deque)->maxlen == -1)
874 fputs("])", fp);
875 else
Christian Heimes1cc69632008-08-22 20:10:27 +0000876 fprintf(fp, "], maxlen=%" PY_FORMAT_SIZE_T "d)", ((dequeobject *)deque)->maxlen);
Raymond Hettinger556b43d2007-10-05 19:07:31 +0000877 Py_END_ALLOW_THREADS
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000878 return 0;
879}
880
Raymond Hettinger738ec902004-02-29 02:15:56 +0000881static PyObject *
882deque_richcompare(PyObject *v, PyObject *w, int op)
883{
884 PyObject *it1=NULL, *it2=NULL, *x, *y;
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000885 Py_ssize_t vs, ws;
886 int b, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000887
Tim Peters1065f752004-10-01 01:03:29 +0000888 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000889 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000890 Py_INCREF(Py_NotImplemented);
891 return Py_NotImplemented;
892 }
893
894 /* Shortcuts */
895 vs = ((dequeobject *)v)->len;
896 ws = ((dequeobject *)w)->len;
897 if (op == Py_EQ) {
898 if (v == w)
899 Py_RETURN_TRUE;
900 if (vs != ws)
901 Py_RETURN_FALSE;
902 }
903 if (op == Py_NE) {
904 if (v == w)
905 Py_RETURN_FALSE;
906 if (vs != ws)
907 Py_RETURN_TRUE;
908 }
909
910 /* Search for the first index where items are different */
911 it1 = PyObject_GetIter(v);
912 if (it1 == NULL)
913 goto done;
914 it2 = PyObject_GetIter(w);
915 if (it2 == NULL)
916 goto done;
Armin Rigo974d7572004-10-02 13:59:34 +0000917 for (;;) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000918 x = PyIter_Next(it1);
Armin Rigo974d7572004-10-02 13:59:34 +0000919 if (x == NULL && PyErr_Occurred())
Raymond Hettinger738ec902004-02-29 02:15:56 +0000920 goto done;
921 y = PyIter_Next(it2);
Armin Rigo974d7572004-10-02 13:59:34 +0000922 if (x == NULL || y == NULL)
923 break;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000924 b = PyObject_RichCompareBool(x, y, Py_EQ);
925 if (b == 0) {
926 cmp = PyObject_RichCompareBool(x, y, op);
927 Py_DECREF(x);
928 Py_DECREF(y);
929 goto done;
930 }
931 Py_DECREF(x);
932 Py_DECREF(y);
933 if (b == -1)
934 goto done;
935 }
Armin Rigo974d7572004-10-02 13:59:34 +0000936 /* We reached the end of one deque or both */
937 Py_XDECREF(x);
938 Py_XDECREF(y);
939 if (PyErr_Occurred())
940 goto done;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000941 switch (op) {
Armin Rigo974d7572004-10-02 13:59:34 +0000942 case Py_LT: cmp = y != NULL; break; /* if w was longer */
943 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
944 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
945 case Py_NE: cmp = x != y; break; /* if one deque continues */
946 case Py_GT: cmp = x != NULL; break; /* if v was longer */
947 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000948 }
Tim Peters1065f752004-10-01 01:03:29 +0000949
Raymond Hettinger738ec902004-02-29 02:15:56 +0000950done:
951 Py_XDECREF(it1);
952 Py_XDECREF(it2);
953 if (cmp == 1)
954 Py_RETURN_TRUE;
955 if (cmp == 0)
956 Py_RETURN_FALSE;
957 return NULL;
958}
959
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000960static int
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000961deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000962{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000963 PyObject *iterable = NULL;
Raymond Hettinger68995862007-10-10 00:26:46 +0000964 PyObject *maxlenobj = NULL;
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000965 Py_ssize_t maxlen = -1;
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000966 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000967
Raymond Hettinger68995862007-10-10 00:26:46 +0000968 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000969 return -1;
Raymond Hettinger68995862007-10-10 00:26:46 +0000970 if (maxlenobj != NULL && maxlenobj != Py_None) {
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +0000971 maxlen = PyInt_AsSsize_t(maxlenobj);
Raymond Hettinger68995862007-10-10 00:26:46 +0000972 if (maxlen == -1 && PyErr_Occurred())
973 return -1;
974 if (maxlen < 0) {
975 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
976 return -1;
977 }
Raymond Hettingera7fc4b12007-10-05 02:47:07 +0000978 }
979 deque->maxlen = maxlen;
Raymond Hettingeradf9ffd2007-12-13 00:08:37 +0000980 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000981 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000982 PyObject *rv = deque_extend(deque, iterable);
983 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000984 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000985 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000986 }
987 return 0;
988}
989
Raymond Hettinger56411aa2009-03-10 12:50:59 +0000990static PyObject *
991deque_get_maxlen(dequeobject *deque)
992{
993 if (deque->maxlen == -1)
994 Py_RETURN_NONE;
995 return PyInt_FromSsize_t(deque->maxlen);
996}
997
998static PyGetSetDef deque_getset[] = {
999 {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
1000 "maximum size of a deque or None if unbounded"},
1001 {0}
1002};
1003
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001004static PySequenceMethods deque_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001005 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001006 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001007 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001008 (ssizeargfunc)deque_item, /* sq_item */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001009 0, /* sq_slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001010 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger0b3263b2009-12-10 06:00:33 +00001011 0, /* sq_ass_slice */
1012 0, /* sq_contains */
1013 (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
1014 0, /* sq_inplace_repeat */
1015
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001016};
1017
1018/* deque object ********************************************************/
1019
1020static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001021static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +00001022PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001023 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001024
1025static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +00001026 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001027 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001028 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001029 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001030 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001031 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001032 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001033 METH_NOARGS, copy_doc},
Raymond Hettinger5f516ed2010-04-03 18:10:37 +00001034 {"count", (PyCFunction)deque_count,
1035 METH_O, count_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001036 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +00001037 METH_O, extend_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +00001038 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +00001039 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001040 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001041 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001042 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001043 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001044 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001045 METH_NOARGS, reduce_doc},
Raymond Hettinger4aec61e2005-03-18 21:20:23 +00001046 {"remove", (PyCFunction)deque_remove,
1047 METH_O, remove_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001048 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001049 METH_NOARGS, reversed_doc},
Raymond Hettingera5fd24e2009-12-10 06:42:54 +00001050 {"reverse", (PyCFunction)deque_reverse,
1051 METH_NOARGS, reverse_doc},
Tim Peters1065f752004-10-01 01:03:29 +00001052 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +00001053 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001054 {NULL, NULL} /* sentinel */
1055};
1056
1057PyDoc_STRVAR(deque_doc,
Raymond Hettingera7fc4b12007-10-05 02:47:07 +00001058"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001059\n\
1060Build an ordered collection accessible from endpoints only.");
1061
Neal Norwitz87f10132004-02-29 15:40:53 +00001062static PyTypeObject deque_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001063 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001064 "collections.deque", /* tp_name */
1065 sizeof(dequeobject), /* tp_basicsize */
1066 0, /* tp_itemsize */
1067 /* methods */
1068 (destructor)deque_dealloc, /* tp_dealloc */
Georg Brandld37ac692006-03-30 11:58:57 +00001069 deque_tp_print, /* tp_print */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001070 0, /* tp_getattr */
1071 0, /* tp_setattr */
1072 0, /* tp_compare */
Georg Brandld37ac692006-03-30 11:58:57 +00001073 deque_repr, /* tp_repr */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001074 0, /* tp_as_number */
1075 &deque_as_sequence, /* tp_as_sequence */
1076 0, /* tp_as_mapping */
Nick Coghlan53663a62008-07-15 14:27:37 +00001077 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001078 0, /* tp_call */
1079 0, /* tp_str */
1080 PyObject_GenericGetAttr, /* tp_getattro */
1081 0, /* tp_setattro */
1082 0, /* tp_as_buffer */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001083 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1084 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001085 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001086 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001087 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +00001088 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001089 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001090 (getiterfunc)deque_iter, /* tp_iter */
1091 0, /* tp_iternext */
1092 deque_methods, /* tp_methods */
1093 0, /* tp_members */
Raymond Hettinger56411aa2009-03-10 12:50:59 +00001094 deque_getset, /* tp_getset */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001095 0, /* tp_base */
1096 0, /* tp_dict */
1097 0, /* tp_descr_get */
1098 0, /* tp_descr_set */
1099 0, /* tp_dictoffset */
1100 (initproc)deque_init, /* tp_init */
1101 PyType_GenericAlloc, /* tp_alloc */
1102 deque_new, /* tp_new */
1103 PyObject_GC_Del, /* tp_free */
1104};
1105
1106/*********************** Deque Iterator **************************/
1107
1108typedef struct {
1109 PyObject_HEAD
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +00001110 Py_ssize_t index;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001111 block *b;
1112 dequeobject *deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001113 long state; /* state when the iterator is created */
Raymond Hettinger33fcf9d2008-07-24 00:08:18 +00001114 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001115} dequeiterobject;
1116
Martin v. Löwis111c1802008-06-13 07:47:47 +00001117static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001118
1119static PyObject *
1120deque_iter(dequeobject *deque)
1121{
1122 dequeiterobject *it;
1123
Antoine Pitrouaa687902009-01-01 14:11:22 +00001124 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001125 if (it == NULL)
1126 return NULL;
1127 it->b = deque->leftblock;
1128 it->index = deque->leftindex;
1129 Py_INCREF(deque);
1130 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001131 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001132 it->counter = deque->len;
Amaury Forgeot d'Arc57eb0e92009-01-02 00:03:54 +00001133 PyObject_GC_Track(it);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001134 return (PyObject *)it;
1135}
1136
Antoine Pitrouaa687902009-01-01 14:11:22 +00001137static int
1138dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
1139{
1140 Py_VISIT(dio->deque);
1141 return 0;
1142}
1143
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001144static void
1145dequeiter_dealloc(dequeiterobject *dio)
1146{
1147 Py_XDECREF(dio->deque);
Antoine Pitrouaa687902009-01-01 14:11:22 +00001148 PyObject_GC_Del(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001149}
1150
1151static PyObject *
1152dequeiter_next(dequeiterobject *it)
1153{
1154 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001155
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001156 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001157 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001158 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001159 "deque mutated during iteration");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001160 return NULL;
1161 }
Raymond Hettinger51c2f6c2007-01-08 18:09:20 +00001162 if (it->counter == 0)
1163 return NULL;
Tim Peters5566e962006-07-28 00:23:15 +00001164 assert (!(it->b == it->deque->rightblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001165 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001166
1167 item = it->b->data[it->index];
1168 it->index++;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001169 it->counter--;
1170 if (it->index == BLOCKLEN && it->counter > 0) {
1171 assert (it->b->rightlink != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001172 it->b = it->b->rightlink;
1173 it->index = 0;
1174 }
1175 Py_INCREF(item);
1176 return item;
1177}
1178
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001179static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001180dequeiter_len(dequeiterobject *it)
1181{
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001182 return PyInt_FromLong(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001183}
1184
Armin Rigof5b3e362006-02-11 21:32:43 +00001185PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001186
1187static PyMethodDef dequeiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00001188 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001189 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001190};
1191
Martin v. Löwis111c1802008-06-13 07:47:47 +00001192static PyTypeObject dequeiter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001193 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001194 "deque_iterator", /* tp_name */
1195 sizeof(dequeiterobject), /* tp_basicsize */
1196 0, /* tp_itemsize */
1197 /* methods */
1198 (destructor)dequeiter_dealloc, /* tp_dealloc */
1199 0, /* tp_print */
1200 0, /* tp_getattr */
1201 0, /* tp_setattr */
1202 0, /* tp_compare */
1203 0, /* tp_repr */
1204 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001205 0, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001206 0, /* tp_as_mapping */
1207 0, /* tp_hash */
1208 0, /* tp_call */
1209 0, /* tp_str */
1210 PyObject_GenericGetAttr, /* tp_getattro */
1211 0, /* tp_setattro */
1212 0, /* tp_as_buffer */
Antoine Pitrouaa687902009-01-01 14:11:22 +00001213 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001214 0, /* tp_doc */
Antoine Pitrouaa687902009-01-01 14:11:22 +00001215 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001216 0, /* tp_clear */
1217 0, /* tp_richcompare */
1218 0, /* tp_weaklistoffset */
1219 PyObject_SelfIter, /* tp_iter */
1220 (iternextfunc)dequeiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001221 dequeiter_methods, /* tp_methods */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001222 0,
1223};
1224
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001225/*********************** Deque Reverse Iterator **************************/
1226
Martin v. Löwis111c1802008-06-13 07:47:47 +00001227static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001228
1229static PyObject *
1230deque_reviter(dequeobject *deque)
1231{
1232 dequeiterobject *it;
1233
Antoine Pitrouaa687902009-01-01 14:11:22 +00001234 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001235 if (it == NULL)
1236 return NULL;
1237 it->b = deque->rightblock;
1238 it->index = deque->rightindex;
1239 Py_INCREF(deque);
1240 it->deque = deque;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001241 it->state = deque->state;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001242 it->counter = deque->len;
Amaury Forgeot d'Arc57eb0e92009-01-02 00:03:54 +00001243 PyObject_GC_Track(it);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001244 return (PyObject *)it;
1245}
1246
1247static PyObject *
1248dequereviter_next(dequeiterobject *it)
1249{
1250 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001251 if (it->counter == 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001252 return NULL;
1253
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001254 if (it->deque->state != it->state) {
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001255 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001256 PyErr_SetString(PyExc_RuntimeError,
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001257 "deque mutated during iteration");
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001258 return NULL;
1259 }
Tim Peters5566e962006-07-28 00:23:15 +00001260 assert (!(it->b == it->deque->leftblock &&
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001261 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001262
1263 item = it->b->data[it->index];
1264 it->index--;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001265 it->counter--;
1266 if (it->index == -1 && it->counter > 0) {
1267 assert (it->b->leftlink != NULL);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001268 it->b = it->b->leftlink;
1269 it->index = BLOCKLEN - 1;
1270 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001271 Py_INCREF(item);
1272 return item;
1273}
1274
Martin v. Löwis111c1802008-06-13 07:47:47 +00001275static PyTypeObject dequereviter_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001276 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001277 "deque_reverse_iterator", /* tp_name */
1278 sizeof(dequeiterobject), /* tp_basicsize */
1279 0, /* tp_itemsize */
1280 /* methods */
1281 (destructor)dequeiter_dealloc, /* tp_dealloc */
1282 0, /* tp_print */
1283 0, /* tp_getattr */
1284 0, /* tp_setattr */
1285 0, /* tp_compare */
1286 0, /* tp_repr */
1287 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001288 0, /* tp_as_sequence */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001289 0, /* tp_as_mapping */
1290 0, /* tp_hash */
1291 0, /* tp_call */
1292 0, /* tp_str */
1293 PyObject_GenericGetAttr, /* tp_getattro */
1294 0, /* tp_setattro */
1295 0, /* tp_as_buffer */
Antoine Pitrouaa687902009-01-01 14:11:22 +00001296 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001297 0, /* tp_doc */
Antoine Pitrouaa687902009-01-01 14:11:22 +00001298 (traverseproc)dequeiter_traverse, /* tp_traverse */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001299 0, /* tp_clear */
1300 0, /* tp_richcompare */
1301 0, /* tp_weaklistoffset */
1302 PyObject_SelfIter, /* tp_iter */
1303 (iternextfunc)dequereviter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001304 dequeiter_methods, /* tp_methods */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001305 0,
1306};
1307
Guido van Rossum1968ad32006-02-25 22:38:04 +00001308/* defaultdict type *********************************************************/
1309
1310typedef struct {
1311 PyDictObject dict;
1312 PyObject *default_factory;
1313} defdictobject;
1314
1315static PyTypeObject defdict_type; /* Forward */
1316
1317PyDoc_STRVAR(defdict_missing_doc,
1318"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Georg Brandlb51a57e2007-03-06 13:32:52 +00001319 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001320 self[key] = value = self.default_factory()\n\
1321 return value\n\
1322");
1323
1324static PyObject *
1325defdict_missing(defdictobject *dd, PyObject *key)
1326{
1327 PyObject *factory = dd->default_factory;
1328 PyObject *value;
1329 if (factory == NULL || factory == Py_None) {
1330 /* XXX Call dict.__missing__(key) */
Georg Brandlb51a57e2007-03-06 13:32:52 +00001331 PyObject *tup;
1332 tup = PyTuple_Pack(1, key);
1333 if (!tup) return NULL;
1334 PyErr_SetObject(PyExc_KeyError, tup);
1335 Py_DECREF(tup);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001336 return NULL;
1337 }
1338 value = PyEval_CallObject(factory, NULL);
1339 if (value == NULL)
1340 return value;
1341 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1342 Py_DECREF(value);
1343 return NULL;
1344 }
1345 return value;
1346}
1347
1348PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1349
1350static PyObject *
1351defdict_copy(defdictobject *dd)
1352{
1353 /* This calls the object's class. That only works for subclasses
1354 whose class constructor has the same signature. Subclasses that
Raymond Hettingera37430a2008-02-12 19:05:36 +00001355 define a different constructor signature must override copy().
Guido van Rossum1968ad32006-02-25 22:38:04 +00001356 */
Raymond Hettinger8fdab952009-08-04 19:08:05 +00001357
1358 if (dd->default_factory == NULL)
1359 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
Christian Heimese93237d2007-12-19 02:37:44 +00001360 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
Guido van Rossum1968ad32006-02-25 22:38:04 +00001361 dd->default_factory, dd, NULL);
1362}
1363
1364static PyObject *
1365defdict_reduce(defdictobject *dd)
1366{
Tim Peters5566e962006-07-28 00:23:15 +00001367 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001368
1369 - factory function
1370 - tuple of args for the factory function
1371 - additional state (here None)
1372 - sequence iterator (here None)
1373 - dictionary iterator (yielding successive (key, value) pairs
1374
1375 This API is used by pickle.py and copy.py.
1376
1377 For this to be useful with pickle.py, the default_factory
1378 must be picklable; e.g., None, a built-in, or a global
1379 function in a module or package.
1380
1381 Both shallow and deep copying are supported, but for deep
1382 copying, the default_factory must be deep-copyable; e.g. None,
1383 or a built-in (functions are not copyable at this time).
1384
1385 This only works for subclasses as long as their constructor
1386 signature is compatible; the first argument must be the
1387 optional default_factory, defaulting to None.
1388 */
1389 PyObject *args;
1390 PyObject *items;
1391 PyObject *result;
1392 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1393 args = PyTuple_New(0);
1394 else
1395 args = PyTuple_Pack(1, dd->default_factory);
1396 if (args == NULL)
1397 return NULL;
1398 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
1399 if (items == NULL) {
1400 Py_DECREF(args);
1401 return NULL;
1402 }
Christian Heimese93237d2007-12-19 02:37:44 +00001403 result = PyTuple_Pack(5, Py_TYPE(dd), args,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001404 Py_None, Py_None, items);
Tim Peters5566e962006-07-28 00:23:15 +00001405 Py_DECREF(items);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001406 Py_DECREF(args);
1407 return result;
1408}
1409
1410static PyMethodDef defdict_methods[] = {
1411 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1412 defdict_missing_doc},
Raymond Hettingera37430a2008-02-12 19:05:36 +00001413 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001414 defdict_copy_doc},
1415 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1416 defdict_copy_doc},
1417 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1418 reduce_doc},
1419 {NULL}
1420};
1421
1422static PyMemberDef defdict_members[] = {
1423 {"default_factory", T_OBJECT,
1424 offsetof(defdictobject, default_factory), 0,
1425 PyDoc_STR("Factory for default value called by __missing__().")},
1426 {NULL}
1427};
1428
1429static void
1430defdict_dealloc(defdictobject *dd)
1431{
1432 Py_CLEAR(dd->default_factory);
1433 PyDict_Type.tp_dealloc((PyObject *)dd);
1434}
1435
1436static int
1437defdict_print(defdictobject *dd, FILE *fp, int flags)
1438{
1439 int sts;
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001440 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001441 fprintf(fp, "defaultdict(");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001442 Py_END_ALLOW_THREADS
1443 if (dd->default_factory == NULL) {
1444 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001445 fprintf(fp, "None");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001446 Py_END_ALLOW_THREADS
1447 } else {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001448 PyObject_Print(dd->default_factory, fp, 0);
1449 }
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001450 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001451 fprintf(fp, ", ");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001452 Py_END_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001453 sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001454 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001455 fprintf(fp, ")");
Raymond Hettinger556b43d2007-10-05 19:07:31 +00001456 Py_END_ALLOW_THREADS
Guido van Rossum1968ad32006-02-25 22:38:04 +00001457 return sts;
1458}
1459
1460static PyObject *
1461defdict_repr(defdictobject *dd)
1462{
1463 PyObject *defrepr;
1464 PyObject *baserepr;
1465 PyObject *result;
1466 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1467 if (baserepr == NULL)
1468 return NULL;
1469 if (dd->default_factory == NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001470 defrepr = PyString_FromString("None");
Guido van Rossum1968ad32006-02-25 22:38:04 +00001471 else
Amaury Forgeot d'Arcb01aa432008-02-08 00:56:02 +00001472 {
1473 int status = Py_ReprEnter(dd->default_factory);
1474 if (status != 0) {
1475 if (status < 0)
1476 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001477 defrepr = PyString_FromString("...");
Amaury Forgeot d'Arcb01aa432008-02-08 00:56:02 +00001478 }
1479 else
1480 defrepr = PyObject_Repr(dd->default_factory);
1481 Py_ReprLeave(dd->default_factory);
1482 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001483 if (defrepr == NULL) {
1484 Py_DECREF(baserepr);
1485 return NULL;
1486 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001487 result = PyString_FromFormat("defaultdict(%s, %s)",
1488 PyString_AS_STRING(defrepr),
1489 PyString_AS_STRING(baserepr));
Guido van Rossum1968ad32006-02-25 22:38:04 +00001490 Py_DECREF(defrepr);
1491 Py_DECREF(baserepr);
1492 return result;
1493}
1494
1495static int
1496defdict_traverse(PyObject *self, visitproc visit, void *arg)
1497{
1498 Py_VISIT(((defdictobject *)self)->default_factory);
1499 return PyDict_Type.tp_traverse(self, visit, arg);
1500}
1501
1502static int
1503defdict_tp_clear(defdictobject *dd)
1504{
Thomas Woutersedf17d82006-04-15 17:28:34 +00001505 Py_CLEAR(dd->default_factory);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001506 return PyDict_Type.tp_clear((PyObject *)dd);
1507}
1508
1509static int
1510defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1511{
1512 defdictobject *dd = (defdictobject *)self;
1513 PyObject *olddefault = dd->default_factory;
1514 PyObject *newdefault = NULL;
1515 PyObject *newargs;
1516 int result;
1517 if (args == NULL || !PyTuple_Check(args))
1518 newargs = PyTuple_New(0);
1519 else {
1520 Py_ssize_t n = PyTuple_GET_SIZE(args);
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001521 if (n > 0) {
Guido van Rossum1968ad32006-02-25 22:38:04 +00001522 newdefault = PyTuple_GET_ITEM(args, 0);
Raymond Hettinger8fdab952009-08-04 19:08:05 +00001523 if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
Raymond Hettinger5a0217e2007-02-07 21:42:17 +00001524 PyErr_SetString(PyExc_TypeError,
1525 "first argument must be callable");
1526 return -1;
1527 }
1528 }
Guido van Rossum1968ad32006-02-25 22:38:04 +00001529 newargs = PySequence_GetSlice(args, 1, n);
1530 }
1531 if (newargs == NULL)
1532 return -1;
1533 Py_XINCREF(newdefault);
1534 dd->default_factory = newdefault;
1535 result = PyDict_Type.tp_init(self, newargs, kwds);
1536 Py_DECREF(newargs);
1537 Py_XDECREF(olddefault);
1538 return result;
1539}
1540
1541PyDoc_STRVAR(defdict_doc,
1542"defaultdict(default_factory) --> dict with default factory\n\
1543\n\
1544The default factory is called without arguments to produce\n\
1545a new value when a key is not present, in __getitem__ only.\n\
1546A defaultdict compares equal to a dict with the same items.\n\
1547");
1548
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001549/* See comment in xxsubtype.c */
1550#define DEFERRED_ADDRESS(ADDR) 0
1551
Guido van Rossum1968ad32006-02-25 22:38:04 +00001552static PyTypeObject defdict_type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001553 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum1968ad32006-02-25 22:38:04 +00001554 "collections.defaultdict", /* tp_name */
1555 sizeof(defdictobject), /* tp_basicsize */
1556 0, /* tp_itemsize */
1557 /* methods */
1558 (destructor)defdict_dealloc, /* tp_dealloc */
1559 (printfunc)defdict_print, /* tp_print */
1560 0, /* tp_getattr */
1561 0, /* tp_setattr */
1562 0, /* tp_compare */
1563 (reprfunc)defdict_repr, /* tp_repr */
1564 0, /* tp_as_number */
1565 0, /* tp_as_sequence */
1566 0, /* tp_as_mapping */
1567 0, /* tp_hash */
1568 0, /* tp_call */
1569 0, /* tp_str */
1570 PyObject_GenericGetAttr, /* tp_getattro */
1571 0, /* tp_setattro */
1572 0, /* tp_as_buffer */
1573 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1574 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
1575 defdict_doc, /* tp_doc */
Georg Brandld37ac692006-03-30 11:58:57 +00001576 defdict_traverse, /* tp_traverse */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001577 (inquiry)defdict_tp_clear, /* tp_clear */
1578 0, /* tp_richcompare */
1579 0, /* tp_weaklistoffset*/
1580 0, /* tp_iter */
1581 0, /* tp_iternext */
1582 defdict_methods, /* tp_methods */
1583 defdict_members, /* tp_members */
1584 0, /* tp_getset */
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001585 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001586 0, /* tp_dict */
1587 0, /* tp_descr_get */
1588 0, /* tp_descr_set */
1589 0, /* tp_dictoffset */
Georg Brandld37ac692006-03-30 11:58:57 +00001590 defdict_init, /* tp_init */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001591 PyType_GenericAlloc, /* tp_alloc */
1592 0, /* tp_new */
1593 PyObject_GC_Del, /* tp_free */
1594};
1595
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001596/* module level code ********************************************************/
1597
1598PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001599"High performance data structures.\n\
1600- deque: ordered collection accessible from endpoints only\n\
1601- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001602");
1603
1604PyMODINIT_FUNC
Raymond Hettingereb979882007-02-28 18:37:52 +00001605init_collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001606{
1607 PyObject *m;
1608
Raymond Hettingereb979882007-02-28 18:37:52 +00001609 m = Py_InitModule3("_collections", NULL, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001610 if (m == NULL)
1611 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001612
1613 if (PyType_Ready(&deque_type) < 0)
1614 return;
1615 Py_INCREF(&deque_type);
1616 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1617
Anthony Baxter3b8ff312006-04-04 15:05:23 +00001618 defdict_type.tp_base = &PyDict_Type;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001619 if (PyType_Ready(&defdict_type) < 0)
1620 return;
1621 Py_INCREF(&defdict_type);
1622 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1623
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001624 if (PyType_Ready(&dequeiter_type) < 0)
Tim Peters1065f752004-10-01 01:03:29 +00001625 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001626
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001627 if (PyType_Ready(&dequereviter_type) < 0)
1628 return;
1629
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001630 return;
1631}