Commit 6d8b2e91 authored by Marius Wachtler's avatar Marius Wachtler

Add pyElements method.

parent 0cf0c7fe
......@@ -19,6 +19,8 @@
// over having them spread randomly in different files, this should probably be split again
// but in a way that makes more sense.
#include <llvm/ADT/iterator_range.h>
#include "core/common.h"
#include "core/stats.h"
......@@ -283,7 +285,7 @@ public:
};
class Box;
class BoxIterator : public std::iterator<std::forward_iterator_tag, Box*> {
class BoxIterator {
public:
BoxIterator(Box* iter) : iter(iter), value(nullptr) {}
......@@ -291,7 +293,6 @@ public:
bool operator!=(BoxIterator const& rhs) const { return !(*this == rhs); }
BoxIterator& operator++();
BoxIterator operator++(int) {
BoxIterator tmp(*this);
operator++();
......@@ -313,8 +314,7 @@ class Box : public GCObject {
public:
BoxedClass* cls;
BoxIterator begin() const;
BoxIterator end() const { return BoxIterator(nullptr); }
llvm::iterator_range<BoxIterator> pyElements();
constexpr Box(const ObjectFlavor* flavor, BoxedClass* c) __attribute__((visibility("default")))
: GCObject(flavor), cls(c) {
......
......@@ -51,7 +51,7 @@ extern "C" Box* abs_(Box* x) {
extern "C" Box* min1(Box* container) {
Box* minElement = nullptr;
for (Box* e : *container) {
for (Box* e : container->pyElements()) {
if (!minElement) {
minElement = e;
} else {
......@@ -80,7 +80,7 @@ extern "C" Box* min2(Box* o0, Box* o1) {
extern "C" Box* max1(Box* container) {
Box* maxElement = nullptr;
for (Box* e : *container) {
for (Box* e : container->pyElements()) {
if (!maxElement) {
maxElement = e;
} else {
......@@ -266,7 +266,7 @@ Box* getattr3(Box* obj, Box* _str, Box* default_value) {
Box* map2(Box* f, Box* container) {
Box* rtn = new BoxedList();
for (Box* e : *container) {
for (Box* e : container->pyElements()) {
listAppendInternal(rtn, runtimeCall(f, 1, e, NULL, NULL, NULL));
}
return rtn;
......
......@@ -355,7 +355,7 @@ extern "C" Box* listNew2(Box* cls, Box* container) {
assert(cls == list_cls);
BoxedList* rtn = new BoxedList();
for (Box* e : *container) {
for (Box* e : container->pyElements()) {
listAppendInternal(rtn, e);
}
return rtn;
......
......@@ -73,7 +73,7 @@ Box* setNew2(Box* cls, Box* container) {
assert(cls == set_cls);
Box* rtn = new BoxedSet();
for (Box* e : *container) {
for (Box* e : container->pyElements()) {
setAdd2(rtn, e);
}
......
......@@ -50,10 +50,14 @@ BoxIterator& BoxIterator::operator++() {
return *this;
}
BoxIterator Box::begin() const {
llvm::iterator_range<BoxIterator> Box::pyElements() {
static std::string iter_str("__iter__");
Box* iter = callattr(const_cast<Box*>(this), &iter_str, true, 0, NULL, NULL, NULL, NULL);
return ++BoxIterator(iter);
if (iter) {
return llvm::iterator_range<BoxIterator>(++BoxIterator(iter), BoxIterator(nullptr));
}
return llvm::iterator_range<BoxIterator>(BoxIterator(nullptr), BoxIterator(nullptr));
}
extern "C" BoxedFunction::BoxedFunction(CLFunction* f) : HCBox(&function_flavor, function_cls), f(f) {
......
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