diff options
author | Raymond Hettinger <python@rcn.com> | 2010-04-03 23:20:46 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-04-03 23:20:46 (GMT) |
commit | 44459debc620106ed156c6ae8bc32ec02e3dec98 (patch) | |
tree | 168e377d9f3b1314365a55a3cae3a33048653021 /Modules | |
parent | 4893abc77ab88fbcd3a80c5017194088cb3d5a61 (diff) | |
download | cpython-44459debc620106ed156c6ae8bc32ec02e3dec98.zip cpython-44459debc620106ed156c6ae8bc32ec02e3dec98.tar.gz cpython-44459debc620106ed156c6ae8bc32ec02e3dec98.tar.bz2 |
Add count() method to collections.deque().
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_collectionsmodule.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index ffb2a80..fa0f004 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -504,6 +504,46 @@ deque_reverse(dequeobject *deque, PyObject *unused) PyDoc_STRVAR(reverse_doc, "D.reverse() -- reverse *IN PLACE*"); +static PyObject * +deque_count(dequeobject *deque, PyObject *v) +{ + block *leftblock = deque->leftblock; + Py_ssize_t leftindex = deque->leftindex; + Py_ssize_t n = (deque->len); + Py_ssize_t i; + Py_ssize_t count = 0; + PyObject *item; + long start_state = deque->state; + int cmp; + + for (i=0 ; i<n ; i++) { + item = leftblock->data[leftindex]; + cmp = PyObject_RichCompareBool(item, v, Py_EQ); + if (cmp > 0) + count++; + else if (cmp < 0) + return NULL; + + if (start_state != deque->state) { + PyErr_SetString(PyExc_RuntimeError, + "deque mutated during iteration"); + return NULL; + } + + /* Advance left block/index pair */ + leftindex++; + if (leftindex == BLOCKLEN) { + assert (leftblock->rightlink != NULL); + leftblock = leftblock->rightlink; + leftindex = 0; + } + } + return PyLong_FromSsize_t(count); +} + +PyDoc_STRVAR(count_doc, +"D.count(value) -> integer -- return number of occurrences of value"); + static Py_ssize_t deque_len(dequeobject *deque) { @@ -933,6 +973,8 @@ static PyMethodDef deque_methods[] = { METH_NOARGS, clear_doc}, {"__copy__", (PyCFunction)deque_copy, METH_NOARGS, copy_doc}, + {"count", (PyCFunction)deque_count, + METH_O, count_doc}, {"extend", (PyCFunction)deque_extend, METH_O, extend_doc}, {"extendleft", (PyCFunction)deque_extendleft, |