[Python-checkins] python/dist/src/Modules collectionsmodule.c, 1.2,
1.3
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Fri Feb 6 14:04:59 EST 2004
Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15523/Modules
Modified Files:
collectionsmodule.c
Log Message:
Have deques support high volume loads.
Index: collectionsmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/collectionsmodule.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** collectionsmodule.c 29 Jan 2004 07:29:32 -0000 1.2
--- collectionsmodule.c 6 Feb 2004 19:04:56 -0000 1.3
***************
*** 175,178 ****
--- 175,244 ----
PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
+ static PyObject *
+ deque_extend(dequeobject *deque, PyObject *iterable)
+ {
+ PyObject *it, *item;
+
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ while ((item = PyIter_Next(it)) != NULL) {
+ deque->rightindex++;
+ deque->len++;
+ if (deque->rightindex == BLOCKLEN) {
+ block *b = newblock(deque->rightblock, NULL);
+ if (b == NULL)
+ return NULL;
+ assert(deque->rightblock->rightlink == NULL);
+ deque->rightblock->rightlink = b;
+ deque->rightblock = b;
+ deque->rightindex = 0;
+ }
+ Py_INCREF(item);
+ deque->rightblock->data[deque->rightindex] = item;
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_RETURN_NONE;
+ }
+
+ PyDoc_STRVAR(extend_doc,
+ "Extend the right side of the deque with elements from the iterable");
+
+ static PyObject *
+ deque_extendleft(dequeobject *deque, PyObject *iterable)
+ {
+ PyObject *it, *item;
+
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ while ((item = PyIter_Next(it)) != NULL) {
+ deque->leftindex--;
+ deque->len++;
+ if (deque->leftindex == -1) {
+ block *b = newblock(NULL, deque->leftblock);
+ if (b == NULL)
+ return NULL;
+ assert(deque->leftblock->leftlink == NULL);
+ deque->leftblock->leftlink = b;
+ deque->leftblock = b;
+ deque->leftindex = BLOCKLEN - 1;
+ }
+ Py_INCREF(item);
+ deque->leftblock->data[deque->leftindex] = item;
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_RETURN_NONE;
+ }
+
+ PyDoc_STRVAR(extendleft_doc,
+ "Extend the left side of the deque with elements from the iterable");
+
static int
deque_len(dequeobject *deque)
***************
*** 357,361 ****
deque_init(dequeobject *deque, PyObject *args, PyObject *kwds)
{
! PyObject *iterable = NULL, *it, *item;
if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable))
--- 423,427 ----
deque_init(dequeobject *deque, PyObject *args, PyObject *kwds)
{
! PyObject *iterable = NULL;
if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable))
***************
*** 363,389 ****
if (iterable != NULL) {
! it = PyObject_GetIter(iterable);
! if (it == NULL)
! return -1;
!
! while ((item = PyIter_Next(it)) != NULL) {
! deque->rightindex++;
! deque->len++;
! if (deque->rightindex == BLOCKLEN) {
! block *b = newblock(deque->rightblock, NULL);
! if (b == NULL) {
! Py_DECREF(it);
! Py_DECREF(item);
! return -1;
! }
! deque->rightblock->rightlink = b;
! deque->rightblock = b;
! deque->rightindex = 0;
! }
! deque->rightblock->data[deque->rightindex] = item;
! }
! Py_DECREF(it);
! if (PyErr_Occurred())
return -1;
}
return 0;
--- 429,436 ----
if (iterable != NULL) {
! PyObject *rv = deque_extend(deque, iterable);
! if (rv == NULL)
return -1;
+ Py_DECREF(rv);
}
return 0;
***************
*** 414,417 ****
--- 461,468 ----
{"__reduce__", (PyCFunction)deque_reduce,
METH_NOARGS, reduce_doc},
+ {"extend", (PyCFunction)deque_extend,
+ METH_O, extend_doc},
+ {"extendleft", (PyCFunction)deque_extendleft,
+ METH_O, extendleft_doc},
{NULL, NULL} /* sentinel */
};
More information about the Python-checkins
mailing list