Commit 61211349 authored by asaka's avatar asaka

use DictView class instead of DictKeys / DictValues / DictItems

parent 71e9cdff
......@@ -91,17 +91,17 @@ Box* dictKeys(BoxedDict* self) {
}
Box* dictViewKeys(BoxedDict* self) {
BoxedDictKeys* rtn = new BoxedDictKeys(self);
BoxedDictView* rtn = new BoxedDictView(self, dict_keys_cls);
return rtn;
}
Box* dictViewValues(BoxedDict* self) {
BoxedDictValues* rtn = new BoxedDictValues(self);
BoxedDictView* rtn = new BoxedDictView(self, dict_values_cls);
return rtn;
}
Box* dictViewItems(BoxedDict* self) {
BoxedDictItems* rtn = new BoxedDictItems(self);
BoxedDictView* rtn = new BoxedDictView(self, dict_items_cls);
return rtn;
}
......@@ -441,15 +441,19 @@ BoxedClass* dict_keys_cls = NULL;
BoxedClass* dict_values_cls = NULL;
BoxedClass* dict_items_cls = NULL;
extern "C" void dictViewGCHandler(GCVisitor* v, Box* b) {
boxGCHandler(v, b);
BoxedDictIterator* it = static_cast<BoxedDictIterator*>(b);
v->visit(it->d);
}
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_keys_cls = new BoxedHeapClass(type_cls, object_cls, &dictViewGCHandler, 0, sizeof(BoxedDictView), false);
dict_values_cls = new BoxedHeapClass(type_cls, object_cls, &dictViewGCHandler, 0, sizeof(BoxedDictView), false);
dict_items_cls = new BoxedHeapClass(type_cls, object_cls, &dictViewGCHandler, 0, sizeof(BoxedDictView), false);
dict_cls->giveAttr("__name__", boxStrConstant("dict"));
dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, BOXED_INT, 1)));
......@@ -514,10 +518,16 @@ void setupDict() {
dict_iterator_cls->freeze();
dict_keys_cls->giveAttr("__name__", boxStrConstant("dictkeys"));
dict_keys_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)dictViewKeysIter, typeFromClass(dict_iterator_cls), 1)));
dict_keys_cls->freeze();
dict_values_cls->giveAttr("__name__", boxStrConstant("dictvalues"));
dict_values_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)dictViewValuesIter, typeFromClass(dict_iterator_cls), 1)));
dict_values_cls->freeze();
dict_items_cls->giveAttr("__name__", boxStrConstant("dictitems"));
dict_items_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)dictViewItemsIter, typeFromClass(dict_iterator_cls), 1)));
dict_items_cls->freeze();
}
......
......@@ -46,23 +46,15 @@ Box* dictIterHasnext(Box* self);
i1 dictIterHasnextUnboxed(Box* self);
Box* dictIterNext(Box* self);
class BoxedDictKeys : public Box {
class BoxedDictView : public Box {
public:
BoxedDict* d;
BoxedDictKeys(BoxedDict* d);
BoxedDictView(BoxedDict* d, BoxedClass* view_cls);
};
class BoxedDictValues : public Box {
public:
BoxedDict* d;
BoxedDictValues(BoxedDict* d);
};
class BoxedDictItems : public Box {
public:
BoxedDict* d;
BoxedDictItems(BoxedDict* d);
};
Box* dictViewKeysIter(Box* self);
Box* dictViewValuesIter(Box* self);
Box* dictViewItemsIter(Box* self);
}
......
......@@ -72,13 +72,26 @@ Box* dictIterNext(Box* s) {
return rtn;
}
BoxedDictKeys::BoxedDictKeys(BoxedDict* d) : Box(dict_keys_cls), d(d) {
BoxedDictView::BoxedDictView(BoxedDict* d, BoxedClass* view_cls)
: Box(view_cls), d(d) {
}
BoxedDictValues::BoxedDictValues(BoxedDict* d) : Box(dict_values_cls), d(d) {
Box* dictViewKeysIter(Box* s) {
assert(s->cls == dict_keys_cls);
BoxedDictView* self = static_cast<BoxedDictView*>(s);
return dictIterKeys(self->d);
}
BoxedDictItems::BoxedDictItems(BoxedDict* d) : Box(dict_items_cls), d(d) {
Box* dictViewValuesIter(Box* s) {
assert(s->cls == dict_values_cls);
BoxedDictView* self = static_cast<BoxedDictView*>(s);
return dictIterValues(self->d);
}
Box* dictViewItemsIter(Box* s) {
assert(s->cls == dict_items_cls);
BoxedDictView* self = static_cast<BoxedDictView*>(s);
return dictIterItems(self->d);
}
}
......@@ -194,3 +194,16 @@ d = {}
d.update({'c':3}, a=1, b=2)
print sorted(d.items())
# viewkeys / viewvalues / viewitems
d = {}
print list(d.viewkeys())
print list(d.viewvalues())
print list(d.viewitems())
d['a'] = 1
print list(d.viewkeys())
print list(d.viewvalues())
print list(d.viewitems())
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