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