summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-02-08 04:05:26 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-02-08 04:05:26 (GMT)
commitee33b27ef0bd49227aa59f0d3bca67e7f1e0ab64 (patch)
tree84eaeb0aa5137a9fc753fec4271421f761487abb /Modules
parent3b6d025d9bc6a0109e9a2ebd28a4864e8007193a (diff)
downloadcpython-ee33b27ef0bd49227aa59f0d3bca67e7f1e0ab64.zip
cpython-ee33b27ef0bd49227aa59f0d3bca67e7f1e0ab64.tar.gz
cpython-ee33b27ef0bd49227aa59f0d3bca67e7f1e0ab64.tar.bz2
Make deque.rotate() smarter. Beef-up related tests.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/collectionsmodule.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c
index d60ede1..0e0b2d6 100644
--- a/Modules/collectionsmodule.c
+++ b/Modules/collectionsmodule.c
@@ -247,14 +247,21 @@ PyDoc_STRVAR(extendleft_doc,
static PyObject *
deque_rotate(dequeobject *deque, PyObject *args)
{
- int i, n;
+ int i, n=1, len=deque->len, halflen=(len+1)>>1;
PyObject *item, *rv;
- if (!PyArg_ParseTuple(args, "i:rotate", &n))
+ if (!PyArg_ParseTuple(args, "|i:rotate", &n))
return NULL;
- if (n == 0 || deque->len == 0)
+ if (len == 0)
Py_RETURN_NONE;
+ if (n > halflen || n < -halflen) {
+ n %= len;
+ if (n > halflen)
+ n -= len;
+ else if (n < -halflen)
+ n += len;
+ }
for (i=0 ; i<n ; i++) {
item = deque_pop(deque, NULL);
@@ -280,7 +287,7 @@ deque_rotate(dequeobject *deque, PyObject *args)
}
PyDoc_STRVAR(rotate_doc,
-"Rotate the deque n steps to the right. If n is negative, rotates left.");
+"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
static int
deque_len(dequeobject *deque)