blob: 27236d9abe057ef08bfea1609db9c3470f281e5a [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 Hettinger4ca4c7c2004-10-01 15:14:39 +000010#define BLOCKLEN 2
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000011#define CENTER ((BLOCKLEN - 1) / 2)
Raymond Hettinger756b3f32004-01-29 06:37:52 +000012
Tim Petersd8768d32004-10-01 01:32:53 +000013/* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
14 * This list is not circular (the leftmost block has leftlink==NULL,
15 * and the rightmost block has rightlink==NULL). A deque d's first
16 * element is at d.leftblock[leftindex] and its last element is at
17 * d.rightblock[rightindex]; note that, unlike as for Python slice
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000018 * indices, these indices are inclusive on both ends. By being inclusive
19 * on both ends, algorithms for left and right operations become
20 * symmetrical which simplifies the design.
21 *
22 * The list of blocks is never empty, so d.leftblock and d.rightblock
23 * are never equal to NULL.
24 *
25 * The indices, d.leftindex and d.rightindex are always in the range
26 * 0 <= index < BLOCKLEN.
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000027 * Their exact relationship is:
28 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000029 *
30 * Empty deques have d.len == 0; d.leftblock==d.rightblock;
31 * d.leftindex == CENTER+1; and d.rightindex == CENTER.
32 * Checking for d.len == 0 is the intended way to see whether d is empty.
33 *
34 * Whenever d.leftblock == d.rightblock,
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000035 * d.leftindex + d.len - 1 == d.rightindex.
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000036 *
Raymond Hettinger4ca4c7c2004-10-01 15:14:39 +000037 * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
38 * become indices into distinct blocks and either may be larger than the
39 * other.
Tim Petersd8768d32004-10-01 01:32:53 +000040 */
41
Raymond Hettinger756b3f32004-01-29 06:37:52 +000042typedef struct BLOCK {
43 struct BLOCK *leftlink;
44 struct BLOCK *rightlink;
45 PyObject *data[BLOCKLEN];
46} block;
47
Tim Peters6f853562004-10-01 01:04:50 +000048static block *
49newblock(block *leftlink, block *rightlink) {
Raymond Hettinger756b3f32004-01-29 06:37:52 +000050 block *b = PyMem_Malloc(sizeof(block));
51 if (b == NULL) {
52 PyErr_NoMemory();
53 return NULL;
54 }
55 b->leftlink = leftlink;
56 b->rightlink = rightlink;
57 return b;
58}
59
60typedef struct {
61 PyObject_HEAD
62 block *leftblock;
63 block *rightblock;
Tim Petersd8768d32004-10-01 01:32:53 +000064 int leftindex; /* in range(BLOCKLEN) */
65 int rightindex; /* in range(BLOCKLEN) */
Raymond Hettinger756b3f32004-01-29 06:37:52 +000066 int len;
Raymond Hettinger691d8052004-05-30 07:26:47 +000067 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger756b3f32004-01-29 06:37:52 +000068} dequeobject;
69
Neal Norwitz87f10132004-02-29 15:40:53 +000070static PyTypeObject deque_type;
Raymond Hettinger738ec902004-02-29 02:15:56 +000071
Raymond Hettinger756b3f32004-01-29 06:37:52 +000072static PyObject *
73deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
74{
75 dequeobject *deque;
76 block *b;
77
78 /* create dequeobject structure */
79 deque = (dequeobject *)type->tp_alloc(type, 0);
80 if (deque == NULL)
81 return NULL;
Tim Peters1065f752004-10-01 01:03:29 +000082
Raymond Hettinger756b3f32004-01-29 06:37:52 +000083 b = newblock(NULL, NULL);
84 if (b == NULL) {
85 Py_DECREF(deque);
86 return NULL;
87 }
88
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000089 assert(BLOCKLEN >= 2);
Raymond Hettinger756b3f32004-01-29 06:37:52 +000090 deque->leftblock = b;
91 deque->rightblock = b;
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000092 deque->leftindex = CENTER + 1;
93 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +000094 deque->len = 0;
Raymond Hettinger691d8052004-05-30 07:26:47 +000095 deque->weakreflist = NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +000096
97 return (PyObject *)deque;
98}
99
100static PyObject *
101deque_append(dequeobject *deque, PyObject *item)
102{
103 deque->rightindex++;
104 deque->len++;
105 if (deque->rightindex == BLOCKLEN) {
106 block *b = newblock(deque->rightblock, NULL);
107 if (b == NULL)
108 return NULL;
109 assert(deque->rightblock->rightlink == NULL);
110 deque->rightblock->rightlink = b;
111 deque->rightblock = b;
112 deque->rightindex = 0;
113 }
114 Py_INCREF(item);
115 deque->rightblock->data[deque->rightindex] = item;
116 Py_RETURN_NONE;
117}
118
119PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
120
121static PyObject *
122deque_appendleft(dequeobject *deque, PyObject *item)
123{
124 deque->leftindex--;
125 deque->len++;
126 if (deque->leftindex == -1) {
127 block *b = newblock(NULL, deque->leftblock);
128 if (b == NULL)
129 return NULL;
130 assert(deque->leftblock->leftlink == NULL);
131 deque->leftblock->leftlink = b;
132 deque->leftblock = b;
133 deque->leftindex = BLOCKLEN - 1;
134 }
135 Py_INCREF(item);
136 deque->leftblock->data[deque->leftindex] = item;
137 Py_RETURN_NONE;
138}
139
140PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
141
142static PyObject *
143deque_pop(dequeobject *deque, PyObject *unused)
144{
145 PyObject *item;
146 block *prevblock;
147
148 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000149 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000150 return NULL;
151 }
152 item = deque->rightblock->data[deque->rightindex];
153 deque->rightindex--;
154 deque->len--;
155
156 if (deque->rightindex == -1) {
157 if (deque->len == 0) {
158 assert(deque->leftblock == deque->rightblock);
159 assert(deque->leftindex == deque->rightindex+1);
160 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000161 deque->leftindex = CENTER + 1;
162 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000163 } else {
164 prevblock = deque->rightblock->leftlink;
165 assert(deque->leftblock != deque->rightblock);
166 PyMem_Free(deque->rightblock);
167 prevblock->rightlink = NULL;
168 deque->rightblock = prevblock;
169 deque->rightindex = BLOCKLEN - 1;
170 }
171 }
172 return item;
173}
174
175PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
176
177static PyObject *
178deque_popleft(dequeobject *deque, PyObject *unused)
179{
180 PyObject *item;
181 block *prevblock;
182
183 if (deque->len == 0) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000184 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000185 return NULL;
186 }
187 item = deque->leftblock->data[deque->leftindex];
188 deque->leftindex++;
189 deque->len--;
190
191 if (deque->leftindex == BLOCKLEN) {
192 if (deque->len == 0) {
193 assert(deque->leftblock == deque->rightblock);
194 assert(deque->leftindex == deque->rightindex+1);
195 /* re-center instead of freeing a block */
Raymond Hettinger61f05fb2004-10-01 06:24:12 +0000196 deque->leftindex = CENTER + 1;
197 deque->rightindex = CENTER;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000198 } else {
199 assert(deque->leftblock != deque->rightblock);
200 prevblock = deque->leftblock->rightlink;
201 assert(deque->leftblock != NULL);
202 PyMem_Free(deque->leftblock);
203 assert(prevblock != NULL);
204 prevblock->leftlink = NULL;
205 deque->leftblock = prevblock;
206 deque->leftindex = 0;
207 }
208 }
209 return item;
210}
211
212PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
213
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000214static PyObject *
215deque_extend(dequeobject *deque, PyObject *iterable)
216{
217 PyObject *it, *item;
218
219 it = PyObject_GetIter(iterable);
220 if (it == NULL)
221 return NULL;
222
223 while ((item = PyIter_Next(it)) != NULL) {
224 deque->rightindex++;
225 deque->len++;
226 if (deque->rightindex == BLOCKLEN) {
227 block *b = newblock(deque->rightblock, NULL);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000228 if (b == NULL) {
229 Py_DECREF(item);
230 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000231 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000232 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000233 assert(deque->rightblock->rightlink == NULL);
234 deque->rightblock->rightlink = b;
235 deque->rightblock = b;
236 deque->rightindex = 0;
237 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000238 deque->rightblock->data[deque->rightindex] = item;
239 }
240 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000241 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000242 return NULL;
243 Py_RETURN_NONE;
244}
245
Tim Peters1065f752004-10-01 01:03:29 +0000246PyDoc_STRVAR(extend_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000247"Extend the right side of the deque with elements from the iterable");
248
249static PyObject *
250deque_extendleft(dequeobject *deque, PyObject *iterable)
251{
252 PyObject *it, *item;
253
254 it = PyObject_GetIter(iterable);
255 if (it == NULL)
256 return NULL;
257
258 while ((item = PyIter_Next(it)) != NULL) {
259 deque->leftindex--;
260 deque->len++;
261 if (deque->leftindex == -1) {
262 block *b = newblock(NULL, deque->leftblock);
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000263 if (b == NULL) {
264 Py_DECREF(item);
265 Py_DECREF(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000266 return NULL;
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000267 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000268 assert(deque->leftblock->leftlink == NULL);
269 deque->leftblock->leftlink = b;
270 deque->leftblock = b;
271 deque->leftindex = BLOCKLEN - 1;
272 }
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000273 deque->leftblock->data[deque->leftindex] = item;
274 }
275 Py_DECREF(it);
Raymond Hettingera435c532004-07-09 04:10:20 +0000276 if (PyErr_Occurred())
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000277 return NULL;
278 Py_RETURN_NONE;
279}
280
Tim Peters1065f752004-10-01 01:03:29 +0000281PyDoc_STRVAR(extendleft_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000282"Extend the left side of the deque with elements from the iterable");
283
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000284static PyObject *
285deque_rotate(dequeobject *deque, PyObject *args)
286{
Raymond Hettingeree33b272004-02-08 04:05:26 +0000287 int i, n=1, len=deque->len, halflen=(len+1)>>1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000288 PyObject *item, *rv;
289
Raymond Hettingeree33b272004-02-08 04:05:26 +0000290 if (!PyArg_ParseTuple(args, "|i:rotate", &n))
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000291 return NULL;
292
Raymond Hettingeree33b272004-02-08 04:05:26 +0000293 if (len == 0)
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000294 Py_RETURN_NONE;
Raymond Hettingeree33b272004-02-08 04:05:26 +0000295 if (n > halflen || n < -halflen) {
296 n %= len;
297 if (n > halflen)
298 n -= len;
299 else if (n < -halflen)
300 n += len;
301 }
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000302
303 for (i=0 ; i<n ; i++) {
304 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000305 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000306 rv = deque_appendleft(deque, item);
307 Py_DECREF(item);
308 if (rv == NULL)
309 return NULL;
310 Py_DECREF(rv);
311 }
312 for (i=0 ; i>n ; i--) {
313 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000314 assert (item != NULL);
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000315 rv = deque_append(deque, item);
316 Py_DECREF(item);
317 if (rv == NULL)
318 return NULL;
319 Py_DECREF(rv);
320 }
321 Py_RETURN_NONE;
322}
323
Tim Peters1065f752004-10-01 01:03:29 +0000324PyDoc_STRVAR(rotate_doc,
Raymond Hettingeree33b272004-02-08 04:05:26 +0000325"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000326
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000327static int
328deque_len(dequeobject *deque)
329{
330 return deque->len;
331}
332
333static int
334deque_clear(dequeobject *deque)
335{
336 PyObject *item;
337
338 while (deque_len(deque)) {
339 item = deque_pop(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000340 assert (item != NULL);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000341 Py_DECREF(item);
342 }
343 assert(deque->leftblock == deque->rightblock &&
344 deque->leftindex > deque->rightindex);
345 return 0;
346}
347
348static PyObject *
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000349deque_item(dequeobject *deque, int i)
350{
351 block *b;
352 PyObject *item;
353 int n;
354
355 if (i < 0 || i >= deque->len) {
356 PyErr_SetString(PyExc_IndexError,
357 "deque index out of range");
358 return NULL;
359 }
360
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000361 if (i == 0) {
362 i = deque->leftindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000363 b = deque->leftblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000364 } else if (i == deque->len - 1) {
365 i = deque->rightindex;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000366 b = deque->rightblock;
Raymond Hettinger6c79a512004-03-04 08:00:54 +0000367 } else {
368 i += deque->leftindex;
369 n = i / BLOCKLEN;
370 i %= BLOCKLEN;
371 if (i < (deque->len >> 1)) {
372 b = deque->leftblock;
373 while (n--)
374 b = b->rightlink;
375 } else {
376 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
377 b = deque->rightblock;
378 while (n--)
379 b = b->leftlink;
380 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000381 }
382 item = b->data[i];
383 Py_INCREF(item);
384 return item;
385}
386
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000387/* delitem() implemented in terms of rotate for simplicity and reasonable
388 performance near the end points. If for some reason this method becomes
Tim Peters1065f752004-10-01 01:03:29 +0000389 popular, it is not hard to re-implement this using direct data movement
Raymond Hettinger616f4f62004-06-26 04:42:06 +0000390 (similar to code in list slice assignment) and achieve a two or threefold
391 performance boost.
392*/
393
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000394static int
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000395deque_del_item(dequeobject *deque, int i)
396{
397 PyObject *item=NULL, *minus_i=NULL, *plus_i=NULL;
398 int rv = -1;
399
Tim Peters1065f752004-10-01 01:03:29 +0000400 assert (i >= 0 && i < deque->len);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000401
402 minus_i = Py_BuildValue("(i)", -i);
403 if (minus_i == NULL)
404 goto fail;
405
406 plus_i = Py_BuildValue("(i)", i);
407 if (plus_i == NULL)
408 goto fail;
409
410 item = deque_rotate(deque, minus_i);
Tim Peters1065f752004-10-01 01:03:29 +0000411 if (item == NULL)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000412 goto fail;
413 Py_DECREF(item);
414
415 item = deque_popleft(deque, NULL);
Raymond Hettingera435c532004-07-09 04:10:20 +0000416 assert (item != NULL);
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000417 Py_DECREF(item);
418
419 item = deque_rotate(deque, plus_i);
Tim Peters1065f752004-10-01 01:03:29 +0000420 if (item == NULL)
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000421 goto fail;
422
423 rv = 0;
424fail:
425 Py_XDECREF(item);
426 Py_XDECREF(minus_i);
427 Py_XDECREF(plus_i);
428 return rv;
429}
430
431static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000432deque_ass_item(dequeobject *deque, int i, PyObject *v)
433{
434 PyObject *old_value;
435 block *b;
Raymond Hettingera435c532004-07-09 04:10:20 +0000436 int n, len=deque->len, halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000437
Raymond Hettingera435c532004-07-09 04:10:20 +0000438 if (i < 0 || i >= len) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000439 PyErr_SetString(PyExc_IndexError,
440 "deque index out of range");
441 return -1;
442 }
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000443 if (v == NULL)
444 return deque_del_item(deque, i);
445
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000446 i += deque->leftindex;
447 n = i / BLOCKLEN;
448 i %= BLOCKLEN;
Raymond Hettingera435c532004-07-09 04:10:20 +0000449 if (index <= halflen) {
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000450 b = deque->leftblock;
451 while (n--)
452 b = b->rightlink;
453 } else {
Raymond Hettingera435c532004-07-09 04:10:20 +0000454 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000455 b = deque->rightblock;
456 while (n--)
457 b = b->leftlink;
458 }
459 Py_INCREF(v);
460 old_value = b->data[i];
461 b->data[i] = v;
462 Py_DECREF(old_value);
463 return 0;
464}
465
466static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000467deque_clearmethod(dequeobject *deque)
468{
Raymond Hettingera435c532004-07-09 04:10:20 +0000469 int rv;
470
471 rv = deque_clear(deque);
472 assert (rv != -1);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000473 Py_RETURN_NONE;
474}
475
476PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
477
478static void
479deque_dealloc(dequeobject *deque)
480{
481 PyObject_GC_UnTrack(deque);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000482 if (deque->weakreflist != NULL)
483 PyObject_ClearWeakRefs((PyObject *) deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000484 if (deque->leftblock != NULL) {
Raymond Hettingere9c89e82004-07-19 00:10:24 +0000485 deque_clear(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000486 assert(deque->leftblock != NULL);
487 PyMem_Free(deque->leftblock);
488 }
489 deque->leftblock = NULL;
490 deque->rightblock = NULL;
491 deque->ob_type->tp_free(deque);
492}
493
494static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000495deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000496{
Tim Peters10c7e862004-10-01 02:01:04 +0000497 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000498 PyObject *item;
Tim Peters10c7e862004-10-01 02:01:04 +0000499 int index;
500 int indexlo = deque->leftindex;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000501
Tim Peters10c7e862004-10-01 02:01:04 +0000502 assert(deque->leftblock != NULL);
503 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
504 const int indexhi = b == deque->rightblock ?
505 deque->rightindex :
506 BLOCKLEN - 1;
507
508 for (index = indexlo; index <= indexhi; ++index) {
509 item = b->data[index];
510 Py_VISIT(item);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000511 }
Tim Peters10c7e862004-10-01 02:01:04 +0000512 indexlo = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000513 }
514 return 0;
515}
516
517static long
518deque_nohash(PyObject *self)
519{
520 PyErr_SetString(PyExc_TypeError, "deque objects are unhashable");
521 return -1;
522}
523
524static PyObject *
525deque_copy(PyObject *deque)
526{
Tim Peters1065f752004-10-01 01:03:29 +0000527 return PyObject_CallFunctionObjArgs((PyObject *)(deque->ob_type),
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000528 deque, NULL);
529}
530
531PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
532
533static PyObject *
534deque_reduce(dequeobject *deque)
535{
536 PyObject *seq, *args, *result;
537
538 seq = PySequence_Tuple((PyObject *)deque);
539 if (seq == NULL)
540 return NULL;
541 args = PyTuple_Pack(1, seq);
542 if (args == NULL) {
543 Py_DECREF(seq);
544 return NULL;
545 }
546 result = PyTuple_Pack(2, deque->ob_type, args);
547 Py_DECREF(seq);
548 Py_DECREF(args);
549 return result;
550}
551
552PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
553
554static PyObject *
555deque_repr(PyObject *deque)
556{
557 PyObject *aslist, *result, *fmt;
558 int i;
559
560 i = Py_ReprEnter(deque);
561 if (i != 0) {
562 if (i < 0)
563 return NULL;
564 return PyString_FromString("[...]");
565 }
566
567 aslist = PySequence_List(deque);
568 if (aslist == NULL) {
569 Py_ReprLeave(deque);
570 return NULL;
571 }
572
573 fmt = PyString_FromString("deque(%r)");
574 if (fmt == NULL) {
575 Py_DECREF(aslist);
576 Py_ReprLeave(deque);
577 return NULL;
578 }
579 result = PyString_Format(fmt, aslist);
580 Py_DECREF(fmt);
581 Py_DECREF(aslist);
582 Py_ReprLeave(deque);
583 return result;
584}
585
586static int
587deque_tp_print(PyObject *deque, FILE *fp, int flags)
588{
589 PyObject *it, *item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000590 char *emit = ""; /* No separator emitted on first pass */
591 char *separator = ", ";
592 int i;
593
594 i = Py_ReprEnter(deque);
595 if (i != 0) {
596 if (i < 0)
597 return i;
598 fputs("[...]", fp);
599 return 0;
600 }
601
602 it = PyObject_GetIter(deque);
603 if (it == NULL)
604 return -1;
605
606 fputs("deque([", fp);
607 while ((item = PyIter_Next(it)) != NULL) {
608 fputs(emit, fp);
609 emit = separator;
610 if (PyObject_Print(item, fp, 0) != 0) {
611 Py_DECREF(item);
612 Py_DECREF(it);
613 Py_ReprLeave(deque);
614 return -1;
615 }
616 Py_DECREF(item);
617 }
618 Py_ReprLeave(deque);
619 Py_DECREF(it);
Tim Peters1065f752004-10-01 01:03:29 +0000620 if (PyErr_Occurred())
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000621 return -1;
622 fputs("])", fp);
623 return 0;
624}
625
Raymond Hettinger738ec902004-02-29 02:15:56 +0000626static PyObject *
627deque_richcompare(PyObject *v, PyObject *w, int op)
628{
629 PyObject *it1=NULL, *it2=NULL, *x, *y;
630 int i, b, vs, ws, minlen, cmp=-1;
631
Tim Peters1065f752004-10-01 01:03:29 +0000632 if (!PyObject_TypeCheck(v, &deque_type) ||
Raymond Hettinger285cfcc2004-05-18 18:15:03 +0000633 !PyObject_TypeCheck(w, &deque_type)) {
Raymond Hettinger738ec902004-02-29 02:15:56 +0000634 Py_INCREF(Py_NotImplemented);
635 return Py_NotImplemented;
636 }
637
638 /* Shortcuts */
639 vs = ((dequeobject *)v)->len;
640 ws = ((dequeobject *)w)->len;
641 if (op == Py_EQ) {
642 if (v == w)
643 Py_RETURN_TRUE;
644 if (vs != ws)
645 Py_RETURN_FALSE;
646 }
647 if (op == Py_NE) {
648 if (v == w)
649 Py_RETURN_FALSE;
650 if (vs != ws)
651 Py_RETURN_TRUE;
652 }
653
654 /* Search for the first index where items are different */
655 it1 = PyObject_GetIter(v);
656 if (it1 == NULL)
657 goto done;
658 it2 = PyObject_GetIter(w);
659 if (it2 == NULL)
660 goto done;
661 minlen = (vs < ws) ? vs : ws;
662 for (i=0 ; i < minlen ; i++) {
663 x = PyIter_Next(it1);
664 if (x == NULL)
665 goto done;
666 y = PyIter_Next(it2);
667 if (y == NULL) {
668 Py_DECREF(x);
669 goto done;
670 }
671 b = PyObject_RichCompareBool(x, y, Py_EQ);
672 if (b == 0) {
673 cmp = PyObject_RichCompareBool(x, y, op);
674 Py_DECREF(x);
675 Py_DECREF(y);
676 goto done;
677 }
678 Py_DECREF(x);
679 Py_DECREF(y);
680 if (b == -1)
681 goto done;
682 }
683 /* Elements are equal through minlen. The longest input is the greatest */
684 switch (op) {
685 case Py_LT: cmp = vs < ws; break;
686 case Py_LE: cmp = vs <= ws; break;
687 case Py_EQ: cmp = vs == ws; break;
688 case Py_NE: cmp = vs != ws; break;
689 case Py_GT: cmp = vs > ws; break;
690 case Py_GE: cmp = vs >= ws; break;
691 }
Tim Peters1065f752004-10-01 01:03:29 +0000692
Raymond Hettinger738ec902004-02-29 02:15:56 +0000693done:
694 Py_XDECREF(it1);
695 Py_XDECREF(it2);
696 if (cmp == 1)
697 Py_RETURN_TRUE;
698 if (cmp == 0)
699 Py_RETURN_FALSE;
700 return NULL;
701}
702
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000703static int
704deque_init(dequeobject *deque, PyObject *args, PyObject *kwds)
705{
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000706 PyObject *iterable = NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000707
708 if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable))
709 return -1;
710
711 if (iterable != NULL) {
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000712 PyObject *rv = deque_extend(deque, iterable);
713 if (rv == NULL)
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000714 return -1;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000715 Py_DECREF(rv);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000716 }
717 return 0;
718}
719
720static PySequenceMethods deque_as_sequence = {
721 (inquiry)deque_len, /* sq_length */
722 0, /* sq_concat */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000723 0, /* sq_repeat */
724 (intargfunc)deque_item, /* sq_item */
725 0, /* sq_slice */
726 (intobjargproc)deque_ass_item, /* sq_ass_item */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000727};
728
729/* deque object ********************************************************/
730
731static PyObject *deque_iter(dequeobject *deque);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000732static PyObject *deque_reviter(dequeobject *deque);
Tim Peters1065f752004-10-01 01:03:29 +0000733PyDoc_STRVAR(reversed_doc,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000734 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000735
736static PyMethodDef deque_methods[] = {
Tim Peters1065f752004-10-01 01:03:29 +0000737 {"append", (PyCFunction)deque_append,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000738 METH_O, append_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000739 {"appendleft", (PyCFunction)deque_appendleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000740 METH_O, appendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000741 {"clear", (PyCFunction)deque_clearmethod,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000742 METH_NOARGS, clear_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000743 {"__copy__", (PyCFunction)deque_copy,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000744 METH_NOARGS, copy_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000745 {"extend", (PyCFunction)deque_extend,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000746 METH_O, extend_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000747 {"extendleft", (PyCFunction)deque_extendleft,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000748 METH_O, extendleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000749 {"pop", (PyCFunction)deque_pop,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000750 METH_NOARGS, pop_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000751 {"popleft", (PyCFunction)deque_popleft,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000752 METH_NOARGS, popleft_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000753 {"__reduce__", (PyCFunction)deque_reduce,
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000754 METH_NOARGS, reduce_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000755 {"__reversed__", (PyCFunction)deque_reviter,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000756 METH_NOARGS, reversed_doc},
Tim Peters1065f752004-10-01 01:03:29 +0000757 {"rotate", (PyCFunction)deque_rotate,
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000758 METH_VARARGS, rotate_doc},
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000759 {NULL, NULL} /* sentinel */
760};
761
762PyDoc_STRVAR(deque_doc,
763"deque(iterable) --> deque object\n\
764\n\
765Build an ordered collection accessible from endpoints only.");
766
Neal Norwitz87f10132004-02-29 15:40:53 +0000767static PyTypeObject deque_type = {
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000768 PyObject_HEAD_INIT(NULL)
769 0, /* ob_size */
770 "collections.deque", /* tp_name */
771 sizeof(dequeobject), /* tp_basicsize */
772 0, /* tp_itemsize */
773 /* methods */
774 (destructor)deque_dealloc, /* tp_dealloc */
775 (printfunc)deque_tp_print, /* tp_print */
776 0, /* tp_getattr */
777 0, /* tp_setattr */
778 0, /* tp_compare */
779 (reprfunc)deque_repr, /* tp_repr */
780 0, /* tp_as_number */
781 &deque_as_sequence, /* tp_as_sequence */
782 0, /* tp_as_mapping */
783 deque_nohash, /* tp_hash */
784 0, /* tp_call */
785 0, /* tp_str */
786 PyObject_GenericGetAttr, /* tp_getattro */
787 0, /* tp_setattro */
788 0, /* tp_as_buffer */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000789 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
790 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000791 deque_doc, /* tp_doc */
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000792 (traverseproc)deque_traverse, /* tp_traverse */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000793 (inquiry)deque_clear, /* tp_clear */
Raymond Hettinger738ec902004-02-29 02:15:56 +0000794 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +0000795 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000796 (getiterfunc)deque_iter, /* tp_iter */
797 0, /* tp_iternext */
798 deque_methods, /* tp_methods */
799 0, /* tp_members */
800 0, /* tp_getset */
801 0, /* tp_base */
802 0, /* tp_dict */
803 0, /* tp_descr_get */
804 0, /* tp_descr_set */
805 0, /* tp_dictoffset */
806 (initproc)deque_init, /* tp_init */
807 PyType_GenericAlloc, /* tp_alloc */
808 deque_new, /* tp_new */
809 PyObject_GC_Del, /* tp_free */
810};
811
812/*********************** Deque Iterator **************************/
813
814typedef struct {
815 PyObject_HEAD
816 int index;
817 block *b;
818 dequeobject *deque;
819 int len;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000820 int counter;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000821} dequeiterobject;
822
823PyTypeObject dequeiter_type;
824
825static PyObject *
826deque_iter(dequeobject *deque)
827{
828 dequeiterobject *it;
829
830 it = PyObject_New(dequeiterobject, &dequeiter_type);
831 if (it == NULL)
832 return NULL;
833 it->b = deque->leftblock;
834 it->index = deque->leftindex;
835 Py_INCREF(deque);
836 it->deque = deque;
837 it->len = deque->len;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000838 it->counter = deque->len;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000839 return (PyObject *)it;
840}
841
842static void
843dequeiter_dealloc(dequeiterobject *dio)
844{
845 Py_XDECREF(dio->deque);
846 dio->ob_type->tp_free(dio);
847}
848
849static PyObject *
850dequeiter_next(dequeiterobject *it)
851{
852 PyObject *item;
853 if (it->b == it->deque->rightblock && it->index > it->deque->rightindex)
854 return NULL;
855
856 if (it->len != it->deque->len) {
857 it->len = -1; /* Make this state sticky */
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000858 it->counter = 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000859 PyErr_SetString(PyExc_RuntimeError,
860 "deque changed size during iteration");
861 return NULL;
862 }
863
864 item = it->b->data[it->index];
865 it->index++;
866 if (it->index == BLOCKLEN && it->b->rightlink != NULL) {
867 it->b = it->b->rightlink;
868 it->index = 0;
869 }
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000870 it->counter--;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000871 Py_INCREF(item);
872 return item;
873}
874
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000875static int
876dequeiter_len(dequeiterobject *it)
877{
878 return it->counter;
879}
880
881static PySequenceMethods dequeiter_as_sequence = {
882 (inquiry)dequeiter_len, /* sq_length */
883 0, /* sq_concat */
884};
885
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000886PyTypeObject dequeiter_type = {
887 PyObject_HEAD_INIT(NULL)
888 0, /* ob_size */
889 "deque_iterator", /* tp_name */
890 sizeof(dequeiterobject), /* tp_basicsize */
891 0, /* tp_itemsize */
892 /* methods */
893 (destructor)dequeiter_dealloc, /* tp_dealloc */
894 0, /* tp_print */
895 0, /* tp_getattr */
896 0, /* tp_setattr */
897 0, /* tp_compare */
898 0, /* tp_repr */
899 0, /* tp_as_number */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000900 &dequeiter_as_sequence, /* tp_as_sequence */
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000901 0, /* tp_as_mapping */
902 0, /* tp_hash */
903 0, /* tp_call */
904 0, /* tp_str */
905 PyObject_GenericGetAttr, /* tp_getattro */
906 0, /* tp_setattro */
907 0, /* tp_as_buffer */
908 Py_TPFLAGS_DEFAULT, /* tp_flags */
909 0, /* tp_doc */
910 0, /* tp_traverse */
911 0, /* tp_clear */
912 0, /* tp_richcompare */
913 0, /* tp_weaklistoffset */
914 PyObject_SelfIter, /* tp_iter */
915 (iternextfunc)dequeiter_next, /* tp_iternext */
916 0,
917};
918
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000919/*********************** Deque Reverse Iterator **************************/
920
921PyTypeObject dequereviter_type;
922
923static PyObject *
924deque_reviter(dequeobject *deque)
925{
926 dequeiterobject *it;
927
928 it = PyObject_New(dequeiterobject, &dequereviter_type);
929 if (it == NULL)
930 return NULL;
931 it->b = deque->rightblock;
932 it->index = deque->rightindex;
933 Py_INCREF(deque);
934 it->deque = deque;
935 it->len = deque->len;
936 it->counter = deque->len;
937 return (PyObject *)it;
938}
939
940static PyObject *
941dequereviter_next(dequeiterobject *it)
942{
943 PyObject *item;
944 if (it->b == it->deque->leftblock && it->index < it->deque->leftindex)
945 return NULL;
946
947 if (it->len != it->deque->len) {
948 it->len = -1; /* Make this state sticky */
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000949 it->counter = 0;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +0000950 PyErr_SetString(PyExc_RuntimeError,
951 "deque changed size during iteration");
952 return NULL;
953 }
954
955 item = it->b->data[it->index];
956 it->index--;
957 if (it->index == -1 && it->b->leftlink != NULL) {
958 it->b = it->b->leftlink;
959 it->index = BLOCKLEN - 1;
960 }
961 it->counter--;
962 Py_INCREF(item);
963 return item;
964}
965
966PyTypeObject dequereviter_type = {
967 PyObject_HEAD_INIT(NULL)
968 0, /* ob_size */
969 "deque_reverse_iterator", /* tp_name */
970 sizeof(dequeiterobject), /* tp_basicsize */
971 0, /* tp_itemsize */
972 /* methods */
973 (destructor)dequeiter_dealloc, /* tp_dealloc */
974 0, /* tp_print */
975 0, /* tp_getattr */
976 0, /* tp_setattr */
977 0, /* tp_compare */
978 0, /* tp_repr */
979 0, /* tp_as_number */
980 &dequeiter_as_sequence, /* tp_as_sequence */
981 0, /* tp_as_mapping */
982 0, /* tp_hash */
983 0, /* tp_call */
984 0, /* tp_str */
985 PyObject_GenericGetAttr, /* tp_getattro */
986 0, /* tp_setattro */
987 0, /* tp_as_buffer */
988 Py_TPFLAGS_DEFAULT, /* tp_flags */
989 0, /* tp_doc */
990 0, /* tp_traverse */
991 0, /* tp_clear */
992 0, /* tp_richcompare */
993 0, /* tp_weaklistoffset */
994 PyObject_SelfIter, /* tp_iter */
995 (iternextfunc)dequereviter_next, /* tp_iternext */
996 0,
997};
998
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000999/* module level code ********************************************************/
1000
1001PyDoc_STRVAR(module_doc,
1002"High performance data structures\n\
1003");
1004
1005PyMODINIT_FUNC
1006initcollections(void)
1007{
1008 PyObject *m;
1009
1010 m = Py_InitModule3("collections", NULL, module_doc);
1011
1012 if (PyType_Ready(&deque_type) < 0)
1013 return;
1014 Py_INCREF(&deque_type);
1015 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1016
1017 if (PyType_Ready(&dequeiter_type) < 0)
Tim Peters1065f752004-10-01 01:03:29 +00001018 return;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001019
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001020 if (PyType_Ready(&dequereviter_type) < 0)
1021 return;
1022
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001023 return;
1024}