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