summaryrefslogtreecommitdiffstats
path: root/Modules/_collectionsmodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-12-10 00:47:21 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-12-10 00:47:21 (GMT)
commite5fdedbeda147f3878f82349464f28f184c01658 (patch)
treeb7a53644e818a23a1207de853919654d2398b729 /Modules/_collectionsmodule.c
parent3e761a98026731d24f3e54cca2f96dc2cf5fa575 (diff)
downloadcpython-e5fdedbeda147f3878f82349464f28f184c01658.zip
cpython-e5fdedbeda147f3878f82349464f28f184c01658.tar.gz
cpython-e5fdedbeda147f3878f82349464f28f184c01658.tar.bz2
Add a reverse() method to collections.deque().
Diffstat (limited to 'Modules/_collectionsmodule.c')
-rw-r--r--Modules/_collectionsmodule.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 68e0183..cdcf4ef 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -427,6 +427,48 @@ deque_rotate(dequeobject *deque, PyObject *args)
PyDoc_STRVAR(rotate_doc,
"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
+static PyObject *
+deque_reverse(dequeobject *deque, PyObject *unused)
+{
+ block *leftblock = deque->leftblock;
+ block *rightblock = deque->rightblock;
+ Py_ssize_t leftindex = deque->leftindex;
+ Py_ssize_t rightindex = deque->rightindex;
+ Py_ssize_t n = (deque->len)/2;
+ Py_ssize_t i;
+ PyObject *tmp;
+
+ for (i=0 ; i<n ; i++) {
+ /* Validate that pointers haven't met in the middle */
+ assert(leftblock != rightblock || leftindex < rightindex);
+
+ /* Swap */
+ tmp = leftblock->data[leftindex];
+ leftblock->data[leftindex] = rightblock->data[rightindex];
+ rightblock->data[rightindex] = tmp;
+
+ /* Advance left block/index pair */
+ leftindex++;
+ if (leftindex == BLOCKLEN) {
+ assert (leftblock->rightlink != NULL);
+ leftblock = leftblock->rightlink;
+ leftindex = 0;
+ }
+
+ /* Step backwards with the right block/index pair */
+ rightindex--;
+ if (rightindex == -1) {
+ assert (rightblock->leftlink != NULL);
+ rightblock = rightblock->leftlink;
+ rightindex = BLOCKLEN - 1;
+ }
+ }
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(reverse_doc,
+"D.reverse() -- reverse *IN PLACE*");
+
static Py_ssize_t
deque_len(dequeobject *deque)
{
@@ -865,6 +907,8 @@ static PyMethodDef deque_methods[] = {
METH_O, remove_doc},
{"__reversed__", (PyCFunction)deque_reviter,
METH_NOARGS, reversed_doc},
+ {"reverse", (PyCFunction)deque_reverse,
+ METH_NOARGS, reverse_doc},
{"rotate", (PyCFunction)deque_rotate,
METH_VARARGS, rotate_doc},
{NULL, NULL} /* sentinel */