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 | |
| 10 | #define BLOCKLEN 46 |
| 11 | |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 12 | /* A `dequeobject` is composed of a doubly-linked list of `block` nodes. |
| 13 | * This list is not circular (the leftmost block has leftlink==NULL, |
| 14 | * and the rightmost block has rightlink==NULL). A deque d's first |
| 15 | * element is at d.leftblock[leftindex] and its last element is at |
| 16 | * d.rightblock[rightindex]; note that, unlike as for Python slice |
Tim Peters | d6e0032 | 2004-10-01 01:35:54 +0000 | [diff] [blame^] | 17 | * indices, these indices are inclusive on both ends. |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 18 | * The list of blocks is never empty. An empty deque d has |
| 19 | * d.leftblock == d.rightblock != NULL; d.len == 0; and |
| 20 | * d.leftindex > d.rightindex; checking for d.len == 0 is the intended |
| 21 | * way to see whether d is empty. |
| 22 | * Note that since d.leftindex and d.rightindex may be indices into |
Tim Peters | d6e0032 | 2004-10-01 01:35:54 +0000 | [diff] [blame^] | 23 | * distinct blocks (and certainly are, for any d with len(d) > BLOCKLEN), |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 24 | * it's not generally true that d.leftindex <= d.rightindex. |
| 25 | */ |
| 26 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 27 | typedef struct BLOCK { |
| 28 | struct BLOCK *leftlink; |
| 29 | struct BLOCK *rightlink; |
| 30 | PyObject *data[BLOCKLEN]; |
| 31 | } block; |
| 32 | |
Tim Peters | 6f85356 | 2004-10-01 01:04:50 +0000 | [diff] [blame] | 33 | static block * |
| 34 | newblock(block *leftlink, block *rightlink) { |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 35 | block *b = PyMem_Malloc(sizeof(block)); |
| 36 | if (b == NULL) { |
| 37 | PyErr_NoMemory(); |
| 38 | return NULL; |
| 39 | } |
| 40 | b->leftlink = leftlink; |
| 41 | b->rightlink = rightlink; |
| 42 | return b; |
| 43 | } |
| 44 | |
| 45 | typedef struct { |
| 46 | PyObject_HEAD |
| 47 | block *leftblock; |
| 48 | block *rightblock; |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 49 | int leftindex; /* in range(BLOCKLEN) */ |
| 50 | int rightindex; /* in range(BLOCKLEN) */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 51 | int len; |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 52 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 53 | } dequeobject; |
| 54 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 55 | static PyTypeObject deque_type; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 56 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 57 | static PyObject * |
| 58 | deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 59 | { |
| 60 | dequeobject *deque; |
| 61 | block *b; |
| 62 | |
| 63 | /* create dequeobject structure */ |
| 64 | deque = (dequeobject *)type->tp_alloc(type, 0); |
| 65 | if (deque == NULL) |
| 66 | return NULL; |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 67 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 68 | b = newblock(NULL, NULL); |
| 69 | if (b == NULL) { |
| 70 | Py_DECREF(deque); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | deque->leftblock = b; |
| 75 | deque->rightblock = b; |
| 76 | deque->leftindex = BLOCKLEN / 2 + 1; |
| 77 | deque->rightindex = BLOCKLEN / 2; |
| 78 | deque->len = 0; |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 79 | deque->weakreflist = NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 80 | |
| 81 | return (PyObject *)deque; |
| 82 | } |
| 83 | |
| 84 | static PyObject * |
| 85 | deque_append(dequeobject *deque, PyObject *item) |
| 86 | { |
| 87 | deque->rightindex++; |
| 88 | deque->len++; |
| 89 | if (deque->rightindex == BLOCKLEN) { |
| 90 | block *b = newblock(deque->rightblock, NULL); |
| 91 | if (b == NULL) |
| 92 | return NULL; |
| 93 | assert(deque->rightblock->rightlink == NULL); |
| 94 | deque->rightblock->rightlink = b; |
| 95 | deque->rightblock = b; |
| 96 | deque->rightindex = 0; |
| 97 | } |
| 98 | Py_INCREF(item); |
| 99 | deque->rightblock->data[deque->rightindex] = item; |
| 100 | Py_RETURN_NONE; |
| 101 | } |
| 102 | |
| 103 | PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque."); |
| 104 | |
| 105 | static PyObject * |
| 106 | deque_appendleft(dequeobject *deque, PyObject *item) |
| 107 | { |
| 108 | deque->leftindex--; |
| 109 | deque->len++; |
| 110 | if (deque->leftindex == -1) { |
| 111 | block *b = newblock(NULL, deque->leftblock); |
| 112 | if (b == NULL) |
| 113 | return NULL; |
| 114 | assert(deque->leftblock->leftlink == NULL); |
| 115 | deque->leftblock->leftlink = b; |
| 116 | deque->leftblock = b; |
| 117 | deque->leftindex = BLOCKLEN - 1; |
| 118 | } |
| 119 | Py_INCREF(item); |
| 120 | deque->leftblock->data[deque->leftindex] = item; |
| 121 | Py_RETURN_NONE; |
| 122 | } |
| 123 | |
| 124 | PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque."); |
| 125 | |
| 126 | static PyObject * |
| 127 | deque_pop(dequeobject *deque, PyObject *unused) |
| 128 | { |
| 129 | PyObject *item; |
| 130 | block *prevblock; |
| 131 | |
| 132 | if (deque->len == 0) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 133 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | item = deque->rightblock->data[deque->rightindex]; |
| 137 | deque->rightindex--; |
| 138 | deque->len--; |
| 139 | |
| 140 | if (deque->rightindex == -1) { |
| 141 | if (deque->len == 0) { |
| 142 | assert(deque->leftblock == deque->rightblock); |
| 143 | assert(deque->leftindex == deque->rightindex+1); |
| 144 | /* re-center instead of freeing a block */ |
| 145 | deque->leftindex = BLOCKLEN / 2 + 1; |
| 146 | deque->rightindex = BLOCKLEN / 2; |
| 147 | } else { |
| 148 | prevblock = deque->rightblock->leftlink; |
| 149 | assert(deque->leftblock != deque->rightblock); |
| 150 | PyMem_Free(deque->rightblock); |
| 151 | prevblock->rightlink = NULL; |
| 152 | deque->rightblock = prevblock; |
| 153 | deque->rightindex = BLOCKLEN - 1; |
| 154 | } |
| 155 | } |
| 156 | return item; |
| 157 | } |
| 158 | |
| 159 | PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element."); |
| 160 | |
| 161 | static PyObject * |
| 162 | deque_popleft(dequeobject *deque, PyObject *unused) |
| 163 | { |
| 164 | PyObject *item; |
| 165 | block *prevblock; |
| 166 | |
| 167 | if (deque->len == 0) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 168 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
| 171 | item = deque->leftblock->data[deque->leftindex]; |
| 172 | deque->leftindex++; |
| 173 | deque->len--; |
| 174 | |
| 175 | if (deque->leftindex == BLOCKLEN) { |
| 176 | if (deque->len == 0) { |
| 177 | assert(deque->leftblock == deque->rightblock); |
| 178 | assert(deque->leftindex == deque->rightindex+1); |
| 179 | /* re-center instead of freeing a block */ |
| 180 | deque->leftindex = BLOCKLEN / 2 + 1; |
| 181 | deque->rightindex = BLOCKLEN / 2; |
| 182 | } else { |
| 183 | assert(deque->leftblock != deque->rightblock); |
| 184 | prevblock = deque->leftblock->rightlink; |
| 185 | assert(deque->leftblock != NULL); |
| 186 | PyMem_Free(deque->leftblock); |
| 187 | assert(prevblock != NULL); |
| 188 | prevblock->leftlink = NULL; |
| 189 | deque->leftblock = prevblock; |
| 190 | deque->leftindex = 0; |
| 191 | } |
| 192 | } |
| 193 | return item; |
| 194 | } |
| 195 | |
| 196 | PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element."); |
| 197 | |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 198 | static PyObject * |
| 199 | deque_extend(dequeobject *deque, PyObject *iterable) |
| 200 | { |
| 201 | PyObject *it, *item; |
| 202 | |
| 203 | it = PyObject_GetIter(iterable); |
| 204 | if (it == NULL) |
| 205 | return NULL; |
| 206 | |
| 207 | while ((item = PyIter_Next(it)) != NULL) { |
| 208 | deque->rightindex++; |
| 209 | deque->len++; |
| 210 | if (deque->rightindex == BLOCKLEN) { |
| 211 | block *b = newblock(deque->rightblock, NULL); |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 212 | if (b == NULL) { |
| 213 | Py_DECREF(item); |
| 214 | Py_DECREF(it); |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 215 | return NULL; |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 216 | } |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 217 | assert(deque->rightblock->rightlink == NULL); |
| 218 | deque->rightblock->rightlink = b; |
| 219 | deque->rightblock = b; |
| 220 | deque->rightindex = 0; |
| 221 | } |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 222 | deque->rightblock->data[deque->rightindex] = item; |
| 223 | } |
| 224 | Py_DECREF(it); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 225 | if (PyErr_Occurred()) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 226 | return NULL; |
| 227 | Py_RETURN_NONE; |
| 228 | } |
| 229 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 230 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 231 | "Extend the right side of the deque with elements from the iterable"); |
| 232 | |
| 233 | static PyObject * |
| 234 | deque_extendleft(dequeobject *deque, PyObject *iterable) |
| 235 | { |
| 236 | PyObject *it, *item; |
| 237 | |
| 238 | it = PyObject_GetIter(iterable); |
| 239 | if (it == NULL) |
| 240 | return NULL; |
| 241 | |
| 242 | while ((item = PyIter_Next(it)) != NULL) { |
| 243 | deque->leftindex--; |
| 244 | deque->len++; |
| 245 | if (deque->leftindex == -1) { |
| 246 | block *b = newblock(NULL, deque->leftblock); |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 247 | if (b == NULL) { |
| 248 | Py_DECREF(item); |
| 249 | Py_DECREF(it); |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 250 | return NULL; |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 251 | } |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 252 | assert(deque->leftblock->leftlink == NULL); |
| 253 | deque->leftblock->leftlink = b; |
| 254 | deque->leftblock = b; |
| 255 | deque->leftindex = BLOCKLEN - 1; |
| 256 | } |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 257 | deque->leftblock->data[deque->leftindex] = item; |
| 258 | } |
| 259 | Py_DECREF(it); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 260 | if (PyErr_Occurred()) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 261 | return NULL; |
| 262 | Py_RETURN_NONE; |
| 263 | } |
| 264 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 265 | PyDoc_STRVAR(extendleft_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 266 | "Extend the left side of the deque with elements from the iterable"); |
| 267 | |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 268 | static PyObject * |
| 269 | deque_rotate(dequeobject *deque, PyObject *args) |
| 270 | { |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 271 | int i, n=1, len=deque->len, halflen=(len+1)>>1; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 272 | PyObject *item, *rv; |
| 273 | |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 274 | if (!PyArg_ParseTuple(args, "|i:rotate", &n)) |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 275 | return NULL; |
| 276 | |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 277 | if (len == 0) |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 278 | Py_RETURN_NONE; |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 279 | if (n > halflen || n < -halflen) { |
| 280 | n %= len; |
| 281 | if (n > halflen) |
| 282 | n -= len; |
| 283 | else if (n < -halflen) |
| 284 | n += len; |
| 285 | } |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 286 | |
| 287 | for (i=0 ; i<n ; i++) { |
| 288 | item = deque_pop(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 289 | assert (item != NULL); |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 290 | rv = deque_appendleft(deque, item); |
| 291 | Py_DECREF(item); |
| 292 | if (rv == NULL) |
| 293 | return NULL; |
| 294 | Py_DECREF(rv); |
| 295 | } |
| 296 | for (i=0 ; i>n ; i--) { |
| 297 | item = deque_popleft(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 298 | assert (item != NULL); |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 299 | rv = deque_append(deque, item); |
| 300 | Py_DECREF(item); |
| 301 | if (rv == NULL) |
| 302 | return NULL; |
| 303 | Py_DECREF(rv); |
| 304 | } |
| 305 | Py_RETURN_NONE; |
| 306 | } |
| 307 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 308 | PyDoc_STRVAR(rotate_doc, |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 309 | "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] | 310 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 311 | static int |
| 312 | deque_len(dequeobject *deque) |
| 313 | { |
| 314 | return deque->len; |
| 315 | } |
| 316 | |
| 317 | static int |
| 318 | deque_clear(dequeobject *deque) |
| 319 | { |
| 320 | PyObject *item; |
| 321 | |
| 322 | while (deque_len(deque)) { |
| 323 | item = deque_pop(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 324 | assert (item != NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 325 | Py_DECREF(item); |
| 326 | } |
| 327 | assert(deque->leftblock == deque->rightblock && |
| 328 | deque->leftindex > deque->rightindex); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static PyObject * |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 333 | deque_item(dequeobject *deque, int i) |
| 334 | { |
| 335 | block *b; |
| 336 | PyObject *item; |
| 337 | int n; |
| 338 | |
| 339 | if (i < 0 || i >= deque->len) { |
| 340 | PyErr_SetString(PyExc_IndexError, |
| 341 | "deque index out of range"); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
Raymond Hettinger | 6c79a51 | 2004-03-04 08:00:54 +0000 | [diff] [blame] | 345 | if (i == 0) { |
| 346 | i = deque->leftindex; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 347 | b = deque->leftblock; |
Raymond Hettinger | 6c79a51 | 2004-03-04 08:00:54 +0000 | [diff] [blame] | 348 | } else if (i == deque->len - 1) { |
| 349 | i = deque->rightindex; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 350 | b = deque->rightblock; |
Raymond Hettinger | 6c79a51 | 2004-03-04 08:00:54 +0000 | [diff] [blame] | 351 | } else { |
| 352 | i += deque->leftindex; |
| 353 | n = i / BLOCKLEN; |
| 354 | i %= BLOCKLEN; |
| 355 | if (i < (deque->len >> 1)) { |
| 356 | b = deque->leftblock; |
| 357 | while (n--) |
| 358 | b = b->rightlink; |
| 359 | } else { |
| 360 | n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n; |
| 361 | b = deque->rightblock; |
| 362 | while (n--) |
| 363 | b = b->leftlink; |
| 364 | } |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 365 | } |
| 366 | item = b->data[i]; |
| 367 | Py_INCREF(item); |
| 368 | return item; |
| 369 | } |
| 370 | |
Raymond Hettinger | 616f4f6 | 2004-06-26 04:42:06 +0000 | [diff] [blame] | 371 | /* delitem() implemented in terms of rotate for simplicity and reasonable |
| 372 | performance near the end points. If for some reason this method becomes |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 373 | 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] | 374 | (similar to code in list slice assignment) and achieve a two or threefold |
| 375 | performance boost. |
| 376 | */ |
| 377 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 378 | static int |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 379 | deque_del_item(dequeobject *deque, int i) |
| 380 | { |
| 381 | PyObject *item=NULL, *minus_i=NULL, *plus_i=NULL; |
| 382 | int rv = -1; |
| 383 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 384 | assert (i >= 0 && i < deque->len); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 385 | |
| 386 | minus_i = Py_BuildValue("(i)", -i); |
| 387 | if (minus_i == NULL) |
| 388 | goto fail; |
| 389 | |
| 390 | plus_i = Py_BuildValue("(i)", i); |
| 391 | if (plus_i == NULL) |
| 392 | goto fail; |
| 393 | |
| 394 | item = deque_rotate(deque, minus_i); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 395 | if (item == NULL) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 396 | goto fail; |
| 397 | Py_DECREF(item); |
| 398 | |
| 399 | item = deque_popleft(deque, NULL); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 400 | assert (item != NULL); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 401 | Py_DECREF(item); |
| 402 | |
| 403 | item = deque_rotate(deque, plus_i); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 404 | if (item == NULL) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 405 | goto fail; |
| 406 | |
| 407 | rv = 0; |
| 408 | fail: |
| 409 | Py_XDECREF(item); |
| 410 | Py_XDECREF(minus_i); |
| 411 | Py_XDECREF(plus_i); |
| 412 | return rv; |
| 413 | } |
| 414 | |
| 415 | static int |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 416 | deque_ass_item(dequeobject *deque, int i, PyObject *v) |
| 417 | { |
| 418 | PyObject *old_value; |
| 419 | block *b; |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 420 | int n, len=deque->len, halflen=(len+1)>>1, index=i; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 421 | |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 422 | if (i < 0 || i >= len) { |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 423 | PyErr_SetString(PyExc_IndexError, |
| 424 | "deque index out of range"); |
| 425 | return -1; |
| 426 | } |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 427 | if (v == NULL) |
| 428 | return deque_del_item(deque, i); |
| 429 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 430 | i += deque->leftindex; |
| 431 | n = i / BLOCKLEN; |
| 432 | i %= BLOCKLEN; |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 433 | if (index <= halflen) { |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 434 | b = deque->leftblock; |
| 435 | while (n--) |
| 436 | b = b->rightlink; |
| 437 | } else { |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 438 | n = (deque->leftindex + len - 1) / BLOCKLEN - n; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 439 | b = deque->rightblock; |
| 440 | while (n--) |
| 441 | b = b->leftlink; |
| 442 | } |
| 443 | Py_INCREF(v); |
| 444 | old_value = b->data[i]; |
| 445 | b->data[i] = v; |
| 446 | Py_DECREF(old_value); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static PyObject * |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 451 | deque_clearmethod(dequeobject *deque) |
| 452 | { |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 453 | int rv; |
| 454 | |
| 455 | rv = deque_clear(deque); |
| 456 | assert (rv != -1); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 457 | Py_RETURN_NONE; |
| 458 | } |
| 459 | |
| 460 | PyDoc_STRVAR(clear_doc, "Remove all elements from the deque."); |
| 461 | |
| 462 | static void |
| 463 | deque_dealloc(dequeobject *deque) |
| 464 | { |
| 465 | PyObject_GC_UnTrack(deque); |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 466 | if (deque->weakreflist != NULL) |
| 467 | PyObject_ClearWeakRefs((PyObject *) deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 468 | if (deque->leftblock != NULL) { |
Raymond Hettinger | e9c89e8 | 2004-07-19 00:10:24 +0000 | [diff] [blame] | 469 | deque_clear(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 470 | assert(deque->leftblock != NULL); |
| 471 | PyMem_Free(deque->leftblock); |
| 472 | } |
| 473 | deque->leftblock = NULL; |
| 474 | deque->rightblock = NULL; |
| 475 | deque->ob_type->tp_free(deque); |
| 476 | } |
| 477 | |
| 478 | static int |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 479 | deque_traverse(dequeobject *deque, visitproc visit, void *arg) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 480 | { |
| 481 | block * b = deque->leftblock; |
| 482 | int index = deque->leftindex; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 483 | PyObject *item; |
| 484 | |
| 485 | while (b != deque->rightblock || index <= deque->rightindex) { |
| 486 | item = b->data[index]; |
| 487 | index++; |
Raymond Hettinger | 67115a2 | 2004-07-15 21:32:18 +0000 | [diff] [blame] | 488 | if (index == BLOCKLEN ) { |
| 489 | assert(b->rightlink != NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 490 | b = b->rightlink; |
| 491 | index = 0; |
| 492 | } |
Raymond Hettinger | 67115a2 | 2004-07-15 21:32:18 +0000 | [diff] [blame] | 493 | Py_VISIT(item); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 494 | } |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static long |
| 499 | deque_nohash(PyObject *self) |
| 500 | { |
| 501 | PyErr_SetString(PyExc_TypeError, "deque objects are unhashable"); |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | static PyObject * |
| 506 | deque_copy(PyObject *deque) |
| 507 | { |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 508 | return PyObject_CallFunctionObjArgs((PyObject *)(deque->ob_type), |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 509 | deque, NULL); |
| 510 | } |
| 511 | |
| 512 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque."); |
| 513 | |
| 514 | static PyObject * |
| 515 | deque_reduce(dequeobject *deque) |
| 516 | { |
| 517 | PyObject *seq, *args, *result; |
| 518 | |
| 519 | seq = PySequence_Tuple((PyObject *)deque); |
| 520 | if (seq == NULL) |
| 521 | return NULL; |
| 522 | args = PyTuple_Pack(1, seq); |
| 523 | if (args == NULL) { |
| 524 | Py_DECREF(seq); |
| 525 | return NULL; |
| 526 | } |
| 527 | result = PyTuple_Pack(2, deque->ob_type, args); |
| 528 | Py_DECREF(seq); |
| 529 | Py_DECREF(args); |
| 530 | return result; |
| 531 | } |
| 532 | |
| 533 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 534 | |
| 535 | static PyObject * |
| 536 | deque_repr(PyObject *deque) |
| 537 | { |
| 538 | PyObject *aslist, *result, *fmt; |
| 539 | int i; |
| 540 | |
| 541 | i = Py_ReprEnter(deque); |
| 542 | if (i != 0) { |
| 543 | if (i < 0) |
| 544 | return NULL; |
| 545 | return PyString_FromString("[...]"); |
| 546 | } |
| 547 | |
| 548 | aslist = PySequence_List(deque); |
| 549 | if (aslist == NULL) { |
| 550 | Py_ReprLeave(deque); |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | fmt = PyString_FromString("deque(%r)"); |
| 555 | if (fmt == NULL) { |
| 556 | Py_DECREF(aslist); |
| 557 | Py_ReprLeave(deque); |
| 558 | return NULL; |
| 559 | } |
| 560 | result = PyString_Format(fmt, aslist); |
| 561 | Py_DECREF(fmt); |
| 562 | Py_DECREF(aslist); |
| 563 | Py_ReprLeave(deque); |
| 564 | return result; |
| 565 | } |
| 566 | |
| 567 | static int |
| 568 | deque_tp_print(PyObject *deque, FILE *fp, int flags) |
| 569 | { |
| 570 | PyObject *it, *item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 571 | char *emit = ""; /* No separator emitted on first pass */ |
| 572 | char *separator = ", "; |
| 573 | int i; |
| 574 | |
| 575 | i = Py_ReprEnter(deque); |
| 576 | if (i != 0) { |
| 577 | if (i < 0) |
| 578 | return i; |
| 579 | fputs("[...]", fp); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | it = PyObject_GetIter(deque); |
| 584 | if (it == NULL) |
| 585 | return -1; |
| 586 | |
| 587 | fputs("deque([", fp); |
| 588 | while ((item = PyIter_Next(it)) != NULL) { |
| 589 | fputs(emit, fp); |
| 590 | emit = separator; |
| 591 | if (PyObject_Print(item, fp, 0) != 0) { |
| 592 | Py_DECREF(item); |
| 593 | Py_DECREF(it); |
| 594 | Py_ReprLeave(deque); |
| 595 | return -1; |
| 596 | } |
| 597 | Py_DECREF(item); |
| 598 | } |
| 599 | Py_ReprLeave(deque); |
| 600 | Py_DECREF(it); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 601 | if (PyErr_Occurred()) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 602 | return -1; |
| 603 | fputs("])", fp); |
| 604 | return 0; |
| 605 | } |
| 606 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 607 | static PyObject * |
| 608 | deque_richcompare(PyObject *v, PyObject *w, int op) |
| 609 | { |
| 610 | PyObject *it1=NULL, *it2=NULL, *x, *y; |
| 611 | int i, b, vs, ws, minlen, cmp=-1; |
| 612 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 613 | if (!PyObject_TypeCheck(v, &deque_type) || |
Raymond Hettinger | 285cfcc | 2004-05-18 18:15:03 +0000 | [diff] [blame] | 614 | !PyObject_TypeCheck(w, &deque_type)) { |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 615 | Py_INCREF(Py_NotImplemented); |
| 616 | return Py_NotImplemented; |
| 617 | } |
| 618 | |
| 619 | /* Shortcuts */ |
| 620 | vs = ((dequeobject *)v)->len; |
| 621 | ws = ((dequeobject *)w)->len; |
| 622 | if (op == Py_EQ) { |
| 623 | if (v == w) |
| 624 | Py_RETURN_TRUE; |
| 625 | if (vs != ws) |
| 626 | Py_RETURN_FALSE; |
| 627 | } |
| 628 | if (op == Py_NE) { |
| 629 | if (v == w) |
| 630 | Py_RETURN_FALSE; |
| 631 | if (vs != ws) |
| 632 | Py_RETURN_TRUE; |
| 633 | } |
| 634 | |
| 635 | /* Search for the first index where items are different */ |
| 636 | it1 = PyObject_GetIter(v); |
| 637 | if (it1 == NULL) |
| 638 | goto done; |
| 639 | it2 = PyObject_GetIter(w); |
| 640 | if (it2 == NULL) |
| 641 | goto done; |
| 642 | minlen = (vs < ws) ? vs : ws; |
| 643 | for (i=0 ; i < minlen ; i++) { |
| 644 | x = PyIter_Next(it1); |
| 645 | if (x == NULL) |
| 646 | goto done; |
| 647 | y = PyIter_Next(it2); |
| 648 | if (y == NULL) { |
| 649 | Py_DECREF(x); |
| 650 | goto done; |
| 651 | } |
| 652 | b = PyObject_RichCompareBool(x, y, Py_EQ); |
| 653 | if (b == 0) { |
| 654 | cmp = PyObject_RichCompareBool(x, y, op); |
| 655 | Py_DECREF(x); |
| 656 | Py_DECREF(y); |
| 657 | goto done; |
| 658 | } |
| 659 | Py_DECREF(x); |
| 660 | Py_DECREF(y); |
| 661 | if (b == -1) |
| 662 | goto done; |
| 663 | } |
| 664 | /* Elements are equal through minlen. The longest input is the greatest */ |
| 665 | switch (op) { |
| 666 | case Py_LT: cmp = vs < ws; break; |
| 667 | case Py_LE: cmp = vs <= ws; break; |
| 668 | case Py_EQ: cmp = vs == ws; break; |
| 669 | case Py_NE: cmp = vs != ws; break; |
| 670 | case Py_GT: cmp = vs > ws; break; |
| 671 | case Py_GE: cmp = vs >= ws; break; |
| 672 | } |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 673 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 674 | done: |
| 675 | Py_XDECREF(it1); |
| 676 | Py_XDECREF(it2); |
| 677 | if (cmp == 1) |
| 678 | Py_RETURN_TRUE; |
| 679 | if (cmp == 0) |
| 680 | Py_RETURN_FALSE; |
| 681 | return NULL; |
| 682 | } |
| 683 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 684 | static int |
| 685 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwds) |
| 686 | { |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 687 | PyObject *iterable = NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 688 | |
| 689 | if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable)) |
| 690 | return -1; |
| 691 | |
| 692 | if (iterable != NULL) { |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 693 | PyObject *rv = deque_extend(deque, iterable); |
| 694 | if (rv == NULL) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 695 | return -1; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 696 | Py_DECREF(rv); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 697 | } |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static PySequenceMethods deque_as_sequence = { |
| 702 | (inquiry)deque_len, /* sq_length */ |
| 703 | 0, /* sq_concat */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 704 | 0, /* sq_repeat */ |
| 705 | (intargfunc)deque_item, /* sq_item */ |
| 706 | 0, /* sq_slice */ |
| 707 | (intobjargproc)deque_ass_item, /* sq_ass_item */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 708 | }; |
| 709 | |
| 710 | /* deque object ********************************************************/ |
| 711 | |
| 712 | static PyObject *deque_iter(dequeobject *deque); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 713 | static PyObject *deque_reviter(dequeobject *deque); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 714 | PyDoc_STRVAR(reversed_doc, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 715 | "D.__reversed__() -- return a reverse iterator over the deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 716 | |
| 717 | static PyMethodDef deque_methods[] = { |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 718 | {"append", (PyCFunction)deque_append, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 719 | METH_O, append_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 720 | {"appendleft", (PyCFunction)deque_appendleft, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 721 | METH_O, appendleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 722 | {"clear", (PyCFunction)deque_clearmethod, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 723 | METH_NOARGS, clear_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 724 | {"__copy__", (PyCFunction)deque_copy, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 725 | METH_NOARGS, copy_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 726 | {"extend", (PyCFunction)deque_extend, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 727 | METH_O, extend_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 728 | {"extendleft", (PyCFunction)deque_extendleft, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 729 | METH_O, extendleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 730 | {"pop", (PyCFunction)deque_pop, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 731 | METH_NOARGS, pop_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 732 | {"popleft", (PyCFunction)deque_popleft, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 733 | METH_NOARGS, popleft_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 734 | {"__reduce__", (PyCFunction)deque_reduce, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 735 | METH_NOARGS, reduce_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 736 | {"__reversed__", (PyCFunction)deque_reviter, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 737 | METH_NOARGS, reversed_doc}, |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 738 | {"rotate", (PyCFunction)deque_rotate, |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 739 | METH_VARARGS, rotate_doc}, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 740 | {NULL, NULL} /* sentinel */ |
| 741 | }; |
| 742 | |
| 743 | PyDoc_STRVAR(deque_doc, |
| 744 | "deque(iterable) --> deque object\n\ |
| 745 | \n\ |
| 746 | Build an ordered collection accessible from endpoints only."); |
| 747 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 748 | static PyTypeObject deque_type = { |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 749 | PyObject_HEAD_INIT(NULL) |
| 750 | 0, /* ob_size */ |
| 751 | "collections.deque", /* tp_name */ |
| 752 | sizeof(dequeobject), /* tp_basicsize */ |
| 753 | 0, /* tp_itemsize */ |
| 754 | /* methods */ |
| 755 | (destructor)deque_dealloc, /* tp_dealloc */ |
| 756 | (printfunc)deque_tp_print, /* tp_print */ |
| 757 | 0, /* tp_getattr */ |
| 758 | 0, /* tp_setattr */ |
| 759 | 0, /* tp_compare */ |
| 760 | (reprfunc)deque_repr, /* tp_repr */ |
| 761 | 0, /* tp_as_number */ |
| 762 | &deque_as_sequence, /* tp_as_sequence */ |
| 763 | 0, /* tp_as_mapping */ |
| 764 | deque_nohash, /* tp_hash */ |
| 765 | 0, /* tp_call */ |
| 766 | 0, /* tp_str */ |
| 767 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 768 | 0, /* tp_setattro */ |
| 769 | 0, /* tp_as_buffer */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 770 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 771 | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 772 | deque_doc, /* tp_doc */ |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 773 | (traverseproc)deque_traverse, /* tp_traverse */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 774 | (inquiry)deque_clear, /* tp_clear */ |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 775 | (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 776 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 777 | (getiterfunc)deque_iter, /* tp_iter */ |
| 778 | 0, /* tp_iternext */ |
| 779 | deque_methods, /* tp_methods */ |
| 780 | 0, /* tp_members */ |
| 781 | 0, /* tp_getset */ |
| 782 | 0, /* tp_base */ |
| 783 | 0, /* tp_dict */ |
| 784 | 0, /* tp_descr_get */ |
| 785 | 0, /* tp_descr_set */ |
| 786 | 0, /* tp_dictoffset */ |
| 787 | (initproc)deque_init, /* tp_init */ |
| 788 | PyType_GenericAlloc, /* tp_alloc */ |
| 789 | deque_new, /* tp_new */ |
| 790 | PyObject_GC_Del, /* tp_free */ |
| 791 | }; |
| 792 | |
| 793 | /*********************** Deque Iterator **************************/ |
| 794 | |
| 795 | typedef struct { |
| 796 | PyObject_HEAD |
| 797 | int index; |
| 798 | block *b; |
| 799 | dequeobject *deque; |
| 800 | int len; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 801 | int counter; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 802 | } dequeiterobject; |
| 803 | |
| 804 | PyTypeObject dequeiter_type; |
| 805 | |
| 806 | static PyObject * |
| 807 | deque_iter(dequeobject *deque) |
| 808 | { |
| 809 | dequeiterobject *it; |
| 810 | |
| 811 | it = PyObject_New(dequeiterobject, &dequeiter_type); |
| 812 | if (it == NULL) |
| 813 | return NULL; |
| 814 | it->b = deque->leftblock; |
| 815 | it->index = deque->leftindex; |
| 816 | Py_INCREF(deque); |
| 817 | it->deque = deque; |
| 818 | it->len = deque->len; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 819 | it->counter = deque->len; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 820 | return (PyObject *)it; |
| 821 | } |
| 822 | |
| 823 | static void |
| 824 | dequeiter_dealloc(dequeiterobject *dio) |
| 825 | { |
| 826 | Py_XDECREF(dio->deque); |
| 827 | dio->ob_type->tp_free(dio); |
| 828 | } |
| 829 | |
| 830 | static PyObject * |
| 831 | dequeiter_next(dequeiterobject *it) |
| 832 | { |
| 833 | PyObject *item; |
| 834 | if (it->b == it->deque->rightblock && it->index > it->deque->rightindex) |
| 835 | return NULL; |
| 836 | |
| 837 | if (it->len != it->deque->len) { |
| 838 | it->len = -1; /* Make this state sticky */ |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 839 | it->counter = 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 840 | PyErr_SetString(PyExc_RuntimeError, |
| 841 | "deque changed size during iteration"); |
| 842 | return NULL; |
| 843 | } |
| 844 | |
| 845 | item = it->b->data[it->index]; |
| 846 | it->index++; |
| 847 | if (it->index == BLOCKLEN && it->b->rightlink != NULL) { |
| 848 | it->b = it->b->rightlink; |
| 849 | it->index = 0; |
| 850 | } |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 851 | it->counter--; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 852 | Py_INCREF(item); |
| 853 | return item; |
| 854 | } |
| 855 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 856 | static int |
| 857 | dequeiter_len(dequeiterobject *it) |
| 858 | { |
| 859 | return it->counter; |
| 860 | } |
| 861 | |
| 862 | static PySequenceMethods dequeiter_as_sequence = { |
| 863 | (inquiry)dequeiter_len, /* sq_length */ |
| 864 | 0, /* sq_concat */ |
| 865 | }; |
| 866 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 867 | PyTypeObject dequeiter_type = { |
| 868 | PyObject_HEAD_INIT(NULL) |
| 869 | 0, /* ob_size */ |
| 870 | "deque_iterator", /* tp_name */ |
| 871 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 872 | 0, /* tp_itemsize */ |
| 873 | /* methods */ |
| 874 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 875 | 0, /* tp_print */ |
| 876 | 0, /* tp_getattr */ |
| 877 | 0, /* tp_setattr */ |
| 878 | 0, /* tp_compare */ |
| 879 | 0, /* tp_repr */ |
| 880 | 0, /* tp_as_number */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 881 | &dequeiter_as_sequence, /* tp_as_sequence */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 882 | 0, /* tp_as_mapping */ |
| 883 | 0, /* tp_hash */ |
| 884 | 0, /* tp_call */ |
| 885 | 0, /* tp_str */ |
| 886 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 887 | 0, /* tp_setattro */ |
| 888 | 0, /* tp_as_buffer */ |
| 889 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 890 | 0, /* tp_doc */ |
| 891 | 0, /* tp_traverse */ |
| 892 | 0, /* tp_clear */ |
| 893 | 0, /* tp_richcompare */ |
| 894 | 0, /* tp_weaklistoffset */ |
| 895 | PyObject_SelfIter, /* tp_iter */ |
| 896 | (iternextfunc)dequeiter_next, /* tp_iternext */ |
| 897 | 0, |
| 898 | }; |
| 899 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 900 | /*********************** Deque Reverse Iterator **************************/ |
| 901 | |
| 902 | PyTypeObject dequereviter_type; |
| 903 | |
| 904 | static PyObject * |
| 905 | deque_reviter(dequeobject *deque) |
| 906 | { |
| 907 | dequeiterobject *it; |
| 908 | |
| 909 | it = PyObject_New(dequeiterobject, &dequereviter_type); |
| 910 | if (it == NULL) |
| 911 | return NULL; |
| 912 | it->b = deque->rightblock; |
| 913 | it->index = deque->rightindex; |
| 914 | Py_INCREF(deque); |
| 915 | it->deque = deque; |
| 916 | it->len = deque->len; |
| 917 | it->counter = deque->len; |
| 918 | return (PyObject *)it; |
| 919 | } |
| 920 | |
| 921 | static PyObject * |
| 922 | dequereviter_next(dequeiterobject *it) |
| 923 | { |
| 924 | PyObject *item; |
| 925 | if (it->b == it->deque->leftblock && it->index < it->deque->leftindex) |
| 926 | return NULL; |
| 927 | |
| 928 | if (it->len != it->deque->len) { |
| 929 | it->len = -1; /* Make this state sticky */ |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 930 | it->counter = 0; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 931 | PyErr_SetString(PyExc_RuntimeError, |
| 932 | "deque changed size during iteration"); |
| 933 | return NULL; |
| 934 | } |
| 935 | |
| 936 | item = it->b->data[it->index]; |
| 937 | it->index--; |
| 938 | if (it->index == -1 && it->b->leftlink != NULL) { |
| 939 | it->b = it->b->leftlink; |
| 940 | it->index = BLOCKLEN - 1; |
| 941 | } |
| 942 | it->counter--; |
| 943 | Py_INCREF(item); |
| 944 | return item; |
| 945 | } |
| 946 | |
| 947 | PyTypeObject dequereviter_type = { |
| 948 | PyObject_HEAD_INIT(NULL) |
| 949 | 0, /* ob_size */ |
| 950 | "deque_reverse_iterator", /* tp_name */ |
| 951 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 952 | 0, /* tp_itemsize */ |
| 953 | /* methods */ |
| 954 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 955 | 0, /* tp_print */ |
| 956 | 0, /* tp_getattr */ |
| 957 | 0, /* tp_setattr */ |
| 958 | 0, /* tp_compare */ |
| 959 | 0, /* tp_repr */ |
| 960 | 0, /* tp_as_number */ |
| 961 | &dequeiter_as_sequence, /* tp_as_sequence */ |
| 962 | 0, /* tp_as_mapping */ |
| 963 | 0, /* tp_hash */ |
| 964 | 0, /* tp_call */ |
| 965 | 0, /* tp_str */ |
| 966 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 967 | 0, /* tp_setattro */ |
| 968 | 0, /* tp_as_buffer */ |
| 969 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 970 | 0, /* tp_doc */ |
| 971 | 0, /* tp_traverse */ |
| 972 | 0, /* tp_clear */ |
| 973 | 0, /* tp_richcompare */ |
| 974 | 0, /* tp_weaklistoffset */ |
| 975 | PyObject_SelfIter, /* tp_iter */ |
| 976 | (iternextfunc)dequereviter_next, /* tp_iternext */ |
| 977 | 0, |
| 978 | }; |
| 979 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 980 | /* module level code ********************************************************/ |
| 981 | |
| 982 | PyDoc_STRVAR(module_doc, |
| 983 | "High performance data structures\n\ |
| 984 | "); |
| 985 | |
| 986 | PyMODINIT_FUNC |
| 987 | initcollections(void) |
| 988 | { |
| 989 | PyObject *m; |
| 990 | |
| 991 | m = Py_InitModule3("collections", NULL, module_doc); |
| 992 | |
| 993 | if (PyType_Ready(&deque_type) < 0) |
| 994 | return; |
| 995 | Py_INCREF(&deque_type); |
| 996 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
| 997 | |
| 998 | if (PyType_Ready(&dequeiter_type) < 0) |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 999 | return; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1000 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1001 | if (PyType_Ready(&dequereviter_type) < 0) |
| 1002 | return; |
| 1003 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1004 | return; |
| 1005 | } |