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