Commit 5947a680 authored by asaka's avatar asaka

add basic dict.viewxxx method

parent e2bc5cd6
......@@ -90,6 +90,21 @@ Box* dictKeys(BoxedDict* self) {
return rtn;
}
Box* dictViewKeys(BoxedDict* self) {
BoxedDictKeys* rtn = new BoxedDictKeys(self);
return rtn;
}
Box* dictViewValues(BoxedDict* self) {
BoxedDictValues* rtn = new BoxedDictValues(self);
return rtn;
}
Box* dictViewItems(BoxedDict* self) {
BoxedDictItems* rtn = new BoxedDictItems(self);
return rtn;
}
Box* dictLen(BoxedDict* self) {
assert(self->cls == dict_cls);
return boxInt(self->d.size());
......@@ -422,9 +437,20 @@ extern "C" void dictIteratorGCHandler(GCVisitor* v, Box* b) {
v->visit(it->d);
}
BoxedClass* dict_keys_cls = NULL;
BoxedClass* dict_values_cls = NULL;
BoxedClass* dict_items_cls = NULL;
extern "C" void dictViewGCHandler(GCVisitor* v, Box* b) {
}
void setupDict() {
dict_iterator_cls = new BoxedHeapClass(type_cls, object_cls, &dictIteratorGCHandler, 0, sizeof(BoxedDict), false);
dict_keys_cls = new BoxedHeapClass(type_cls, object_cls, &dictViewGCHandler, 0, sizeof(BoxedDictKeys), false);
dict_values_cls = new BoxedHeapClass(type_cls, object_cls, &dictViewGCHandler, 0, sizeof(BoxedDictValues), false);
dict_items_cls = new BoxedHeapClass(type_cls, object_cls, &dictViewGCHandler, 0, sizeof(BoxedDictItems), false);
dict_cls->giveAttr("__name__", boxStrConstant("dict"));
dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, BOXED_INT, 1)));
dict_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)dictNew, UNKNOWN, 1, 0, true, true)));
......@@ -458,6 +484,10 @@ void setupDict() {
dict_cls->giveAttr("popitem", new BoxedFunction(boxRTFunction((void*)dictPopitem, BOXED_TUPLE, 1)));
dict_cls->giveAttr("viewkeys", new BoxedFunction(boxRTFunction((void*)dictViewKeys, UNKNOWN, 1)));
dict_cls->giveAttr("viewvalues", new BoxedFunction(boxRTFunction((void*)dictViewValues, UNKNOWN, 1)));
dict_cls->giveAttr("viewitems", new BoxedFunction(boxRTFunction((void*)dictViewItems, UNKNOWN, 1)));
dict_cls->giveAttr("get", new BoxedFunction(boxRTFunction((void*)dictGet, UNKNOWN, 3, 1, false, false), { None }));
dict_cls->giveAttr("setdefault",
......
......@@ -21,6 +21,9 @@
namespace pyston {
extern BoxedClass* dict_iterator_cls;
extern BoxedClass* dict_keys_cls;
extern BoxedClass* dict_values_cls;
extern BoxedClass* dict_items_cls;
class BoxedDictIterator : public Box {
public:
enum IteratorType { KeyIterator, ValueIterator, ItemIterator };
......@@ -42,6 +45,22 @@ Box* dictIterIter(Box* self);
Box* dictIterHasnext(Box* self);
i1 dictIterHasnextUnboxed(Box* self);
Box* dictIterNext(Box* self);
class BoxedDictKeys : public Box {
public:
BoxedDictKeys(BoxedDict* d);
};
class BoxedDictValues : public Box {
public:
BoxedDictValues(BoxedDict* d);
};
class BoxedDictItems : public Box {
public:
BoxedDictItems(BoxedDict* d);
};
}
#endif
......@@ -71,4 +71,14 @@ Box* dictIterNext(Box* s) {
++self->it;
return rtn;
}
BoxedDictKeys::BoxedDictKeys(BoxedDict* d) : Box(dict_keys_cls) {
}
BoxedDictValues::BoxedDictValues(BoxedDict* d) : Box(dict_values_cls) {
}
BoxedDictItems::BoxedDictItems(BoxedDict* d) : Box(dict_items_cls) {
}
}
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