Mercurial > cpython-lru_cache-weakref
annotate Modules/_collectionsmodule.c @ 44825:ef859cd754a7 legacy-trunk
Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Sun, 20 Jan 2008 14:28:28 +0000 |
| parents | 26384b8180c7 |
| children | e94af0c7aa12 |
| rev | line source |
|---|---|
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1 #include "Python.h" |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
2 #include "structmember.h" |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
3 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
4 /* collections module implementation of a deque() datatype |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
5 Written and maintained by Raymond D. Hettinger <python@rcn.com> |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
6 Copyright (c) 2004 Python Software Foundation. |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
7 All rights reserved. |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
8 */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
9 |
|
33643
91634f3ba213
Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents:
33642
diff
changeset
|
10 /* The block length may be set to any number over 1. Larger numbers |
|
91634f3ba213
Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents:
33642
diff
changeset
|
11 * reduce the number of calls to the memory allocator but take more |
|
91634f3ba213
Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents:
33642
diff
changeset
|
12 * memory. Ideally, BLOCKLEN should be set with an eye to the |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
13 * length of a cache line. |
|
33643
91634f3ba213
Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents:
33642
diff
changeset
|
14 */ |
|
91634f3ba213
Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents:
33642
diff
changeset
|
15 |
| 33879 | 16 #define BLOCKLEN 62 |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
17 #define CENTER ((BLOCKLEN - 1) / 2) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
18 |
|
33637
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
19 /* A `dequeobject` is composed of a doubly-linked list of `block` nodes. |
|
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
20 * This list is not circular (the leftmost block has leftlink==NULL, |
|
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
21 * and the rightmost block has rightlink==NULL). A deque d's first |
|
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
22 * element is at d.leftblock[leftindex] and its last element is at |
|
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
23 * d.rightblock[rightindex]; note that, unlike as for Python slice |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
24 * indices, these indices are inclusive on both ends. By being inclusive |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
25 * on both ends, algorithms for left and right operations become |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
26 * symmetrical which simplifies the design. |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
27 * |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
28 * The list of blocks is never empty, so d.leftblock and d.rightblock |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
29 * are never equal to NULL. |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
30 * |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
31 * The indices, d.leftindex and d.rightindex are always in the range |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
32 * 0 <= index < BLOCKLEN. |
|
33642
4fb8c89f66a0
Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents:
33641
diff
changeset
|
33 * Their exact relationship is: |
|
4fb8c89f66a0
Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents:
33641
diff
changeset
|
34 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex. |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
35 * |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
36 * Empty deques have d.len == 0; d.leftblock==d.rightblock; |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
37 * d.leftindex == CENTER+1; and d.rightindex == CENTER. |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
38 * Checking for d.len == 0 is the intended way to see whether d is empty. |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
39 * |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
40 * Whenever d.leftblock == d.rightblock, |
|
33642
4fb8c89f66a0
Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents:
33641
diff
changeset
|
41 * d.leftindex + d.len - 1 == d.rightindex. |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
42 * |
|
33642
4fb8c89f66a0
Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents:
33641
diff
changeset
|
43 * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
44 * become indices into distinct blocks and either may be larger than the |
|
33642
4fb8c89f66a0
Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents:
33641
diff
changeset
|
45 * other. |
|
33637
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
46 */ |
|
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
47 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
48 typedef struct BLOCK { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
49 struct BLOCK *leftlink; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
50 struct BLOCK *rightlink; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
51 PyObject *data[BLOCKLEN]; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
52 } block; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
53 |
|
43812
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
54 #define MAXFREEBLOCKS 10 |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
55 static int numfreeblocks = 0; |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
56 static block *freeblocks[MAXFREEBLOCKS]; |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
57 |
|
33635
836e92cc5c4f
Definition consistency.
Tim Peters <tim.peters@gmail.com>
parents:
33634
diff
changeset
|
58 static block * |
|
33677
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
59 newblock(block *leftlink, block *rightlink, int len) { |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
60 block *b; |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
61 /* To prevent len from overflowing INT_MAX on 64-bit machines, we |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
62 * refuse to allocate new blocks if the current len is dangerously |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
63 * close. There is some extra margin to prevent spurious arithmetic |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
64 * overflows at various places. The following check ensures that |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
65 * the blocks allocated to the deque, in the worst case, can only |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
66 * have INT_MAX-2 entries in total. |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
67 */ |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
68 if (len >= INT_MAX - 2*BLOCKLEN) { |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
69 PyErr_SetString(PyExc_OverflowError, |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
70 "cannot add more blocks to the deque"); |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
71 return NULL; |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
72 } |
|
43812
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
73 if (numfreeblocks) { |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
74 numfreeblocks -= 1; |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
75 b = freeblocks[numfreeblocks]; |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
76 } else { |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
77 b = PyMem_Malloc(sizeof(block)); |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
78 if (b == NULL) { |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
79 PyErr_NoMemory(); |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
80 return NULL; |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
81 } |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
82 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
83 b->leftlink = leftlink; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
84 b->rightlink = rightlink; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
85 return b; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
86 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
87 |
|
43812
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
88 void |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
89 freeblock(block *b) |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
90 { |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
91 if (numfreeblocks < MAXFREEBLOCKS) { |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
92 freeblocks[numfreeblocks] = b; |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
93 numfreeblocks++; |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
94 } else { |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
95 PyMem_Free(b); |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
96 } |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
97 } |
|
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
98 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
99 typedef struct { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
100 PyObject_HEAD |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
101 block *leftblock; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
102 block *rightblock; |
|
33637
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
103 int leftindex; /* in range(BLOCKLEN) */ |
|
6f4bcb3bd202
Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents:
33635
diff
changeset
|
104 int rightindex; /* in range(BLOCKLEN) */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
105 int len; |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
106 int maxlen; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
107 long state; /* incremented whenever the indices move */ |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
108 PyObject *weakreflist; /* List of weak references */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
109 } dequeobject; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
110 |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
111 /* The deque's size limit is d.maxlen. The limit can be zero or positive. |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
112 * If there is no limit, then d.maxlen == -1. |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
113 * |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
114 * After an item is added to a deque, we check to see if the size has grown past |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
115 * the limit. If it has, we get the size back down to the limit by popping an |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
116 * item off of the opposite end. The methods that can trigger this are append(), |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
117 * appendleft(), extend(), and extendleft(). |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
118 */ |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
119 |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
120 #define TRIM(d, popfunction) \ |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
121 if (d->maxlen != -1 && d->len > d->maxlen) { \ |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
122 PyObject *rv = popfunction(d, NULL); \ |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
123 assert(rv != NULL && d->len <= d->maxlen); \ |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
124 Py_DECREF(rv); \ |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
125 } |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
126 |
|
31605
2fed331de014
Make deque_type static so namespace is not polluted.
Neal Norwitz <nnorwitz@gmail.com>
parents:
31603
diff
changeset
|
127 static PyTypeObject deque_type; |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
128 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
129 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
130 deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
131 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
132 dequeobject *deque; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
133 block *b; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
134 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
135 /* create dequeobject structure */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
136 deque = (dequeobject *)type->tp_alloc(type, 0); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
137 if (deque == NULL) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
138 return NULL; |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
139 |
|
33677
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
140 b = newblock(NULL, NULL, 0); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
141 if (b == NULL) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
142 Py_DECREF(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
143 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
144 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
145 |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
146 assert(BLOCKLEN >= 2); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
147 deque->leftblock = b; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
148 deque->rightblock = b; |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
149 deque->leftindex = CENTER + 1; |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
150 deque->rightindex = CENTER; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
151 deque->len = 0; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
152 deque->state = 0; |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
153 deque->weakreflist = NULL; |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
154 deque->maxlen = -1; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
155 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
156 return (PyObject *)deque; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
157 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
158 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
159 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
160 deque_pop(dequeobject *deque, PyObject *unused) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
161 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
162 PyObject *item; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
163 block *prevblock; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
164 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
165 if (deque->len == 0) { |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
166 PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
167 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
168 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
169 item = deque->rightblock->data[deque->rightindex]; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
170 deque->rightindex--; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
171 deque->len--; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
172 deque->state++; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
173 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
174 if (deque->rightindex == -1) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
175 if (deque->len == 0) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
176 assert(deque->leftblock == deque->rightblock); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
177 assert(deque->leftindex == deque->rightindex+1); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
178 /* re-center instead of freeing a block */ |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
179 deque->leftindex = CENTER + 1; |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
180 deque->rightindex = CENTER; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
181 } else { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
182 prevblock = deque->rightblock->leftlink; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
183 assert(deque->leftblock != deque->rightblock); |
|
43812
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
184 freeblock(deque->rightblock); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
185 prevblock->rightlink = NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
186 deque->rightblock = prevblock; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
187 deque->rightindex = BLOCKLEN - 1; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
188 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
189 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
190 return item; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
191 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
192 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
193 PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element."); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
194 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
195 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
196 deque_popleft(dequeobject *deque, PyObject *unused) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
197 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
198 PyObject *item; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
199 block *prevblock; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
200 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
201 if (deque->len == 0) { |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
202 PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
203 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
204 } |
|
39475
249a59acb6ef
Really address the issue of where to place the assert for leftblock.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39451
diff
changeset
|
205 assert(deque->leftblock != NULL); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
206 item = deque->leftblock->data[deque->leftindex]; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
207 deque->leftindex++; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
208 deque->len--; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
209 deque->state++; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
210 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
211 if (deque->leftindex == BLOCKLEN) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
212 if (deque->len == 0) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
213 assert(deque->leftblock == deque->rightblock); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
214 assert(deque->leftindex == deque->rightindex+1); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
215 /* re-center instead of freeing a block */ |
|
33641
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
216 deque->leftindex = CENTER + 1; |
|
cd70f1ac3c25
* Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents:
33639
diff
changeset
|
217 deque->rightindex = CENTER; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
218 } else { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
219 assert(deque->leftblock != deque->rightblock); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
220 prevblock = deque->leftblock->rightlink; |
|
43812
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
221 freeblock(deque->leftblock); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
222 assert(prevblock != NULL); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
223 prevblock->leftlink = NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
224 deque->leftblock = prevblock; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
225 deque->leftindex = 0; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
226 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
227 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
228 return item; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
229 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
230 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
231 PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element."); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
232 |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
233 static PyObject * |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
234 deque_append(dequeobject *deque, PyObject *item) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
235 { |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
236 deque->state++; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
237 if (deque->rightindex == BLOCKLEN-1) { |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
238 block *b = newblock(deque->rightblock, NULL, deque->len); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
239 if (b == NULL) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
240 return NULL; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
241 assert(deque->rightblock->rightlink == NULL); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
242 deque->rightblock->rightlink = b; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
243 deque->rightblock = b; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
244 deque->rightindex = -1; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
245 } |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
246 Py_INCREF(item); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
247 deque->len++; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
248 deque->rightindex++; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
249 deque->rightblock->data[deque->rightindex] = item; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
250 TRIM(deque, deque_popleft); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
251 Py_RETURN_NONE; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
252 } |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
253 |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
254 PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque."); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
255 |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
256 static PyObject * |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
257 deque_appendleft(dequeobject *deque, PyObject *item) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
258 { |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
259 deque->state++; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
260 if (deque->leftindex == 0) { |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
261 block *b = newblock(NULL, deque->leftblock, deque->len); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
262 if (b == NULL) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
263 return NULL; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
264 assert(deque->leftblock->leftlink == NULL); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
265 deque->leftblock->leftlink = b; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
266 deque->leftblock = b; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
267 deque->leftindex = BLOCKLEN; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
268 } |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
269 Py_INCREF(item); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
270 deque->len++; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
271 deque->leftindex--; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
272 deque->leftblock->data[deque->leftindex] = item; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
273 TRIM(deque, deque_pop); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
274 Py_RETURN_NONE; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
275 } |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
276 |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
277 PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque."); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
278 |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
279 static PyObject * |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
280 deque_extend(dequeobject *deque, PyObject *iterable) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
281 { |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
282 PyObject *it, *item; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
283 |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
284 it = PyObject_GetIter(iterable); |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
285 if (it == NULL) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
286 return NULL; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
287 |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
288 while ((item = PyIter_Next(it)) != NULL) { |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
289 deque->state++; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
290 if (deque->rightindex == BLOCKLEN-1) { |
|
33677
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
291 block *b = newblock(deque->rightblock, NULL, |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
292 deque->len); |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
293 if (b == NULL) { |
|
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
294 Py_DECREF(item); |
|
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
295 Py_DECREF(it); |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
296 return NULL; |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
297 } |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
298 assert(deque->rightblock->rightlink == NULL); |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
299 deque->rightblock->rightlink = b; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
300 deque->rightblock = b; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
301 deque->rightindex = -1; |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
302 } |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
303 deque->len++; |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
304 deque->rightindex++; |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
305 deque->rightblock->data[deque->rightindex] = item; |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
306 TRIM(deque, deque_popleft); |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
307 } |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
308 Py_DECREF(it); |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
309 if (PyErr_Occurred()) |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
310 return NULL; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
311 Py_RETURN_NONE; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
312 } |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
313 |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
314 PyDoc_STRVAR(extend_doc, |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
315 "Extend the right side of the deque with elements from the iterable"); |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
316 |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
317 static PyObject * |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
318 deque_extendleft(dequeobject *deque, PyObject *iterable) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
319 { |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
320 PyObject *it, *item; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
321 |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
322 it = PyObject_GetIter(iterable); |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
323 if (it == NULL) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
324 return NULL; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
325 |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
326 while ((item = PyIter_Next(it)) != NULL) { |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
327 deque->state++; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
328 if (deque->leftindex == 0) { |
|
33677
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
329 block *b = newblock(NULL, deque->leftblock, |
|
262e1cf41d34
Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
330 deque->len); |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
331 if (b == NULL) { |
|
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
332 Py_DECREF(item); |
|
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
333 Py_DECREF(it); |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
334 return NULL; |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
335 } |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
336 assert(deque->leftblock->leftlink == NULL); |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
337 deque->leftblock->leftlink = b; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
338 deque->leftblock = b; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
339 deque->leftindex = BLOCKLEN; |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
340 } |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
341 deque->len++; |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
342 deque->leftindex--; |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
343 deque->leftblock->data[deque->leftindex] = item; |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
344 TRIM(deque, deque_pop); |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
345 } |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
346 Py_DECREF(it); |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
347 if (PyErr_Occurred()) |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
348 return NULL; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
349 Py_RETURN_NONE; |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
350 } |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
351 |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
352 PyDoc_STRVAR(extendleft_doc, |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
353 "Extend the left side of the deque with elements from the iterable"); |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
354 |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
355 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
356 _deque_rotate(dequeobject *deque, Py_ssize_t n) |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
357 { |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
358 int i, len=deque->len, halflen=(len+1)>>1; |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
359 PyObject *item, *rv; |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
360 |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
361 if (len == 0) |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
362 return 0; |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
363 if (n > halflen || n < -halflen) { |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
364 n %= len; |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
365 if (n > halflen) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
366 n -= len; |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
367 else if (n < -halflen) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
368 n += len; |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
369 } |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
370 |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
371 for (i=0 ; i<n ; i++) { |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
372 item = deque_pop(deque, NULL); |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
373 assert (item != NULL); |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
374 rv = deque_appendleft(deque, item); |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
375 Py_DECREF(item); |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
376 if (rv == NULL) |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
377 return -1; |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
378 Py_DECREF(rv); |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
379 } |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
380 for (i=0 ; i>n ; i--) { |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
381 item = deque_popleft(deque, NULL); |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
382 assert (item != NULL); |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
383 rv = deque_append(deque, item); |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
384 Py_DECREF(item); |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
385 if (rv == NULL) |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
386 return -1; |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
387 Py_DECREF(rv); |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
388 } |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
389 return 0; |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
390 } |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
391 |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
392 static PyObject * |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
393 deque_rotate(dequeobject *deque, PyObject *args) |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
394 { |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
395 int n=1; |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
396 |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
397 if (!PyArg_ParseTuple(args, "|i:rotate", &n)) |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
398 return NULL; |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
399 if (_deque_rotate(deque, n) == 0) |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
400 Py_RETURN_NONE; |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
401 return NULL; |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
402 } |
|
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
403 |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
404 PyDoc_STRVAR(rotate_doc, |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
405 "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left."); |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
406 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
407 static Py_ssize_t |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
408 deque_len(dequeobject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
409 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
410 return deque->len; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
411 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
412 |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
413 static PyObject * |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
414 deque_remove(dequeobject *deque, PyObject *value) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
415 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
416 Py_ssize_t i, n=deque->len; |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
417 |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
418 for (i=0 ; i<n ; i++) { |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
419 PyObject *item = deque->leftblock->data[deque->leftindex]; |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
420 int cmp = PyObject_RichCompareBool(item, value, Py_EQ); |
|
34772
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
421 |
|
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
422 if (deque->len != n) { |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
423 PyErr_SetString(PyExc_IndexError, |
|
34772
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
424 "deque mutated during remove()."); |
|
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
425 return NULL; |
|
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
426 } |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
427 if (cmp > 0) { |
|
34772
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
428 PyObject *tgt = deque_popleft(deque, NULL); |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
429 assert (tgt != NULL); |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
430 Py_DECREF(tgt); |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
431 if (_deque_rotate(deque, i) == -1) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
432 return NULL; |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
433 Py_RETURN_NONE; |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
434 } |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
435 else if (cmp < 0) { |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
436 _deque_rotate(deque, i); |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
437 return NULL; |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
438 } |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
439 _deque_rotate(deque, -1); |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
440 } |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
441 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"); |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
442 return NULL; |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
443 } |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
444 |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
445 PyDoc_STRVAR(remove_doc, |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
446 "D.remove(value) -- remove first occurrence of value."); |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
447 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
448 static int |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
449 deque_clear(dequeobject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
450 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
451 PyObject *item; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
452 |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
453 while (deque->len) { |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
454 item = deque_pop(deque, NULL); |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
455 assert (item != NULL); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
456 Py_DECREF(item); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
457 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
458 assert(deque->leftblock == deque->rightblock && |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
459 deque->leftindex - 1 == deque->rightindex && |
|
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
460 deque->len == 0); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
461 return 0; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
462 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
463 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
464 static PyObject * |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
465 deque_item(dequeobject *deque, int i) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
466 { |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
467 block *b; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
468 PyObject *item; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
469 int n, index=i; |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
470 |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
471 if (i < 0 || i >= deque->len) { |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
472 PyErr_SetString(PyExc_IndexError, |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
473 "deque index out of range"); |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
474 return NULL; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
475 } |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
476 |
|
31615
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
477 if (i == 0) { |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
478 i = deque->leftindex; |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
479 b = deque->leftblock; |
|
31615
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
480 } else if (i == deque->len - 1) { |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
481 i = deque->rightindex; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
482 b = deque->rightblock; |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
483 } else { |
|
31615
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
484 i += deque->leftindex; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
485 n = i / BLOCKLEN; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
486 i %= BLOCKLEN; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
487 if (index < (deque->len >> 1)) { |
|
31615
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
488 b = deque->leftblock; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
489 while (n--) |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
490 b = b->rightlink; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
491 } else { |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
492 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
493 b = deque->rightblock; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
494 while (n--) |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
495 b = b->leftlink; |
|
739ab9de2a1b
Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents:
31607
diff
changeset
|
496 } |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
497 } |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
498 item = b->data[i]; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
499 Py_INCREF(item); |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
500 return item; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
501 } |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
502 |
|
32413
d7110bb6c13d
Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents:
32363
diff
changeset
|
503 /* delitem() implemented in terms of rotate for simplicity and reasonable |
|
d7110bb6c13d
Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents:
32363
diff
changeset
|
504 performance near the end points. If for some reason this method becomes |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
505 popular, it is not hard to re-implement this using direct data movement |
|
32413
d7110bb6c13d
Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents:
32363
diff
changeset
|
506 (similar to code in list slice assignment) and achieve a two or threefold |
|
d7110bb6c13d
Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents:
32363
diff
changeset
|
507 performance boost. |
|
d7110bb6c13d
Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents:
32363
diff
changeset
|
508 */ |
|
d7110bb6c13d
Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents:
32363
diff
changeset
|
509 |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
510 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
511 deque_del_item(dequeobject *deque, Py_ssize_t i) |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
512 { |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
513 PyObject *item; |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
514 |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
515 assert (i >= 0 && i < deque->len); |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
516 if (_deque_rotate(deque, -i) == -1) |
|
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
517 return -1; |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
518 |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
519 item = deque_popleft(deque, NULL); |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
520 assert (item != NULL); |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
521 Py_DECREF(item); |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
522 |
|
33697
aac4cc6618b5
Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents:
33677
diff
changeset
|
523 return _deque_rotate(deque, i); |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
524 } |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
525 |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
526 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
527 deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
528 { |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
529 PyObject *old_value; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
530 block *b; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
531 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i; |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
532 |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
533 if (i < 0 || i >= len) { |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
534 PyErr_SetString(PyExc_IndexError, |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
535 "deque index out of range"); |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
536 return -1; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
537 } |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
538 if (v == NULL) |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
539 return deque_del_item(deque, i); |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
31908
diff
changeset
|
540 |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
541 i += deque->leftindex; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
542 n = i / BLOCKLEN; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
543 i %= BLOCKLEN; |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
544 if (index <= halflen) { |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
545 b = deque->leftblock; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
546 while (n--) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
547 b = b->rightlink; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
548 } else { |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
549 n = (deque->leftindex + len - 1) / BLOCKLEN - n; |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
550 b = deque->rightblock; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
551 while (n--) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
552 b = b->leftlink; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
553 } |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
554 Py_INCREF(v); |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
555 old_value = b->data[i]; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
556 b->data[i] = v; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
557 Py_DECREF(old_value); |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
558 return 0; |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
559 } |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
560 |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
561 static PyObject * |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
562 deque_clearmethod(dequeobject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
563 { |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
564 int rv; |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
565 |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
566 rv = deque_clear(deque); |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32413
diff
changeset
|
567 assert (rv != -1); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
568 Py_RETURN_NONE; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
569 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
570 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
571 PyDoc_STRVAR(clear_doc, "Remove all elements from the deque."); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
572 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
573 static void |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
574 deque_dealloc(dequeobject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
575 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
576 PyObject_GC_UnTrack(deque); |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
577 if (deque->weakreflist != NULL) |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
578 PyObject_ClearWeakRefs((PyObject *) deque); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
579 if (deque->leftblock != NULL) { |
|
32752
dbd3a04eb0c6
Silence a GCC unused variable warning in debug builds.
Raymond Hettinger <python@rcn.com>
parents:
32701
diff
changeset
|
580 deque_clear(deque); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
581 assert(deque->leftblock != NULL); |
|
43812
f82a20dda465
Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
43517
diff
changeset
|
582 freeblock(deque->leftblock); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
583 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
584 deque->leftblock = NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
585 deque->rightblock = NULL; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
586 Py_TYPE(deque)->tp_free(deque); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
587 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
588 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
589 static int |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
590 deque_traverse(dequeobject *deque, visitproc visit, void *arg) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
591 { |
|
33639
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
592 block *b; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
593 PyObject *item; |
|
33639
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
594 int index; |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
595 int indexlo = deque->leftindex; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
596 |
|
33639
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
597 for (b = deque->leftblock; b != NULL; b = b->rightlink) { |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
598 const int indexhi = b == deque->rightblock ? |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
599 deque->rightindex : |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
600 BLOCKLEN - 1; |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
601 |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
602 for (index = indexlo; index <= indexhi; ++index) { |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
603 item = b->data[index]; |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
604 Py_VISIT(item); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
605 } |
|
33639
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33638
diff
changeset
|
606 indexlo = 0; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
607 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
608 return 0; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
609 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
610 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
611 static long |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
612 deque_nohash(PyObject *self) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
613 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
614 PyErr_SetString(PyExc_TypeError, "deque objects are unhashable"); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
615 return -1; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
616 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
617 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
618 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
619 deque_copy(PyObject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
620 { |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
621 if (((dequeobject *)deque)->maxlen == -1) |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
622 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL); |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
623 else |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
624 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi", |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
625 deque, ((dequeobject *)deque)->maxlen, NULL); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
626 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
627 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
628 PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque."); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
629 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
630 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
631 deque_reduce(dequeobject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
632 { |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
633 PyObject *dict, *result, *aslist; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
634 |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33879
diff
changeset
|
635 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__"); |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
636 if (dict == NULL) |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33879
diff
changeset
|
637 PyErr_Clear(); |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
638 aslist = PySequence_List((PyObject *)deque); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
639 if (aslist == NULL) { |
|
43469
f2c9f24a0f5a
dict could be NULL, so we need to XDECREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
43466
diff
changeset
|
640 Py_XDECREF(dict); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
641 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
642 } |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
643 if (dict == NULL) { |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
644 if (deque->maxlen == -1) |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
645 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist); |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
646 else |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
647 result = Py_BuildValue("O(Oi)", Py_TYPE(deque), aslist, deque->maxlen); |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
648 } else { |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
649 if (deque->maxlen == -1) |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
650 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict); |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
651 else |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
652 result = Py_BuildValue("O(Oi)O", Py_TYPE(deque), aslist, deque->maxlen, dict); |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
653 } |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
654 Py_XDECREF(dict); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
655 Py_DECREF(aslist); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
656 return result; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
657 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
658 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
659 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
660 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
661 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
662 deque_repr(PyObject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
663 { |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
664 PyObject *aslist, *result, *fmt; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
665 int i; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
666 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
667 i = Py_ReprEnter(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
668 if (i != 0) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
669 if (i < 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
670 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
671 return PyString_FromString("[...]"); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
672 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
673 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
674 aslist = PySequence_List(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
675 if (aslist == NULL) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
676 Py_ReprLeave(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
677 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
678 } |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
679 if (((dequeobject *)deque)->maxlen != -1) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
680 fmt = PyString_FromFormat("deque(%%r, maxlen=%i)", |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
681 ((dequeobject *)deque)->maxlen); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
682 else |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
683 fmt = PyString_FromString("deque(%r)"); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
684 if (fmt == NULL) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
685 Py_DECREF(aslist); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
686 Py_ReprLeave(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
687 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
688 } |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
689 result = PyString_Format(fmt, aslist); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
690 Py_DECREF(fmt); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
691 Py_DECREF(aslist); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
692 Py_ReprLeave(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
693 return result; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
694 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
695 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
696 static int |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
697 deque_tp_print(PyObject *deque, FILE *fp, int flags) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
698 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
699 PyObject *it, *item; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
700 char *emit = ""; /* No separator emitted on first pass */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
701 char *separator = ", "; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
702 int i; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
703 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
704 i = Py_ReprEnter(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
705 if (i != 0) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
706 if (i < 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
707 return i; |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
708 Py_BEGIN_ALLOW_THREADS |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
709 fputs("[...]", fp); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
710 Py_END_ALLOW_THREADS |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
711 return 0; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
712 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
713 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
714 it = PyObject_GetIter(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
715 if (it == NULL) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
716 return -1; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
717 |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
718 Py_BEGIN_ALLOW_THREADS |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
719 fputs("deque([", fp); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
720 Py_END_ALLOW_THREADS |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
721 while ((item = PyIter_Next(it)) != NULL) { |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
722 Py_BEGIN_ALLOW_THREADS |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
723 fputs(emit, fp); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
724 Py_END_ALLOW_THREADS |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
725 emit = separator; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
726 if (PyObject_Print(item, fp, 0) != 0) { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
727 Py_DECREF(item); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
728 Py_DECREF(it); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
729 Py_ReprLeave(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
730 return -1; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
731 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
732 Py_DECREF(item); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
733 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
734 Py_ReprLeave(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
735 Py_DECREF(it); |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
736 if (PyErr_Occurred()) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
737 return -1; |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
738 |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
739 Py_BEGIN_ALLOW_THREADS |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
740 if (((dequeobject *)deque)->maxlen == -1) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
741 fputs("])", fp); |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
742 else |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
743 fprintf(fp, "], maxlen=%d)", ((dequeobject *)deque)->maxlen); |
|
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
744 Py_END_ALLOW_THREADS |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
745 return 0; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
746 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
747 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
748 static PyObject * |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
749 deque_richcompare(PyObject *v, PyObject *w, int op) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
750 { |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
751 PyObject *it1=NULL, *it2=NULL, *x, *y; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
752 int b, vs, ws, cmp=-1; |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
753 |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
754 if (!PyObject_TypeCheck(v, &deque_type) || |
|
32119
dd1e55deaf52
Make type check work with subclasses
Raymond Hettinger <python@rcn.com>
parents:
32084
diff
changeset
|
755 !PyObject_TypeCheck(w, &deque_type)) { |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
756 Py_INCREF(Py_NotImplemented); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
757 return Py_NotImplemented; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
758 } |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
759 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
760 /* Shortcuts */ |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
761 vs = ((dequeobject *)v)->len; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
762 ws = ((dequeobject *)w)->len; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
763 if (op == Py_EQ) { |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
764 if (v == w) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
765 Py_RETURN_TRUE; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
766 if (vs != ws) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
767 Py_RETURN_FALSE; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
768 } |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
769 if (op == Py_NE) { |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
770 if (v == w) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
771 Py_RETURN_FALSE; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
772 if (vs != ws) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
773 Py_RETURN_TRUE; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
774 } |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
775 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
776 /* Search for the first index where items are different */ |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
777 it1 = PyObject_GetIter(v); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
778 if (it1 == NULL) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
779 goto done; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
780 it2 = PyObject_GetIter(w); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
781 if (it2 == NULL) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
782 goto done; |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
783 for (;;) { |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
784 x = PyIter_Next(it1); |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
785 if (x == NULL && PyErr_Occurred()) |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
786 goto done; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
787 y = PyIter_Next(it2); |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
788 if (x == NULL || y == NULL) |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
789 break; |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
790 b = PyObject_RichCompareBool(x, y, Py_EQ); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
791 if (b == 0) { |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
792 cmp = PyObject_RichCompareBool(x, y, op); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
793 Py_DECREF(x); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
794 Py_DECREF(y); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
795 goto done; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
796 } |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
797 Py_DECREF(x); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
798 Py_DECREF(y); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
799 if (b == -1) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
800 goto done; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
801 } |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
802 /* We reached the end of one deque or both */ |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
803 Py_XDECREF(x); |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
804 Py_XDECREF(y); |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
805 if (PyErr_Occurred()) |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
806 goto done; |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
807 switch (op) { |
|
33648
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
808 case Py_LT: cmp = y != NULL; break; /* if w was longer */ |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
809 case Py_LE: cmp = x == NULL; break; /* if v was not longer */ |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
810 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
811 case Py_NE: cmp = x != y; break; /* if one deque continues */ |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
812 case Py_GT: cmp = x != NULL; break; /* if v was longer */ |
|
7c50cde1e4fd
Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents:
33644
diff
changeset
|
813 case Py_GE: cmp = y == NULL; break; /* if w was not longer */ |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
814 } |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
815 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
816 done: |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
817 Py_XDECREF(it1); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
818 Py_XDECREF(it2); |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
819 if (cmp == 1) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
820 Py_RETURN_TRUE; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
821 if (cmp == 0) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
822 Py_RETURN_FALSE; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
823 return NULL; |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
824 } |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
825 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
826 static int |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
827 deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
828 { |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
829 PyObject *iterable = NULL; |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
830 PyObject *maxlenobj = NULL; |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
831 int maxlen = -1; |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
832 char *kwlist[] = {"iterable", "maxlen", 0}; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
833 |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
834 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
835 return -1; |
|
43517
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
836 if (maxlenobj != NULL && maxlenobj != Py_None) { |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
837 maxlen = PyInt_AsLong(maxlenobj); |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
838 if (maxlen == -1 && PyErr_Occurred()) |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
839 return -1; |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
840 if (maxlen < 0) { |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
841 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
842 return -1; |
|
da52b4c776a4
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents:
43475
diff
changeset
|
843 } |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
844 } |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
845 deque->maxlen = maxlen; |
|
44281
c2e72feb53b3
Fix bug 1604. deque.__init__() did not clear existing contents like list.__init__. Not a backport candidate.
Raymond Hettinger <python@rcn.com>
parents:
43812
diff
changeset
|
846 deque_clear(deque); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
847 if (iterable != NULL) { |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
848 PyObject *rv = deque_extend(deque, iterable); |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
849 if (rv == NULL) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
850 return -1; |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31438
diff
changeset
|
851 Py_DECREF(rv); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
852 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
853 return 0; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
854 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
855 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
856 static PySequenceMethods deque_as_sequence = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
857 (lenfunc)deque_len, /* sq_length */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
858 0, /* sq_concat */ |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
859 0, /* sq_repeat */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
860 (ssizeargfunc)deque_item, /* sq_item */ |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
861 0, /* sq_slice */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
862 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
863 }; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
864 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
865 /* deque object ********************************************************/ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
866 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
867 static PyObject *deque_iter(dequeobject *deque); |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
868 static PyObject *deque_reviter(dequeobject *deque); |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
869 PyDoc_STRVAR(reversed_doc, |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
870 "D.__reversed__() -- return a reverse iterator over the deque"); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
871 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
872 static PyMethodDef deque_methods[] = { |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
873 {"append", (PyCFunction)deque_append, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
874 METH_O, append_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
875 {"appendleft", (PyCFunction)deque_appendleft, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
876 METH_O, appendleft_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
877 {"clear", (PyCFunction)deque_clearmethod, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
878 METH_NOARGS, clear_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
879 {"__copy__", (PyCFunction)deque_copy, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
880 METH_NOARGS, copy_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
881 {"extend", (PyCFunction)deque_extend, |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
882 METH_O, extend_doc}, |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
883 {"extendleft", (PyCFunction)deque_extendleft, |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
884 METH_O, extendleft_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
885 {"pop", (PyCFunction)deque_pop, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
886 METH_NOARGS, pop_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
887 {"popleft", (PyCFunction)deque_popleft, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
888 METH_NOARGS, popleft_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
889 {"__reduce__", (PyCFunction)deque_reduce, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
890 METH_NOARGS, reduce_doc}, |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
891 {"remove", (PyCFunction)deque_remove, |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
892 METH_O, remove_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
893 {"__reversed__", (PyCFunction)deque_reviter, |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
894 METH_NOARGS, reversed_doc}, |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
895 {"rotate", (PyCFunction)deque_rotate, |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
896 METH_VARARGS, rotate_doc}, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
897 {NULL, NULL} /* sentinel */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
898 }; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
899 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
900 PyDoc_STRVAR(deque_doc, |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
43378
diff
changeset
|
901 "deque(iterable[, maxlen]) --> deque object\n\ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
902 \n\ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
903 Build an ordered collection accessible from endpoints only."); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
904 |
|
31605
2fed331de014
Make deque_type static so namespace is not polluted.
Neal Norwitz <nnorwitz@gmail.com>
parents:
31603
diff
changeset
|
905 static PyTypeObject deque_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41019
diff
changeset
|
906 PyVarObject_HEAD_INIT(NULL, 0) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
907 "collections.deque", /* tp_name */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
908 sizeof(dequeobject), /* tp_basicsize */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
909 0, /* tp_itemsize */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
910 /* methods */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
911 (destructor)deque_dealloc, /* tp_dealloc */ |
|
37305
5e1b6f553763
Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
912 deque_tp_print, /* tp_print */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
913 0, /* tp_getattr */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
914 0, /* tp_setattr */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
915 0, /* tp_compare */ |
|
37305
5e1b6f553763
Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
916 deque_repr, /* tp_repr */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
917 0, /* tp_as_number */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
918 &deque_as_sequence, /* tp_as_sequence */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
919 0, /* tp_as_mapping */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
920 deque_nohash, /* tp_hash */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
921 0, /* tp_call */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
922 0, /* tp_str */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
923 PyObject_GenericGetAttr, /* tp_getattro */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
924 0, /* tp_setattro */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
925 0, /* tp_as_buffer */ |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
926 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
927 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
928 deque_doc, /* tp_doc */ |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31605
diff
changeset
|
929 (traverseproc)deque_traverse, /* tp_traverse */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
930 (inquiry)deque_clear, /* tp_clear */ |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31595
diff
changeset
|
931 (richcmpfunc)deque_richcompare, /* tp_richcompare */ |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32119
diff
changeset
|
932 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
933 (getiterfunc)deque_iter, /* tp_iter */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
934 0, /* tp_iternext */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
935 deque_methods, /* tp_methods */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
936 0, /* tp_members */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
937 0, /* tp_getset */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
938 0, /* tp_base */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
939 0, /* tp_dict */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
940 0, /* tp_descr_get */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
941 0, /* tp_descr_set */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
942 0, /* tp_dictoffset */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
943 (initproc)deque_init, /* tp_init */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
944 PyType_GenericAlloc, /* tp_alloc */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
945 deque_new, /* tp_new */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
946 PyObject_GC_Del, /* tp_free */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
947 }; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
948 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
949 /*********************** Deque Iterator **************************/ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
950 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
951 typedef struct { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
952 PyObject_HEAD |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
953 int index; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
954 block *b; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
955 dequeobject *deque; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
956 long state; /* state when the iterator is created */ |
|
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
957 int counter; /* number of items remaining for iteration */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
958 } dequeiterobject; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
959 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
960 PyTypeObject dequeiter_type; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
961 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
962 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
963 deque_iter(dequeobject *deque) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
964 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
965 dequeiterobject *it; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
966 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
967 it = PyObject_New(dequeiterobject, &dequeiter_type); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
968 if (it == NULL) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
969 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
970 it->b = deque->leftblock; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
971 it->index = deque->leftindex; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
972 Py_INCREF(deque); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
973 it->deque = deque; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
974 it->state = deque->state; |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
975 it->counter = deque->len; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
976 return (PyObject *)it; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
977 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
978 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
979 static void |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
980 dequeiter_dealloc(dequeiterobject *dio) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
981 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
982 Py_XDECREF(dio->deque); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
983 Py_TYPE(dio)->tp_free(dio); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
984 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
985 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
986 static PyObject * |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
987 dequeiter_next(dequeiterobject *it) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
988 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
989 PyObject *item; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
990 |
|
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
991 if (it->deque->state != it->state) { |
|
31908
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31685
diff
changeset
|
992 it->counter = 0; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
993 PyErr_SetString(PyExc_RuntimeError, |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
994 "deque mutated during iteration"); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
995 return NULL; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
996 } |
|
40573
bd7e41de982f
Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents:
39475
diff
changeset
|
997 if (it->counter == 0) |
|
bd7e41de982f
Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents:
39475
diff
changeset
|
998 return NULL; |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
999 assert (!(it->b == it->deque->rightblock && |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1000 it->index > it->deque->rightindex)); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1001 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1002 item = it->b->data[it->index]; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1003 it->index++; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1004 it->counter--; |
|
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1005 if (it->index == BLOCKLEN && it->counter > 0) { |
|
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1006 assert (it->b->rightlink != NULL); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1007 it->b = it->b->rightlink; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1008 it->index = 0; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1009 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1010 Py_INCREF(item); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1011 return item; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1012 } |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1013 |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1014 static PyObject * |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1015 dequeiter_len(dequeiterobject *it) |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1016 { |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1017 return PyInt_FromLong(it->counter); |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1018 } |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1019 |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
36324
diff
changeset
|
1020 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1021 |
|
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1022 static PyMethodDef dequeiter_methods[] = { |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
36324
diff
changeset
|
1023 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc}, |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1024 {NULL, NULL} /* sentinel */ |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1025 }; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1026 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1027 PyTypeObject dequeiter_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41019
diff
changeset
|
1028 PyVarObject_HEAD_INIT(NULL, 0) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1029 "deque_iterator", /* tp_name */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1030 sizeof(dequeiterobject), /* tp_basicsize */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1031 0, /* tp_itemsize */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1032 /* methods */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1033 (destructor)dequeiter_dealloc, /* tp_dealloc */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1034 0, /* tp_print */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1035 0, /* tp_getattr */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1036 0, /* tp_setattr */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1037 0, /* tp_compare */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1038 0, /* tp_repr */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1039 0, /* tp_as_number */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1040 0, /* tp_as_sequence */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1041 0, /* tp_as_mapping */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1042 0, /* tp_hash */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1043 0, /* tp_call */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1044 0, /* tp_str */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1045 PyObject_GenericGetAttr, /* tp_getattro */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1046 0, /* tp_setattro */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1047 0, /* tp_as_buffer */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1048 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1049 0, /* tp_doc */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1050 0, /* tp_traverse */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1051 0, /* tp_clear */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1052 0, /* tp_richcompare */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1053 0, /* tp_weaklistoffset */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1054 PyObject_SelfIter, /* tp_iter */ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1055 (iternextfunc)dequeiter_next, /* tp_iternext */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1056 dequeiter_methods, /* tp_methods */ |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1057 0, |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1058 }; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1059 |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1060 /*********************** Deque Reverse Iterator **************************/ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1061 |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1062 PyTypeObject dequereviter_type; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1063 |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1064 static PyObject * |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1065 deque_reviter(dequeobject *deque) |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1066 { |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1067 dequeiterobject *it; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1068 |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1069 it = PyObject_New(dequeiterobject, &dequereviter_type); |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1070 if (it == NULL) |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1071 return NULL; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1072 it->b = deque->rightblock; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1073 it->index = deque->rightindex; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1074 Py_INCREF(deque); |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1075 it->deque = deque; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1076 it->state = deque->state; |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1077 it->counter = deque->len; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1078 return (PyObject *)it; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1079 } |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1080 |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1081 static PyObject * |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1082 dequereviter_next(dequeiterobject *it) |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1083 { |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1084 PyObject *item; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1085 if (it->counter == 0) |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1086 return NULL; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1087 |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1088 if (it->deque->state != it->state) { |
|
31908
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31685
diff
changeset
|
1089 it->counter = 0; |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1090 PyErr_SetString(PyExc_RuntimeError, |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1091 "deque mutated during iteration"); |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1092 return NULL; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1093 } |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
1094 assert (!(it->b == it->deque->leftblock && |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1095 it->index < it->deque->leftindex)); |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1096 |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1097 item = it->b->data[it->index]; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1098 it->index--; |
|
33644
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1099 it->counter--; |
|
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1100 if (it->index == -1 && it->counter > 0) { |
|
00c36e1bc50b
* Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents:
33643
diff
changeset
|
1101 assert (it->b->leftlink != NULL); |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1102 it->b = it->b->leftlink; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1103 it->index = BLOCKLEN - 1; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1104 } |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1105 Py_INCREF(item); |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1106 return item; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1107 } |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1108 |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1109 PyTypeObject dequereviter_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41019
diff
changeset
|
1110 PyVarObject_HEAD_INIT(NULL, 0) |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1111 "deque_reverse_iterator", /* tp_name */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1112 sizeof(dequeiterobject), /* tp_basicsize */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1113 0, /* tp_itemsize */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1114 /* methods */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1115 (destructor)dequeiter_dealloc, /* tp_dealloc */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1116 0, /* tp_print */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1117 0, /* tp_getattr */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1118 0, /* tp_setattr */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1119 0, /* tp_compare */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1120 0, /* tp_repr */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1121 0, /* tp_as_number */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1122 0, /* tp_as_sequence */ |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1123 0, /* tp_as_mapping */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1124 0, /* tp_hash */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1125 0, /* tp_call */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1126 0, /* tp_str */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1127 PyObject_GenericGetAttr, /* tp_getattro */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1128 0, /* tp_setattro */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1129 0, /* tp_as_buffer */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1130 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1131 0, /* tp_doc */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1132 0, /* tp_traverse */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1133 0, /* tp_clear */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1134 0, /* tp_richcompare */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1135 0, /* tp_weaklistoffset */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1136 PyObject_SelfIter, /* tp_iter */ |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1137 (iternextfunc)dequereviter_next, /* tp_iternext */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1138 dequeiter_methods, /* tp_methods */ |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1139 0, |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1140 }; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1141 |
| 36656 | 1142 /* defaultdict type *********************************************************/ |
| 1143 | |
| 1144 typedef struct { | |
| 1145 PyDictObject dict; | |
| 1146 PyObject *default_factory; | |
| 1147 } defdictobject; | |
| 1148 | |
| 1149 static PyTypeObject defdict_type; /* Forward */ | |
| 1150 | |
| 1151 PyDoc_STRVAR(defdict_missing_doc, | |
| 1152 "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ | |
|
41019
b0f26a7813c9
Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents:
40989
diff
changeset
|
1153 if self.default_factory is None: raise KeyError((key,))\n\ |
| 36656 | 1154 self[key] = value = self.default_factory()\n\ |
| 1155 return value\n\ | |
| 1156 "); | |
| 1157 | |
| 1158 static PyObject * | |
| 1159 defdict_missing(defdictobject *dd, PyObject *key) | |
| 1160 { | |
| 1161 PyObject *factory = dd->default_factory; | |
| 1162 PyObject *value; | |
| 1163 if (factory == NULL || factory == Py_None) { | |
| 1164 /* XXX Call dict.__missing__(key) */ | |
|
41019
b0f26a7813c9
Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents:
40989
diff
changeset
|
1165 PyObject *tup; |
|
b0f26a7813c9
Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents:
40989
diff
changeset
|
1166 tup = PyTuple_Pack(1, key); |
|
b0f26a7813c9
Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents:
40989
diff
changeset
|
1167 if (!tup) return NULL; |
|
b0f26a7813c9
Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents:
40989
diff
changeset
|
1168 PyErr_SetObject(PyExc_KeyError, tup); |
|
b0f26a7813c9
Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents:
40989
diff
changeset
|
1169 Py_DECREF(tup); |
| 36656 | 1170 return NULL; |
| 1171 } | |
| 1172 value = PyEval_CallObject(factory, NULL); | |
| 1173 if (value == NULL) | |
| 1174 return value; | |
| 1175 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) { | |
| 1176 Py_DECREF(value); | |
| 1177 return NULL; | |
| 1178 } | |
| 1179 return value; | |
| 1180 } | |
| 1181 | |
| 1182 PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D."); | |
| 1183 | |
| 1184 static PyObject * | |
| 1185 defdict_copy(defdictobject *dd) | |
| 1186 { | |
| 1187 /* This calls the object's class. That only works for subclasses | |
| 1188 whose class constructor has the same signature. Subclasses that | |
| 1189 define a different constructor signature must override copy(). | |
| 1190 */ | |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
1191 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), |
| 36656 | 1192 dd->default_factory, dd, NULL); |
| 1193 } | |
| 1194 | |
| 1195 static PyObject * | |
| 1196 defdict_reduce(defdictobject *dd) | |
| 1197 { | |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
1198 /* __reduce__ must return a 5-tuple as follows: |
| 36656 | 1199 |
| 1200 - factory function | |
| 1201 - tuple of args for the factory function | |
| 1202 - additional state (here None) | |
| 1203 - sequence iterator (here None) | |
| 1204 - dictionary iterator (yielding successive (key, value) pairs | |
| 1205 | |
| 1206 This API is used by pickle.py and copy.py. | |
| 1207 | |
| 1208 For this to be useful with pickle.py, the default_factory | |
| 1209 must be picklable; e.g., None, a built-in, or a global | |
| 1210 function in a module or package. | |
| 1211 | |
| 1212 Both shallow and deep copying are supported, but for deep | |
| 1213 copying, the default_factory must be deep-copyable; e.g. None, | |
| 1214 or a built-in (functions are not copyable at this time). | |
| 1215 | |
| 1216 This only works for subclasses as long as their constructor | |
| 1217 signature is compatible; the first argument must be the | |
| 1218 optional default_factory, defaulting to None. | |
| 1219 */ | |
| 1220 PyObject *args; | |
| 1221 PyObject *items; | |
| 1222 PyObject *result; | |
| 1223 if (dd->default_factory == NULL || dd->default_factory == Py_None) | |
| 1224 args = PyTuple_New(0); | |
| 1225 else | |
| 1226 args = PyTuple_Pack(1, dd->default_factory); | |
| 1227 if (args == NULL) | |
| 1228 return NULL; | |
| 1229 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()"); | |
| 1230 if (items == NULL) { | |
| 1231 Py_DECREF(args); | |
| 1232 return NULL; | |
| 1233 } | |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44281
diff
changeset
|
1234 result = PyTuple_Pack(5, Py_TYPE(dd), args, |
| 36656 | 1235 Py_None, Py_None, items); |
|
39232
cd8f20d400c1
defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents:
37748
diff
changeset
|
1236 Py_DECREF(items); |
| 36656 | 1237 Py_DECREF(args); |
| 1238 return result; | |
| 1239 } | |
| 1240 | |
| 1241 static PyMethodDef defdict_methods[] = { | |
| 1242 {"__missing__", (PyCFunction)defdict_missing, METH_O, | |
| 1243 defdict_missing_doc}, | |
| 1244 {"copy", (PyCFunction)defdict_copy, METH_NOARGS, | |
| 1245 defdict_copy_doc}, | |
| 1246 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS, | |
| 1247 defdict_copy_doc}, | |
| 1248 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS, | |
| 1249 reduce_doc}, | |
| 1250 {NULL} | |
| 1251 }; | |
| 1252 | |
| 1253 static PyMemberDef defdict_members[] = { | |
| 1254 {"default_factory", T_OBJECT, | |
| 1255 offsetof(defdictobject, default_factory), 0, | |
| 1256 PyDoc_STR("Factory for default value called by __missing__().")}, | |
| 1257 {NULL} | |
| 1258 }; | |
| 1259 | |
| 1260 static void | |
| 1261 defdict_dealloc(defdictobject *dd) | |
| 1262 { | |
| 1263 Py_CLEAR(dd->default_factory); | |
| 1264 PyDict_Type.tp_dealloc((PyObject *)dd); | |
| 1265 } | |
| 1266 | |
| 1267 static int | |
| 1268 defdict_print(defdictobject *dd, FILE *fp, int flags) | |
| 1269 { | |
| 1270 int sts; | |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1271 Py_BEGIN_ALLOW_THREADS |
| 36656 | 1272 fprintf(fp, "defaultdict("); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1273 Py_END_ALLOW_THREADS |
|
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1274 if (dd->default_factory == NULL) { |
|
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1275 Py_BEGIN_ALLOW_THREADS |
| 36656 | 1276 fprintf(fp, "None"); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1277 Py_END_ALLOW_THREADS |
|
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1278 } else { |
| 36656 | 1279 PyObject_Print(dd->default_factory, fp, 0); |
| 1280 } | |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1281 Py_BEGIN_ALLOW_THREADS |
| 36656 | 1282 fprintf(fp, ", "); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1283 Py_END_ALLOW_THREADS |
| 36656 | 1284 sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1285 Py_BEGIN_ALLOW_THREADS |
| 36656 | 1286 fprintf(fp, ")"); |
|
43475
17b07370a782
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents:
43469
diff
changeset
|
1287 Py_END_ALLOW_THREADS |
| 36656 | 1288 return sts; |
| 1289 } | |
| 1290 | |
| 1291 static PyObject * | |
| 1292 defdict_repr(defdictobject *dd) | |
| 1293 { | |
| 1294 PyObject *defrepr; | |
| 1295 PyObject *baserepr; | |
| 1296 PyObject *result; | |
| 1297 baserepr = PyDict_Type.tp_repr((PyObject *)dd); | |
| 1298 if (baserepr == NULL) | |
| 1299 return NULL; | |
| 1300 if (dd->default_factory == NULL) | |
| 1301 defrepr = PyString_FromString("None"); | |
| 1302 else | |
| 1303 defrepr = PyObject_Repr(dd->default_factory); | |
| 1304 if (defrepr == NULL) { | |
| 1305 Py_DECREF(baserepr); | |
| 1306 return NULL; | |
| 1307 } | |
| 1308 result = PyString_FromFormat("defaultdict(%s, %s)", | |
| 1309 PyString_AS_STRING(defrepr), | |
| 1310 PyString_AS_STRING(baserepr)); | |
| 1311 Py_DECREF(defrepr); | |
| 1312 Py_DECREF(baserepr); | |
| 1313 return result; | |
| 1314 } | |
| 1315 | |
| 1316 static int | |
| 1317 defdict_traverse(PyObject *self, visitproc visit, void *arg) | |
| 1318 { | |
| 1319 Py_VISIT(((defdictobject *)self)->default_factory); | |
| 1320 return PyDict_Type.tp_traverse(self, visit, arg); | |
| 1321 } | |
| 1322 | |
| 1323 static int | |
| 1324 defdict_tp_clear(defdictobject *dd) | |
| 1325 { | |
|
37748
d1547426ab61
Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents:
37470
diff
changeset
|
1326 Py_CLEAR(dd->default_factory); |
| 36656 | 1327 return PyDict_Type.tp_clear((PyObject *)dd); |
| 1328 } | |
| 1329 | |
| 1330 static int | |
| 1331 defdict_init(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1332 { | |
| 1333 defdictobject *dd = (defdictobject *)self; | |
| 1334 PyObject *olddefault = dd->default_factory; | |
| 1335 PyObject *newdefault = NULL; | |
| 1336 PyObject *newargs; | |
| 1337 int result; | |
| 1338 if (args == NULL || !PyTuple_Check(args)) | |
| 1339 newargs = PyTuple_New(0); | |
| 1340 else { | |
| 1341 Py_ssize_t n = PyTuple_GET_SIZE(args); | |
|
40760
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
1342 if (n > 0) { |
| 36656 | 1343 newdefault = PyTuple_GET_ITEM(args, 0); |
|
40760
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
1344 if (!PyCallable_Check(newdefault)) { |
|
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
1345 PyErr_SetString(PyExc_TypeError, |
|
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
1346 "first argument must be callable"); |
|
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
1347 return -1; |
|
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
1348 } |
|
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
1349 } |
| 36656 | 1350 newargs = PySequence_GetSlice(args, 1, n); |
| 1351 } | |
| 1352 if (newargs == NULL) | |
| 1353 return -1; | |
| 1354 Py_XINCREF(newdefault); | |
| 1355 dd->default_factory = newdefault; | |
| 1356 result = PyDict_Type.tp_init(self, newargs, kwds); | |
| 1357 Py_DECREF(newargs); | |
| 1358 Py_XDECREF(olddefault); | |
| 1359 return result; | |
| 1360 } | |
| 1361 | |
| 1362 PyDoc_STRVAR(defdict_doc, | |
| 1363 "defaultdict(default_factory) --> dict with default factory\n\ | |
| 1364 \n\ | |
| 1365 The default factory is called without arguments to produce\n\ | |
| 1366 a new value when a key is not present, in __getitem__ only.\n\ | |
| 1367 A defaultdict compares equal to a dict with the same items.\n\ | |
| 1368 "); | |
| 1369 | |
|
37470
9ecbce55a454
SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37305
diff
changeset
|
1370 /* See comment in xxsubtype.c */ |
|
9ecbce55a454
SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37305
diff
changeset
|
1371 #define DEFERRED_ADDRESS(ADDR) 0 |
|
9ecbce55a454
SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37305
diff
changeset
|
1372 |
| 36656 | 1373 static PyTypeObject defdict_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41019
diff
changeset
|
1374 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 36656 | 1375 "collections.defaultdict", /* tp_name */ |
| 1376 sizeof(defdictobject), /* tp_basicsize */ | |
| 1377 0, /* tp_itemsize */ | |
| 1378 /* methods */ | |
| 1379 (destructor)defdict_dealloc, /* tp_dealloc */ | |
| 1380 (printfunc)defdict_print, /* tp_print */ | |
| 1381 0, /* tp_getattr */ | |
| 1382 0, /* tp_setattr */ | |
| 1383 0, /* tp_compare */ | |
| 1384 (reprfunc)defdict_repr, /* tp_repr */ | |
| 1385 0, /* tp_as_number */ | |
| 1386 0, /* tp_as_sequence */ | |
| 1387 0, /* tp_as_mapping */ | |
| 1388 0, /* tp_hash */ | |
| 1389 0, /* tp_call */ | |
| 1390 0, /* tp_str */ | |
| 1391 PyObject_GenericGetAttr, /* tp_getattro */ | |
| 1392 0, /* tp_setattro */ | |
| 1393 0, /* tp_as_buffer */ | |
| 1394 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | | |
| 1395 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ | |
| 1396 defdict_doc, /* tp_doc */ | |
|
37305
5e1b6f553763
Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
1397 defdict_traverse, /* tp_traverse */ |
| 36656 | 1398 (inquiry)defdict_tp_clear, /* tp_clear */ |
| 1399 0, /* tp_richcompare */ | |
| 1400 0, /* tp_weaklistoffset*/ | |
| 1401 0, /* tp_iter */ | |
| 1402 0, /* tp_iternext */ | |
| 1403 defdict_methods, /* tp_methods */ | |
| 1404 defdict_members, /* tp_members */ | |
| 1405 0, /* tp_getset */ | |
|
37470
9ecbce55a454
SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37305
diff
changeset
|
1406 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */ |
| 36656 | 1407 0, /* tp_dict */ |
| 1408 0, /* tp_descr_get */ | |
| 1409 0, /* tp_descr_set */ | |
| 1410 0, /* tp_dictoffset */ | |
|
37305
5e1b6f553763
Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
1411 defdict_init, /* tp_init */ |
| 36656 | 1412 PyType_GenericAlloc, /* tp_alloc */ |
| 1413 0, /* tp_new */ | |
| 1414 PyObject_GC_Del, /* tp_free */ | |
| 1415 }; | |
| 1416 | |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1417 /* module level code ********************************************************/ |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1418 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1419 PyDoc_STRVAR(module_doc, |
| 36656 | 1420 "High performance data structures.\n\ |
| 1421 - deque: ordered collection accessible from endpoints only\n\ | |
| 1422 - defaultdict: dict subclass with a default value factory\n\ | |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1423 "); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1424 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1425 PyMODINIT_FUNC |
|
40989
4087c0ec733b
Prepare collections module for pure python code entries.
Raymond Hettinger <python@rcn.com>
parents:
40760
diff
changeset
|
1426 init_collections(void) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1427 { |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1428 PyObject *m; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1429 |
|
40989
4087c0ec733b
Prepare collections module for pure python code entries.
Raymond Hettinger <python@rcn.com>
parents:
40760
diff
changeset
|
1430 m = Py_InitModule3("_collections", NULL, module_doc); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35636
diff
changeset
|
1431 if (m == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35636
diff
changeset
|
1432 return; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1433 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1434 if (PyType_Ready(&deque_type) < 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1435 return; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1436 Py_INCREF(&deque_type); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1437 PyModule_AddObject(m, "deque", (PyObject *)&deque_type); |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1438 |
|
37470
9ecbce55a454
SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37305
diff
changeset
|
1439 defdict_type.tp_base = &PyDict_Type; |
| 36656 | 1440 if (PyType_Ready(&defdict_type) < 0) |
| 1441 return; | |
| 1442 Py_INCREF(&defdict_type); | |
| 1443 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); | |
| 1444 | |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1445 if (PyType_Ready(&dequeiter_type) < 0) |
|
33634
6bd4a1056716
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32752
diff
changeset
|
1446 return; |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1447 |
|
31685
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1448 if (PyType_Ready(&dequereviter_type) < 0) |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1449 return; |
|
aafe5fef58ba
Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents:
31615
diff
changeset
|
1450 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1451 return; |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1452 } |
