I have a std::vector<double>
and I need to interpolate its values. For example with only 1 intermediate value and given a vector filled with
1 / 2 / 3 / 4
I want to access the following values
1 / 1.5 / 2 / 2.5 / 3 / 3.5 / 4
Of course I do not have to store this intermediate values (simple linear interpolation and I do not have to read them too often), so I wrote this simple class:
typedef std::vector<double> DVector;
class InterpolatedVector {
public:
InterpolatedVector(const DVector& v,int steps) : v(v),steps(steps){}
double at(int i){
int j = i%steps;
int k = (int)i/steps;
if (i==0){return v[0];}
else if (i==v.size()*steps){return v.back();}
else {return ((steps-j)*v[k] + j*v[k+1])/steps;}
}
int size(){return steps*(v.size()-1) + 1;}
private:
DVector v;
int steps;
};
It works fine and I get (almost) what I want. However, this "container" I cannot use with the std::algorithms and I do not have iterators for it. (Of course, I cannot write the intermediate values, but at least when it is about reading, I would like to use the algorithms.) I should mention that I am still lacking a bit of understanding on iterators and the like.
How should I implement this "InterpolatedVector", so that I can do things like
std::accumulate(/* passing Interpolated iterators? */ );
?