blob: 8990071f519ea2ff38877652da4095eae97ca447 [file] [log] [blame]
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001#include "Python.h"
Victor Stinner37834132020-10-27 17:12:53 +01002#include "pycore_long.h" // _PyLong_GetZero()
Victor Stinner4a21e572020-04-15 02:35:41 +02003#include "structmember.h" // PyMemberDef
Raymond Hettinger756b3f32004-01-29 06:37:52 +00004
Raymond Hettingerc2083082015-02-28 23:29:16 -08005#ifdef STDC_HEADERS
6#include <stddef.h>
7#else
Victor Stinner4a21e572020-04-15 02:35:41 +02008#include <sys/types.h> // size_t
Raymond Hettingerc2083082015-02-28 23:29:16 -08009#endif
10
Pablo Galindo3f5fc702018-12-30 09:24:03 +000011/*[clinic input]
Raymond Hettingere9858042019-06-05 16:05:25 -070012module _collections
Pablo Galindo3f5fc702018-12-30 09:24:03 +000013class _tuplegetter "_tuplegetterobject *" "&tuplegetter_type"
14[clinic start generated code]*/
Raymond Hettingere9858042019-06-05 16:05:25 -070015/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a8ece4ccad7e30ac]*/
Pablo Galindo3f5fc702018-12-30 09:24:03 +000016
17static PyTypeObject tuplegetter_type;
18#include "clinic/_collectionsmodule.c.h"
19
Raymond Hettinger756b3f32004-01-29 06:37:52 +000020/* collections module implementation of a deque() datatype
21 Written and maintained by Raymond D. Hettinger <python@rcn.com>
Raymond Hettinger756b3f32004-01-29 06:37:52 +000022*/
23
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000024/* The block length may be set to any number over 1. Larger numbers
Raymond Hettinger20b0f872013-06-23 15:44:33 -070025 * reduce the number of calls to the memory allocator, give faster
Raymond Hettinger30c90742015-03-02 22:31:35 -080026 * indexing and rotation, and reduce the link to data overhead ratio.
Raymond Hettingerdaf57f22015-02-26 23:21:29 -080027 * Making the block length a power of two speeds-up the modulo
Raymond Hettinger30c90742015-03-02 22:31:35 -080028 * and division calculations in deque_item() and deque_ass_item().
Raymond Hettinger77e8bf12004-10-01 15:25:53 +000029 */
30
Raymond Hettingerdaf57f22015-02-26 23:21:29 -080031#define BLOCKLEN 64
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000032#define CENTER ((BLOCKLEN - 1) / 2)
Raymond Hettinger756b3f32004-01-29 06:37:52 +000033
Raymond Hettinger551350a2015-03-24 00:19:53 -070034/* Data for deque objects is stored in a doubly-linked list of fixed
35 * length blocks. This assures that appends or pops never move any
36 * other data elements besides the one being appended or popped.
37 *
38 * Another advantage is that it completely avoids use of realloc(),
39 * resulting in more predictable performance.
40 *
41 * Textbook implementations of doubly-linked lists store one datum
42 * per link, but that gives them a 200% memory overhead (a prev and
43 * next link for each datum) and it costs one malloc() call per data
44 * element. By using fixed-length blocks, the link to data ratio is
45 * significantly improved and there are proportionally fewer calls
46 * to malloc() and free(). The data blocks of consecutive pointers
47 * also improve cache locality.
48 *
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000049 * The list of blocks is never empty, so d.leftblock and d.rightblock
Raymond Hettinger82df9252013-07-07 01:43:42 -100050 * are never equal to NULL. The list is not circular.
51 *
52 * A deque d's first element is at d.leftblock[leftindex]
53 * and its last element is at d.rightblock[rightindex].
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000054 *
Raymond Hettinger551350a2015-03-24 00:19:53 -070055 * Unlike Python slice indices, these indices are inclusive on both
56 * ends. This makes the algorithms for left and right operations
57 * more symmetrical and it simplifies the design.
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000058 *
Raymond Hettinger551350a2015-03-24 00:19:53 -070059 * The indices, d.leftindex and d.rightindex are always in the range:
60 * 0 <= index < BLOCKLEN
61 *
62 * And their exact relationship is:
63 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex
Raymond Hettinger61f05fb2004-10-01 06:24:12 +000064 *
Raymond Hettinger8dbbae22015-03-24 21:01:50 -070065 * Whenever d.leftblock == d.rightblock, then:
Raymond Hettinger551350a2015-03-24 00:19:53 -070066 * d.leftindex + d.len - 1 == d.rightindex
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 *
Raymond Hettinger551350a2015-03-24 00:19:53 -070068 * However, when d.leftblock != d.rightblock, the d.leftindex and
69 * d.rightindex become indices into distinct blocks and either may
70 * be larger than the other.
71 *
72 * Empty deques have:
73 * d.len == 0
74 * d.leftblock == d.rightblock
75 * d.leftindex == CENTER + 1
76 * d.rightindex == CENTER
77 *
78 * Checking for d.len == 0 is the intended way to see whether d is empty.
Tim Petersd8768d32004-10-01 01:32:53 +000079 */
80
Raymond Hettinger756b3f32004-01-29 06:37:52 +000081typedef struct BLOCK {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 struct BLOCK *leftlink;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 PyObject *data[BLOCKLEN];
Raymond Hettinger20b0f872013-06-23 15:44:33 -070084 struct BLOCK *rightlink;
Raymond Hettinger756b3f32004-01-29 06:37:52 +000085} block;
86
Raymond Hettinger30c90742015-03-02 22:31:35 -080087typedef struct {
88 PyObject_VAR_HEAD
89 block *leftblock;
90 block *rightblock;
Raymond Hettinger551350a2015-03-24 00:19:53 -070091 Py_ssize_t leftindex; /* 0 <= leftindex < BLOCKLEN */
92 Py_ssize_t rightindex; /* 0 <= rightindex < BLOCKLEN */
Raymond Hettingerf9d9c792015-03-02 22:47:46 -080093 size_t state; /* incremented whenever the indices move */
Raymond Hettingerd84ec222016-01-24 09:12:06 -080094 Py_ssize_t maxlen; /* maxlen is -1 for unbounded deques */
Raymond Hettinger0f6f9472015-03-21 01:42:10 -070095 PyObject *weakreflist;
Raymond Hettinger30c90742015-03-02 22:31:35 -080096} dequeobject;
97
98static PyTypeObject deque_type;
99
Raymond Hettinger82df9252013-07-07 01:43:42 -1000100/* For debug builds, add error checking to track the endpoints
101 * in the chain of links. The goal is to make sure that link
102 * assignments only take place at endpoints so that links already
103 * in use do not get overwritten.
104 *
105 * CHECK_END should happen before each assignment to a block's link field.
106 * MARK_END should happen whenever a link field becomes a new endpoint.
107 * This happens when new blocks are added or whenever an existing
108 * block is freed leaving another existing block as the new endpoint.
109 */
110
Raymond Hettinger3223dd52013-07-26 23:14:22 -0700111#ifndef NDEBUG
Raymond Hettinger82df9252013-07-07 01:43:42 -1000112#define MARK_END(link) link = NULL;
113#define CHECK_END(link) assert(link == NULL);
114#define CHECK_NOT_END(link) assert(link != NULL);
115#else
116#define MARK_END(link)
117#define CHECK_END(link)
118#define CHECK_NOT_END(link)
119#endif
120
Tim Peters6f853562004-10-01 01:04:50 +0000121static block *
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700122newblock(void) {
Raymond Hettinger32f2eda2020-06-23 06:50:15 -0700123 block *b = PyMem_Malloc(sizeof(block));
Raymond Hettinger82df9252013-07-07 01:43:42 -1000124 if (b != NULL) {
125 return b;
126 }
127 PyErr_NoMemory();
128 return NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000129}
130
Martin v. Löwis59683e82008-06-13 07:50:45 +0000131static void
Guido van Rossum58da9312007-11-10 23:39:45 +0000132freeblock(block *b)
133{
Raymond Hettinger32f2eda2020-06-23 06:50:15 -0700134 PyMem_Free(b);
Guido van Rossum58da9312007-11-10 23:39:45 +0000135}
136
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000137static PyObject *
138deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 dequeobject *deque;
141 block *b;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 /* create dequeobject structure */
144 deque = (dequeobject *)type->tp_alloc(type, 0);
145 if (deque == NULL)
146 return NULL;
Tim Peters1065f752004-10-01 01:03:29 +0000147
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700148 b = newblock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 if (b == NULL) {
150 Py_DECREF(deque);
151 return NULL;
152 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000153 MARK_END(b->leftlink);
154 MARK_END(b->rightlink);
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 assert(BLOCKLEN >= 2);
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100157 Py_SET_SIZE(deque, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 deque->leftblock = b;
159 deque->rightblock = b;
160 deque->leftindex = CENTER + 1;
161 deque->rightindex = CENTER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 deque->state = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 deque->maxlen = -1;
Raymond Hettinger87e69122015-03-02 23:32:02 -0800164 deque->weakreflist = NULL;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return (PyObject *)deque;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000167}
168
169static PyObject *
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000170deque_pop(dequeobject *deque, PyObject *unused)
171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyObject *item;
173 block *prevblock;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000174
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000175 if (Py_SIZE(deque) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
177 return NULL;
178 }
179 item = deque->rightblock->data[deque->rightindex];
180 deque->rightindex--;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100181 Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000183
Raymond Hettingerd3d2b2c2015-09-21 23:41:56 -0700184 if (deque->rightindex < 0) {
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700185 if (Py_SIZE(deque)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 prevblock = deque->rightblock->leftlink;
187 assert(deque->leftblock != deque->rightblock);
188 freeblock(deque->rightblock);
Raymond Hettinger82df9252013-07-07 01:43:42 -1000189 CHECK_NOT_END(prevblock);
190 MARK_END(prevblock->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 deque->rightblock = prevblock;
192 deque->rightindex = BLOCKLEN - 1;
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700193 } else {
194 assert(deque->leftblock == deque->rightblock);
195 assert(deque->leftindex == deque->rightindex+1);
196 /* re-center instead of freeing a block */
197 deque->leftindex = CENTER + 1;
198 deque->rightindex = CENTER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 }
200 }
201 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000202}
203
204PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
205
206static PyObject *
207deque_popleft(dequeobject *deque, PyObject *unused)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 PyObject *item;
210 block *prevblock;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000211
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000212 if (Py_SIZE(deque) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
214 return NULL;
215 }
216 assert(deque->leftblock != NULL);
217 item = deque->leftblock->data[deque->leftindex];
218 deque->leftindex++;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100219 Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 deque->state++;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (deque->leftindex == BLOCKLEN) {
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700223 if (Py_SIZE(deque)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 assert(deque->leftblock != deque->rightblock);
225 prevblock = deque->leftblock->rightlink;
226 freeblock(deque->leftblock);
Raymond Hettinger82df9252013-07-07 01:43:42 -1000227 CHECK_NOT_END(prevblock);
228 MARK_END(prevblock->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 deque->leftblock = prevblock;
230 deque->leftindex = 0;
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700231 } else {
232 assert(deque->leftblock == deque->rightblock);
233 assert(deque->leftindex == deque->rightindex+1);
234 /* re-center instead of freeing a block */
235 deque->leftindex = CENTER + 1;
236 deque->rightindex = CENTER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 }
238 }
239 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000240}
241
242PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
243
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800244/* The deque's size limit is d.maxlen. The limit can be zero or positive.
245 * If there is no limit, then d.maxlen == -1.
246 *
Raymond Hettingera4b13d02015-10-15 08:05:31 -0700247 * After an item is added to a deque, we check to see if the size has
248 * grown past the limit. If it has, we get the size back down to the limit
249 * by popping an item off of the opposite end. The methods that can
250 * trigger this are append(), appendleft(), extend(), and extendleft().
Raymond Hettingerd96db092015-10-11 22:34:48 -0700251 *
252 * The macro to check whether a deque needs to be trimmed uses a single
253 * unsigned test that returns true whenever 0 <= maxlen < Py_SIZE(deque).
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800254 */
255
Raymond Hettingerd96db092015-10-11 22:34:48 -0700256#define NEEDS_TRIM(deque, maxlen) ((size_t)(maxlen) < (size_t)(Py_SIZE(deque)))
Raymond Hettingerf30f5b92015-03-02 22:23:37 -0800257
Raymond Hettinger66953f02018-07-10 04:17:40 -0700258static inline int
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800259deque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000260{
Raymond Hettinger8dbbae22015-03-24 21:01:50 -0700261 if (deque->rightindex == BLOCKLEN - 1) {
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700262 block *b = newblock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 if (b == NULL)
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800264 return -1;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000265 b->leftlink = deque->rightblock;
266 CHECK_END(deque->rightblock->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 deque->rightblock->rightlink = b;
268 deque->rightblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000269 MARK_END(b->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 deque->rightindex = -1;
271 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100272 Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 deque->rightindex++;
274 deque->rightblock->data[deque->rightindex] = item;
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800275 if (NEEDS_TRIM(deque, maxlen)) {
Raymond Hettinger1286d142015-10-14 23:16:57 -0700276 PyObject *olditem = deque_popleft(deque, NULL);
277 Py_DECREF(olditem);
Raymond Hettinger0f43bb12015-10-20 00:03:33 -0700278 } else {
279 deque->state++;
Raymond Hettingerd96db092015-10-11 22:34:48 -0700280 }
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800281 return 0;
282}
283
284static PyObject *
285deque_append(dequeobject *deque, PyObject *item)
286{
287 Py_INCREF(item);
288 if (deque_append_internal(deque, item, deque->maxlen) < 0)
289 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_RETURN_NONE;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000291}
292
293PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
294
Raymond Hettinger66953f02018-07-10 04:17:40 -0700295static inline int
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800296deque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (deque->leftindex == 0) {
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700299 block *b = newblock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (b == NULL)
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800301 return -1;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000302 b->rightlink = deque->leftblock;
303 CHECK_END(deque->leftblock->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 deque->leftblock->leftlink = b;
305 deque->leftblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000306 MARK_END(b->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 deque->leftindex = BLOCKLEN;
308 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100309 Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 deque->leftindex--;
311 deque->leftblock->data[deque->leftindex] = item;
Raymond Hettingerd96db092015-10-11 22:34:48 -0700312 if (NEEDS_TRIM(deque, deque->maxlen)) {
Raymond Hettinger1286d142015-10-14 23:16:57 -0700313 PyObject *olditem = deque_pop(deque, NULL);
314 Py_DECREF(olditem);
Raymond Hettinger0f43bb12015-10-20 00:03:33 -0700315 } else {
316 deque->state++;
Raymond Hettingerd96db092015-10-11 22:34:48 -0700317 }
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800318 return 0;
319}
320
321static PyObject *
322deque_appendleft(dequeobject *deque, PyObject *item)
323{
324 Py_INCREF(item);
325 if (deque_appendleft_internal(deque, item, deque->maxlen) < 0)
326 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_RETURN_NONE;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000328}
329
330PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
331
Raymond Hettingerfd265f42015-10-02 23:17:33 -0700332static PyObject*
333finalize_iterator(PyObject *it)
334{
335 if (PyErr_Occurred()) {
336 if (PyErr_ExceptionMatches(PyExc_StopIteration))
337 PyErr_Clear();
338 else {
339 Py_DECREF(it);
340 return NULL;
341 }
342 }
343 Py_DECREF(it);
344 Py_RETURN_NONE;
345}
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347/* Run an iterator to exhaustion. Shortcut for
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000348 the extend/extendleft methods when maxlen == 0. */
349static PyObject*
350consume_iterator(PyObject *it)
351{
Raymond Hettingerfd265f42015-10-02 23:17:33 -0700352 PyObject *(*iternext)(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 PyObject *item;
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000354
Raymond Hettingerfd265f42015-10-02 23:17:33 -0700355 iternext = *Py_TYPE(it)->tp_iternext;
356 while ((item = iternext(it)) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 Py_DECREF(item);
358 }
Raymond Hettingerfd265f42015-10-02 23:17:33 -0700359 return finalize_iterator(it);
Raymond Hettinger060c7f62009-03-10 09:36:07 +0000360}
361
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000362static PyObject *
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000363deque_extend(dequeobject *deque, PyObject *iterable)
364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 PyObject *it, *item;
Raymond Hettinger7a845522015-09-26 01:30:51 -0700366 PyObject *(*iternext)(PyObject *);
Raymond Hettinger6b1e1132015-10-11 09:43:50 -0700367 Py_ssize_t maxlen = deque->maxlen;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* Handle case where id(deque) == id(iterable) */
370 if ((PyObject *)deque == iterable) {
371 PyObject *result;
372 PyObject *s = PySequence_List(iterable);
373 if (s == NULL)
374 return NULL;
375 result = deque_extend(deque, s);
376 Py_DECREF(s);
377 return result;
378 }
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000379
Raymond Hettingerd79d5b12016-03-04 09:55:07 -0800380 it = PyObject_GetIter(iterable);
381 if (it == NULL)
382 return NULL;
383
384 if (maxlen == 0)
385 return consume_iterator(it);
386
Raymond Hettingerd9c116c2013-07-09 00:13:21 -0700387 /* Space saving heuristic. Start filling from the left */
388 if (Py_SIZE(deque) == 0) {
389 assert(deque->leftblock == deque->rightblock);
390 assert(deque->leftindex == deque->rightindex+1);
391 deque->leftindex = 1;
392 deque->rightindex = 0;
393 }
394
Raymond Hettinger7a845522015-09-26 01:30:51 -0700395 iternext = *Py_TYPE(it)->tp_iternext;
396 while ((item = iternext(it)) != NULL) {
Raymond Hettinger66953f02018-07-10 04:17:40 -0700397 if (deque_append_internal(deque, item, maxlen) == -1) {
398 Py_DECREF(item);
399 Py_DECREF(it);
400 return NULL;
Raymond Hettinger6b1e1132015-10-11 09:43:50 -0700401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 }
Raymond Hettingerfd265f42015-10-02 23:17:33 -0700403 return finalize_iterator(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000404}
405
Tim Peters1065f752004-10-01 01:03:29 +0000406PyDoc_STRVAR(extend_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000407"Extend the right side of the deque with elements from the iterable");
408
409static PyObject *
410deque_extendleft(dequeobject *deque, PyObject *iterable)
411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 PyObject *it, *item;
Raymond Hettinger7a845522015-09-26 01:30:51 -0700413 PyObject *(*iternext)(PyObject *);
Raymond Hettinger6b1e1132015-10-11 09:43:50 -0700414 Py_ssize_t maxlen = deque->maxlen;
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 /* Handle case where id(deque) == id(iterable) */
417 if ((PyObject *)deque == iterable) {
418 PyObject *result;
419 PyObject *s = PySequence_List(iterable);
420 if (s == NULL)
421 return NULL;
422 result = deque_extendleft(deque, s);
423 Py_DECREF(s);
424 return result;
425 }
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000426
Raymond Hettingerd79d5b12016-03-04 09:55:07 -0800427 it = PyObject_GetIter(iterable);
428 if (it == NULL)
429 return NULL;
430
431 if (maxlen == 0)
432 return consume_iterator(it);
433
Raymond Hettingerd9c116c2013-07-09 00:13:21 -0700434 /* Space saving heuristic. Start filling from the right */
435 if (Py_SIZE(deque) == 0) {
436 assert(deque->leftblock == deque->rightblock);
437 assert(deque->leftindex == deque->rightindex+1);
438 deque->leftindex = BLOCKLEN - 1;
439 deque->rightindex = BLOCKLEN - 2;
440 }
441
Raymond Hettinger7a845522015-09-26 01:30:51 -0700442 iternext = *Py_TYPE(it)->tp_iternext;
443 while ((item = iternext(it)) != NULL) {
Raymond Hettinger66953f02018-07-10 04:17:40 -0700444 if (deque_appendleft_internal(deque, item, maxlen) == -1) {
445 Py_DECREF(item);
446 Py_DECREF(it);
447 return NULL;
Raymond Hettinger6b1e1132015-10-11 09:43:50 -0700448 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 }
Raymond Hettingerfd265f42015-10-02 23:17:33 -0700450 return finalize_iterator(it);
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000451}
452
Tim Peters1065f752004-10-01 01:03:29 +0000453PyDoc_STRVAR(extendleft_doc,
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000454"Extend the left side of the deque with elements from the iterable");
455
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000456static PyObject *
457deque_inplace_concat(dequeobject *deque, PyObject *other)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyObject *result;
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 result = deque_extend(deque, other);
462 if (result == NULL)
463 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 Py_INCREF(deque);
Raymond Hettinger2b2b7532015-09-05 17:05:52 -0700465 Py_DECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 return (PyObject *)deque;
Raymond Hettinger3f9afd82009-12-10 03:03:02 +0000467}
468
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700469static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530470deque_copy(PyObject *deque, PyObject *Py_UNUSED(ignored))
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700471{
Oren Milman24bd50b2018-09-11 21:46:55 +0300472 PyObject *result;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700473 dequeobject *old_deque = (dequeobject *)deque;
Andy Lesterdffe4c02020-03-04 07:15:20 -0600474 if (Py_IS_TYPE(deque, &deque_type)) {
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700475 dequeobject *new_deque;
476 PyObject *rv;
477
478 new_deque = (dequeobject *)deque_new(&deque_type, (PyObject *)NULL, (PyObject *)NULL);
479 if (new_deque == NULL)
480 return NULL;
481 new_deque->maxlen = old_deque->maxlen;
482 /* Fast path for the deque_repeat() common case where len(deque) == 1 */
Raymond Hettinger0443ac22015-10-05 22:52:37 -0400483 if (Py_SIZE(deque) == 1) {
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700484 PyObject *item = old_deque->leftblock->data[old_deque->leftindex];
485 rv = deque_append(new_deque, item);
486 } else {
487 rv = deque_extend(new_deque, deque);
488 }
489 if (rv != NULL) {
490 Py_DECREF(rv);
491 return (PyObject *)new_deque;
492 }
493 Py_DECREF(new_deque);
494 return NULL;
495 }
496 if (old_deque->maxlen < 0)
Petr Viktorinffd97532020-02-11 17:46:57 +0100497 result = PyObject_CallOneArg((PyObject *)(Py_TYPE(deque)), deque);
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700498 else
Oren Milman24bd50b2018-09-11 21:46:55 +0300499 result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
500 deque, old_deque->maxlen, NULL);
501 if (result != NULL && !PyObject_TypeCheck(result, &deque_type)) {
502 PyErr_Format(PyExc_TypeError,
503 "%.200s() must return a deque, not %.200s",
504 Py_TYPE(deque)->tp_name, Py_TYPE(result)->tp_name);
505 Py_DECREF(result);
506 return NULL;
507 }
508 return result;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700509}
510
511PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
Raymond Hettinger41290a62015-03-31 08:12:23 -0700512
513static PyObject *
514deque_concat(dequeobject *deque, PyObject *other)
515{
Benjamin Peterson1a629212015-04-04 10:52:36 -0400516 PyObject *new_deque, *result;
Raymond Hettinger41290a62015-03-31 08:12:23 -0700517 int rv;
518
519 rv = PyObject_IsInstance(other, (PyObject *)&deque_type);
520 if (rv <= 0) {
521 if (rv == 0) {
522 PyErr_Format(PyExc_TypeError,
523 "can only concatenate deque (not \"%.200s\") to deque",
Victor Stinnerdaa97562020-02-07 03:37:06 +0100524 Py_TYPE(other)->tp_name);
Raymond Hettinger41290a62015-03-31 08:12:23 -0700525 }
526 return NULL;
527 }
528
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530529 new_deque = deque_copy((PyObject *)deque, NULL);
Raymond Hettinger41290a62015-03-31 08:12:23 -0700530 if (new_deque == NULL)
531 return NULL;
Benjamin Peterson1a629212015-04-04 10:52:36 -0400532 result = deque_extend((dequeobject *)new_deque, other);
533 if (result == NULL) {
534 Py_DECREF(new_deque);
535 return NULL;
536 }
537 Py_DECREF(result);
538 return new_deque;
Raymond Hettinger41290a62015-03-31 08:12:23 -0700539}
540
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300541static int
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700542deque_clear(dequeobject *deque)
543{
544 block *b;
545 block *prevblock;
546 block *leftblock;
547 Py_ssize_t leftindex;
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800548 Py_ssize_t n, m;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700549 PyObject *item;
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800550 PyObject **itemptr, **limit;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700551
Raymond Hettinger38031142015-09-29 22:45:05 -0700552 if (Py_SIZE(deque) == 0)
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300553 return 0;
Raymond Hettinger38031142015-09-29 22:45:05 -0700554
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700555 /* During the process of clearing a deque, decrefs can cause the
556 deque to mutate. To avoid fatal confusion, we have to make the
557 deque empty before clearing the blocks and never refer to
558 anything via deque->ref while clearing. (This is the same
559 technique used for clearing lists, sets, and dicts.)
560
561 Making the deque empty requires allocating a new empty block. In
562 the unlikely event that memory is full, we fall back to an
563 alternate method that doesn't require a new block. Repeating
564 pops in a while-loop is slower, possibly re-entrant (and a clever
565 adversary could cause it to never terminate).
566 */
567
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700568 b = newblock();
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700569 if (b == NULL) {
570 PyErr_Clear();
571 goto alternate_method;
572 }
573
574 /* Remember the old size, leftblock, and leftindex */
Raymond Hettinger6f86a332016-03-02 00:30:58 -0800575 n = Py_SIZE(deque);
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700576 leftblock = deque->leftblock;
577 leftindex = deque->leftindex;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700578
579 /* Set the deque to be empty using the newly allocated block */
580 MARK_END(b->leftlink);
581 MARK_END(b->rightlink);
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100582 Py_SET_SIZE(deque, 0);
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700583 deque->leftblock = b;
584 deque->rightblock = b;
585 deque->leftindex = CENTER + 1;
586 deque->rightindex = CENTER;
587 deque->state++;
588
589 /* Now the old size, leftblock, and leftindex are disconnected from
590 the empty deque and we can use them to decref the pointers.
591 */
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800592 m = (BLOCKLEN - leftindex > n) ? n : BLOCKLEN - leftindex;
Raymond Hettinger589106b2016-03-02 00:06:21 -0800593 itemptr = &leftblock->data[leftindex];
Raymond Hettinger6f86a332016-03-02 00:30:58 -0800594 limit = itemptr + m;
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800595 n -= m;
596 while (1) {
597 if (itemptr == limit) {
598 if (n == 0)
599 break;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700600 CHECK_NOT_END(leftblock->rightlink);
601 prevblock = leftblock;
602 leftblock = leftblock->rightlink;
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800603 m = (n > BLOCKLEN) ? BLOCKLEN : n;
Raymond Hettinger589106b2016-03-02 00:06:21 -0800604 itemptr = leftblock->data;
Raymond Hettinger6f86a332016-03-02 00:30:58 -0800605 limit = itemptr + m;
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800606 n -= m;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700607 freeblock(prevblock);
608 }
Raymond Hettingerd84ec222016-01-24 09:12:06 -0800609 item = *(itemptr++);
610 Py_DECREF(item);
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700611 }
612 CHECK_END(leftblock->rightlink);
613 freeblock(leftblock);
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300614 return 0;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700615
616 alternate_method:
617 while (Py_SIZE(deque)) {
618 item = deque_pop(deque, NULL);
619 assert (item != NULL);
620 Py_DECREF(item);
621 }
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300622 return 0;
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700623}
624
625static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530626deque_clearmethod(dequeobject *deque, PyObject *Py_UNUSED(ignored))
Raymond Hettinger8299e9b2015-09-26 21:31:23 -0700627{
628 deque_clear(deque);
629 Py_RETURN_NONE;
630}
631
632PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
Raymond Hettinger41290a62015-03-31 08:12:23 -0700633
634static PyObject *
Raymond Hettinger41290a62015-03-31 08:12:23 -0700635deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
636{
Raymond Hettingerc22eee62015-09-26 02:14:50 -0700637 Py_ssize_t i, m, size;
Raymond Hettinger41290a62015-03-31 08:12:23 -0700638 PyObject *seq;
639 PyObject *rv;
640
641 size = Py_SIZE(deque);
642 if (size == 0 || n == 1) {
643 Py_INCREF(deque);
644 return (PyObject *)deque;
645 }
646
647 if (n <= 0) {
648 deque_clear(deque);
649 Py_INCREF(deque);
650 return (PyObject *)deque;
651 }
652
Raymond Hettinger41290a62015-03-31 08:12:23 -0700653 if (size == 1) {
654 /* common case, repeating a single element */
655 PyObject *item = deque->leftblock->data[deque->leftindex];
656
Raymond Hettingera7f630092015-10-10 23:56:02 -0400657 if (deque->maxlen >= 0 && n > deque->maxlen)
Raymond Hettinger41290a62015-03-31 08:12:23 -0700658 n = deque->maxlen;
659
Raymond Hettinger67c78b52015-09-12 11:00:20 -0400660 deque->state++;
Raymond Hettingerad262252015-09-14 01:03:04 -0400661 for (i = 0 ; i < n-1 ; ) {
Raymond Hettinger67c78b52015-09-12 11:00:20 -0400662 if (deque->rightindex == BLOCKLEN - 1) {
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700663 block *b = newblock();
Raymond Hettinger67c78b52015-09-12 11:00:20 -0400664 if (b == NULL) {
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100665 Py_SET_SIZE(deque, Py_SIZE(deque) + i);
Raymond Hettinger67c78b52015-09-12 11:00:20 -0400666 return NULL;
667 }
668 b->leftlink = deque->rightblock;
669 CHECK_END(deque->rightblock->rightlink);
670 deque->rightblock->rightlink = b;
671 deque->rightblock = b;
672 MARK_END(b->rightlink);
673 deque->rightindex = -1;
674 }
Raymond Hettingerc22eee62015-09-26 02:14:50 -0700675 m = n - 1 - i;
676 if (m > BLOCKLEN - 1 - deque->rightindex)
677 m = BLOCKLEN - 1 - deque->rightindex;
678 i += m;
679 while (m--) {
Raymond Hettingerad262252015-09-14 01:03:04 -0400680 deque->rightindex++;
681 Py_INCREF(item);
682 deque->rightblock->data[deque->rightindex] = item;
683 }
Raymond Hettinger41290a62015-03-31 08:12:23 -0700684 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100685 Py_SET_SIZE(deque, Py_SIZE(deque) + i);
Raymond Hettinger41290a62015-03-31 08:12:23 -0700686 Py_INCREF(deque);
687 return (PyObject *)deque;
688 }
689
Raymond Hettinger20151f52015-10-16 22:47:29 -0700690 if ((size_t)size > PY_SSIZE_T_MAX / (size_t)n) {
Raymond Hettingerf5d72f32015-09-09 22:39:44 -0400691 return PyErr_NoMemory();
692 }
693
Raymond Hettinger41290a62015-03-31 08:12:23 -0700694 seq = PySequence_List((PyObject *)deque);
695 if (seq == NULL)
696 return seq;
697
Louie Lu357bad72017-02-24 11:59:49 +0800698 /* Reduce the number of repetitions when maxlen would be exceeded */
699 if (deque->maxlen >= 0 && n * size > deque->maxlen)
700 n = (deque->maxlen + size - 1) / size;
701
Raymond Hettinger41290a62015-03-31 08:12:23 -0700702 for (i = 0 ; i < n-1 ; i++) {
703 rv = deque_extend(deque, seq);
704 if (rv == NULL) {
705 Py_DECREF(seq);
706 return NULL;
707 }
708 Py_DECREF(rv);
709 }
710 Py_INCREF(deque);
711 Py_DECREF(seq);
712 return (PyObject *)deque;
713}
714
Raymond Hettingerf5d72f32015-09-09 22:39:44 -0400715static PyObject *
716deque_repeat(dequeobject *deque, Py_ssize_t n)
717{
718 dequeobject *new_deque;
Raymond Hettinger95e2cc52015-09-13 02:41:18 -0400719 PyObject *rv;
Raymond Hettingerf5d72f32015-09-09 22:39:44 -0400720
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530721 new_deque = (dequeobject *)deque_copy((PyObject *) deque, NULL);
Raymond Hettingerf5d72f32015-09-09 22:39:44 -0400722 if (new_deque == NULL)
723 return NULL;
Raymond Hettinger95e2cc52015-09-13 02:41:18 -0400724 rv = deque_inplace_repeat(new_deque, n);
725 Py_DECREF(new_deque);
726 return rv;
Raymond Hettingerf5d72f32015-09-09 22:39:44 -0400727}
728
Raymond Hettinger54023152014-04-23 00:58:48 -0700729/* The rotate() method is part of the public API and is used internally
730as a primitive for other methods.
731
732Rotation by 1 or -1 is a common case, so any optimizations for high
733volume rotations should take care not to penalize the common case.
734
735Conceptually, a rotate by one is equivalent to a pop on one side and an
736append on the other. However, a pop/append pair is unnecessarily slow
Martin Panterd2ad5712015-11-02 04:20:33 +0000737because it requires an incref/decref pair for an object located randomly
Raymond Hettinger54023152014-04-23 00:58:48 -0700738in memory. It is better to just move the object pointer from one block
739to the next without changing the reference count.
740
741When moving batches of pointers, it is tempting to use memcpy() but that
742proved to be slower than a simple loop for a variety of reasons.
743Memcpy() cannot know in advance that we're copying pointers instead of
744bytes, that the source and destination are pointer aligned and
745non-overlapping, that moving just one pointer is a common case, that we
746never need to move more than BLOCKLEN pointers, and that at least one
747pointer is always moved.
748
749For high volume rotations, newblock() and freeblock() are never called
750more than once. Previously emptied blocks are immediately reused as a
751destination block. If a block is left-over at the end, it is freed.
752*/
753
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000754static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000755_deque_rotate(dequeobject *deque, Py_ssize_t n)
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000756{
Raymond Hettinger3959af92013-07-13 02:34:08 -0700757 block *b = NULL;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700758 block *leftblock = deque->leftblock;
759 block *rightblock = deque->rightblock;
760 Py_ssize_t leftindex = deque->leftindex;
761 Py_ssize_t rightindex = deque->rightindex;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000762 Py_ssize_t len=Py_SIZE(deque), halflen=len>>1;
Raymond Hettinger3959af92013-07-13 02:34:08 -0700763 int rv = -1;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000764
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800765 if (len <= 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 return 0;
767 if (n > halflen || n < -halflen) {
768 n %= len;
769 if (n > halflen)
770 n -= len;
771 else if (n < -halflen)
772 n += len;
773 }
Raymond Hettinger1f0044c2013-02-05 01:30:46 -0500774 assert(len > 1);
Raymond Hettingera4409c12013-02-04 00:08:12 -0500775 assert(-halflen <= n && n <= halflen);
Raymond Hettinger231ee4d2013-02-02 11:24:43 -0800776
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800777 deque->state++;
Raymond Hettinger1f0044c2013-02-05 01:30:46 -0500778 while (n > 0) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700779 if (leftindex == 0) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700780 if (b == NULL) {
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700781 b = newblock();
Raymond Hettinger3959af92013-07-13 02:34:08 -0700782 if (b == NULL)
783 goto done;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700784 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000785 b->rightlink = leftblock;
786 CHECK_END(leftblock->leftlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700787 leftblock->leftlink = b;
788 leftblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000789 MARK_END(b->leftlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700790 leftindex = BLOCKLEN;
Raymond Hettinger3959af92013-07-13 02:34:08 -0700791 b = NULL;
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800792 }
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700793 assert(leftindex > 0);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700794 {
795 PyObject **src, **dest;
796 Py_ssize_t m = n;
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800797
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700798 if (m > rightindex + 1)
799 m = rightindex + 1;
800 if (m > leftindex)
801 m = leftindex;
802 assert (m > 0 && m <= len);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700803 rightindex -= m;
804 leftindex -= m;
Raymond Hettinger0e259f12015-02-01 22:53:41 -0800805 src = &rightblock->data[rightindex + 1];
806 dest = &leftblock->data[leftindex];
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700807 n -= m;
Raymond Hettinger840533b2013-07-13 17:03:58 -0700808 do {
Raymond Hettinger0e259f12015-02-01 22:53:41 -0800809 *(dest++) = *(src++);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700810 } while (--m);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700811 }
Raymond Hettingerd3d2b2c2015-09-21 23:41:56 -0700812 if (rightindex < 0) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700813 assert(leftblock != rightblock);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700814 assert(b == NULL);
Raymond Hettinger3959af92013-07-13 02:34:08 -0700815 b = rightblock;
Raymond Hettingerb97cc492013-07-21 01:51:07 -0700816 CHECK_NOT_END(rightblock->leftlink);
817 rightblock = rightblock->leftlink;
818 MARK_END(rightblock->rightlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700819 rightindex = BLOCKLEN - 1;
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800820 }
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800821 }
Raymond Hettinger1f0044c2013-02-05 01:30:46 -0500822 while (n < 0) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700823 if (rightindex == BLOCKLEN - 1) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700824 if (b == NULL) {
Raymond Hettingerdb41fd42015-10-22 22:48:16 -0700825 b = newblock();
Raymond Hettinger3959af92013-07-13 02:34:08 -0700826 if (b == NULL)
827 goto done;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700828 }
Raymond Hettinger82df9252013-07-07 01:43:42 -1000829 b->leftlink = rightblock;
830 CHECK_END(rightblock->rightlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700831 rightblock->rightlink = b;
832 rightblock = b;
Raymond Hettinger82df9252013-07-07 01:43:42 -1000833 MARK_END(b->rightlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700834 rightindex = -1;
Raymond Hettinger3959af92013-07-13 02:34:08 -0700835 b = NULL;
Raymond Hettinger464d89b2013-01-11 22:29:50 -0800836 }
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700837 assert (rightindex < BLOCKLEN - 1);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700838 {
839 PyObject **src, **dest;
840 Py_ssize_t m = -n;
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800841
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700842 if (m > BLOCKLEN - leftindex)
843 m = BLOCKLEN - leftindex;
844 if (m > BLOCKLEN - 1 - rightindex)
845 m = BLOCKLEN - 1 - rightindex;
846 assert (m > 0 && m <= len);
847 src = &leftblock->data[leftindex];
848 dest = &rightblock->data[rightindex + 1];
849 leftindex += m;
850 rightindex += m;
851 n += m;
Raymond Hettinger840533b2013-07-13 17:03:58 -0700852 do {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700853 *(dest++) = *(src++);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700854 } while (--m);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700855 }
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700856 if (leftindex == BLOCKLEN) {
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700857 assert(leftblock != rightblock);
Raymond Hettinger840533b2013-07-13 17:03:58 -0700858 assert(b == NULL);
Raymond Hettinger3959af92013-07-13 02:34:08 -0700859 b = leftblock;
Raymond Hettingerb97cc492013-07-21 01:51:07 -0700860 CHECK_NOT_END(leftblock->rightlink);
861 leftblock = leftblock->rightlink;
862 MARK_END(leftblock->leftlink);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700863 leftindex = 0;
Raymond Hettinger21777ac2013-02-02 09:56:08 -0800864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 }
Raymond Hettinger3959af92013-07-13 02:34:08 -0700866 rv = 0;
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700867done:
Raymond Hettinger3959af92013-07-13 02:34:08 -0700868 if (b != NULL)
869 freeblock(b);
Raymond Hettinger20b0f872013-06-23 15:44:33 -0700870 deque->leftblock = leftblock;
871 deque->rightblock = rightblock;
872 deque->leftindex = leftindex;
873 deque->rightindex = rightindex;
874
875 return rv;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000876}
877
878static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200879deque_rotate(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_ssize_t n=1;
Raymond Hettingerdcb9d942004-10-09 16:02:18 +0000882
Victor Stinnerdd407d52017-02-06 16:06:49 +0100883 if (!_PyArg_ParseStack(args, nargs, "|n:rotate", &n)) {
884 return NULL;
885 }
886
Raymond Hettinger6921c132015-03-21 02:03:40 -0700887 if (!_deque_rotate(deque, n))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_RETURN_NONE;
889 return NULL;
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000890}
891
Tim Peters1065f752004-10-01 01:03:29 +0000892PyDoc_STRVAR(rotate_doc,
Raymond Hettingeree33b272004-02-08 04:05:26 +0000893"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000894
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000895static PyObject *
896deque_reverse(dequeobject *deque, PyObject *unused)
897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 block *leftblock = deque->leftblock;
899 block *rightblock = deque->rightblock;
900 Py_ssize_t leftindex = deque->leftindex;
901 Py_ssize_t rightindex = deque->rightindex;
Raymond Hettingercfe5b6c2015-07-20 00:25:50 -0400902 Py_ssize_t n = Py_SIZE(deque) >> 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 PyObject *tmp;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000904
Raymond Hettingere1b02872017-09-04 16:07:06 -0700905 while (--n >= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* Validate that pointers haven't met in the middle */
907 assert(leftblock != rightblock || leftindex < rightindex);
Raymond Hettinger82df9252013-07-07 01:43:42 -1000908 CHECK_NOT_END(leftblock);
909 CHECK_NOT_END(rightblock);
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* Swap */
912 tmp = leftblock->data[leftindex];
913 leftblock->data[leftindex] = rightblock->data[rightindex];
914 rightblock->data[rightindex] = tmp;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 /* Advance left block/index pair */
917 leftindex++;
918 if (leftindex == BLOCKLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 leftblock = leftblock->rightlink;
920 leftindex = 0;
921 }
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 /* Step backwards with the right block/index pair */
924 rightindex--;
Raymond Hettingerd3d2b2c2015-09-21 23:41:56 -0700925 if (rightindex < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 rightblock = rightblock->leftlink;
927 rightindex = BLOCKLEN - 1;
928 }
929 }
930 Py_RETURN_NONE;
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000931}
932
933PyDoc_STRVAR(reverse_doc,
934"D.reverse() -- reverse *IN PLACE*");
935
Raymond Hettinger44459de2010-04-03 23:20:46 +0000936static PyObject *
937deque_count(dequeobject *deque, PyObject *v)
938{
Raymond Hettingerf3a67b72013-07-06 17:49:06 -1000939 block *b = deque->leftblock;
940 Py_ssize_t index = deque->leftindex;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -1000941 Py_ssize_t n = Py_SIZE(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 Py_ssize_t count = 0;
Raymond Hettingerf9d9c792015-03-02 22:47:46 -0800943 size_t start_state = deque->state;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyObject *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 int cmp;
Raymond Hettinger44459de2010-04-03 23:20:46 +0000946
Raymond Hettingere1b02872017-09-04 16:07:06 -0700947 while (--n >= 0) {
Raymond Hettinger82df9252013-07-07 01:43:42 -1000948 CHECK_NOT_END(b);
Raymond Hettingerf3a67b72013-07-06 17:49:06 -1000949 item = b->data[index];
sweeneydec6dedde2020-02-09 03:16:43 -0500950 Py_INCREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
sweeneydec6dedde2020-02-09 03:16:43 -0500952 Py_DECREF(item);
Raymond Hettinger2b0d6462015-09-23 19:15:44 -0700953 if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 return NULL;
Raymond Hettinger2b0d6462015-09-23 19:15:44 -0700955 count += cmp;
Raymond Hettinger44459de2010-04-03 23:20:46 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 if (start_state != deque->state) {
958 PyErr_SetString(PyExc_RuntimeError,
959 "deque mutated during iteration");
960 return NULL;
961 }
Raymond Hettinger44459de2010-04-03 23:20:46 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* Advance left block/index pair */
Raymond Hettingerf3a67b72013-07-06 17:49:06 -1000964 index++;
965 if (index == BLOCKLEN) {
966 b = b->rightlink;
967 index = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 }
969 }
970 return PyLong_FromSsize_t(count);
Raymond Hettinger44459de2010-04-03 23:20:46 +0000971}
972
973PyDoc_STRVAR(count_doc,
974"D.count(value) -> integer -- return number of occurrences of value");
975
Raymond Hettinger39dadf72015-03-20 16:38:56 -0700976static int
977deque_contains(dequeobject *deque, PyObject *v)
978{
979 block *b = deque->leftblock;
980 Py_ssize_t index = deque->leftindex;
981 Py_ssize_t n = Py_SIZE(deque);
Raymond Hettinger39dadf72015-03-20 16:38:56 -0700982 size_t start_state = deque->state;
983 PyObject *item;
984 int cmp;
985
Raymond Hettingere1b02872017-09-04 16:07:06 -0700986 while (--n >= 0) {
Raymond Hettinger39dadf72015-03-20 16:38:56 -0700987 CHECK_NOT_END(b);
988 item = b->data[index];
sweeneydec6dedde2020-02-09 03:16:43 -0500989 Py_INCREF(item);
Raymond Hettinger39dadf72015-03-20 16:38:56 -0700990 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
sweeneydec6dedde2020-02-09 03:16:43 -0500991 Py_DECREF(item);
Raymond Hettinger39dadf72015-03-20 16:38:56 -0700992 if (cmp) {
993 return cmp;
994 }
995 if (start_state != deque->state) {
996 PyErr_SetString(PyExc_RuntimeError,
997 "deque mutated during iteration");
998 return -1;
999 }
1000 index++;
1001 if (index == BLOCKLEN) {
1002 b = b->rightlink;
1003 index = 0;
1004 }
1005 }
1006 return 0;
1007}
1008
Martin v. Löwis18e16552006-02-15 17:27:45 +00001009static Py_ssize_t
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001010deque_len(dequeobject *deque)
1011{
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001012 return Py_SIZE(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001013}
1014
Raymond Hettinger4aec61e2005-03-18 21:20:23 +00001015static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001016deque_index(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001017{
Raymond Hettinger67b97b82015-11-01 23:57:37 -05001018 Py_ssize_t i, n, start=0, stop=Py_SIZE(deque);
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001019 PyObject *v, *item;
1020 block *b = deque->leftblock;
1021 Py_ssize_t index = deque->leftindex;
1022 size_t start_state = deque->state;
Raymond Hettinger67b97b82015-11-01 23:57:37 -05001023 int cmp;
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001024
Victor Stinnerdd407d52017-02-06 16:06:49 +01001025 if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index", &v,
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03001026 _PyEval_SliceIndexNotNone, &start,
1027 _PyEval_SliceIndexNotNone, &stop)) {
Victor Stinnerdd407d52017-02-06 16:06:49 +01001028 return NULL;
1029 }
1030
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001031 if (start < 0) {
1032 start += Py_SIZE(deque);
1033 if (start < 0)
1034 start = 0;
1035 }
1036 if (stop < 0) {
1037 stop += Py_SIZE(deque);
1038 if (stop < 0)
1039 stop = 0;
1040 }
Raymond Hettinger87674ec2015-08-26 08:08:38 -07001041 if (stop > Py_SIZE(deque))
1042 stop = Py_SIZE(deque);
Raymond Hettinger67b97b82015-11-01 23:57:37 -05001043 if (start > stop)
1044 start = stop;
1045 assert(0 <= start && start <= stop && stop <= Py_SIZE(deque));
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001046
Raymond Hettingerb46ad542018-09-21 01:46:41 -07001047 for (i=0 ; i < start - BLOCKLEN ; i += BLOCKLEN) {
1048 b = b->rightlink;
1049 }
1050 for ( ; i < start ; i++) {
Raymond Hettinger67b97b82015-11-01 23:57:37 -05001051 index++;
1052 if (index == BLOCKLEN) {
1053 b = b->rightlink;
1054 index = 0;
1055 }
1056 }
1057
Raymond Hettingere1b02872017-09-04 16:07:06 -07001058 n = stop - i;
1059 while (--n >= 0) {
Raymond Hettinger67b97b82015-11-01 23:57:37 -05001060 CHECK_NOT_END(b);
1061 item = b->data[index];
1062 cmp = PyObject_RichCompareBool(item, v, Py_EQ);
1063 if (cmp > 0)
Raymond Hettingere1b02872017-09-04 16:07:06 -07001064 return PyLong_FromSsize_t(stop - n - 1);
Raymond Hettingerdf8f5b52015-11-02 07:27:40 -05001065 if (cmp < 0)
Raymond Hettinger67b97b82015-11-01 23:57:37 -05001066 return NULL;
1067 if (start_state != deque->state) {
1068 PyErr_SetString(PyExc_RuntimeError,
1069 "deque mutated during iteration");
1070 return NULL;
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001071 }
1072 index++;
1073 if (index == BLOCKLEN) {
1074 b = b->rightlink;
1075 index = 0;
1076 }
1077 }
1078 PyErr_Format(PyExc_ValueError, "%R is not in deque", v);
1079 return NULL;
1080}
1081
1082PyDoc_STRVAR(index_doc,
1083"D.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
1084"Raises ValueError if the value is not present.");
1085
Raymond Hettinger551350a2015-03-24 00:19:53 -07001086/* insert(), remove(), and delitem() are implemented in terms of
1087 rotate() for simplicity and reasonable performance near the end
1088 points. If for some reason these methods become popular, it is not
1089 hard to re-implement this using direct data movement (similar to
1090 the code used in list slice assignments) and achieve a performance
Raymond Hettingerfef9c1b2015-03-24 21:12:57 -07001091 boost (by moving each pointer only once instead of twice).
Raymond Hettinger551350a2015-03-24 00:19:53 -07001092*/
1093
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001094static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001095deque_insert(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001096{
1097 Py_ssize_t index;
1098 Py_ssize_t n = Py_SIZE(deque);
1099 PyObject *value;
1100 PyObject *rv;
1101
Victor Stinnerdd407d52017-02-06 16:06:49 +01001102 if (!_PyArg_ParseStack(args, nargs, "nO:insert", &index, &value)) {
1103 return NULL;
1104 }
1105
Raymond Hettinger37434322016-01-26 21:44:16 -08001106 if (deque->maxlen == Py_SIZE(deque)) {
Raymond Hettingera6389712016-02-01 21:21:19 -08001107 PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
1108 return NULL;
Raymond Hettinger37434322016-01-26 21:44:16 -08001109 }
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001110 if (index >= n)
1111 return deque_append(deque, value);
1112 if (index <= -n || index == 0)
1113 return deque_appendleft(deque, value);
1114 if (_deque_rotate(deque, -index))
1115 return NULL;
1116 if (index < 0)
1117 rv = deque_append(deque, value);
1118 else
1119 rv = deque_appendleft(deque, value);
1120 if (rv == NULL)
1121 return NULL;
1122 Py_DECREF(rv);
1123 if (_deque_rotate(deque, index))
1124 return NULL;
1125 Py_RETURN_NONE;
1126}
1127
1128PyDoc_STRVAR(insert_doc,
1129"D.insert(index, object) -- insert object before index");
1130
1131static PyObject *
Raymond Hettinger4aec61e2005-03-18 21:20:23 +00001132deque_remove(dequeobject *deque, PyObject *value)
1133{
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001134 Py_ssize_t i, n=Py_SIZE(deque);
Raymond Hettinger4aec61e2005-03-18 21:20:23 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 for (i=0 ; i<n ; i++) {
1137 PyObject *item = deque->leftblock->data[deque->leftindex];
1138 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Raymond Hettingerd73202c2005-03-19 00:00:51 +00001139
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001140 if (Py_SIZE(deque) != n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 PyErr_SetString(PyExc_IndexError,
1142 "deque mutated during remove().");
1143 return NULL;
1144 }
1145 if (cmp > 0) {
1146 PyObject *tgt = deque_popleft(deque, NULL);
1147 assert (tgt != NULL);
Raymond Hettinger6921c132015-03-21 02:03:40 -07001148 if (_deque_rotate(deque, i))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 return NULL;
Raymond Hettingerac13ad62015-03-21 01:53:16 -07001150 Py_DECREF(tgt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 Py_RETURN_NONE;
1152 }
1153 else if (cmp < 0) {
1154 _deque_rotate(deque, i);
1155 return NULL;
1156 }
1157 _deque_rotate(deque, -1);
1158 }
1159 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
1160 return NULL;
Raymond Hettinger4aec61e2005-03-18 21:20:23 +00001161}
1162
1163PyDoc_STRVAR(remove_doc,
1164"D.remove(value) -- remove first occurrence of value.");
1165
Raymond Hettinger3c186ba2015-03-02 21:45:02 -08001166static int
1167valid_index(Py_ssize_t i, Py_ssize_t limit)
1168{
Raymond Hettinger12f896c2015-07-31 12:03:20 -07001169 /* The cast to size_t lets us use just a single comparison
Raymond Hettinger3c186ba2015-03-02 21:45:02 -08001170 to check whether i is in the range: 0 <= i < limit */
1171 return (size_t) i < (size_t) limit;
1172}
1173
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001174static PyObject *
Benjamin Petersond6313712008-07-31 16:23:04 +00001175deque_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 block *b;
1178 PyObject *item;
1179 Py_ssize_t n, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001180
Raymond Hettinger3c186ba2015-03-02 21:45:02 -08001181 if (!valid_index(i, Py_SIZE(deque))) {
1182 PyErr_SetString(PyExc_IndexError, "deque index out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 return NULL;
1184 }
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (i == 0) {
1187 i = deque->leftindex;
1188 b = deque->leftblock;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001189 } else if (i == Py_SIZE(deque) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 i = deque->rightindex;
1191 b = deque->rightblock;
1192 } else {
1193 i += deque->leftindex;
Raymond Hettingerc2083082015-02-28 23:29:16 -08001194 n = (Py_ssize_t)((size_t) i / BLOCKLEN);
1195 i = (Py_ssize_t)((size_t) i % BLOCKLEN);
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001196 if (index < (Py_SIZE(deque) >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 b = deque->leftblock;
Raymond Hettingere1b02872017-09-04 16:07:06 -07001198 while (--n >= 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 b = b->rightlink;
1200 } else {
Raymond Hettinger63d1ff22015-02-28 07:41:30 -08001201 n = (Py_ssize_t)(
Raymond Hettingerc2083082015-02-28 23:29:16 -08001202 ((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
Raymond Hettinger63d1ff22015-02-28 07:41:30 -08001203 / BLOCKLEN - n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 b = deque->rightblock;
Raymond Hettingere1b02872017-09-04 16:07:06 -07001205 while (--n >= 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 b = b->leftlink;
1207 }
1208 }
1209 item = b->data[i];
1210 Py_INCREF(item);
1211 return item;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001212}
1213
1214static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001215deque_del_item(dequeobject *deque, Py_ssize_t i)
Raymond Hettinger0e371f22004-05-12 20:55:56 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PyObject *item;
Raymond Hettingerac13ad62015-03-21 01:53:16 -07001218 int rv;
Raymond Hettinger0e371f22004-05-12 20:55:56 +00001219
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001220 assert (i >= 0 && i < Py_SIZE(deque));
Raymond Hettinger6921c132015-03-21 02:03:40 -07001221 if (_deque_rotate(deque, -i))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 item = deque_popleft(deque, NULL);
Raymond Hettingerac13ad62015-03-21 01:53:16 -07001224 rv = _deque_rotate(deque, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 assert (item != NULL);
1226 Py_DECREF(item);
Raymond Hettingerac13ad62015-03-21 01:53:16 -07001227 return rv;
Raymond Hettinger0e371f22004-05-12 20:55:56 +00001228}
1229
1230static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001231deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001232{
Raymond Hettinger38418662016-02-08 20:34:49 -08001233 PyObject *old_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 block *b;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001235 Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001236
Raymond Hettinger3c186ba2015-03-02 21:45:02 -08001237 if (!valid_index(i, len)) {
1238 PyErr_SetString(PyExc_IndexError, "deque index out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 return -1;
1240 }
1241 if (v == NULL)
1242 return deque_del_item(deque, i);
Raymond Hettinger0e371f22004-05-12 20:55:56 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 i += deque->leftindex;
Raymond Hettingerc2083082015-02-28 23:29:16 -08001245 n = (Py_ssize_t)((size_t) i / BLOCKLEN);
1246 i = (Py_ssize_t)((size_t) i % BLOCKLEN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (index <= halflen) {
1248 b = deque->leftblock;
Raymond Hettingere1b02872017-09-04 16:07:06 -07001249 while (--n >= 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 b = b->rightlink;
1251 } else {
Raymond Hettingera473b9d2015-02-28 17:49:47 -08001252 n = (Py_ssize_t)(
Raymond Hettingerc2083082015-02-28 23:29:16 -08001253 ((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
Raymond Hettingera473b9d2015-02-28 17:49:47 -08001254 / BLOCKLEN - n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 b = deque->rightblock;
Raymond Hettingere1b02872017-09-04 16:07:06 -07001256 while (--n >= 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 b = b->leftlink;
1258 }
1259 Py_INCREF(v);
Raymond Hettinger38418662016-02-08 20:34:49 -08001260 old_value = b->data[i];
1261 b->data[i] = v;
1262 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 return 0;
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001264}
1265
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001266static void
1267deque_dealloc(dequeobject *deque)
1268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 PyObject_GC_UnTrack(deque);
1270 if (deque->weakreflist != NULL)
1271 PyObject_ClearWeakRefs((PyObject *) deque);
1272 if (deque->leftblock != NULL) {
1273 deque_clear(deque);
1274 assert(deque->leftblock != NULL);
1275 freeblock(deque->leftblock);
1276 }
1277 deque->leftblock = NULL;
1278 deque->rightblock = NULL;
1279 Py_TYPE(deque)->tp_free(deque);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001280}
1281
1282static int
Raymond Hettinger0a4977c2004-03-01 23:16:22 +00001283deque_traverse(dequeobject *deque, visitproc visit, void *arg)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 block *b;
1286 PyObject *item;
1287 Py_ssize_t index;
1288 Py_ssize_t indexlo = deque->leftindex;
Raymond Hettinger1286d142015-10-14 23:16:57 -07001289 Py_ssize_t indexhigh;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001290
Raymond Hettinger5bfa8672013-07-06 11:58:09 -10001291 for (b = deque->leftblock; b != deque->rightblock; b = b->rightlink) {
1292 for (index = indexlo; index < BLOCKLEN ; index++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 item = b->data[index];
1294 Py_VISIT(item);
1295 }
1296 indexlo = 0;
1297 }
Raymond Hettinger1286d142015-10-14 23:16:57 -07001298 indexhigh = deque->rightindex;
1299 for (index = indexlo; index <= indexhigh; index++) {
Raymond Hettinger5bfa8672013-07-06 11:58:09 -10001300 item = b->data[index];
1301 Py_VISIT(item);
1302 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 return 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001304}
1305
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001306static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02001307deque_reduce(dequeobject *deque, PyObject *Py_UNUSED(ignored))
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001308{
Serhiy Storchakaa0d416f2016-03-06 08:55:21 +02001309 PyObject *dict, *it;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001310 _Py_IDENTIFIER(__dict__);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001311
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001312 if (_PyObject_LookupAttrId((PyObject *)deque, &PyId___dict__, &dict) < 0) {
1313 return NULL;
1314 }
Serhiy Storchakaa0d416f2016-03-06 08:55:21 +02001315 if (dict == NULL) {
Serhiy Storchakaa0d416f2016-03-06 08:55:21 +02001316 dict = Py_None;
1317 Py_INCREF(dict);
1318 }
1319
1320 it = PyObject_GetIter((PyObject *)deque);
1321 if (it == NULL) {
1322 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 return NULL;
1324 }
Serhiy Storchakaa0d416f2016-03-06 08:55:21 +02001325
1326 if (deque->maxlen < 0) {
1327 return Py_BuildValue("O()NN", Py_TYPE(deque), dict, it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 }
Serhiy Storchakaa0d416f2016-03-06 08:55:21 +02001329 else {
1330 return Py_BuildValue("O(()n)NN", Py_TYPE(deque), deque->maxlen, dict, it);
1331 }
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001332}
1333
1334PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1335
1336static PyObject *
1337deque_repr(PyObject *deque)
1338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 PyObject *aslist, *result;
1340 int i;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 i = Py_ReprEnter(deque);
1343 if (i != 0) {
1344 if (i < 0)
1345 return NULL;
1346 return PyUnicode_FromString("[...]");
1347 }
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 aslist = PySequence_List(deque);
1350 if (aslist == NULL) {
1351 Py_ReprLeave(deque);
1352 return NULL;
1353 }
Raymond Hettingera7f630092015-10-10 23:56:02 -04001354 if (((dequeobject *)deque)->maxlen >= 0)
Serhiy Storchakab3a77962017-09-21 14:24:13 +03001355 result = PyUnicode_FromFormat("%s(%R, maxlen=%zd)",
1356 _PyType_Name(Py_TYPE(deque)), aslist,
1357 ((dequeobject *)deque)->maxlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 else
Serhiy Storchakab3a77962017-09-21 14:24:13 +03001359 result = PyUnicode_FromFormat("%s(%R)",
1360 _PyType_Name(Py_TYPE(deque)), aslist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 Py_ReprLeave(deque);
Raymond Hettinger2b2b7532015-09-05 17:05:52 -07001362 Py_DECREF(aslist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 return result;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001364}
1365
Raymond Hettinger738ec902004-02-29 02:15:56 +00001366static PyObject *
1367deque_richcompare(PyObject *v, PyObject *w, int op)
1368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PyObject *it1=NULL, *it2=NULL, *x, *y;
1370 Py_ssize_t vs, ws;
1371 int b, cmp=-1;
Raymond Hettinger738ec902004-02-29 02:15:56 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (!PyObject_TypeCheck(v, &deque_type) ||
1374 !PyObject_TypeCheck(w, &deque_type)) {
Brian Curtindfc80e32011-08-10 20:28:54 -05001375 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 }
Raymond Hettinger738ec902004-02-29 02:15:56 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 /* Shortcuts */
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001379 vs = Py_SIZE((dequeobject *)v);
1380 ws = Py_SIZE((dequeobject *)w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if (op == Py_EQ) {
1382 if (v == w)
1383 Py_RETURN_TRUE;
1384 if (vs != ws)
1385 Py_RETURN_FALSE;
1386 }
1387 if (op == Py_NE) {
1388 if (v == w)
1389 Py_RETURN_FALSE;
1390 if (vs != ws)
1391 Py_RETURN_TRUE;
1392 }
Raymond Hettinger738ec902004-02-29 02:15:56 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 /* Search for the first index where items are different */
1395 it1 = PyObject_GetIter(v);
1396 if (it1 == NULL)
1397 goto done;
1398 it2 = PyObject_GetIter(w);
1399 if (it2 == NULL)
1400 goto done;
1401 for (;;) {
1402 x = PyIter_Next(it1);
1403 if (x == NULL && PyErr_Occurred())
1404 goto done;
1405 y = PyIter_Next(it2);
1406 if (x == NULL || y == NULL)
1407 break;
1408 b = PyObject_RichCompareBool(x, y, Py_EQ);
1409 if (b == 0) {
1410 cmp = PyObject_RichCompareBool(x, y, op);
1411 Py_DECREF(x);
1412 Py_DECREF(y);
1413 goto done;
1414 }
1415 Py_DECREF(x);
1416 Py_DECREF(y);
Raymond Hettingerd3d2b2c2015-09-21 23:41:56 -07001417 if (b < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 goto done;
1419 }
1420 /* We reached the end of one deque or both */
1421 Py_XDECREF(x);
1422 Py_XDECREF(y);
1423 if (PyErr_Occurred())
1424 goto done;
1425 switch (op) {
1426 case Py_LT: cmp = y != NULL; break; /* if w was longer */
1427 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
1428 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
1429 case Py_NE: cmp = x != y; break; /* if one deque continues */
1430 case Py_GT: cmp = x != NULL; break; /* if v was longer */
1431 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
1432 }
Tim Peters1065f752004-10-01 01:03:29 +00001433
Raymond Hettinger738ec902004-02-29 02:15:56 +00001434done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 Py_XDECREF(it1);
1436 Py_XDECREF(it2);
1437 if (cmp == 1)
1438 Py_RETURN_TRUE;
1439 if (cmp == 0)
1440 Py_RETURN_FALSE;
1441 return NULL;
Raymond Hettinger738ec902004-02-29 02:15:56 +00001442}
1443
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001444static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001445deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 PyObject *iterable = NULL;
1448 PyObject *maxlenobj = NULL;
1449 Py_ssize_t maxlen = -1;
1450 char *kwlist[] = {"iterable", "maxlen", 0};
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001451
Raymond Hettinger05f1b932019-01-31 22:13:43 -08001452 if (kwdargs == NULL && PyTuple_GET_SIZE(args) <= 2) {
1453 if (PyTuple_GET_SIZE(args) > 0) {
1454 iterable = PyTuple_GET_ITEM(args, 0);
1455 }
1456 if (PyTuple_GET_SIZE(args) > 1) {
1457 maxlenobj = PyTuple_GET_ITEM(args, 1);
1458 }
Raymond Hettinger0d309402015-09-30 23:15:02 -07001459 } else {
1460 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist,
1461 &iterable, &maxlenobj))
1462 return -1;
1463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (maxlenobj != NULL && maxlenobj != Py_None) {
1465 maxlen = PyLong_AsSsize_t(maxlenobj);
1466 if (maxlen == -1 && PyErr_Occurred())
1467 return -1;
1468 if (maxlen < 0) {
1469 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
1470 return -1;
1471 }
1472 }
1473 deque->maxlen = maxlen;
Raymond Hettinger0d309402015-09-30 23:15:02 -07001474 if (Py_SIZE(deque) > 0)
1475 deque_clear(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (iterable != NULL) {
1477 PyObject *rv = deque_extend(deque, iterable);
1478 if (rv == NULL)
1479 return -1;
1480 Py_DECREF(rv);
1481 }
1482 return 0;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001483}
1484
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001485static PyObject *
Jesus Cea16e2fca2012-08-03 14:49:42 +02001486deque_sizeof(dequeobject *deque, void *unused)
1487{
1488 Py_ssize_t res;
1489 Py_ssize_t blocks;
1490
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001491 res = _PyObject_SIZE(Py_TYPE(deque));
Raymond Hettingerbc003412015-10-14 23:33:23 -07001492 blocks = (size_t)(deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001493 assert(deque->leftindex + Py_SIZE(deque) - 1 ==
Jesus Cea16e2fca2012-08-03 14:49:42 +02001494 (blocks - 1) * BLOCKLEN + deque->rightindex);
1495 res += blocks * sizeof(block);
1496 return PyLong_FromSsize_t(res);
1497}
1498
1499PyDoc_STRVAR(sizeof_doc,
1500"D.__sizeof__() -- size of D in memory, in bytes");
1501
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001502static int
1503deque_bool(dequeobject *deque)
1504{
1505 return Py_SIZE(deque) != 0;
1506}
1507
Jesus Cea16e2fca2012-08-03 14:49:42 +02001508static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001509deque_get_maxlen(dequeobject *deque, void *Py_UNUSED(ignored))
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001510{
Raymond Hettingerd3d2b2c2015-09-21 23:41:56 -07001511 if (deque->maxlen < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 Py_RETURN_NONE;
1513 return PyLong_FromSsize_t(deque->maxlen);
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001514}
1515
Raymond Hettinger41290a62015-03-31 08:12:23 -07001516
1517/* deque object ********************************************************/
1518
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001519static PyGetSetDef deque_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
1521 "maximum size of a deque or None if unbounded"},
1522 {0}
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +00001523};
1524
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001525static PySequenceMethods deque_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 (lenfunc)deque_len, /* sq_length */
Raymond Hettinger41290a62015-03-31 08:12:23 -07001527 (binaryfunc)deque_concat, /* sq_concat */
1528 (ssizeargfunc)deque_repeat, /* sq_repeat */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 (ssizeargfunc)deque_item, /* sq_item */
1530 0, /* sq_slice */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001531 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 0, /* sq_ass_slice */
Raymond Hettinger39dadf72015-03-20 16:38:56 -07001533 (objobjproc)deque_contains, /* sq_contains */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001534 (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
Raymond Hettinger41290a62015-03-31 08:12:23 -07001535 (ssizeargfunc)deque_inplace_repeat, /* sq_inplace_repeat */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001536};
1537
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001538static PyNumberMethods deque_as_number = {
1539 0, /* nb_add */
1540 0, /* nb_subtract */
1541 0, /* nb_multiply */
1542 0, /* nb_remainder */
1543 0, /* nb_divmod */
1544 0, /* nb_power */
1545 0, /* nb_negative */
1546 0, /* nb_positive */
1547 0, /* nb_absolute */
1548 (inquiry)deque_bool, /* nb_bool */
1549 0, /* nb_invert */
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001550 };
1551
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001552static PyObject *deque_iter(dequeobject *deque);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301553static PyObject *deque_reviter(dequeobject *deque, PyObject *Py_UNUSED(ignored));
Tim Peters1065f752004-10-01 01:03:29 +00001554PyDoc_STRVAR(reversed_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 "D.__reversed__() -- return a reverse iterator over the deque");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001556
1557static PyMethodDef deque_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 {"append", (PyCFunction)deque_append,
1559 METH_O, append_doc},
1560 {"appendleft", (PyCFunction)deque_appendleft,
1561 METH_O, appendleft_doc},
1562 {"clear", (PyCFunction)deque_clearmethod,
1563 METH_NOARGS, clear_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301564 {"__copy__", deque_copy,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 METH_NOARGS, copy_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301566 {"copy", deque_copy,
Raymond Hettinger32ea1652015-03-21 01:37:37 -07001567 METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 {"count", (PyCFunction)deque_count,
Raymond Hettinger0f6f9472015-03-21 01:42:10 -07001569 METH_O, count_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 {"extend", (PyCFunction)deque_extend,
1571 METH_O, extend_doc},
1572 {"extendleft", (PyCFunction)deque_extendleft,
1573 METH_O, extendleft_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001574 {"index", (PyCFunction)(void(*)(void))deque_index,
Victor Stinnerdd407d52017-02-06 16:06:49 +01001575 METH_FASTCALL, index_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001576 {"insert", (PyCFunction)(void(*)(void))deque_insert,
Victor Stinnerdd407d52017-02-06 16:06:49 +01001577 METH_FASTCALL, insert_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 {"pop", (PyCFunction)deque_pop,
1579 METH_NOARGS, pop_doc},
1580 {"popleft", (PyCFunction)deque_popleft,
1581 METH_NOARGS, popleft_doc},
Raymond Hettinger0f6f9472015-03-21 01:42:10 -07001582 {"__reduce__", (PyCFunction)deque_reduce,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 METH_NOARGS, reduce_doc},
1584 {"remove", (PyCFunction)deque_remove,
1585 METH_O, remove_doc},
1586 {"__reversed__", (PyCFunction)deque_reviter,
1587 METH_NOARGS, reversed_doc},
1588 {"reverse", (PyCFunction)deque_reverse,
1589 METH_NOARGS, reverse_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001590 {"rotate", (PyCFunction)(void(*)(void))deque_rotate,
Victor Stinnerdd407d52017-02-06 16:06:49 +01001591 METH_FASTCALL, rotate_doc},
Jesus Cea16e2fca2012-08-03 14:49:42 +02001592 {"__sizeof__", (PyCFunction)deque_sizeof,
1593 METH_NOARGS, sizeof_doc},
Guido van Rossum48b069a2020-04-07 09:50:06 -07001594 {"__class_getitem__", (PyCFunction)Py_GenericAlias,
1595 METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 {NULL, NULL} /* sentinel */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001597};
1598
1599PyDoc_STRVAR(deque_doc,
Andrew Svetlov6a5c7c32012-10-31 11:50:40 +02001600"deque([iterable[, maxlen]]) --> deque object\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001601\n\
Raymond Hettinger41290a62015-03-31 08:12:23 -07001602A list-like sequence optimized for data accesses near its endpoints.");
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001603
Neal Norwitz87f10132004-02-29 15:40:53 +00001604static PyTypeObject deque_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 PyVarObject_HEAD_INIT(NULL, 0)
1606 "collections.deque", /* tp_name */
1607 sizeof(dequeobject), /* tp_basicsize */
1608 0, /* tp_itemsize */
1609 /* methods */
1610 (destructor)deque_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001611 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 0, /* tp_getattr */
1613 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001614 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 deque_repr, /* tp_repr */
Raymond Hettinger0f1451c2015-03-23 23:23:55 -07001616 &deque_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 &deque_as_sequence, /* tp_as_sequence */
1618 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00001619 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 0, /* tp_call */
1621 0, /* tp_str */
1622 PyObject_GenericGetAttr, /* tp_getattro */
1623 0, /* tp_setattro */
1624 0, /* tp_as_buffer */
1625 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandlf038b322010-10-18 07:35:09 +00001626 /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 deque_doc, /* tp_doc */
1628 (traverseproc)deque_traverse, /* tp_traverse */
1629 (inquiry)deque_clear, /* tp_clear */
1630 (richcmpfunc)deque_richcompare, /* tp_richcompare */
Georg Brandlf038b322010-10-18 07:35:09 +00001631 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 (getiterfunc)deque_iter, /* tp_iter */
1633 0, /* tp_iternext */
1634 deque_methods, /* tp_methods */
1635 0, /* tp_members */
Georg Brandlf038b322010-10-18 07:35:09 +00001636 deque_getset, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 0, /* tp_base */
1638 0, /* tp_dict */
1639 0, /* tp_descr_get */
1640 0, /* tp_descr_set */
1641 0, /* tp_dictoffset */
1642 (initproc)deque_init, /* tp_init */
1643 PyType_GenericAlloc, /* tp_alloc */
1644 deque_new, /* tp_new */
1645 PyObject_GC_Del, /* tp_free */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001646};
1647
1648/*********************** Deque Iterator **************************/
1649
1650typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 block *b;
Raymond Hettinger87e69122015-03-02 23:32:02 -08001653 Py_ssize_t index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 dequeobject *deque;
Raymond Hettingerf9d9c792015-03-02 22:47:46 -08001655 size_t state; /* state when the iterator is created */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 Py_ssize_t counter; /* number of items remaining for iteration */
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001657} dequeiterobject;
1658
Martin v. Löwis59683e82008-06-13 07:50:45 +00001659static PyTypeObject dequeiter_type;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001660
1661static PyObject *
1662deque_iter(dequeobject *deque)
1663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 dequeiterobject *it;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
1667 if (it == NULL)
1668 return NULL;
1669 it->b = deque->leftblock;
1670 it->index = deque->leftindex;
1671 Py_INCREF(deque);
1672 it->deque = deque;
1673 it->state = deque->state;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001674 it->counter = Py_SIZE(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 PyObject_GC_Track(it);
1676 return (PyObject *)it;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001677}
1678
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001679static int
1680dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 Py_VISIT(dio->deque);
1683 return 0;
Antoine Pitrou7ddda782009-01-01 15:35:33 +00001684}
1685
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001686static void
1687dequeiter_dealloc(dequeiterobject *dio)
1688{
INADA Naokia6296d32017-08-24 14:55:17 +09001689 /* bpo-31095: UnTrack is needed before calling any callbacks */
1690 PyObject_GC_UnTrack(dio);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 Py_XDECREF(dio->deque);
1692 PyObject_GC_Del(dio);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001693}
1694
1695static PyObject *
1696dequeiter_next(dequeiterobject *it)
1697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 PyObject *item;
Raymond Hettingerd1b3d882004-10-02 00:43:13 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (it->deque->state != it->state) {
1701 it->counter = 0;
1702 PyErr_SetString(PyExc_RuntimeError,
1703 "deque mutated during iteration");
1704 return NULL;
1705 }
1706 if (it->counter == 0)
1707 return NULL;
1708 assert (!(it->b == it->deque->rightblock &&
1709 it->index > it->deque->rightindex));
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 item = it->b->data[it->index];
1712 it->index++;
1713 it->counter--;
1714 if (it->index == BLOCKLEN && it->counter > 0) {
Raymond Hettinger82df9252013-07-07 01:43:42 -10001715 CHECK_NOT_END(it->b->rightlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 it->b = it->b->rightlink;
1717 it->index = 0;
1718 }
1719 Py_INCREF(item);
1720 return item;
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001721}
1722
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001723static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001724dequeiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1725{
1726 Py_ssize_t i, index=0;
1727 PyObject *deque;
1728 dequeiterobject *it;
1729 if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index))
1730 return NULL;
1731 assert(type == &dequeiter_type);
1732
1733 it = (dequeiterobject*)deque_iter((dequeobject *)deque);
1734 if (!it)
1735 return NULL;
1736 /* consume items from the queue */
1737 for(i=0; i<index; i++) {
1738 PyObject *item = dequeiter_next(it);
1739 if (item) {
1740 Py_DECREF(item);
1741 } else {
1742 if (it->counter) {
1743 Py_DECREF(it);
1744 return NULL;
1745 } else
1746 break;
1747 }
1748 }
1749 return (PyObject*)it;
1750}
1751
1752static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301753dequeiter_len(dequeiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001754{
Antoine Pitrou554f3342010-08-17 18:30:06 +00001755 return PyLong_FromSsize_t(it->counter);
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001756}
1757
Armin Rigof5b3e362006-02-11 21:32:43 +00001758PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001759
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001760static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301761dequeiter_reduce(dequeiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001762{
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001763 return Py_BuildValue("O(On)", Py_TYPE(it), it->deque, Py_SIZE(it->deque) - it->counter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001764}
1765
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001766static PyMethodDef dequeiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001768 {"__reduce__", (PyCFunction)dequeiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 {NULL, NULL} /* sentinel */
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001770};
1771
Martin v. Löwis59683e82008-06-13 07:50:45 +00001772static PyTypeObject dequeiter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger87e69122015-03-02 23:32:02 -08001774 "_collections._deque_iterator", /* tp_name */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 sizeof(dequeiterobject), /* tp_basicsize */
1776 0, /* tp_itemsize */
1777 /* methods */
1778 (destructor)dequeiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001779 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 0, /* tp_getattr */
1781 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001782 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 0, /* tp_repr */
1784 0, /* tp_as_number */
1785 0, /* tp_as_sequence */
1786 0, /* tp_as_mapping */
1787 0, /* tp_hash */
1788 0, /* tp_call */
1789 0, /* tp_str */
1790 PyObject_GenericGetAttr, /* tp_getattro */
1791 0, /* tp_setattro */
1792 0, /* tp_as_buffer */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001793 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 0, /* tp_doc */
1795 (traverseproc)dequeiter_traverse, /* tp_traverse */
1796 0, /* tp_clear */
1797 0, /* tp_richcompare */
1798 0, /* tp_weaklistoffset */
1799 PyObject_SelfIter, /* tp_iter */
1800 (iternextfunc)dequeiter_next, /* tp_iternext */
1801 dequeiter_methods, /* tp_methods */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001802 0, /* tp_members */
1803 0, /* tp_getset */
1804 0, /* tp_base */
1805 0, /* tp_dict */
1806 0, /* tp_descr_get */
1807 0, /* tp_descr_set */
1808 0, /* tp_dictoffset */
1809 0, /* tp_init */
1810 0, /* tp_alloc */
1811 dequeiter_new, /* tp_new */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 0,
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001813};
1814
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001815/*********************** Deque Reverse Iterator **************************/
1816
Martin v. Löwis59683e82008-06-13 07:50:45 +00001817static PyTypeObject dequereviter_type;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001818
1819static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301820deque_reviter(dequeobject *deque, PyObject *Py_UNUSED(ignored))
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 dequeiterobject *it;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
1825 if (it == NULL)
1826 return NULL;
1827 it->b = deque->rightblock;
1828 it->index = deque->rightindex;
1829 Py_INCREF(deque);
1830 it->deque = deque;
1831 it->state = deque->state;
Raymond Hettingerdf715ba2013-07-06 13:01:13 -10001832 it->counter = Py_SIZE(deque);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 PyObject_GC_Track(it);
1834 return (PyObject *)it;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001835}
1836
1837static PyObject *
1838dequereviter_next(dequeiterobject *it)
1839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 PyObject *item;
1841 if (it->counter == 0)
1842 return NULL;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 if (it->deque->state != it->state) {
1845 it->counter = 0;
1846 PyErr_SetString(PyExc_RuntimeError,
1847 "deque mutated during iteration");
1848 return NULL;
1849 }
1850 assert (!(it->b == it->deque->leftblock &&
1851 it->index < it->deque->leftindex));
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 item = it->b->data[it->index];
1854 it->index--;
1855 it->counter--;
Raymond Hettingerd3d2b2c2015-09-21 23:41:56 -07001856 if (it->index < 0 && it->counter > 0) {
Raymond Hettinger82df9252013-07-07 01:43:42 -10001857 CHECK_NOT_END(it->b->leftlink);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 it->b = it->b->leftlink;
1859 it->index = BLOCKLEN - 1;
1860 }
1861 Py_INCREF(item);
1862 return item;
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001863}
1864
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001865static PyObject *
1866dequereviter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1867{
1868 Py_ssize_t i, index=0;
1869 PyObject *deque;
1870 dequeiterobject *it;
1871 if (!PyArg_ParseTuple(args, "O!|n", &deque_type, &deque, &index))
1872 return NULL;
1873 assert(type == &dequereviter_type);
1874
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301875 it = (dequeiterobject*)deque_reviter((dequeobject *)deque, NULL);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001876 if (!it)
1877 return NULL;
1878 /* consume items from the queue */
1879 for(i=0; i<index; i++) {
1880 PyObject *item = dequereviter_next(it);
1881 if (item) {
1882 Py_DECREF(item);
1883 } else {
1884 if (it->counter) {
1885 Py_DECREF(it);
1886 return NULL;
1887 } else
1888 break;
1889 }
1890 }
1891 return (PyObject*)it;
1892}
1893
Martin v. Löwis59683e82008-06-13 07:50:45 +00001894static PyTypeObject dequereviter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger87e69122015-03-02 23:32:02 -08001896 "_collections._deque_reverse_iterator", /* tp_name */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 sizeof(dequeiterobject), /* tp_basicsize */
1898 0, /* tp_itemsize */
1899 /* methods */
1900 (destructor)dequeiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001901 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 0, /* tp_getattr */
1903 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001904 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 0, /* tp_repr */
1906 0, /* tp_as_number */
1907 0, /* tp_as_sequence */
1908 0, /* tp_as_mapping */
1909 0, /* tp_hash */
1910 0, /* tp_call */
1911 0, /* tp_str */
1912 PyObject_GenericGetAttr, /* tp_getattro */
1913 0, /* tp_setattro */
1914 0, /* tp_as_buffer */
Raymond Hettinger87e69122015-03-02 23:32:02 -08001915 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 0, /* tp_doc */
1917 (traverseproc)dequeiter_traverse, /* tp_traverse */
1918 0, /* tp_clear */
1919 0, /* tp_richcompare */
1920 0, /* tp_weaklistoffset */
1921 PyObject_SelfIter, /* tp_iter */
1922 (iternextfunc)dequereviter_next, /* tp_iternext */
1923 dequeiter_methods, /* tp_methods */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001924 0, /* tp_members */
1925 0, /* tp_getset */
1926 0, /* tp_base */
1927 0, /* tp_dict */
1928 0, /* tp_descr_get */
1929 0, /* tp_descr_set */
1930 0, /* tp_dictoffset */
1931 0, /* tp_init */
1932 0, /* tp_alloc */
1933 dequereviter_new, /* tp_new */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 0,
Raymond Hettinger1e5809f2004-03-18 11:04:57 +00001935};
1936
Guido van Rossum1968ad32006-02-25 22:38:04 +00001937/* defaultdict type *********************************************************/
1938
1939typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 PyDictObject dict;
1941 PyObject *default_factory;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001942} defdictobject;
1943
1944static PyTypeObject defdict_type; /* Forward */
1945
1946PyDoc_STRVAR(defdict_missing_doc,
1947"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
Guido van Rossumd8faa362007-04-27 19:54:29 +00001948 if self.default_factory is None: raise KeyError((key,))\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00001949 self[key] = value = self.default_factory()\n\
1950 return value\n\
1951");
1952
1953static PyObject *
1954defdict_missing(defdictobject *dd, PyObject *key)
1955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 PyObject *factory = dd->default_factory;
1957 PyObject *value;
1958 if (factory == NULL || factory == Py_None) {
1959 /* XXX Call dict.__missing__(key) */
1960 PyObject *tup;
1961 tup = PyTuple_Pack(1, key);
1962 if (!tup) return NULL;
1963 PyErr_SetObject(PyExc_KeyError, tup);
1964 Py_DECREF(tup);
1965 return NULL;
1966 }
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +02001967 value = _PyObject_CallNoArg(factory);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 if (value == NULL)
1969 return value;
1970 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1971 Py_DECREF(value);
1972 return NULL;
1973 }
1974 return value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001975}
1976
Brandt Bucher57c9d172020-03-06 09:24:08 -08001977static inline PyObject*
1978new_defdict(defdictobject *dd, PyObject *arg)
1979{
1980 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
1981 dd->default_factory ? dd->default_factory : Py_None, arg, NULL);
1982}
1983
Guido van Rossum1968ad32006-02-25 22:38:04 +00001984PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
1985
1986static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301987defdict_copy(defdictobject *dd, PyObject *Py_UNUSED(ignored))
Guido van Rossum1968ad32006-02-25 22:38:04 +00001988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 /* This calls the object's class. That only works for subclasses
1990 whose class constructor has the same signature. Subclasses that
1991 define a different constructor signature must override copy().
1992 */
Brandt Bucher57c9d172020-03-06 09:24:08 -08001993 return new_defdict(dd, (PyObject*)dd);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001994}
1995
1996static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301997defdict_reduce(defdictobject *dd, PyObject *Py_UNUSED(ignored))
Guido van Rossum1968ad32006-02-25 22:38:04 +00001998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 /* __reduce__ must return a 5-tuple as follows:
Guido van Rossum1968ad32006-02-25 22:38:04 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 - factory function
2002 - tuple of args for the factory function
2003 - additional state (here None)
2004 - sequence iterator (here None)
2005 - dictionary iterator (yielding successive (key, value) pairs
Guido van Rossum1968ad32006-02-25 22:38:04 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 This API is used by pickle.py and copy.py.
Guido van Rossum1968ad32006-02-25 22:38:04 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 For this to be useful with pickle.py, the default_factory
2010 must be picklable; e.g., None, a built-in, or a global
2011 function in a module or package.
Guido van Rossum1968ad32006-02-25 22:38:04 +00002012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 Both shallow and deep copying are supported, but for deep
2014 copying, the default_factory must be deep-copyable; e.g. None,
2015 or a built-in (functions are not copyable at this time).
Guido van Rossum1968ad32006-02-25 22:38:04 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 This only works for subclasses as long as their constructor
2018 signature is compatible; the first argument must be the
2019 optional default_factory, defaulting to None.
2020 */
2021 PyObject *args;
2022 PyObject *items;
2023 PyObject *iter;
2024 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002025 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 if (dd->default_factory == NULL || dd->default_factory == Py_None)
2028 args = PyTuple_New(0);
2029 else
2030 args = PyTuple_Pack(1, dd->default_factory);
2031 if (args == NULL)
2032 return NULL;
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002033 items = _PyObject_CallMethodIdNoArgs((PyObject *)dd, &PyId_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 if (items == NULL) {
2035 Py_DECREF(args);
2036 return NULL;
2037 }
2038 iter = PyObject_GetIter(items);
2039 if (iter == NULL) {
2040 Py_DECREF(items);
2041 Py_DECREF(args);
2042 return NULL;
2043 }
2044 result = PyTuple_Pack(5, Py_TYPE(dd), args,
2045 Py_None, Py_None, iter);
2046 Py_DECREF(iter);
2047 Py_DECREF(items);
2048 Py_DECREF(args);
2049 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00002050}
2051
2052static PyMethodDef defdict_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 {"__missing__", (PyCFunction)defdict_missing, METH_O,
2054 defdict_missing_doc},
2055 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
2056 defdict_copy_doc},
2057 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
2058 defdict_copy_doc},
2059 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
2060 reduce_doc},
Guido van Rossum48b069a2020-04-07 09:50:06 -07002061 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS,
2062 PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 {NULL}
Guido van Rossum1968ad32006-02-25 22:38:04 +00002064};
2065
2066static PyMemberDef defdict_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 {"default_factory", T_OBJECT,
2068 offsetof(defdictobject, default_factory), 0,
2069 PyDoc_STR("Factory for default value called by __missing__().")},
2070 {NULL}
Guido van Rossum1968ad32006-02-25 22:38:04 +00002071};
2072
2073static void
2074defdict_dealloc(defdictobject *dd)
2075{
INADA Naokia6296d32017-08-24 14:55:17 +09002076 /* bpo-31095: UnTrack is needed before calling any callbacks */
2077 PyObject_GC_UnTrack(dd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 Py_CLEAR(dd->default_factory);
2079 PyDict_Type.tp_dealloc((PyObject *)dd);
Guido van Rossum1968ad32006-02-25 22:38:04 +00002080}
2081
Guido van Rossum1968ad32006-02-25 22:38:04 +00002082static PyObject *
2083defdict_repr(defdictobject *dd)
2084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 PyObject *baserepr;
2086 PyObject *defrepr;
2087 PyObject *result;
2088 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
2089 if (baserepr == NULL)
2090 return NULL;
2091 if (dd->default_factory == NULL)
2092 defrepr = PyUnicode_FromString("None");
2093 else
2094 {
2095 int status = Py_ReprEnter(dd->default_factory);
2096 if (status != 0) {
Antoine Pitrouf5f1fe02012-02-15 02:42:46 +01002097 if (status < 0) {
2098 Py_DECREF(baserepr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 return NULL;
Antoine Pitrouf5f1fe02012-02-15 02:42:46 +01002100 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 defrepr = PyUnicode_FromString("...");
2102 }
2103 else
2104 defrepr = PyObject_Repr(dd->default_factory);
2105 Py_ReprLeave(dd->default_factory);
2106 }
2107 if (defrepr == NULL) {
2108 Py_DECREF(baserepr);
2109 return NULL;
2110 }
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002111 result = PyUnicode_FromFormat("%s(%U, %U)",
2112 _PyType_Name(Py_TYPE(dd)),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 defrepr, baserepr);
2114 Py_DECREF(defrepr);
2115 Py_DECREF(baserepr);
2116 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00002117}
2118
Brandt Bucher57c9d172020-03-06 09:24:08 -08002119static PyObject*
2120defdict_or(PyObject* left, PyObject* right)
2121{
Brandt Bucher57c9d172020-03-06 09:24:08 -08002122 PyObject *self, *other;
Brandt Bucher1dd37942020-03-11 21:06:46 -07002123 if (PyObject_TypeCheck(left, &defdict_type)) {
Brandt Bucher57c9d172020-03-06 09:24:08 -08002124 self = left;
2125 other = right;
2126 }
2127 else {
2128 self = right;
2129 other = left;
2130 }
2131 if (!PyDict_Check(other)) {
2132 Py_RETURN_NOTIMPLEMENTED;
2133 }
2134 // Like copy(), this calls the object's class.
2135 // Override __or__/__ror__ for subclasses with different constructors.
2136 PyObject *new = new_defdict((defdictobject*)self, left);
2137 if (!new) {
2138 return NULL;
2139 }
2140 if (PyDict_Update(new, right)) {
2141 Py_DECREF(new);
2142 return NULL;
2143 }
2144 return new;
2145}
2146
2147static PyNumberMethods defdict_as_number = {
2148 .nb_or = defdict_or,
2149};
2150
Guido van Rossum1968ad32006-02-25 22:38:04 +00002151static int
2152defdict_traverse(PyObject *self, visitproc visit, void *arg)
2153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 Py_VISIT(((defdictobject *)self)->default_factory);
2155 return PyDict_Type.tp_traverse(self, visit, arg);
Guido van Rossum1968ad32006-02-25 22:38:04 +00002156}
2157
2158static int
2159defdict_tp_clear(defdictobject *dd)
2160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 Py_CLEAR(dd->default_factory);
2162 return PyDict_Type.tp_clear((PyObject *)dd);
Guido van Rossum1968ad32006-02-25 22:38:04 +00002163}
2164
2165static int
2166defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
2167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 defdictobject *dd = (defdictobject *)self;
2169 PyObject *olddefault = dd->default_factory;
2170 PyObject *newdefault = NULL;
2171 PyObject *newargs;
2172 int result;
2173 if (args == NULL || !PyTuple_Check(args))
2174 newargs = PyTuple_New(0);
2175 else {
2176 Py_ssize_t n = PyTuple_GET_SIZE(args);
2177 if (n > 0) {
2178 newdefault = PyTuple_GET_ITEM(args, 0);
2179 if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
2180 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger239aba72015-07-20 03:09:22 -04002181 "first argument must be callable or None");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 return -1;
2183 }
2184 }
2185 newargs = PySequence_GetSlice(args, 1, n);
2186 }
2187 if (newargs == NULL)
2188 return -1;
2189 Py_XINCREF(newdefault);
2190 dd->default_factory = newdefault;
2191 result = PyDict_Type.tp_init(self, newargs, kwds);
2192 Py_DECREF(newargs);
2193 Py_XDECREF(olddefault);
2194 return result;
Guido van Rossum1968ad32006-02-25 22:38:04 +00002195}
2196
2197PyDoc_STRVAR(defdict_doc,
Benjamin Peterson9cb33b72014-01-13 23:56:05 -05002198"defaultdict(default_factory[, ...]) --> dict with default factory\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00002199\n\
2200The default factory is called without arguments to produce\n\
2201a new value when a key is not present, in __getitem__ only.\n\
2202A defaultdict compares equal to a dict with the same items.\n\
Benjamin Peterson9cb33b72014-01-13 23:56:05 -05002203All remaining arguments are treated the same as if they were\n\
2204passed to the dict constructor, including keyword arguments.\n\
Guido van Rossum1968ad32006-02-25 22:38:04 +00002205");
2206
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002207/* See comment in xxsubtype.c */
2208#define DEFERRED_ADDRESS(ADDR) 0
2209
Guido van Rossum1968ad32006-02-25 22:38:04 +00002210static PyTypeObject defdict_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
2212 "collections.defaultdict", /* tp_name */
2213 sizeof(defdictobject), /* tp_basicsize */
2214 0, /* tp_itemsize */
2215 /* methods */
2216 (destructor)defdict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002217 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 0, /* tp_getattr */
2219 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002220 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 (reprfunc)defdict_repr, /* tp_repr */
Brandt Bucher57c9d172020-03-06 09:24:08 -08002222 &defdict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 0, /* tp_as_sequence */
2224 0, /* tp_as_mapping */
2225 0, /* tp_hash */
2226 0, /* tp_call */
2227 0, /* tp_str */
2228 PyObject_GenericGetAttr, /* tp_getattro */
2229 0, /* tp_setattro */
2230 0, /* tp_as_buffer */
2231 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
2232 /* tp_flags */
2233 defdict_doc, /* tp_doc */
2234 defdict_traverse, /* tp_traverse */
2235 (inquiry)defdict_tp_clear, /* tp_clear */
2236 0, /* tp_richcompare */
2237 0, /* tp_weaklistoffset*/
2238 0, /* tp_iter */
2239 0, /* tp_iternext */
2240 defdict_methods, /* tp_methods */
2241 defdict_members, /* tp_members */
2242 0, /* tp_getset */
2243 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
2244 0, /* tp_dict */
2245 0, /* tp_descr_get */
2246 0, /* tp_descr_set */
2247 0, /* tp_dictoffset */
2248 defdict_init, /* tp_init */
2249 PyType_GenericAlloc, /* tp_alloc */
2250 0, /* tp_new */
2251 PyObject_GC_Del, /* tp_free */
Guido van Rossum1968ad32006-02-25 22:38:04 +00002252};
2253
Raymond Hettinger96f34102010-12-15 16:30:37 +00002254/* helper function for Counter *********************************************/
2255
Raymond Hettingere9858042019-06-05 16:05:25 -07002256/*[clinic input]
2257_collections._count_elements
2258
2259 mapping: object
2260 iterable: object
2261 /
2262
2263Count elements in the iterable, updating the mapping
2264[clinic start generated code]*/
Raymond Hettinger96f34102010-12-15 16:30:37 +00002265
2266static PyObject *
Raymond Hettingere9858042019-06-05 16:05:25 -07002267_collections__count_elements_impl(PyObject *module, PyObject *mapping,
2268 PyObject *iterable)
2269/*[clinic end generated code: output=7e0c1789636b3d8f input=e79fad04534a0b45]*/
Raymond Hettinger96f34102010-12-15 16:30:37 +00002270{
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002271 _Py_IDENTIFIER(get);
Raymond Hettinger2ff21902013-10-01 00:55:43 -07002272 _Py_IDENTIFIER(__setitem__);
Raymond Hettingere9858042019-06-05 16:05:25 -07002273 PyObject *it, *oldval;
Raymond Hettinger96f34102010-12-15 16:30:37 +00002274 PyObject *newval = NULL;
2275 PyObject *key = NULL;
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002276 PyObject *bound_get = NULL;
2277 PyObject *mapping_get;
2278 PyObject *dict_get;
Raymond Hettinger2ff21902013-10-01 00:55:43 -07002279 PyObject *mapping_setitem;
Raymond Hettinger2ff21902013-10-01 00:55:43 -07002280 PyObject *dict_setitem;
Raymond Hettinger96f34102010-12-15 16:30:37 +00002281
Raymond Hettinger96f34102010-12-15 16:30:37 +00002282 it = PyObject_GetIter(iterable);
2283 if (it == NULL)
2284 return NULL;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002285
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002286 /* Only take the fast path when get() and __setitem__()
2287 * have not been overridden.
2288 */
2289 mapping_get = _PyType_LookupId(Py_TYPE(mapping), &PyId_get);
2290 dict_get = _PyType_LookupId(&PyDict_Type, &PyId_get);
Raymond Hettinger2ff21902013-10-01 00:55:43 -07002291 mapping_setitem = _PyType_LookupId(Py_TYPE(mapping), &PyId___setitem__);
2292 dict_setitem = _PyType_LookupId(&PyDict_Type, &PyId___setitem__);
2293
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002294 if (mapping_get != NULL && mapping_get == dict_get &&
Oren Milman31aca4b2017-09-27 06:18:21 +03002295 mapping_setitem != NULL && mapping_setitem == dict_setitem &&
2296 PyDict_Check(mapping))
2297 {
Raymond Hettinger426e0522011-01-03 02:12:02 +00002298 while (1) {
Raymond Hettinger507d9972014-05-18 21:32:40 +01002299 /* Fast path advantages:
2300 1. Eliminate double hashing
2301 (by re-using the same hash for both the get and set)
2302 2. Avoid argument overhead of PyObject_CallFunctionObjArgs
2303 (argument tuple creation and parsing)
2304 3. Avoid indirection through a bound method object
2305 (creates another argument tuple)
2306 4. Avoid initial increment from zero
2307 (reuse an existing one-object instead)
2308 */
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002309 Py_hash_t hash;
2310
Raymond Hettinger426e0522011-01-03 02:12:02 +00002311 key = PyIter_Next(it);
Victor Stinnera154b5c2011-04-20 23:23:52 +02002312 if (key == NULL)
2313 break;
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002314
2315 if (!PyUnicode_CheckExact(key) ||
2316 (hash = ((PyASCIIObject *) key)->hash) == -1)
2317 {
2318 hash = PyObject_Hash(key);
Raymond Hettingerda2850f2015-02-27 12:42:54 -08002319 if (hash == -1)
Raymond Hettinger4b0b1ac2014-05-03 16:41:19 -07002320 goto done;
2321 }
2322
2323 oldval = _PyDict_GetItem_KnownHash(mapping, key, hash);
Raymond Hettinger426e0522011-01-03 02:12:02 +00002324 if (oldval == NULL) {
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002325 if (PyErr_Occurred())
2326 goto done;
Victor Stinner37834132020-10-27 17:12:53 +01002327 if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_GetOne(), hash) < 0)
Raymond Hettinger507d9972014-05-18 21:32:40 +01002328 goto done;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002329 } else {
Victor Stinner37834132020-10-27 17:12:53 +01002330 newval = PyNumber_Add(oldval, _PyLong_GetOne());
Raymond Hettinger426e0522011-01-03 02:12:02 +00002331 if (newval == NULL)
Raymond Hettinger507d9972014-05-18 21:32:40 +01002332 goto done;
Raymond Hettinger28c995d2015-08-14 02:07:41 -07002333 if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
Raymond Hettinger507d9972014-05-18 21:32:40 +01002334 goto done;
Raymond Hettinger426e0522011-01-03 02:12:02 +00002335 Py_CLEAR(newval);
2336 }
2337 Py_DECREF(key);
Raymond Hettinger96f34102010-12-15 16:30:37 +00002338 }
Raymond Hettinger426e0522011-01-03 02:12:02 +00002339 } else {
Victor Stinnere7f516c2013-11-06 23:52:55 +01002340 bound_get = _PyObject_GetAttrId(mapping, &PyId_get);
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002341 if (bound_get == NULL)
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002342 goto done;
2343
Victor Stinner37834132020-10-27 17:12:53 +01002344 PyObject *zero = _PyLong_GetZero(); // borrowed reference
2345 PyObject *one = _PyLong_GetOne(); // borrowed reference
Raymond Hettinger426e0522011-01-03 02:12:02 +00002346 while (1) {
2347 key = PyIter_Next(it);
Victor Stinnera154b5c2011-04-20 23:23:52 +02002348 if (key == NULL)
2349 break;
Victor Stinner37834132020-10-27 17:12:53 +01002350 oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002351 if (oldval == NULL)
2352 break;
Victor Stinner37834132020-10-27 17:12:53 +01002353 newval = PyNumber_Add(oldval, one);
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002354 Py_DECREF(oldval);
2355 if (newval == NULL)
2356 break;
Raymond Hettinger28c995d2015-08-14 02:07:41 -07002357 if (PyObject_SetItem(mapping, key, newval) < 0)
Raymond Hettinger96f34102010-12-15 16:30:37 +00002358 break;
2359 Py_CLEAR(newval);
Raymond Hettinger426e0522011-01-03 02:12:02 +00002360 Py_DECREF(key);
Raymond Hettinger96f34102010-12-15 16:30:37 +00002361 }
Raymond Hettinger96f34102010-12-15 16:30:37 +00002362 }
Raymond Hettinger426e0522011-01-03 02:12:02 +00002363
Raymond Hettinger224c87d2013-10-01 21:36:09 -07002364done:
Raymond Hettinger96f34102010-12-15 16:30:37 +00002365 Py_DECREF(it);
2366 Py_XDECREF(key);
2367 Py_XDECREF(newval);
Raymond Hettingercb1d96f2013-10-04 16:51:02 -07002368 Py_XDECREF(bound_get);
Raymond Hettinger96f34102010-12-15 16:30:37 +00002369 if (PyErr_Occurred())
2370 return NULL;
2371 Py_RETURN_NONE;
2372}
2373
Serhiy Storchaka052b2df2018-12-31 14:15:16 +02002374/* Helper function for namedtuple() ************************************/
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002375
2376typedef struct {
2377 PyObject_HEAD
2378 Py_ssize_t index;
2379 PyObject* doc;
2380} _tuplegetterobject;
2381
2382/*[clinic input]
2383@classmethod
2384_tuplegetter.__new__ as tuplegetter_new
2385
2386 index: Py_ssize_t
2387 doc: object
2388 /
2389[clinic start generated code]*/
2390
2391static PyObject *
2392tuplegetter_new_impl(PyTypeObject *type, Py_ssize_t index, PyObject *doc)
2393/*[clinic end generated code: output=014be444ad80263f input=87c576a5bdbc0bbb]*/
2394{
2395 _tuplegetterobject* self;
2396 self = (_tuplegetterobject *)type->tp_alloc(type, 0);
2397 if (self == NULL) {
2398 return NULL;
2399 }
2400 self->index = index;
2401 Py_INCREF(doc);
2402 self->doc = doc;
2403 return (PyObject *)self;
2404}
2405
2406static PyObject *
Serhiy Storchaka052b2df2018-12-31 14:15:16 +02002407tuplegetter_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002408{
Serhiy Storchaka052b2df2018-12-31 14:15:16 +02002409 Py_ssize_t index = ((_tuplegetterobject*)self)->index;
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002410 PyObject *result;
Serhiy Storchaka052b2df2018-12-31 14:15:16 +02002411
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002412 if (obj == NULL) {
2413 Py_INCREF(self);
2414 return self;
2415 }
2416 if (!PyTuple_Check(obj)) {
2417 if (obj == Py_None) {
2418 Py_INCREF(self);
2419 return self;
2420 }
2421 PyErr_Format(PyExc_TypeError,
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +02002422 "descriptor for index '%zd' for tuple subclasses "
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002423 "doesn't apply to '%s' object",
Serhiy Storchaka052b2df2018-12-31 14:15:16 +02002424 index,
Victor Stinnerdaa97562020-02-07 03:37:06 +01002425 Py_TYPE(obj)->tp_name);
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002426 return NULL;
2427 }
2428
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002429 if (!valid_index(index, PyTuple_GET_SIZE(obj))) {
2430 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
2431 return NULL;
2432 }
2433
2434 result = PyTuple_GET_ITEM(obj, index);
2435 Py_INCREF(result);
2436 return result;
2437}
2438
2439static int
Serhiy Storchaka052b2df2018-12-31 14:15:16 +02002440tuplegetter_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002441{
2442 if (value == NULL) {
2443 PyErr_SetString(PyExc_AttributeError, "can't delete attribute");
2444 } else {
2445 PyErr_SetString(PyExc_AttributeError, "can't set attribute");
2446 }
2447 return -1;
2448}
2449
2450static int
2451tuplegetter_traverse(PyObject *self, visitproc visit, void *arg)
2452{
2453 _tuplegetterobject *tuplegetter = (_tuplegetterobject *)self;
2454 Py_VISIT(tuplegetter->doc);
2455 return 0;
2456}
2457
2458static int
2459tuplegetter_clear(PyObject *self)
2460{
2461 _tuplegetterobject *tuplegetter = (_tuplegetterobject *)self;
2462 Py_CLEAR(tuplegetter->doc);
2463 return 0;
2464}
2465
2466static void
2467tuplegetter_dealloc(_tuplegetterobject *self)
2468{
2469 PyObject_GC_UnTrack(self);
2470 tuplegetter_clear((PyObject*)self);
2471 Py_TYPE(self)->tp_free((PyObject*)self);
2472}
2473
Joe Jevnikf36f8922019-02-21 16:00:40 -05002474static PyObject*
Serhiy Storchakaadfffc72019-03-05 18:41:09 +02002475tuplegetter_reduce(_tuplegetterobject *self, PyObject *Py_UNUSED(ignored))
Joe Jevnikf36f8922019-02-21 16:00:40 -05002476{
2477 return Py_BuildValue("(O(nO))", (PyObject*) Py_TYPE(self), self->index, self->doc);
2478}
2479
Ammar Askara86b5222020-04-14 23:36:08 -07002480static PyObject*
2481tuplegetter_repr(_tuplegetterobject *self)
2482{
2483 return PyUnicode_FromFormat("%s(%zd, %R)",
2484 _PyType_Name(Py_TYPE(self)),
2485 self->index, self->doc);
2486}
2487
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002488
2489static PyMemberDef tuplegetter_members[] = {
2490 {"__doc__", T_OBJECT, offsetof(_tuplegetterobject, doc), 0},
2491 {0}
2492};
2493
Joe Jevnikf36f8922019-02-21 16:00:40 -05002494static PyMethodDef tuplegetter_methods[] = {
Serhiy Storchakaadfffc72019-03-05 18:41:09 +02002495 {"__reduce__", (PyCFunction)tuplegetter_reduce, METH_NOARGS, NULL},
Joe Jevnikf36f8922019-02-21 16:00:40 -05002496 {NULL},
2497};
2498
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002499static PyTypeObject tuplegetter_type = {
2500 PyVarObject_HEAD_INIT(NULL, 0)
2501 "_collections._tuplegetter", /* tp_name */
2502 sizeof(_tuplegetterobject), /* tp_basicsize */
2503 0, /* tp_itemsize */
2504 /* methods */
2505 (destructor)tuplegetter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002506 0, /* tp_vectorcall_offset */
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002507 0, /* tp_getattr */
2508 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002509 0, /* tp_as_async */
Ammar Askara86b5222020-04-14 23:36:08 -07002510 (reprfunc)tuplegetter_repr, /* tp_repr */
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002511 0, /* tp_as_number */
2512 0, /* tp_as_sequence */
2513 0, /* tp_as_mapping */
2514 0, /* tp_hash */
2515 0, /* tp_call */
2516 0, /* tp_str */
2517 0, /* tp_getattro */
2518 0, /* tp_setattro */
2519 0, /* tp_as_buffer */
2520 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2521 0, /* tp_doc */
2522 (traverseproc)tuplegetter_traverse, /* tp_traverse */
2523 (inquiry)tuplegetter_clear, /* tp_clear */
2524 0, /* tp_richcompare */
2525 0, /* tp_weaklistoffset */
2526 0, /* tp_iter */
2527 0, /* tp_iternext */
Joe Jevnikf36f8922019-02-21 16:00:40 -05002528 tuplegetter_methods, /* tp_methods */
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002529 tuplegetter_members, /* tp_members */
2530 0, /* tp_getset */
2531 0, /* tp_base */
2532 0, /* tp_dict */
Serhiy Storchaka052b2df2018-12-31 14:15:16 +02002533 tuplegetter_descr_get, /* tp_descr_get */
2534 tuplegetter_descr_set, /* tp_descr_set */
Pablo Galindo3f5fc702018-12-30 09:24:03 +00002535 0, /* tp_dictoffset */
2536 0, /* tp_init */
2537 0, /* tp_alloc */
2538 tuplegetter_new, /* tp_new */
2539 0,
2540};
2541
2542
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002543/* module level code ********************************************************/
2544
Dong-hee Na77248a22020-03-20 01:16:04 +09002545PyDoc_STRVAR(collections_doc,
Guido van Rossum1968ad32006-02-25 22:38:04 +00002546"High performance data structures.\n\
2547- deque: ordered collection accessible from endpoints only\n\
2548- defaultdict: dict subclass with a default value factory\n\
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002549");
2550
Dong-hee Na77248a22020-03-20 01:16:04 +09002551static struct PyMethodDef collections_methods[] = {
Raymond Hettingere9858042019-06-05 16:05:25 -07002552 _COLLECTIONS__COUNT_ELEMENTS_METHODDEF
Raymond Hettinger96f34102010-12-15 16:30:37 +00002553 {NULL, NULL} /* sentinel */
2554};
Martin v. Löwis1a214512008-06-11 05:26:20 +00002555
Dong-hee Na77248a22020-03-20 01:16:04 +09002556static int
2557collections_exec(PyObject *module) {
2558 PyTypeObject *typelist[] = {
2559 &deque_type,
2560 &defdict_type,
2561 &PyODict_Type,
2562 &dequeiter_type,
2563 &dequereviter_type,
Dong-hee Na05e4a292020-03-23 01:17:34 +09002564 &tuplegetter_type
Dong-hee Na77248a22020-03-20 01:16:04 +09002565 };
2566
2567 defdict_type.tp_base = &PyDict_Type;
2568
Dong-hee Na05e4a292020-03-23 01:17:34 +09002569 for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) {
2570 if (PyModule_AddType(module, typelist[i]) < 0) {
Dong-hee Na77248a22020-03-20 01:16:04 +09002571 return -1;
2572 }
2573 }
2574
2575 return 0;
2576}
2577
2578static struct PyModuleDef_Slot collections_slots[] = {
2579 {Py_mod_exec, collections_exec},
2580 {0, NULL}
2581};
2582
Martin v. Löwis1a214512008-06-11 05:26:20 +00002583static struct PyModuleDef _collectionsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 PyModuleDef_HEAD_INIT,
2585 "_collections",
Dong-hee Na77248a22020-03-20 01:16:04 +09002586 collections_doc,
2587 0,
2588 collections_methods,
2589 collections_slots,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 NULL,
2591 NULL,
2592 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002593};
2594
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002595PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002596PyInit__collections(void)
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002597{
Dong-hee Na77248a22020-03-20 01:16:04 +09002598 return PyModuleDef_Init(&_collectionsmodule);
Raymond Hettinger756b3f32004-01-29 06:37:52 +00002599}