how not use memmove when insert a object in the list
- From: "kyo guan" <kyoguan@xxxxxxxxx>
- Date: Sun, 30 Apr 2006 09:57:13 +0800
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.
.
- Follow-Ups:
- Re: how not use memmove when insert a object in the list
- From: John Machin
- Re: how not use memmove when insert a object in the list
- References:
- Re: resume picking items from a previous list
- From: James Stroud
- Re: resume picking items from a previous list
- Prev by Date: Re: resume picking items from a previous list
- Next by Date: Re: Need help removing list elements.
- Previous by thread: Re: resume picking items from a previous list
- Next by thread: Re: how not use memmove when insert a object in the list
- Index(es):