Commit d7799fcd authored by Leandro Lameiro's avatar Leandro Lameiro

Add support to list.count

parent 21b0ec56
......@@ -314,6 +314,19 @@ Box* listContains(BoxedList* self, Box *elt) {
return False;
}
Box* listCount(BoxedList* self, Box *elt) {
int size = self->size;
int count = 0;
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)
count++;
}
return new BoxedInt(count);
}
BoxedClass *list_iterator_cls = NULL;
......@@ -405,6 +418,8 @@ void setupList() {
addRTFunction(new_, (void*)listNew2, NULL, 2, false);
list_cls->giveAttr("__new__", new BoxedFunction(new_));
list_cls->giveAttr("count", new BoxedFunction(boxRTFunction((void*)listCount, BOXED_INT, 2, false)));
list_cls->freeze();
......
......@@ -28,3 +28,8 @@ for i in xrange(-5, 4):
print l3
print [1, 2, 3, 4, 5]
# test count method
l = [1, 2, 1, 2, 3]
print l.count(1)
print l.count(42)
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