Added repeat (for list*integet).

Added methods remove(), reverse() and index().
This commit is contained in:
Guido van Rossum 1991-03-06 13:07:53 +00:00
parent ce5ba841d9
commit ed98d48027
1 changed files with 99 additions and 1 deletions

View File

@ -318,6 +318,32 @@ list_concat(a, bb)
#undef b #undef b
} }
static object *
list_repeat(a, n)
listobject *a;
int n;
{
int i, j;
int size;
listobject *np;
object **p;
if (n < 0)
n = 0;
size = a->ob_size * n;
np = (listobject *) newlistobject(size);
if (np == NULL)
return NULL;
p = np->ob_item;
for (i = 0; i < n; i++) {
for (j = 0; j < a->ob_size; j++) {
*p = a->ob_item[j];
INCREF(*p);
p++;
}
}
return (object *) np;
}
static int static int
list_ass_item(a, i, v) list_ass_item(a, i, v)
listobject *a; listobject *a;
@ -461,6 +487,32 @@ listsort(self, args)
return None; return None;
} }
static object *
listreverse(self, args)
listobject *self;
object *args;
{
register object **p, **q;
register object *tmp;
if (args != NULL) {
err_badarg();
return NULL;
}
if (self->ob_size > 1) {
for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
p < q; p++, q--) {
tmp = *p;
*p = *q;
*q = tmp;
}
}
INCREF(None);
return None;
}
int int
sortlist(v) sortlist(v)
object *v; object *v;
@ -476,10 +528,56 @@ sortlist(v)
return 0; return 0;
} }
static object *
listindex(self, args)
listobject *self;
object *args;
{
int i;
if (args == NULL) {
err_badarg();
return NULL;
}
for (i = 0; i < self->ob_size; i++) {
if (cmpobject(self->ob_item[i], args) == 0)
return newintobject(i);
}
err_setstr(RuntimeError, "list.index(x): x not in list");
return NULL;
}
static object *
listremove(self, args)
listobject *self;
object *args;
{
int i;
if (args == NULL) {
err_badarg();
return NULL;
}
for (i = 0; i < self->ob_size; i++) {
if (cmpobject(self->ob_item[i], args) == 0) {
if (list_ass_slice(self, i, i+1, (object *)NULL) != 0)
return NULL;
INCREF(None);
return None;
}
}
err_setstr(RuntimeError, "list.remove(x): x not in list");
return NULL;
}
static struct methodlist list_methods[] = { static struct methodlist list_methods[] = {
{"append", listappend}, {"append", listappend},
{"index", listindex},
{"insert", listinsert}, {"insert", listinsert},
{"sort", listsort}, {"sort", listsort},
{"remove", listremove},
{"reverse", listreverse},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
@ -494,7 +592,7 @@ list_getattr(f, name)
static sequence_methods list_as_sequence = { static sequence_methods list_as_sequence = {
list_length, /*sq_length*/ list_length, /*sq_length*/
list_concat, /*sq_concat*/ list_concat, /*sq_concat*/
0, /*sq_repeat*/ list_repeat, /*sq_repeat*/
list_item, /*sq_item*/ list_item, /*sq_item*/
list_slice, /*sq_slice*/ list_slice, /*sq_slice*/
list_ass_item, /*sq_ass_item*/ list_ass_item, /*sq_ass_item*/