blob: 40e253d39086f580959c662e0a6326ec12a7d00e [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 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 struct BLOCK *leftlink;
50 struct BLOCK *rightlink;
51 PyObject *data[BLOCKLEN];
Raymond Hettinger756b3f32004-01-29 06:37:52 +000052} 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) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 block *b;
61 /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
62 * 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
66 * have PY_SSIZE_T_MAX-2 entries in total.
67 */
68 if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
69 PyErr_SetString(PyExc_OverflowError,
70 "cannot add more blocks to the deque");
71 return NULL;
72 }
73 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 }
82 }
83 b->leftlink = leftlink;
84 b->rightlink = rightlink;
85 return b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +000086}
87
Martin v. Löwis59683e82008-06-13 07:50:45 +000088static void
Guido van Rossum58da9312007-11-10 23:39:45 +000089freeblock(block *b)
90{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (numfreeblocks < MAXFREEBLOCKS) {
92 freeblocks[numfreeblocks] = b;
93 numfreeblocks++;
94 } else {
95 PyMem_Free(b);
96 }
Guido van Rossum58da9312007-11-10 23:39:45 +000097}
98
Raymond Hettinger756b3f32004-01-29 06:37:52 +000099typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 PyObject_HEAD
101 block *leftblock;
102 block *rightblock;
103 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;
107 long state; /* incremented whenever the indices move */
108 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.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000114 * 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120#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); \
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000125 }
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 dequeobject *deque;
133 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 /* 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 b = newblock(NULL, NULL, 0);
141 if (b == NULL) {
142 Py_DECREF(deque);
143 return NULL;
144 }
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 assert(BLOCKLEN >= 2);
147 deque->leftblock = b;
148 deque->rightblock = b;
149 deque->leftindex = CENTER + 1;
150 deque->rightindex = CENTER;
151 deque->len = 0;
152 deque->state = 0;
153 deque->weakreflist = NULL;
154 deque->maxlen = -1;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return (PyObject *)deque;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000157}
158
159static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000160deque_pop(dequeobject *deque, PyObject *unused)
161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 PyObject *item;
163 block *prevblock;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 if (deque->len == 0) {
166 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
167 return NULL;
168 }
169 item = deque->rightblock->data[deque->rightindex];
170 deque->rightindex--;
171 deque->len--;
172 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 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 */
179 deque->leftindex = CENTER + 1;
180 deque->rightindex = CENTER;
181 } else {
182 prevblock = deque->rightblock->leftlink;
183 assert(deque->leftblock != deque->rightblock);
184 freeblock(deque->rightblock);
185 prevblock->rightlink = NULL;
186 deque->rightblock = prevblock;
187 deque->rightindex = BLOCKLEN - 1;
188 }
189 }
190 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000191}
192
193PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
194
195static PyObject *
196deque_popleft(dequeobject *deque, PyObject *unused)
197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 PyObject *item;
199 block *prevblock;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (deque->len == 0) {
202 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
203 return NULL;
204 }
205 assert(deque->leftblock != NULL);
206 item = deque->leftblock->data[deque->leftindex];
207 deque->leftindex++;
208 deque->len--;
209 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 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 */
216 deque->leftindex = CENTER + 1;
217 deque->rightindex = CENTER;
218 } else {
219 assert(deque->leftblock != deque->rightblock);
220 prevblock = deque->leftblock->rightlink;
221 freeblock(deque->leftblock);
222 assert(prevblock != NULL);
223 prevblock->leftlink = NULL;
224 deque->leftblock = prevblock;
225 deque->leftindex = 0;
226 }
227 }
228 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000229}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 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;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000252}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 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;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000275}
276
277PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
278
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280/* Run an iterator to exhaustion. Shortcut for
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000281 the extend/extendleft methods when maxlen == 0. */
282static PyObject*
283consume_iterator(PyObject *it)
284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 PyObject *item;
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 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;
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000294}
295
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000296static PyObject *
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000297deque_extend(dequeobject *deque, PyObject *iterable)
298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 PyObject *it, *item;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 }
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 it = PyObject_GetIter(iterable);
313 if (it == NULL)
314 return NULL;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (deque->maxlen == 0)
317 return consume_iterator(it);
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 while ((item = PyIter_Next(it)) != NULL) {
320 deque->state++;
321 if (deque->rightindex == BLOCKLEN-1) {
322 block *b = newblock(deque->rightblock, NULL,
323 deque->len);
324 if (b == NULL) {
325 Py_DECREF(item);
326 Py_DECREF(it);
327 return NULL;
328 }
329 assert(deque->rightblock->rightlink == NULL);
330 deque->rightblock->rightlink = b;
331 deque->rightblock = b;
332 deque->rightindex = -1;
333 }
334 deque->len++;
335 deque->rightindex++;
336 deque->rightblock->data[deque->rightindex] = item;
337 TRIM(deque, deque_popleft);
338 }
339 Py_DECREF(it);
340 if (PyErr_Occurred())
341 return NULL;
342 Py_RETURN_NONE;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000343}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 PyObject *it, *item;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 }
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 it = PyObject_GetIter(iterable);
365 if (it == NULL)
366 return NULL;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (deque->maxlen == 0)
369 return consume_iterator(it);
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 while ((item = PyIter_Next(it)) != NULL) {
372 deque->state++;
373 if (deque->leftindex == 0) {
374 block *b = newblock(NULL, deque->leftblock,
375 deque->len);
376 if (b == NULL) {
377 Py_DECREF(item);
378 Py_DECREF(it);
379 return NULL;
380 }
381 assert(deque->leftblock->leftlink == NULL);
382 deque->leftblock->leftlink = b;
383 deque->leftblock = b;
384 deque->leftindex = BLOCKLEN;
385 }
386 deque->len++;
387 deque->leftindex--;
388 deque->leftblock->data[deque->leftindex] = item;
389 TRIM(deque, deque_pop);
390 }
391 Py_DECREF(it);
392 if (PyErr_Occurred())
393 return NULL;
394 Py_RETURN_NONE;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000395}
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 Hettinger3f9afd82009-12-10 03:03:02 +0000400static PyObject *
401deque_inplace_concat(dequeobject *deque, PyObject *other)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 PyObject *result;
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 result = deque_extend(deque, other);
406 if (result == NULL)
407 return result;
408 Py_DECREF(result);
409 Py_INCREF(deque);
410 return (PyObject *)deque;
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000411}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
417 PyObject *item, *rv;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (len == 0)
420 return 0;
421 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 for (i=0 ; i<n ; i++) {
430 item = deque_pop(deque, NULL);
431 assert (item != NULL);
432 rv = deque_appendleft(deque, item);
433 Py_DECREF(item);
434 if (rv == NULL)
435 return -1;
436 Py_DECREF(rv);
437 }
438 for (i=0 ; i>n ; i--) {
439 item = deque_popleft(deque, NULL);
440 assert (item != NULL);
441 rv = deque_append(deque, item);
442 Py_DECREF(item);
443 if (rv == NULL)
444 return -1;
445 Py_DECREF(rv);
446 }
447 return 0;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000448}
449
450static PyObject *
451deque_rotate(dequeobject *deque, PyObject *args)
452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 Py_ssize_t n=1;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (!PyArg_ParseTuple(args, "|n:rotate", &n))
456 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 Hettingere5fdedb2009-12-10 00:47:21 +0000465static PyObject *
466deque_reverse(dequeobject *deque, PyObject *unused)
467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 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;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 for (i=0 ; i<n ; i++) {
477 /* Validate that pointers haven't met in the middle */
478 assert(leftblock != rightblock || leftindex < rightindex);
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 /* Swap */
481 tmp = leftblock->data[leftindex];
482 leftblock->data[leftindex] = rightblock->data[rightindex];
483 rightblock->data[rightindex] = tmp;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* Advance left block/index pair */
486 leftindex++;
487 if (leftindex == BLOCKLEN) {
Raymond Hettinger512d2cc2011-01-25 21:32:39 +0000488 if (leftblock->rightlink == NULL)
489 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 leftblock = leftblock->rightlink;
491 leftindex = 0;
492 }
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 /* Step backwards with the right block/index pair */
495 rightindex--;
496 if (rightindex == -1) {
Raymond Hettinger512d2cc2011-01-25 21:32:39 +0000497 if (rightblock->leftlink == NULL)
498 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 rightblock = rightblock->leftlink;
500 rightindex = BLOCKLEN - 1;
501 }
502 }
503 Py_RETURN_NONE;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000504}
505
506PyDoc_STRVAR(reverse_doc,
507"D.reverse() -- reverse *IN PLACE*");
508
Raymond Hettinger44459de2010-04-03 23:20:46 +0000509static PyObject *
510deque_count(dequeobject *deque, PyObject *v)
511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 block *leftblock = deque->leftblock;
513 Py_ssize_t leftindex = deque->leftindex;
Raymond Hettinger512d2cc2011-01-25 21:32:39 +0000514 Py_ssize_t n = deque->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 Py_ssize_t i;
516 Py_ssize_t count = 0;
517 PyObject *item;
518 long start_state = deque->state;
519 int cmp;
Raymond Hettinger44459de2010-04-03 23:20:46 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 for (i=0 ; i<n ; i++) {
522 item = leftblock->data[leftindex];
523 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
524 if (cmp > 0)
525 count++;
526 else if (cmp < 0)
527 return NULL;
Raymond Hettinger44459de2010-04-03 23:20:46 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (start_state != deque->state) {
530 PyErr_SetString(PyExc_RuntimeError,
531 "deque mutated during iteration");
532 return NULL;
533 }
Raymond Hettinger44459de2010-04-03 23:20:46 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 /* Advance left block/index pair */
536 leftindex++;
537 if (leftindex == BLOCKLEN) {
Raymond Hettinger512d2cc2011-01-25 21:32:39 +0000538 if (leftblock->rightlink == NULL) /* can occur when i==n-1 */
539 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 leftblock = leftblock->rightlink;
541 leftindex = 0;
542 }
543 }
544 return PyLong_FromSsize_t(count);
Raymond Hettinger44459de2010-04-03 23:20:46 +0000545}
546
547PyDoc_STRVAR(count_doc,
548"D.count(value) -> integer -- return number of occurrences of value");
549
Martin v. Löwis18e16552006-02-15 17:27:45 +0000550static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000551deque_len(dequeobject *deque)
552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return deque->len;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000554}
555
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000556static PyObject *
557deque_remove(dequeobject *deque, PyObject *value)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_ssize_t i, n=deque->len;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 for (i=0 ; i<n ; i++) {
562 PyObject *item = deque->leftblock->data[deque->leftindex];
563 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (deque->len != n) {
566 PyErr_SetString(PyExc_IndexError,
567 "deque mutated during remove().");
568 return NULL;
569 }
570 if (cmp > 0) {
571 PyObject *tgt = deque_popleft(deque, NULL);
572 assert (tgt != NULL);
573 Py_DECREF(tgt);
574 if (_deque_rotate(deque, i) == -1)
575 return NULL;
576 Py_RETURN_NONE;
577 }
578 else if (cmp < 0) {
579 _deque_rotate(deque, i);
580 return NULL;
581 }
582 _deque_rotate(deque, -1);
583 }
584 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
585 return NULL;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +0000586}
587
588PyDoc_STRVAR(remove_doc,
589"D.remove(value) -- remove first occurrence of value.");
590
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000591static int
592deque_clear(dequeobject *deque)
593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyObject *item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 while (deque->len) {
597 item = deque_pop(deque, NULL);
598 assert (item != NULL);
599 Py_DECREF(item);
600 }
601 assert(deque->leftblock == deque->rightblock &&
602 deque->leftindex - 1 == deque->rightindex &&
603 deque->len == 0);
604 return 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000605}
606
607static PyObject *
Benjamin Petersond6313712008-07-31 16:23:04 +0000608deque_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 block *b;
611 PyObject *item;
612 Py_ssize_t n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (i < 0 || i >= deque->len) {
615 PyErr_SetString(PyExc_IndexError,
616 "deque index out of range");
617 return NULL;
618 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 if (i == 0) {
621 i = deque->leftindex;
622 b = deque->leftblock;
623 } else if (i == deque->len - 1) {
624 i = deque->rightindex;
625 b = deque->rightblock;
626 } else {
627 i += deque->leftindex;
628 n = i / BLOCKLEN;
629 i %= BLOCKLEN;
630 if (index < (deque->len >> 1)) {
631 b = deque->leftblock;
632 while (n--)
633 b = b->rightlink;
634 } else {
635 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
636 b = deque->rightblock;
637 while (n--)
638 b = b->leftlink;
639 }
640 }
641 item = b->data[i];
642 Py_INCREF(item);
643 return item;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000644}
645
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000646/* delitem() implemented in terms of rotate for simplicity and reasonable
647 performance near the end points. If for some reason this method becomes
Tim Peters1065f752004-10-01 01:03:29 +0000648 popular, it is not hard to re-implement this using direct data movement
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000649 (similar to code in list slice assignment) and achieve a two or threefold
650 performance boost.
651*/
652
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000653static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000654deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 PyObject *item;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 assert (i >= 0 && i < deque->len);
659 if (_deque_rotate(deque, -i) == -1)
660 return -1;
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 item = deque_popleft(deque, NULL);
663 assert (item != NULL);
664 Py_DECREF(item);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 return _deque_rotate(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000667}
668
669static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000670deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 PyObject *old_value;
673 block *b;
674 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (i < 0 || i >= len) {
677 PyErr_SetString(PyExc_IndexError,
678 "deque index out of range");
679 return -1;
680 }
681 if (v == NULL)
682 return deque_del_item(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 i += deque->leftindex;
685 n = i / BLOCKLEN;
686 i %= BLOCKLEN;
687 if (index <= halflen) {
688 b = deque->leftblock;
689 while (n--)
690 b = b->rightlink;
691 } else {
692 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
693 b = deque->rightblock;
694 while (n--)
695 b = b->leftlink;
696 }
697 Py_INCREF(v);
698 old_value = b->data[i];
699 b->data[i] = v;
700 Py_DECREF(old_value);
701 return 0;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000702}
703
704static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000705deque_clearmethod(dequeobject *deque)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 int rv;
Raymond Hettingera435c532004-07-09 04:10:20 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 rv = deque_clear(deque);
710 assert (rv != -1);
711 Py_RETURN_NONE;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000712}
713
714PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
715
716static void
717deque_dealloc(dequeobject *deque)
718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyObject_GC_UnTrack(deque);
720 if (deque->weakreflist != NULL)
721 PyObject_ClearWeakRefs((PyObject *) deque);
722 if (deque->leftblock != NULL) {
723 deque_clear(deque);
724 assert(deque->leftblock != NULL);
725 freeblock(deque->leftblock);
726 }
727 deque->leftblock = NULL;
728 deque->rightblock = NULL;
729 Py_TYPE(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000730}
731
732static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000733deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 block *b;
736 PyObject *item;
737 Py_ssize_t index;
738 Py_ssize_t indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
741 const Py_ssize_t indexhi = b == deque->rightblock ?
742 deque->rightindex :
743 BLOCKLEN - 1;
Tim Peters10c7e862004-10-01 02:01:04 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 for (index = indexlo; index <= indexhi; ++index) {
746 item = b->data[index];
747 Py_VISIT(item);
748 }
749 indexlo = 0;
750 }
751 return 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000752}
753
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000754static PyObject *
755deque_copy(PyObject *deque)
756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (((dequeobject *)deque)->maxlen == -1)
758 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
759 else
760 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
761 deque, ((dequeobject *)deque)->maxlen, NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000762}
763
764PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
765
766static PyObject *
767deque_reduce(dequeobject *deque)
768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject *dict, *result, *aslist;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
772 if (dict == NULL)
773 PyErr_Clear();
774 aslist = PySequence_List((PyObject *)deque);
775 if (aslist == NULL) {
776 Py_XDECREF(dict);
777 return NULL;
778 }
779 if (dict == NULL) {
780 if (deque->maxlen == -1)
781 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
782 else
783 result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
784 } else {
785 if (deque->maxlen == -1)
786 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
787 else
788 result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
789 }
790 Py_XDECREF(dict);
791 Py_DECREF(aslist);
792 return result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000793}
794
795PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
796
797static PyObject *
798deque_repr(PyObject *deque)
799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject *aslist, *result;
801 int i;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 i = Py_ReprEnter(deque);
804 if (i != 0) {
805 if (i < 0)
806 return NULL;
807 return PyUnicode_FromString("[...]");
808 }
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 aslist = PySequence_List(deque);
811 if (aslist == NULL) {
812 Py_ReprLeave(deque);
813 return NULL;
814 }
815 if (((dequeobject *)deque)->maxlen != -1)
Benjamin Petersona786b022008-08-25 21:05:21 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
818 aslist, ((dequeobject *)deque)->maxlen);
819 else
820 result = PyUnicode_FromFormat("deque(%R)", aslist);
821 Py_DECREF(aslist);
822 Py_ReprLeave(deque);
823 return result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000824}
825
Raymond Hettinger738ec902004-02-29 02:15:56 +0000826static PyObject *
827deque_richcompare(PyObject *v, PyObject *w, int op)
828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyObject *it1=NULL, *it2=NULL, *x, *y;
830 Py_ssize_t vs, ws;
831 int b, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 if (!PyObject_TypeCheck(v, &deque_type) ||
834 !PyObject_TypeCheck(w, &deque_type)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500835 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 }
Raymond Hettinger738ec902004-02-29 02:15:56 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 /* Shortcuts */
839 vs = ((dequeobject *)v)->len;
840 ws = ((dequeobject *)w)->len;
841 if (op == Py_EQ) {
842 if (v == w)
843 Py_RETURN_TRUE;
844 if (vs != ws)
845 Py_RETURN_FALSE;
846 }
847 if (op == Py_NE) {
848 if (v == w)
849 Py_RETURN_FALSE;
850 if (vs != ws)
851 Py_RETURN_TRUE;
852 }
Raymond Hettinger738ec902004-02-29 02:15:56 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 /* Search for the first index where items are different */
855 it1 = PyObject_GetIter(v);
856 if (it1 == NULL)
857 goto done;
858 it2 = PyObject_GetIter(w);
859 if (it2 == NULL)
860 goto done;
861 for (;;) {
862 x = PyIter_Next(it1);
863 if (x == NULL && PyErr_Occurred())
864 goto done;
865 y = PyIter_Next(it2);
866 if (x == NULL || y == NULL)
867 break;
868 b = PyObject_RichCompareBool(x, y, Py_EQ);
869 if (b == 0) {
870 cmp = PyObject_RichCompareBool(x, y, op);
871 Py_DECREF(x);
872 Py_DECREF(y);
873 goto done;
874 }
875 Py_DECREF(x);
876 Py_DECREF(y);
877 if (b == -1)
878 goto done;
879 }
880 /* We reached the end of one deque or both */
881 Py_XDECREF(x);
882 Py_XDECREF(y);
883 if (PyErr_Occurred())
884 goto done;
885 switch (op) {
886 case Py_LT: cmp = y != NULL; break; /* if w was longer */
887 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
888 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
889 case Py_NE: cmp = x != y; break; /* if one deque continues */
890 case Py_GT: cmp = x != NULL; break; /* if v was longer */
891 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
892 }
Tim Peters1065f752004-10-01 01:03:29 +0000893
Raymond Hettinger738ec902004-02-29 02:15:56 +0000894done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_XDECREF(it1);
896 Py_XDECREF(it2);
897 if (cmp == 1)
898 Py_RETURN_TRUE;
899 if (cmp == 0)
900 Py_RETURN_FALSE;
901 return NULL;
Raymond Hettinger738ec902004-02-29 02:15:56 +0000902}
903
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000904static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000905deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyObject *iterable = NULL;
908 PyObject *maxlenobj = NULL;
909 Py_ssize_t maxlen = -1;
910 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
913 return -1;
914 if (maxlenobj != NULL && maxlenobj != Py_None) {
915 maxlen = PyLong_AsSsize_t(maxlenobj);
916 if (maxlen == -1 && PyErr_Occurred())
917 return -1;
918 if (maxlen < 0) {
919 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
920 return -1;
921 }
922 }
923 deque->maxlen = maxlen;
924 deque_clear(deque);
925 if (iterable != NULL) {
926 PyObject *rv = deque_extend(deque, iterable);
927 if (rv == NULL)
928 return -1;
929 Py_DECREF(rv);
930 }
931 return 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000932}
933
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000934static PyObject *
935deque_get_maxlen(dequeobject *deque)
936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 if (deque->maxlen == -1)
938 Py_RETURN_NONE;
939 return PyLong_FromSsize_t(deque->maxlen);
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000940}
941
942static PyGetSetDef deque_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
944 "maximum size of a deque or None if unbounded"},
945 {0}
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000946};
947
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000948static PySequenceMethods deque_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 (lenfunc)deque_len, /* sq_length */
950 0, /* sq_concat */
951 0, /* sq_repeat */
952 (ssizeargfunc)deque_item, /* sq_item */
953 0, /* sq_slice */
954 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
955 0, /* sq_ass_slice */
956 0, /* sq_contains */
957 (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
958 0, /* sq_inplace_repeat */
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000959
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000960};
961
962/* deque object ********************************************************/
963
964static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000965static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000966PyDoc_STRVAR(reversed_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000968
969static PyMethodDef deque_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 {"append", (PyCFunction)deque_append,
971 METH_O, append_doc},
972 {"appendleft", (PyCFunction)deque_appendleft,
973 METH_O, appendleft_doc},
974 {"clear", (PyCFunction)deque_clearmethod,
975 METH_NOARGS, clear_doc},
976 {"__copy__", (PyCFunction)deque_copy,
977 METH_NOARGS, copy_doc},
978 {"count", (PyCFunction)deque_count,
979 METH_O, count_doc},
980 {"extend", (PyCFunction)deque_extend,
981 METH_O, extend_doc},
982 {"extendleft", (PyCFunction)deque_extendleft,
983 METH_O, extendleft_doc},
984 {"pop", (PyCFunction)deque_pop,
985 METH_NOARGS, pop_doc},
986 {"popleft", (PyCFunction)deque_popleft,
987 METH_NOARGS, popleft_doc},
988 {"__reduce__", (PyCFunction)deque_reduce,
989 METH_NOARGS, reduce_doc},
990 {"remove", (PyCFunction)deque_remove,
991 METH_O, remove_doc},
992 {"__reversed__", (PyCFunction)deque_reviter,
993 METH_NOARGS, reversed_doc},
994 {"reverse", (PyCFunction)deque_reverse,
995 METH_NOARGS, reverse_doc},
996 {"rotate", (PyCFunction)deque_rotate,
997 METH_VARARGS, rotate_doc},
998 {NULL, NULL} /* sentinel */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000999};
1000
1001PyDoc_STRVAR(deque_doc,
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001002"deque(iterable[, maxlen]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001003\n\
Raymond Hettinger49747052011-03-29 17:36:31 -07001004Build an ordered collection with optimized access from its endpoints.");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001005
Neal Norwitz87f10132004-02-29 15:40:53 +00001006static PyTypeObject deque_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 PyVarObject_HEAD_INIT(NULL, 0)
1008 "collections.deque", /* tp_name */
1009 sizeof(dequeobject), /* tp_basicsize */
1010 0, /* tp_itemsize */
1011 /* methods */
1012 (destructor)deque_dealloc, /* tp_dealloc */
1013 0, /* tp_print */
1014 0, /* tp_getattr */
1015 0, /* tp_setattr */
1016 0, /* tp_reserved */
1017 deque_repr, /* tp_repr */
1018 0, /* tp_as_number */
1019 &deque_as_sequence, /* tp_as_sequence */
1020 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00001021 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 0, /* tp_call */
1023 0, /* tp_str */
1024 PyObject_GenericGetAttr, /* tp_getattro */
1025 0, /* tp_setattro */
1026 0, /* tp_as_buffer */
1027 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandlf038b322010-10-18 07:35:09 +00001028 /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 deque_doc, /* tp_doc */
1030 (traverseproc)deque_traverse, /* tp_traverse */
1031 (inquiry)deque_clear, /* tp_clear */
1032 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Georg Brandlf038b322010-10-18 07:35:09 +00001033 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 (getiterfunc)deque_iter, /* tp_iter */
1035 0, /* tp_iternext */
1036 deque_methods, /* tp_methods */
1037 0, /* tp_members */
Georg Brandlf038b322010-10-18 07:35:09 +00001038 deque_getset, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 0, /* tp_base */
1040 0, /* tp_dict */
1041 0, /* tp_descr_get */
1042 0, /* tp_descr_set */
1043 0, /* tp_dictoffset */
1044 (initproc)deque_init, /* tp_init */
1045 PyType_GenericAlloc, /* tp_alloc */
1046 deque_new, /* tp_new */
1047 PyObject_GC_Del, /* tp_free */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001048};
1049
1050/*********************** Deque Iterator **************************/
1051
1052typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 PyObject_HEAD
1054 Py_ssize_t index;
1055 block *b;
1056 dequeobject *deque;
1057 long state; /* state when the iterator is created */
1058 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001059} dequeiterobject;
1060
Martin v. Löwis59683e82008-06-13 07:50:45 +00001061static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001062
1063static PyObject *
1064deque_iter(dequeobject *deque)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 dequeiterobject *it;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
1069 if (it == NULL)
1070 return NULL;
1071 it->b = deque->leftblock;
1072 it->index = deque->leftindex;
1073 Py_INCREF(deque);
1074 it->deque = deque;
1075 it->state = deque->state;
1076 it->counter = deque->len;
1077 PyObject_GC_Track(it);
1078 return (PyObject *)it;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001079}
1080
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001081static int
1082dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
1083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 Py_VISIT(dio->deque);
1085 return 0;
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001086}
1087
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001088static void
1089dequeiter_dealloc(dequeiterobject *dio)
1090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 Py_XDECREF(dio->deque);
1092 PyObject_GC_Del(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001093}
1094
1095static PyObject *
1096dequeiter_next(dequeiterobject *it)
1097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (it->deque->state != it->state) {
1101 it->counter = 0;
1102 PyErr_SetString(PyExc_RuntimeError,
1103 "deque mutated during iteration");
1104 return NULL;
1105 }
1106 if (it->counter == 0)
1107 return NULL;
1108 assert (!(it->b == it->deque->rightblock &&
1109 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 item = it->b->data[it->index];
1112 it->index++;
1113 it->counter--;
1114 if (it->index == BLOCKLEN && it->counter > 0) {
1115 assert (it->b->rightlink != NULL);
1116 it->b = it->b->rightlink;
1117 it->index = 0;
1118 }
1119 Py_INCREF(item);
1120 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001121}
1122
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001123static PyObject *
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001124dequeiter_len(dequeiterobject *it)
1125{
Antoine Pitrou554f3342010-08-17 18:30:06 +00001126 return PyLong_FromSsize_t(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001127}
1128
Armin Rigof5b3e362006-02-11 21:32:43 +00001129PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001130
1131static PyMethodDef dequeiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
1133 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001134};
1135
Martin v. Löwis59683e82008-06-13 07:50:45 +00001136static PyTypeObject dequeiter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 PyVarObject_HEAD_INIT(NULL, 0)
1138 "deque_iterator", /* tp_name */
1139 sizeof(dequeiterobject), /* tp_basicsize */
1140 0, /* tp_itemsize */
1141 /* methods */
1142 (destructor)dequeiter_dealloc, /* tp_dealloc */
1143 0, /* tp_print */
1144 0, /* tp_getattr */
1145 0, /* tp_setattr */
1146 0, /* tp_reserved */
1147 0, /* tp_repr */
1148 0, /* tp_as_number */
1149 0, /* tp_as_sequence */
1150 0, /* tp_as_mapping */
1151 0, /* tp_hash */
1152 0, /* tp_call */
1153 0, /* tp_str */
1154 PyObject_GenericGetAttr, /* tp_getattro */
1155 0, /* tp_setattro */
1156 0, /* tp_as_buffer */
1157 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1158 0, /* tp_doc */
1159 (traverseproc)dequeiter_traverse, /* tp_traverse */
1160 0, /* tp_clear */
1161 0, /* tp_richcompare */
1162 0, /* tp_weaklistoffset */
1163 PyObject_SelfIter, /* tp_iter */
1164 (iternextfunc)dequeiter_next, /* tp_iternext */
1165 dequeiter_methods, /* tp_methods */
1166 0,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001167};
1168
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001169/*********************** Deque Reverse Iterator **************************/
1170
Martin v. Löwis59683e82008-06-13 07:50:45 +00001171static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001172
1173static PyObject *
1174deque_reviter(dequeobject *deque)
1175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 dequeiterobject *it;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
1179 if (it == NULL)
1180 return NULL;
1181 it->b = deque->rightblock;
1182 it->index = deque->rightindex;
1183 Py_INCREF(deque);
1184 it->deque = deque;
1185 it->state = deque->state;
1186 it->counter = deque->len;
1187 PyObject_GC_Track(it);
1188 return (PyObject *)it;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001189}
1190
1191static PyObject *
1192dequereviter_next(dequeiterobject *it)
1193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 PyObject *item;
1195 if (it->counter == 0)
1196 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 if (it->deque->state != it->state) {
1199 it->counter = 0;
1200 PyErr_SetString(PyExc_RuntimeError,
1201 "deque mutated during iteration");
1202 return NULL;
1203 }
1204 assert (!(it->b == it->deque->leftblock &&
1205 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 item = it->b->data[it->index];
1208 it->index--;
1209 it->counter--;
1210 if (it->index == -1 && it->counter > 0) {
1211 assert (it->b->leftlink != NULL);
1212 it->b = it->b->leftlink;
1213 it->index = BLOCKLEN - 1;
1214 }
1215 Py_INCREF(item);
1216 return item;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001217}
1218
Martin v. Löwis59683e82008-06-13 07:50:45 +00001219static PyTypeObject dequereviter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PyVarObject_HEAD_INIT(NULL, 0)
1221 "deque_reverse_iterator", /* tp_name */
1222 sizeof(dequeiterobject), /* tp_basicsize */
1223 0, /* tp_itemsize */
1224 /* methods */
1225 (destructor)dequeiter_dealloc, /* tp_dealloc */
1226 0, /* tp_print */
1227 0, /* tp_getattr */
1228 0, /* tp_setattr */
1229 0, /* tp_reserved */
1230 0, /* tp_repr */
1231 0, /* tp_as_number */
1232 0, /* tp_as_sequence */
1233 0, /* tp_as_mapping */
1234 0, /* tp_hash */
1235 0, /* tp_call */
1236 0, /* tp_str */
1237 PyObject_GenericGetAttr, /* tp_getattro */
1238 0, /* tp_setattro */
1239 0, /* tp_as_buffer */
1240 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1241 0, /* tp_doc */
1242 (traverseproc)dequeiter_traverse, /* tp_traverse */
1243 0, /* tp_clear */
1244 0, /* tp_richcompare */
1245 0, /* tp_weaklistoffset */
1246 PyObject_SelfIter, /* tp_iter */
1247 (iternextfunc)dequereviter_next, /* tp_iternext */
1248 dequeiter_methods, /* tp_methods */
1249 0,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001250};
1251
Guido van Rossum1968ad32006-02-25 22:38:04 +00001252/* defaultdict type *********************************************************/
1253
1254typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 PyDictObject dict;
1256 PyObject *default_factory;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001257} defdictobject;
1258
1259static PyTypeObject defdict_type; /* Forward */
1260
1261PyDoc_STRVAR(defdict_missing_doc,
1262"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Guido van Rossumd8faa362007-04-27 19:54:29 +00001263 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001264 self[key] = value = self.default_factory()\n\
1265 return value\n\
1266");
1267
1268static PyObject *
1269defdict_missing(defdictobject *dd, PyObject *key)
1270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 PyObject *factory = dd->default_factory;
1272 PyObject *value;
1273 if (factory == NULL || factory == Py_None) {
1274 /* XXX Call dict.__missing__(key) */
1275 PyObject *tup;
1276 tup = PyTuple_Pack(1, key);
1277 if (!tup) return NULL;
1278 PyErr_SetObject(PyExc_KeyError, tup);
1279 Py_DECREF(tup);
1280 return NULL;
1281 }
1282 value = PyEval_CallObject(factory, NULL);
1283 if (value == NULL)
1284 return value;
1285 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1286 Py_DECREF(value);
1287 return NULL;
1288 }
1289 return value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001290}
1291
1292PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1293
1294static PyObject *
1295defdict_copy(defdictobject *dd)
1296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 /* This calls the object's class. That only works for subclasses
1298 whose class constructor has the same signature. Subclasses that
1299 define a different constructor signature must override copy().
1300 */
Raymond Hettinger54628fa2009-08-04 19:16:39 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 if (dd->default_factory == NULL)
1303 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
1304 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
1305 dd->default_factory, dd, NULL);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001306}
1307
1308static PyObject *
1309defdict_reduce(defdictobject *dd)
1310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 - factory function
1314 - tuple of args for the factory function
1315 - additional state (here None)
1316 - sequence iterator (here None)
1317 - dictionary iterator (yielding successive (key, value) pairs
Guido van Rossum1968ad32006-02-25 22:38:04 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 This API is used by pickle.py and copy.py.
Guido van Rossum1968ad32006-02-25 22:38:04 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 For this to be useful with pickle.py, the default_factory
1322 must be picklable; e.g., None, a built-in, or a global
1323 function in a module or package.
Guido van Rossum1968ad32006-02-25 22:38:04 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 Both shallow and deep copying are supported, but for deep
1326 copying, the default_factory must be deep-copyable; e.g. None,
1327 or a built-in (functions are not copyable at this time).
Guido van Rossum1968ad32006-02-25 22:38:04 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 This only works for subclasses as long as their constructor
1330 signature is compatible; the first argument must be the
1331 optional default_factory, defaulting to None.
1332 */
1333 PyObject *args;
1334 PyObject *items;
1335 PyObject *iter;
1336 PyObject *result;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001337 _Py_identifier(items);
1338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (dd->default_factory == NULL || dd->default_factory == Py_None)
1340 args = PyTuple_New(0);
1341 else
1342 args = PyTuple_Pack(1, dd->default_factory);
1343 if (args == NULL)
1344 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001345 items = _PyObject_CallMethodId((PyObject *)dd, &PyId_items, "()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (items == NULL) {
1347 Py_DECREF(args);
1348 return NULL;
1349 }
1350 iter = PyObject_GetIter(items);
1351 if (iter == NULL) {
1352 Py_DECREF(items);
1353 Py_DECREF(args);
1354 return NULL;
1355 }
1356 result = PyTuple_Pack(5, Py_TYPE(dd), args,
1357 Py_None, Py_None, iter);
1358 Py_DECREF(iter);
1359 Py_DECREF(items);
1360 Py_DECREF(args);
1361 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001362}
1363
1364static PyMethodDef defdict_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 {"__missing__", (PyCFunction)defdict_missing, METH_O,
1366 defdict_missing_doc},
1367 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
1368 defdict_copy_doc},
1369 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
1370 defdict_copy_doc},
1371 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
1372 reduce_doc},
1373 {NULL}
Guido van Rossum1968ad32006-02-25 22:38:04 +00001374};
1375
1376static PyMemberDef defdict_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 {"default_factory", T_OBJECT,
1378 offsetof(defdictobject, default_factory), 0,
1379 PyDoc_STR("Factory for default value called by __missing__().")},
1380 {NULL}
Guido van Rossum1968ad32006-02-25 22:38:04 +00001381};
1382
1383static void
1384defdict_dealloc(defdictobject *dd)
1385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 Py_CLEAR(dd->default_factory);
1387 PyDict_Type.tp_dealloc((PyObject *)dd);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001388}
1389
Guido van Rossum1968ad32006-02-25 22:38:04 +00001390static PyObject *
1391defdict_repr(defdictobject *dd)
1392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 PyObject *baserepr;
1394 PyObject *defrepr;
1395 PyObject *result;
1396 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1397 if (baserepr == NULL)
1398 return NULL;
1399 if (dd->default_factory == NULL)
1400 defrepr = PyUnicode_FromString("None");
1401 else
1402 {
1403 int status = Py_ReprEnter(dd->default_factory);
1404 if (status != 0) {
1405 if (status < 0)
1406 return NULL;
1407 defrepr = PyUnicode_FromString("...");
1408 }
1409 else
1410 defrepr = PyObject_Repr(dd->default_factory);
1411 Py_ReprLeave(dd->default_factory);
1412 }
1413 if (defrepr == NULL) {
1414 Py_DECREF(baserepr);
1415 return NULL;
1416 }
1417 result = PyUnicode_FromFormat("defaultdict(%U, %U)",
1418 defrepr, baserepr);
1419 Py_DECREF(defrepr);
1420 Py_DECREF(baserepr);
1421 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001422}
1423
1424static int
1425defdict_traverse(PyObject *self, visitproc visit, void *arg)
1426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 Py_VISIT(((defdictobject *)self)->default_factory);
1428 return PyDict_Type.tp_traverse(self, visit, arg);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001429}
1430
1431static int
1432defdict_tp_clear(defdictobject *dd)
1433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 Py_CLEAR(dd->default_factory);
1435 return PyDict_Type.tp_clear((PyObject *)dd);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001436}
1437
1438static int
1439defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 defdictobject *dd = (defdictobject *)self;
1442 PyObject *olddefault = dd->default_factory;
1443 PyObject *newdefault = NULL;
1444 PyObject *newargs;
1445 int result;
1446 if (args == NULL || !PyTuple_Check(args))
1447 newargs = PyTuple_New(0);
1448 else {
1449 Py_ssize_t n = PyTuple_GET_SIZE(args);
1450 if (n > 0) {
1451 newdefault = PyTuple_GET_ITEM(args, 0);
1452 if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
1453 PyErr_SetString(PyExc_TypeError,
1454 "first argument must be callable");
1455 return -1;
1456 }
1457 }
1458 newargs = PySequence_GetSlice(args, 1, n);
1459 }
1460 if (newargs == NULL)
1461 return -1;
1462 Py_XINCREF(newdefault);
1463 dd->default_factory = newdefault;
1464 result = PyDict_Type.tp_init(self, newargs, kwds);
1465 Py_DECREF(newargs);
1466 Py_XDECREF(olddefault);
1467 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001468}
1469
1470PyDoc_STRVAR(defdict_doc,
1471"defaultdict(default_factory) --> dict with default factory\n\
1472\n\
1473The default factory is called without arguments to produce\n\
1474a new value when a key is not present, in __getitem__ only.\n\
1475A defaultdict compares equal to a dict with the same items.\n\
1476");
1477
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001478/* See comment in xxsubtype.c */
1479#define DEFERRED_ADDRESS(ADDR) 0
1480
Guido van Rossum1968ad32006-02-25 22:38:04 +00001481static PyTypeObject defdict_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
1483 "collections.defaultdict", /* tp_name */
1484 sizeof(defdictobject), /* tp_basicsize */
1485 0, /* tp_itemsize */
1486 /* methods */
1487 (destructor)defdict_dealloc, /* tp_dealloc */
1488 0, /* tp_print */
1489 0, /* tp_getattr */
1490 0, /* tp_setattr */
1491 0, /* tp_reserved */
1492 (reprfunc)defdict_repr, /* tp_repr */
1493 0, /* tp_as_number */
1494 0, /* tp_as_sequence */
1495 0, /* tp_as_mapping */
1496 0, /* tp_hash */
1497 0, /* tp_call */
1498 0, /* tp_str */
1499 PyObject_GenericGetAttr, /* tp_getattro */
1500 0, /* tp_setattro */
1501 0, /* tp_as_buffer */
1502 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1503 /* tp_flags */
1504 defdict_doc, /* tp_doc */
1505 defdict_traverse, /* tp_traverse */
1506 (inquiry)defdict_tp_clear, /* tp_clear */
1507 0, /* tp_richcompare */
1508 0, /* tp_weaklistoffset*/
1509 0, /* tp_iter */
1510 0, /* tp_iternext */
1511 defdict_methods, /* tp_methods */
1512 defdict_members, /* tp_members */
1513 0, /* tp_getset */
1514 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
1515 0, /* tp_dict */
1516 0, /* tp_descr_get */
1517 0, /* tp_descr_set */
1518 0, /* tp_dictoffset */
1519 defdict_init, /* tp_init */
1520 PyType_GenericAlloc, /* tp_alloc */
1521 0, /* tp_new */
1522 PyObject_GC_Del, /* tp_free */
Guido van Rossum1968ad32006-02-25 22:38:04 +00001523};
1524
Raymond Hettinger96f34102010-12-15 16:30:37 +00001525/* helper function for Counter *********************************************/
1526
1527PyDoc_STRVAR(_count_elements_doc,
1528"_count_elements(mapping, iterable) -> None\n\
1529\n\
1530Count elements in the iterable, updating the mappping");
1531
1532static PyObject *
1533_count_elements(PyObject *self, PyObject *args)
1534{
1535 PyObject *it, *iterable, *mapping, *oldval;
1536 PyObject *newval = NULL;
1537 PyObject *key = NULL;
1538 PyObject *one = NULL;
1539
1540 if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable))
1541 return NULL;
1542
Raymond Hettinger96f34102010-12-15 16:30:37 +00001543 it = PyObject_GetIter(iterable);
1544 if (it == NULL)
1545 return NULL;
Raymond Hettinger426e0522011-01-03 02:12:02 +00001546
Raymond Hettinger96f34102010-12-15 16:30:37 +00001547 one = PyLong_FromLong(1);
1548 if (one == NULL) {
1549 Py_DECREF(it);
1550 return NULL;
1551 }
Raymond Hettinger426e0522011-01-03 02:12:02 +00001552
1553 if (PyDict_CheckExact(mapping)) {
1554 while (1) {
1555 key = PyIter_Next(it);
Victor Stinnera154b5c2011-04-20 23:23:52 +02001556 if (key == NULL)
1557 break;
Raymond Hettinger426e0522011-01-03 02:12:02 +00001558 oldval = PyDict_GetItem(mapping, key);
1559 if (oldval == NULL) {
1560 if (PyDict_SetItem(mapping, key, one) == -1)
1561 break;
1562 } else {
1563 newval = PyNumber_Add(oldval, one);
1564 if (newval == NULL)
1565 break;
1566 if (PyDict_SetItem(mapping, key, newval) == -1)
1567 break;
1568 Py_CLEAR(newval);
1569 }
1570 Py_DECREF(key);
Raymond Hettinger96f34102010-12-15 16:30:37 +00001571 }
Raymond Hettinger426e0522011-01-03 02:12:02 +00001572 } else {
1573 while (1) {
1574 key = PyIter_Next(it);
Victor Stinnera154b5c2011-04-20 23:23:52 +02001575 if (key == NULL)
1576 break;
Raymond Hettinger426e0522011-01-03 02:12:02 +00001577 oldval = PyObject_GetItem(mapping, key);
1578 if (oldval == NULL) {
1579 if (!PyErr_Occurred() || !PyErr_ExceptionMatches(PyExc_KeyError))
1580 break;
1581 PyErr_Clear();
1582 Py_INCREF(one);
1583 newval = one;
1584 } else {
1585 newval = PyNumber_Add(oldval, one);
1586 Py_DECREF(oldval);
1587 if (newval == NULL)
1588 break;
1589 }
1590 if (PyObject_SetItem(mapping, key, newval) == -1)
Raymond Hettinger96f34102010-12-15 16:30:37 +00001591 break;
1592 Py_CLEAR(newval);
Raymond Hettinger426e0522011-01-03 02:12:02 +00001593 Py_DECREF(key);
Raymond Hettinger96f34102010-12-15 16:30:37 +00001594 }
Raymond Hettinger96f34102010-12-15 16:30:37 +00001595 }
Raymond Hettinger426e0522011-01-03 02:12:02 +00001596
Raymond Hettinger96f34102010-12-15 16:30:37 +00001597 Py_DECREF(it);
1598 Py_XDECREF(key);
1599 Py_XDECREF(newval);
1600 Py_DECREF(one);
1601 if (PyErr_Occurred())
1602 return NULL;
1603 Py_RETURN_NONE;
1604}
1605
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001606/* module level code ********************************************************/
1607
1608PyDoc_STRVAR(module_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00001609"High performance data structures.\n\
1610- deque: ordered collection accessible from endpoints only\n\
1611- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001612");
1613
Raymond Hettinger96f34102010-12-15 16:30:37 +00001614static struct PyMethodDef module_functions[] = {
1615 {"_count_elements", _count_elements, METH_VARARGS, _count_elements_doc},
1616 {NULL, NULL} /* sentinel */
1617};
Martin v. Löwis1a214512008-06-11 05:26:20 +00001618
1619static struct PyModuleDef _collectionsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 PyModuleDef_HEAD_INIT,
1621 "_collections",
1622 module_doc,
1623 -1,
Raymond Hettinger96f34102010-12-15 16:30:37 +00001624 module_functions,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 NULL,
1626 NULL,
1627 NULL,
1628 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001629};
1630
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001631PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001632PyInit__collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 PyObject *m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 m = PyModule_Create(&_collectionsmodule);
1637 if (m == NULL)
1638 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (PyType_Ready(&deque_type) < 0)
1641 return NULL;
1642 Py_INCREF(&deque_type);
1643 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 defdict_type.tp_base = &PyDict_Type;
1646 if (PyType_Ready(&defdict_type) < 0)
1647 return NULL;
1648 Py_INCREF(&defdict_type);
1649 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 if (PyType_Ready(&dequeiter_type) < 0)
1652 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (PyType_Ready(&dequereviter_type) < 0)
1655 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 return m;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001658}