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