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