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