I am currently working on a linear algebra library (custom vector and matrix plus some algorithms) for educational purposes and personal use. I tried to implement a column iterator, an iterator that traverses a specified column of a Matrix.
Here is a code sample of the vector class (upon which the matrix class is built on):
template<class T>
class MVector
{
std::vector<T> data;
public:
explicit MVector(const std::size_t& n) :data(n) {}
explicit MVector(std::size_t&& n) :data(n) {}
typename std::vector<T>::iterator Begin(){
return data.begin();
}
typename std::vector<T>::iterator End(){
return data.end();
}
// many more functions and overloaded operators
// end of class
};
The matrix class is based on this vector (or the std::vector for this matter), and looks like:
template<class T, std::size_t rowsize, std::size_t colsize>
class Matrix
{
private:
// Data is stored in a MVector, a modified std::vector
MVector<T> matrix;
// size of row dimension of the matrix
std::size_t row_dim;
// size of row dimension of the matrix
std::size_t column_dim;
public:
Matrix(std::initializer_list<T> il) :matrix(il),
row_dim(rowsize), column_dim(colsize){}
//other constructors...
// iterator
typename std::vector<T>::iterator Begin(std::size_t row = 0){
return matrix.Begin()+index(row,0);
}
typename std::vector<T>::iterator End(std::size_t row = rowsize){
return matrix.Begin()+index(row,0);
// index (convenience) function to access elements of the matrix via some_matrix(i,j)
std::size_t index(std::size_t r, std::size_t c) const {
return r*cols()+c;
}
// this is exactly what I want the iterator to do:
// only without creating and returning an object.
// get c'th column
// slicing is possible from both ends and by "jumping" over elements
// @ param "begin" - starts at the n'th element
// @ param "end" - subtracts m from from the last element.
// @ param "by" - selects every n'th column
MVector<T> get_column(std::size_t c, std::size_t begin = 0,
std::size_t end = 0, std::size_t by = 1) const{
assert(c < cols() && end < rows());
MVector<T> columns;
for (std::size_t i = index(begin, c); i < index(rows()-end,c); i+=by*cols()) {
columns.addTo(matrix[i]);
}
return columns;
}
// end of class
};
So, iterating the rows works fine all I have to do is:
int main{
Matrix<int, 3, 2> a = {1,2,3,4,5,6};
for (std::vector<int>::iterator iter = a.Begin(1); iter != a.End(2); iter++) {
std::cout << *iter << " ";
}
std::cout << endl;
return 0;
}
bash-3.2$ ./main
3 4
which is exactly what I want. However traversing the columns does not work with that approach. Hence I look for other solutions and found this Designing iterators for a Matrix class article which sounds very similar to my problem and situation, however I could not deduce a solution to the problem.
Other suggestions pointed to the boost libraries iterators: in particular to:
boost::adaptors::stride(rng, n)
boost::adaptors::slice(rng, n, m)
which indeed provide very similar results as desired. But so does my get_column function. However I do not want to create a new object. Which is what the boost functions do. From the documentation "Returns: A new range based on rng where traversal is performed in steps of n."
So it seems that the iterator does not know when to stop.
So I am back at square one: how to return am iterator that iterates through the columns of a Matrix that is stored as a vector?
size_t
, just doMVector(std::size_t n)
– Jarod42std::vector
. – 5gon12eder