Commit c577cf0c authored by Kevin Modzelewski's avatar Kevin Modzelewski

set.__contains__

parent 224e1ea7
...@@ -205,11 +205,16 @@ Box* setLen(BoxedSet* self) { ...@@ -205,11 +205,16 @@ Box* setLen(BoxedSet* self) {
} }
Box* setAdd(BoxedSet* self, Box* v) { Box* setAdd(BoxedSet* self, Box* v) {
assert(self->cls == set_cls || self->cls == frozenset_cls); assert(self->cls == set_cls);
self->s.insert(v); self->s.insert(v);
return None; return None;
} }
Box* setContains(BoxedSet* self, Box* v) {
assert(self->cls == set_cls || self->cls == frozenset_cls);
return boxBool(self->s.count(v) != 0);
}
} // namespace set } // namespace set
...@@ -275,6 +280,9 @@ void setupSet() { ...@@ -275,6 +280,9 @@ void setupSet() {
set_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)setLen, BOXED_INT, 1))); set_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)setLen, BOXED_INT, 1)));
frozenset_cls->giveAttr("__len__", set_cls->getattr("__len__")); frozenset_cls->giveAttr("__len__", set_cls->getattr("__len__"));
set_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)setContains, BOXED_BOOL, 2)));
frozenset_cls->giveAttr("__contains__", set_cls->getattr("__contains__"));
set_cls->giveAttr("add", new BoxedFunction(boxRTFunction((void*)setAdd, NONE, 2))); set_cls->giveAttr("add", new BoxedFunction(boxRTFunction((void*)setAdd, NONE, 2)));
set_cls->freeze(); set_cls->freeze();
......
...@@ -48,3 +48,7 @@ print frozenset() | frozenset() ...@@ -48,3 +48,7 @@ print frozenset() | frozenset()
print set() | frozenset() print set() | frozenset()
print frozenset() | set() print frozenset() | set()
print set() | set() print set() | set()
for i in xrange(8):
print i, i in set(range(2, 5))
print i, i in frozenset(range(2, 5))
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