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 | |
Raymond Hettinger | c208308 | 2015-02-28 23:29:16 -0800 | [diff] [blame] | 4 | #ifdef STDC_HEADERS |
| 5 | #include <stddef.h> |
| 6 | #else |
| 7 | #include <sys/types.h> /* For size_t */ |
| 8 | #endif |
| 9 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 10 | /* collections module implementation of a deque() datatype |
| 11 | Written and maintained by Raymond D. Hettinger <python@rcn.com> |
Raymond Hettinger | eb6b554 | 2015-02-10 22:37:22 -0600 | [diff] [blame] | 12 | Copyright (c) 2004-2015 Python Software Foundation. |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 13 | All rights reserved. |
| 14 | */ |
| 15 | |
Raymond Hettinger | 77e8bf1 | 2004-10-01 15:25:53 +0000 | [diff] [blame] | 16 | /* The block length may be set to any number over 1. Larger numbers |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 17 | * reduce the number of calls to the memory allocator, give faster |
Raymond Hettinger | 30c9074 | 2015-03-02 22:31:35 -0800 | [diff] [blame] | 18 | * indexing and rotation, and reduce the link to data overhead ratio. |
Raymond Hettinger | daf57f2 | 2015-02-26 23:21:29 -0800 | [diff] [blame] | 19 | * Making the block length a power of two speeds-up the modulo |
Raymond Hettinger | 30c9074 | 2015-03-02 22:31:35 -0800 | [diff] [blame] | 20 | * and division calculations in deque_item() and deque_ass_item(). |
Raymond Hettinger | 77e8bf1 | 2004-10-01 15:25:53 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Raymond Hettinger | daf57f2 | 2015-02-26 23:21:29 -0800 | [diff] [blame] | 23 | #define BLOCKLEN 64 |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 24 | #define CENTER ((BLOCKLEN - 1) / 2) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 25 | |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 26 | /* A `dequeobject` is composed of a doubly-linked list of `block` nodes. |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 27 | * The list of blocks is never empty, so d.leftblock and d.rightblock |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 28 | * are never equal to NULL. The list is not circular. |
| 29 | * |
| 30 | * A deque d's first element is at d.leftblock[leftindex] |
| 31 | * and its last element is at d.rightblock[rightindex]. |
| 32 | * Unlike Python slice indices, these indices are inclusive |
| 33 | * on both ends. This makes the algorithms for left and |
| 34 | * right operations more symmetrical and simplifies the design. |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 35 | * |
| 36 | * The indices, d.leftindex and d.rightindex are always in the range |
| 37 | * 0 <= index < BLOCKLEN. |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 38 | * Their exact relationship is: |
| 39 | * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex. |
Raymond Hettinger | 61f05fb | 2004-10-01 06:24:12 +0000 | [diff] [blame] | 40 | * |
| 41 | * Empty deques have d.len == 0; d.leftblock==d.rightblock; |
| 42 | * d.leftindex == CENTER+1; and d.rightindex == CENTER. |
| 43 | * Checking for d.len == 0 is the intended way to see whether d is empty. |
| 44 | * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 45 | * Whenever d.leftblock == d.rightblock, |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 46 | * d.leftindex + d.len - 1 == d.rightindex. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 47 | * |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 48 | * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | * become indices into distinct blocks and either may be larger than the |
Raymond Hettinger | 4ca4c7c | 2004-10-01 15:14:39 +0000 | [diff] [blame] | 50 | * other. |
Tim Peters | d8768d3 | 2004-10-01 01:32:53 +0000 | [diff] [blame] | 51 | */ |
| 52 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 53 | typedef struct BLOCK { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | struct BLOCK *leftlink; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | PyObject *data[BLOCKLEN]; |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 56 | struct BLOCK *rightlink; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 57 | } block; |
| 58 | |
Raymond Hettinger | 30c9074 | 2015-03-02 22:31:35 -0800 | [diff] [blame] | 59 | typedef struct { |
| 60 | PyObject_VAR_HEAD |
| 61 | block *leftblock; |
| 62 | block *rightblock; |
| 63 | Py_ssize_t leftindex; /* in range(BLOCKLEN) */ |
| 64 | Py_ssize_t rightindex; /* in range(BLOCKLEN) */ |
Raymond Hettinger | f9d9c79 | 2015-03-02 22:47:46 -0800 | [diff] [blame] | 65 | size_t state; /* incremented whenever the indices move */ |
Raymond Hettinger | 30c9074 | 2015-03-02 22:31:35 -0800 | [diff] [blame] | 66 | Py_ssize_t maxlen; |
Raymond Hettinger | 0f6f947 | 2015-03-21 01:42:10 -0700 | [diff] [blame] | 67 | PyObject *weakreflist; |
Raymond Hettinger | 30c9074 | 2015-03-02 22:31:35 -0800 | [diff] [blame] | 68 | } dequeobject; |
| 69 | |
| 70 | static PyTypeObject deque_type; |
| 71 | |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 72 | /* For debug builds, add error checking to track the endpoints |
| 73 | * in the chain of links. The goal is to make sure that link |
| 74 | * assignments only take place at endpoints so that links already |
| 75 | * in use do not get overwritten. |
| 76 | * |
| 77 | * CHECK_END should happen before each assignment to a block's link field. |
| 78 | * MARK_END should happen whenever a link field becomes a new endpoint. |
| 79 | * This happens when new blocks are added or whenever an existing |
| 80 | * block is freed leaving another existing block as the new endpoint. |
| 81 | */ |
| 82 | |
Raymond Hettinger | 3223dd5 | 2013-07-26 23:14:22 -0700 | [diff] [blame] | 83 | #ifndef NDEBUG |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 84 | #define MARK_END(link) link = NULL; |
| 85 | #define CHECK_END(link) assert(link == NULL); |
| 86 | #define CHECK_NOT_END(link) assert(link != NULL); |
| 87 | #else |
| 88 | #define MARK_END(link) |
| 89 | #define CHECK_END(link) |
| 90 | #define CHECK_NOT_END(link) |
| 91 | #endif |
| 92 | |
| 93 | /* A simple freelisting scheme is used to minimize calls to the memory |
| 94 | allocator. It accomodates common use cases where new blocks are being |
| 95 | added at about the same rate as old blocks are being freed. |
| 96 | */ |
| 97 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 98 | #define MAXFREEBLOCKS 10 |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 99 | static Py_ssize_t numfreeblocks = 0; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 100 | static block *freeblocks[MAXFREEBLOCKS]; |
| 101 | |
Tim Peters | 6f85356 | 2004-10-01 01:04:50 +0000 | [diff] [blame] | 102 | static block * |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 103 | newblock(Py_ssize_t len) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | block *b; |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 105 | /* To prevent len from overflowing PY_SSIZE_T_MAX, we refuse to |
| 106 | * allocate new blocks if the current len is nearing overflow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) { |
| 108 | PyErr_SetString(PyExc_OverflowError, |
| 109 | "cannot add more blocks to the deque"); |
| 110 | return NULL; |
| 111 | } |
| 112 | if (numfreeblocks) { |
Raymond Hettinger | 59cf23a | 2013-02-07 00:57:19 -0500 | [diff] [blame] | 113 | numfreeblocks--; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 114 | return freeblocks[numfreeblocks]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | } |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 116 | b = PyMem_Malloc(sizeof(block)); |
| 117 | if (b != NULL) { |
| 118 | return b; |
| 119 | } |
| 120 | PyErr_NoMemory(); |
| 121 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 124 | static void |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 125 | freeblock(block *b) |
| 126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | if (numfreeblocks < MAXFREEBLOCKS) { |
| 128 | freeblocks[numfreeblocks] = b; |
| 129 | numfreeblocks++; |
| 130 | } else { |
| 131 | PyMem_Free(b); |
| 132 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Raymond Hettinger | da2850f | 2015-02-27 12:42:54 -0800 | [diff] [blame] | 135 | /* XXX Todo: |
Raymond Hettinger | 5402315 | 2014-04-23 00:58:48 -0700 | [diff] [blame] | 136 | If aligned memory allocations become available, make the |
| 137 | deque object 64 byte aligned so that all of the fields |
| 138 | can be retrieved or updated in a single cache line. |
| 139 | */ |
| 140 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 141 | static PyObject * |
| 142 | deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | dequeobject *deque; |
| 145 | block *b; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | /* create dequeobject structure */ |
| 148 | deque = (dequeobject *)type->tp_alloc(type, 0); |
| 149 | if (deque == NULL) |
| 150 | return NULL; |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 151 | |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 152 | b = newblock(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | if (b == NULL) { |
| 154 | Py_DECREF(deque); |
| 155 | return NULL; |
| 156 | } |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 157 | MARK_END(b->leftlink); |
| 158 | MARK_END(b->rightlink); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | assert(BLOCKLEN >= 2); |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 161 | Py_SIZE(deque) = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | deque->leftblock = b; |
| 163 | deque->rightblock = b; |
| 164 | deque->leftindex = CENTER + 1; |
| 165 | deque->rightindex = CENTER; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | deque->state = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | deque->maxlen = -1; |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 168 | deque->weakreflist = NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | return (PyObject *)deque; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static PyObject * |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 174 | deque_pop(dequeobject *deque, PyObject *unused) |
| 175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | PyObject *item; |
| 177 | block *prevblock; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 178 | |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 179 | if (Py_SIZE(deque) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
| 181 | return NULL; |
| 182 | } |
| 183 | item = deque->rightblock->data[deque->rightindex]; |
| 184 | deque->rightindex--; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 185 | Py_SIZE(deque)--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | deque->state++; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | if (deque->rightindex == -1) { |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 189 | if (Py_SIZE(deque) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | assert(deque->leftblock == deque->rightblock); |
| 191 | assert(deque->leftindex == deque->rightindex+1); |
| 192 | /* re-center instead of freeing a block */ |
| 193 | deque->leftindex = CENTER + 1; |
| 194 | deque->rightindex = CENTER; |
| 195 | } else { |
| 196 | prevblock = deque->rightblock->leftlink; |
| 197 | assert(deque->leftblock != deque->rightblock); |
| 198 | freeblock(deque->rightblock); |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 199 | CHECK_NOT_END(prevblock); |
| 200 | MARK_END(prevblock->rightlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | deque->rightblock = prevblock; |
| 202 | deque->rightindex = BLOCKLEN - 1; |
| 203 | } |
| 204 | } |
| 205 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element."); |
| 209 | |
| 210 | static PyObject * |
| 211 | deque_popleft(dequeobject *deque, PyObject *unused) |
| 212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | PyObject *item; |
| 214 | block *prevblock; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 215 | |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 216 | if (Py_SIZE(deque) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
| 218 | return NULL; |
| 219 | } |
| 220 | assert(deque->leftblock != NULL); |
| 221 | item = deque->leftblock->data[deque->leftindex]; |
| 222 | deque->leftindex++; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 223 | Py_SIZE(deque)--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | deque->state++; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | if (deque->leftindex == BLOCKLEN) { |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 227 | if (Py_SIZE(deque) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | assert(deque->leftblock == deque->rightblock); |
| 229 | assert(deque->leftindex == deque->rightindex+1); |
| 230 | /* re-center instead of freeing a block */ |
| 231 | deque->leftindex = CENTER + 1; |
| 232 | deque->rightindex = CENTER; |
| 233 | } else { |
| 234 | assert(deque->leftblock != deque->rightblock); |
| 235 | prevblock = deque->leftblock->rightlink; |
| 236 | freeblock(deque->leftblock); |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 237 | CHECK_NOT_END(prevblock); |
| 238 | MARK_END(prevblock->leftlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | deque->leftblock = prevblock; |
| 240 | deque->leftindex = 0; |
| 241 | } |
| 242 | } |
| 243 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element."); |
| 247 | |
Raymond Hettinger | f30f5b9 | 2015-03-02 22:23:37 -0800 | [diff] [blame] | 248 | /* The deque's size limit is d.maxlen. The limit can be zero or positive. |
| 249 | * If there is no limit, then d.maxlen == -1. |
| 250 | * |
| 251 | * After an item is added to a deque, we check to see if the size has grown past |
| 252 | * the limit. If it has, we get the size back down to the limit by popping an |
| 253 | * item off of the opposite end. The methods that can trigger this are append(), |
| 254 | * appendleft(), extend(), and extendleft(). |
| 255 | */ |
| 256 | |
| 257 | static void |
| 258 | deque_trim_right(dequeobject *deque) |
| 259 | { |
| 260 | if (deque->maxlen != -1 && Py_SIZE(deque) > deque->maxlen) { |
| 261 | PyObject *rv = deque_pop(deque, NULL); |
| 262 | assert(rv != NULL); |
| 263 | assert(Py_SIZE(deque) <= deque->maxlen); |
| 264 | Py_DECREF(rv); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void |
| 269 | deque_trim_left(dequeobject *deque) |
| 270 | { |
| 271 | if (deque->maxlen != -1 && Py_SIZE(deque) > deque->maxlen) { |
| 272 | PyObject *rv = deque_popleft(deque, NULL); |
| 273 | assert(rv != NULL); |
| 274 | assert(Py_SIZE(deque) <= deque->maxlen); |
| 275 | Py_DECREF(rv); |
| 276 | } |
| 277 | } |
| 278 | |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 279 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 280 | deque_append(dequeobject *deque, PyObject *item) |
| 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | deque->state++; |
| 283 | if (deque->rightindex == BLOCKLEN-1) { |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 284 | block *b = newblock(Py_SIZE(deque)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | if (b == NULL) |
| 286 | return NULL; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 287 | b->leftlink = deque->rightblock; |
| 288 | CHECK_END(deque->rightblock->rightlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | deque->rightblock->rightlink = b; |
| 290 | deque->rightblock = b; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 291 | MARK_END(b->rightlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | deque->rightindex = -1; |
| 293 | } |
| 294 | Py_INCREF(item); |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 295 | Py_SIZE(deque)++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | deque->rightindex++; |
| 297 | deque->rightblock->data[deque->rightindex] = item; |
Raymond Hettinger | f30f5b9 | 2015-03-02 22:23:37 -0800 | [diff] [blame] | 298 | deque_trim_left(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | Py_RETURN_NONE; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque."); |
| 303 | |
| 304 | static PyObject * |
| 305 | deque_appendleft(dequeobject *deque, PyObject *item) |
| 306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | deque->state++; |
| 308 | if (deque->leftindex == 0) { |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 309 | block *b = newblock(Py_SIZE(deque)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | if (b == NULL) |
| 311 | return NULL; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 312 | b->rightlink = deque->leftblock; |
| 313 | CHECK_END(deque->leftblock->leftlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | deque->leftblock->leftlink = b; |
| 315 | deque->leftblock = b; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 316 | MARK_END(b->leftlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | deque->leftindex = BLOCKLEN; |
| 318 | } |
| 319 | Py_INCREF(item); |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 320 | Py_SIZE(deque)++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | deque->leftindex--; |
| 322 | deque->leftblock->data[deque->leftindex] = item; |
Raymond Hettinger | f30f5b9 | 2015-03-02 22:23:37 -0800 | [diff] [blame] | 323 | deque_trim_right(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | Py_RETURN_NONE; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque."); |
| 328 | |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | /* Run an iterator to exhaustion. Shortcut for |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 331 | the extend/extendleft methods when maxlen == 0. */ |
| 332 | static PyObject* |
| 333 | consume_iterator(PyObject *it) |
| 334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyObject *item; |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | while ((item = PyIter_Next(it)) != NULL) { |
| 338 | Py_DECREF(item); |
| 339 | } |
| 340 | Py_DECREF(it); |
| 341 | if (PyErr_Occurred()) |
| 342 | return NULL; |
| 343 | Py_RETURN_NONE; |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 346 | static PyObject * |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 347 | deque_extend(dequeobject *deque, PyObject *iterable) |
| 348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | PyObject *it, *item; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | /* Handle case where id(deque) == id(iterable) */ |
| 352 | if ((PyObject *)deque == iterable) { |
| 353 | PyObject *result; |
| 354 | PyObject *s = PySequence_List(iterable); |
| 355 | if (s == NULL) |
| 356 | return NULL; |
| 357 | result = deque_extend(deque, s); |
| 358 | Py_DECREF(s); |
| 359 | return result; |
| 360 | } |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 361 | |
Raymond Hettinger | d9c116c | 2013-07-09 00:13:21 -0700 | [diff] [blame] | 362 | /* Space saving heuristic. Start filling from the left */ |
| 363 | if (Py_SIZE(deque) == 0) { |
| 364 | assert(deque->leftblock == deque->rightblock); |
| 365 | assert(deque->leftindex == deque->rightindex+1); |
| 366 | deque->leftindex = 1; |
| 367 | deque->rightindex = 0; |
| 368 | } |
| 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | it = PyObject_GetIter(iterable); |
| 371 | if (it == NULL) |
| 372 | return NULL; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | if (deque->maxlen == 0) |
| 375 | return consume_iterator(it); |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | while ((item = PyIter_Next(it)) != NULL) { |
| 378 | deque->state++; |
| 379 | if (deque->rightindex == BLOCKLEN-1) { |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 380 | block *b = newblock(Py_SIZE(deque)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | if (b == NULL) { |
| 382 | Py_DECREF(item); |
| 383 | Py_DECREF(it); |
| 384 | return NULL; |
| 385 | } |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 386 | b->leftlink = deque->rightblock; |
| 387 | CHECK_END(deque->rightblock->rightlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | deque->rightblock->rightlink = b; |
| 389 | deque->rightblock = b; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 390 | MARK_END(b->rightlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | deque->rightindex = -1; |
| 392 | } |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 393 | Py_SIZE(deque)++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | deque->rightindex++; |
| 395 | deque->rightblock->data[deque->rightindex] = item; |
Raymond Hettinger | f30f5b9 | 2015-03-02 22:23:37 -0800 | [diff] [blame] | 396 | deque_trim_left(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | } |
| 398 | Py_DECREF(it); |
| 399 | if (PyErr_Occurred()) |
| 400 | return NULL; |
| 401 | Py_RETURN_NONE; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 404 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 405 | "Extend the right side of the deque with elements from the iterable"); |
| 406 | |
| 407 | static PyObject * |
| 408 | deque_extendleft(dequeobject *deque, PyObject *iterable) |
| 409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | PyObject *it, *item; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | /* Handle case where id(deque) == id(iterable) */ |
| 413 | if ((PyObject *)deque == iterable) { |
| 414 | PyObject *result; |
| 415 | PyObject *s = PySequence_List(iterable); |
| 416 | if (s == NULL) |
| 417 | return NULL; |
| 418 | result = deque_extendleft(deque, s); |
| 419 | Py_DECREF(s); |
| 420 | return result; |
| 421 | } |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 422 | |
Raymond Hettinger | d9c116c | 2013-07-09 00:13:21 -0700 | [diff] [blame] | 423 | /* Space saving heuristic. Start filling from the right */ |
| 424 | if (Py_SIZE(deque) == 0) { |
| 425 | assert(deque->leftblock == deque->rightblock); |
| 426 | assert(deque->leftindex == deque->rightindex+1); |
| 427 | deque->leftindex = BLOCKLEN - 1; |
| 428 | deque->rightindex = BLOCKLEN - 2; |
| 429 | } |
| 430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | it = PyObject_GetIter(iterable); |
| 432 | if (it == NULL) |
| 433 | return NULL; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | if (deque->maxlen == 0) |
| 436 | return consume_iterator(it); |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | while ((item = PyIter_Next(it)) != NULL) { |
| 439 | deque->state++; |
| 440 | if (deque->leftindex == 0) { |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 441 | block *b = newblock(Py_SIZE(deque)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | if (b == NULL) { |
| 443 | Py_DECREF(item); |
| 444 | Py_DECREF(it); |
| 445 | return NULL; |
| 446 | } |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 447 | b->rightlink = deque->leftblock; |
| 448 | CHECK_END(deque->leftblock->leftlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | deque->leftblock->leftlink = b; |
| 450 | deque->leftblock = b; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 451 | MARK_END(b->leftlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | deque->leftindex = BLOCKLEN; |
| 453 | } |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 454 | Py_SIZE(deque)++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | deque->leftindex--; |
| 456 | deque->leftblock->data[deque->leftindex] = item; |
Raymond Hettinger | f30f5b9 | 2015-03-02 22:23:37 -0800 | [diff] [blame] | 457 | deque_trim_right(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | } |
| 459 | Py_DECREF(it); |
| 460 | if (PyErr_Occurred()) |
| 461 | return NULL; |
| 462 | Py_RETURN_NONE; |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 465 | PyDoc_STRVAR(extendleft_doc, |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 466 | "Extend the left side of the deque with elements from the iterable"); |
| 467 | |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 468 | static PyObject * |
| 469 | deque_inplace_concat(dequeobject *deque, PyObject *other) |
| 470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | PyObject *result; |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | result = deque_extend(deque, other); |
| 474 | if (result == NULL) |
| 475 | return result; |
| 476 | Py_DECREF(result); |
| 477 | Py_INCREF(deque); |
| 478 | return (PyObject *)deque; |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Raymond Hettinger | 5402315 | 2014-04-23 00:58:48 -0700 | [diff] [blame] | 481 | /* The rotate() method is part of the public API and is used internally |
| 482 | as a primitive for other methods. |
| 483 | |
| 484 | Rotation by 1 or -1 is a common case, so any optimizations for high |
| 485 | volume rotations should take care not to penalize the common case. |
| 486 | |
| 487 | Conceptually, a rotate by one is equivalent to a pop on one side and an |
| 488 | append on the other. However, a pop/append pair is unnecessarily slow |
| 489 | because it requires a incref/decref pair for an object located randomly |
| 490 | in memory. It is better to just move the object pointer from one block |
| 491 | to the next without changing the reference count. |
| 492 | |
| 493 | When moving batches of pointers, it is tempting to use memcpy() but that |
| 494 | proved to be slower than a simple loop for a variety of reasons. |
| 495 | Memcpy() cannot know in advance that we're copying pointers instead of |
| 496 | bytes, that the source and destination are pointer aligned and |
| 497 | non-overlapping, that moving just one pointer is a common case, that we |
| 498 | never need to move more than BLOCKLEN pointers, and that at least one |
| 499 | pointer is always moved. |
| 500 | |
| 501 | For high volume rotations, newblock() and freeblock() are never called |
| 502 | more than once. Previously emptied blocks are immediately reused as a |
| 503 | destination block. If a block is left-over at the end, it is freed. |
| 504 | */ |
| 505 | |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 506 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 507 | _deque_rotate(dequeobject *deque, Py_ssize_t n) |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 508 | { |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 509 | block *b = NULL; |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 510 | block *leftblock = deque->leftblock; |
| 511 | block *rightblock = deque->rightblock; |
| 512 | Py_ssize_t leftindex = deque->leftindex; |
| 513 | Py_ssize_t rightindex = deque->rightindex; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 514 | Py_ssize_t len=Py_SIZE(deque), halflen=len>>1; |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 515 | int rv = -1; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 516 | |
Raymond Hettinger | 464d89b | 2013-01-11 22:29:50 -0800 | [diff] [blame] | 517 | if (len <= 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | return 0; |
| 519 | if (n > halflen || n < -halflen) { |
| 520 | n %= len; |
| 521 | if (n > halflen) |
| 522 | n -= len; |
| 523 | else if (n < -halflen) |
| 524 | n += len; |
| 525 | } |
Raymond Hettinger | 1f0044c | 2013-02-05 01:30:46 -0500 | [diff] [blame] | 526 | assert(len > 1); |
Raymond Hettinger | a4409c1 | 2013-02-04 00:08:12 -0500 | [diff] [blame] | 527 | assert(-halflen <= n && n <= halflen); |
Raymond Hettinger | 231ee4d | 2013-02-02 11:24:43 -0800 | [diff] [blame] | 528 | |
Raymond Hettinger | 464d89b | 2013-01-11 22:29:50 -0800 | [diff] [blame] | 529 | deque->state++; |
Raymond Hettinger | 1f0044c | 2013-02-05 01:30:46 -0500 | [diff] [blame] | 530 | while (n > 0) { |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 531 | if (leftindex == 0) { |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 532 | if (b == NULL) { |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 533 | b = newblock(len); |
| 534 | if (b == NULL) |
| 535 | goto done; |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 536 | } |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 537 | b->rightlink = leftblock; |
| 538 | CHECK_END(leftblock->leftlink); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 539 | leftblock->leftlink = b; |
| 540 | leftblock = b; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 541 | MARK_END(b->leftlink); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 542 | leftindex = BLOCKLEN; |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 543 | b = NULL; |
Raymond Hettinger | 464d89b | 2013-01-11 22:29:50 -0800 | [diff] [blame] | 544 | } |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 545 | assert(leftindex > 0); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 546 | { |
| 547 | PyObject **src, **dest; |
| 548 | Py_ssize_t m = n; |
Raymond Hettinger | 21777ac | 2013-02-02 09:56:08 -0800 | [diff] [blame] | 549 | |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 550 | if (m > rightindex + 1) |
| 551 | m = rightindex + 1; |
| 552 | if (m > leftindex) |
| 553 | m = leftindex; |
| 554 | assert (m > 0 && m <= len); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 555 | rightindex -= m; |
| 556 | leftindex -= m; |
Raymond Hettinger | 0e259f1 | 2015-02-01 22:53:41 -0800 | [diff] [blame] | 557 | src = &rightblock->data[rightindex + 1]; |
| 558 | dest = &leftblock->data[leftindex]; |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 559 | n -= m; |
Raymond Hettinger | 840533b | 2013-07-13 17:03:58 -0700 | [diff] [blame] | 560 | do { |
Raymond Hettinger | 0e259f1 | 2015-02-01 22:53:41 -0800 | [diff] [blame] | 561 | *(dest++) = *(src++); |
Raymond Hettinger | 840533b | 2013-07-13 17:03:58 -0700 | [diff] [blame] | 562 | } while (--m); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 563 | } |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 564 | if (rightindex == -1) { |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 565 | assert(leftblock != rightblock); |
Raymond Hettinger | 840533b | 2013-07-13 17:03:58 -0700 | [diff] [blame] | 566 | assert(b == NULL); |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 567 | b = rightblock; |
Raymond Hettinger | b97cc49 | 2013-07-21 01:51:07 -0700 | [diff] [blame] | 568 | CHECK_NOT_END(rightblock->leftlink); |
| 569 | rightblock = rightblock->leftlink; |
| 570 | MARK_END(rightblock->rightlink); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 571 | rightindex = BLOCKLEN - 1; |
Raymond Hettinger | 464d89b | 2013-01-11 22:29:50 -0800 | [diff] [blame] | 572 | } |
Raymond Hettinger | 21777ac | 2013-02-02 09:56:08 -0800 | [diff] [blame] | 573 | } |
Raymond Hettinger | 1f0044c | 2013-02-05 01:30:46 -0500 | [diff] [blame] | 574 | while (n < 0) { |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 575 | if (rightindex == BLOCKLEN - 1) { |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 576 | if (b == NULL) { |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 577 | b = newblock(len); |
| 578 | if (b == NULL) |
| 579 | goto done; |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 580 | } |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 581 | b->leftlink = rightblock; |
| 582 | CHECK_END(rightblock->rightlink); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 583 | rightblock->rightlink = b; |
| 584 | rightblock = b; |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 585 | MARK_END(b->rightlink); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 586 | rightindex = -1; |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 587 | b = NULL; |
Raymond Hettinger | 464d89b | 2013-01-11 22:29:50 -0800 | [diff] [blame] | 588 | } |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 589 | assert (rightindex < BLOCKLEN - 1); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 590 | { |
| 591 | PyObject **src, **dest; |
| 592 | Py_ssize_t m = -n; |
Raymond Hettinger | 21777ac | 2013-02-02 09:56:08 -0800 | [diff] [blame] | 593 | |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 594 | if (m > BLOCKLEN - leftindex) |
| 595 | m = BLOCKLEN - leftindex; |
| 596 | if (m > BLOCKLEN - 1 - rightindex) |
| 597 | m = BLOCKLEN - 1 - rightindex; |
| 598 | assert (m > 0 && m <= len); |
| 599 | src = &leftblock->data[leftindex]; |
| 600 | dest = &rightblock->data[rightindex + 1]; |
| 601 | leftindex += m; |
| 602 | rightindex += m; |
| 603 | n += m; |
Raymond Hettinger | 840533b | 2013-07-13 17:03:58 -0700 | [diff] [blame] | 604 | do { |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 605 | *(dest++) = *(src++); |
Raymond Hettinger | 840533b | 2013-07-13 17:03:58 -0700 | [diff] [blame] | 606 | } while (--m); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 607 | } |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 608 | if (leftindex == BLOCKLEN) { |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 609 | assert(leftblock != rightblock); |
Raymond Hettinger | 840533b | 2013-07-13 17:03:58 -0700 | [diff] [blame] | 610 | assert(b == NULL); |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 611 | b = leftblock; |
Raymond Hettinger | b97cc49 | 2013-07-21 01:51:07 -0700 | [diff] [blame] | 612 | CHECK_NOT_END(leftblock->rightlink); |
| 613 | leftblock = leftblock->rightlink; |
| 614 | MARK_END(leftblock->leftlink); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 615 | leftindex = 0; |
Raymond Hettinger | 21777ac | 2013-02-02 09:56:08 -0800 | [diff] [blame] | 616 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | } |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 618 | rv = 0; |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 619 | done: |
Raymond Hettinger | 3959af9 | 2013-07-13 02:34:08 -0700 | [diff] [blame] | 620 | if (b != NULL) |
| 621 | freeblock(b); |
Raymond Hettinger | 20b0f87 | 2013-06-23 15:44:33 -0700 | [diff] [blame] | 622 | deque->leftblock = leftblock; |
| 623 | deque->rightblock = rightblock; |
| 624 | deque->leftindex = leftindex; |
| 625 | deque->rightindex = rightindex; |
| 626 | |
| 627 | return rv; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static PyObject * |
| 631 | deque_rotate(dequeobject *deque, PyObject *args) |
| 632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | Py_ssize_t n=1; |
Raymond Hettinger | dcb9d94 | 2004-10-09 16:02:18 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | if (!PyArg_ParseTuple(args, "|n:rotate", &n)) |
| 636 | return NULL; |
Raymond Hettinger | 6921c13 | 2015-03-21 02:03:40 -0700 | [diff] [blame] | 637 | if (!_deque_rotate(deque, n)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | Py_RETURN_NONE; |
| 639 | return NULL; |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 642 | PyDoc_STRVAR(rotate_doc, |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 643 | "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] | 644 | |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 645 | static PyObject * |
| 646 | deque_reverse(dequeobject *deque, PyObject *unused) |
| 647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | block *leftblock = deque->leftblock; |
| 649 | block *rightblock = deque->rightblock; |
| 650 | Py_ssize_t leftindex = deque->leftindex; |
| 651 | Py_ssize_t rightindex = deque->rightindex; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 652 | Py_ssize_t n = (Py_SIZE(deque))/2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | Py_ssize_t i; |
| 654 | PyObject *tmp; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | for (i=0 ; i<n ; i++) { |
| 657 | /* Validate that pointers haven't met in the middle */ |
| 658 | assert(leftblock != rightblock || leftindex < rightindex); |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 659 | CHECK_NOT_END(leftblock); |
| 660 | CHECK_NOT_END(rightblock); |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | /* Swap */ |
| 663 | tmp = leftblock->data[leftindex]; |
| 664 | leftblock->data[leftindex] = rightblock->data[rightindex]; |
| 665 | rightblock->data[rightindex] = tmp; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | /* Advance left block/index pair */ |
| 668 | leftindex++; |
| 669 | if (leftindex == BLOCKLEN) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | leftblock = leftblock->rightlink; |
| 671 | leftindex = 0; |
| 672 | } |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | /* Step backwards with the right block/index pair */ |
| 675 | rightindex--; |
| 676 | if (rightindex == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | rightblock = rightblock->leftlink; |
| 678 | rightindex = BLOCKLEN - 1; |
| 679 | } |
| 680 | } |
| 681 | Py_RETURN_NONE; |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | PyDoc_STRVAR(reverse_doc, |
| 685 | "D.reverse() -- reverse *IN PLACE*"); |
| 686 | |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 687 | static PyObject * |
| 688 | deque_count(dequeobject *deque, PyObject *v) |
| 689 | { |
Raymond Hettinger | f3a67b7 | 2013-07-06 17:49:06 -1000 | [diff] [blame] | 690 | block *b = deque->leftblock; |
| 691 | Py_ssize_t index = deque->leftindex; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 692 | Py_ssize_t n = Py_SIZE(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | Py_ssize_t i; |
| 694 | Py_ssize_t count = 0; |
Raymond Hettinger | f9d9c79 | 2015-03-02 22:47:46 -0800 | [diff] [blame] | 695 | size_t start_state = deque->state; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | PyObject *item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | int cmp; |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | for (i=0 ; i<n ; i++) { |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 700 | CHECK_NOT_END(b); |
Raymond Hettinger | f3a67b7 | 2013-07-06 17:49:06 -1000 | [diff] [blame] | 701 | item = b->data[index]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | cmp = PyObject_RichCompareBool(item, v, Py_EQ); |
| 703 | if (cmp > 0) |
| 704 | count++; |
| 705 | else if (cmp < 0) |
| 706 | return NULL; |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | if (start_state != deque->state) { |
| 709 | PyErr_SetString(PyExc_RuntimeError, |
| 710 | "deque mutated during iteration"); |
| 711 | return NULL; |
| 712 | } |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | /* Advance left block/index pair */ |
Raymond Hettinger | f3a67b7 | 2013-07-06 17:49:06 -1000 | [diff] [blame] | 715 | index++; |
| 716 | if (index == BLOCKLEN) { |
| 717 | b = b->rightlink; |
| 718 | index = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | return PyLong_FromSsize_t(count); |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | PyDoc_STRVAR(count_doc, |
| 725 | "D.count(value) -> integer -- return number of occurrences of value"); |
| 726 | |
Raymond Hettinger | 39dadf7 | 2015-03-20 16:38:56 -0700 | [diff] [blame] | 727 | static int |
| 728 | deque_contains(dequeobject *deque, PyObject *v) |
| 729 | { |
| 730 | block *b = deque->leftblock; |
| 731 | Py_ssize_t index = deque->leftindex; |
| 732 | Py_ssize_t n = Py_SIZE(deque); |
| 733 | Py_ssize_t i; |
| 734 | size_t start_state = deque->state; |
| 735 | PyObject *item; |
| 736 | int cmp; |
| 737 | |
| 738 | for (i=0 ; i<n ; i++) { |
| 739 | CHECK_NOT_END(b); |
| 740 | item = b->data[index]; |
| 741 | cmp = PyObject_RichCompareBool(item, v, Py_EQ); |
| 742 | if (cmp) { |
| 743 | return cmp; |
| 744 | } |
| 745 | if (start_state != deque->state) { |
| 746 | PyErr_SetString(PyExc_RuntimeError, |
| 747 | "deque mutated during iteration"); |
| 748 | return -1; |
| 749 | } |
| 750 | index++; |
| 751 | if (index == BLOCKLEN) { |
| 752 | b = b->rightlink; |
| 753 | index = 0; |
| 754 | } |
| 755 | } |
| 756 | return 0; |
| 757 | } |
| 758 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 759 | static Py_ssize_t |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 760 | deque_len(dequeobject *deque) |
| 761 | { |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 762 | return Py_SIZE(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 765 | static PyObject * |
Raymond Hettinger | 32ea165 | 2015-03-21 01:37:37 -0700 | [diff] [blame] | 766 | deque_index(dequeobject *deque, PyObject *args) |
| 767 | { |
| 768 | Py_ssize_t i, start=0, stop=Py_SIZE(deque); |
| 769 | PyObject *v, *item; |
| 770 | block *b = deque->leftblock; |
| 771 | Py_ssize_t index = deque->leftindex; |
| 772 | size_t start_state = deque->state; |
| 773 | |
| 774 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, |
| 775 | _PyEval_SliceIndex, &start, |
| 776 | _PyEval_SliceIndex, &stop)) |
| 777 | return NULL; |
| 778 | if (start < 0) { |
| 779 | start += Py_SIZE(deque); |
| 780 | if (start < 0) |
| 781 | start = 0; |
| 782 | } |
| 783 | if (stop < 0) { |
| 784 | stop += Py_SIZE(deque); |
| 785 | if (stop < 0) |
| 786 | stop = 0; |
| 787 | } |
| 788 | |
| 789 | for (i=0 ; i<stop ; i++) { |
| 790 | if (i >= start) { |
| 791 | int cmp; |
| 792 | CHECK_NOT_END(b); |
| 793 | item = b->data[index]; |
| 794 | cmp = PyObject_RichCompareBool(item, v, Py_EQ); |
| 795 | if (cmp > 0) |
| 796 | return PyLong_FromSsize_t(i); |
| 797 | else if (cmp < 0) |
| 798 | return NULL; |
| 799 | if (start_state != deque->state) { |
| 800 | PyErr_SetString(PyExc_RuntimeError, |
| 801 | "deque mutated during iteration"); |
| 802 | return NULL; |
| 803 | } |
| 804 | } |
| 805 | index++; |
| 806 | if (index == BLOCKLEN) { |
| 807 | b = b->rightlink; |
| 808 | index = 0; |
| 809 | } |
| 810 | } |
| 811 | PyErr_Format(PyExc_ValueError, "%R is not in deque", v); |
| 812 | return NULL; |
| 813 | } |
| 814 | |
| 815 | PyDoc_STRVAR(index_doc, |
| 816 | "D.index(value, [start, [stop]]) -> integer -- return first index of value.\n" |
| 817 | "Raises ValueError if the value is not present."); |
| 818 | |
| 819 | static PyObject * |
| 820 | deque_insert(dequeobject *deque, PyObject *args) |
| 821 | { |
| 822 | Py_ssize_t index; |
| 823 | Py_ssize_t n = Py_SIZE(deque); |
| 824 | PyObject *value; |
| 825 | PyObject *rv; |
| 826 | |
| 827 | if (!PyArg_ParseTuple(args, "nO:insert", &index, &value)) |
| 828 | return NULL; |
| 829 | if (index >= n) |
| 830 | return deque_append(deque, value); |
| 831 | if (index <= -n || index == 0) |
| 832 | return deque_appendleft(deque, value); |
| 833 | if (_deque_rotate(deque, -index)) |
| 834 | return NULL; |
| 835 | if (index < 0) |
| 836 | rv = deque_append(deque, value); |
| 837 | else |
| 838 | rv = deque_appendleft(deque, value); |
| 839 | if (rv == NULL) |
| 840 | return NULL; |
| 841 | Py_DECREF(rv); |
| 842 | if (_deque_rotate(deque, index)) |
| 843 | return NULL; |
| 844 | Py_RETURN_NONE; |
| 845 | } |
| 846 | |
| 847 | PyDoc_STRVAR(insert_doc, |
| 848 | "D.insert(index, object) -- insert object before index"); |
| 849 | |
| 850 | static PyObject * |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 851 | deque_remove(dequeobject *deque, PyObject *value) |
| 852 | { |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 853 | Py_ssize_t i, n=Py_SIZE(deque); |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | for (i=0 ; i<n ; i++) { |
| 856 | PyObject *item = deque->leftblock->data[deque->leftindex]; |
| 857 | int cmp = PyObject_RichCompareBool(item, value, Py_EQ); |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 858 | |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 859 | if (Py_SIZE(deque) != n) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | PyErr_SetString(PyExc_IndexError, |
| 861 | "deque mutated during remove()."); |
| 862 | return NULL; |
| 863 | } |
| 864 | if (cmp > 0) { |
| 865 | PyObject *tgt = deque_popleft(deque, NULL); |
| 866 | assert (tgt != NULL); |
Raymond Hettinger | 6921c13 | 2015-03-21 02:03:40 -0700 | [diff] [blame] | 867 | if (_deque_rotate(deque, i)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | return NULL; |
Raymond Hettinger | ac13ad6 | 2015-03-21 01:53:16 -0700 | [diff] [blame] | 869 | Py_DECREF(tgt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | Py_RETURN_NONE; |
| 871 | } |
| 872 | else if (cmp < 0) { |
| 873 | _deque_rotate(deque, i); |
| 874 | return NULL; |
| 875 | } |
| 876 | _deque_rotate(deque, -1); |
| 877 | } |
| 878 | PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"); |
| 879 | return NULL; |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | PyDoc_STRVAR(remove_doc, |
| 883 | "D.remove(value) -- remove first occurrence of value."); |
| 884 | |
Benjamin Peterson | 0e5c48a | 2013-01-12 21:22:18 -0500 | [diff] [blame] | 885 | static void |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 886 | deque_clear(dequeobject *deque) |
| 887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | PyObject *item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 889 | |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 890 | while (Py_SIZE(deque)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | item = deque_pop(deque, NULL); |
| 892 | assert (item != NULL); |
| 893 | Py_DECREF(item); |
| 894 | } |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 895 | assert(deque->leftblock == deque->rightblock); |
| 896 | assert(deque->leftindex - 1 == deque->rightindex); |
| 897 | assert(Py_SIZE(deque) == 0); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Raymond Hettinger | 3c186ba | 2015-03-02 21:45:02 -0800 | [diff] [blame] | 900 | static int |
| 901 | valid_index(Py_ssize_t i, Py_ssize_t limit) |
| 902 | { |
| 903 | /* The cast to size_t let us use just a single comparison |
| 904 | to check whether i is in the range: 0 <= i < limit */ |
| 905 | return (size_t) i < (size_t) limit; |
| 906 | } |
| 907 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 908 | static PyObject * |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 909 | deque_item(dequeobject *deque, Py_ssize_t i) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | block *b; |
| 912 | PyObject *item; |
| 913 | Py_ssize_t n, index=i; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 914 | |
Raymond Hettinger | 3c186ba | 2015-03-02 21:45:02 -0800 | [diff] [blame] | 915 | if (!valid_index(i, Py_SIZE(deque))) { |
| 916 | PyErr_SetString(PyExc_IndexError, "deque index out of range"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | return NULL; |
| 918 | } |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | if (i == 0) { |
| 921 | i = deque->leftindex; |
| 922 | b = deque->leftblock; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 923 | } else if (i == Py_SIZE(deque) - 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | i = deque->rightindex; |
| 925 | b = deque->rightblock; |
| 926 | } else { |
| 927 | i += deque->leftindex; |
Raymond Hettinger | c208308 | 2015-02-28 23:29:16 -0800 | [diff] [blame] | 928 | n = (Py_ssize_t)((size_t) i / BLOCKLEN); |
| 929 | i = (Py_ssize_t)((size_t) i % BLOCKLEN); |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 930 | if (index < (Py_SIZE(deque) >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | b = deque->leftblock; |
| 932 | while (n--) |
| 933 | b = b->rightlink; |
| 934 | } else { |
Raymond Hettinger | 63d1ff2 | 2015-02-28 07:41:30 -0800 | [diff] [blame] | 935 | n = (Py_ssize_t)( |
Raymond Hettinger | c208308 | 2015-02-28 23:29:16 -0800 | [diff] [blame] | 936 | ((size_t)(deque->leftindex + Py_SIZE(deque) - 1)) |
Raymond Hettinger | 63d1ff2 | 2015-02-28 07:41:30 -0800 | [diff] [blame] | 937 | / BLOCKLEN - n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | b = deque->rightblock; |
| 939 | while (n--) |
| 940 | b = b->leftlink; |
| 941 | } |
| 942 | } |
| 943 | item = b->data[i]; |
| 944 | Py_INCREF(item); |
| 945 | return item; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Raymond Hettinger | 616f4f6 | 2004-06-26 04:42:06 +0000 | [diff] [blame] | 948 | /* delitem() implemented in terms of rotate for simplicity and reasonable |
| 949 | performance near the end points. If for some reason this method becomes |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 950 | 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] | 951 | (similar to code in list slice assignment) and achieve a two or threefold |
| 952 | performance boost. |
| 953 | */ |
| 954 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 955 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 956 | deque_del_item(dequeobject *deque, Py_ssize_t i) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 957 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | PyObject *item; |
Raymond Hettinger | ac13ad6 | 2015-03-21 01:53:16 -0700 | [diff] [blame] | 959 | int rv; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 960 | |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 961 | assert (i >= 0 && i < Py_SIZE(deque)); |
Raymond Hettinger | 6921c13 | 2015-03-21 02:03:40 -0700 | [diff] [blame] | 962 | if (_deque_rotate(deque, -i)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | item = deque_popleft(deque, NULL); |
Raymond Hettinger | ac13ad6 | 2015-03-21 01:53:16 -0700 | [diff] [blame] | 965 | rv = _deque_rotate(deque, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | assert (item != NULL); |
| 967 | Py_DECREF(item); |
Raymond Hettinger | ac13ad6 | 2015-03-21 01:53:16 -0700 | [diff] [blame] | 968 | return rv; |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 972 | deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | PyObject *old_value; |
| 975 | block *b; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 976 | Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 977 | |
Raymond Hettinger | 3c186ba | 2015-03-02 21:45:02 -0800 | [diff] [blame] | 978 | if (!valid_index(i, len)) { |
| 979 | PyErr_SetString(PyExc_IndexError, "deque index out of range"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | return -1; |
| 981 | } |
| 982 | if (v == NULL) |
| 983 | return deque_del_item(deque, i); |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | i += deque->leftindex; |
Raymond Hettinger | c208308 | 2015-02-28 23:29:16 -0800 | [diff] [blame] | 986 | n = (Py_ssize_t)((size_t) i / BLOCKLEN); |
| 987 | i = (Py_ssize_t)((size_t) i % BLOCKLEN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | if (index <= halflen) { |
| 989 | b = deque->leftblock; |
| 990 | while (n--) |
| 991 | b = b->rightlink; |
| 992 | } else { |
Raymond Hettinger | a473b9d | 2015-02-28 17:49:47 -0800 | [diff] [blame] | 993 | n = (Py_ssize_t)( |
Raymond Hettinger | c208308 | 2015-02-28 23:29:16 -0800 | [diff] [blame] | 994 | ((size_t)(deque->leftindex + Py_SIZE(deque) - 1)) |
Raymond Hettinger | a473b9d | 2015-02-28 17:49:47 -0800 | [diff] [blame] | 995 | / BLOCKLEN - n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | b = deque->rightblock; |
| 997 | while (n--) |
| 998 | b = b->leftlink; |
| 999 | } |
| 1000 | Py_INCREF(v); |
| 1001 | old_value = b->data[i]; |
| 1002 | b->data[i] = v; |
| 1003 | Py_DECREF(old_value); |
| 1004 | return 0; |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static PyObject * |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1008 | deque_clearmethod(dequeobject *deque) |
| 1009 | { |
Benjamin Peterson | 0e5c48a | 2013-01-12 21:22:18 -0500 | [diff] [blame] | 1010 | deque_clear(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | Py_RETURN_NONE; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | PyDoc_STRVAR(clear_doc, "Remove all elements from the deque."); |
| 1015 | |
| 1016 | static void |
| 1017 | deque_dealloc(dequeobject *deque) |
| 1018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | PyObject_GC_UnTrack(deque); |
| 1020 | if (deque->weakreflist != NULL) |
| 1021 | PyObject_ClearWeakRefs((PyObject *) deque); |
| 1022 | if (deque->leftblock != NULL) { |
| 1023 | deque_clear(deque); |
| 1024 | assert(deque->leftblock != NULL); |
| 1025 | freeblock(deque->leftblock); |
| 1026 | } |
| 1027 | deque->leftblock = NULL; |
| 1028 | deque->rightblock = NULL; |
| 1029 | Py_TYPE(deque)->tp_free(deque); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static int |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 1033 | deque_traverse(dequeobject *deque, visitproc visit, void *arg) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | block *b; |
| 1036 | PyObject *item; |
| 1037 | Py_ssize_t index; |
| 1038 | Py_ssize_t indexlo = deque->leftindex; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1039 | |
Raymond Hettinger | 5bfa867 | 2013-07-06 11:58:09 -1000 | [diff] [blame] | 1040 | for (b = deque->leftblock; b != deque->rightblock; b = b->rightlink) { |
| 1041 | for (index = indexlo; index < BLOCKLEN ; index++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | item = b->data[index]; |
| 1043 | Py_VISIT(item); |
| 1044 | } |
| 1045 | indexlo = 0; |
| 1046 | } |
Raymond Hettinger | 5bfa867 | 2013-07-06 11:58:09 -1000 | [diff] [blame] | 1047 | for (index = indexlo; index <= deque->rightindex; index++) { |
| 1048 | item = b->data[index]; |
| 1049 | Py_VISIT(item); |
| 1050 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1054 | static PyObject * |
| 1055 | deque_copy(PyObject *deque) |
| 1056 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | if (((dequeobject *)deque)->maxlen == -1) |
| 1058 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL); |
| 1059 | else |
| 1060 | return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi", |
| 1061 | deque, ((dequeobject *)deque)->maxlen, NULL); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque."); |
| 1065 | |
| 1066 | static PyObject * |
| 1067 | deque_reduce(dequeobject *deque) |
| 1068 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | PyObject *dict, *result, *aslist; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1070 | _Py_IDENTIFIER(__dict__); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1071 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1072 | dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | if (dict == NULL) |
| 1074 | PyErr_Clear(); |
| 1075 | aslist = PySequence_List((PyObject *)deque); |
| 1076 | if (aslist == NULL) { |
| 1077 | Py_XDECREF(dict); |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | if (dict == NULL) { |
| 1081 | if (deque->maxlen == -1) |
| 1082 | result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist); |
| 1083 | else |
| 1084 | result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen); |
| 1085 | } else { |
| 1086 | if (deque->maxlen == -1) |
| 1087 | result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict); |
| 1088 | else |
| 1089 | result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict); |
| 1090 | } |
| 1091 | Py_XDECREF(dict); |
| 1092 | Py_DECREF(aslist); |
| 1093 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 1097 | |
| 1098 | static PyObject * |
| 1099 | deque_repr(PyObject *deque) |
| 1100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | PyObject *aslist, *result; |
| 1102 | int i; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | i = Py_ReprEnter(deque); |
| 1105 | if (i != 0) { |
| 1106 | if (i < 0) |
| 1107 | return NULL; |
| 1108 | return PyUnicode_FromString("[...]"); |
| 1109 | } |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | aslist = PySequence_List(deque); |
| 1112 | if (aslist == NULL) { |
| 1113 | Py_ReprLeave(deque); |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | if (((dequeobject *)deque)->maxlen != -1) |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)", |
| 1119 | aslist, ((dequeobject *)deque)->maxlen); |
| 1120 | else |
| 1121 | result = PyUnicode_FromFormat("deque(%R)", aslist); |
| 1122 | Py_DECREF(aslist); |
| 1123 | Py_ReprLeave(deque); |
| 1124 | return result; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1127 | static PyObject * |
| 1128 | deque_richcompare(PyObject *v, PyObject *w, int op) |
| 1129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | PyObject *it1=NULL, *it2=NULL, *x, *y; |
| 1131 | Py_ssize_t vs, ws; |
| 1132 | int b, cmp=-1; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | if (!PyObject_TypeCheck(v, &deque_type) || |
| 1135 | !PyObject_TypeCheck(w, &deque_type)) { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1136 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | /* Shortcuts */ |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 1140 | vs = Py_SIZE((dequeobject *)v); |
| 1141 | ws = Py_SIZE((dequeobject *)w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | if (op == Py_EQ) { |
| 1143 | if (v == w) |
| 1144 | Py_RETURN_TRUE; |
| 1145 | if (vs != ws) |
| 1146 | Py_RETURN_FALSE; |
| 1147 | } |
| 1148 | if (op == Py_NE) { |
| 1149 | if (v == w) |
| 1150 | Py_RETURN_FALSE; |
| 1151 | if (vs != ws) |
| 1152 | Py_RETURN_TRUE; |
| 1153 | } |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | /* Search for the first index where items are different */ |
| 1156 | it1 = PyObject_GetIter(v); |
| 1157 | if (it1 == NULL) |
| 1158 | goto done; |
| 1159 | it2 = PyObject_GetIter(w); |
| 1160 | if (it2 == NULL) |
| 1161 | goto done; |
| 1162 | for (;;) { |
| 1163 | x = PyIter_Next(it1); |
| 1164 | if (x == NULL && PyErr_Occurred()) |
| 1165 | goto done; |
| 1166 | y = PyIter_Next(it2); |
| 1167 | if (x == NULL || y == NULL) |
| 1168 | break; |
| 1169 | b = PyObject_RichCompareBool(x, y, Py_EQ); |
| 1170 | if (b == 0) { |
| 1171 | cmp = PyObject_RichCompareBool(x, y, op); |
| 1172 | Py_DECREF(x); |
| 1173 | Py_DECREF(y); |
| 1174 | goto done; |
| 1175 | } |
| 1176 | Py_DECREF(x); |
| 1177 | Py_DECREF(y); |
| 1178 | if (b == -1) |
| 1179 | goto done; |
| 1180 | } |
| 1181 | /* We reached the end of one deque or both */ |
| 1182 | Py_XDECREF(x); |
| 1183 | Py_XDECREF(y); |
| 1184 | if (PyErr_Occurred()) |
| 1185 | goto done; |
| 1186 | switch (op) { |
| 1187 | case Py_LT: cmp = y != NULL; break; /* if w was longer */ |
| 1188 | case Py_LE: cmp = x == NULL; break; /* if v was not longer */ |
| 1189 | case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ |
| 1190 | case Py_NE: cmp = x != y; break; /* if one deque continues */ |
| 1191 | case Py_GT: cmp = x != NULL; break; /* if v was longer */ |
| 1192 | case Py_GE: cmp = y == NULL; break; /* if w was not longer */ |
| 1193 | } |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 1194 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1195 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | Py_XDECREF(it1); |
| 1197 | Py_XDECREF(it2); |
| 1198 | if (cmp == 1) |
| 1199 | Py_RETURN_TRUE; |
| 1200 | if (cmp == 0) |
| 1201 | Py_RETURN_FALSE; |
| 1202 | return NULL; |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1205 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1206 | deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1207 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | PyObject *iterable = NULL; |
| 1209 | PyObject *maxlenobj = NULL; |
| 1210 | Py_ssize_t maxlen = -1; |
| 1211 | char *kwlist[] = {"iterable", "maxlen", 0}; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) |
| 1214 | return -1; |
| 1215 | if (maxlenobj != NULL && maxlenobj != Py_None) { |
| 1216 | maxlen = PyLong_AsSsize_t(maxlenobj); |
| 1217 | if (maxlen == -1 && PyErr_Occurred()) |
| 1218 | return -1; |
| 1219 | if (maxlen < 0) { |
| 1220 | PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | } |
| 1224 | deque->maxlen = maxlen; |
| 1225 | deque_clear(deque); |
| 1226 | if (iterable != NULL) { |
| 1227 | PyObject *rv = deque_extend(deque, iterable); |
| 1228 | if (rv == NULL) |
| 1229 | return -1; |
| 1230 | Py_DECREF(rv); |
| 1231 | } |
| 1232 | return 0; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 1235 | static PyObject * |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 1236 | deque_sizeof(dequeobject *deque, void *unused) |
| 1237 | { |
| 1238 | Py_ssize_t res; |
| 1239 | Py_ssize_t blocks; |
| 1240 | |
| 1241 | res = sizeof(dequeobject); |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 1242 | blocks = (deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN; |
| 1243 | assert(deque->leftindex + Py_SIZE(deque) - 1 == |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 1244 | (blocks - 1) * BLOCKLEN + deque->rightindex); |
| 1245 | res += blocks * sizeof(block); |
| 1246 | return PyLong_FromSsize_t(res); |
| 1247 | } |
| 1248 | |
| 1249 | PyDoc_STRVAR(sizeof_doc, |
| 1250 | "D.__sizeof__() -- size of D in memory, in bytes"); |
| 1251 | |
Raymond Hettinger | 0f1451c | 2015-03-23 23:23:55 -0700 | [diff] [blame^] | 1252 | static int |
| 1253 | deque_bool(dequeobject *deque) |
| 1254 | { |
| 1255 | return Py_SIZE(deque) != 0; |
| 1256 | } |
| 1257 | |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 1258 | static PyObject * |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 1259 | deque_get_maxlen(dequeobject *deque) |
| 1260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | if (deque->maxlen == -1) |
| 1262 | Py_RETURN_NONE; |
| 1263 | return PyLong_FromSsize_t(deque->maxlen); |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static PyGetSetDef deque_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | {"maxlen", (getter)deque_get_maxlen, (setter)NULL, |
| 1268 | "maximum size of a deque or None if unbounded"}, |
| 1269 | {0} |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 1270 | }; |
| 1271 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1272 | static PySequenceMethods deque_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | (lenfunc)deque_len, /* sq_length */ |
| 1274 | 0, /* sq_concat */ |
| 1275 | 0, /* sq_repeat */ |
| 1276 | (ssizeargfunc)deque_item, /* sq_item */ |
| 1277 | 0, /* sq_slice */ |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 1278 | (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | 0, /* sq_ass_slice */ |
Raymond Hettinger | 39dadf7 | 2015-03-20 16:38:56 -0700 | [diff] [blame] | 1280 | (objobjproc)deque_contains, /* sq_contains */ |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 1281 | (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | 0, /* sq_inplace_repeat */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1283 | }; |
| 1284 | |
Raymond Hettinger | 0f1451c | 2015-03-23 23:23:55 -0700 | [diff] [blame^] | 1285 | static PyNumberMethods deque_as_number = { |
| 1286 | 0, /* nb_add */ |
| 1287 | 0, /* nb_subtract */ |
| 1288 | 0, /* nb_multiply */ |
| 1289 | 0, /* nb_remainder */ |
| 1290 | 0, /* nb_divmod */ |
| 1291 | 0, /* nb_power */ |
| 1292 | 0, /* nb_negative */ |
| 1293 | 0, /* nb_positive */ |
| 1294 | 0, /* nb_absolute */ |
| 1295 | (inquiry)deque_bool, /* nb_bool */ |
| 1296 | 0, /* nb_invert */ |
| 1297 | 0, /* nb_lshift */ |
| 1298 | 0, /* nb_rshift */ |
| 1299 | }; |
| 1300 | |
| 1301 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1302 | /* deque object ********************************************************/ |
| 1303 | |
| 1304 | static PyObject *deque_iter(dequeobject *deque); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1305 | static PyObject *deque_reviter(dequeobject *deque); |
Tim Peters | 1065f75 | 2004-10-01 01:03:29 +0000 | [diff] [blame] | 1306 | PyDoc_STRVAR(reversed_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | "D.__reversed__() -- return a reverse iterator over the deque"); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1308 | |
| 1309 | static PyMethodDef deque_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | {"append", (PyCFunction)deque_append, |
| 1311 | METH_O, append_doc}, |
| 1312 | {"appendleft", (PyCFunction)deque_appendleft, |
| 1313 | METH_O, appendleft_doc}, |
| 1314 | {"clear", (PyCFunction)deque_clearmethod, |
| 1315 | METH_NOARGS, clear_doc}, |
| 1316 | {"__copy__", (PyCFunction)deque_copy, |
| 1317 | METH_NOARGS, copy_doc}, |
Raymond Hettinger | 32ea165 | 2015-03-21 01:37:37 -0700 | [diff] [blame] | 1318 | {"copy", (PyCFunction)deque_copy, |
| 1319 | METH_NOARGS, copy_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | {"count", (PyCFunction)deque_count, |
Raymond Hettinger | 0f6f947 | 2015-03-21 01:42:10 -0700 | [diff] [blame] | 1321 | METH_O, count_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | {"extend", (PyCFunction)deque_extend, |
| 1323 | METH_O, extend_doc}, |
| 1324 | {"extendleft", (PyCFunction)deque_extendleft, |
| 1325 | METH_O, extendleft_doc}, |
Raymond Hettinger | 32ea165 | 2015-03-21 01:37:37 -0700 | [diff] [blame] | 1326 | {"index", (PyCFunction)deque_index, |
| 1327 | METH_VARARGS, index_doc}, |
| 1328 | {"insert", (PyCFunction)deque_insert, |
| 1329 | METH_VARARGS, insert_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | {"pop", (PyCFunction)deque_pop, |
| 1331 | METH_NOARGS, pop_doc}, |
| 1332 | {"popleft", (PyCFunction)deque_popleft, |
| 1333 | METH_NOARGS, popleft_doc}, |
Raymond Hettinger | 0f6f947 | 2015-03-21 01:42:10 -0700 | [diff] [blame] | 1334 | {"__reduce__", (PyCFunction)deque_reduce, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | METH_NOARGS, reduce_doc}, |
| 1336 | {"remove", (PyCFunction)deque_remove, |
| 1337 | METH_O, remove_doc}, |
| 1338 | {"__reversed__", (PyCFunction)deque_reviter, |
| 1339 | METH_NOARGS, reversed_doc}, |
| 1340 | {"reverse", (PyCFunction)deque_reverse, |
| 1341 | METH_NOARGS, reverse_doc}, |
| 1342 | {"rotate", (PyCFunction)deque_rotate, |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 1343 | METH_VARARGS, rotate_doc}, |
| 1344 | {"__sizeof__", (PyCFunction)deque_sizeof, |
| 1345 | METH_NOARGS, sizeof_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1347 | }; |
| 1348 | |
| 1349 | PyDoc_STRVAR(deque_doc, |
Andrew Svetlov | 6a5c7c3 | 2012-10-31 11:50:40 +0200 | [diff] [blame] | 1350 | "deque([iterable[, maxlen]]) --> deque object\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1351 | \n\ |
Raymond Hettinger | 4974705 | 2011-03-29 17:36:31 -0700 | [diff] [blame] | 1352 | Build an ordered collection with optimized access from its endpoints."); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1353 | |
Neal Norwitz | 87f1013 | 2004-02-29 15:40:53 +0000 | [diff] [blame] | 1354 | static PyTypeObject deque_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1356 | "collections.deque", /* tp_name */ |
| 1357 | sizeof(dequeobject), /* tp_basicsize */ |
| 1358 | 0, /* tp_itemsize */ |
| 1359 | /* methods */ |
| 1360 | (destructor)deque_dealloc, /* tp_dealloc */ |
| 1361 | 0, /* tp_print */ |
| 1362 | 0, /* tp_getattr */ |
| 1363 | 0, /* tp_setattr */ |
| 1364 | 0, /* tp_reserved */ |
| 1365 | deque_repr, /* tp_repr */ |
Raymond Hettinger | 0f1451c | 2015-03-23 23:23:55 -0700 | [diff] [blame^] | 1366 | &deque_as_number, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | &deque_as_sequence, /* tp_as_sequence */ |
| 1368 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1369 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | 0, /* tp_call */ |
| 1371 | 0, /* tp_str */ |
| 1372 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1373 | 0, /* tp_setattro */ |
| 1374 | 0, /* tp_as_buffer */ |
| 1375 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1376 | /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | deque_doc, /* tp_doc */ |
| 1378 | (traverseproc)deque_traverse, /* tp_traverse */ |
| 1379 | (inquiry)deque_clear, /* tp_clear */ |
| 1380 | (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1381 | offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | (getiterfunc)deque_iter, /* tp_iter */ |
| 1383 | 0, /* tp_iternext */ |
| 1384 | deque_methods, /* tp_methods */ |
| 1385 | 0, /* tp_members */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 1386 | deque_getset, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | 0, /* tp_base */ |
| 1388 | 0, /* tp_dict */ |
| 1389 | 0, /* tp_descr_get */ |
| 1390 | 0, /* tp_descr_set */ |
| 1391 | 0, /* tp_dictoffset */ |
| 1392 | (initproc)deque_init, /* tp_init */ |
| 1393 | PyType_GenericAlloc, /* tp_alloc */ |
| 1394 | deque_new, /* tp_new */ |
| 1395 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1396 | }; |
| 1397 | |
| 1398 | /*********************** Deque Iterator **************************/ |
| 1399 | |
| 1400 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | block *b; |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 1403 | Py_ssize_t index; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | dequeobject *deque; |
Raymond Hettinger | f9d9c79 | 2015-03-02 22:47:46 -0800 | [diff] [blame] | 1405 | size_t state; /* state when the iterator is created */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | Py_ssize_t counter; /* number of items remaining for iteration */ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1407 | } dequeiterobject; |
| 1408 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1409 | static PyTypeObject dequeiter_type; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1410 | |
| 1411 | static PyObject * |
| 1412 | deque_iter(dequeobject *deque) |
| 1413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | dequeiterobject *it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | it = PyObject_GC_New(dequeiterobject, &dequeiter_type); |
| 1417 | if (it == NULL) |
| 1418 | return NULL; |
| 1419 | it->b = deque->leftblock; |
| 1420 | it->index = deque->leftindex; |
| 1421 | Py_INCREF(deque); |
| 1422 | it->deque = deque; |
| 1423 | it->state = deque->state; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 1424 | it->counter = Py_SIZE(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | PyObject_GC_Track(it); |
| 1426 | return (PyObject *)it; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 1429 | static int |
| 1430 | dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg) |
| 1431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | Py_VISIT(dio->deque); |
| 1433 | return 0; |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1436 | static void |
| 1437 | dequeiter_dealloc(dequeiterobject *dio) |
| 1438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | Py_XDECREF(dio->deque); |
| 1440 | PyObject_GC_Del(dio); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | static PyObject * |
| 1444 | dequeiter_next(dequeiterobject *it) |
| 1445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | PyObject *item; |
Raymond Hettinger | d1b3d88 | 2004-10-02 00:43:13 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (it->deque->state != it->state) { |
| 1449 | it->counter = 0; |
| 1450 | PyErr_SetString(PyExc_RuntimeError, |
| 1451 | "deque mutated during iteration"); |
| 1452 | return NULL; |
| 1453 | } |
| 1454 | if (it->counter == 0) |
| 1455 | return NULL; |
| 1456 | assert (!(it->b == it->deque->rightblock && |
| 1457 | it->index > it->deque->rightindex)); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | item = it->b->data[it->index]; |
| 1460 | it->index++; |
| 1461 | it->counter--; |
| 1462 | if (it->index == BLOCKLEN && it->counter > 0) { |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 1463 | CHECK_NOT_END(it->b->rightlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | it->b = it->b->rightlink; |
| 1465 | it->index = 0; |
| 1466 | } |
| 1467 | Py_INCREF(item); |
| 1468 | return item; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1471 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1472 | dequeiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1473 | { |
| 1474 | Py_ssize_t i, index=0; |
| 1475 | PyObject *deque; |
| 1476 | dequeiterobject *it; |
| 1477 | if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index)) |
| 1478 | return NULL; |
| 1479 | assert(type == &dequeiter_type); |
| 1480 | |
| 1481 | it = (dequeiterobject*)deque_iter((dequeobject *)deque); |
| 1482 | if (!it) |
| 1483 | return NULL; |
| 1484 | /* consume items from the queue */ |
| 1485 | for(i=0; i<index; i++) { |
| 1486 | PyObject *item = dequeiter_next(it); |
| 1487 | if (item) { |
| 1488 | Py_DECREF(item); |
| 1489 | } else { |
| 1490 | if (it->counter) { |
| 1491 | Py_DECREF(it); |
| 1492 | return NULL; |
| 1493 | } else |
| 1494 | break; |
| 1495 | } |
| 1496 | } |
| 1497 | return (PyObject*)it; |
| 1498 | } |
| 1499 | |
| 1500 | static PyObject * |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1501 | dequeiter_len(dequeiterobject *it) |
| 1502 | { |
Antoine Pitrou | 554f334 | 2010-08-17 18:30:06 +0000 | [diff] [blame] | 1503 | return PyLong_FromSsize_t(it->counter); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 1506 | 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] | 1507 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1508 | static PyObject * |
| 1509 | dequeiter_reduce(dequeiterobject *it) |
| 1510 | { |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 1511 | return Py_BuildValue("O(On)", Py_TYPE(it), it->deque, Py_SIZE(it->deque) - it->counter); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1514 | static PyMethodDef dequeiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1516 | {"__reduce__", (PyCFunction)dequeiter_reduce, METH_NOARGS, reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1518 | }; |
| 1519 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1520 | static PyTypeObject dequeiter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 1522 | "_collections._deque_iterator", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1524 | 0, /* tp_itemsize */ |
| 1525 | /* methods */ |
| 1526 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1527 | 0, /* tp_print */ |
| 1528 | 0, /* tp_getattr */ |
| 1529 | 0, /* tp_setattr */ |
| 1530 | 0, /* tp_reserved */ |
| 1531 | 0, /* tp_repr */ |
| 1532 | 0, /* tp_as_number */ |
| 1533 | 0, /* tp_as_sequence */ |
| 1534 | 0, /* tp_as_mapping */ |
| 1535 | 0, /* tp_hash */ |
| 1536 | 0, /* tp_call */ |
| 1537 | 0, /* tp_str */ |
| 1538 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1539 | 0, /* tp_setattro */ |
| 1540 | 0, /* tp_as_buffer */ |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 1541 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | 0, /* tp_doc */ |
| 1543 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1544 | 0, /* tp_clear */ |
| 1545 | 0, /* tp_richcompare */ |
| 1546 | 0, /* tp_weaklistoffset */ |
| 1547 | PyObject_SelfIter, /* tp_iter */ |
| 1548 | (iternextfunc)dequeiter_next, /* tp_iternext */ |
| 1549 | dequeiter_methods, /* tp_methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1550 | 0, /* tp_members */ |
| 1551 | 0, /* tp_getset */ |
| 1552 | 0, /* tp_base */ |
| 1553 | 0, /* tp_dict */ |
| 1554 | 0, /* tp_descr_get */ |
| 1555 | 0, /* tp_descr_set */ |
| 1556 | 0, /* tp_dictoffset */ |
| 1557 | 0, /* tp_init */ |
| 1558 | 0, /* tp_alloc */ |
| 1559 | dequeiter_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | 0, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1561 | }; |
| 1562 | |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1563 | /*********************** Deque Reverse Iterator **************************/ |
| 1564 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1565 | static PyTypeObject dequereviter_type; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1566 | |
| 1567 | static PyObject * |
| 1568 | deque_reviter(dequeobject *deque) |
| 1569 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | dequeiterobject *it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | it = PyObject_GC_New(dequeiterobject, &dequereviter_type); |
| 1573 | if (it == NULL) |
| 1574 | return NULL; |
| 1575 | it->b = deque->rightblock; |
| 1576 | it->index = deque->rightindex; |
| 1577 | Py_INCREF(deque); |
| 1578 | it->deque = deque; |
| 1579 | it->state = deque->state; |
Raymond Hettinger | df715ba | 2013-07-06 13:01:13 -1000 | [diff] [blame] | 1580 | it->counter = Py_SIZE(deque); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | PyObject_GC_Track(it); |
| 1582 | return (PyObject *)it; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | static PyObject * |
| 1586 | dequereviter_next(dequeiterobject *it) |
| 1587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | PyObject *item; |
| 1589 | if (it->counter == 0) |
| 1590 | return NULL; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | if (it->deque->state != it->state) { |
| 1593 | it->counter = 0; |
| 1594 | PyErr_SetString(PyExc_RuntimeError, |
| 1595 | "deque mutated during iteration"); |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | assert (!(it->b == it->deque->leftblock && |
| 1599 | it->index < it->deque->leftindex)); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | item = it->b->data[it->index]; |
| 1602 | it->index--; |
| 1603 | it->counter--; |
| 1604 | if (it->index == -1 && it->counter > 0) { |
Raymond Hettinger | 82df925 | 2013-07-07 01:43:42 -1000 | [diff] [blame] | 1605 | CHECK_NOT_END(it->b->leftlink); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | it->b = it->b->leftlink; |
| 1607 | it->index = BLOCKLEN - 1; |
| 1608 | } |
| 1609 | Py_INCREF(item); |
| 1610 | return item; |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1613 | static PyObject * |
| 1614 | dequereviter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1615 | { |
| 1616 | Py_ssize_t i, index=0; |
| 1617 | PyObject *deque; |
| 1618 | dequeiterobject *it; |
| 1619 | if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index)) |
| 1620 | return NULL; |
| 1621 | assert(type == &dequereviter_type); |
| 1622 | |
| 1623 | it = (dequeiterobject*)deque_reviter((dequeobject *)deque); |
| 1624 | if (!it) |
| 1625 | return NULL; |
| 1626 | /* consume items from the queue */ |
| 1627 | for(i=0; i<index; i++) { |
| 1628 | PyObject *item = dequereviter_next(it); |
| 1629 | if (item) { |
| 1630 | Py_DECREF(item); |
| 1631 | } else { |
| 1632 | if (it->counter) { |
| 1633 | Py_DECREF(it); |
| 1634 | return NULL; |
| 1635 | } else |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | return (PyObject*)it; |
| 1640 | } |
| 1641 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1642 | static PyTypeObject dequereviter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 1644 | "_collections._deque_reverse_iterator", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | sizeof(dequeiterobject), /* tp_basicsize */ |
| 1646 | 0, /* tp_itemsize */ |
| 1647 | /* methods */ |
| 1648 | (destructor)dequeiter_dealloc, /* tp_dealloc */ |
| 1649 | 0, /* tp_print */ |
| 1650 | 0, /* tp_getattr */ |
| 1651 | 0, /* tp_setattr */ |
| 1652 | 0, /* tp_reserved */ |
| 1653 | 0, /* tp_repr */ |
| 1654 | 0, /* tp_as_number */ |
| 1655 | 0, /* tp_as_sequence */ |
| 1656 | 0, /* tp_as_mapping */ |
| 1657 | 0, /* tp_hash */ |
| 1658 | 0, /* tp_call */ |
| 1659 | 0, /* tp_str */ |
| 1660 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1661 | 0, /* tp_setattro */ |
| 1662 | 0, /* tp_as_buffer */ |
Raymond Hettinger | 87e6912 | 2015-03-02 23:32:02 -0800 | [diff] [blame] | 1663 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | 0, /* tp_doc */ |
| 1665 | (traverseproc)dequeiter_traverse, /* tp_traverse */ |
| 1666 | 0, /* tp_clear */ |
| 1667 | 0, /* tp_richcompare */ |
| 1668 | 0, /* tp_weaklistoffset */ |
| 1669 | PyObject_SelfIter, /* tp_iter */ |
| 1670 | (iternextfunc)dequereviter_next, /* tp_iternext */ |
| 1671 | dequeiter_methods, /* tp_methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1672 | 0, /* tp_members */ |
| 1673 | 0, /* tp_getset */ |
| 1674 | 0, /* tp_base */ |
| 1675 | 0, /* tp_dict */ |
| 1676 | 0, /* tp_descr_get */ |
| 1677 | 0, /* tp_descr_set */ |
| 1678 | 0, /* tp_dictoffset */ |
| 1679 | 0, /* tp_init */ |
| 1680 | 0, /* tp_alloc */ |
| 1681 | dequereviter_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | 0, |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 1683 | }; |
| 1684 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1685 | /* defaultdict type *********************************************************/ |
| 1686 | |
| 1687 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | PyDictObject dict; |
| 1689 | PyObject *default_factory; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1690 | } defdictobject; |
| 1691 | |
| 1692 | static PyTypeObject defdict_type; /* Forward */ |
| 1693 | |
| 1694 | PyDoc_STRVAR(defdict_missing_doc, |
| 1695 | "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1696 | if self.default_factory is None: raise KeyError((key,))\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1697 | self[key] = value = self.default_factory()\n\ |
| 1698 | return value\n\ |
| 1699 | "); |
| 1700 | |
| 1701 | static PyObject * |
| 1702 | defdict_missing(defdictobject *dd, PyObject *key) |
| 1703 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | PyObject *factory = dd->default_factory; |
| 1705 | PyObject *value; |
| 1706 | if (factory == NULL || factory == Py_None) { |
| 1707 | /* XXX Call dict.__missing__(key) */ |
| 1708 | PyObject *tup; |
| 1709 | tup = PyTuple_Pack(1, key); |
| 1710 | if (!tup) return NULL; |
| 1711 | PyErr_SetObject(PyExc_KeyError, tup); |
| 1712 | Py_DECREF(tup); |
| 1713 | return NULL; |
| 1714 | } |
| 1715 | value = PyEval_CallObject(factory, NULL); |
| 1716 | if (value == NULL) |
| 1717 | return value; |
| 1718 | if (PyObject_SetItem((PyObject *)dd, key, value) < 0) { |
| 1719 | Py_DECREF(value); |
| 1720 | return NULL; |
| 1721 | } |
| 1722 | return value; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D."); |
| 1726 | |
| 1727 | static PyObject * |
| 1728 | defdict_copy(defdictobject *dd) |
| 1729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | /* This calls the object's class. That only works for subclasses |
| 1731 | whose class constructor has the same signature. Subclasses that |
| 1732 | define a different constructor signature must override copy(). |
| 1733 | */ |
Raymond Hettinger | 54628fa | 2009-08-04 19:16:39 +0000 | [diff] [blame] | 1734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | if (dd->default_factory == NULL) |
| 1736 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL); |
| 1737 | return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), |
| 1738 | dd->default_factory, dd, NULL); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | static PyObject * |
| 1742 | defdict_reduce(defdictobject *dd) |
| 1743 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | /* __reduce__ must return a 5-tuple as follows: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1746 | - factory function |
| 1747 | - tuple of args for the factory function |
| 1748 | - additional state (here None) |
| 1749 | - sequence iterator (here None) |
| 1750 | - dictionary iterator (yielding successive (key, value) pairs |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | This API is used by pickle.py and copy.py. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | For this to be useful with pickle.py, the default_factory |
| 1755 | must be picklable; e.g., None, a built-in, or a global |
| 1756 | function in a module or package. |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | Both shallow and deep copying are supported, but for deep |
| 1759 | copying, the default_factory must be deep-copyable; e.g. None, |
| 1760 | or a built-in (functions are not copyable at this time). |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | This only works for subclasses as long as their constructor |
| 1763 | signature is compatible; the first argument must be the |
| 1764 | optional default_factory, defaulting to None. |
| 1765 | */ |
| 1766 | PyObject *args; |
| 1767 | PyObject *items; |
| 1768 | PyObject *iter; |
| 1769 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1770 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | if (dd->default_factory == NULL || dd->default_factory == Py_None) |
| 1773 | args = PyTuple_New(0); |
| 1774 | else |
| 1775 | args = PyTuple_Pack(1, dd->default_factory); |
| 1776 | if (args == NULL) |
| 1777 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1778 | items = _PyObject_CallMethodId((PyObject *)dd, &PyId_items, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | if (items == NULL) { |
| 1780 | Py_DECREF(args); |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | iter = PyObject_GetIter(items); |
| 1784 | if (iter == NULL) { |
| 1785 | Py_DECREF(items); |
| 1786 | Py_DECREF(args); |
| 1787 | return NULL; |
| 1788 | } |
| 1789 | result = PyTuple_Pack(5, Py_TYPE(dd), args, |
| 1790 | Py_None, Py_None, iter); |
| 1791 | Py_DECREF(iter); |
| 1792 | Py_DECREF(items); |
| 1793 | Py_DECREF(args); |
| 1794 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | static PyMethodDef defdict_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | {"__missing__", (PyCFunction)defdict_missing, METH_O, |
| 1799 | defdict_missing_doc}, |
| 1800 | {"copy", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1801 | defdict_copy_doc}, |
| 1802 | {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS, |
| 1803 | defdict_copy_doc}, |
| 1804 | {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS, |
| 1805 | reduce_doc}, |
| 1806 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1807 | }; |
| 1808 | |
| 1809 | static PyMemberDef defdict_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | {"default_factory", T_OBJECT, |
| 1811 | offsetof(defdictobject, default_factory), 0, |
| 1812 | PyDoc_STR("Factory for default value called by __missing__().")}, |
| 1813 | {NULL} |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1814 | }; |
| 1815 | |
| 1816 | static void |
| 1817 | defdict_dealloc(defdictobject *dd) |
| 1818 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | Py_CLEAR(dd->default_factory); |
| 1820 | PyDict_Type.tp_dealloc((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1823 | static PyObject * |
| 1824 | defdict_repr(defdictobject *dd) |
| 1825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | PyObject *baserepr; |
| 1827 | PyObject *defrepr; |
| 1828 | PyObject *result; |
| 1829 | baserepr = PyDict_Type.tp_repr((PyObject *)dd); |
| 1830 | if (baserepr == NULL) |
| 1831 | return NULL; |
| 1832 | if (dd->default_factory == NULL) |
| 1833 | defrepr = PyUnicode_FromString("None"); |
| 1834 | else |
| 1835 | { |
| 1836 | int status = Py_ReprEnter(dd->default_factory); |
| 1837 | if (status != 0) { |
Antoine Pitrou | f5f1fe0 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1838 | if (status < 0) { |
| 1839 | Py_DECREF(baserepr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | return NULL; |
Antoine Pitrou | f5f1fe0 | 2012-02-15 02:42:46 +0100 | [diff] [blame] | 1841 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | defrepr = PyUnicode_FromString("..."); |
| 1843 | } |
| 1844 | else |
| 1845 | defrepr = PyObject_Repr(dd->default_factory); |
| 1846 | Py_ReprLeave(dd->default_factory); |
| 1847 | } |
| 1848 | if (defrepr == NULL) { |
| 1849 | Py_DECREF(baserepr); |
| 1850 | return NULL; |
| 1851 | } |
| 1852 | result = PyUnicode_FromFormat("defaultdict(%U, %U)", |
| 1853 | defrepr, baserepr); |
| 1854 | Py_DECREF(defrepr); |
| 1855 | Py_DECREF(baserepr); |
| 1856 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | static int |
| 1860 | defdict_traverse(PyObject *self, visitproc visit, void *arg) |
| 1861 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | Py_VISIT(((defdictobject *)self)->default_factory); |
| 1863 | return PyDict_Type.tp_traverse(self, visit, arg); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | static int |
| 1867 | defdict_tp_clear(defdictobject *dd) |
| 1868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | Py_CLEAR(dd->default_factory); |
| 1870 | return PyDict_Type.tp_clear((PyObject *)dd); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | static int |
| 1874 | defdict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1875 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | defdictobject *dd = (defdictobject *)self; |
| 1877 | PyObject *olddefault = dd->default_factory; |
| 1878 | PyObject *newdefault = NULL; |
| 1879 | PyObject *newargs; |
| 1880 | int result; |
| 1881 | if (args == NULL || !PyTuple_Check(args)) |
| 1882 | newargs = PyTuple_New(0); |
| 1883 | else { |
| 1884 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
| 1885 | if (n > 0) { |
| 1886 | newdefault = PyTuple_GET_ITEM(args, 0); |
| 1887 | if (!PyCallable_Check(newdefault) && newdefault != Py_None) { |
| 1888 | PyErr_SetString(PyExc_TypeError, |
| 1889 | "first argument must be callable"); |
| 1890 | return -1; |
| 1891 | } |
| 1892 | } |
| 1893 | newargs = PySequence_GetSlice(args, 1, n); |
| 1894 | } |
| 1895 | if (newargs == NULL) |
| 1896 | return -1; |
| 1897 | Py_XINCREF(newdefault); |
| 1898 | dd->default_factory = newdefault; |
| 1899 | result = PyDict_Type.tp_init(self, newargs, kwds); |
| 1900 | Py_DECREF(newargs); |
| 1901 | Py_XDECREF(olddefault); |
| 1902 | return result; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | PyDoc_STRVAR(defdict_doc, |
Benjamin Peterson | 9cb33b7 | 2014-01-13 23:56:05 -0500 | [diff] [blame] | 1906 | "defaultdict(default_factory[, ...]) --> dict with default factory\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1907 | \n\ |
| 1908 | The default factory is called without arguments to produce\n\ |
| 1909 | a new value when a key is not present, in __getitem__ only.\n\ |
| 1910 | A defaultdict compares equal to a dict with the same items.\n\ |
Benjamin Peterson | 9cb33b7 | 2014-01-13 23:56:05 -0500 | [diff] [blame] | 1911 | All remaining arguments are treated the same as if they were\n\ |
| 1912 | passed to the dict constructor, including keyword arguments.\n\ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1913 | "); |
| 1914 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1915 | /* See comment in xxsubtype.c */ |
| 1916 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 1917 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1918 | static PyTypeObject defdict_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 1920 | "collections.defaultdict", /* tp_name */ |
| 1921 | sizeof(defdictobject), /* tp_basicsize */ |
| 1922 | 0, /* tp_itemsize */ |
| 1923 | /* methods */ |
| 1924 | (destructor)defdict_dealloc, /* tp_dealloc */ |
| 1925 | 0, /* tp_print */ |
| 1926 | 0, /* tp_getattr */ |
| 1927 | 0, /* tp_setattr */ |
| 1928 | 0, /* tp_reserved */ |
| 1929 | (reprfunc)defdict_repr, /* tp_repr */ |
| 1930 | 0, /* tp_as_number */ |
| 1931 | 0, /* tp_as_sequence */ |
| 1932 | 0, /* tp_as_mapping */ |
| 1933 | 0, /* tp_hash */ |
| 1934 | 0, /* tp_call */ |
| 1935 | 0, /* tp_str */ |
| 1936 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1937 | 0, /* tp_setattro */ |
| 1938 | 0, /* tp_as_buffer */ |
| 1939 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1940 | /* tp_flags */ |
| 1941 | defdict_doc, /* tp_doc */ |
| 1942 | defdict_traverse, /* tp_traverse */ |
| 1943 | (inquiry)defdict_tp_clear, /* tp_clear */ |
| 1944 | 0, /* tp_richcompare */ |
| 1945 | 0, /* tp_weaklistoffset*/ |
| 1946 | 0, /* tp_iter */ |
| 1947 | 0, /* tp_iternext */ |
| 1948 | defdict_methods, /* tp_methods */ |
| 1949 | defdict_members, /* tp_members */ |
| 1950 | 0, /* tp_getset */ |
| 1951 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */ |
| 1952 | 0, /* tp_dict */ |
| 1953 | 0, /* tp_descr_get */ |
| 1954 | 0, /* tp_descr_set */ |
| 1955 | 0, /* tp_dictoffset */ |
| 1956 | defdict_init, /* tp_init */ |
| 1957 | PyType_GenericAlloc, /* tp_alloc */ |
| 1958 | 0, /* tp_new */ |
| 1959 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1960 | }; |
| 1961 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1962 | /* helper function for Counter *********************************************/ |
| 1963 | |
| 1964 | PyDoc_STRVAR(_count_elements_doc, |
| 1965 | "_count_elements(mapping, iterable) -> None\n\ |
| 1966 | \n\ |
| 1967 | Count elements in the iterable, updating the mappping"); |
| 1968 | |
| 1969 | static PyObject * |
| 1970 | _count_elements(PyObject *self, PyObject *args) |
| 1971 | { |
Raymond Hettinger | cb1d96f | 2013-10-04 16:51:02 -0700 | [diff] [blame] | 1972 | _Py_IDENTIFIER(get); |
Raymond Hettinger | 2ff2190 | 2013-10-01 00:55:43 -0700 | [diff] [blame] | 1973 | _Py_IDENTIFIER(__setitem__); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1974 | PyObject *it, *iterable, *mapping, *oldval; |
| 1975 | PyObject *newval = NULL; |
| 1976 | PyObject *key = NULL; |
Raymond Hettinger | 224c87d | 2013-10-01 21:36:09 -0700 | [diff] [blame] | 1977 | PyObject *zero = NULL; |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1978 | PyObject *one = NULL; |
Raymond Hettinger | cb1d96f | 2013-10-04 16:51:02 -0700 | [diff] [blame] | 1979 | PyObject *bound_get = NULL; |
| 1980 | PyObject *mapping_get; |
| 1981 | PyObject *dict_get; |
Raymond Hettinger | 2ff2190 | 2013-10-01 00:55:43 -0700 | [diff] [blame] | 1982 | PyObject *mapping_setitem; |
Raymond Hettinger | 2ff2190 | 2013-10-01 00:55:43 -0700 | [diff] [blame] | 1983 | PyObject *dict_setitem; |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1984 | |
| 1985 | if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable)) |
| 1986 | return NULL; |
| 1987 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1988 | it = PyObject_GetIter(iterable); |
| 1989 | if (it == NULL) |
| 1990 | return NULL; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1991 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 1992 | one = PyLong_FromLong(1); |
Raymond Hettinger | 224c87d | 2013-10-01 21:36:09 -0700 | [diff] [blame] | 1993 | if (one == NULL) |
| 1994 | goto done; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 1995 | |
Raymond Hettinger | cb1d96f | 2013-10-04 16:51:02 -0700 | [diff] [blame] | 1996 | /* Only take the fast path when get() and __setitem__() |
| 1997 | * have not been overridden. |
| 1998 | */ |
| 1999 | mapping_get = _PyType_LookupId(Py_TYPE(mapping), &PyId_get); |
| 2000 | dict_get = _PyType_LookupId(&PyDict_Type, &PyId_get); |
Raymond Hettinger | 2ff2190 | 2013-10-01 00:55:43 -0700 | [diff] [blame] | 2001 | mapping_setitem = _PyType_LookupId(Py_TYPE(mapping), &PyId___setitem__); |
| 2002 | dict_setitem = _PyType_LookupId(&PyDict_Type, &PyId___setitem__); |
| 2003 | |
Raymond Hettinger | cb1d96f | 2013-10-04 16:51:02 -0700 | [diff] [blame] | 2004 | if (mapping_get != NULL && mapping_get == dict_get && |
| 2005 | mapping_setitem != NULL && mapping_setitem == dict_setitem) { |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2006 | while (1) { |
Raymond Hettinger | 507d997 | 2014-05-18 21:32:40 +0100 | [diff] [blame] | 2007 | /* Fast path advantages: |
| 2008 | 1. Eliminate double hashing |
| 2009 | (by re-using the same hash for both the get and set) |
| 2010 | 2. Avoid argument overhead of PyObject_CallFunctionObjArgs |
| 2011 | (argument tuple creation and parsing) |
| 2012 | 3. Avoid indirection through a bound method object |
| 2013 | (creates another argument tuple) |
| 2014 | 4. Avoid initial increment from zero |
| 2015 | (reuse an existing one-object instead) |
| 2016 | */ |
Raymond Hettinger | 4b0b1ac | 2014-05-03 16:41:19 -0700 | [diff] [blame] | 2017 | Py_hash_t hash; |
| 2018 | |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2019 | key = PyIter_Next(it); |
Victor Stinner | a154b5c | 2011-04-20 23:23:52 +0200 | [diff] [blame] | 2020 | if (key == NULL) |
| 2021 | break; |
Raymond Hettinger | 4b0b1ac | 2014-05-03 16:41:19 -0700 | [diff] [blame] | 2022 | |
| 2023 | if (!PyUnicode_CheckExact(key) || |
| 2024 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
| 2025 | { |
| 2026 | hash = PyObject_Hash(key); |
Raymond Hettinger | da2850f | 2015-02-27 12:42:54 -0800 | [diff] [blame] | 2027 | if (hash == -1) |
Raymond Hettinger | 4b0b1ac | 2014-05-03 16:41:19 -0700 | [diff] [blame] | 2028 | goto done; |
| 2029 | } |
| 2030 | |
| 2031 | oldval = _PyDict_GetItem_KnownHash(mapping, key, hash); |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2032 | if (oldval == NULL) { |
Raymond Hettinger | 4b0b1ac | 2014-05-03 16:41:19 -0700 | [diff] [blame] | 2033 | if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) == -1) |
Raymond Hettinger | 507d997 | 2014-05-18 21:32:40 +0100 | [diff] [blame] | 2034 | goto done; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2035 | } else { |
| 2036 | newval = PyNumber_Add(oldval, one); |
| 2037 | if (newval == NULL) |
Raymond Hettinger | 507d997 | 2014-05-18 21:32:40 +0100 | [diff] [blame] | 2038 | goto done; |
Raymond Hettinger | 4b0b1ac | 2014-05-03 16:41:19 -0700 | [diff] [blame] | 2039 | if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) == -1) |
Raymond Hettinger | 507d997 | 2014-05-18 21:32:40 +0100 | [diff] [blame] | 2040 | goto done; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2041 | Py_CLEAR(newval); |
| 2042 | } |
| 2043 | Py_DECREF(key); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2044 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2045 | } else { |
Victor Stinner | e7f516c | 2013-11-06 23:52:55 +0100 | [diff] [blame] | 2046 | bound_get = _PyObject_GetAttrId(mapping, &PyId_get); |
Raymond Hettinger | cb1d96f | 2013-10-04 16:51:02 -0700 | [diff] [blame] | 2047 | if (bound_get == NULL) |
Raymond Hettinger | 224c87d | 2013-10-01 21:36:09 -0700 | [diff] [blame] | 2048 | goto done; |
| 2049 | |
| 2050 | zero = PyLong_FromLong(0); |
| 2051 | if (zero == NULL) |
| 2052 | goto done; |
| 2053 | |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2054 | while (1) { |
| 2055 | key = PyIter_Next(it); |
Victor Stinner | a154b5c | 2011-04-20 23:23:52 +0200 | [diff] [blame] | 2056 | if (key == NULL) |
| 2057 | break; |
Raymond Hettinger | cb1d96f | 2013-10-04 16:51:02 -0700 | [diff] [blame] | 2058 | oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL); |
Raymond Hettinger | 224c87d | 2013-10-01 21:36:09 -0700 | [diff] [blame] | 2059 | if (oldval == NULL) |
| 2060 | break; |
| 2061 | newval = PyNumber_Add(oldval, one); |
| 2062 | Py_DECREF(oldval); |
| 2063 | if (newval == NULL) |
| 2064 | break; |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2065 | if (PyObject_SetItem(mapping, key, newval) == -1) |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2066 | break; |
| 2067 | Py_CLEAR(newval); |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2068 | Py_DECREF(key); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2069 | } |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2070 | } |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 2071 | |
Raymond Hettinger | 224c87d | 2013-10-01 21:36:09 -0700 | [diff] [blame] | 2072 | done: |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2073 | Py_DECREF(it); |
| 2074 | Py_XDECREF(key); |
| 2075 | Py_XDECREF(newval); |
Raymond Hettinger | cb1d96f | 2013-10-04 16:51:02 -0700 | [diff] [blame] | 2076 | Py_XDECREF(bound_get); |
Raymond Hettinger | 224c87d | 2013-10-01 21:36:09 -0700 | [diff] [blame] | 2077 | Py_XDECREF(zero); |
| 2078 | Py_XDECREF(one); |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2079 | if (PyErr_Occurred()) |
| 2080 | return NULL; |
| 2081 | Py_RETURN_NONE; |
| 2082 | } |
| 2083 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2084 | /* module level code ********************************************************/ |
| 2085 | |
| 2086 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 2087 | "High performance data structures.\n\ |
| 2088 | - deque: ordered collection accessible from endpoints only\n\ |
| 2089 | - defaultdict: dict subclass with a default value factory\n\ |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2090 | "); |
| 2091 | |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2092 | static struct PyMethodDef module_functions[] = { |
| 2093 | {"_count_elements", _count_elements, METH_VARARGS, _count_elements_doc}, |
| 2094 | {NULL, NULL} /* sentinel */ |
| 2095 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2096 | |
| 2097 | static struct PyModuleDef _collectionsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | PyModuleDef_HEAD_INIT, |
| 2099 | "_collections", |
| 2100 | module_doc, |
| 2101 | -1, |
Raymond Hettinger | 96f3410 | 2010-12-15 16:30:37 +0000 | [diff] [blame] | 2102 | module_functions, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2103 | NULL, |
| 2104 | NULL, |
| 2105 | NULL, |
| 2106 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2107 | }; |
| 2108 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2109 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2110 | PyInit__collections(void) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | PyObject *m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | m = PyModule_Create(&_collectionsmodule); |
| 2115 | if (m == NULL) |
| 2116 | return NULL; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | if (PyType_Ready(&deque_type) < 0) |
| 2119 | return NULL; |
| 2120 | Py_INCREF(&deque_type); |
| 2121 | PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | defdict_type.tp_base = &PyDict_Type; |
| 2124 | if (PyType_Ready(&defdict_type) < 0) |
| 2125 | return NULL; |
| 2126 | Py_INCREF(&defdict_type); |
| 2127 | PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 2128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | if (PyType_Ready(&dequeiter_type) < 0) |
| 2130 | return NULL; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2131 | Py_INCREF(&dequeiter_type); |
| 2132 | PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | if (PyType_Ready(&dequereviter_type) < 0) |
| 2135 | return NULL; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2136 | Py_INCREF(&dequereviter_type); |
| 2137 | PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type); |
Raymond Hettinger | 1e5809f | 2004-03-18 11:04:57 +0000 | [diff] [blame] | 2138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | return m; |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 2140 | } |