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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | Py_ssize_t i, len=deque->len, halflen=(len+1)>>1; |
| 417 | PyObject *item, *rv; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | if (len == 0) |
| 420 | return 0; |
| 421 | if (n > halflen || n < -halflen) { |
| 422 | n %= len; |
| 423 | if (n > halflen) |
| 424 | n -= len; |
| 425 | else if (n < -halflen) |
| 426 | n += len; |
| 427 | } |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | for (i=0 ; i<n ; i++) { |
| 430 | item = deque_pop(deque, NULL); |
| 431 | assert (item != NULL); |
| 432 | rv = deque_appendleft(deque, item); |
| 433 | Py_DECREF(item); |
| 434 | if (rv == NULL) |
| 435 | return -1; |
| 436 | Py_DECREF(rv); |
| 437 | } |
| 438 | for (i=0 ; i>n ; i--) { |
| 439 | item = deque_popleft(deque, NULL); |
| 440 | assert (item != NULL); |
| 441 | rv = deque_append(deque, item); |
| 442 | Py_DECREF(item); |
| 443 | if (rv == NULL) |
| 444 | return -1; |
| 445 | Py_DECREF(rv); |
| 446 | } |
| 447 | return 0; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static PyObject * |
| 451 | deque_rotate(dequeobject *deque, PyObject *args) |
| 452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | Py_ssize_t n=1; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | if (!PyArg_ParseTuple(args, "|n:rotate", &n)) |
| 456 | return NULL; |
| 457 | if (_deque_rotate(deque, n) == 0) |
| 458 | Py_RETURN_NONE; |
| 459 | return NULL; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 462 | PyDoc_STRVAR(rotate_doc, |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 463 | "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] | 464 | |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 465 | static PyObject * |
| 466 | deque_reverse(dequeobject *deque, PyObject *unused) |
| 467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | block *leftblock = deque->leftblock; |
| 469 | block *rightblock = deque->rightblock; |
| 470 | Py_ssize_t leftindex = deque->leftindex; |
| 471 | Py_ssize_t rightindex = deque->rightindex; |
| 472 | Py_ssize_t n = (deque->len)/2; |
| 473 | Py_ssize_t i; |
| 474 | PyObject *tmp; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | for (i=0 ; i<n ; i++) { |
| 477 | /* Validate that pointers haven't met in the middle */ |
| 478 | assert(leftblock != rightblock || leftindex < rightindex); |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | /* Swap */ |
| 481 | tmp = leftblock->data[leftindex]; |
| 482 | leftblock->data[leftindex] = rightblock->data[rightindex]; |
| 483 | rightblock->data[rightindex] = tmp; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | /* Advance left block/index pair */ |
| 486 | leftindex++; |
| 487 | if (leftindex == BLOCKLEN) { |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 488 | if (leftblock->rightlink == NULL) |
| 489 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | leftblock = leftblock->rightlink; |
| 491 | leftindex = 0; |
| 492 | } |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | /* Step backwards with the right block/index pair */ |
| 495 | rightindex--; |
| 496 | if (rightindex == -1) { |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 497 | if (rightblock->leftlink == NULL) |
| 498 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | rightblock = rightblock->leftlink; |
| 500 | rightindex = BLOCKLEN - 1; |
| 501 | } |
| 502 | } |
| 503 | Py_RETURN_NONE; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | PyDoc_STRVAR(reverse_doc, |
| 507 | "D.reverse() -- reverse *IN PLACE*"); |
| 508 | |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 509 | static PyObject * |
| 510 | deque_count(dequeobject *deque, PyObject *v) |
| 511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | block *leftblock = deque->leftblock; |
| 513 | Py_ssize_t leftindex = deque->leftindex; |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 514 | Py_ssize_t n = deque->len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | Py_ssize_t i; |
| 516 | Py_ssize_t count = 0; |
| 517 | PyObject *item; |
| 518 | long start_state = deque->state; |
| 519 | int cmp; |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | for (i=0 ; i<n ; i++) { |
| 522 | item = leftblock->data[leftindex]; |
| 523 | cmp = PyObject_RichCompareBool(item, v, Py_EQ); |
| 524 | if (cmp > 0) |
| 525 | count++; |
| 526 | else if (cmp < 0) |
| 527 | return NULL; |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | if (start_state != deque->state) { |
| 530 | PyErr_SetString(PyExc_RuntimeError, |
| 531 | "deque mutated during iteration"); |
| 532 | return NULL; |
| 533 | } |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | /* Advance left block/index pair */ |
| 536 | leftindex++; |
| 537 | if (leftindex == BLOCKLEN) { |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 538 | if (leftblock->rightlink == NULL) /* can occur when i==n-1 */ |
| 539 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | leftblock = leftblock->rightlink; |
| 541 | leftindex = 0; |
| 542 | } |
| 543 | } |
| 544 | return PyLong_FromSsize_t(count); |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | PyDoc_STRVAR(count_doc, |
| 548 | "D.count(value) -> integer -- return number of occurrences of value"); |
| 549 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 550 | static Py_ssize_t |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 551 | deque_len(dequeobject *deque) |
| 552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | return deque->len; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 556 | static PyObject * |
| 557 | deque_remove(dequeobject *deque, PyObject *value) |
| 558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | Py_ssize_t i, n=deque->len; |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | for (i=0 ; i<n ; i++) { |
| 562 | PyObject *item = deque->leftblock->data[deque->leftindex]; |
| 563 | int cmp = PyObject_RichCompareBool(item, value, Py_EQ); |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | if (deque->len != n) { |
| 566 | PyErr_SetString(PyExc_IndexError, |
| 567 | "deque mutated during remove()."); |
| 568 | return NULL; |
| 569 | } |
| 570 | if (cmp > 0) { |
| 571 | PyObject *tgt = deque_popleft(deque, NULL); |
| 572 | assert (tgt != NULL); |
| 573 | Py_DECREF(tgt); |
| 574 | if (_deque_rotate(deque, i) == -1) |
| 575 | return NULL; |
| 576 | Py_RETURN_NONE; |
| 577 | } |
| 578 | else if (cmp < 0) { |
| 579 | _deque_rotate(deque, i); |
| 580 | return NULL; |
| 581 | } |
| 582 | _deque_rotate(deque, -1); |
| 583 | } |
| 584 | PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"); |
| 585 | return NULL; |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | PyDoc_STRVAR(remove_doc, |
| 589 | "D.remove(value) -- remove first occurrence of value."); |
| 590 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 591 | static int |
| 592 | deque_clear(dequeobject *deque) |
| 593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | PyObject *item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | while (deque->len) { |
| 597 | item = deque_pop(deque, NULL); |
| 598 | assert (item != NULL); |
| 599 | Py_DECREF(item); |
| 600 | } |
| 601 | assert(deque->leftblock == deque->rightblock && |
| 602 | deque->leftindex - 1 == deque->rightindex && |
| 603 | deque->len == 0); |
| 604 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static PyObject * |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 608 | deque_item(dequeobject *deque, Py_ssize_t i) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | block *b; |
| 611 | PyObject *item; |
| 612 | Py_ssize_t n, index=i; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | if (i < 0 || i >= deque->len) { |
| 615 | PyErr_SetString(PyExc_IndexError, |
| 616 | "deque index out of range"); |
| 617 | return NULL; |
| 618 | } |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | if (i == 0) { |
| 621 | i = deque->leftindex; |
| 622 | b = deque->leftblock; |
| 623 | } else if (i == deque->len - 1) { |
| 624 | i = deque->rightindex; |
| 625 | b = deque->rightblock; |
| 626 | } else { |
| 627 | i += deque->leftindex; |
| 628 | n = i / BLOCKLEN; |
| 629 | i %= BLOCKLEN; |
| 630 | if (index < (deque->len >> 1)) { |
| 631 | b = deque->leftblock; |
| 632 | while (n--) |
| 633 | b = b->rightlink; |
| 634 | } else { |
| 635 | n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n; |
| 636 | b = deque->rightblock; |
| 637 | while (n--) |
| 638 | b = b->leftlink; |
| 639 | } |
| 640 | } |
| 641 | item = b->data[i]; |
| 642 | Py_INCREF(item); |
| 643 | return item; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Raymond Hettinger | 616f4f6 | 2004-06-26 04:42:06 +0000 | [diff] [blame] | 646 | /* delitem() implemented in terms of rotate for simplicity and reasonable |
| 647 | performance near the end points. If for some reason this method becomes |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 648 | 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] | 649 | (similar to code in list slice assignment) and achieve a two or threefold |
| 650 | performance boost. |
| 651 | */ |
| 652 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 653 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 654 | deque_del_item(dequeobject *deque, Py_ssize_t i) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | PyObject *item; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | assert (i >= 0 && i < deque->len); |
| 659 | if (_deque_rotate(deque, -i) == -1) |
| 660 | return -1; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | item = deque_popleft(deque, NULL); |
| 663 | assert (item != NULL); |
| 664 | Py_DECREF(item); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | return _deque_rotate(deque, i); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 670 | deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 671 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | PyObject *old_value; |
| 673 | block *b; |
| 674 | 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] | 675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | if (i < 0 || i >= len) { |
| 677 | PyErr_SetString(PyExc_IndexError, |
| 678 | "deque index out of range"); |
| 679 | return -1; |
| 680 | } |
| 681 | if (v == NULL) |
| 682 | return deque_del_item(deque, i); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | i += deque->leftindex; |
| 685 | n = i / BLOCKLEN; |
| 686 | i %= BLOCKLEN; |
| 687 | if (index <= halflen) { |
| 688 | b = deque->leftblock; |
| 689 | while (n--) |
| 690 | b = b->rightlink; |
| 691 | } else { |
| 692 | n = (deque->leftindex + len - 1) / BLOCKLEN - n; |
| 693 | b = deque->rightblock; |
| 694 | while (n--) |
| 695 | b = b->leftlink; |
| 696 | } |
| 697 | Py_INCREF(v); |
| 698 | old_value = b->data[i]; |
| 699 | b->data[i] = v; |
| 700 | Py_DECREF(old_value); |
| 701 | return 0; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static PyObject * |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 705 | deque_clearmethod(dequeobject *deque) |
| 706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | int rv; |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | rv = deque_clear(deque); |
| 710 | assert (rv != -1); |
| 711 | Py_RETURN_NONE; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | PyDoc_STRVAR(clear_doc, "Remove all elements from the deque."); |
| 715 | |
| 716 | static void |
| 717 | deque_dealloc(dequeobject *deque) |
| 718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | PyObject_GC_UnTrack(deque); |
| 720 | if (deque->weakreflist != NULL) |
| 721 | PyObject_ClearWeakRefs((PyObject *) deque); |
| 722 | if (deque->leftblock != NULL) { |
| 723 | deque_clear(deque); |
| 724 | assert(deque->leftblock != NULL); |
| 725 | freeblock(deque->leftblock); |
| 726 | } |
| 727 | deque->leftblock = NULL; |
| 728 | deque->rightblock = NULL; |
| 729 | Py_TYPE(deque)->tp_free(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static int |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 733 | deque_traverse(dequeobject *deque, visitproc visit, void *arg) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | block *b; |
| 736 | PyObject *item; |
| 737 | Py_ssize_t index; |
| 738 | Py_ssize_t indexlo = deque->leftindex; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | for (b = deque->leftblock; b != NULL; b = b->rightlink) { |
| 741 | const Py_ssize_t indexhi = b == deque->rightblock ? |
| 742 | deque->rightindex : |
| 743 | BLOCKLEN - 1; |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | for (index = indexlo; index <= indexhi; ++index) { |
| 746 | item = b->data[index]; |
| 747 | Py_VISIT(item); |
| 748 | } |
| 749 | indexlo = 0; |
| 750 | } |
| 751 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 754 | static PyObject * |
| 755 | deque_copy(PyObject *deque) |
| 756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | if (((dequeobject *)deque)->maxlen == -1) |
| 758 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL); |
| 759 | else |
| 760 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi", |
| 761 | deque, ((dequeobject *)deque)->maxlen, NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque."); |
| 765 | |
| 766 | static PyObject * |
| 767 | deque_reduce(dequeobject *deque) |
| 768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | PyObject *dict, *result, *aslist; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | dict = PyObject_GetAttrString((PyObject *)deque, "__dict__"); |
| 772 | if (dict == NULL) |
| 773 | PyErr_Clear(); |
| 774 | aslist = PySequence_List((PyObject *)deque); |
| 775 | if (aslist == NULL) { |
| 776 | Py_XDECREF(dict); |
| 777 | return NULL; |
| 778 | } |
| 779 | if (dict == NULL) { |
| 780 | if (deque->maxlen == -1) |
| 781 | result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist); |
| 782 | else |
| 783 | result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen); |
| 784 | } else { |
| 785 | if (deque->maxlen == -1) |
| 786 | result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict); |
| 787 | else |
| 788 | result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict); |
| 789 | } |
| 790 | Py_XDECREF(dict); |
| 791 | Py_DECREF(aslist); |
| 792 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 796 | |
| 797 | static PyObject * |
| 798 | deque_repr(PyObject *deque) |
| 799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | PyObject *aslist, *result; |
| 801 | int i; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | i = Py_ReprEnter(deque); |
| 804 | if (i != 0) { |
| 805 | if (i < 0) |
| 806 | return NULL; |
| 807 | return PyUnicode_FromString("[...]"); |
| 808 | } |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | aslist = PySequence_List(deque); |
| 811 | if (aslist == NULL) { |
| 812 | Py_ReprLeave(deque); |
| 813 | return NULL; |
| 814 | } |
| 815 | if (((dequeobject *)deque)->maxlen != -1) |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)", |
| 818 | aslist, ((dequeobject *)deque)->maxlen); |
| 819 | else |
| 820 | result = PyUnicode_FromFormat("deque(%R)", aslist); |
| 821 | Py_DECREF(aslist); |
| 822 | Py_ReprLeave(deque); |
| 823 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 826 | static PyObject * |
| 827 | deque_richcompare(PyObject *v, PyObject *w, int op) |
| 828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | PyObject *it1=NULL, *it2=NULL, *x, *y; |
| 830 | Py_ssize_t vs, ws; |
| 831 | int b, cmp=-1; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | if (!PyObject_TypeCheck(v, &deque_type) || |
| 834 | !PyObject_TypeCheck(w, &deque_type)) { |
| 835 | Py_INCREF(Py_NotImplemented); |
| 836 | return Py_NotImplemented; |
| 837 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | /* Shortcuts */ |
| 840 | vs = ((dequeobject *)v)->len; |
| 841 | ws = ((dequeobject *)w)->len; |
| 842 | if (op == Py_EQ) { |
| 843 | if (v == w) |
| 844 | Py_RETURN_TRUE; |
| 845 | if (vs != ws) |
| 846 | Py_RETURN_FALSE; |
| 847 | } |
| 848 | if (op == Py_NE) { |
| 849 | if (v == w) |
| 850 | Py_RETURN_FALSE; |
| 851 | if (vs != ws) |
| 852 | Py_RETURN_TRUE; |
| 853 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | /* Search for the first index where items are different */ |
| 856 | it1 = PyObject_GetIter(v); |
| 857 | if (it1 == NULL) |
| 858 | goto done; |
| 859 | it2 = PyObject_GetIter(w); |
| 860 | if (it2 == NULL) |
| 861 | goto done; |
| 862 | for (;;) { |
| 863 | x = PyIter_Next(it1); |
| 864 | if (x == NULL && PyErr_Occurred()) |
| 865 | goto done; |
| 866 | y = PyIter_Next(it2); |
| 867 | if (x == NULL || y == NULL) |
| 868 | break; |
| 869 | b = PyObject_RichCompareBool(x, y, Py_EQ); |
| 870 | if (b == 0) { |
| 871 | cmp = PyObject_RichCompareBool(x, y, op); |
| 872 | Py_DECREF(x); |
| 873 | Py_DECREF(y); |
| 874 | goto done; |
| 875 | } |
| 876 | Py_DECREF(x); |
| 877 | Py_DECREF(y); |
| 878 | if (b == -1) |
| 879 | goto done; |
| 880 | } |
| 881 | /* We reached the end of one deque or both */ |
| 882 | Py_XDECREF(x); |
| 883 | Py_XDECREF(y); |
| 884 | if (PyErr_Occurred()) |
| 885 | goto done; |
| 886 | switch (op) { |
| 887 | case Py_LT: cmp = y != NULL; break; /* if w was longer */ |
| 888 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */ |
| 889 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ |
| 890 | case Py_NE: cmp = x != y; break; /* if one deque continues */ |
| 891 | case Py_GT: cmp = x != NULL; break; /* if v was longer */ |
| 892 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */ |
| 893 | } |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 894 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 895 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | Py_XDECREF(it1); |
| 897 | Py_XDECREF(it2); |
| 898 | if (cmp == 1) |
| 899 | Py_RETURN_TRUE; |
| 900 | if (cmp == 0) |
| 901 | Py_RETURN_FALSE; |
| 902 | return NULL; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 905 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 906 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | PyObject *iterable = NULL; |
| 909 | PyObject *maxlenobj = NULL; |
| 910 | Py_ssize_t maxlen = -1; |
| 911 | char *kwlist[] = {"iterable", "maxlen", 0}; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) |
| 914 | return -1; |
| 915 | if (maxlenobj != NULL && maxlenobj != Py_None) { |
| 916 | maxlen = PyLong_AsSsize_t(maxlenobj); |
| 917 | if (maxlen == -1 && PyErr_Occurred()) |
| 918 | return -1; |
| 919 | if (maxlen < 0) { |
| 920 | PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); |
| 921 | return -1; |
| 922 | } |
| 923 | } |
| 924 | deque->maxlen = maxlen; |
| 925 | deque_clear(deque); |
| 926 | if (iterable != NULL) { |
| 927 | PyObject *rv = deque_extend(deque, iterable); |
| 928 | if (rv == NULL) |
| 929 | return -1; |
| 930 | Py_DECREF(rv); |
| 931 | } |
| 932 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 935 | static PyObject * |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 936 | deque_sizeof(dequeobject *deque, void *unused) |
| 937 | { |
| 938 | Py_ssize_t res; |
| 939 | Py_ssize_t blocks; |
| 940 | |
| 941 | res = sizeof(dequeobject); |
| 942 | blocks = (deque->leftindex + deque->len + BLOCKLEN - 1) / BLOCKLEN; |
| 943 | assert(deque->leftindex + deque->len - 1 == |
| 944 | (blocks - 1) * BLOCKLEN + deque->rightindex); |
| 945 | res += blocks * sizeof(block); |
| 946 | return PyLong_FromSsize_t(res); |
| 947 | } |
| 948 | |
| 949 | PyDoc_STRVAR(sizeof_doc, |
| 950 | "D.__sizeof__() -- size of D in memory, in bytes"); |
| 951 | |
| 952 | static PyObject * |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 953 | deque_get_maxlen(dequeobject *deque) |
| 954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | if (deque->maxlen == -1) |
| 956 | Py_RETURN_NONE; |
| 957 | return PyLong_FromSsize_t(deque->maxlen); |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | static PyGetSetDef deque_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | {"maxlen", (getter)deque_get_maxlen, (setter)NULL, |
| 962 | "maximum size of a deque or None if unbounded"}, |
| 963 | {0} |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 964 | }; |
| 965 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 966 | static PySequenceMethods deque_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | (lenfunc)deque_len, /* sq_length */ |
| 968 | 0, /* sq_concat */ |
| 969 | 0, /* sq_repeat */ |
| 970 | (ssizeargfunc)deque_item, /* sq_item */ |
| 971 | 0, /* sq_slice */ |
| 972 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ |
| 973 | 0, /* sq_ass_slice */ |
| 974 | 0, /* sq_contains */ |
| 975 | (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */ |
| 976 | 0, /* sq_inplace_repeat */ |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 977 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 978 | }; |
| 979 | |
| 980 | /* deque object ********************************************************/ |
| 981 | |
| 982 | static PyObject *deque_iter(dequeobject *deque); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 983 | static PyObject *deque_reviter(dequeobject *deque); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 984 | PyDoc_STRVAR(reversed_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | "D.__reversed__() -- return a reverse iterator over the deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 986 | |
| 987 | static PyMethodDef deque_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | {"append", (PyCFunction)deque_append, |
| 989 | METH_O, append_doc}, |
| 990 | {"appendleft", (PyCFunction)deque_appendleft, |
| 991 | METH_O, appendleft_doc}, |
| 992 | {"clear", (PyCFunction)deque_clearmethod, |
| 993 | METH_NOARGS, clear_doc}, |
| 994 | {"__copy__", (PyCFunction)deque_copy, |
| 995 | METH_NOARGS, copy_doc}, |
| 996 | {"count", (PyCFunction)deque_count, |
| 997 | METH_O, count_doc}, |
| 998 | {"extend", (PyCFunction)deque_extend, |
| 999 | METH_O, extend_doc}, |
| 1000 | {"extendleft", (PyCFunction)deque_extendleft, |
| 1001 | METH_O, extendleft_doc}, |
| 1002 | {"pop", (PyCFunction)deque_pop, |
| 1003 | METH_NOARGS, pop_doc}, |
| 1004 | {"popleft", (PyCFunction)deque_popleft, |
| 1005 | METH_NOARGS, popleft_doc}, |
| 1006 | {"__reduce__", (PyCFunction)deque_reduce, |
| 1007 | METH_NOARGS, reduce_doc}, |
| 1008 | {"remove", (PyCFunction)deque_remove, |
| 1009 | METH_O, remove_doc}, |
| 1010 | {"__reversed__", (PyCFunction)deque_reviter, |
| 1011 | METH_NOARGS, reversed_doc}, |
| 1012 | {"reverse", (PyCFunction)deque_reverse, |
| 1013 | METH_NOARGS, reverse_doc}, |
| 1014 | {"rotate", (PyCFunction)deque_rotate, |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 1015 | METH_VARARGS, rotate_doc}, |
| 1016 | {"__sizeof__", (PyCFunction)deque_sizeof, |
| 1017 | METH_NOARGS, sizeof_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1019 | }; |
| 1020 | |
| 1021 | PyDoc_STRVAR(deque_doc, |
Andrew Svetlov | 6a5c7c3 | 2012-10-31 11:50:40 +0200 | [diff] [blame] | 1022 | "deque([iterable[, maxlen]]) --> deque object\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1023 | \n\ |
Raymond Hettinger | 4974705 | 2011-03-29 17:36:31 -0700 | [diff] [blame] | 1024 | Build an ordered collection with optimized access from its endpoints."); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1025 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 1026 | static PyTypeObject deque_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1028 | "collections.deque", /* tp_name */ |
| 1029 | sizeof(dequeobject), /* tp_basicsize */ |
| 1030 | 0, /* tp_itemsize */ |
| 1031 | /* methods */ |
| 1032 | (destructor)deque_dealloc, /* tp_dealloc */ |
| 1033 | 0, /* tp_print */ |
| 1034 | 0, /* tp_getattr */ |
| 1035 | 0, /* tp_setattr */ |
| 1036 | 0, /* tp_reserved */ |
| 1037 | deque_repr, /* tp_repr */ |
| 1038 | 0, /* tp_as_number */ |
| 1039 | &deque_as_sequence, /* tp_as_sequence */ |
| 1040 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1041 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | 0, /* tp_call */ |
| 1043 | 0, /* tp_str */ |
| 1044 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1045 | 0, /* tp_setattro */ |
| 1046 | 0, /* tp_as_buffer */ |
| 1047 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1048 | /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | deque_doc, /* tp_doc */ |
| 1050 | (traverseproc)deque_traverse, /* tp_traverse */ |
| 1051 | (inquiry)deque_clear, /* tp_clear */ |
| 1052 | (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1053 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | (getiterfunc)deque_iter, /* tp_iter */ |
| 1055 | 0, /* tp_iternext */ |
| 1056 | deque_methods, /* tp_methods */ |
| 1057 | 0, /* tp_members */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1058 | deque_getset, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | 0, /* tp_base */ |
| 1060 | 0, /* tp_dict */ |
| 1061 | 0, /* tp_descr_get */ |
| 1062 | 0, /* tp_descr_set */ |
| 1063 | 0, /* tp_dictoffset */ |
| 1064 | (initproc)deque_init, /* tp_init */ |
| 1065 | PyType_GenericAlloc, /* tp_alloc */ |
| 1066 | deque_new, /* tp_new */ |
| 1067 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | /*********************** Deque Iterator **************************/ |
| 1071 | |
| 1072 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | PyObject_HEAD |
| 1074 | Py_ssize_t index; |
| 1075 | block *b; |
| 1076 | dequeobject *deque; |
| 1077 | long state; /* state when the iterator is created */ |
| 1078 | Py_ssize_t counter; /* number of items remaining for iteration */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1079 | } dequeiterobject; |
| 1080 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1081 | static PyTypeObject dequeiter_type; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1082 | |
| 1083 | static PyObject * |
| 1084 | deque_iter(dequeobject *deque) |
| 1085 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | dequeiterobject *it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | it = PyObject_GC_New(dequeiterobject, &dequeiter_type); |
| 1089 | if (it == NULL) |
| 1090 | return NULL; |
| 1091 | it->b = deque->leftblock; |
| 1092 | it->index = deque->leftindex; |
| 1093 | Py_INCREF(deque); |
| 1094 | it->deque = deque; |
| 1095 | it->state = deque->state; |
| 1096 | it->counter = deque->len; |
| 1097 | PyObject_GC_Track(it); |
| 1098 | return (PyObject *)it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 1101 | static int |
| 1102 | dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg) |
| 1103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | Py_VISIT(dio->deque); |
| 1105 | return 0; |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1108 | static void |
| 1109 | dequeiter_dealloc(dequeiterobject *dio) |
| 1110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | Py_XDECREF(dio->deque); |
| 1112 | PyObject_GC_Del(dio); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static PyObject * |
| 1116 | dequeiter_next(dequeiterobject *it) |
| 1117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | if (it->deque->state != it->state) { |
| 1121 | it->counter = 0; |
| 1122 | PyErr_SetString(PyExc_RuntimeError, |
| 1123 | "deque mutated during iteration"); |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | if (it->counter == 0) |
| 1127 | return NULL; |
| 1128 | assert (!(it->b == it->deque->rightblock && |
| 1129 | it->index > it->deque->rightindex)); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | item = it->b->data[it->index]; |
| 1132 | it->index++; |
| 1133 | it->counter--; |
| 1134 | if (it->index == BLOCKLEN && it->counter > 0) { |
| 1135 | assert (it->b->rightlink != NULL); |
| 1136 | it->b = it->b->rightlink; |
| 1137 | it->index = 0; |
| 1138 | } |
| 1139 | Py_INCREF(item); |
| 1140 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1143 | static PyObject * |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1144 | dequeiter_len(dequeiterobject *it) |
| 1145 | { |
Antoine Pitrou | 554f334 | 2010-08-17 18:30:06 +0000 | [diff] [blame] | 1146 | return PyLong_FromSsize_t(it->counter); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 1149 | 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] | 1150 | |
| 1151 | static PyMethodDef dequeiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc}, |
| 1153 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1154 | }; |
| 1155 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1156 | static PyTypeObject dequeiter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1158 | "deque_iterator", /* tp_name */ |
| 1159 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1160 | 0, /* tp_itemsize */ |
| 1161 | /* methods */ |
| 1162 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1163 | 0, /* tp_print */ |
| 1164 | 0, /* tp_getattr */ |
| 1165 | 0, /* tp_setattr */ |
| 1166 | 0, /* tp_reserved */ |
| 1167 | 0, /* tp_repr */ |
| 1168 | 0, /* tp_as_number */ |
| 1169 | 0, /* tp_as_sequence */ |
| 1170 | 0, /* tp_as_mapping */ |
| 1171 | 0, /* tp_hash */ |
| 1172 | 0, /* tp_call */ |
| 1173 | 0, /* tp_str */ |
| 1174 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1175 | 0, /* tp_setattro */ |
| 1176 | 0, /* tp_as_buffer */ |
| 1177 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1178 | 0, /* tp_doc */ |
| 1179 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1180 | 0, /* tp_clear */ |
| 1181 | 0, /* tp_richcompare */ |
| 1182 | 0, /* tp_weaklistoffset */ |
| 1183 | PyObject_SelfIter, /* tp_iter */ |
| 1184 | (iternextfunc)dequeiter_next, /* tp_iternext */ |
| 1185 | dequeiter_methods, /* tp_methods */ |
| 1186 | 0, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1187 | }; |
| 1188 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1189 | /*********************** Deque Reverse Iterator **************************/ |
| 1190 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1191 | static PyTypeObject dequereviter_type; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1192 | |
| 1193 | static PyObject * |
| 1194 | deque_reviter(dequeobject *deque) |
| 1195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | dequeiterobject *it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | it = PyObject_GC_New(dequeiterobject, &dequereviter_type); |
| 1199 | if (it == NULL) |
| 1200 | return NULL; |
| 1201 | it->b = deque->rightblock; |
| 1202 | it->index = deque->rightindex; |
| 1203 | Py_INCREF(deque); |
| 1204 | it->deque = deque; |
| 1205 | it->state = deque->state; |
| 1206 | it->counter = deque->len; |
| 1207 | PyObject_GC_Track(it); |
| 1208 | return (PyObject *)it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | static PyObject * |
| 1212 | dequereviter_next(dequeiterobject *it) |
| 1213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | PyObject *item; |
| 1215 | if (it->counter == 0) |
| 1216 | return NULL; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | if (it->deque->state != it->state) { |
| 1219 | it->counter = 0; |
| 1220 | PyErr_SetString(PyExc_RuntimeError, |
| 1221 | "deque mutated during iteration"); |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | assert (!(it->b == it->deque->leftblock && |
| 1225 | it->index < it->deque->leftindex)); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | item = it->b->data[it->index]; |
| 1228 | it->index--; |
| 1229 | it->counter--; |
| 1230 | if (it->index == -1 && it->counter > 0) { |
| 1231 | assert (it->b->leftlink != NULL); |
| 1232 | it->b = it->b->leftlink; |
| 1233 | it->index = BLOCKLEN - 1; |
| 1234 | } |
| 1235 | Py_INCREF(item); |
| 1236 | return item; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1239 | static PyTypeObject dequereviter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1241 | "deque_reverse_iterator", /* tp_name */ |
| 1242 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1243 | 0, /* tp_itemsize */ |
| 1244 | /* methods */ |
| 1245 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1246 | 0, /* tp_print */ |
| 1247 | 0, /* tp_getattr */ |
| 1248 | 0, /* tp_setattr */ |
| 1249 | 0, /* tp_reserved */ |
| 1250 | 0, /* tp_repr */ |
| 1251 | 0, /* tp_as_number */ |
| 1252 | 0, /* tp_as_sequence */ |
| 1253 | 0, /* tp_as_mapping */ |
| 1254 | 0, /* tp_hash */ |
| 1255 | 0, /* tp_call */ |
| 1256 | 0, /* tp_str */ |
| 1257 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1258 | 0, /* tp_setattro */ |
| 1259 | 0, /* tp_as_buffer */ |
| 1260 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1261 | 0, /* tp_doc */ |
| 1262 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1263 | 0, /* tp_clear */ |
| 1264 | 0, /* tp_richcompare */ |
| 1265 | 0, /* tp_weaklistoffset */ |
| 1266 | PyObject_SelfIter, /* tp_iter */ |
| 1267 | (iternextfunc)dequereviter_next, /* tp_iternext */ |
| 1268 | dequeiter_methods, /* tp_methods */ |
| 1269 | 0, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1270 | }; |
| 1271 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1272 | /* defaultdict type *********************************************************/ |
| 1273 | |
| 1274 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | PyDictObject dict; |
| 1276 | PyObject *default_factory; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1277 | } defdictobject; |
| 1278 | |
| 1279 | static PyTypeObject defdict_type; /* Forward */ |
| 1280 | |
| 1281 | PyDoc_STRVAR(defdict_missing_doc, |
| 1282 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1283 | if self.default_factory is None: raise KeyError((key,))\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1284 | self[key] = value = self.default_factory()\n\ |
| 1285 | return value\n\ |
| 1286 | "); |
| 1287 | |
| 1288 | static PyObject * |
| 1289 | defdict_missing(defdictobject *dd, PyObject *key) |
| 1290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | PyObject *factory = dd->default_factory; |
| 1292 | PyObject *value; |
| 1293 | if (factory == NULL || factory == Py_None) { |
| 1294 | /* XXX Call dict.__missing__(key) */ |
| 1295 | PyObject *tup; |
| 1296 | tup = PyTuple_Pack(1, key); |
| 1297 | if (!tup) return NULL; |
| 1298 | PyErr_SetObject(PyExc_KeyError, tup); |
| 1299 | Py_DECREF(tup); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | value = PyEval_CallObject(factory, NULL); |
| 1303 | if (value == NULL) |
| 1304 | return value; |
| 1305 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) { |
| 1306 | Py_DECREF(value); |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | return value; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D."); |
| 1313 | |
| 1314 | static PyObject * |
| 1315 | defdict_copy(defdictobject *dd) |
| 1316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | /* This calls the object's class. That only works for subclasses |
| 1318 | whose class constructor has the same signature. Subclasses that |
| 1319 | define a different constructor signature must override copy(). |
| 1320 | */ |
Raymond Hettinger | 54628fa | 2009-08-04 19:16:39 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | if (dd->default_factory == NULL) |
| 1323 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL); |
| 1324 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), |
| 1325 | dd->default_factory, dd, NULL); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | static PyObject * |
| 1329 | defdict_reduce(defdictobject *dd) |
| 1330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | /* __reduce__ must return a 5-tuple as follows: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | - factory function |
| 1334 | - tuple of args for the factory function |
| 1335 | - additional state (here None) |
| 1336 | - sequence iterator (here None) |
| 1337 | - dictionary iterator (yielding successive (key, value) pairs |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | This API is used by pickle.py and copy.py. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | For this to be useful with pickle.py, the default_factory |
| 1342 | must be picklable; e.g., None, a built-in, or a global |
| 1343 | function in a module or package. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | Both shallow and deep copying are supported, but for deep |
| 1346 | copying, the default_factory must be deep-copyable; e.g. None, |
| 1347 | or a built-in (functions are not copyable at this time). |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | This only works for subclasses as long as their constructor |
| 1350 | signature is compatible; the first argument must be the |
| 1351 | optional default_factory, defaulting to None. |
| 1352 | */ |
| 1353 | PyObject *args; |
| 1354 | PyObject *items; |
| 1355 | PyObject *iter; |
| 1356 | PyObject *result; |
| 1357 | if (dd->default_factory == NULL || dd->default_factory == Py_None) |
| 1358 | args = PyTuple_New(0); |
| 1359 | else |
| 1360 | args = PyTuple_Pack(1, dd->default_factory); |
| 1361 | if (args == NULL) |
| 1362 | return NULL; |
| 1363 | items = PyObject_CallMethod((PyObject *)dd, "items", "()"); |
| 1364 | if (items == NULL) { |
| 1365 | Py_DECREF(args); |
| 1366 | return NULL; |
| 1367 | } |
| 1368 | iter = PyObject_GetIter(items); |
| 1369 | if (iter == NULL) { |
| 1370 | Py_DECREF(items); |
| 1371 | Py_DECREF(args); |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | result = PyTuple_Pack(5, Py_TYPE(dd), args, |
| 1375 | Py_None, Py_None, iter); |
| 1376 | Py_DECREF(iter); |
| 1377 | Py_DECREF(items); |
| 1378 | Py_DECREF(args); |
| 1379 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | static PyMethodDef defdict_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | {"__missing__", (PyCFunction)defdict_missing, METH_O, |
| 1384 | defdict_missing_doc}, |
| 1385 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1386 | defdict_copy_doc}, |
| 1387 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1388 | defdict_copy_doc}, |
| 1389 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS, |
| 1390 | reduce_doc}, |
| 1391 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1392 | }; |
| 1393 | |
| 1394 | static PyMemberDef defdict_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | {"default_factory", T_OBJECT, |
| 1396 | offsetof(defdictobject, default_factory), 0, |
| 1397 | PyDoc_STR("Factory for default value called by __missing__().")}, |
| 1398 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1399 | }; |
| 1400 | |
| 1401 | static void |
| 1402 | defdict_dealloc(defdictobject *dd) |
| 1403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | Py_CLEAR(dd->default_factory); |
| 1405 | PyDict_Type.tp_dealloc((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1408 | static PyObject * |
| 1409 | defdict_repr(defdictobject *dd) |
| 1410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | PyObject *baserepr; |
| 1412 | PyObject *defrepr; |
| 1413 | PyObject *result; |
| 1414 | baserepr = PyDict_Type.tp_repr((PyObject *)dd); |
| 1415 | if (baserepr == NULL) |
| 1416 | return NULL; |
| 1417 | if (dd->default_factory == NULL) |
| 1418 | defrepr = PyUnicode_FromString("None"); |
| 1419 | else |
| 1420 | { |
| 1421 | int status = Py_ReprEnter(dd->default_factory); |
| 1422 | if (status != 0) { |
Antoine Pitrou | f5f1fe0 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1423 | if (status < 0) { |
| 1424 | Py_DECREF(baserepr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | return NULL; |
Antoine Pitrou | f5f1fe0 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1426 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | defrepr = PyUnicode_FromString("..."); |
| 1428 | } |
| 1429 | else |
| 1430 | defrepr = PyObject_Repr(dd->default_factory); |
| 1431 | Py_ReprLeave(dd->default_factory); |
| 1432 | } |
| 1433 | if (defrepr == NULL) { |
| 1434 | Py_DECREF(baserepr); |
| 1435 | return NULL; |
| 1436 | } |
| 1437 | result = PyUnicode_FromFormat("defaultdict(%U, %U)", |
| 1438 | defrepr, baserepr); |
| 1439 | Py_DECREF(defrepr); |
| 1440 | Py_DECREF(baserepr); |
| 1441 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static int |
| 1445 | defdict_traverse(PyObject *self, visitproc visit, void *arg) |
| 1446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | Py_VISIT(((defdictobject *)self)->default_factory); |
| 1448 | return PyDict_Type.tp_traverse(self, visit, arg); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | static int |
| 1452 | defdict_tp_clear(defdictobject *dd) |
| 1453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | Py_CLEAR(dd->default_factory); |
| 1455 | return PyDict_Type.tp_clear((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | static int |
| 1459 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | defdictobject *dd = (defdictobject *)self; |
| 1462 | PyObject *olddefault = dd->default_factory; |
| 1463 | PyObject *newdefault = NULL; |
| 1464 | PyObject *newargs; |
| 1465 | int result; |
| 1466 | if (args == NULL || !PyTuple_Check(args)) |
| 1467 | newargs = PyTuple_New(0); |
| 1468 | else { |
| 1469 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
| 1470 | if (n > 0) { |
| 1471 | newdefault = PyTuple_GET_ITEM(args, 0); |
| 1472 | if (!PyCallable_Check(newdefault) && newdefault != Py_None) { |
| 1473 | PyErr_SetString(PyExc_TypeError, |
| 1474 | "first argument must be callable"); |
| 1475 | return -1; |
| 1476 | } |
| 1477 | } |
| 1478 | newargs = PySequence_GetSlice(args, 1, n); |
| 1479 | } |
| 1480 | if (newargs == NULL) |
| 1481 | return -1; |
| 1482 | Py_XINCREF(newdefault); |
| 1483 | dd->default_factory = newdefault; |
| 1484 | result = PyDict_Type.tp_init(self, newargs, kwds); |
| 1485 | Py_DECREF(newargs); |
| 1486 | Py_XDECREF(olddefault); |
| 1487 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | PyDoc_STRVAR(defdict_doc, |
| 1491 | "defaultdict(default_factory) --> dict with default factory\n\ |
| 1492 | \n\ |
| 1493 | The default factory is called without arguments to produce\n\ |
| 1494 | a new value when a key is not present, in __getitem__ only.\n\ |
| 1495 | A defaultdict compares equal to a dict with the same items.\n\ |
| 1496 | "); |
| 1497 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1498 | /* See comment in xxsubtype.c */ |
| 1499 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 1500 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1501 | static PyTypeObject defdict_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 1503 | "collections.defaultdict", /* tp_name */ |
| 1504 | sizeof(defdictobject), /* tp_basicsize */ |
| 1505 | 0, /* tp_itemsize */ |
| 1506 | /* methods */ |
| 1507 | (destructor)defdict_dealloc, /* tp_dealloc */ |
| 1508 | 0, /* tp_print */ |
| 1509 | 0, /* tp_getattr */ |
| 1510 | 0, /* tp_setattr */ |
| 1511 | 0, /* tp_reserved */ |
| 1512 | (reprfunc)defdict_repr, /* tp_repr */ |
| 1513 | 0, /* tp_as_number */ |
| 1514 | 0, /* tp_as_sequence */ |
| 1515 | 0, /* tp_as_mapping */ |
| 1516 | 0, /* tp_hash */ |
| 1517 | 0, /* tp_call */ |
| 1518 | 0, /* tp_str */ |
| 1519 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1520 | 0, /* tp_setattro */ |
| 1521 | 0, /* tp_as_buffer */ |
| 1522 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1523 | /* tp_flags */ |
| 1524 | defdict_doc, /* tp_doc */ |
| 1525 | defdict_traverse, /* tp_traverse */ |
| 1526 | (inquiry)defdict_tp_clear, /* tp_clear */ |
| 1527 | 0, /* tp_richcompare */ |
| 1528 | 0, /* tp_weaklistoffset*/ |
| 1529 | 0, /* tp_iter */ |
| 1530 | 0, /* tp_iternext */ |
| 1531 | defdict_methods, /* tp_methods */ |
| 1532 | defdict_members, /* tp_members */ |
| 1533 | 0, /* tp_getset */ |
| 1534 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */ |
| 1535 | 0, /* tp_dict */ |
| 1536 | 0, /* tp_descr_get */ |
| 1537 | 0, /* tp_descr_set */ |
| 1538 | 0, /* tp_dictoffset */ |
| 1539 | defdict_init, /* tp_init */ |
| 1540 | PyType_GenericAlloc, /* tp_alloc */ |
| 1541 | 0, /* tp_new */ |
| 1542 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1543 | }; |
| 1544 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1545 | /* helper function for Counter *********************************************/ |
| 1546 | |
| 1547 | PyDoc_STRVAR(_count_elements_doc, |
| 1548 | "_count_elements(mapping, iterable) -> None\n\ |
| 1549 | \n\ |
| 1550 | Count elements in the iterable, updating the mappping"); |
| 1551 | |
| 1552 | static PyObject * |
| 1553 | _count_elements(PyObject *self, PyObject *args) |
| 1554 | { |
| 1555 | PyObject *it, *iterable, *mapping, *oldval; |
| 1556 | PyObject *newval = NULL; |
| 1557 | PyObject *key = NULL; |
| 1558 | PyObject *one = NULL; |
| 1559 | |
| 1560 | if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable)) |
| 1561 | return NULL; |
| 1562 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1563 | it = PyObject_GetIter(iterable); |
| 1564 | if (it == NULL) |
| 1565 | return NULL; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1566 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1567 | one = PyLong_FromLong(1); |
| 1568 | if (one == NULL) { |
| 1569 | Py_DECREF(it); |
| 1570 | return NULL; |
| 1571 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1572 | |
| 1573 | if (PyDict_CheckExact(mapping)) { |
| 1574 | while (1) { |
| 1575 | key = PyIter_Next(it); |
| 1576 | if (key == NULL) { |
| 1577 | if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 1578 | PyErr_Clear(); |
| 1579 | else |
| 1580 | break; |
| 1581 | } |
| 1582 | oldval = PyDict_GetItem(mapping, key); |
| 1583 | if (oldval == NULL) { |
| 1584 | if (PyDict_SetItem(mapping, key, one) == -1) |
| 1585 | break; |
| 1586 | } else { |
| 1587 | newval = PyNumber_Add(oldval, one); |
| 1588 | if (newval == NULL) |
| 1589 | break; |
| 1590 | if (PyDict_SetItem(mapping, key, newval) == -1) |
| 1591 | break; |
| 1592 | Py_CLEAR(newval); |
| 1593 | } |
| 1594 | Py_DECREF(key); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1595 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1596 | } else { |
| 1597 | while (1) { |
| 1598 | key = PyIter_Next(it); |
| 1599 | if (key == NULL) { |
| 1600 | if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 1601 | PyErr_Clear(); |
| 1602 | else |
| 1603 | break; |
| 1604 | } |
| 1605 | oldval = PyObject_GetItem(mapping, key); |
| 1606 | if (oldval == NULL) { |
| 1607 | if (!PyErr_Occurred() || !PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1608 | break; |
| 1609 | PyErr_Clear(); |
| 1610 | Py_INCREF(one); |
| 1611 | newval = one; |
| 1612 | } else { |
| 1613 | newval = PyNumber_Add(oldval, one); |
| 1614 | Py_DECREF(oldval); |
| 1615 | if (newval == NULL) |
| 1616 | break; |
| 1617 | } |
| 1618 | if (PyObject_SetItem(mapping, key, newval) == -1) |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1619 | break; |
| 1620 | Py_CLEAR(newval); |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1621 | Py_DECREF(key); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1622 | } |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1623 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1624 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1625 | Py_DECREF(it); |
| 1626 | Py_XDECREF(key); |
| 1627 | Py_XDECREF(newval); |
| 1628 | Py_DECREF(one); |
| 1629 | if (PyErr_Occurred()) |
| 1630 | return NULL; |
| 1631 | Py_RETURN_NONE; |
| 1632 | } |
| 1633 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1634 | /* module level code ********************************************************/ |
| 1635 | |
| 1636 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1637 | "High performance data structures.\n\ |
| 1638 | - deque: ordered collection accessible from endpoints only\n\ |
| 1639 | - defaultdict: dict subclass with a default value factory\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1640 | "); |
| 1641 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1642 | static struct PyMethodDef module_functions[] = { |
| 1643 | {"_count_elements", _count_elements, METH_VARARGS, _count_elements_doc}, |
| 1644 | {NULL, NULL} /* sentinel */ |
| 1645 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1646 | |
| 1647 | static struct PyModuleDef _collectionsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | PyModuleDef_HEAD_INIT, |
| 1649 | "_collections", |
| 1650 | module_doc, |
| 1651 | -1, |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1652 | module_functions, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | NULL, |
| 1654 | NULL, |
| 1655 | NULL, |
| 1656 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1657 | }; |
| 1658 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1659 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1660 | PyInit__collections(void) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1661 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | PyObject *m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | m = PyModule_Create(&_collectionsmodule); |
| 1665 | if (m == NULL) |
| 1666 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | if (PyType_Ready(&deque_type) < 0) |
| 1669 | return NULL; |
| 1670 | Py_INCREF(&deque_type); |
| 1671 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | defdict_type.tp_base = &PyDict_Type; |
| 1674 | if (PyType_Ready(&defdict_type) < 0) |
| 1675 | return NULL; |
| 1676 | Py_INCREF(&defdict_type); |
| 1677 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | if (PyType_Ready(&dequeiter_type) < 0) |
| 1680 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | if (PyType_Ready(&dequereviter_type) < 0) |
| 1683 | return NULL; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | return m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1686 | } |