Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 2 | #include "structmember.h" |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 3 | |
| 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 Hettinger | 77e8bf1 | 2004-10-01 15:25:53 +0000 | [diff] [blame] | 10 | /* 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13 | * length of a cache line. |
Raymond Hettinger | 77e8bf1 | 2004-10-01 15:25:53 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Raymond Hettinger | 7d112df | 2004-11-02 02:11:35 +0000 | [diff] [blame] | 16 | #define BLOCKLEN 62 |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 17 | #define CENTER ((BLOCKLEN - 1) / 2) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 18 | |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 19 | /* 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 Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 24 | * indices, these indices are inclusive on both ends. By being inclusive |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 25 | * on both ends, algorithms for left and right operations become |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 26 | * symmetrical which simplifies the design. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | * |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 28 | * 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 Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 33 | * Their exact relationship is: |
| 34 | * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex. |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 35 | * |
| 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 40 | * Whenever d.leftblock == d.rightblock, |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 41 | * d.leftindex + d.len - 1 == d.rightindex. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 42 | * |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 43 | * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 44 | * become indices into distinct blocks and either may be larger than the |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 45 | * other. |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 48 | typedef struct BLOCK { |
| 49 | struct BLOCK *leftlink; |
| 50 | struct BLOCK *rightlink; |
| 51 | PyObject *data[BLOCKLEN]; |
| 52 | } block; |
| 53 | |
Tim Peters | 6f85356 | 2004-10-01 01:04:50 +0000 | [diff] [blame] | 54 | static block * |
Raymond Hettinger | c5fa992 | 2004-10-06 17:51:54 +0000 | [diff] [blame] | 55 | newblock(block *leftlink, block *rightlink, int len) { |
| 56 | block *b; |
| 57 | /* To prevent len from overflowing INT_MAX on 64-bit machines, we |
| 58 | * refuse to allocate new blocks if the current len is dangerously |
| 59 | * close. There is some extra margin to prevent spurious arithmetic |
| 60 | * overflows at various places. The following check ensures that |
| 61 | * the blocks allocated to the deque, in the worst case, can only |
| 62 | * have INT_MAX-2 entries in total. |
| 63 | */ |
| 64 | if (len >= INT_MAX - 2*BLOCKLEN) { |
| 65 | PyErr_SetString(PyExc_OverflowError, |
| 66 | "cannot add more blocks to the deque"); |
| 67 | return NULL; |
| 68 | } |
| 69 | b = PyMem_Malloc(sizeof(block)); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 70 | if (b == NULL) { |
| 71 | PyErr_NoMemory(); |
| 72 | return NULL; |
| 73 | } |
| 74 | b->leftlink = leftlink; |
| 75 | b->rightlink = rightlink; |
| 76 | return b; |
| 77 | } |
| 78 | |
| 79 | typedef struct { |
| 80 | PyObject_HEAD |
| 81 | block *leftblock; |
| 82 | block *rightblock; |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 83 | int leftindex; /* in range(BLOCKLEN) */ |
| 84 | int rightindex; /* in range(BLOCKLEN) */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 85 | int len; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 86 | long state; /* incremented whenever the indices move */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 87 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 88 | } dequeobject; |
| 89 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 90 | static PyTypeObject deque_type; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 91 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 92 | static PyObject * |
| 93 | deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 94 | { |
| 95 | dequeobject *deque; |
| 96 | block *b; |
| 97 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 98 | if (type == &deque_type && !_PyArg_NoKeywords("deque()", kwds)) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 99 | return NULL; |
| 100 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 101 | /* create dequeobject structure */ |
| 102 | deque = (dequeobject *)type->tp_alloc(type, 0); |
| 103 | if (deque == NULL) |
| 104 | return NULL; |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 105 | |
Raymond Hettinger | c5fa992 | 2004-10-06 17:51:54 +0000 | [diff] [blame] | 106 | b = newblock(NULL, NULL, 0); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 107 | if (b == NULL) { |
| 108 | Py_DECREF(deque); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 112 | assert(BLOCKLEN >= 2); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 113 | deque->leftblock = b; |
| 114 | deque->rightblock = b; |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 115 | deque->leftindex = CENTER + 1; |
| 116 | deque->rightindex = CENTER; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 117 | deque->len = 0; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 118 | deque->state = 0; |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 119 | deque->weakreflist = NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 120 | |
| 121 | return (PyObject *)deque; |
| 122 | } |
| 123 | |
| 124 | static PyObject * |
| 125 | deque_append(dequeobject *deque, PyObject *item) |
| 126 | { |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 127 | deque->state++; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 128 | if (deque->rightindex == BLOCKLEN-1) { |
Raymond Hettinger | c5fa992 | 2004-10-06 17:51:54 +0000 | [diff] [blame] | 129 | block *b = newblock(deque->rightblock, NULL, deque->len); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 130 | if (b == NULL) |
| 131 | return NULL; |
| 132 | assert(deque->rightblock->rightlink == NULL); |
| 133 | deque->rightblock->rightlink = b; |
| 134 | deque->rightblock = b; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 135 | deque->rightindex = -1; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 136 | } |
| 137 | Py_INCREF(item); |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 138 | deque->len++; |
| 139 | deque->rightindex++; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 140 | deque->rightblock->data[deque->rightindex] = item; |
| 141 | Py_RETURN_NONE; |
| 142 | } |
| 143 | |
| 144 | PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque."); |
| 145 | |
| 146 | static PyObject * |
| 147 | deque_appendleft(dequeobject *deque, PyObject *item) |
| 148 | { |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 149 | deque->state++; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 150 | if (deque->leftindex == 0) { |
Raymond Hettinger | c5fa992 | 2004-10-06 17:51:54 +0000 | [diff] [blame] | 151 | block *b = newblock(NULL, deque->leftblock, deque->len); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 152 | if (b == NULL) |
| 153 | return NULL; |
| 154 | assert(deque->leftblock->leftlink == NULL); |
| 155 | deque->leftblock->leftlink = b; |
| 156 | deque->leftblock = b; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 157 | deque->leftindex = BLOCKLEN; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 158 | } |
| 159 | Py_INCREF(item); |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 160 | deque->len++; |
| 161 | deque->leftindex--; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 162 | deque->leftblock->data[deque->leftindex] = item; |
| 163 | Py_RETURN_NONE; |
| 164 | } |
| 165 | |
| 166 | PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque."); |
| 167 | |
| 168 | static PyObject * |
| 169 | deque_pop(dequeobject *deque, PyObject *unused) |
| 170 | { |
| 171 | PyObject *item; |
| 172 | block *prevblock; |
| 173 | |
| 174 | if (deque->len == 0) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 175 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | item = deque->rightblock->data[deque->rightindex]; |
| 179 | deque->rightindex--; |
| 180 | deque->len--; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 181 | deque->state++; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 182 | |
| 183 | if (deque->rightindex == -1) { |
| 184 | if (deque->len == 0) { |
| 185 | assert(deque->leftblock == deque->rightblock); |
| 186 | assert(deque->leftindex == deque->rightindex+1); |
| 187 | /* re-center instead of freeing a block */ |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 188 | deque->leftindex = CENTER + 1; |
| 189 | deque->rightindex = CENTER; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 190 | } else { |
| 191 | prevblock = deque->rightblock->leftlink; |
| 192 | assert(deque->leftblock != deque->rightblock); |
| 193 | PyMem_Free(deque->rightblock); |
| 194 | prevblock->rightlink = NULL; |
| 195 | deque->rightblock = prevblock; |
| 196 | deque->rightindex = BLOCKLEN - 1; |
| 197 | } |
| 198 | } |
| 199 | return item; |
| 200 | } |
| 201 | |
| 202 | PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element."); |
| 203 | |
| 204 | static PyObject * |
| 205 | deque_popleft(dequeobject *deque, PyObject *unused) |
| 206 | { |
| 207 | PyObject *item; |
| 208 | block *prevblock; |
| 209 | |
| 210 | if (deque->len == 0) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 211 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 212 | return NULL; |
| 213 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 214 | assert(deque->leftblock != NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 215 | item = deque->leftblock->data[deque->leftindex]; |
| 216 | deque->leftindex++; |
| 217 | deque->len--; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 218 | deque->state++; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 219 | |
| 220 | if (deque->leftindex == BLOCKLEN) { |
| 221 | if (deque->len == 0) { |
| 222 | assert(deque->leftblock == deque->rightblock); |
| 223 | assert(deque->leftindex == deque->rightindex+1); |
| 224 | /* re-center instead of freeing a block */ |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 225 | deque->leftindex = CENTER + 1; |
| 226 | deque->rightindex = CENTER; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 227 | } else { |
| 228 | assert(deque->leftblock != deque->rightblock); |
| 229 | prevblock = deque->leftblock->rightlink; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 230 | PyMem_Free(deque->leftblock); |
| 231 | assert(prevblock != NULL); |
| 232 | prevblock->leftlink = NULL; |
| 233 | deque->leftblock = prevblock; |
| 234 | deque->leftindex = 0; |
| 235 | } |
| 236 | } |
| 237 | return item; |
| 238 | } |
| 239 | |
| 240 | PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element."); |
| 241 | |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 242 | static PyObject * |
| 243 | deque_extend(dequeobject *deque, PyObject *iterable) |
| 244 | { |
| 245 | PyObject *it, *item; |
| 246 | |
| 247 | it = PyObject_GetIter(iterable); |
| 248 | if (it == NULL) |
| 249 | return NULL; |
| 250 | |
| 251 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 252 | deque->state++; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 253 | if (deque->rightindex == BLOCKLEN-1) { |
Raymond Hettinger | c5fa992 | 2004-10-06 17:51:54 +0000 | [diff] [blame] | 254 | block *b = newblock(deque->rightblock, NULL, |
| 255 | deque->len); |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 256 | if (b == NULL) { |
| 257 | Py_DECREF(item); |
| 258 | Py_DECREF(it); |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 259 | return NULL; |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 260 | } |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 261 | assert(deque->rightblock->rightlink == NULL); |
| 262 | deque->rightblock->rightlink = b; |
| 263 | deque->rightblock = b; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 264 | deque->rightindex = -1; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 265 | } |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 266 | deque->len++; |
| 267 | deque->rightindex++; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 268 | deque->rightblock->data[deque->rightindex] = item; |
| 269 | } |
| 270 | Py_DECREF(it); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 271 | if (PyErr_Occurred()) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 272 | return NULL; |
| 273 | Py_RETURN_NONE; |
| 274 | } |
| 275 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 276 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 277 | "Extend the right side of the deque with elements from the iterable"); |
| 278 | |
| 279 | static PyObject * |
| 280 | deque_extendleft(dequeobject *deque, PyObject *iterable) |
| 281 | { |
| 282 | PyObject *it, *item; |
| 283 | |
| 284 | it = PyObject_GetIter(iterable); |
| 285 | if (it == NULL) |
| 286 | return NULL; |
| 287 | |
| 288 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 289 | deque->state++; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 290 | if (deque->leftindex == 0) { |
Raymond Hettinger | c5fa992 | 2004-10-06 17:51:54 +0000 | [diff] [blame] | 291 | block *b = newblock(NULL, deque->leftblock, |
| 292 | deque->len); |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 293 | if (b == NULL) { |
| 294 | Py_DECREF(item); |
| 295 | Py_DECREF(it); |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 296 | return NULL; |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 297 | } |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 298 | assert(deque->leftblock->leftlink == NULL); |
| 299 | deque->leftblock->leftlink = b; |
| 300 | deque->leftblock = b; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 301 | deque->leftindex = BLOCKLEN; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 302 | } |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 303 | deque->len++; |
| 304 | deque->leftindex--; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 305 | deque->leftblock->data[deque->leftindex] = item; |
| 306 | } |
| 307 | Py_DECREF(it); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 308 | if (PyErr_Occurred()) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 309 | return NULL; |
| 310 | Py_RETURN_NONE; |
| 311 | } |
| 312 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 313 | PyDoc_STRVAR(extendleft_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 314 | "Extend the left side of the deque with elements from the iterable"); |
| 315 | |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 316 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 317 | _deque_rotate(dequeobject *deque, Py_ssize_t n) |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 318 | { |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 319 | int i, len=deque->len, halflen=(len+1)>>1; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 320 | PyObject *item, *rv; |
| 321 | |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 322 | if (len == 0) |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 323 | return 0; |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 324 | if (n > halflen || n < -halflen) { |
| 325 | n %= len; |
| 326 | if (n > halflen) |
| 327 | n -= len; |
| 328 | else if (n < -halflen) |
| 329 | n += len; |
| 330 | } |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 331 | |
| 332 | for (i=0 ; i<n ; i++) { |
| 333 | item = deque_pop(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 334 | assert (item != NULL); |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 335 | rv = deque_appendleft(deque, item); |
| 336 | Py_DECREF(item); |
| 337 | if (rv == NULL) |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 338 | return -1; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 339 | Py_DECREF(rv); |
| 340 | } |
| 341 | for (i=0 ; i>n ; i--) { |
| 342 | item = deque_popleft(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 343 | assert (item != NULL); |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 344 | rv = deque_append(deque, item); |
| 345 | Py_DECREF(item); |
| 346 | if (rv == NULL) |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 347 | return -1; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 348 | Py_DECREF(rv); |
| 349 | } |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static PyObject * |
| 354 | deque_rotate(dequeobject *deque, PyObject *args) |
| 355 | { |
| 356 | int n=1; |
| 357 | |
| 358 | if (!PyArg_ParseTuple(args, "|i:rotate", &n)) |
| 359 | return NULL; |
| 360 | if (_deque_rotate(deque, n) == 0) |
| 361 | Py_RETURN_NONE; |
| 362 | return NULL; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 365 | PyDoc_STRVAR(rotate_doc, |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 366 | "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left."); |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 367 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 368 | static Py_ssize_t |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 369 | deque_len(dequeobject *deque) |
| 370 | { |
| 371 | return deque->len; |
| 372 | } |
| 373 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 374 | static PyObject * |
| 375 | deque_remove(dequeobject *deque, PyObject *value) |
| 376 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 377 | Py_ssize_t i, n=deque->len; |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 378 | |
| 379 | for (i=0 ; i<n ; i++) { |
| 380 | PyObject *item = deque->leftblock->data[deque->leftindex]; |
| 381 | int cmp = PyObject_RichCompareBool(item, value, Py_EQ); |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 382 | |
| 383 | if (deque->len != n) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 384 | PyErr_SetString(PyExc_IndexError, |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 385 | "deque mutated during remove()."); |
| 386 | return NULL; |
| 387 | } |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 388 | if (cmp > 0) { |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 389 | PyObject *tgt = deque_popleft(deque, NULL); |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 390 | assert (tgt != NULL); |
| 391 | Py_DECREF(tgt); |
| 392 | if (_deque_rotate(deque, i) == -1) |
| 393 | return NULL; |
| 394 | Py_RETURN_NONE; |
| 395 | } |
| 396 | else if (cmp < 0) { |
| 397 | _deque_rotate(deque, i); |
| 398 | return NULL; |
| 399 | } |
| 400 | _deque_rotate(deque, -1); |
| 401 | } |
| 402 | PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | PyDoc_STRVAR(remove_doc, |
| 407 | "D.remove(value) -- remove first occurrence of value."); |
| 408 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 409 | static int |
| 410 | deque_clear(dequeobject *deque) |
| 411 | { |
| 412 | PyObject *item; |
| 413 | |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 414 | while (deque->len) { |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 415 | item = deque_pop(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 416 | assert (item != NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 417 | Py_DECREF(item); |
| 418 | } |
| 419 | assert(deque->leftblock == deque->rightblock && |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 420 | deque->leftindex - 1 == deque->rightindex && |
| 421 | deque->len == 0); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static PyObject * |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 426 | deque_item(dequeobject *deque, int i) |
| 427 | { |
| 428 | block *b; |
| 429 | PyObject *item; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 430 | int n, index=i; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 431 | |
| 432 | if (i < 0 || i >= deque->len) { |
| 433 | PyErr_SetString(PyExc_IndexError, |
| 434 | "deque index out of range"); |
| 435 | return NULL; |
| 436 | } |
| 437 | |
Raymond Hettinger | 6c79a51 | 2004-03-04 08:00:54 +0000 | [diff] [blame] | 438 | if (i == 0) { |
| 439 | i = deque->leftindex; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 440 | b = deque->leftblock; |
Raymond Hettinger | 6c79a51 | 2004-03-04 08:00:54 +0000 | [diff] [blame] | 441 | } else if (i == deque->len - 1) { |
| 442 | i = deque->rightindex; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 443 | b = deque->rightblock; |
Raymond Hettinger | 6c79a51 | 2004-03-04 08:00:54 +0000 | [diff] [blame] | 444 | } else { |
| 445 | i += deque->leftindex; |
| 446 | n = i / BLOCKLEN; |
| 447 | i %= BLOCKLEN; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 448 | if (index < (deque->len >> 1)) { |
Raymond Hettinger | 6c79a51 | 2004-03-04 08:00:54 +0000 | [diff] [blame] | 449 | b = deque->leftblock; |
| 450 | while (n--) |
| 451 | b = b->rightlink; |
| 452 | } else { |
| 453 | n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n; |
| 454 | b = deque->rightblock; |
| 455 | while (n--) |
| 456 | b = b->leftlink; |
| 457 | } |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 458 | } |
| 459 | item = b->data[i]; |
| 460 | Py_INCREF(item); |
| 461 | return item; |
| 462 | } |
| 463 | |
Raymond Hettinger | 616f4f6 | 2004-06-26 04:42:06 +0000 | [diff] [blame] | 464 | /* delitem() implemented in terms of rotate for simplicity and reasonable |
| 465 | performance near the end points. If for some reason this method becomes |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 466 | popular, it is not hard to re-implement this using direct data movement |
Raymond Hettinger | 616f4f6 | 2004-06-26 04:42:06 +0000 | [diff] [blame] | 467 | (similar to code in list slice assignment) and achieve a two or threefold |
| 468 | performance boost. |
| 469 | */ |
| 470 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 471 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 472 | deque_del_item(dequeobject *deque, Py_ssize_t i) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 473 | { |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 474 | PyObject *item; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 475 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 476 | assert (i >= 0 && i < deque->len); |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 477 | if (_deque_rotate(deque, -i) == -1) |
| 478 | return -1; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 479 | |
| 480 | item = deque_popleft(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 481 | assert (item != NULL); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 482 | Py_DECREF(item); |
| 483 | |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 484 | return _deque_rotate(deque, i); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 488 | deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 489 | { |
| 490 | PyObject *old_value; |
| 491 | block *b; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 492 | Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 493 | |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 494 | if (i < 0 || i >= len) { |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 495 | PyErr_SetString(PyExc_IndexError, |
| 496 | "deque index out of range"); |
| 497 | return -1; |
| 498 | } |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 499 | if (v == NULL) |
| 500 | return deque_del_item(deque, i); |
| 501 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 502 | i += deque->leftindex; |
| 503 | n = i / BLOCKLEN; |
| 504 | i %= BLOCKLEN; |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 505 | if (index <= halflen) { |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 506 | b = deque->leftblock; |
| 507 | while (n--) |
| 508 | b = b->rightlink; |
| 509 | } else { |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 510 | n = (deque->leftindex + len - 1) / BLOCKLEN - n; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 511 | b = deque->rightblock; |
| 512 | while (n--) |
| 513 | b = b->leftlink; |
| 514 | } |
| 515 | Py_INCREF(v); |
| 516 | old_value = b->data[i]; |
| 517 | b->data[i] = v; |
| 518 | Py_DECREF(old_value); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static PyObject * |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 523 | deque_clearmethod(dequeobject *deque) |
| 524 | { |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 525 | int rv; |
| 526 | |
| 527 | rv = deque_clear(deque); |
| 528 | assert (rv != -1); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 529 | Py_RETURN_NONE; |
| 530 | } |
| 531 | |
| 532 | PyDoc_STRVAR(clear_doc, "Remove all elements from the deque."); |
| 533 | |
| 534 | static void |
| 535 | deque_dealloc(dequeobject *deque) |
| 536 | { |
| 537 | PyObject_GC_UnTrack(deque); |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 538 | if (deque->weakreflist != NULL) |
| 539 | PyObject_ClearWeakRefs((PyObject *) deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 540 | if (deque->leftblock != NULL) { |
Raymond Hettinger | e9c89e8 | 2004-07-19 00:10:24 +0000 | [diff] [blame] | 541 | deque_clear(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 542 | assert(deque->leftblock != NULL); |
| 543 | PyMem_Free(deque->leftblock); |
| 544 | } |
| 545 | deque->leftblock = NULL; |
| 546 | deque->rightblock = NULL; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 547 | Py_Type(deque)->tp_free(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static int |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 551 | deque_traverse(dequeobject *deque, visitproc visit, void *arg) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 552 | { |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 553 | block *b; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 554 | PyObject *item; |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 555 | int index; |
| 556 | int indexlo = deque->leftindex; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 557 | |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 558 | for (b = deque->leftblock; b != NULL; b = b->rightlink) { |
| 559 | const int indexhi = b == deque->rightblock ? |
| 560 | deque->rightindex : |
| 561 | BLOCKLEN - 1; |
| 562 | |
| 563 | for (index = indexlo; index <= indexhi; ++index) { |
| 564 | item = b->data[index]; |
| 565 | Py_VISIT(item); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 566 | } |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 567 | indexlo = 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static long |
| 573 | deque_nohash(PyObject *self) |
| 574 | { |
| 575 | PyErr_SetString(PyExc_TypeError, "deque objects are unhashable"); |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | static PyObject * |
| 580 | deque_copy(PyObject *deque) |
| 581 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 582 | return PyObject_CallFunctionObjArgs((PyObject *)(Py_Type(deque)), |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 583 | deque, NULL); |
| 584 | } |
| 585 | |
| 586 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque."); |
| 587 | |
| 588 | static PyObject * |
| 589 | deque_reduce(dequeobject *deque) |
| 590 | { |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 591 | PyObject *dict, *result, *it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 592 | |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 593 | dict = PyObject_GetAttrString((PyObject *)deque, "__dict__"); |
| 594 | if (dict == NULL) { |
| 595 | PyErr_Clear(); |
| 596 | dict = Py_None; |
| 597 | Py_INCREF(dict); |
| 598 | } |
| 599 | it = PyObject_GetIter((PyObject *)deque); |
| 600 | if (it == NULL) { |
| 601 | Py_DECREF(dict); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 602 | return NULL; |
| 603 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 604 | result = Py_BuildValue("O()ON", Py_Type(deque), dict, it); |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 605 | Py_DECREF(dict); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 606 | return result; |
| 607 | } |
| 608 | |
| 609 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 610 | |
| 611 | static PyObject * |
| 612 | deque_repr(PyObject *deque) |
| 613 | { |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 614 | PyObject *aslist, *result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 615 | int i; |
| 616 | |
| 617 | i = Py_ReprEnter(deque); |
| 618 | if (i != 0) { |
| 619 | if (i < 0) |
| 620 | return NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 621 | return PyUnicode_FromString("[...]"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | aslist = PySequence_List(deque); |
| 625 | if (aslist == NULL) { |
| 626 | Py_ReprLeave(deque); |
| 627 | return NULL; |
| 628 | } |
| 629 | |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 630 | result = PyUnicode_FromFormat("deque(%R)", aslist); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 631 | Py_DECREF(aslist); |
| 632 | Py_ReprLeave(deque); |
| 633 | return result; |
| 634 | } |
| 635 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 636 | static PyObject * |
| 637 | deque_richcompare(PyObject *v, PyObject *w, int op) |
| 638 | { |
| 639 | PyObject *it1=NULL, *it2=NULL, *x, *y; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 640 | int b, vs, ws, cmp=-1; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 641 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 642 | if (!PyObject_TypeCheck(v, &deque_type) || |
Raymond Hettinger | 285cfcc | 2004-05-18 18:15:03 +0000 | [diff] [blame] | 643 | !PyObject_TypeCheck(w, &deque_type)) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 644 | Py_INCREF(Py_NotImplemented); |
| 645 | return Py_NotImplemented; |
| 646 | } |
| 647 | |
| 648 | /* Shortcuts */ |
| 649 | vs = ((dequeobject *)v)->len; |
| 650 | ws = ((dequeobject *)w)->len; |
| 651 | if (op == Py_EQ) { |
| 652 | if (v == w) |
| 653 | Py_RETURN_TRUE; |
| 654 | if (vs != ws) |
| 655 | Py_RETURN_FALSE; |
| 656 | } |
| 657 | if (op == Py_NE) { |
| 658 | if (v == w) |
| 659 | Py_RETURN_FALSE; |
| 660 | if (vs != ws) |
| 661 | Py_RETURN_TRUE; |
| 662 | } |
| 663 | |
| 664 | /* Search for the first index where items are different */ |
| 665 | it1 = PyObject_GetIter(v); |
| 666 | if (it1 == NULL) |
| 667 | goto done; |
| 668 | it2 = PyObject_GetIter(w); |
| 669 | if (it2 == NULL) |
| 670 | goto done; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 671 | for (;;) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 672 | x = PyIter_Next(it1); |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 673 | if (x == NULL && PyErr_Occurred()) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 674 | goto done; |
| 675 | y = PyIter_Next(it2); |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 676 | if (x == NULL || y == NULL) |
| 677 | break; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 678 | b = PyObject_RichCompareBool(x, y, Py_EQ); |
| 679 | if (b == 0) { |
| 680 | cmp = PyObject_RichCompareBool(x, y, op); |
| 681 | Py_DECREF(x); |
| 682 | Py_DECREF(y); |
| 683 | goto done; |
| 684 | } |
| 685 | Py_DECREF(x); |
| 686 | Py_DECREF(y); |
| 687 | if (b == -1) |
| 688 | goto done; |
| 689 | } |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 690 | /* We reached the end of one deque or both */ |
| 691 | Py_XDECREF(x); |
| 692 | Py_XDECREF(y); |
| 693 | if (PyErr_Occurred()) |
| 694 | goto done; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 695 | switch (op) { |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 696 | case Py_LT: cmp = y != NULL; break; /* if w was longer */ |
| 697 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */ |
| 698 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ |
| 699 | case Py_NE: cmp = x != y; break; /* if one deque continues */ |
| 700 | case Py_GT: cmp = x != NULL; break; /* if v was longer */ |
| 701 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */ |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 702 | } |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 703 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 704 | done: |
| 705 | Py_XDECREF(it1); |
| 706 | Py_XDECREF(it2); |
| 707 | if (cmp == 1) |
| 708 | Py_RETURN_TRUE; |
| 709 | if (cmp == 0) |
| 710 | Py_RETURN_FALSE; |
| 711 | return NULL; |
| 712 | } |
| 713 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 714 | static int |
| 715 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwds) |
| 716 | { |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 717 | PyObject *iterable = NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 718 | |
| 719 | if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable)) |
| 720 | return -1; |
| 721 | |
| 722 | if (iterable != NULL) { |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 723 | PyObject *rv = deque_extend(deque, iterable); |
| 724 | if (rv == NULL) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 725 | return -1; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 726 | Py_DECREF(rv); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 727 | } |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static PySequenceMethods deque_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 732 | (lenfunc)deque_len, /* sq_length */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 733 | 0, /* sq_concat */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 734 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 735 | (ssizeargfunc)deque_item, /* sq_item */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 736 | 0, /* sq_slice */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 737 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 738 | }; |
| 739 | |
| 740 | /* deque object ********************************************************/ |
| 741 | |
| 742 | static PyObject *deque_iter(dequeobject *deque); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 743 | static PyObject *deque_reviter(dequeobject *deque); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 744 | PyDoc_STRVAR(reversed_doc, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 745 | "D.__reversed__() -- return a reverse iterator over the deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 746 | |
| 747 | static PyMethodDef deque_methods[] = { |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 748 | {"append", (PyCFunction)deque_append, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 749 | METH_O, append_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 750 | {"appendleft", (PyCFunction)deque_appendleft, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 751 | METH_O, appendleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 752 | {"clear", (PyCFunction)deque_clearmethod, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 753 | METH_NOARGS, clear_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 754 | {"__copy__", (PyCFunction)deque_copy, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 755 | METH_NOARGS, copy_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 756 | {"extend", (PyCFunction)deque_extend, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 757 | METH_O, extend_doc}, |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 758 | {"extendleft", (PyCFunction)deque_extendleft, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 759 | METH_O, extendleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 760 | {"pop", (PyCFunction)deque_pop, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 761 | METH_NOARGS, pop_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 762 | {"popleft", (PyCFunction)deque_popleft, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 763 | METH_NOARGS, popleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 764 | {"__reduce__", (PyCFunction)deque_reduce, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 765 | METH_NOARGS, reduce_doc}, |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 766 | {"remove", (PyCFunction)deque_remove, |
| 767 | METH_O, remove_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 768 | {"__reversed__", (PyCFunction)deque_reviter, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 769 | METH_NOARGS, reversed_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 770 | {"rotate", (PyCFunction)deque_rotate, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 771 | METH_VARARGS, rotate_doc}, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 772 | {NULL, NULL} /* sentinel */ |
| 773 | }; |
| 774 | |
| 775 | PyDoc_STRVAR(deque_doc, |
| 776 | "deque(iterable) --> deque object\n\ |
| 777 | \n\ |
| 778 | Build an ordered collection accessible from endpoints only."); |
| 779 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 780 | static PyTypeObject deque_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 781 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 782 | "collections.deque", /* tp_name */ |
| 783 | sizeof(dequeobject), /* tp_basicsize */ |
| 784 | 0, /* tp_itemsize */ |
| 785 | /* methods */ |
| 786 | (destructor)deque_dealloc, /* tp_dealloc */ |
Guido van Rossum | 346f1a8 | 2007-08-07 19:58:47 +0000 | [diff] [blame] | 787 | 0, /* tp_print */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 788 | 0, /* tp_getattr */ |
| 789 | 0, /* tp_setattr */ |
| 790 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 791 | deque_repr, /* tp_repr */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 792 | 0, /* tp_as_number */ |
| 793 | &deque_as_sequence, /* tp_as_sequence */ |
| 794 | 0, /* tp_as_mapping */ |
| 795 | deque_nohash, /* tp_hash */ |
| 796 | 0, /* tp_call */ |
| 797 | 0, /* tp_str */ |
| 798 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 799 | 0, /* tp_setattro */ |
| 800 | 0, /* tp_as_buffer */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 801 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 802 | /* tp_flags */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 803 | deque_doc, /* tp_doc */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 804 | (traverseproc)deque_traverse, /* tp_traverse */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 805 | (inquiry)deque_clear, /* tp_clear */ |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 806 | (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 807 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 808 | (getiterfunc)deque_iter, /* tp_iter */ |
| 809 | 0, /* tp_iternext */ |
| 810 | deque_methods, /* tp_methods */ |
| 811 | 0, /* tp_members */ |
| 812 | 0, /* tp_getset */ |
| 813 | 0, /* tp_base */ |
| 814 | 0, /* tp_dict */ |
| 815 | 0, /* tp_descr_get */ |
| 816 | 0, /* tp_descr_set */ |
| 817 | 0, /* tp_dictoffset */ |
| 818 | (initproc)deque_init, /* tp_init */ |
| 819 | PyType_GenericAlloc, /* tp_alloc */ |
| 820 | deque_new, /* tp_new */ |
| 821 | PyObject_GC_Del, /* tp_free */ |
| 822 | }; |
| 823 | |
| 824 | /*********************** Deque Iterator **************************/ |
| 825 | |
| 826 | typedef struct { |
| 827 | PyObject_HEAD |
| 828 | int index; |
| 829 | block *b; |
| 830 | dequeobject *deque; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 831 | long state; /* state when the iterator is created */ |
| 832 | int counter; /* number of items remaining for iteration */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 833 | } dequeiterobject; |
| 834 | |
| 835 | PyTypeObject dequeiter_type; |
| 836 | |
| 837 | static PyObject * |
| 838 | deque_iter(dequeobject *deque) |
| 839 | { |
| 840 | dequeiterobject *it; |
| 841 | |
| 842 | it = PyObject_New(dequeiterobject, &dequeiter_type); |
| 843 | if (it == NULL) |
| 844 | return NULL; |
| 845 | it->b = deque->leftblock; |
| 846 | it->index = deque->leftindex; |
| 847 | Py_INCREF(deque); |
| 848 | it->deque = deque; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 849 | it->state = deque->state; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 850 | it->counter = deque->len; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 851 | return (PyObject *)it; |
| 852 | } |
| 853 | |
| 854 | static void |
| 855 | dequeiter_dealloc(dequeiterobject *dio) |
| 856 | { |
| 857 | Py_XDECREF(dio->deque); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 858 | Py_Type(dio)->tp_free(dio); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static PyObject * |
| 862 | dequeiter_next(dequeiterobject *it) |
| 863 | { |
| 864 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 865 | |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 866 | if (it->deque->state != it->state) { |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 867 | it->counter = 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 868 | PyErr_SetString(PyExc_RuntimeError, |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 869 | "deque mutated during iteration"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 870 | return NULL; |
| 871 | } |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 872 | if (it->counter == 0) |
| 873 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 874 | assert (!(it->b == it->deque->rightblock && |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 875 | it->index > it->deque->rightindex)); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 876 | |
| 877 | item = it->b->data[it->index]; |
| 878 | it->index++; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 879 | it->counter--; |
| 880 | if (it->index == BLOCKLEN && it->counter > 0) { |
| 881 | assert (it->b->rightlink != NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 882 | it->b = it->b->rightlink; |
| 883 | it->index = 0; |
| 884 | } |
| 885 | Py_INCREF(item); |
| 886 | return item; |
| 887 | } |
| 888 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 889 | static PyObject * |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 890 | dequeiter_len(dequeiterobject *it) |
| 891 | { |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 892 | return PyInt_FromLong(it->counter); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 895 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 896 | |
| 897 | static PyMethodDef dequeiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 898 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 899 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 900 | }; |
| 901 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 902 | PyTypeObject dequeiter_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 903 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 904 | "deque_iterator", /* tp_name */ |
| 905 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 906 | 0, /* tp_itemsize */ |
| 907 | /* methods */ |
| 908 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 909 | 0, /* tp_print */ |
| 910 | 0, /* tp_getattr */ |
| 911 | 0, /* tp_setattr */ |
| 912 | 0, /* tp_compare */ |
| 913 | 0, /* tp_repr */ |
| 914 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 915 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 916 | 0, /* tp_as_mapping */ |
| 917 | 0, /* tp_hash */ |
| 918 | 0, /* tp_call */ |
| 919 | 0, /* tp_str */ |
| 920 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 921 | 0, /* tp_setattro */ |
| 922 | 0, /* tp_as_buffer */ |
| 923 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 924 | 0, /* tp_doc */ |
| 925 | 0, /* tp_traverse */ |
| 926 | 0, /* tp_clear */ |
| 927 | 0, /* tp_richcompare */ |
| 928 | 0, /* tp_weaklistoffset */ |
| 929 | PyObject_SelfIter, /* tp_iter */ |
| 930 | (iternextfunc)dequeiter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 931 | dequeiter_methods, /* tp_methods */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 932 | 0, |
| 933 | }; |
| 934 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 935 | /*********************** Deque Reverse Iterator **************************/ |
| 936 | |
| 937 | PyTypeObject dequereviter_type; |
| 938 | |
| 939 | static PyObject * |
| 940 | deque_reviter(dequeobject *deque) |
| 941 | { |
| 942 | dequeiterobject *it; |
| 943 | |
| 944 | it = PyObject_New(dequeiterobject, &dequereviter_type); |
| 945 | if (it == NULL) |
| 946 | return NULL; |
| 947 | it->b = deque->rightblock; |
| 948 | it->index = deque->rightindex; |
| 949 | Py_INCREF(deque); |
| 950 | it->deque = deque; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 951 | it->state = deque->state; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 952 | it->counter = deque->len; |
| 953 | return (PyObject *)it; |
| 954 | } |
| 955 | |
| 956 | static PyObject * |
| 957 | dequereviter_next(dequeiterobject *it) |
| 958 | { |
| 959 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 960 | if (it->counter == 0) |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 961 | return NULL; |
| 962 | |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 963 | if (it->deque->state != it->state) { |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 964 | it->counter = 0; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 965 | PyErr_SetString(PyExc_RuntimeError, |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 966 | "deque mutated during iteration"); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 967 | return NULL; |
| 968 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 969 | assert (!(it->b == it->deque->leftblock && |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 970 | it->index < it->deque->leftindex)); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 971 | |
| 972 | item = it->b->data[it->index]; |
| 973 | it->index--; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 974 | it->counter--; |
| 975 | if (it->index == -1 && it->counter > 0) { |
| 976 | assert (it->b->leftlink != NULL); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 977 | it->b = it->b->leftlink; |
| 978 | it->index = BLOCKLEN - 1; |
| 979 | } |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 980 | Py_INCREF(item); |
| 981 | return item; |
| 982 | } |
| 983 | |
| 984 | PyTypeObject dequereviter_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 985 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 986 | "deque_reverse_iterator", /* tp_name */ |
| 987 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 988 | 0, /* tp_itemsize */ |
| 989 | /* methods */ |
| 990 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 991 | 0, /* tp_print */ |
| 992 | 0, /* tp_getattr */ |
| 993 | 0, /* tp_setattr */ |
| 994 | 0, /* tp_compare */ |
| 995 | 0, /* tp_repr */ |
| 996 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 997 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 998 | 0, /* tp_as_mapping */ |
| 999 | 0, /* tp_hash */ |
| 1000 | 0, /* tp_call */ |
| 1001 | 0, /* tp_str */ |
| 1002 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1003 | 0, /* tp_setattro */ |
| 1004 | 0, /* tp_as_buffer */ |
| 1005 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1006 | 0, /* tp_doc */ |
| 1007 | 0, /* tp_traverse */ |
| 1008 | 0, /* tp_clear */ |
| 1009 | 0, /* tp_richcompare */ |
| 1010 | 0, /* tp_weaklistoffset */ |
| 1011 | PyObject_SelfIter, /* tp_iter */ |
| 1012 | (iternextfunc)dequereviter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1013 | dequeiter_methods, /* tp_methods */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1014 | 0, |
| 1015 | }; |
| 1016 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1017 | /* defaultdict type *********************************************************/ |
| 1018 | |
| 1019 | typedef struct { |
| 1020 | PyDictObject dict; |
| 1021 | PyObject *default_factory; |
| 1022 | } defdictobject; |
| 1023 | |
| 1024 | static PyTypeObject defdict_type; /* Forward */ |
| 1025 | |
| 1026 | PyDoc_STRVAR(defdict_missing_doc, |
| 1027 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1028 | if self.default_factory is None: raise KeyError((key,))\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1029 | self[key] = value = self.default_factory()\n\ |
| 1030 | return value\n\ |
| 1031 | "); |
| 1032 | |
| 1033 | static PyObject * |
| 1034 | defdict_missing(defdictobject *dd, PyObject *key) |
| 1035 | { |
| 1036 | PyObject *factory = dd->default_factory; |
| 1037 | PyObject *value; |
| 1038 | if (factory == NULL || factory == Py_None) { |
| 1039 | /* XXX Call dict.__missing__(key) */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1040 | PyObject *tup; |
| 1041 | tup = PyTuple_Pack(1, key); |
| 1042 | if (!tup) return NULL; |
| 1043 | PyErr_SetObject(PyExc_KeyError, tup); |
| 1044 | Py_DECREF(tup); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1045 | return NULL; |
| 1046 | } |
| 1047 | value = PyEval_CallObject(factory, NULL); |
| 1048 | if (value == NULL) |
| 1049 | return value; |
| 1050 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) { |
| 1051 | Py_DECREF(value); |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | return value; |
| 1055 | } |
| 1056 | |
| 1057 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D."); |
| 1058 | |
| 1059 | static PyObject * |
| 1060 | defdict_copy(defdictobject *dd) |
| 1061 | { |
| 1062 | /* This calls the object's class. That only works for subclasses |
| 1063 | whose class constructor has the same signature. Subclasses that |
| 1064 | define a different constructor signature must override copy(). |
| 1065 | */ |
Guido van Rossum | 2f2fffb | 2007-07-25 16:47:51 +0000 | [diff] [blame] | 1066 | return PyObject_CallFunctionObjArgs((PyObject *)Py_Type(dd), |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1067 | dd->default_factory, dd, NULL); |
| 1068 | } |
| 1069 | |
| 1070 | static PyObject * |
| 1071 | defdict_reduce(defdictobject *dd) |
| 1072 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1073 | /* __reduce__ must return a 5-tuple as follows: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1074 | |
| 1075 | - factory function |
| 1076 | - tuple of args for the factory function |
| 1077 | - additional state (here None) |
| 1078 | - sequence iterator (here None) |
| 1079 | - dictionary iterator (yielding successive (key, value) pairs |
| 1080 | |
| 1081 | This API is used by pickle.py and copy.py. |
| 1082 | |
| 1083 | For this to be useful with pickle.py, the default_factory |
| 1084 | must be picklable; e.g., None, a built-in, or a global |
| 1085 | function in a module or package. |
| 1086 | |
| 1087 | Both shallow and deep copying are supported, but for deep |
| 1088 | copying, the default_factory must be deep-copyable; e.g. None, |
| 1089 | or a built-in (functions are not copyable at this time). |
| 1090 | |
| 1091 | This only works for subclasses as long as their constructor |
| 1092 | signature is compatible; the first argument must be the |
| 1093 | optional default_factory, defaulting to None. |
| 1094 | */ |
| 1095 | PyObject *args; |
| 1096 | PyObject *items; |
| 1097 | PyObject *result; |
| 1098 | if (dd->default_factory == NULL || dd->default_factory == Py_None) |
| 1099 | args = PyTuple_New(0); |
| 1100 | else |
| 1101 | args = PyTuple_Pack(1, dd->default_factory); |
| 1102 | if (args == NULL) |
| 1103 | return NULL; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1104 | items = PyObject_CallMethod((PyObject *)dd, "items", "()"); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1105 | if (items == NULL) { |
| 1106 | Py_DECREF(args); |
| 1107 | return NULL; |
| 1108 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1109 | result = PyTuple_Pack(5, Py_Type(dd), args, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1110 | Py_None, Py_None, items); |
| 1111 | Py_DECREF(items); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1112 | Py_DECREF(args); |
| 1113 | return result; |
| 1114 | } |
| 1115 | |
| 1116 | static PyMethodDef defdict_methods[] = { |
| 1117 | {"__missing__", (PyCFunction)defdict_missing, METH_O, |
| 1118 | defdict_missing_doc}, |
| 1119 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1120 | defdict_copy_doc}, |
| 1121 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1122 | defdict_copy_doc}, |
| 1123 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS, |
| 1124 | reduce_doc}, |
| 1125 | {NULL} |
| 1126 | }; |
| 1127 | |
| 1128 | static PyMemberDef defdict_members[] = { |
| 1129 | {"default_factory", T_OBJECT, |
| 1130 | offsetof(defdictobject, default_factory), 0, |
| 1131 | PyDoc_STR("Factory for default value called by __missing__().")}, |
| 1132 | {NULL} |
| 1133 | }; |
| 1134 | |
| 1135 | static void |
| 1136 | defdict_dealloc(defdictobject *dd) |
| 1137 | { |
| 1138 | Py_CLEAR(dd->default_factory); |
| 1139 | PyDict_Type.tp_dealloc((PyObject *)dd); |
| 1140 | } |
| 1141 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1142 | static PyObject * |
| 1143 | defdict_repr(defdictobject *dd) |
| 1144 | { |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1145 | PyObject *baserepr; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1146 | PyObject *def; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1147 | PyObject *result; |
| 1148 | baserepr = PyDict_Type.tp_repr((PyObject *)dd); |
| 1149 | if (baserepr == NULL) |
| 1150 | return NULL; |
| 1151 | if (dd->default_factory == NULL) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1152 | def = Py_None; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1153 | else |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1154 | def = dd->default_factory; |
| 1155 | result = PyUnicode_FromFormat("defaultdict(%R, %U)", def, baserepr); |
| 1156 | Py_DECREF(baserepr); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1157 | return result; |
| 1158 | } |
| 1159 | |
| 1160 | static int |
| 1161 | defdict_traverse(PyObject *self, visitproc visit, void *arg) |
| 1162 | { |
| 1163 | Py_VISIT(((defdictobject *)self)->default_factory); |
| 1164 | return PyDict_Type.tp_traverse(self, visit, arg); |
| 1165 | } |
| 1166 | |
| 1167 | static int |
| 1168 | defdict_tp_clear(defdictobject *dd) |
| 1169 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1170 | Py_CLEAR(dd->default_factory); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1171 | return PyDict_Type.tp_clear((PyObject *)dd); |
| 1172 | } |
| 1173 | |
| 1174 | static int |
| 1175 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1176 | { |
| 1177 | defdictobject *dd = (defdictobject *)self; |
| 1178 | PyObject *olddefault = dd->default_factory; |
| 1179 | PyObject *newdefault = NULL; |
| 1180 | PyObject *newargs; |
| 1181 | int result; |
| 1182 | if (args == NULL || !PyTuple_Check(args)) |
| 1183 | newargs = PyTuple_New(0); |
| 1184 | else { |
| 1185 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1186 | if (n > 0) { |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1187 | newdefault = PyTuple_GET_ITEM(args, 0); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1188 | if (!PyCallable_Check(newdefault)) { |
| 1189 | PyErr_SetString(PyExc_TypeError, |
| 1190 | "first argument must be callable"); |
| 1191 | return -1; |
| 1192 | } |
| 1193 | } |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1194 | newargs = PySequence_GetSlice(args, 1, n); |
| 1195 | } |
| 1196 | if (newargs == NULL) |
| 1197 | return -1; |
| 1198 | Py_XINCREF(newdefault); |
| 1199 | dd->default_factory = newdefault; |
| 1200 | result = PyDict_Type.tp_init(self, newargs, kwds); |
| 1201 | Py_DECREF(newargs); |
| 1202 | Py_XDECREF(olddefault); |
| 1203 | return result; |
| 1204 | } |
| 1205 | |
| 1206 | PyDoc_STRVAR(defdict_doc, |
| 1207 | "defaultdict(default_factory) --> dict with default factory\n\ |
| 1208 | \n\ |
| 1209 | The default factory is called without arguments to produce\n\ |
| 1210 | a new value when a key is not present, in __getitem__ only.\n\ |
| 1211 | A defaultdict compares equal to a dict with the same items.\n\ |
| 1212 | "); |
| 1213 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1214 | /* See comment in xxsubtype.c */ |
| 1215 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 1216 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1217 | static PyTypeObject defdict_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1218 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1219 | "collections.defaultdict", /* tp_name */ |
| 1220 | sizeof(defdictobject), /* tp_basicsize */ |
| 1221 | 0, /* tp_itemsize */ |
| 1222 | /* methods */ |
| 1223 | (destructor)defdict_dealloc, /* tp_dealloc */ |
Guido van Rossum | 346f1a8 | 2007-08-07 19:58:47 +0000 | [diff] [blame] | 1224 | 0, /* tp_print */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1225 | 0, /* tp_getattr */ |
| 1226 | 0, /* tp_setattr */ |
| 1227 | 0, /* tp_compare */ |
| 1228 | (reprfunc)defdict_repr, /* tp_repr */ |
| 1229 | 0, /* tp_as_number */ |
| 1230 | 0, /* tp_as_sequence */ |
| 1231 | 0, /* tp_as_mapping */ |
| 1232 | 0, /* tp_hash */ |
| 1233 | 0, /* tp_call */ |
| 1234 | 0, /* tp_str */ |
| 1235 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1236 | 0, /* tp_setattro */ |
| 1237 | 0, /* tp_as_buffer */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1238 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1239 | /* tp_flags */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1240 | defdict_doc, /* tp_doc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1241 | defdict_traverse, /* tp_traverse */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1242 | (inquiry)defdict_tp_clear, /* tp_clear */ |
| 1243 | 0, /* tp_richcompare */ |
| 1244 | 0, /* tp_weaklistoffset*/ |
| 1245 | 0, /* tp_iter */ |
| 1246 | 0, /* tp_iternext */ |
| 1247 | defdict_methods, /* tp_methods */ |
| 1248 | defdict_members, /* tp_members */ |
| 1249 | 0, /* tp_getset */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1250 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1251 | 0, /* tp_dict */ |
| 1252 | 0, /* tp_descr_get */ |
| 1253 | 0, /* tp_descr_set */ |
| 1254 | 0, /* tp_dictoffset */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1255 | defdict_init, /* tp_init */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1256 | PyType_GenericAlloc, /* tp_alloc */ |
| 1257 | 0, /* tp_new */ |
| 1258 | PyObject_GC_Del, /* tp_free */ |
| 1259 | }; |
| 1260 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1261 | /* module level code ********************************************************/ |
| 1262 | |
| 1263 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1264 | "High performance data structures.\n\ |
| 1265 | - deque: ordered collection accessible from endpoints only\n\ |
| 1266 | - defaultdict: dict subclass with a default value factory\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1267 | "); |
| 1268 | |
| 1269 | PyMODINIT_FUNC |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1270 | init_collections(void) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1271 | { |
| 1272 | PyObject *m; |
| 1273 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1274 | m = Py_InitModule3("_collections", NULL, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1275 | if (m == NULL) |
| 1276 | return; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1277 | |
| 1278 | if (PyType_Ready(&deque_type) < 0) |
| 1279 | return; |
| 1280 | Py_INCREF(&deque_type); |
| 1281 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
| 1282 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1283 | defdict_type.tp_base = &PyDict_Type; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1284 | if (PyType_Ready(&defdict_type) < 0) |
| 1285 | return; |
| 1286 | Py_INCREF(&defdict_type); |
| 1287 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); |
| 1288 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1289 | if (PyType_Ready(&dequeiter_type) < 0) |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 1290 | return; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1291 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1292 | if (PyType_Ready(&dequereviter_type) < 0) |
| 1293 | return; |
| 1294 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1295 | return; |
| 1296 | } |