Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 2 | #include "structmember.h" |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 3 | |
| 4 | /* collections module implementation of a deque() datatype |
| 5 | Written and maintained by Raymond D. Hettinger <python@rcn.com> |
| 6 | Copyright (c) 2004 Python Software Foundation. |
| 7 | All rights reserved. |
| 8 | */ |
| 9 | |
Raymond Hettinger | 77e8bf1 | 2004-10-01 15:25:53 +0000 | [diff] [blame] | 10 | /* The block length may be set to any number over 1. Larger numbers |
| 11 | * reduce the number of calls to the memory allocator but take more |
| 12 | * memory. Ideally, BLOCKLEN should be set with an eye to the |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13 | * length of a cache line. |
Raymond Hettinger | 77e8bf1 | 2004-10-01 15:25:53 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Raymond Hettinger | 7d112df | 2004-11-02 02:11:35 +0000 | [diff] [blame] | 16 | #define BLOCKLEN 62 |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 17 | #define CENTER ((BLOCKLEN - 1) / 2) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 18 | |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 19 | /* A `dequeobject` is composed of a doubly-linked list of `block` nodes. |
| 20 | * This list is not circular (the leftmost block has leftlink==NULL, |
| 21 | * and the rightmost block has rightlink==NULL). A deque d's first |
| 22 | * element is at d.leftblock[leftindex] and its last element is at |
| 23 | * d.rightblock[rightindex]; note that, unlike as for Python slice |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 24 | * indices, these indices are inclusive on both ends. By being inclusive |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 25 | * on both ends, algorithms for left and right operations become |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 26 | * symmetrical which simplifies the design. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | * |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 28 | * The list of blocks is never empty, so d.leftblock and d.rightblock |
| 29 | * are never equal to NULL. |
| 30 | * |
| 31 | * The indices, d.leftindex and d.rightindex are always in the range |
| 32 | * 0 <= index < BLOCKLEN. |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 33 | * Their exact relationship is: |
| 34 | * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex. |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 35 | * |
| 36 | * Empty deques have d.len == 0; d.leftblock==d.rightblock; |
| 37 | * d.leftindex == CENTER+1; and d.rightindex == CENTER. |
| 38 | * Checking for d.len == 0 is the intended way to see whether d is empty. |
| 39 | * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 40 | * Whenever d.leftblock == d.rightblock, |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 41 | * d.leftindex + d.len - 1 == d.rightindex. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 42 | * |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 43 | * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 44 | * become indices into distinct blocks and either may be larger than the |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 45 | * other. |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 48 | typedef struct BLOCK { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | struct BLOCK *leftlink; |
| 50 | struct BLOCK *rightlink; |
| 51 | PyObject *data[BLOCKLEN]; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 52 | } block; |
| 53 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 54 | #define MAXFREEBLOCKS 10 |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 55 | static Py_ssize_t numfreeblocks = 0; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 56 | static block *freeblocks[MAXFREEBLOCKS]; |
| 57 | |
Tim Peters | 6f85356 | 2004-10-01 01:04:50 +0000 | [diff] [blame] | 58 | static block * |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 59 | newblock(block *leftlink, block *rightlink, Py_ssize_t len) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | block *b; |
| 61 | /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we |
| 62 | * refuse to allocate new blocks if the current len is dangerously |
| 63 | * close. There is some extra margin to prevent spurious arithmetic |
| 64 | * overflows at various places. The following check ensures that |
| 65 | * the blocks allocated to the deque, in the worst case, can only |
| 66 | * have PY_SSIZE_T_MAX-2 entries in total. |
| 67 | */ |
| 68 | if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) { |
| 69 | PyErr_SetString(PyExc_OverflowError, |
| 70 | "cannot add more blocks to the deque"); |
| 71 | return NULL; |
| 72 | } |
| 73 | if (numfreeblocks) { |
| 74 | numfreeblocks -= 1; |
| 75 | b = freeblocks[numfreeblocks]; |
| 76 | } else { |
| 77 | b = PyMem_Malloc(sizeof(block)); |
| 78 | if (b == NULL) { |
| 79 | PyErr_NoMemory(); |
| 80 | return NULL; |
| 81 | } |
| 82 | } |
| 83 | b->leftlink = leftlink; |
| 84 | b->rightlink = rightlink; |
| 85 | return b; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 88 | static void |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 89 | freeblock(block *b) |
| 90 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | if (numfreeblocks < MAXFREEBLOCKS) { |
| 92 | freeblocks[numfreeblocks] = b; |
| 93 | numfreeblocks++; |
| 94 | } else { |
| 95 | PyMem_Free(b); |
| 96 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 99 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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 | f95a1b3 | 2010-05-09 15:52:27 +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); \ |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | dequeobject *deque; |
| 133 | block *b; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | PyObject *item; |
| 163 | block *prevblock; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | PyObject *item; |
| 199 | block *prevblock; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 234 | deque_append(dequeobject *deque, PyObject *item) |
| 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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 | f95a1b3 | 2010-05-09 15:52:27 +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; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque."); |
| 278 | |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | /* Run an iterator to exhaustion. Shortcut for |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 281 | the extend/extendleft methods when maxlen == 0. */ |
| 282 | static PyObject* |
| 283 | consume_iterator(PyObject *it) |
| 284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | PyObject *item; |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyObject *it, *item; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | if (deque->maxlen == 0) |
| 317 | return consume_iterator(it); |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | PyObject *it, *item; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | if (deque->maxlen == 0) |
| 369 | return consume_iterator(it); |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 400 | static PyObject * |
| 401 | deque_inplace_concat(dequeobject *deque, PyObject *other) |
| 402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | PyObject *result; |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 3f9afd8 | 2009-12-10 03:03:02 +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 | 986bbfc | 2013-02-09 20:00:55 -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 | 986bbfc | 2013-02-09 20:00:55 -0500 | [diff] [blame^] | 418 | if (len <= 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 986bbfc | 2013-02-09 20:00:55 -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 | 986bbfc | 2013-02-09 20:00:55 -0500 | [diff] [blame^] | 430 | deque->state++; |
| 431 | while (n > 0) { |
| 432 | if (deque->leftindex == 0) { |
| 433 | block *b = newblock(NULL, deque->leftblock, len); |
| 434 | if (b == NULL) |
| 435 | return -1; |
| 436 | assert(deque->leftblock->leftlink == NULL); |
| 437 | deque->leftblock->leftlink = b; |
| 438 | deque->leftblock = b; |
| 439 | deque->leftindex = BLOCKLEN; |
| 440 | } |
| 441 | assert(deque->leftindex > 0); |
| 442 | |
| 443 | m = n; |
| 444 | if (m > deque->rightindex + 1) |
| 445 | m = deque->rightindex + 1; |
| 446 | if (m > deque->leftindex) |
| 447 | m = deque->leftindex; |
| 448 | assert (m > 0 && m <= len); |
| 449 | memcpy(&deque->leftblock->data[deque->leftindex - m], |
| 450 | &deque->rightblock->data[deque->rightindex + 1 - m], |
| 451 | m * sizeof(PyObject *)); |
| 452 | deque->rightindex -= m; |
| 453 | deque->leftindex -= m; |
| 454 | n -= m; |
| 455 | |
| 456 | if (deque->rightindex == -1) { |
| 457 | block *prevblock = deque->rightblock->leftlink; |
| 458 | assert(deque->rightblock != NULL); |
| 459 | assert(deque->leftblock != deque->rightblock); |
| 460 | freeblock(deque->rightblock); |
| 461 | prevblock->rightlink = NULL; |
| 462 | deque->rightblock = prevblock; |
| 463 | deque->rightindex = BLOCKLEN - 1; |
| 464 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | } |
Raymond Hettinger | 986bbfc | 2013-02-09 20:00:55 -0500 | [diff] [blame^] | 466 | while (n < 0) { |
| 467 | if (deque->rightindex == BLOCKLEN - 1) { |
| 468 | block *b = newblock(deque->rightblock, NULL, len); |
| 469 | if (b == NULL) |
| 470 | return -1; |
| 471 | assert(deque->rightblock->rightlink == NULL); |
| 472 | deque->rightblock->rightlink = b; |
| 473 | deque->rightblock = b; |
| 474 | deque->rightindex = -1; |
| 475 | } |
| 476 | assert (deque->rightindex < BLOCKLEN - 1); |
| 477 | |
| 478 | m = -n; |
| 479 | if (m > BLOCKLEN - deque->leftindex) |
| 480 | m = BLOCKLEN - deque->leftindex; |
| 481 | if (m > BLOCKLEN - 1 - deque->rightindex) |
| 482 | m = BLOCKLEN - 1 - deque->rightindex; |
| 483 | assert (m > 0 && m <= len); |
| 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; |
| 489 | n += m; |
| 490 | |
| 491 | if (deque->leftindex == BLOCKLEN) { |
| 492 | block *nextblock = deque->leftblock->rightlink; |
| 493 | assert(deque->leftblock != deque->rightblock); |
| 494 | freeblock(deque->leftblock); |
| 495 | assert(nextblock != NULL); |
| 496 | nextblock->leftlink = NULL; |
| 497 | deque->leftblock = nextblock; |
| 498 | deque->leftindex = 0; |
| 499 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | Py_ssize_t n=1; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 519 | static PyObject * |
| 520 | deque_reverse(dequeobject *deque, PyObject *unused) |
| 521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | /* Swap */ |
| 535 | tmp = leftblock->data[leftindex]; |
| 536 | leftblock->data[leftindex] = rightblock->data[rightindex]; |
| 537 | rightblock->data[rightindex] = tmp; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | /* Advance left block/index pair */ |
| 540 | leftindex++; |
| 541 | if (leftindex == BLOCKLEN) { |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 542 | if (leftblock->rightlink == NULL) |
| 543 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | leftblock = leftblock->rightlink; |
| 545 | leftindex = 0; |
| 546 | } |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | /* Step backwards with the right block/index pair */ |
| 549 | rightindex--; |
| 550 | if (rightindex == -1) { |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 551 | if (rightblock->leftlink == NULL) |
| 552 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | rightblock = rightblock->leftlink; |
| 554 | rightindex = BLOCKLEN - 1; |
| 555 | } |
| 556 | } |
| 557 | Py_RETURN_NONE; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | PyDoc_STRVAR(reverse_doc, |
| 561 | "D.reverse() -- reverse *IN PLACE*"); |
| 562 | |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 563 | static PyObject * |
| 564 | deque_count(dequeobject *deque, PyObject *v) |
| 565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | block *leftblock = deque->leftblock; |
| 567 | Py_ssize_t leftindex = deque->leftindex; |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 568 | Py_ssize_t n = deque->len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | /* Advance left block/index pair */ |
| 590 | leftindex++; |
| 591 | if (leftindex == BLOCKLEN) { |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 592 | if (leftblock->rightlink == NULL) /* can occur when i==n-1 */ |
| 593 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | leftblock = leftblock->rightlink; |
| 595 | leftindex = 0; |
| 596 | } |
| 597 | } |
| 598 | return PyLong_FromSsize_t(count); |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | 0e5c48a | 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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | PyObject *item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 * |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | PyObject *item; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | 0e5c48a | 2013-01-12 21:22:18 -0500 | [diff] [blame] | 760 | deque_clear(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | PyObject *dict, *result, *aslist; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 820 | _Py_IDENTIFIER(__dict__); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 821 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 822 | dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | if (dict == NULL) |
| 824 | PyErr_Clear(); |
| 825 | aslist = PySequence_List((PyObject *)deque); |
| 826 | if (aslist == NULL) { |
| 827 | Py_XDECREF(dict); |
| 828 | return NULL; |
| 829 | } |
| 830 | if (dict == NULL) { |
| 831 | if (deque->maxlen == -1) |
| 832 | result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist); |
| 833 | else |
| 834 | result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen); |
| 835 | } else { |
| 836 | if (deque->maxlen == -1) |
| 837 | result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict); |
| 838 | else |
| 839 | result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict); |
| 840 | } |
| 841 | Py_XDECREF(dict); |
| 842 | Py_DECREF(aslist); |
| 843 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 847 | |
| 848 | static PyObject * |
| 849 | deque_repr(PyObject *deque) |
| 850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | PyObject *aslist, *result; |
| 852 | int i; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | i = Py_ReprEnter(deque); |
| 855 | if (i != 0) { |
| 856 | if (i < 0) |
| 857 | return NULL; |
| 858 | return PyUnicode_FromString("[...]"); |
| 859 | } |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | aslist = PySequence_List(deque); |
| 862 | if (aslist == NULL) { |
| 863 | Py_ReprLeave(deque); |
| 864 | return NULL; |
| 865 | } |
| 866 | if (((dequeobject *)deque)->maxlen != -1) |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)", |
| 869 | aslist, ((dequeobject *)deque)->maxlen); |
| 870 | else |
| 871 | result = PyUnicode_FromFormat("deque(%R)", aslist); |
| 872 | Py_DECREF(aslist); |
| 873 | Py_ReprLeave(deque); |
| 874 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 877 | static PyObject * |
| 878 | deque_richcompare(PyObject *v, PyObject *w, int op) |
| 879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | PyObject *it1=NULL, *it2=NULL, *x, *y; |
| 881 | Py_ssize_t vs, ws; |
| 882 | int b, cmp=-1; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | if (!PyObject_TypeCheck(v, &deque_type) || |
| 885 | !PyObject_TypeCheck(w, &deque_type)) { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 886 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | /* Shortcuts */ |
| 890 | vs = ((dequeobject *)v)->len; |
| 891 | ws = ((dequeobject *)w)->len; |
| 892 | if (op == Py_EQ) { |
| 893 | if (v == w) |
| 894 | Py_RETURN_TRUE; |
| 895 | if (vs != ws) |
| 896 | Py_RETURN_FALSE; |
| 897 | } |
| 898 | if (op == Py_NE) { |
| 899 | if (v == w) |
| 900 | Py_RETURN_FALSE; |
| 901 | if (vs != ws) |
| 902 | Py_RETURN_TRUE; |
| 903 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | /* Search for the first index where items are different */ |
| 906 | it1 = PyObject_GetIter(v); |
| 907 | if (it1 == NULL) |
| 908 | goto done; |
| 909 | it2 = PyObject_GetIter(w); |
| 910 | if (it2 == NULL) |
| 911 | goto done; |
| 912 | for (;;) { |
| 913 | x = PyIter_Next(it1); |
| 914 | if (x == NULL && PyErr_Occurred()) |
| 915 | goto done; |
| 916 | y = PyIter_Next(it2); |
| 917 | if (x == NULL || y == NULL) |
| 918 | break; |
| 919 | b = PyObject_RichCompareBool(x, y, Py_EQ); |
| 920 | if (b == 0) { |
| 921 | cmp = PyObject_RichCompareBool(x, y, op); |
| 922 | Py_DECREF(x); |
| 923 | Py_DECREF(y); |
| 924 | goto done; |
| 925 | } |
| 926 | Py_DECREF(x); |
| 927 | Py_DECREF(y); |
| 928 | if (b == -1) |
| 929 | goto done; |
| 930 | } |
| 931 | /* We reached the end of one deque or both */ |
| 932 | Py_XDECREF(x); |
| 933 | Py_XDECREF(y); |
| 934 | if (PyErr_Occurred()) |
| 935 | goto done; |
| 936 | switch (op) { |
| 937 | case Py_LT: cmp = y != NULL; break; /* if w was longer */ |
| 938 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */ |
| 939 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ |
| 940 | case Py_NE: cmp = x != y; break; /* if one deque continues */ |
| 941 | case Py_GT: cmp = x != NULL; break; /* if v was longer */ |
| 942 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */ |
| 943 | } |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 944 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 945 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | Py_XDECREF(it1); |
| 947 | Py_XDECREF(it2); |
| 948 | if (cmp == 1) |
| 949 | Py_RETURN_TRUE; |
| 950 | if (cmp == 0) |
| 951 | Py_RETURN_FALSE; |
| 952 | return NULL; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 955 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 956 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 957 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | PyObject *iterable = NULL; |
| 959 | PyObject *maxlenobj = NULL; |
| 960 | Py_ssize_t maxlen = -1; |
| 961 | char *kwlist[] = {"iterable", "maxlen", 0}; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) |
| 964 | return -1; |
| 965 | if (maxlenobj != NULL && maxlenobj != Py_None) { |
| 966 | maxlen = PyLong_AsSsize_t(maxlenobj); |
| 967 | if (maxlen == -1 && PyErr_Occurred()) |
| 968 | return -1; |
| 969 | if (maxlen < 0) { |
| 970 | PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); |
| 971 | return -1; |
| 972 | } |
| 973 | } |
| 974 | deque->maxlen = maxlen; |
| 975 | deque_clear(deque); |
| 976 | if (iterable != NULL) { |
| 977 | PyObject *rv = deque_extend(deque, iterable); |
| 978 | if (rv == NULL) |
| 979 | return -1; |
| 980 | Py_DECREF(rv); |
| 981 | } |
| 982 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 985 | static PyObject * |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 986 | deque_sizeof(dequeobject *deque, void *unused) |
| 987 | { |
| 988 | Py_ssize_t res; |
| 989 | Py_ssize_t blocks; |
| 990 | |
| 991 | res = sizeof(dequeobject); |
| 992 | blocks = (deque->leftindex + deque->len + BLOCKLEN - 1) / BLOCKLEN; |
| 993 | assert(deque->leftindex + deque->len - 1 == |
| 994 | (blocks - 1) * BLOCKLEN + deque->rightindex); |
| 995 | res += blocks * sizeof(block); |
| 996 | return PyLong_FromSsize_t(res); |
| 997 | } |
| 998 | |
| 999 | PyDoc_STRVAR(sizeof_doc, |
| 1000 | "D.__sizeof__() -- size of D in memory, in bytes"); |
| 1001 | |
| 1002 | static PyObject * |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 1003 | deque_get_maxlen(dequeobject *deque) |
| 1004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | if (deque->maxlen == -1) |
| 1006 | Py_RETURN_NONE; |
| 1007 | return PyLong_FromSsize_t(deque->maxlen); |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | static PyGetSetDef deque_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | {"maxlen", (getter)deque_get_maxlen, (setter)NULL, |
| 1012 | "maximum size of a deque or None if unbounded"}, |
| 1013 | {0} |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 1014 | }; |
| 1015 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1016 | static PySequenceMethods deque_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | (lenfunc)deque_len, /* sq_length */ |
| 1018 | 0, /* sq_concat */ |
| 1019 | 0, /* sq_repeat */ |
| 1020 | (ssizeargfunc)deque_item, /* sq_item */ |
| 1021 | 0, /* sq_slice */ |
| 1022 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ |
| 1023 | 0, /* sq_ass_slice */ |
| 1024 | 0, /* sq_contains */ |
| 1025 | (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */ |
| 1026 | 0, /* sq_inplace_repeat */ |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 1027 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1028 | }; |
| 1029 | |
| 1030 | /* deque object ********************************************************/ |
| 1031 | |
| 1032 | static PyObject *deque_iter(dequeobject *deque); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1033 | static PyObject *deque_reviter(dequeobject *deque); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 1034 | PyDoc_STRVAR(reversed_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | "D.__reversed__() -- return a reverse iterator over the deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1036 | |
| 1037 | static PyMethodDef deque_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | {"append", (PyCFunction)deque_append, |
| 1039 | METH_O, append_doc}, |
| 1040 | {"appendleft", (PyCFunction)deque_appendleft, |
| 1041 | METH_O, appendleft_doc}, |
| 1042 | {"clear", (PyCFunction)deque_clearmethod, |
| 1043 | METH_NOARGS, clear_doc}, |
| 1044 | {"__copy__", (PyCFunction)deque_copy, |
| 1045 | METH_NOARGS, copy_doc}, |
| 1046 | {"count", (PyCFunction)deque_count, |
| 1047 | METH_O, count_doc}, |
| 1048 | {"extend", (PyCFunction)deque_extend, |
| 1049 | METH_O, extend_doc}, |
| 1050 | {"extendleft", (PyCFunction)deque_extendleft, |
| 1051 | METH_O, extendleft_doc}, |
| 1052 | {"pop", (PyCFunction)deque_pop, |
| 1053 | METH_NOARGS, pop_doc}, |
| 1054 | {"popleft", (PyCFunction)deque_popleft, |
| 1055 | METH_NOARGS, popleft_doc}, |
| 1056 | {"__reduce__", (PyCFunction)deque_reduce, |
| 1057 | METH_NOARGS, reduce_doc}, |
| 1058 | {"remove", (PyCFunction)deque_remove, |
| 1059 | METH_O, remove_doc}, |
| 1060 | {"__reversed__", (PyCFunction)deque_reviter, |
| 1061 | METH_NOARGS, reversed_doc}, |
| 1062 | {"reverse", (PyCFunction)deque_reverse, |
| 1063 | METH_NOARGS, reverse_doc}, |
| 1064 | {"rotate", (PyCFunction)deque_rotate, |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 1065 | METH_VARARGS, rotate_doc}, |
| 1066 | {"__sizeof__", (PyCFunction)deque_sizeof, |
| 1067 | METH_NOARGS, sizeof_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1069 | }; |
| 1070 | |
| 1071 | PyDoc_STRVAR(deque_doc, |
Andrew Svetlov | 6a5c7c3 | 2012-10-31 11:50:40 +0200 | [diff] [blame] | 1072 | "deque([iterable[, maxlen]]) --> deque object\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1073 | \n\ |
Raymond Hettinger | 4974705 | 2011-03-29 17:36:31 -0700 | [diff] [blame] | 1074 | Build an ordered collection with optimized access from its endpoints."); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1075 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 1076 | static PyTypeObject deque_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1078 | "collections.deque", /* tp_name */ |
| 1079 | sizeof(dequeobject), /* tp_basicsize */ |
| 1080 | 0, /* tp_itemsize */ |
| 1081 | /* methods */ |
| 1082 | (destructor)deque_dealloc, /* tp_dealloc */ |
| 1083 | 0, /* tp_print */ |
| 1084 | 0, /* tp_getattr */ |
| 1085 | 0, /* tp_setattr */ |
| 1086 | 0, /* tp_reserved */ |
| 1087 | deque_repr, /* tp_repr */ |
| 1088 | 0, /* tp_as_number */ |
| 1089 | &deque_as_sequence, /* tp_as_sequence */ |
| 1090 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1091 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | 0, /* tp_call */ |
| 1093 | 0, /* tp_str */ |
| 1094 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1095 | 0, /* tp_setattro */ |
| 1096 | 0, /* tp_as_buffer */ |
| 1097 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1098 | /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | deque_doc, /* tp_doc */ |
| 1100 | (traverseproc)deque_traverse, /* tp_traverse */ |
| 1101 | (inquiry)deque_clear, /* tp_clear */ |
| 1102 | (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1103 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | (getiterfunc)deque_iter, /* tp_iter */ |
| 1105 | 0, /* tp_iternext */ |
| 1106 | deque_methods, /* tp_methods */ |
| 1107 | 0, /* tp_members */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1108 | deque_getset, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | 0, /* tp_base */ |
| 1110 | 0, /* tp_dict */ |
| 1111 | 0, /* tp_descr_get */ |
| 1112 | 0, /* tp_descr_set */ |
| 1113 | 0, /* tp_dictoffset */ |
| 1114 | (initproc)deque_init, /* tp_init */ |
| 1115 | PyType_GenericAlloc, /* tp_alloc */ |
| 1116 | deque_new, /* tp_new */ |
| 1117 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1118 | }; |
| 1119 | |
| 1120 | /*********************** Deque Iterator **************************/ |
| 1121 | |
| 1122 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | PyObject_HEAD |
| 1124 | Py_ssize_t index; |
| 1125 | block *b; |
| 1126 | dequeobject *deque; |
| 1127 | long state; /* state when the iterator is created */ |
| 1128 | Py_ssize_t counter; /* number of items remaining for iteration */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1129 | } dequeiterobject; |
| 1130 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1131 | static PyTypeObject dequeiter_type; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1132 | |
| 1133 | static PyObject * |
| 1134 | deque_iter(dequeobject *deque) |
| 1135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | dequeiterobject *it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | it = PyObject_GC_New(dequeiterobject, &dequeiter_type); |
| 1139 | if (it == NULL) |
| 1140 | return NULL; |
| 1141 | it->b = deque->leftblock; |
| 1142 | it->index = deque->leftindex; |
| 1143 | Py_INCREF(deque); |
| 1144 | it->deque = deque; |
| 1145 | it->state = deque->state; |
| 1146 | it->counter = deque->len; |
| 1147 | PyObject_GC_Track(it); |
| 1148 | return (PyObject *)it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 1151 | static int |
| 1152 | dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg) |
| 1153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | Py_VISIT(dio->deque); |
| 1155 | return 0; |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1158 | static void |
| 1159 | dequeiter_dealloc(dequeiterobject *dio) |
| 1160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | Py_XDECREF(dio->deque); |
| 1162 | PyObject_GC_Del(dio); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | static PyObject * |
| 1166 | dequeiter_next(dequeiterobject *it) |
| 1167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | if (it->deque->state != it->state) { |
| 1171 | it->counter = 0; |
| 1172 | PyErr_SetString(PyExc_RuntimeError, |
| 1173 | "deque mutated during iteration"); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | if (it->counter == 0) |
| 1177 | return NULL; |
| 1178 | assert (!(it->b == it->deque->rightblock && |
| 1179 | it->index > it->deque->rightindex)); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | item = it->b->data[it->index]; |
| 1182 | it->index++; |
| 1183 | it->counter--; |
| 1184 | if (it->index == BLOCKLEN && it->counter > 0) { |
| 1185 | assert (it->b->rightlink != NULL); |
| 1186 | it->b = it->b->rightlink; |
| 1187 | it->index = 0; |
| 1188 | } |
| 1189 | Py_INCREF(item); |
| 1190 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1193 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1194 | dequeiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1195 | { |
| 1196 | Py_ssize_t i, index=0; |
| 1197 | PyObject *deque; |
| 1198 | dequeiterobject *it; |
| 1199 | if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index)) |
| 1200 | return NULL; |
| 1201 | assert(type == &dequeiter_type); |
| 1202 | |
| 1203 | it = (dequeiterobject*)deque_iter((dequeobject *)deque); |
| 1204 | if (!it) |
| 1205 | return NULL; |
| 1206 | /* consume items from the queue */ |
| 1207 | for(i=0; i<index; i++) { |
| 1208 | PyObject *item = dequeiter_next(it); |
| 1209 | if (item) { |
| 1210 | Py_DECREF(item); |
| 1211 | } else { |
| 1212 | if (it->counter) { |
| 1213 | Py_DECREF(it); |
| 1214 | return NULL; |
| 1215 | } else |
| 1216 | break; |
| 1217 | } |
| 1218 | } |
| 1219 | return (PyObject*)it; |
| 1220 | } |
| 1221 | |
| 1222 | static PyObject * |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1223 | dequeiter_len(dequeiterobject *it) |
| 1224 | { |
Antoine Pitrou | 554f334 | 2010-08-17 18:30:06 +0000 | [diff] [blame] | 1225 | return PyLong_FromSsize_t(it->counter); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 1228 | 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] | 1229 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1230 | static PyObject * |
| 1231 | dequeiter_reduce(dequeiterobject *it) |
| 1232 | { |
| 1233 | return Py_BuildValue("O(On)", Py_TYPE(it), it->deque, it->deque->len - it->counter); |
| 1234 | } |
| 1235 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1236 | static PyMethodDef dequeiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1238 | {"__reduce__", (PyCFunction)dequeiter_reduce, METH_NOARGS, reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1240 | }; |
| 1241 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1242 | static PyTypeObject dequeiter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1244 | "_collections._deque_iterator", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1246 | 0, /* tp_itemsize */ |
| 1247 | /* methods */ |
| 1248 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1249 | 0, /* tp_print */ |
| 1250 | 0, /* tp_getattr */ |
| 1251 | 0, /* tp_setattr */ |
| 1252 | 0, /* tp_reserved */ |
| 1253 | 0, /* tp_repr */ |
| 1254 | 0, /* tp_as_number */ |
| 1255 | 0, /* tp_as_sequence */ |
| 1256 | 0, /* tp_as_mapping */ |
| 1257 | 0, /* tp_hash */ |
| 1258 | 0, /* tp_call */ |
| 1259 | 0, /* tp_str */ |
| 1260 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1261 | 0, /* tp_setattro */ |
| 1262 | 0, /* tp_as_buffer */ |
| 1263 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1264 | 0, /* tp_doc */ |
| 1265 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1266 | 0, /* tp_clear */ |
| 1267 | 0, /* tp_richcompare */ |
| 1268 | 0, /* tp_weaklistoffset */ |
| 1269 | PyObject_SelfIter, /* tp_iter */ |
| 1270 | (iternextfunc)dequeiter_next, /* tp_iternext */ |
| 1271 | dequeiter_methods, /* tp_methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1272 | 0, /* tp_members */ |
| 1273 | 0, /* tp_getset */ |
| 1274 | 0, /* tp_base */ |
| 1275 | 0, /* tp_dict */ |
| 1276 | 0, /* tp_descr_get */ |
| 1277 | 0, /* tp_descr_set */ |
| 1278 | 0, /* tp_dictoffset */ |
| 1279 | 0, /* tp_init */ |
| 1280 | 0, /* tp_alloc */ |
| 1281 | dequeiter_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | 0, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1283 | }; |
| 1284 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1285 | /*********************** Deque Reverse Iterator **************************/ |
| 1286 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1287 | static PyTypeObject dequereviter_type; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1288 | |
| 1289 | static PyObject * |
| 1290 | deque_reviter(dequeobject *deque) |
| 1291 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | dequeiterobject *it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | it = PyObject_GC_New(dequeiterobject, &dequereviter_type); |
| 1295 | if (it == NULL) |
| 1296 | return NULL; |
| 1297 | it->b = deque->rightblock; |
| 1298 | it->index = deque->rightindex; |
| 1299 | Py_INCREF(deque); |
| 1300 | it->deque = deque; |
| 1301 | it->state = deque->state; |
| 1302 | it->counter = deque->len; |
| 1303 | PyObject_GC_Track(it); |
| 1304 | return (PyObject *)it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | static PyObject * |
| 1308 | dequereviter_next(dequeiterobject *it) |
| 1309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | PyObject *item; |
| 1311 | if (it->counter == 0) |
| 1312 | return NULL; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | if (it->deque->state != it->state) { |
| 1315 | it->counter = 0; |
| 1316 | PyErr_SetString(PyExc_RuntimeError, |
| 1317 | "deque mutated during iteration"); |
| 1318 | return NULL; |
| 1319 | } |
| 1320 | assert (!(it->b == it->deque->leftblock && |
| 1321 | it->index < it->deque->leftindex)); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | item = it->b->data[it->index]; |
| 1324 | it->index--; |
| 1325 | it->counter--; |
| 1326 | if (it->index == -1 && it->counter > 0) { |
| 1327 | assert (it->b->leftlink != NULL); |
| 1328 | it->b = it->b->leftlink; |
| 1329 | it->index = BLOCKLEN - 1; |
| 1330 | } |
| 1331 | Py_INCREF(item); |
| 1332 | return item; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1335 | static PyObject * |
| 1336 | dequereviter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1337 | { |
| 1338 | Py_ssize_t i, index=0; |
| 1339 | PyObject *deque; |
| 1340 | dequeiterobject *it; |
| 1341 | if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index)) |
| 1342 | return NULL; |
| 1343 | assert(type == &dequereviter_type); |
| 1344 | |
| 1345 | it = (dequeiterobject*)deque_reviter((dequeobject *)deque); |
| 1346 | if (!it) |
| 1347 | return NULL; |
| 1348 | /* consume items from the queue */ |
| 1349 | for(i=0; i<index; i++) { |
| 1350 | PyObject *item = dequereviter_next(it); |
| 1351 | if (item) { |
| 1352 | Py_DECREF(item); |
| 1353 | } else { |
| 1354 | if (it->counter) { |
| 1355 | Py_DECREF(it); |
| 1356 | return NULL; |
| 1357 | } else |
| 1358 | break; |
| 1359 | } |
| 1360 | } |
| 1361 | return (PyObject*)it; |
| 1362 | } |
| 1363 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1364 | static PyTypeObject dequereviter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1366 | "_collections._deque_reverse_iterator", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1368 | 0, /* tp_itemsize */ |
| 1369 | /* methods */ |
| 1370 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1371 | 0, /* tp_print */ |
| 1372 | 0, /* tp_getattr */ |
| 1373 | 0, /* tp_setattr */ |
| 1374 | 0, /* tp_reserved */ |
| 1375 | 0, /* tp_repr */ |
| 1376 | 0, /* tp_as_number */ |
| 1377 | 0, /* tp_as_sequence */ |
| 1378 | 0, /* tp_as_mapping */ |
| 1379 | 0, /* tp_hash */ |
| 1380 | 0, /* tp_call */ |
| 1381 | 0, /* tp_str */ |
| 1382 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1383 | 0, /* tp_setattro */ |
| 1384 | 0, /* tp_as_buffer */ |
| 1385 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1386 | 0, /* tp_doc */ |
| 1387 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1388 | 0, /* tp_clear */ |
| 1389 | 0, /* tp_richcompare */ |
| 1390 | 0, /* tp_weaklistoffset */ |
| 1391 | PyObject_SelfIter, /* tp_iter */ |
| 1392 | (iternextfunc)dequereviter_next, /* tp_iternext */ |
| 1393 | dequeiter_methods, /* tp_methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1394 | 0, /* tp_members */ |
| 1395 | 0, /* tp_getset */ |
| 1396 | 0, /* tp_base */ |
| 1397 | 0, /* tp_dict */ |
| 1398 | 0, /* tp_descr_get */ |
| 1399 | 0, /* tp_descr_set */ |
| 1400 | 0, /* tp_dictoffset */ |
| 1401 | 0, /* tp_init */ |
| 1402 | 0, /* tp_alloc */ |
| 1403 | dequereviter_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | 0, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1405 | }; |
| 1406 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1407 | /* defaultdict type *********************************************************/ |
| 1408 | |
| 1409 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | PyDictObject dict; |
| 1411 | PyObject *default_factory; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1412 | } defdictobject; |
| 1413 | |
| 1414 | static PyTypeObject defdict_type; /* Forward */ |
| 1415 | |
| 1416 | PyDoc_STRVAR(defdict_missing_doc, |
| 1417 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1418 | if self.default_factory is None: raise KeyError((key,))\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1419 | self[key] = value = self.default_factory()\n\ |
| 1420 | return value\n\ |
| 1421 | "); |
| 1422 | |
| 1423 | static PyObject * |
| 1424 | defdict_missing(defdictobject *dd, PyObject *key) |
| 1425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | PyObject *factory = dd->default_factory; |
| 1427 | PyObject *value; |
| 1428 | if (factory == NULL || factory == Py_None) { |
| 1429 | /* XXX Call dict.__missing__(key) */ |
| 1430 | PyObject *tup; |
| 1431 | tup = PyTuple_Pack(1, key); |
| 1432 | if (!tup) return NULL; |
| 1433 | PyErr_SetObject(PyExc_KeyError, tup); |
| 1434 | Py_DECREF(tup); |
| 1435 | return NULL; |
| 1436 | } |
| 1437 | value = PyEval_CallObject(factory, NULL); |
| 1438 | if (value == NULL) |
| 1439 | return value; |
| 1440 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) { |
| 1441 | Py_DECREF(value); |
| 1442 | return NULL; |
| 1443 | } |
| 1444 | return value; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D."); |
| 1448 | |
| 1449 | static PyObject * |
| 1450 | defdict_copy(defdictobject *dd) |
| 1451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | /* This calls the object's class. That only works for subclasses |
| 1453 | whose class constructor has the same signature. Subclasses that |
| 1454 | define a different constructor signature must override copy(). |
| 1455 | */ |
Raymond Hettinger | 54628fa | 2009-08-04 19:16:39 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | if (dd->default_factory == NULL) |
| 1458 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL); |
| 1459 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), |
| 1460 | dd->default_factory, dd, NULL); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static PyObject * |
| 1464 | defdict_reduce(defdictobject *dd) |
| 1465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | /* __reduce__ must return a 5-tuple as follows: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | - factory function |
| 1469 | - tuple of args for the factory function |
| 1470 | - additional state (here None) |
| 1471 | - sequence iterator (here None) |
| 1472 | - dictionary iterator (yielding successive (key, value) pairs |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | This API is used by pickle.py and copy.py. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | For this to be useful with pickle.py, the default_factory |
| 1477 | must be picklable; e.g., None, a built-in, or a global |
| 1478 | function in a module or package. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | Both shallow and deep copying are supported, but for deep |
| 1481 | copying, the default_factory must be deep-copyable; e.g. None, |
| 1482 | or a built-in (functions are not copyable at this time). |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | This only works for subclasses as long as their constructor |
| 1485 | signature is compatible; the first argument must be the |
| 1486 | optional default_factory, defaulting to None. |
| 1487 | */ |
| 1488 | PyObject *args; |
| 1489 | PyObject *items; |
| 1490 | PyObject *iter; |
| 1491 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1492 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | if (dd->default_factory == NULL || dd->default_factory == Py_None) |
| 1495 | args = PyTuple_New(0); |
| 1496 | else |
| 1497 | args = PyTuple_Pack(1, dd->default_factory); |
| 1498 | if (args == NULL) |
| 1499 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1500 | items = _PyObject_CallMethodId((PyObject *)dd, &PyId_items, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | if (items == NULL) { |
| 1502 | Py_DECREF(args); |
| 1503 | return NULL; |
| 1504 | } |
| 1505 | iter = PyObject_GetIter(items); |
| 1506 | if (iter == NULL) { |
| 1507 | Py_DECREF(items); |
| 1508 | Py_DECREF(args); |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | result = PyTuple_Pack(5, Py_TYPE(dd), args, |
| 1512 | Py_None, Py_None, iter); |
| 1513 | Py_DECREF(iter); |
| 1514 | Py_DECREF(items); |
| 1515 | Py_DECREF(args); |
| 1516 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | static PyMethodDef defdict_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | {"__missing__", (PyCFunction)defdict_missing, METH_O, |
| 1521 | defdict_missing_doc}, |
| 1522 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1523 | defdict_copy_doc}, |
| 1524 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1525 | defdict_copy_doc}, |
| 1526 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS, |
| 1527 | reduce_doc}, |
| 1528 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1529 | }; |
| 1530 | |
| 1531 | static PyMemberDef defdict_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | {"default_factory", T_OBJECT, |
| 1533 | offsetof(defdictobject, default_factory), 0, |
| 1534 | PyDoc_STR("Factory for default value called by __missing__().")}, |
| 1535 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1536 | }; |
| 1537 | |
| 1538 | static void |
| 1539 | defdict_dealloc(defdictobject *dd) |
| 1540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | Py_CLEAR(dd->default_factory); |
| 1542 | PyDict_Type.tp_dealloc((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1545 | static PyObject * |
| 1546 | defdict_repr(defdictobject *dd) |
| 1547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | PyObject *baserepr; |
| 1549 | PyObject *defrepr; |
| 1550 | PyObject *result; |
| 1551 | baserepr = PyDict_Type.tp_repr((PyObject *)dd); |
| 1552 | if (baserepr == NULL) |
| 1553 | return NULL; |
| 1554 | if (dd->default_factory == NULL) |
| 1555 | defrepr = PyUnicode_FromString("None"); |
| 1556 | else |
| 1557 | { |
| 1558 | int status = Py_ReprEnter(dd->default_factory); |
| 1559 | if (status != 0) { |
Antoine Pitrou | f5f1fe0 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1560 | if (status < 0) { |
| 1561 | Py_DECREF(baserepr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | return NULL; |
Antoine Pitrou | f5f1fe0 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1563 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | defrepr = PyUnicode_FromString("..."); |
| 1565 | } |
| 1566 | else |
| 1567 | defrepr = PyObject_Repr(dd->default_factory); |
| 1568 | Py_ReprLeave(dd->default_factory); |
| 1569 | } |
| 1570 | if (defrepr == NULL) { |
| 1571 | Py_DECREF(baserepr); |
| 1572 | return NULL; |
| 1573 | } |
| 1574 | result = PyUnicode_FromFormat("defaultdict(%U, %U)", |
| 1575 | defrepr, baserepr); |
| 1576 | Py_DECREF(defrepr); |
| 1577 | Py_DECREF(baserepr); |
| 1578 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | static int |
| 1582 | defdict_traverse(PyObject *self, visitproc visit, void *arg) |
| 1583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | Py_VISIT(((defdictobject *)self)->default_factory); |
| 1585 | return PyDict_Type.tp_traverse(self, visit, arg); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | static int |
| 1589 | defdict_tp_clear(defdictobject *dd) |
| 1590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | Py_CLEAR(dd->default_factory); |
| 1592 | return PyDict_Type.tp_clear((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static int |
| 1596 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | defdictobject *dd = (defdictobject *)self; |
| 1599 | PyObject *olddefault = dd->default_factory; |
| 1600 | PyObject *newdefault = NULL; |
| 1601 | PyObject *newargs; |
| 1602 | int result; |
| 1603 | if (args == NULL || !PyTuple_Check(args)) |
| 1604 | newargs = PyTuple_New(0); |
| 1605 | else { |
| 1606 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
| 1607 | if (n > 0) { |
| 1608 | newdefault = PyTuple_GET_ITEM(args, 0); |
| 1609 | if (!PyCallable_Check(newdefault) && newdefault != Py_None) { |
| 1610 | PyErr_SetString(PyExc_TypeError, |
| 1611 | "first argument must be callable"); |
| 1612 | return -1; |
| 1613 | } |
| 1614 | } |
| 1615 | newargs = PySequence_GetSlice(args, 1, n); |
| 1616 | } |
| 1617 | if (newargs == NULL) |
| 1618 | return -1; |
| 1619 | Py_XINCREF(newdefault); |
| 1620 | dd->default_factory = newdefault; |
| 1621 | result = PyDict_Type.tp_init(self, newargs, kwds); |
| 1622 | Py_DECREF(newargs); |
| 1623 | Py_XDECREF(olddefault); |
| 1624 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | PyDoc_STRVAR(defdict_doc, |
| 1628 | "defaultdict(default_factory) --> dict with default factory\n\ |
| 1629 | \n\ |
| 1630 | The default factory is called without arguments to produce\n\ |
| 1631 | a new value when a key is not present, in __getitem__ only.\n\ |
| 1632 | A defaultdict compares equal to a dict with the same items.\n\ |
| 1633 | "); |
| 1634 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1635 | /* See comment in xxsubtype.c */ |
| 1636 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 1637 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1638 | static PyTypeObject defdict_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 1640 | "collections.defaultdict", /* tp_name */ |
| 1641 | sizeof(defdictobject), /* tp_basicsize */ |
| 1642 | 0, /* tp_itemsize */ |
| 1643 | /* methods */ |
| 1644 | (destructor)defdict_dealloc, /* tp_dealloc */ |
| 1645 | 0, /* tp_print */ |
| 1646 | 0, /* tp_getattr */ |
| 1647 | 0, /* tp_setattr */ |
| 1648 | 0, /* tp_reserved */ |
| 1649 | (reprfunc)defdict_repr, /* tp_repr */ |
| 1650 | 0, /* tp_as_number */ |
| 1651 | 0, /* tp_as_sequence */ |
| 1652 | 0, /* tp_as_mapping */ |
| 1653 | 0, /* tp_hash */ |
| 1654 | 0, /* tp_call */ |
| 1655 | 0, /* tp_str */ |
| 1656 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1657 | 0, /* tp_setattro */ |
| 1658 | 0, /* tp_as_buffer */ |
| 1659 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1660 | /* tp_flags */ |
| 1661 | defdict_doc, /* tp_doc */ |
| 1662 | defdict_traverse, /* tp_traverse */ |
| 1663 | (inquiry)defdict_tp_clear, /* tp_clear */ |
| 1664 | 0, /* tp_richcompare */ |
| 1665 | 0, /* tp_weaklistoffset*/ |
| 1666 | 0, /* tp_iter */ |
| 1667 | 0, /* tp_iternext */ |
| 1668 | defdict_methods, /* tp_methods */ |
| 1669 | defdict_members, /* tp_members */ |
| 1670 | 0, /* tp_getset */ |
| 1671 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */ |
| 1672 | 0, /* tp_dict */ |
| 1673 | 0, /* tp_descr_get */ |
| 1674 | 0, /* tp_descr_set */ |
| 1675 | 0, /* tp_dictoffset */ |
| 1676 | defdict_init, /* tp_init */ |
| 1677 | PyType_GenericAlloc, /* tp_alloc */ |
| 1678 | 0, /* tp_new */ |
| 1679 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1680 | }; |
| 1681 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1682 | /* helper function for Counter *********************************************/ |
| 1683 | |
| 1684 | PyDoc_STRVAR(_count_elements_doc, |
| 1685 | "_count_elements(mapping, iterable) -> None\n\ |
| 1686 | \n\ |
| 1687 | Count elements in the iterable, updating the mappping"); |
| 1688 | |
| 1689 | static PyObject * |
| 1690 | _count_elements(PyObject *self, PyObject *args) |
| 1691 | { |
| 1692 | PyObject *it, *iterable, *mapping, *oldval; |
| 1693 | PyObject *newval = NULL; |
| 1694 | PyObject *key = NULL; |
| 1695 | PyObject *one = NULL; |
| 1696 | |
| 1697 | if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable)) |
| 1698 | return NULL; |
| 1699 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1700 | it = PyObject_GetIter(iterable); |
| 1701 | if (it == NULL) |
| 1702 | return NULL; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1703 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1704 | one = PyLong_FromLong(1); |
| 1705 | if (one == NULL) { |
| 1706 | Py_DECREF(it); |
| 1707 | return NULL; |
| 1708 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1709 | |
| 1710 | if (PyDict_CheckExact(mapping)) { |
| 1711 | while (1) { |
| 1712 | key = PyIter_Next(it); |
Victor Stinner | a154b5c | 2011-04-20 23:23:52 +0200 | [diff] [blame] | 1713 | if (key == NULL) |
| 1714 | break; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1715 | oldval = PyDict_GetItem(mapping, key); |
| 1716 | if (oldval == NULL) { |
| 1717 | if (PyDict_SetItem(mapping, key, one) == -1) |
| 1718 | break; |
| 1719 | } else { |
| 1720 | newval = PyNumber_Add(oldval, one); |
| 1721 | if (newval == NULL) |
| 1722 | break; |
| 1723 | if (PyDict_SetItem(mapping, key, newval) == -1) |
| 1724 | break; |
| 1725 | Py_CLEAR(newval); |
| 1726 | } |
| 1727 | Py_DECREF(key); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1728 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1729 | } else { |
| 1730 | while (1) { |
| 1731 | key = PyIter_Next(it); |
Victor Stinner | a154b5c | 2011-04-20 23:23:52 +0200 | [diff] [blame] | 1732 | if (key == NULL) |
| 1733 | break; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1734 | oldval = PyObject_GetItem(mapping, key); |
| 1735 | if (oldval == NULL) { |
| 1736 | if (!PyErr_Occurred() || !PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1737 | break; |
| 1738 | PyErr_Clear(); |
| 1739 | Py_INCREF(one); |
| 1740 | newval = one; |
| 1741 | } else { |
| 1742 | newval = PyNumber_Add(oldval, one); |
| 1743 | Py_DECREF(oldval); |
| 1744 | if (newval == NULL) |
| 1745 | break; |
| 1746 | } |
| 1747 | if (PyObject_SetItem(mapping, key, newval) == -1) |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1748 | break; |
| 1749 | Py_CLEAR(newval); |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1750 | Py_DECREF(key); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1751 | } |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1752 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1753 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1754 | Py_DECREF(it); |
| 1755 | Py_XDECREF(key); |
| 1756 | Py_XDECREF(newval); |
| 1757 | Py_DECREF(one); |
| 1758 | if (PyErr_Occurred()) |
| 1759 | return NULL; |
| 1760 | Py_RETURN_NONE; |
| 1761 | } |
| 1762 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1763 | /* module level code ********************************************************/ |
| 1764 | |
| 1765 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1766 | "High performance data structures.\n\ |
| 1767 | - deque: ordered collection accessible from endpoints only\n\ |
| 1768 | - defaultdict: dict subclass with a default value factory\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1769 | "); |
| 1770 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1771 | static struct PyMethodDef module_functions[] = { |
| 1772 | {"_count_elements", _count_elements, METH_VARARGS, _count_elements_doc}, |
| 1773 | {NULL, NULL} /* sentinel */ |
| 1774 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1775 | |
| 1776 | static struct PyModuleDef _collectionsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | PyModuleDef_HEAD_INIT, |
| 1778 | "_collections", |
| 1779 | module_doc, |
| 1780 | -1, |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1781 | module_functions, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | NULL, |
| 1783 | NULL, |
| 1784 | NULL, |
| 1785 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1786 | }; |
| 1787 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1788 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1789 | PyInit__collections(void) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1790 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | PyObject *m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | m = PyModule_Create(&_collectionsmodule); |
| 1794 | if (m == NULL) |
| 1795 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | if (PyType_Ready(&deque_type) < 0) |
| 1798 | return NULL; |
| 1799 | Py_INCREF(&deque_type); |
| 1800 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1802 | defdict_type.tp_base = &PyDict_Type; |
| 1803 | if (PyType_Ready(&defdict_type) < 0) |
| 1804 | return NULL; |
| 1805 | Py_INCREF(&defdict_type); |
| 1806 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | if (PyType_Ready(&dequeiter_type) < 0) |
| 1809 | return NULL; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1810 | Py_INCREF(&dequeiter_type); |
| 1811 | PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | if (PyType_Ready(&dequereviter_type) < 0) |
| 1814 | return NULL; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1815 | Py_INCREF(&dequereviter_type); |
| 1816 | PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | return m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1819 | } |