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