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