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 { |
| 49 | struct BLOCK *leftlink; |
| 50 | struct BLOCK *rightlink; |
| 51 | PyObject *data[BLOCKLEN]; |
| 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) { |
Raymond Hettinger | c5fa992 | 2004-10-06 17:51:54 +0000 | [diff] [blame] | 60 | block *b; |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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 |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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 | */ |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +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 | 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 | { |
| 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; |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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 | |
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. |
| 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; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +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 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +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; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +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 * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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 | { |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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 | { |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 395 | Py_ssize_t n=1; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 396 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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 * |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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; |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +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 | 90aa764 | 2007-12-19 02:45:37 +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; |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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) { |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +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 | { |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 614 | if (((dequeobject *)deque)->maxlen == -1) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 615 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 616 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 617 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi", |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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 | { |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +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__"); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 629 | if (dict == NULL) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 630 | PyErr_Clear(); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 631 | aslist = PySequence_List((PyObject *)deque); |
| 632 | if (aslist == NULL) { |
| 633 | Py_XDECREF(dict); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 634 | return NULL; |
| 635 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 636 | if (dict == NULL) { |
| 637 | if (deque->maxlen == -1) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 638 | result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 639 | else |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 640 | result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 641 | } else { |
| 642 | if (deque->maxlen == -1) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 643 | result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 644 | else |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 645 | result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 646 | } |
| 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 | { |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 657 | PyObject *aslist, *result; |
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; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 664 | return PyUnicode_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 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 672 | if (((dequeobject *)deque)->maxlen != -1) |
| 673 | result = PyUnicode_FromFormat("deque(%R, maxlen=%i)", aslist, |
| 674 | ((dequeobject *)deque)->maxlen); |
| 675 | else |
| 676 | result = PyUnicode_FromFormat("deque(%R)", aslist); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 677 | Py_DECREF(aslist); |
| 678 | Py_ReprLeave(deque); |
| 679 | return result; |
| 680 | } |
| 681 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 682 | static PyObject * |
| 683 | deque_richcompare(PyObject *v, PyObject *w, int op) |
| 684 | { |
| 685 | PyObject *it1=NULL, *it2=NULL, *x, *y; |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 686 | Py_ssize_t vs, ws; |
| 687 | int b, cmp=-1; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 688 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 689 | if (!PyObject_TypeCheck(v, &deque_type) || |
Raymond Hettinger | 285cfcc | 2004-05-18 18:15:03 +0000 | [diff] [blame] | 690 | !PyObject_TypeCheck(w, &deque_type)) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 691 | Py_INCREF(Py_NotImplemented); |
| 692 | return Py_NotImplemented; |
| 693 | } |
| 694 | |
| 695 | /* Shortcuts */ |
| 696 | vs = ((dequeobject *)v)->len; |
| 697 | ws = ((dequeobject *)w)->len; |
| 698 | if (op == Py_EQ) { |
| 699 | if (v == w) |
| 700 | Py_RETURN_TRUE; |
| 701 | if (vs != ws) |
| 702 | Py_RETURN_FALSE; |
| 703 | } |
| 704 | if (op == Py_NE) { |
| 705 | if (v == w) |
| 706 | Py_RETURN_FALSE; |
| 707 | if (vs != ws) |
| 708 | Py_RETURN_TRUE; |
| 709 | } |
| 710 | |
| 711 | /* Search for the first index where items are different */ |
| 712 | it1 = PyObject_GetIter(v); |
| 713 | if (it1 == NULL) |
| 714 | goto done; |
| 715 | it2 = PyObject_GetIter(w); |
| 716 | if (it2 == NULL) |
| 717 | goto done; |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 718 | for (;;) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 719 | x = PyIter_Next(it1); |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 720 | if (x == NULL && PyErr_Occurred()) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 721 | goto done; |
| 722 | y = PyIter_Next(it2); |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 723 | if (x == NULL || y == NULL) |
| 724 | break; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 725 | b = PyObject_RichCompareBool(x, y, Py_EQ); |
| 726 | if (b == 0) { |
| 727 | cmp = PyObject_RichCompareBool(x, y, op); |
| 728 | Py_DECREF(x); |
| 729 | Py_DECREF(y); |
| 730 | goto done; |
| 731 | } |
| 732 | Py_DECREF(x); |
| 733 | Py_DECREF(y); |
| 734 | if (b == -1) |
| 735 | goto done; |
| 736 | } |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 737 | /* We reached the end of one deque or both */ |
| 738 | Py_XDECREF(x); |
| 739 | Py_XDECREF(y); |
| 740 | if (PyErr_Occurred()) |
| 741 | goto done; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 742 | switch (op) { |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 743 | case Py_LT: cmp = y != NULL; break; /* if w was longer */ |
| 744 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */ |
| 745 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ |
| 746 | case Py_NE: cmp = x != y; break; /* if one deque continues */ |
| 747 | case Py_GT: cmp = x != NULL; break; /* if v was longer */ |
| 748 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */ |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 749 | } |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 750 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 751 | done: |
| 752 | Py_XDECREF(it1); |
| 753 | Py_XDECREF(it2); |
| 754 | if (cmp == 1) |
| 755 | Py_RETURN_TRUE; |
| 756 | if (cmp == 0) |
| 757 | Py_RETURN_FALSE; |
| 758 | return NULL; |
| 759 | } |
| 760 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 761 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 762 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 763 | { |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 764 | PyObject *iterable = NULL; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 765 | PyObject *maxlenobj = NULL; |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 766 | Py_ssize_t maxlen = -1; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 767 | char *kwlist[] = {"iterable", "maxlen", 0}; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 768 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 769 | if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 770 | return -1; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 771 | if (maxlenobj != NULL && maxlenobj != Py_None) { |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 772 | maxlen = PyLong_AsSsize_t(maxlenobj); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 773 | if (maxlen == -1 && PyErr_Occurred()) |
| 774 | return -1; |
| 775 | if (maxlen < 0) { |
| 776 | PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); |
| 777 | return -1; |
| 778 | } |
| 779 | } |
| 780 | deque->maxlen = maxlen; |
Christian Heimes | 3805321 | 2007-12-14 01:24:44 +0000 | [diff] [blame] | 781 | deque_clear(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 782 | if (iterable != NULL) { |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 783 | PyObject *rv = deque_extend(deque, iterable); |
| 784 | if (rv == NULL) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 785 | return -1; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 786 | Py_DECREF(rv); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 787 | } |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static PySequenceMethods deque_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 792 | (lenfunc)deque_len, /* sq_length */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 793 | 0, /* sq_concat */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 794 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 795 | (ssizeargfunc)deque_item, /* sq_item */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 796 | 0, /* sq_slice */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 797 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 798 | }; |
| 799 | |
| 800 | /* deque object ********************************************************/ |
| 801 | |
| 802 | static PyObject *deque_iter(dequeobject *deque); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 803 | static PyObject *deque_reviter(dequeobject *deque); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 804 | PyDoc_STRVAR(reversed_doc, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 805 | "D.__reversed__() -- return a reverse iterator over the deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 806 | |
| 807 | static PyMethodDef deque_methods[] = { |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 808 | {"append", (PyCFunction)deque_append, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 809 | METH_O, append_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 810 | {"appendleft", (PyCFunction)deque_appendleft, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 811 | METH_O, appendleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 812 | {"clear", (PyCFunction)deque_clearmethod, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 813 | METH_NOARGS, clear_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 814 | {"__copy__", (PyCFunction)deque_copy, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 815 | METH_NOARGS, copy_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 816 | {"extend", (PyCFunction)deque_extend, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 817 | METH_O, extend_doc}, |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 818 | {"extendleft", (PyCFunction)deque_extendleft, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 819 | METH_O, extendleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 820 | {"pop", (PyCFunction)deque_pop, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 821 | METH_NOARGS, pop_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 822 | {"popleft", (PyCFunction)deque_popleft, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 823 | METH_NOARGS, popleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 824 | {"__reduce__", (PyCFunction)deque_reduce, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 825 | METH_NOARGS, reduce_doc}, |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 826 | {"remove", (PyCFunction)deque_remove, |
| 827 | METH_O, remove_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 828 | {"__reversed__", (PyCFunction)deque_reviter, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 829 | METH_NOARGS, reversed_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 830 | {"rotate", (PyCFunction)deque_rotate, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 831 | METH_VARARGS, rotate_doc}, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 832 | {NULL, NULL} /* sentinel */ |
| 833 | }; |
| 834 | |
| 835 | PyDoc_STRVAR(deque_doc, |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 836 | "deque(iterable[, maxlen]) --> deque object\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 837 | \n\ |
| 838 | Build an ordered collection accessible from endpoints only."); |
| 839 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 840 | static PyTypeObject deque_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 841 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 842 | "collections.deque", /* tp_name */ |
| 843 | sizeof(dequeobject), /* tp_basicsize */ |
| 844 | 0, /* tp_itemsize */ |
| 845 | /* methods */ |
| 846 | (destructor)deque_dealloc, /* tp_dealloc */ |
Guido van Rossum | 346f1a8 | 2007-08-07 19:58:47 +0000 | [diff] [blame] | 847 | 0, /* tp_print */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 848 | 0, /* tp_getattr */ |
| 849 | 0, /* tp_setattr */ |
| 850 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 851 | deque_repr, /* tp_repr */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 852 | 0, /* tp_as_number */ |
| 853 | &deque_as_sequence, /* tp_as_sequence */ |
| 854 | 0, /* tp_as_mapping */ |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 855 | (hashfunc)PyObject_HashNotImplemented, /* tp_hash */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 856 | 0, /* tp_call */ |
| 857 | 0, /* tp_str */ |
| 858 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 859 | 0, /* tp_setattro */ |
| 860 | 0, /* tp_as_buffer */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 861 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 862 | /* tp_flags */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 863 | deque_doc, /* tp_doc */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 864 | (traverseproc)deque_traverse, /* tp_traverse */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 865 | (inquiry)deque_clear, /* tp_clear */ |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 866 | (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 867 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 868 | (getiterfunc)deque_iter, /* tp_iter */ |
| 869 | 0, /* tp_iternext */ |
| 870 | deque_methods, /* tp_methods */ |
| 871 | 0, /* tp_members */ |
| 872 | 0, /* tp_getset */ |
| 873 | 0, /* tp_base */ |
| 874 | 0, /* tp_dict */ |
| 875 | 0, /* tp_descr_get */ |
| 876 | 0, /* tp_descr_set */ |
| 877 | 0, /* tp_dictoffset */ |
| 878 | (initproc)deque_init, /* tp_init */ |
| 879 | PyType_GenericAlloc, /* tp_alloc */ |
| 880 | deque_new, /* tp_new */ |
| 881 | PyObject_GC_Del, /* tp_free */ |
| 882 | }; |
| 883 | |
| 884 | /*********************** Deque Iterator **************************/ |
| 885 | |
| 886 | typedef struct { |
| 887 | PyObject_HEAD |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 888 | Py_ssize_t index; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 889 | block *b; |
| 890 | dequeobject *deque; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 891 | long state; /* state when the iterator is created */ |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 892 | Py_ssize_t counter; /* number of items remaining for iteration */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 893 | } dequeiterobject; |
| 894 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 895 | static PyTypeObject dequeiter_type; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 896 | |
| 897 | static PyObject * |
| 898 | deque_iter(dequeobject *deque) |
| 899 | { |
| 900 | dequeiterobject *it; |
| 901 | |
| 902 | it = PyObject_New(dequeiterobject, &dequeiter_type); |
| 903 | if (it == NULL) |
| 904 | return NULL; |
| 905 | it->b = deque->leftblock; |
| 906 | it->index = deque->leftindex; |
| 907 | Py_INCREF(deque); |
| 908 | it->deque = deque; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 909 | it->state = deque->state; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 910 | it->counter = deque->len; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 911 | return (PyObject *)it; |
| 912 | } |
| 913 | |
| 914 | static void |
| 915 | dequeiter_dealloc(dequeiterobject *dio) |
| 916 | { |
| 917 | Py_XDECREF(dio->deque); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 918 | Py_TYPE(dio)->tp_free(dio); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | static PyObject * |
| 922 | dequeiter_next(dequeiterobject *it) |
| 923 | { |
| 924 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 925 | |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 926 | if (it->deque->state != it->state) { |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 927 | it->counter = 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 928 | PyErr_SetString(PyExc_RuntimeError, |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 929 | "deque mutated during iteration"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 930 | return NULL; |
| 931 | } |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 932 | if (it->counter == 0) |
| 933 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 934 | assert (!(it->b == it->deque->rightblock && |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 935 | it->index > it->deque->rightindex)); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 936 | |
| 937 | item = it->b->data[it->index]; |
| 938 | it->index++; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 939 | it->counter--; |
| 940 | if (it->index == BLOCKLEN && it->counter > 0) { |
| 941 | assert (it->b->rightlink != NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 942 | it->b = it->b->rightlink; |
| 943 | it->index = 0; |
| 944 | } |
| 945 | Py_INCREF(item); |
| 946 | return item; |
| 947 | } |
| 948 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 949 | static PyObject * |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 950 | dequeiter_len(dequeiterobject *it) |
| 951 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 952 | return PyLong_FromLong(it->counter); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 955 | 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] | 956 | |
| 957 | static PyMethodDef dequeiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 958 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 959 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 960 | }; |
| 961 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 962 | static PyTypeObject dequeiter_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 963 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 964 | "deque_iterator", /* tp_name */ |
| 965 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 966 | 0, /* tp_itemsize */ |
| 967 | /* methods */ |
| 968 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 969 | 0, /* tp_print */ |
| 970 | 0, /* tp_getattr */ |
| 971 | 0, /* tp_setattr */ |
| 972 | 0, /* tp_compare */ |
| 973 | 0, /* tp_repr */ |
| 974 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 975 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 976 | 0, /* tp_as_mapping */ |
| 977 | 0, /* tp_hash */ |
| 978 | 0, /* tp_call */ |
| 979 | 0, /* tp_str */ |
| 980 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 981 | 0, /* tp_setattro */ |
| 982 | 0, /* tp_as_buffer */ |
| 983 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 984 | 0, /* tp_doc */ |
| 985 | 0, /* tp_traverse */ |
| 986 | 0, /* tp_clear */ |
| 987 | 0, /* tp_richcompare */ |
| 988 | 0, /* tp_weaklistoffset */ |
| 989 | PyObject_SelfIter, /* tp_iter */ |
| 990 | (iternextfunc)dequeiter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 991 | dequeiter_methods, /* tp_methods */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 992 | 0, |
| 993 | }; |
| 994 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 995 | /*********************** Deque Reverse Iterator **************************/ |
| 996 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 997 | static PyTypeObject dequereviter_type; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 998 | |
| 999 | static PyObject * |
| 1000 | deque_reviter(dequeobject *deque) |
| 1001 | { |
| 1002 | dequeiterobject *it; |
| 1003 | |
| 1004 | it = PyObject_New(dequeiterobject, &dequereviter_type); |
| 1005 | if (it == NULL) |
| 1006 | return NULL; |
| 1007 | it->b = deque->rightblock; |
| 1008 | it->index = deque->rightindex; |
| 1009 | Py_INCREF(deque); |
| 1010 | it->deque = deque; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1011 | it->state = deque->state; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1012 | it->counter = deque->len; |
| 1013 | return (PyObject *)it; |
| 1014 | } |
| 1015 | |
| 1016 | static PyObject * |
| 1017 | dequereviter_next(dequeiterobject *it) |
| 1018 | { |
| 1019 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1020 | if (it->counter == 0) |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1021 | return NULL; |
| 1022 | |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1023 | if (it->deque->state != it->state) { |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 1024 | it->counter = 0; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1025 | PyErr_SetString(PyExc_RuntimeError, |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1026 | "deque mutated during iteration"); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1027 | return NULL; |
| 1028 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1029 | assert (!(it->b == it->deque->leftblock && |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1030 | it->index < it->deque->leftindex)); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1031 | |
| 1032 | item = it->b->data[it->index]; |
| 1033 | it->index--; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1034 | it->counter--; |
| 1035 | if (it->index == -1 && it->counter > 0) { |
| 1036 | assert (it->b->leftlink != NULL); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1037 | it->b = it->b->leftlink; |
| 1038 | it->index = BLOCKLEN - 1; |
| 1039 | } |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1040 | Py_INCREF(item); |
| 1041 | return item; |
| 1042 | } |
| 1043 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1044 | static PyTypeObject dequereviter_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1045 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1046 | "deque_reverse_iterator", /* tp_name */ |
| 1047 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1048 | 0, /* tp_itemsize */ |
| 1049 | /* methods */ |
| 1050 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1051 | 0, /* tp_print */ |
| 1052 | 0, /* tp_getattr */ |
| 1053 | 0, /* tp_setattr */ |
| 1054 | 0, /* tp_compare */ |
| 1055 | 0, /* tp_repr */ |
| 1056 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1057 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1058 | 0, /* tp_as_mapping */ |
| 1059 | 0, /* tp_hash */ |
| 1060 | 0, /* tp_call */ |
| 1061 | 0, /* tp_str */ |
| 1062 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1063 | 0, /* tp_setattro */ |
| 1064 | 0, /* tp_as_buffer */ |
| 1065 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1066 | 0, /* tp_doc */ |
| 1067 | 0, /* tp_traverse */ |
| 1068 | 0, /* tp_clear */ |
| 1069 | 0, /* tp_richcompare */ |
| 1070 | 0, /* tp_weaklistoffset */ |
| 1071 | PyObject_SelfIter, /* tp_iter */ |
| 1072 | (iternextfunc)dequereviter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1073 | dequeiter_methods, /* tp_methods */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1074 | 0, |
| 1075 | }; |
| 1076 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1077 | /* defaultdict type *********************************************************/ |
| 1078 | |
| 1079 | typedef struct { |
| 1080 | PyDictObject dict; |
| 1081 | PyObject *default_factory; |
| 1082 | } defdictobject; |
| 1083 | |
| 1084 | static PyTypeObject defdict_type; /* Forward */ |
| 1085 | |
| 1086 | PyDoc_STRVAR(defdict_missing_doc, |
| 1087 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1088 | if self.default_factory is None: raise KeyError((key,))\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1089 | self[key] = value = self.default_factory()\n\ |
| 1090 | return value\n\ |
| 1091 | "); |
| 1092 | |
| 1093 | static PyObject * |
| 1094 | defdict_missing(defdictobject *dd, PyObject *key) |
| 1095 | { |
| 1096 | PyObject *factory = dd->default_factory; |
| 1097 | PyObject *value; |
| 1098 | if (factory == NULL || factory == Py_None) { |
| 1099 | /* XXX Call dict.__missing__(key) */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1100 | PyObject *tup; |
| 1101 | tup = PyTuple_Pack(1, key); |
| 1102 | if (!tup) return NULL; |
| 1103 | PyErr_SetObject(PyExc_KeyError, tup); |
| 1104 | Py_DECREF(tup); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1105 | return NULL; |
| 1106 | } |
| 1107 | value = PyEval_CallObject(factory, NULL); |
| 1108 | if (value == NULL) |
| 1109 | return value; |
| 1110 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) { |
| 1111 | Py_DECREF(value); |
| 1112 | return NULL; |
| 1113 | } |
| 1114 | return value; |
| 1115 | } |
| 1116 | |
| 1117 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D."); |
| 1118 | |
| 1119 | static PyObject * |
| 1120 | defdict_copy(defdictobject *dd) |
| 1121 | { |
| 1122 | /* This calls the object's class. That only works for subclasses |
| 1123 | whose class constructor has the same signature. Subclasses that |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 1124 | define a different constructor signature must override copy(). |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1125 | */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1126 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1127 | dd->default_factory, dd, NULL); |
| 1128 | } |
| 1129 | |
| 1130 | static PyObject * |
| 1131 | defdict_reduce(defdictobject *dd) |
| 1132 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1133 | /* __reduce__ must return a 5-tuple as follows: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1134 | |
| 1135 | - factory function |
| 1136 | - tuple of args for the factory function |
| 1137 | - additional state (here None) |
| 1138 | - sequence iterator (here None) |
| 1139 | - dictionary iterator (yielding successive (key, value) pairs |
| 1140 | |
| 1141 | This API is used by pickle.py and copy.py. |
| 1142 | |
| 1143 | For this to be useful with pickle.py, the default_factory |
| 1144 | must be picklable; e.g., None, a built-in, or a global |
| 1145 | function in a module or package. |
| 1146 | |
| 1147 | Both shallow and deep copying are supported, but for deep |
| 1148 | copying, the default_factory must be deep-copyable; e.g. None, |
| 1149 | or a built-in (functions are not copyable at this time). |
| 1150 | |
| 1151 | This only works for subclasses as long as their constructor |
| 1152 | signature is compatible; the first argument must be the |
| 1153 | optional default_factory, defaulting to None. |
| 1154 | */ |
| 1155 | PyObject *args; |
| 1156 | PyObject *items; |
| 1157 | PyObject *result; |
| 1158 | if (dd->default_factory == NULL || dd->default_factory == Py_None) |
| 1159 | args = PyTuple_New(0); |
| 1160 | else |
| 1161 | args = PyTuple_Pack(1, dd->default_factory); |
| 1162 | if (args == NULL) |
| 1163 | return NULL; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1164 | items = PyObject_CallMethod((PyObject *)dd, "items", "()"); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1165 | if (items == NULL) { |
| 1166 | Py_DECREF(args); |
| 1167 | return NULL; |
| 1168 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1169 | result = PyTuple_Pack(5, Py_TYPE(dd), args, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1170 | Py_None, Py_None, items); |
| 1171 | Py_DECREF(items); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1172 | Py_DECREF(args); |
| 1173 | return result; |
| 1174 | } |
| 1175 | |
| 1176 | static PyMethodDef defdict_methods[] = { |
| 1177 | {"__missing__", (PyCFunction)defdict_missing, METH_O, |
| 1178 | defdict_missing_doc}, |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 1179 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1180 | defdict_copy_doc}, |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1181 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1182 | defdict_copy_doc}, |
| 1183 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS, |
| 1184 | reduce_doc}, |
| 1185 | {NULL} |
| 1186 | }; |
| 1187 | |
| 1188 | static PyMemberDef defdict_members[] = { |
| 1189 | {"default_factory", T_OBJECT, |
| 1190 | offsetof(defdictobject, default_factory), 0, |
| 1191 | PyDoc_STR("Factory for default value called by __missing__().")}, |
| 1192 | {NULL} |
| 1193 | }; |
| 1194 | |
| 1195 | static void |
| 1196 | defdict_dealloc(defdictobject *dd) |
| 1197 | { |
| 1198 | Py_CLEAR(dd->default_factory); |
| 1199 | PyDict_Type.tp_dealloc((PyObject *)dd); |
| 1200 | } |
| 1201 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1202 | static PyObject * |
| 1203 | defdict_repr(defdictobject *dd) |
| 1204 | { |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1205 | PyObject *baserepr; |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 1206 | PyObject *defrepr; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1207 | PyObject *result; |
| 1208 | baserepr = PyDict_Type.tp_repr((PyObject *)dd); |
| 1209 | if (baserepr == NULL) |
| 1210 | return NULL; |
| 1211 | if (dd->default_factory == NULL) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 1212 | defrepr = PyUnicode_FromString("None"); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1213 | else |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 1214 | { |
| 1215 | int status = Py_ReprEnter(dd->default_factory); |
| 1216 | if (status != 0) { |
| 1217 | if (status < 0) |
| 1218 | return NULL; |
| 1219 | defrepr = PyUnicode_FromString("..."); |
| 1220 | } |
| 1221 | else |
| 1222 | defrepr = PyObject_Repr(dd->default_factory); |
| 1223 | Py_ReprLeave(dd->default_factory); |
| 1224 | } |
| 1225 | if (defrepr == NULL) { |
| 1226 | Py_DECREF(baserepr); |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | result = PyUnicode_FromFormat("defaultdict(%U, %U)", |
| 1230 | defrepr, baserepr); |
| 1231 | Py_DECREF(defrepr); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1232 | Py_DECREF(baserepr); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1233 | return result; |
| 1234 | } |
| 1235 | |
| 1236 | static int |
| 1237 | defdict_traverse(PyObject *self, visitproc visit, void *arg) |
| 1238 | { |
| 1239 | Py_VISIT(((defdictobject *)self)->default_factory); |
| 1240 | return PyDict_Type.tp_traverse(self, visit, arg); |
| 1241 | } |
| 1242 | |
| 1243 | static int |
| 1244 | defdict_tp_clear(defdictobject *dd) |
| 1245 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1246 | Py_CLEAR(dd->default_factory); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1247 | return PyDict_Type.tp_clear((PyObject *)dd); |
| 1248 | } |
| 1249 | |
| 1250 | static int |
| 1251 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1252 | { |
| 1253 | defdictobject *dd = (defdictobject *)self; |
| 1254 | PyObject *olddefault = dd->default_factory; |
| 1255 | PyObject *newdefault = NULL; |
| 1256 | PyObject *newargs; |
| 1257 | int result; |
| 1258 | if (args == NULL || !PyTuple_Check(args)) |
| 1259 | newargs = PyTuple_New(0); |
| 1260 | else { |
| 1261 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1262 | if (n > 0) { |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1263 | newdefault = PyTuple_GET_ITEM(args, 0); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1264 | if (!PyCallable_Check(newdefault)) { |
| 1265 | PyErr_SetString(PyExc_TypeError, |
| 1266 | "first argument must be callable"); |
| 1267 | return -1; |
| 1268 | } |
| 1269 | } |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1270 | newargs = PySequence_GetSlice(args, 1, n); |
| 1271 | } |
| 1272 | if (newargs == NULL) |
| 1273 | return -1; |
| 1274 | Py_XINCREF(newdefault); |
| 1275 | dd->default_factory = newdefault; |
| 1276 | result = PyDict_Type.tp_init(self, newargs, kwds); |
| 1277 | Py_DECREF(newargs); |
| 1278 | Py_XDECREF(olddefault); |
| 1279 | return result; |
| 1280 | } |
| 1281 | |
| 1282 | PyDoc_STRVAR(defdict_doc, |
| 1283 | "defaultdict(default_factory) --> dict with default factory\n\ |
| 1284 | \n\ |
| 1285 | The default factory is called without arguments to produce\n\ |
| 1286 | a new value when a key is not present, in __getitem__ only.\n\ |
| 1287 | A defaultdict compares equal to a dict with the same items.\n\ |
| 1288 | "); |
| 1289 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1290 | /* See comment in xxsubtype.c */ |
| 1291 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 1292 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1293 | static PyTypeObject defdict_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1294 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1295 | "collections.defaultdict", /* tp_name */ |
| 1296 | sizeof(defdictobject), /* tp_basicsize */ |
| 1297 | 0, /* tp_itemsize */ |
| 1298 | /* methods */ |
| 1299 | (destructor)defdict_dealloc, /* tp_dealloc */ |
Guido van Rossum | 346f1a8 | 2007-08-07 19:58:47 +0000 | [diff] [blame] | 1300 | 0, /* tp_print */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1301 | 0, /* tp_getattr */ |
| 1302 | 0, /* tp_setattr */ |
| 1303 | 0, /* tp_compare */ |
| 1304 | (reprfunc)defdict_repr, /* tp_repr */ |
| 1305 | 0, /* tp_as_number */ |
| 1306 | 0, /* tp_as_sequence */ |
| 1307 | 0, /* tp_as_mapping */ |
| 1308 | 0, /* tp_hash */ |
| 1309 | 0, /* tp_call */ |
| 1310 | 0, /* tp_str */ |
| 1311 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1312 | 0, /* tp_setattro */ |
| 1313 | 0, /* tp_as_buffer */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1314 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1315 | /* tp_flags */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1316 | defdict_doc, /* tp_doc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1317 | defdict_traverse, /* tp_traverse */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1318 | (inquiry)defdict_tp_clear, /* tp_clear */ |
| 1319 | 0, /* tp_richcompare */ |
| 1320 | 0, /* tp_weaklistoffset*/ |
| 1321 | 0, /* tp_iter */ |
| 1322 | 0, /* tp_iternext */ |
| 1323 | defdict_methods, /* tp_methods */ |
| 1324 | defdict_members, /* tp_members */ |
| 1325 | 0, /* tp_getset */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1326 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1327 | 0, /* tp_dict */ |
| 1328 | 0, /* tp_descr_get */ |
| 1329 | 0, /* tp_descr_set */ |
| 1330 | 0, /* tp_dictoffset */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1331 | defdict_init, /* tp_init */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1332 | PyType_GenericAlloc, /* tp_alloc */ |
| 1333 | 0, /* tp_new */ |
| 1334 | PyObject_GC_Del, /* tp_free */ |
| 1335 | }; |
| 1336 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1337 | /* module level code ********************************************************/ |
| 1338 | |
| 1339 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1340 | "High performance data structures.\n\ |
| 1341 | - deque: ordered collection accessible from endpoints only\n\ |
| 1342 | - defaultdict: dict subclass with a default value factory\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1343 | "); |
| 1344 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1345 | |
| 1346 | static struct PyModuleDef _collectionsmodule = { |
| 1347 | PyModuleDef_HEAD_INIT, |
| 1348 | "_collections", |
| 1349 | module_doc, |
| 1350 | -1, |
| 1351 | NULL, |
| 1352 | NULL, |
| 1353 | NULL, |
| 1354 | NULL, |
| 1355 | NULL |
| 1356 | }; |
| 1357 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1358 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1359 | PyInit__collections(void) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1360 | { |
| 1361 | PyObject *m; |
| 1362 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1363 | m = PyModule_Create(&_collectionsmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1364 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1365 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1366 | |
| 1367 | if (PyType_Ready(&deque_type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1368 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1369 | Py_INCREF(&deque_type); |
| 1370 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
| 1371 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1372 | defdict_type.tp_base = &PyDict_Type; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1373 | if (PyType_Ready(&defdict_type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1374 | return NULL; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1375 | Py_INCREF(&defdict_type); |
| 1376 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); |
| 1377 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1378 | if (PyType_Ready(&dequeiter_type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1379 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1380 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1381 | if (PyType_Ready(&dequereviter_type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1382 | return NULL; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1383 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1384 | return m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1385 | } |