I have a class that manage data.
I wish to return only a part of the data that is inside it, but since it is a process that will be done multiple times I do not wish to just copy the data inside a container and return the container.
It would be nice if I could just send a reference or something similar. Iterators come to mind. but since I use Eigen3 Matrix (which does not have iterators (2D matrix anyway))
I am thinking of emulating(?) the iterators behavior, something like that:
typedef unsigned int Index;
class MatrixIterator
{
public:
MatrixIterator(Eigen::MatrixXd *m, Index min, Index max):
_col(0), _index(0), _min(min), _max(max), _matrix(m)
{}
void operator++ ()
{
if (_index + _min + 1 != _max)
_index++;
}
void operator--()
{
if (_index != _min)
_index--;
}
double operator*()
{
return _matrix->operator() (_index + _min, _col);
}
void setCol(Index col) { _col = col; }
Index min() { return _min; }
Index max() { return _max; }
private:
// the matrix is 2D we can select
// on which column we want to iterate
Index _col;
// current position
Index _index;
// select the range on which the user can iterate
Index _max;
Index _min;
// the matrix on which we want to iterate over
Eigen::MatrixXd* _matrix;
}
- I never really used iterators before, is it correct ?
- Can I inherit my
MatrixIterator
fromstd::iterator
sostl
would be able to understand it as a usual iterator ? - Do you know a better way to do something similar ?
I have read :
- Creating my own Iterators - which does not really speak of implementing iterators since they use vector iterator
- http://www.cplusplus.com/reference/iterator/
- How to use iterator to iterate a 2D vector?
EDIT : I want to iterate over only a part of the matrix (that is why I have _min and _max), the data I am manipulating are time series so the data are already ordered. I think we can consider MatrixIterator as a response to a data query.