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