Skip to content

Commit

Permalink
Formula engine: allow lists to be indexed by a list
Browse files Browse the repository at this point in the history
This produces a slice of the original list
  • Loading branch information
CelticMinstrel committed Mar 18, 2016
1 parent 0674f2a commit 279ced9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/variant.cpp
Expand Up @@ -373,7 +373,7 @@ variant& variant::operator=(const variant& v)
return *this;
}

const variant& variant::operator[](size_t n) const
variant variant::operator[](size_t n) const
{
if(type_ == TYPE_CALLABLE) {
assert(n == 0);
Expand All @@ -389,7 +389,7 @@ const variant& variant::operator[](size_t n) const
return list_->elements[n];
}

const variant& variant::operator[](const variant& v) const
variant variant::operator[](const variant& v) const
{
if(type_ == TYPE_CALLABLE) {
assert(v.as_int() == 0);
Expand All @@ -406,6 +406,14 @@ const variant& variant::operator[](const variant& v) const
}
return i->second;
} else if(type_ == TYPE_LIST) {
if(v.is_list()) {
std::vector<variant> slice;

for(size_t i = 0; i < v.num_elements(); ++i) {
slice.push_back( (*this)[v[i]] );
}
return variant(&slice);
}
return operator[](v.as_int());
} else {
throw type_error((formatter() << "type error: "
Expand Down
4 changes: 2 additions & 2 deletions src/variant.hpp
Expand Up @@ -68,8 +68,8 @@ class variant {
variant(const variant& v);
variant& operator=(const variant& v);

const variant& operator[](size_t n) const;
const variant& operator[](const variant& v) const;
variant operator[](size_t n) const;
variant operator[](const variant& v) const;
size_t num_elements() const;
bool is_empty() const;

Expand Down

0 comments on commit 279ced9

Please sign in to comment.