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