diff options
author | Guido van Rossum <guido@python.org> | 1991-03-06 13:07:53 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1991-03-06 13:07:53 (GMT) |
commit | ed98d480279d4d89ca8fe7e10a9e8831755adaf3 (patch) | |
tree | 1755638c03a42b7e760a470e1535a8ff24092ab5 /Objects/listobject.c | |
parent | ce5ba841d996449810bcff72dc167e20a73481fe (diff) | |
download | cpython-ed98d480279d4d89ca8fe7e10a9e8831755adaf3.zip cpython-ed98d480279d4d89ca8fe7e10a9e8831755adaf3.tar.gz cpython-ed98d480279d4d89ca8fe7e10a9e8831755adaf3.tar.bz2 |
Added repeat (for list*integet).
Added methods remove(), reverse() and index().
Diffstat (limited to 'Objects/listobject.c')
-rw-r--r-- | Objects/listobject.c | 100 |
1 files changed, 99 insertions, 1 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index 02192a1..743951c 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -318,6 +318,32 @@ list_concat(a, bb) #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 list_ass_item(a, i, v) listobject *a; @@ -461,6 +487,32 @@ listsort(self, args) 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 sortlist(v) object *v; @@ -476,10 +528,56 @@ sortlist(v) 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[] = { {"append", listappend}, + {"index", listindex}, {"insert", listinsert}, {"sort", listsort}, + {"remove", listremove}, + {"reverse", listreverse}, {NULL, NULL} /* sentinel */ }; @@ -494,7 +592,7 @@ list_getattr(f, name) static sequence_methods list_as_sequence = { list_length, /*sq_length*/ list_concat, /*sq_concat*/ - 0, /*sq_repeat*/ + list_repeat, /*sq_repeat*/ list_item, /*sq_item*/ list_slice, /*sq_slice*/ list_ass_item, /*sq_ass_item*/ |