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 |
Benjamin Peterson | 227f0fa | 2013-06-25 11:34:48 -0700 | [diff] [blame] | 11 | * reduce the number of calls to the memory allocator, give faster |
| 12 | * indexing and rotation, and reduce the link::data overhead ratio. |
| 13 | * Ideally, the block length should be a power-of-two for faster |
| 14 | * division/modulo computations during indexing. |
Raymond Hettinger | 77e8bf1 | 2004-10-01 15:25:53 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Benjamin Peterson | 227f0fa | 2013-06-25 11:34:48 -0700 | [diff] [blame] | 17 | #define BLOCKLEN 64 |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 18 | #define CENTER ((BLOCKLEN - 1) / 2) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 19 | |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 20 | /* A `dequeobject` is composed of a doubly-linked list of `block` nodes. |
| 21 | * This list is not circular (the leftmost block has leftlink==NULL, |
| 22 | * and the rightmost block has rightlink==NULL). A deque d's first |
| 23 | * element is at d.leftblock[leftindex] and its last element is at |
| 24 | * d.rightblock[rightindex]; note that, unlike as for Python slice |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 25 | * indices, these indices are inclusive on both ends. By being inclusive |
Tim Peters | 5566e96 | 2006-07-28 00:23:15 +0000 | [diff] [blame] | 26 | * on both ends, algorithms for left and right operations become |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 27 | * symmetrical which simplifies the design. |
Tim Peters | 5566e96 | 2006-07-28 00:23:15 +0000 | [diff] [blame] | 28 | * |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 29 | * The list of blocks is never empty, so d.leftblock and d.rightblock |
| 30 | * are never equal to NULL. |
| 31 | * |
| 32 | * The indices, d.leftindex and d.rightindex are always in the range |
| 33 | * 0 <= index < BLOCKLEN. |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 34 | * Their exact relationship is: |
| 35 | * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex. |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 36 | * |
| 37 | * Empty deques have d.len == 0; d.leftblock==d.rightblock; |
| 38 | * d.leftindex == CENTER+1; and d.rightindex == CENTER. |
| 39 | * Checking for d.len == 0 is the intended way to see whether d is empty. |
| 40 | * |
Tim Peters | 5566e96 | 2006-07-28 00:23:15 +0000 | [diff] [blame] | 41 | * Whenever d.leftblock == d.rightblock, |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 42 | * d.leftindex + d.len - 1 == d.rightindex. |
Tim Peters | 5566e96 | 2006-07-28 00:23:15 +0000 | [diff] [blame] | 43 | * |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 44 | * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex |
Tim Peters | 5566e96 | 2006-07-28 00:23:15 +0000 | [diff] [blame] | 45 | * become indices into distinct blocks and either may be larger than the |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 46 | * other. |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 47 | */ |
| 48 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 49 | typedef struct BLOCK { |
Benjamin Peterson | 3968e29 | 2013-06-25 11:26:20 -0700 | [diff] [blame] | 50 | PyObject *data[BLOCKLEN]; |
Benjamin Peterson | 73d6aca | 2013-06-25 11:35:44 -0700 | [diff] [blame] | 51 | struct BLOCK *rightlink; |
Raymond Hettinger | 90180c1 | 2013-07-16 01:59:30 -0700 | [diff] [blame] | 52 | struct BLOCK *leftlink; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 53 | } block; |
| 54 | |
Raymond Hettinger | d3ffd34 | 2007-11-10 01:54:03 +0000 | [diff] [blame] | 55 | #define MAXFREEBLOCKS 10 |
Raymond Hettinger | 33fcf9d | 2008-07-24 00:08:18 +0000 | [diff] [blame] | 56 | static Py_ssize_t numfreeblocks = 0; |
Raymond Hettinger | d3ffd34 | 2007-11-10 01:54:03 +0000 | [diff] [blame] | 57 | static block *freeblocks[MAXFREEBLOCKS]; |
| 58 | |
Tim Peters | 6f85356 | 2004-10-01 01:04:50 +0000 | [diff] [blame] | 59 | static block * |
Raymond Hettinger | 33fcf9d | 2008-07-24 00:08:18 +0000 | [diff] [blame] | 60 | newblock(block *leftlink, block *rightlink, Py_ssize_t len) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 61 | block *b; |
Benjamin Peterson | 227f0fa | 2013-06-25 11:34:48 -0700 | [diff] [blame] | 62 | /* To prevent len from overflowing PY_SSIZE_T_MAX on 32-bit machines, we |
| 63 | * refuse to allocate new blocks if the current len is nearing overflow. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 64 | if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) { |
| 65 | PyErr_SetString(PyExc_OverflowError, |
| 66 | "cannot add more blocks to the deque"); |
| 67 | return NULL; |
| 68 | } |
| 69 | if (numfreeblocks) { |
| 70 | numfreeblocks -= 1; |
| 71 | b = freeblocks[numfreeblocks]; |
| 72 | } else { |
| 73 | b = PyMem_Malloc(sizeof(block)); |
| 74 | if (b == NULL) { |
| 75 | PyErr_NoMemory(); |
| 76 | return NULL; |
| 77 | } |
| 78 | } |
| 79 | b->leftlink = leftlink; |
| 80 | b->rightlink = rightlink; |
| 81 | return b; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Martin v. Löwis | 111c180 | 2008-06-13 07:47:47 +0000 | [diff] [blame] | 84 | static void |
Raymond Hettinger | d3ffd34 | 2007-11-10 01:54:03 +0000 | [diff] [blame] | 85 | freeblock(block *b) |
| 86 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 87 | if (numfreeblocks < MAXFREEBLOCKS) { |
| 88 | freeblocks[numfreeblocks] = b; |
| 89 | numfreeblocks++; |
| 90 | } else { |
| 91 | PyMem_Free(b); |
| 92 | } |
Raymond Hettinger | d3ffd34 | 2007-11-10 01:54:03 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 95 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 96 | PyObject_HEAD |
| 97 | block *leftblock; |
| 98 | block *rightblock; |
| 99 | Py_ssize_t leftindex; /* in range(BLOCKLEN) */ |
| 100 | Py_ssize_t rightindex; /* in range(BLOCKLEN) */ |
| 101 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 102 | long state; /* incremented whenever the indices move */ |
Raymond Hettinger | b77ed2c | 2013-07-16 02:34:19 -0700 | [diff] [blame^] | 103 | Py_ssize_t maxlen; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 104 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 105 | } dequeobject; |
| 106 | |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 107 | /* The deque's size limit is d.maxlen. The limit can be zero or positive. |
| 108 | * If there is no limit, then d.maxlen == -1. |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 109 | * |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 110 | * After an item is added to a deque, we check to see if the size has grown past |
| 111 | * the limit. If it has, we get the size back down to the limit by popping an |
| 112 | * item off of the opposite end. The methods that can trigger this are append(), |
| 113 | * appendleft(), extend(), and extendleft(). |
| 114 | */ |
| 115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 116 | #define TRIM(d, popfunction) \ |
| 117 | if (d->maxlen != -1 && d->len > d->maxlen) { \ |
| 118 | PyObject *rv = popfunction(d, NULL); \ |
| 119 | assert(rv != NULL && d->len <= d->maxlen); \ |
| 120 | Py_DECREF(rv); \ |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 123 | static PyTypeObject deque_type; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 124 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 125 | static PyObject * |
| 126 | deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 127 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 128 | dequeobject *deque; |
| 129 | block *b; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 131 | /* create dequeobject structure */ |
| 132 | deque = (dequeobject *)type->tp_alloc(type, 0); |
| 133 | if (deque == NULL) |
| 134 | return NULL; |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 136 | b = newblock(NULL, NULL, 0); |
| 137 | if (b == NULL) { |
| 138 | Py_DECREF(deque); |
| 139 | return NULL; |
| 140 | } |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 142 | assert(BLOCKLEN >= 2); |
| 143 | deque->leftblock = b; |
| 144 | deque->rightblock = b; |
| 145 | deque->leftindex = CENTER + 1; |
| 146 | deque->rightindex = CENTER; |
| 147 | deque->len = 0; |
| 148 | deque->state = 0; |
| 149 | deque->weakreflist = NULL; |
| 150 | deque->maxlen = -1; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 151 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 152 | return (PyObject *)deque; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static PyObject * |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 156 | deque_pop(dequeobject *deque, PyObject *unused) |
| 157 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 158 | PyObject *item; |
| 159 | block *prevblock; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 160 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 161 | if (deque->len == 0) { |
| 162 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
| 163 | return NULL; |
| 164 | } |
| 165 | item = deque->rightblock->data[deque->rightindex]; |
| 166 | deque->rightindex--; |
| 167 | deque->len--; |
| 168 | deque->state++; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 170 | if (deque->rightindex == -1) { |
| 171 | if (deque->len == 0) { |
| 172 | assert(deque->leftblock == deque->rightblock); |
| 173 | assert(deque->leftindex == deque->rightindex+1); |
| 174 | /* re-center instead of freeing a block */ |
| 175 | deque->leftindex = CENTER + 1; |
| 176 | deque->rightindex = CENTER; |
| 177 | } else { |
| 178 | prevblock = deque->rightblock->leftlink; |
| 179 | assert(deque->leftblock != deque->rightblock); |
| 180 | freeblock(deque->rightblock); |
| 181 | prevblock->rightlink = NULL; |
| 182 | deque->rightblock = prevblock; |
| 183 | deque->rightindex = BLOCKLEN - 1; |
| 184 | } |
| 185 | } |
| 186 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element."); |
| 190 | |
| 191 | static PyObject * |
| 192 | deque_popleft(dequeobject *deque, PyObject *unused) |
| 193 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 194 | PyObject *item; |
| 195 | block *prevblock; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 197 | if (deque->len == 0) { |
| 198 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
| 199 | return NULL; |
| 200 | } |
| 201 | assert(deque->leftblock != NULL); |
| 202 | item = deque->leftblock->data[deque->leftindex]; |
| 203 | deque->leftindex++; |
| 204 | deque->len--; |
| 205 | deque->state++; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 207 | if (deque->leftindex == BLOCKLEN) { |
| 208 | if (deque->len == 0) { |
| 209 | assert(deque->leftblock == deque->rightblock); |
| 210 | assert(deque->leftindex == deque->rightindex+1); |
| 211 | /* re-center instead of freeing a block */ |
| 212 | deque->leftindex = CENTER + 1; |
| 213 | deque->rightindex = CENTER; |
| 214 | } else { |
| 215 | assert(deque->leftblock != deque->rightblock); |
| 216 | prevblock = deque->leftblock->rightlink; |
| 217 | freeblock(deque->leftblock); |
| 218 | assert(prevblock != NULL); |
| 219 | prevblock->leftlink = NULL; |
| 220 | deque->leftblock = prevblock; |
| 221 | deque->leftindex = 0; |
| 222 | } |
| 223 | } |
| 224 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element."); |
| 228 | |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 229 | static PyObject * |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 230 | deque_append(dequeobject *deque, PyObject *item) |
| 231 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 232 | deque->state++; |
| 233 | if (deque->rightindex == BLOCKLEN-1) { |
| 234 | block *b = newblock(deque->rightblock, NULL, deque->len); |
| 235 | if (b == NULL) |
| 236 | return NULL; |
| 237 | assert(deque->rightblock->rightlink == NULL); |
| 238 | deque->rightblock->rightlink = b; |
| 239 | deque->rightblock = b; |
| 240 | deque->rightindex = -1; |
| 241 | } |
| 242 | Py_INCREF(item); |
| 243 | deque->len++; |
| 244 | deque->rightindex++; |
| 245 | deque->rightblock->data[deque->rightindex] = item; |
| 246 | TRIM(deque, deque_popleft); |
| 247 | Py_RETURN_NONE; |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque."); |
| 251 | |
| 252 | static PyObject * |
| 253 | deque_appendleft(dequeobject *deque, PyObject *item) |
| 254 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 255 | deque->state++; |
| 256 | if (deque->leftindex == 0) { |
| 257 | block *b = newblock(NULL, deque->leftblock, deque->len); |
| 258 | if (b == NULL) |
| 259 | return NULL; |
| 260 | assert(deque->leftblock->leftlink == NULL); |
| 261 | deque->leftblock->leftlink = b; |
| 262 | deque->leftblock = b; |
| 263 | deque->leftindex = BLOCKLEN; |
| 264 | } |
| 265 | Py_INCREF(item); |
| 266 | deque->len++; |
| 267 | deque->leftindex--; |
| 268 | deque->leftblock->data[deque->leftindex] = item; |
| 269 | TRIM(deque, deque_pop); |
| 270 | Py_RETURN_NONE; |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque."); |
| 274 | |
Raymond Hettinger | bac769b | 2009-03-10 09:31:48 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 276 | /* Run an iterator to exhaustion. Shortcut for |
Raymond Hettinger | bac769b | 2009-03-10 09:31:48 +0000 | [diff] [blame] | 277 | the extend/extendleft methods when maxlen == 0. */ |
| 278 | static PyObject* |
| 279 | consume_iterator(PyObject *it) |
| 280 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 281 | PyObject *item; |
Raymond Hettinger | bac769b | 2009-03-10 09:31:48 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 283 | while ((item = PyIter_Next(it)) != NULL) { |
| 284 | Py_DECREF(item); |
| 285 | } |
| 286 | Py_DECREF(it); |
| 287 | if (PyErr_Occurred()) |
| 288 | return NULL; |
| 289 | Py_RETURN_NONE; |
Raymond Hettinger | bac769b | 2009-03-10 09:31:48 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 292 | static PyObject * |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 293 | deque_extend(dequeobject *deque, PyObject *iterable) |
| 294 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 295 | PyObject *it, *item; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 297 | /* Handle case where id(deque) == id(iterable) */ |
| 298 | if ((PyObject *)deque == iterable) { |
| 299 | PyObject *result; |
| 300 | PyObject *s = PySequence_List(iterable); |
| 301 | if (s == NULL) |
| 302 | return NULL; |
| 303 | result = deque_extend(deque, s); |
| 304 | Py_DECREF(s); |
| 305 | return result; |
| 306 | } |
Raymond Hettinger | 0b3263b | 2009-12-10 06:00:33 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 308 | it = PyObject_GetIter(iterable); |
| 309 | if (it == NULL) |
| 310 | return NULL; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 312 | if (deque->maxlen == 0) |
| 313 | return consume_iterator(it); |
Raymond Hettinger | bac769b | 2009-03-10 09:31:48 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 315 | while ((item = PyIter_Next(it)) != NULL) { |
| 316 | deque->state++; |
| 317 | if (deque->rightindex == BLOCKLEN-1) { |
| 318 | block *b = newblock(deque->rightblock, NULL, |
| 319 | deque->len); |
| 320 | if (b == NULL) { |
| 321 | Py_DECREF(item); |
| 322 | Py_DECREF(it); |
| 323 | return NULL; |
| 324 | } |
| 325 | assert(deque->rightblock->rightlink == NULL); |
| 326 | deque->rightblock->rightlink = b; |
| 327 | deque->rightblock = b; |
| 328 | deque->rightindex = -1; |
| 329 | } |
| 330 | deque->len++; |
| 331 | deque->rightindex++; |
| 332 | deque->rightblock->data[deque->rightindex] = item; |
| 333 | TRIM(deque, deque_popleft); |
| 334 | } |
| 335 | Py_DECREF(it); |
| 336 | if (PyErr_Occurred()) |
| 337 | return NULL; |
| 338 | Py_RETURN_NONE; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 341 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 342 | "Extend the right side of the deque with elements from the iterable"); |
| 343 | |
| 344 | static PyObject * |
| 345 | deque_extendleft(dequeobject *deque, PyObject *iterable) |
| 346 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 347 | PyObject *it, *item; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 348 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 349 | /* Handle case where id(deque) == id(iterable) */ |
| 350 | if ((PyObject *)deque == iterable) { |
| 351 | PyObject *result; |
| 352 | PyObject *s = PySequence_List(iterable); |
| 353 | if (s == NULL) |
| 354 | return NULL; |
| 355 | result = deque_extendleft(deque, s); |
| 356 | Py_DECREF(s); |
| 357 | return result; |
| 358 | } |
Raymond Hettinger | 0b3263b | 2009-12-10 06:00:33 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 360 | it = PyObject_GetIter(iterable); |
| 361 | if (it == NULL) |
| 362 | return NULL; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 364 | if (deque->maxlen == 0) |
| 365 | return consume_iterator(it); |
Raymond Hettinger | bac769b | 2009-03-10 09:31:48 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 367 | while ((item = PyIter_Next(it)) != NULL) { |
| 368 | deque->state++; |
| 369 | if (deque->leftindex == 0) { |
| 370 | block *b = newblock(NULL, deque->leftblock, |
| 371 | deque->len); |
| 372 | if (b == NULL) { |
| 373 | Py_DECREF(item); |
| 374 | Py_DECREF(it); |
| 375 | return NULL; |
| 376 | } |
| 377 | assert(deque->leftblock->leftlink == NULL); |
| 378 | deque->leftblock->leftlink = b; |
| 379 | deque->leftblock = b; |
| 380 | deque->leftindex = BLOCKLEN; |
| 381 | } |
| 382 | deque->len++; |
| 383 | deque->leftindex--; |
| 384 | deque->leftblock->data[deque->leftindex] = item; |
| 385 | TRIM(deque, deque_pop); |
| 386 | } |
| 387 | Py_DECREF(it); |
| 388 | if (PyErr_Occurred()) |
| 389 | return NULL; |
| 390 | Py_RETURN_NONE; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 393 | PyDoc_STRVAR(extendleft_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 394 | "Extend the left side of the deque with elements from the iterable"); |
| 395 | |
Raymond Hettinger | 0b3263b | 2009-12-10 06:00:33 +0000 | [diff] [blame] | 396 | static PyObject * |
| 397 | deque_inplace_concat(dequeobject *deque, PyObject *other) |
| 398 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 399 | PyObject *result; |
Raymond Hettinger | 0b3263b | 2009-12-10 06:00:33 +0000 | [diff] [blame] | 400 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 401 | result = deque_extend(deque, other); |
| 402 | if (result == NULL) |
| 403 | return result; |
| 404 | Py_DECREF(result); |
| 405 | Py_INCREF(deque); |
| 406 | return (PyObject *)deque; |
Raymond Hettinger | 0b3263b | 2009-12-10 06:00:33 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 409 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 410 | _deque_rotate(dequeobject *deque, Py_ssize_t n) |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 411 | { |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 412 | Py_ssize_t m, len=deque->len, halflen=len>>1; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 413 | |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 414 | if (len <= 1) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 415 | return 0; |
| 416 | if (n > halflen || n < -halflen) { |
| 417 | n %= len; |
| 418 | if (n > halflen) |
| 419 | n -= len; |
| 420 | else if (n < -halflen) |
| 421 | n += len; |
| 422 | } |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 423 | assert(len > 1); |
| 424 | assert(-halflen <= n && n <= halflen); |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 425 | |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 426 | deque->state++; |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 427 | while (n > 0) { |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 428 | if (deque->leftindex == 0) { |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 429 | block *b = newblock(NULL, deque->leftblock, len); |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 430 | if (b == NULL) |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 431 | return -1; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 432 | assert(deque->leftblock->leftlink == NULL); |
| 433 | deque->leftblock->leftlink = b; |
| 434 | deque->leftblock = b; |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 435 | deque->leftindex = BLOCKLEN; |
| 436 | } |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 437 | assert(deque->leftindex > 0); |
| 438 | |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 439 | m = n; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 440 | if (m > deque->rightindex + 1) |
| 441 | m = deque->rightindex + 1; |
| 442 | if (m > deque->leftindex) |
| 443 | m = deque->leftindex; |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 444 | assert (m > 0 && m <= len); |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 445 | memcpy(&deque->leftblock->data[deque->leftindex - m], |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 446 | &deque->rightblock->data[deque->rightindex + 1 - m], |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 447 | m * sizeof(PyObject *)); |
| 448 | deque->rightindex -= m; |
| 449 | deque->leftindex -= m; |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 450 | n -= m; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 451 | |
| 452 | if (deque->rightindex == -1) { |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 453 | block *prevblock = deque->rightblock->leftlink; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 454 | assert(deque->rightblock != NULL); |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 455 | assert(deque->leftblock != deque->rightblock); |
| 456 | freeblock(deque->rightblock); |
| 457 | prevblock->rightlink = NULL; |
| 458 | deque->rightblock = prevblock; |
| 459 | deque->rightindex = BLOCKLEN - 1; |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 460 | } |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 461 | } |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 462 | while (n < 0) { |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 463 | if (deque->rightindex == BLOCKLEN - 1) { |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 464 | block *b = newblock(deque->rightblock, NULL, len); |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 465 | if (b == NULL) |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 466 | return -1; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 467 | assert(deque->rightblock->rightlink == NULL); |
| 468 | deque->rightblock->rightlink = b; |
| 469 | deque->rightblock = b; |
Raymond Hettinger | 2cdb643 | 2013-01-12 00:05:00 -0800 | [diff] [blame] | 470 | deque->rightindex = -1; |
| 471 | } |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 472 | assert (deque->rightindex < BLOCKLEN - 1); |
| 473 | |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 474 | m = -n; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 475 | if (m > BLOCKLEN - deque->leftindex) |
| 476 | m = BLOCKLEN - deque->leftindex; |
| 477 | if (m > BLOCKLEN - 1 - deque->rightindex) |
| 478 | m = BLOCKLEN - 1 - deque->rightindex; |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 479 | assert (m > 0 && m <= len); |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 480 | memcpy(&deque->rightblock->data[deque->rightindex + 1], |
| 481 | &deque->leftblock->data[deque->leftindex], |
| 482 | m * sizeof(PyObject *)); |
| 483 | deque->leftindex += m; |
| 484 | deque->rightindex += m; |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 485 | n += m; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 486 | |
| 487 | if (deque->leftindex == BLOCKLEN) { |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 488 | block *nextblock = deque->leftblock->rightlink; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 489 | assert(deque->leftblock != deque->rightblock); |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 490 | freeblock(deque->leftblock); |
Raymond Hettinger | 6688bdb | 2013-02-09 18:55:44 -0500 | [diff] [blame] | 491 | assert(nextblock != NULL); |
| 492 | nextblock->leftlink = NULL; |
| 493 | deque->leftblock = nextblock; |
Raymond Hettinger | 4264532 | 2013-02-02 10:23:37 -0800 | [diff] [blame] | 494 | deque->leftindex = 0; |
| 495 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 496 | } |
| 497 | return 0; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static PyObject * |
| 501 | deque_rotate(dequeobject *deque, PyObject *args) |
| 502 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 503 | Py_ssize_t n=1; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 505 | if (!PyArg_ParseTuple(args, "|n:rotate", &n)) |
| 506 | return NULL; |
| 507 | if (_deque_rotate(deque, n) == 0) |
| 508 | Py_RETURN_NONE; |
| 509 | return NULL; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 512 | PyDoc_STRVAR(rotate_doc, |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 513 | "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] | 514 | |
Raymond Hettinger | a5fd24e | 2009-12-10 06:42:54 +0000 | [diff] [blame] | 515 | static PyObject * |
| 516 | deque_reverse(dequeobject *deque, PyObject *unused) |
| 517 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 518 | block *leftblock = deque->leftblock; |
| 519 | block *rightblock = deque->rightblock; |
| 520 | Py_ssize_t leftindex = deque->leftindex; |
| 521 | Py_ssize_t rightindex = deque->rightindex; |
| 522 | Py_ssize_t n = (deque->len)/2; |
| 523 | Py_ssize_t i; |
| 524 | PyObject *tmp; |
Raymond Hettinger | a5fd24e | 2009-12-10 06:42:54 +0000 | [diff] [blame] | 525 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 526 | for (i=0 ; i<n ; i++) { |
| 527 | /* Validate that pointers haven't met in the middle */ |
| 528 | assert(leftblock != rightblock || leftindex < rightindex); |
Raymond Hettinger | a5fd24e | 2009-12-10 06:42:54 +0000 | [diff] [blame] | 529 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | /* Swap */ |
| 531 | tmp = leftblock->data[leftindex]; |
| 532 | leftblock->data[leftindex] = rightblock->data[rightindex]; |
| 533 | rightblock->data[rightindex] = tmp; |
Raymond Hettinger | a5fd24e | 2009-12-10 06:42:54 +0000 | [diff] [blame] | 534 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 535 | /* Advance left block/index pair */ |
| 536 | leftindex++; |
| 537 | if (leftindex == BLOCKLEN) { |
Raymond Hettinger | 57a8689 | 2011-01-25 21:43:29 +0000 | [diff] [blame] | 538 | if (leftblock->rightlink == NULL) |
| 539 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 540 | leftblock = leftblock->rightlink; |
| 541 | leftindex = 0; |
| 542 | } |
Raymond Hettinger | a5fd24e | 2009-12-10 06:42:54 +0000 | [diff] [blame] | 543 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 544 | /* Step backwards with the right block/index pair */ |
| 545 | rightindex--; |
| 546 | if (rightindex == -1) { |
Raymond Hettinger | 57a8689 | 2011-01-25 21:43:29 +0000 | [diff] [blame] | 547 | if (rightblock->leftlink == NULL) |
| 548 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 549 | rightblock = rightblock->leftlink; |
| 550 | rightindex = BLOCKLEN - 1; |
| 551 | } |
| 552 | } |
| 553 | Py_RETURN_NONE; |
Raymond Hettinger | a5fd24e | 2009-12-10 06:42:54 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | PyDoc_STRVAR(reverse_doc, |
| 557 | "D.reverse() -- reverse *IN PLACE*"); |
| 558 | |
Raymond Hettinger | 5f516ed | 2010-04-03 18:10:37 +0000 | [diff] [blame] | 559 | static PyObject * |
| 560 | deque_count(dequeobject *deque, PyObject *v) |
| 561 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 562 | block *leftblock = deque->leftblock; |
| 563 | Py_ssize_t leftindex = deque->leftindex; |
Raymond Hettinger | 57a8689 | 2011-01-25 21:43:29 +0000 | [diff] [blame] | 564 | Py_ssize_t n = deque->len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 565 | Py_ssize_t i; |
| 566 | Py_ssize_t count = 0; |
| 567 | PyObject *item; |
| 568 | long start_state = deque->state; |
| 569 | int cmp; |
Raymond Hettinger | 5f516ed | 2010-04-03 18:10:37 +0000 | [diff] [blame] | 570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 571 | for (i=0 ; i<n ; i++) { |
| 572 | item = leftblock->data[leftindex]; |
| 573 | cmp = PyObject_RichCompareBool(item, v, Py_EQ); |
| 574 | if (cmp > 0) |
| 575 | count++; |
| 576 | else if (cmp < 0) |
| 577 | return NULL; |
Raymond Hettinger | 5f516ed | 2010-04-03 18:10:37 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 579 | if (start_state != deque->state) { |
| 580 | PyErr_SetString(PyExc_RuntimeError, |
| 581 | "deque mutated during iteration"); |
| 582 | return NULL; |
| 583 | } |
Raymond Hettinger | 5f516ed | 2010-04-03 18:10:37 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 585 | /* Advance left block/index pair */ |
| 586 | leftindex++; |
| 587 | if (leftindex == BLOCKLEN) { |
Raymond Hettinger | 57a8689 | 2011-01-25 21:43:29 +0000 | [diff] [blame] | 588 | if (leftblock->rightlink == NULL) /* can occur when i==n-1 */ |
| 589 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 590 | leftblock = leftblock->rightlink; |
| 591 | leftindex = 0; |
| 592 | } |
| 593 | } |
| 594 | return PyInt_FromSsize_t(count); |
Raymond Hettinger | 5f516ed | 2010-04-03 18:10:37 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | PyDoc_STRVAR(count_doc, |
| 598 | "D.count(value) -> integer -- return number of occurrences of value"); |
| 599 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 600 | static Py_ssize_t |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 601 | deque_len(dequeobject *deque) |
| 602 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 603 | return deque->len; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 606 | static PyObject * |
| 607 | deque_remove(dequeobject *deque, PyObject *value) |
| 608 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 609 | Py_ssize_t i, n=deque->len; |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 611 | for (i=0 ; i<n ; i++) { |
| 612 | PyObject *item = deque->leftblock->data[deque->leftindex]; |
| 613 | int cmp = PyObject_RichCompareBool(item, value, Py_EQ); |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 615 | if (deque->len != n) { |
| 616 | PyErr_SetString(PyExc_IndexError, |
| 617 | "deque mutated during remove()."); |
| 618 | return NULL; |
| 619 | } |
| 620 | if (cmp > 0) { |
| 621 | PyObject *tgt = deque_popleft(deque, NULL); |
| 622 | assert (tgt != NULL); |
| 623 | Py_DECREF(tgt); |
| 624 | if (_deque_rotate(deque, i) == -1) |
| 625 | return NULL; |
| 626 | Py_RETURN_NONE; |
| 627 | } |
| 628 | else if (cmp < 0) { |
| 629 | _deque_rotate(deque, i); |
| 630 | return NULL; |
| 631 | } |
| 632 | _deque_rotate(deque, -1); |
| 633 | } |
| 634 | PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"); |
| 635 | return NULL; |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | PyDoc_STRVAR(remove_doc, |
| 639 | "D.remove(value) -- remove first occurrence of value."); |
| 640 | |
Benjamin Peterson | 40056de | 2013-01-12 21:22:18 -0500 | [diff] [blame] | 641 | static void |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 642 | deque_clear(dequeobject *deque) |
| 643 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 644 | PyObject *item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 646 | while (deque->len) { |
| 647 | item = deque_pop(deque, NULL); |
| 648 | assert (item != NULL); |
| 649 | Py_DECREF(item); |
| 650 | } |
| 651 | assert(deque->leftblock == deque->rightblock && |
| 652 | deque->leftindex - 1 == deque->rightindex && |
| 653 | deque->len == 0); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static PyObject * |
Raymond Hettinger | 33fcf9d | 2008-07-24 00:08:18 +0000 | [diff] [blame] | 657 | deque_item(dequeobject *deque, Py_ssize_t i) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 658 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 659 | block *b; |
| 660 | PyObject *item; |
| 661 | Py_ssize_t n, index=i; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | if (i < 0 || i >= deque->len) { |
| 664 | PyErr_SetString(PyExc_IndexError, |
| 665 | "deque index out of range"); |
| 666 | return NULL; |
| 667 | } |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 669 | if (i == 0) { |
| 670 | i = deque->leftindex; |
| 671 | b = deque->leftblock; |
| 672 | } else if (i == deque->len - 1) { |
| 673 | i = deque->rightindex; |
| 674 | b = deque->rightblock; |
| 675 | } else { |
| 676 | i += deque->leftindex; |
| 677 | n = i / BLOCKLEN; |
| 678 | i %= BLOCKLEN; |
| 679 | if (index < (deque->len >> 1)) { |
| 680 | b = deque->leftblock; |
| 681 | while (n--) |
| 682 | b = b->rightlink; |
| 683 | } else { |
| 684 | n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n; |
| 685 | b = deque->rightblock; |
| 686 | while (n--) |
| 687 | b = b->leftlink; |
| 688 | } |
| 689 | } |
| 690 | item = b->data[i]; |
| 691 | Py_INCREF(item); |
| 692 | return item; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Raymond Hettinger | 616f4f6 | 2004-06-26 04:42:06 +0000 | [diff] [blame] | 695 | /* delitem() implemented in terms of rotate for simplicity and reasonable |
| 696 | performance near the end points. If for some reason this method becomes |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 697 | 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] | 698 | (similar to code in list slice assignment) and achieve a two or threefold |
| 699 | performance boost. |
| 700 | */ |
| 701 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 702 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 703 | deque_del_item(dequeobject *deque, Py_ssize_t i) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 704 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 705 | PyObject *item; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 707 | assert (i >= 0 && i < deque->len); |
| 708 | if (_deque_rotate(deque, -i) == -1) |
| 709 | return -1; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 710 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 711 | item = deque_popleft(deque, NULL); |
| 712 | assert (item != NULL); |
| 713 | Py_DECREF(item); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 714 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 715 | return _deque_rotate(deque, i); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 719 | deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 720 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 721 | PyObject *old_value; |
| 722 | block *b; |
| 723 | 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] | 724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 725 | if (i < 0 || i >= len) { |
| 726 | PyErr_SetString(PyExc_IndexError, |
| 727 | "deque index out of range"); |
| 728 | return -1; |
| 729 | } |
| 730 | if (v == NULL) |
| 731 | return deque_del_item(deque, i); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 733 | i += deque->leftindex; |
| 734 | n = i / BLOCKLEN; |
| 735 | i %= BLOCKLEN; |
| 736 | if (index <= halflen) { |
| 737 | b = deque->leftblock; |
| 738 | while (n--) |
| 739 | b = b->rightlink; |
| 740 | } else { |
| 741 | n = (deque->leftindex + len - 1) / BLOCKLEN - n; |
| 742 | b = deque->rightblock; |
| 743 | while (n--) |
| 744 | b = b->leftlink; |
| 745 | } |
| 746 | Py_INCREF(v); |
| 747 | old_value = b->data[i]; |
| 748 | b->data[i] = v; |
| 749 | Py_DECREF(old_value); |
| 750 | return 0; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | static PyObject * |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 754 | deque_clearmethod(dequeobject *deque) |
| 755 | { |
Benjamin Peterson | 40056de | 2013-01-12 21:22:18 -0500 | [diff] [blame] | 756 | deque_clear(deque); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 757 | Py_RETURN_NONE; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | PyDoc_STRVAR(clear_doc, "Remove all elements from the deque."); |
| 761 | |
| 762 | static void |
| 763 | deque_dealloc(dequeobject *deque) |
| 764 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 765 | PyObject_GC_UnTrack(deque); |
| 766 | if (deque->weakreflist != NULL) |
| 767 | PyObject_ClearWeakRefs((PyObject *) deque); |
| 768 | if (deque->leftblock != NULL) { |
| 769 | deque_clear(deque); |
| 770 | assert(deque->leftblock != NULL); |
| 771 | freeblock(deque->leftblock); |
| 772 | } |
| 773 | deque->leftblock = NULL; |
| 774 | deque->rightblock = NULL; |
| 775 | Py_TYPE(deque)->tp_free(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static int |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 779 | deque_traverse(dequeobject *deque, visitproc visit, void *arg) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 780 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 781 | block *b; |
| 782 | PyObject *item; |
| 783 | Py_ssize_t index; |
| 784 | Py_ssize_t indexlo = deque->leftindex; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 785 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 786 | for (b = deque->leftblock; b != NULL; b = b->rightlink) { |
| 787 | const Py_ssize_t indexhi = b == deque->rightblock ? |
| 788 | deque->rightindex : |
| 789 | BLOCKLEN - 1; |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 790 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 791 | for (index = indexlo; index <= indexhi; ++index) { |
| 792 | item = b->data[index]; |
| 793 | Py_VISIT(item); |
| 794 | } |
| 795 | indexlo = 0; |
| 796 | } |
| 797 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 800 | static PyObject * |
| 801 | deque_copy(PyObject *deque) |
| 802 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 803 | if (((dequeobject *)deque)->maxlen == -1) |
| 804 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL); |
| 805 | else |
| 806 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi", |
| 807 | deque, ((dequeobject *)deque)->maxlen, NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque."); |
| 811 | |
| 812 | static PyObject * |
| 813 | deque_reduce(dequeobject *deque) |
| 814 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 815 | PyObject *dict, *result, *aslist; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 817 | dict = PyObject_GetAttrString((PyObject *)deque, "__dict__"); |
| 818 | if (dict == NULL) |
| 819 | PyErr_Clear(); |
| 820 | aslist = PySequence_List((PyObject *)deque); |
| 821 | if (aslist == NULL) { |
| 822 | Py_XDECREF(dict); |
| 823 | return NULL; |
| 824 | } |
| 825 | if (dict == NULL) { |
| 826 | if (deque->maxlen == -1) |
| 827 | result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist); |
| 828 | else |
| 829 | result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen); |
| 830 | } else { |
| 831 | if (deque->maxlen == -1) |
| 832 | result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict); |
| 833 | else |
| 834 | result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict); |
| 835 | } |
| 836 | Py_XDECREF(dict); |
| 837 | Py_DECREF(aslist); |
| 838 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 842 | |
| 843 | static PyObject * |
| 844 | deque_repr(PyObject *deque) |
| 845 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 846 | PyObject *aslist, *result, *fmt; |
| 847 | int i; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 849 | i = Py_ReprEnter(deque); |
| 850 | if (i != 0) { |
| 851 | if (i < 0) |
| 852 | return NULL; |
| 853 | return PyString_FromString("[...]"); |
| 854 | } |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 856 | aslist = PySequence_List(deque); |
| 857 | if (aslist == NULL) { |
| 858 | Py_ReprLeave(deque); |
| 859 | return NULL; |
| 860 | } |
| 861 | if (((dequeobject *)deque)->maxlen != -1) |
| 862 | fmt = PyString_FromFormat("deque(%%r, maxlen=%zd)", |
| 863 | ((dequeobject *)deque)->maxlen); |
| 864 | else |
| 865 | fmt = PyString_FromString("deque(%r)"); |
| 866 | if (fmt == NULL) { |
| 867 | Py_DECREF(aslist); |
| 868 | Py_ReprLeave(deque); |
| 869 | return NULL; |
| 870 | } |
| 871 | result = PyString_Format(fmt, aslist); |
| 872 | Py_DECREF(fmt); |
| 873 | Py_DECREF(aslist); |
| 874 | Py_ReprLeave(deque); |
| 875 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | static int |
| 879 | deque_tp_print(PyObject *deque, FILE *fp, int flags) |
| 880 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 881 | PyObject *it, *item; |
| 882 | char *emit = ""; /* No separator emitted on first pass */ |
| 883 | char *separator = ", "; |
| 884 | int i; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 885 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 886 | i = Py_ReprEnter(deque); |
| 887 | if (i != 0) { |
| 888 | if (i < 0) |
| 889 | return i; |
| 890 | Py_BEGIN_ALLOW_THREADS |
| 891 | fputs("[...]", fp); |
| 892 | Py_END_ALLOW_THREADS |
| 893 | return 0; |
| 894 | } |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 896 | it = PyObject_GetIter(deque); |
| 897 | if (it == NULL) |
| 898 | return -1; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 900 | Py_BEGIN_ALLOW_THREADS |
| 901 | fputs("deque([", fp); |
| 902 | Py_END_ALLOW_THREADS |
| 903 | while ((item = PyIter_Next(it)) != NULL) { |
| 904 | Py_BEGIN_ALLOW_THREADS |
| 905 | fputs(emit, fp); |
| 906 | Py_END_ALLOW_THREADS |
| 907 | emit = separator; |
| 908 | if (PyObject_Print(item, fp, 0) != 0) { |
| 909 | Py_DECREF(item); |
| 910 | Py_DECREF(it); |
| 911 | Py_ReprLeave(deque); |
| 912 | return -1; |
| 913 | } |
| 914 | Py_DECREF(item); |
| 915 | } |
| 916 | Py_ReprLeave(deque); |
| 917 | Py_DECREF(it); |
| 918 | if (PyErr_Occurred()) |
| 919 | return -1; |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 921 | Py_BEGIN_ALLOW_THREADS |
| 922 | if (((dequeobject *)deque)->maxlen == -1) |
| 923 | fputs("])", fp); |
| 924 | else |
| 925 | fprintf(fp, "], maxlen=%" PY_FORMAT_SIZE_T "d)", ((dequeobject *)deque)->maxlen); |
| 926 | Py_END_ALLOW_THREADS |
| 927 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 930 | static PyObject * |
| 931 | deque_richcompare(PyObject *v, PyObject *w, int op) |
| 932 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 933 | PyObject *it1=NULL, *it2=NULL, *x, *y; |
| 934 | Py_ssize_t vs, ws; |
| 935 | int b, cmp=-1; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 936 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 937 | if (!PyObject_TypeCheck(v, &deque_type) || |
| 938 | !PyObject_TypeCheck(w, &deque_type)) { |
| 939 | Py_INCREF(Py_NotImplemented); |
| 940 | return Py_NotImplemented; |
| 941 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 943 | /* Shortcuts */ |
| 944 | vs = ((dequeobject *)v)->len; |
| 945 | ws = ((dequeobject *)w)->len; |
| 946 | if (op == Py_EQ) { |
| 947 | if (v == w) |
| 948 | Py_RETURN_TRUE; |
| 949 | if (vs != ws) |
| 950 | Py_RETURN_FALSE; |
| 951 | } |
| 952 | if (op == Py_NE) { |
| 953 | if (v == w) |
| 954 | Py_RETURN_FALSE; |
| 955 | if (vs != ws) |
| 956 | Py_RETURN_TRUE; |
| 957 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 958 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 959 | /* Search for the first index where items are different */ |
| 960 | it1 = PyObject_GetIter(v); |
| 961 | if (it1 == NULL) |
| 962 | goto done; |
| 963 | it2 = PyObject_GetIter(w); |
| 964 | if (it2 == NULL) |
| 965 | goto done; |
| 966 | for (;;) { |
| 967 | x = PyIter_Next(it1); |
| 968 | if (x == NULL && PyErr_Occurred()) |
| 969 | goto done; |
| 970 | y = PyIter_Next(it2); |
| 971 | if (x == NULL || y == NULL) |
| 972 | break; |
| 973 | b = PyObject_RichCompareBool(x, y, Py_EQ); |
| 974 | if (b == 0) { |
| 975 | cmp = PyObject_RichCompareBool(x, y, op); |
| 976 | Py_DECREF(x); |
| 977 | Py_DECREF(y); |
| 978 | goto done; |
| 979 | } |
| 980 | Py_DECREF(x); |
| 981 | Py_DECREF(y); |
| 982 | if (b == -1) |
| 983 | goto done; |
| 984 | } |
| 985 | /* We reached the end of one deque or both */ |
| 986 | Py_XDECREF(x); |
| 987 | Py_XDECREF(y); |
| 988 | if (PyErr_Occurred()) |
| 989 | goto done; |
| 990 | switch (op) { |
| 991 | case Py_LT: cmp = y != NULL; break; /* if w was longer */ |
| 992 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */ |
| 993 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ |
| 994 | case Py_NE: cmp = x != y; break; /* if one deque continues */ |
| 995 | case Py_GT: cmp = x != NULL; break; /* if v was longer */ |
| 996 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */ |
| 997 | } |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 998 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 999 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1000 | Py_XDECREF(it1); |
| 1001 | Py_XDECREF(it2); |
| 1002 | if (cmp == 1) |
| 1003 | Py_RETURN_TRUE; |
| 1004 | if (cmp == 0) |
| 1005 | Py_RETURN_FALSE; |
| 1006 | return NULL; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1009 | static int |
Raymond Hettinger | a7fc4b1 | 2007-10-05 02:47:07 +0000 | [diff] [blame] | 1010 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1011 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1012 | PyObject *iterable = NULL; |
| 1013 | PyObject *maxlenobj = NULL; |
| 1014 | Py_ssize_t maxlen = -1; |
| 1015 | char *kwlist[] = {"iterable", "maxlen", 0}; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1017 | if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) |
| 1018 | return -1; |
| 1019 | if (maxlenobj != NULL && maxlenobj != Py_None) { |
| 1020 | maxlen = PyInt_AsSsize_t(maxlenobj); |
| 1021 | if (maxlen == -1 && PyErr_Occurred()) |
| 1022 | return -1; |
| 1023 | if (maxlen < 0) { |
| 1024 | PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); |
| 1025 | return -1; |
| 1026 | } |
| 1027 | } |
| 1028 | deque->maxlen = maxlen; |
| 1029 | deque_clear(deque); |
| 1030 | if (iterable != NULL) { |
| 1031 | PyObject *rv = deque_extend(deque, iterable); |
| 1032 | if (rv == NULL) |
| 1033 | return -1; |
| 1034 | Py_DECREF(rv); |
| 1035 | } |
| 1036 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Raymond Hettinger | 56411aa | 2009-03-10 12:50:59 +0000 | [diff] [blame] | 1039 | static PyObject * |
Jesus Cea | d4e58dc | 2012-08-03 14:48:23 +0200 | [diff] [blame] | 1040 | deque_sizeof(dequeobject *deque, void *unused) |
| 1041 | { |
| 1042 | Py_ssize_t res; |
| 1043 | Py_ssize_t blocks; |
| 1044 | |
| 1045 | res = sizeof(dequeobject); |
| 1046 | blocks = (deque->leftindex + deque->len + BLOCKLEN - 1) / BLOCKLEN; |
| 1047 | assert(deque->leftindex + deque->len - 1 == |
| 1048 | (blocks - 1) * BLOCKLEN + deque->rightindex); |
| 1049 | res += blocks * sizeof(block); |
| 1050 | return PyLong_FromSsize_t(res); |
| 1051 | } |
| 1052 | |
| 1053 | PyDoc_STRVAR(sizeof_doc, |
| 1054 | "D.__sizeof__() -- size of D in memory, in bytes"); |
| 1055 | |
| 1056 | static PyObject * |
Raymond Hettinger | 56411aa | 2009-03-10 12:50:59 +0000 | [diff] [blame] | 1057 | deque_get_maxlen(dequeobject *deque) |
| 1058 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1059 | if (deque->maxlen == -1) |
| 1060 | Py_RETURN_NONE; |
| 1061 | return PyInt_FromSsize_t(deque->maxlen); |
Raymond Hettinger | 56411aa | 2009-03-10 12:50:59 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | static PyGetSetDef deque_getset[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1065 | {"maxlen", (getter)deque_get_maxlen, (setter)NULL, |
| 1066 | "maximum size of a deque or None if unbounded"}, |
| 1067 | {0} |
Raymond Hettinger | 56411aa | 2009-03-10 12:50:59 +0000 | [diff] [blame] | 1068 | }; |
| 1069 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1070 | static PySequenceMethods deque_as_sequence = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1071 | (lenfunc)deque_len, /* sq_length */ |
| 1072 | 0, /* sq_concat */ |
| 1073 | 0, /* sq_repeat */ |
| 1074 | (ssizeargfunc)deque_item, /* sq_item */ |
| 1075 | 0, /* sq_slice */ |
| 1076 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ |
| 1077 | 0, /* sq_ass_slice */ |
| 1078 | 0, /* sq_contains */ |
| 1079 | (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */ |
| 1080 | 0, /* sq_inplace_repeat */ |
Raymond Hettinger | 0b3263b | 2009-12-10 06:00:33 +0000 | [diff] [blame] | 1081 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1082 | }; |
| 1083 | |
| 1084 | /* deque object ********************************************************/ |
| 1085 | |
| 1086 | static PyObject *deque_iter(dequeobject *deque); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1087 | static PyObject *deque_reviter(dequeobject *deque); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 1088 | PyDoc_STRVAR(reversed_doc, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1089 | "D.__reversed__() -- return a reverse iterator over the deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1090 | |
| 1091 | static PyMethodDef deque_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1092 | {"append", (PyCFunction)deque_append, |
| 1093 | METH_O, append_doc}, |
| 1094 | {"appendleft", (PyCFunction)deque_appendleft, |
| 1095 | METH_O, appendleft_doc}, |
| 1096 | {"clear", (PyCFunction)deque_clearmethod, |
| 1097 | METH_NOARGS, clear_doc}, |
| 1098 | {"__copy__", (PyCFunction)deque_copy, |
| 1099 | METH_NOARGS, copy_doc}, |
| 1100 | {"count", (PyCFunction)deque_count, |
| 1101 | METH_O, count_doc}, |
| 1102 | {"extend", (PyCFunction)deque_extend, |
| 1103 | METH_O, extend_doc}, |
| 1104 | {"extendleft", (PyCFunction)deque_extendleft, |
| 1105 | METH_O, extendleft_doc}, |
| 1106 | {"pop", (PyCFunction)deque_pop, |
| 1107 | METH_NOARGS, pop_doc}, |
| 1108 | {"popleft", (PyCFunction)deque_popleft, |
| 1109 | METH_NOARGS, popleft_doc}, |
| 1110 | {"__reduce__", (PyCFunction)deque_reduce, |
| 1111 | METH_NOARGS, reduce_doc}, |
| 1112 | {"remove", (PyCFunction)deque_remove, |
| 1113 | METH_O, remove_doc}, |
| 1114 | {"__reversed__", (PyCFunction)deque_reviter, |
| 1115 | METH_NOARGS, reversed_doc}, |
| 1116 | {"reverse", (PyCFunction)deque_reverse, |
| 1117 | METH_NOARGS, reverse_doc}, |
| 1118 | {"rotate", (PyCFunction)deque_rotate, |
Jesus Cea | d4e58dc | 2012-08-03 14:48:23 +0200 | [diff] [blame] | 1119 | METH_VARARGS, rotate_doc}, |
| 1120 | {"__sizeof__", (PyCFunction)deque_sizeof, |
| 1121 | METH_NOARGS, sizeof_doc}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1122 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1123 | }; |
| 1124 | |
| 1125 | PyDoc_STRVAR(deque_doc, |
Andrew Svetlov | 227f59b | 2012-10-31 11:50:00 +0200 | [diff] [blame] | 1126 | "deque([iterable[, maxlen]]) --> deque object\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1127 | \n\ |
Raymond Hettinger | db9d64b | 2011-03-29 17:28:25 -0700 | [diff] [blame] | 1128 | Build an ordered collection with optimized access from its endpoints."); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1129 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 1130 | static PyTypeObject deque_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1131 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1132 | "collections.deque", /* tp_name */ |
| 1133 | sizeof(dequeobject), /* tp_basicsize */ |
| 1134 | 0, /* tp_itemsize */ |
| 1135 | /* methods */ |
| 1136 | (destructor)deque_dealloc, /* tp_dealloc */ |
| 1137 | deque_tp_print, /* tp_print */ |
| 1138 | 0, /* tp_getattr */ |
| 1139 | 0, /* tp_setattr */ |
| 1140 | 0, /* tp_compare */ |
| 1141 | deque_repr, /* tp_repr */ |
| 1142 | 0, /* tp_as_number */ |
| 1143 | &deque_as_sequence, /* tp_as_sequence */ |
| 1144 | 0, /* tp_as_mapping */ |
| 1145 | (hashfunc)PyObject_HashNotImplemented, /* tp_hash */ |
| 1146 | 0, /* tp_call */ |
| 1147 | 0, /* tp_str */ |
| 1148 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1149 | 0, /* tp_setattro */ |
| 1150 | 0, /* tp_as_buffer */ |
| 1151 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 1152 | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 1153 | deque_doc, /* tp_doc */ |
| 1154 | (traverseproc)deque_traverse, /* tp_traverse */ |
| 1155 | (inquiry)deque_clear, /* tp_clear */ |
| 1156 | (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
| 1157 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
| 1158 | (getiterfunc)deque_iter, /* tp_iter */ |
| 1159 | 0, /* tp_iternext */ |
| 1160 | deque_methods, /* tp_methods */ |
| 1161 | 0, /* tp_members */ |
| 1162 | deque_getset, /* tp_getset */ |
| 1163 | 0, /* tp_base */ |
| 1164 | 0, /* tp_dict */ |
| 1165 | 0, /* tp_descr_get */ |
| 1166 | 0, /* tp_descr_set */ |
| 1167 | 0, /* tp_dictoffset */ |
| 1168 | (initproc)deque_init, /* tp_init */ |
| 1169 | PyType_GenericAlloc, /* tp_alloc */ |
| 1170 | deque_new, /* tp_new */ |
| 1171 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1172 | }; |
| 1173 | |
| 1174 | /*********************** Deque Iterator **************************/ |
| 1175 | |
| 1176 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1177 | PyObject_HEAD |
| 1178 | Py_ssize_t index; |
| 1179 | block *b; |
| 1180 | dequeobject *deque; |
| 1181 | long state; /* state when the iterator is created */ |
| 1182 | Py_ssize_t counter; /* number of items remaining for iteration */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1183 | } dequeiterobject; |
| 1184 | |
Martin v. Löwis | 111c180 | 2008-06-13 07:47:47 +0000 | [diff] [blame] | 1185 | static PyTypeObject dequeiter_type; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1186 | |
| 1187 | static PyObject * |
| 1188 | deque_iter(dequeobject *deque) |
| 1189 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1190 | dequeiterobject *it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1192 | it = PyObject_GC_New(dequeiterobject, &dequeiter_type); |
| 1193 | if (it == NULL) |
| 1194 | return NULL; |
| 1195 | it->b = deque->leftblock; |
| 1196 | it->index = deque->leftindex; |
| 1197 | Py_INCREF(deque); |
| 1198 | it->deque = deque; |
| 1199 | it->state = deque->state; |
| 1200 | it->counter = deque->len; |
| 1201 | PyObject_GC_Track(it); |
| 1202 | return (PyObject *)it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Antoine Pitrou | aa68790 | 2009-01-01 14:11:22 +0000 | [diff] [blame] | 1205 | static int |
| 1206 | dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg) |
| 1207 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1208 | Py_VISIT(dio->deque); |
| 1209 | return 0; |
Antoine Pitrou | aa68790 | 2009-01-01 14:11:22 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1212 | static void |
| 1213 | dequeiter_dealloc(dequeiterobject *dio) |
| 1214 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1215 | Py_XDECREF(dio->deque); |
| 1216 | PyObject_GC_Del(dio); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | static PyObject * |
| 1220 | dequeiter_next(dequeiterobject *it) |
| 1221 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1222 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1223 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1224 | if (it->deque->state != it->state) { |
| 1225 | it->counter = 0; |
| 1226 | PyErr_SetString(PyExc_RuntimeError, |
| 1227 | "deque mutated during iteration"); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | if (it->counter == 0) |
| 1231 | return NULL; |
| 1232 | assert (!(it->b == it->deque->rightblock && |
| 1233 | it->index > it->deque->rightindex)); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1235 | item = it->b->data[it->index]; |
| 1236 | it->index++; |
| 1237 | it->counter--; |
| 1238 | if (it->index == BLOCKLEN && it->counter > 0) { |
| 1239 | assert (it->b->rightlink != NULL); |
| 1240 | it->b = it->b->rightlink; |
| 1241 | it->index = 0; |
| 1242 | } |
| 1243 | Py_INCREF(item); |
| 1244 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1247 | static PyObject * |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1248 | dequeiter_len(dequeiterobject *it) |
| 1249 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1250 | return PyInt_FromLong(it->counter); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 1253 | 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] | 1254 | |
| 1255 | static PyMethodDef dequeiter_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1256 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc}, |
| 1257 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1258 | }; |
| 1259 | |
Martin v. Löwis | 111c180 | 2008-06-13 07:47:47 +0000 | [diff] [blame] | 1260 | static PyTypeObject dequeiter_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1261 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1262 | "deque_iterator", /* tp_name */ |
| 1263 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1264 | 0, /* tp_itemsize */ |
| 1265 | /* methods */ |
| 1266 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1267 | 0, /* tp_print */ |
| 1268 | 0, /* tp_getattr */ |
| 1269 | 0, /* tp_setattr */ |
| 1270 | 0, /* tp_compare */ |
| 1271 | 0, /* tp_repr */ |
| 1272 | 0, /* tp_as_number */ |
| 1273 | 0, /* tp_as_sequence */ |
| 1274 | 0, /* tp_as_mapping */ |
| 1275 | 0, /* tp_hash */ |
| 1276 | 0, /* tp_call */ |
| 1277 | 0, /* tp_str */ |
| 1278 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1279 | 0, /* tp_setattro */ |
| 1280 | 0, /* tp_as_buffer */ |
| 1281 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1282 | 0, /* tp_doc */ |
| 1283 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1284 | 0, /* tp_clear */ |
| 1285 | 0, /* tp_richcompare */ |
| 1286 | 0, /* tp_weaklistoffset */ |
| 1287 | PyObject_SelfIter, /* tp_iter */ |
| 1288 | (iternextfunc)dequeiter_next, /* tp_iternext */ |
| 1289 | dequeiter_methods, /* tp_methods */ |
| 1290 | 0, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1291 | }; |
| 1292 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1293 | /*********************** Deque Reverse Iterator **************************/ |
| 1294 | |
Martin v. Löwis | 111c180 | 2008-06-13 07:47:47 +0000 | [diff] [blame] | 1295 | static PyTypeObject dequereviter_type; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1296 | |
| 1297 | static PyObject * |
| 1298 | deque_reviter(dequeobject *deque) |
| 1299 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1300 | dequeiterobject *it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1302 | it = PyObject_GC_New(dequeiterobject, &dequereviter_type); |
| 1303 | if (it == NULL) |
| 1304 | return NULL; |
| 1305 | it->b = deque->rightblock; |
| 1306 | it->index = deque->rightindex; |
| 1307 | Py_INCREF(deque); |
| 1308 | it->deque = deque; |
| 1309 | it->state = deque->state; |
| 1310 | it->counter = deque->len; |
| 1311 | PyObject_GC_Track(it); |
| 1312 | return (PyObject *)it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | static PyObject * |
| 1316 | dequereviter_next(dequeiterobject *it) |
| 1317 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1318 | PyObject *item; |
| 1319 | if (it->counter == 0) |
| 1320 | return NULL; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1322 | if (it->deque->state != it->state) { |
| 1323 | it->counter = 0; |
| 1324 | PyErr_SetString(PyExc_RuntimeError, |
| 1325 | "deque mutated during iteration"); |
| 1326 | return NULL; |
| 1327 | } |
| 1328 | assert (!(it->b == it->deque->leftblock && |
| 1329 | it->index < it->deque->leftindex)); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1330 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1331 | item = it->b->data[it->index]; |
| 1332 | it->index--; |
| 1333 | it->counter--; |
| 1334 | if (it->index == -1 && it->counter > 0) { |
| 1335 | assert (it->b->leftlink != NULL); |
| 1336 | it->b = it->b->leftlink; |
| 1337 | it->index = BLOCKLEN - 1; |
| 1338 | } |
| 1339 | Py_INCREF(item); |
| 1340 | return item; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Martin v. Löwis | 111c180 | 2008-06-13 07:47:47 +0000 | [diff] [blame] | 1343 | static PyTypeObject dequereviter_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1344 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1345 | "deque_reverse_iterator", /* tp_name */ |
| 1346 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1347 | 0, /* tp_itemsize */ |
| 1348 | /* methods */ |
| 1349 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1350 | 0, /* tp_print */ |
| 1351 | 0, /* tp_getattr */ |
| 1352 | 0, /* tp_setattr */ |
| 1353 | 0, /* tp_compare */ |
| 1354 | 0, /* tp_repr */ |
| 1355 | 0, /* tp_as_number */ |
| 1356 | 0, /* tp_as_sequence */ |
| 1357 | 0, /* tp_as_mapping */ |
| 1358 | 0, /* tp_hash */ |
| 1359 | 0, /* tp_call */ |
| 1360 | 0, /* tp_str */ |
| 1361 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1362 | 0, /* tp_setattro */ |
| 1363 | 0, /* tp_as_buffer */ |
| 1364 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1365 | 0, /* tp_doc */ |
| 1366 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1367 | 0, /* tp_clear */ |
| 1368 | 0, /* tp_richcompare */ |
| 1369 | 0, /* tp_weaklistoffset */ |
| 1370 | PyObject_SelfIter, /* tp_iter */ |
| 1371 | (iternextfunc)dequereviter_next, /* tp_iternext */ |
| 1372 | dequeiter_methods, /* tp_methods */ |
| 1373 | 0, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1374 | }; |
| 1375 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1376 | /* defaultdict type *********************************************************/ |
| 1377 | |
| 1378 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1379 | PyDictObject dict; |
| 1380 | PyObject *default_factory; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1381 | } defdictobject; |
| 1382 | |
| 1383 | static PyTypeObject defdict_type; /* Forward */ |
| 1384 | |
| 1385 | PyDoc_STRVAR(defdict_missing_doc, |
| 1386 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ |
Georg Brandl | b51a57e | 2007-03-06 13:32:52 +0000 | [diff] [blame] | 1387 | if self.default_factory is None: raise KeyError((key,))\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1388 | self[key] = value = self.default_factory()\n\ |
| 1389 | return value\n\ |
| 1390 | "); |
| 1391 | |
| 1392 | static PyObject * |
| 1393 | defdict_missing(defdictobject *dd, PyObject *key) |
| 1394 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1395 | PyObject *factory = dd->default_factory; |
| 1396 | PyObject *value; |
| 1397 | if (factory == NULL || factory == Py_None) { |
| 1398 | /* XXX Call dict.__missing__(key) */ |
| 1399 | PyObject *tup; |
| 1400 | tup = PyTuple_Pack(1, key); |
| 1401 | if (!tup) return NULL; |
| 1402 | PyErr_SetObject(PyExc_KeyError, tup); |
| 1403 | Py_DECREF(tup); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | value = PyEval_CallObject(factory, NULL); |
| 1407 | if (value == NULL) |
| 1408 | return value; |
| 1409 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) { |
| 1410 | Py_DECREF(value); |
| 1411 | return NULL; |
| 1412 | } |
| 1413 | return value; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D."); |
| 1417 | |
| 1418 | static PyObject * |
| 1419 | defdict_copy(defdictobject *dd) |
| 1420 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1421 | /* This calls the object's class. That only works for subclasses |
| 1422 | whose class constructor has the same signature. Subclasses that |
| 1423 | define a different constructor signature must override copy(). |
| 1424 | */ |
Raymond Hettinger | 8fdab95 | 2009-08-04 19:08:05 +0000 | [diff] [blame] | 1425 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1426 | if (dd->default_factory == NULL) |
| 1427 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL); |
| 1428 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), |
| 1429 | dd->default_factory, dd, NULL); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | static PyObject * |
| 1433 | defdict_reduce(defdictobject *dd) |
| 1434 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1435 | /* __reduce__ must return a 5-tuple as follows: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1436 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1437 | - factory function |
| 1438 | - tuple of args for the factory function |
| 1439 | - additional state (here None) |
| 1440 | - sequence iterator (here None) |
| 1441 | - dictionary iterator (yielding successive (key, value) pairs |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1442 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1443 | This API is used by pickle.py and copy.py. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1445 | For this to be useful with pickle.py, the default_factory |
| 1446 | must be picklable; e.g., None, a built-in, or a global |
| 1447 | function in a module or package. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1448 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1449 | Both shallow and deep copying are supported, but for deep |
| 1450 | copying, the default_factory must be deep-copyable; e.g. None, |
| 1451 | or a built-in (functions are not copyable at this time). |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1453 | This only works for subclasses as long as their constructor |
| 1454 | signature is compatible; the first argument must be the |
| 1455 | optional default_factory, defaulting to None. |
| 1456 | */ |
| 1457 | PyObject *args; |
| 1458 | PyObject *items; |
| 1459 | PyObject *result; |
| 1460 | if (dd->default_factory == NULL || dd->default_factory == Py_None) |
| 1461 | args = PyTuple_New(0); |
| 1462 | else |
| 1463 | args = PyTuple_Pack(1, dd->default_factory); |
| 1464 | if (args == NULL) |
| 1465 | return NULL; |
| 1466 | items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()"); |
| 1467 | if (items == NULL) { |
| 1468 | Py_DECREF(args); |
| 1469 | return NULL; |
| 1470 | } |
| 1471 | result = PyTuple_Pack(5, Py_TYPE(dd), args, |
| 1472 | Py_None, Py_None, items); |
| 1473 | Py_DECREF(items); |
| 1474 | Py_DECREF(args); |
| 1475 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static PyMethodDef defdict_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1479 | {"__missing__", (PyCFunction)defdict_missing, METH_O, |
| 1480 | defdict_missing_doc}, |
| 1481 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1482 | defdict_copy_doc}, |
| 1483 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1484 | defdict_copy_doc}, |
| 1485 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS, |
| 1486 | reduce_doc}, |
| 1487 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1488 | }; |
| 1489 | |
| 1490 | static PyMemberDef defdict_members[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1491 | {"default_factory", T_OBJECT, |
| 1492 | offsetof(defdictobject, default_factory), 0, |
| 1493 | PyDoc_STR("Factory for default value called by __missing__().")}, |
| 1494 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1495 | }; |
| 1496 | |
| 1497 | static void |
| 1498 | defdict_dealloc(defdictobject *dd) |
| 1499 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1500 | Py_CLEAR(dd->default_factory); |
| 1501 | PyDict_Type.tp_dealloc((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | static int |
| 1505 | defdict_print(defdictobject *dd, FILE *fp, int flags) |
| 1506 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1507 | int sts; |
| 1508 | Py_BEGIN_ALLOW_THREADS |
| 1509 | fprintf(fp, "defaultdict("); |
| 1510 | Py_END_ALLOW_THREADS |
| 1511 | if (dd->default_factory == NULL) { |
| 1512 | Py_BEGIN_ALLOW_THREADS |
| 1513 | fprintf(fp, "None"); |
| 1514 | Py_END_ALLOW_THREADS |
| 1515 | } else { |
| 1516 | PyObject_Print(dd->default_factory, fp, 0); |
| 1517 | } |
| 1518 | Py_BEGIN_ALLOW_THREADS |
| 1519 | fprintf(fp, ", "); |
| 1520 | Py_END_ALLOW_THREADS |
| 1521 | sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0); |
| 1522 | Py_BEGIN_ALLOW_THREADS |
| 1523 | fprintf(fp, ")"); |
| 1524 | Py_END_ALLOW_THREADS |
| 1525 | return sts; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | static PyObject * |
| 1529 | defdict_repr(defdictobject *dd) |
| 1530 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1531 | PyObject *defrepr; |
| 1532 | PyObject *baserepr; |
| 1533 | PyObject *result; |
| 1534 | baserepr = PyDict_Type.tp_repr((PyObject *)dd); |
| 1535 | if (baserepr == NULL) |
| 1536 | return NULL; |
| 1537 | if (dd->default_factory == NULL) |
| 1538 | defrepr = PyString_FromString("None"); |
| 1539 | else |
| 1540 | { |
| 1541 | int status = Py_ReprEnter(dd->default_factory); |
| 1542 | if (status != 0) { |
Antoine Pitrou | c39cd78 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1543 | if (status < 0) { |
| 1544 | Py_DECREF(baserepr); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1545 | return NULL; |
Antoine Pitrou | c39cd78 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1546 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1547 | defrepr = PyString_FromString("..."); |
| 1548 | } |
| 1549 | else |
| 1550 | defrepr = PyObject_Repr(dd->default_factory); |
| 1551 | Py_ReprLeave(dd->default_factory); |
| 1552 | } |
| 1553 | if (defrepr == NULL) { |
| 1554 | Py_DECREF(baserepr); |
| 1555 | return NULL; |
| 1556 | } |
| 1557 | result = PyString_FromFormat("defaultdict(%s, %s)", |
| 1558 | PyString_AS_STRING(defrepr), |
| 1559 | PyString_AS_STRING(baserepr)); |
| 1560 | Py_DECREF(defrepr); |
| 1561 | Py_DECREF(baserepr); |
| 1562 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | static int |
| 1566 | defdict_traverse(PyObject *self, visitproc visit, void *arg) |
| 1567 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1568 | Py_VISIT(((defdictobject *)self)->default_factory); |
| 1569 | return PyDict_Type.tp_traverse(self, visit, arg); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static int |
| 1573 | defdict_tp_clear(defdictobject *dd) |
| 1574 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1575 | Py_CLEAR(dd->default_factory); |
| 1576 | return PyDict_Type.tp_clear((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | static int |
| 1580 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1581 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1582 | defdictobject *dd = (defdictobject *)self; |
| 1583 | PyObject *olddefault = dd->default_factory; |
| 1584 | PyObject *newdefault = NULL; |
| 1585 | PyObject *newargs; |
| 1586 | int result; |
| 1587 | if (args == NULL || !PyTuple_Check(args)) |
| 1588 | newargs = PyTuple_New(0); |
| 1589 | else { |
| 1590 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
| 1591 | if (n > 0) { |
| 1592 | newdefault = PyTuple_GET_ITEM(args, 0); |
| 1593 | if (!PyCallable_Check(newdefault) && newdefault != Py_None) { |
| 1594 | PyErr_SetString(PyExc_TypeError, |
| 1595 | "first argument must be callable"); |
| 1596 | return -1; |
| 1597 | } |
| 1598 | } |
| 1599 | newargs = PySequence_GetSlice(args, 1, n); |
| 1600 | } |
| 1601 | if (newargs == NULL) |
| 1602 | return -1; |
| 1603 | Py_XINCREF(newdefault); |
| 1604 | dd->default_factory = newdefault; |
| 1605 | result = PyDict_Type.tp_init(self, newargs, kwds); |
| 1606 | Py_DECREF(newargs); |
| 1607 | Py_XDECREF(olddefault); |
| 1608 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | PyDoc_STRVAR(defdict_doc, |
| 1612 | "defaultdict(default_factory) --> dict with default factory\n\ |
| 1613 | \n\ |
| 1614 | The default factory is called without arguments to produce\n\ |
| 1615 | a new value when a key is not present, in __getitem__ only.\n\ |
| 1616 | A defaultdict compares equal to a dict with the same items.\n\ |
| 1617 | "); |
| 1618 | |
Anthony Baxter | 3b8ff31 | 2006-04-04 15:05:23 +0000 | [diff] [blame] | 1619 | /* See comment in xxsubtype.c */ |
| 1620 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 1621 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1622 | static PyTypeObject defdict_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1623 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 1624 | "collections.defaultdict", /* tp_name */ |
| 1625 | sizeof(defdictobject), /* tp_basicsize */ |
| 1626 | 0, /* tp_itemsize */ |
| 1627 | /* methods */ |
| 1628 | (destructor)defdict_dealloc, /* tp_dealloc */ |
| 1629 | (printfunc)defdict_print, /* tp_print */ |
| 1630 | 0, /* tp_getattr */ |
| 1631 | 0, /* tp_setattr */ |
| 1632 | 0, /* tp_compare */ |
| 1633 | (reprfunc)defdict_repr, /* tp_repr */ |
| 1634 | 0, /* tp_as_number */ |
| 1635 | 0, /* tp_as_sequence */ |
| 1636 | 0, /* tp_as_mapping */ |
| 1637 | 0, /* tp_hash */ |
| 1638 | 0, /* tp_call */ |
| 1639 | 0, /* tp_str */ |
| 1640 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1641 | 0, /* tp_setattro */ |
| 1642 | 0, /* tp_as_buffer */ |
| 1643 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 1644 | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 1645 | defdict_doc, /* tp_doc */ |
| 1646 | defdict_traverse, /* tp_traverse */ |
| 1647 | (inquiry)defdict_tp_clear, /* tp_clear */ |
| 1648 | 0, /* tp_richcompare */ |
| 1649 | 0, /* tp_weaklistoffset*/ |
| 1650 | 0, /* tp_iter */ |
| 1651 | 0, /* tp_iternext */ |
| 1652 | defdict_methods, /* tp_methods */ |
| 1653 | defdict_members, /* tp_members */ |
| 1654 | 0, /* tp_getset */ |
| 1655 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */ |
| 1656 | 0, /* tp_dict */ |
| 1657 | 0, /* tp_descr_get */ |
| 1658 | 0, /* tp_descr_set */ |
| 1659 | 0, /* tp_dictoffset */ |
| 1660 | defdict_init, /* tp_init */ |
| 1661 | PyType_GenericAlloc, /* tp_alloc */ |
| 1662 | 0, /* tp_new */ |
| 1663 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1664 | }; |
| 1665 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1666 | /* module level code ********************************************************/ |
| 1667 | |
| 1668 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1669 | "High performance data structures.\n\ |
| 1670 | - deque: ordered collection accessible from endpoints only\n\ |
| 1671 | - defaultdict: dict subclass with a default value factory\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1672 | "); |
| 1673 | |
| 1674 | PyMODINIT_FUNC |
Raymond Hettinger | eb97988 | 2007-02-28 18:37:52 +0000 | [diff] [blame] | 1675 | init_collections(void) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1676 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1677 | PyObject *m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1679 | m = Py_InitModule3("_collections", NULL, module_doc); |
| 1680 | if (m == NULL) |
| 1681 | return; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1682 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1683 | if (PyType_Ready(&deque_type) < 0) |
| 1684 | return; |
| 1685 | Py_INCREF(&deque_type); |
| 1686 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1687 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1688 | defdict_type.tp_base = &PyDict_Type; |
| 1689 | if (PyType_Ready(&defdict_type) < 0) |
| 1690 | return; |
| 1691 | Py_INCREF(&defdict_type); |
| 1692 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1694 | if (PyType_Ready(&dequeiter_type) < 0) |
| 1695 | return; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1697 | if (PyType_Ready(&dequereviter_type) < 0) |
| 1698 | return; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1700 | return; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1701 | } |