Fix [ expr ] when expr is a vector

.. whilst its possible to assign to a selection of elements in a
   vector e.g. v[c(3,5,7)] <- 99 to assign 99 to el 3,5 and 7
   I forgot to update the code to /return/ a vector e.g.;

   a <- v[c(3,5,7)]

   was not handled at all. This commit fixes that. It means we
   can select from a vector element-wise -or- via a logical
   expression.
This commit is contained in:
Mark Liversedge
2020-03-14 16:12:52 +00:00
parent 0125e2aadf
commit 3559395c83

View File

@@ -3827,12 +3827,29 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, float x, long it, RideItem
// INDEXING INTO VECTORS
case Leaf::Index :
{
int index = eval(df,leaf->fparms[0],x, it, m, p, c, s, d).number;
Result index = eval(df,leaf->fparms[0],x, it, m, p, c, s, d);
Result value = eval(df,leaf->lvalue.l,x, it, m, p, c, s, d); // lhs might also be a symbol
if (index < 0 || index >= value.vector.count()) return Result(0); // out of bounds
// are we returning the value or a vector of values?
if (index.vector.count()) {
return Result(value.vector[index]);
Result returning(0);
// a range
for(int i=0; i<index.vector.count(); i++) {
int it=index.vector[i];
if (it < 0 || it >= value.vector.count()) continue; // ignore out of bounds
returning.vector << value.vector[it];
returning.number += value.vector[it];
}
return returning;
} else {
// a single value
if (index.number < 0 || index.number >= value.vector.count()) return Result(0);
return Result(value.vector[index.number]);
}
}
// SELECTING FROM VECTORS