Commit 4ff9b619 authored by Krzysztof Klinikowski's avatar Krzysztof Klinikowski

Implement list.index()

parent bed955e7
......@@ -366,6 +366,21 @@ Box* listCount(BoxedList* self, Box* elt) {
return new BoxedInt(count);
}
Box* listIndex(BoxedList* self, Box* elt) {
int size = self->size;
for (int i = 0; i < size; i++) {
Box* e = self->elts->elts[i];
Box* cmp = compareInternal(e, elt, AST_TYPE::Eq, NULL);
bool b = nonzero(cmp);
if (b)
return new BoxedInt(i);
}
BoxedString* tostr = static_cast<BoxedString*>(repr(elt));
raiseExcHelper(ValueError, "%s is not in list", tostr->s.c_str());
}
Box* listRemove(BoxedList* self, Box* elt) {
assert(self->cls == list_cls);
......@@ -473,6 +488,7 @@ void setupList() {
list_cls->giveAttr("__new__", new BoxedFunction(new_));
list_cls->giveAttr("count", new BoxedFunction(boxRTFunction((void*)listCount, BOXED_INT, 2, false)));
list_cls->giveAttr("index", new BoxedFunction(boxRTFunction((void*)listIndex, NULL, 2, false)));
list_cls->giveAttr("remove", new BoxedFunction(boxRTFunction((void*)listRemove, NONE, 2, false)));
list_cls->giveAttr("reverse", new BoxedFunction(boxRTFunction((void*)listReverse, NONE, 1, false)));
list_cls->freeze();
......
......@@ -49,6 +49,13 @@ for i in xrange(5):
l.reverse()
print l
# list index
list_index = [1, 2, 3, 4, 5]
for i in xrange(1, 6):
assert list_index.index(i) == i-1
assert list_index.index(3) == 2
assert [1, '2'].index('2') == 1
# growing and shrinking a list:
l = []
for i in xrange(100):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment