Skip to content
Snippets Groups Projects
Commit 5130d908 authored by Jesper Andersson's avatar Jesper Andersson
Browse files

Made iterator inherit from standard library iterator

parent de4e2997
No related branches found
No related tags found
No related merge requests found
......@@ -89,39 +89,6 @@ template<class T>
class SpMat
{
public:
class Iterator {
public:
Iterator(SpMat<T>& mat, bool oob=false) : _mat(mat), _i(0), _oob(oob)
{
_j = 0;
while (_j < _mat._n && !_mat._ri[_j].size()) _j++;
if (_j == _mat._n) _oob = true;
}
~Iterator() {}
bool operator==(const Iterator& other) { return(&_mat==&other._mat && ((_oob && other._oob) || (_i==other._i && _j==other._j))); }
bool operator!=(const Iterator& other) { return(!(*this==other)); }
T& operator*() { return((_mat._val[_j])[_i]); }
Iterator& operator++()
{
if (++_i < _mat._ri[_j].size()) return(*this);
else {
while (++_j < _mat._n && !_mat._ri[_j].size()) ;
}
if (_j == _mat._n) _oob = true;
else _i = 0;
return(*this);
}
unsigned int Row() { return((_mat._ri[_j])[_i]+1); }
unsigned int Col() { return(_j+1); }
private:
SpMat<T>& _mat;
unsigned int _i;
unsigned int _j;
bool _oob;
};
SpMat() : _m(0), _n(0), _nz(0), _ri(0), _val(0), _pw(false), _ei(*this,true) {}
SpMat(unsigned int m, unsigned int n) : _m(m), _n(n), _nz(0), _ri(n), _val(n), _pw(false), _ei(*this,true) {}
SpMat(unsigned int m, unsigned int n, const unsigned int *irp, const unsigned int *jcp, const double *sp);
......@@ -133,9 +100,6 @@ public:
unsigned int Ncols() const {return(_n);}
unsigned int NZ() const {return(_nz);}
Iterator begin() { return(Iterator(*this)); }
const Iterator& end() { return(_ei); }
NEWMAT::ReturnMatrix AsNEWMAT() const;
void Save(const std::string& fname,
unsigned int precision) const;
......@@ -205,6 +169,42 @@ public:
boost::shared_ptr<Preconditioner<T> > C,
const NEWMAT::ColumnVector& x_init) const;
// Declaration and definition of bundled Iterator class
class Iterator : public std::iterator<std::forward_iterator_tag, T> {
public:
Iterator(SpMat<T>& mat, bool oob=false) : _mat(mat), _i(0), _oob(oob)
{
_j = 0;
while (_j < _mat._n && !_mat._ri[_j].size()) _j++;
if (_j == _mat._n) _oob = true;
}
~Iterator() {}
bool operator==(const Iterator& other) { return(&_mat==&other._mat && ((_oob && other._oob) || (_i==other._i && _j==other._j))); }
bool operator!=(const Iterator& other) { return(!(*this==other)); }
T& operator*() { return((_mat._val[_j])[_i]); }
Iterator& operator++()
{
if (++_i < _mat._ri[_j].size()) return(*this);
else {
while (++_j < _mat._n && !_mat._ri[_j].size()) ;
}
if (_j == _mat._n) _oob = true;
else _i = 0;
return(*this);
}
unsigned int Row() { return((_mat._ri[_j])[_i]+1); }
unsigned int Col() { return(_j+1); }
private:
SpMat<T>& _mat;
unsigned int _i;
unsigned int _j;
bool _oob;
};
Iterator begin() { return(Iterator(*this)); }
const Iterator& end() { return(_ei); }
private:
unsigned int _m;
unsigned int _n;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment