how not use memmove when insert a object in the list
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

how not use memmove when insert a object in the list

From: kyo guan <kyoguan@gmail.com>
Date: Sun Apr 30 2006 - 03:57:13 CEST

Hi :

        python list object like a stl vector, if insert a object in the front or the middle of it,
all the object after the insert point need to move backward.

look at this code ( in python 2.4.3)

static int
ins1(PyListObject *self, int where, PyObject *v)
{
        int i, n = self->ob_size;
        PyObject **items;
        if (v == NULL) {
                PyErr_BadInternalCall();
                return -1;
        }
        if (n == INT_MAX) {
                PyErr_SetString(PyExc_OverflowError,
                        "cannot add more objects to list");
                return -1;
        }

        if (list_resize(self, n+1) == -1)
                return -1;

        if (where < 0) {
                where += n;
                if (where < 0)
                        where = 0;
        }
        if (where > n)
                where = n;
        items = self->ob_item;
        for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop.
                items[i+1] = items[i];
        Py_INCREF(v);
        items[where] = v;
        return 0;
}

Kyo.
Received on Mon May 1 00:46:24 2006