From f071ee0ba6e01720c3cffefc3b5d521d1523906f Mon Sep 17 00:00:00 2001 From: wolf Date: Mon, 10 Mar 2003 19:19:38 +0000 Subject: [PATCH] Make the accessor class a member class of the const_iterator, rather than a private base class. git-svn-id: https://svn.dealii.org/trunk@7300 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/sparse_matrix.h | 280 +++++++++++---------- deal.II/lac/include/lac/sparse_matrix_ez.h | 274 ++++++++++---------- 2 files changed, 293 insertions(+), 261 deletions(-) diff --git a/deal.II/lac/include/lac/sparse_matrix.h b/deal.II/lac/include/lac/sparse_matrix.h index c8839a0ed0..8a7df79ae0 100644 --- a/deal.II/lac/include/lac/sparse_matrix.h +++ b/deal.II/lac/include/lac/sparse_matrix.h @@ -45,120 +45,134 @@ class SparseMatrix : public Subscriptor { public: /** - * Accessor class for iterators + * STL conforming iterator. */ - class Accessor + class const_iterator { - public: - /** - * Constructor. Since we use - * accessors only for read - * access, a const matrix - * pointer is sufficient. - */ - Accessor (const SparseMatrix*, - unsigned int row, - unsigned short index); - - /** - * Row number of the element - * represented by this - * object. - */ - unsigned int row() const; - - /** - * Index in row of the element - * represented by this - * object. - */ - unsigned short index() const; - - /** - * Column number of the - * element represented by - * this object. - */ - unsigned int column() const; - - /** - * Value of this matrix entry. - */ - number value() const; + private: + /** + * Accessor class for iterators + */ + class Accessor + { + public: + /** + * Constructor. Since we use + * accessors only for read + * access, a const matrix + * pointer is sufficient. + */ + Accessor (const SparseMatrix *matrix, + const unsigned int row, + const unsigned short index); + + /** + * Row number of the element + * represented by this + * object. + */ + unsigned int row() const; + + /** + * Index in row of the element + * represented by this + * object. + */ + unsigned short index() const; + + /** + * Column number of the + * element represented by + * this object. + */ + unsigned int column() const; + + /** + * Value of this matrix entry. + */ + number value() const; - protected: - /** - * The matrix accessed. - */ - const SparseMatrix* matrix; - - /** - * Current row number. - */ - unsigned int a_row; - - /** - * Current index in row. - */ - unsigned short a_index; - }; - - /** - * STL conforming iterator. - */ - class const_iterator : private Accessor - { - public: - /** - * Constructor. - */ - const_iterator(const SparseMatrix*, - unsigned int row, - unsigned short index); + protected: + /** + * The matrix accessed. + */ + const SparseMatrix* matrix; + + /** + * Current row number. + */ + unsigned int a_row; + + /** + * Current index in row. + */ + unsigned short a_index; + + /** + * Make enclosing class a + * friend. + */ + friend class const_iterator; + }; + + public: + /** + * Constructor. + */ + const_iterator(const SparseMatrix *matrix, + const unsigned int row, + const unsigned short index); - /** - * Prefix increment. - */ + /** + * Prefix increment. + */ const_iterator& operator++ (); - /** - * Postfix increment. - */ + /** + * Postfix increment. + */ const_iterator& operator++ (int); - /** - * Dereferencing operator. - */ + /** + * Dereferencing operator. + */ const Accessor& operator* () const; - /** - * Dereferencing operator. - */ + /** + * Dereferencing operator. + */ const Accessor* operator-> () const; - /** - * Comparison. True, if - * both iterators point to - * the same matrix - * position. - */ + /** + * Comparison. True, if + * both iterators point to + * the same matrix + * position. + */ bool operator == (const const_iterator&) const; - /** - * Inverse of @p{==}. - */ + /** + * Inverse of @p{==}. + */ bool operator != (const const_iterator&) const; - /** - * Comparison - * operator. Result is true - * if either the first row - * number is smaller or if - * the row numbers are - * equal and the first - * index is smaller. - */ + /** + * Comparison + * operator. Result is true + * if either the first row + * number is smaller or if + * the row numbers are + * equal and the first + * index is smaller. + */ bool operator < (const const_iterator&) const; - }; + + private: + /** + * Store an object of the + * accessor class. + */ + Accessor accessor; + }; /** * Type of matrix entries. In analogy to @@ -1322,10 +1336,10 @@ SparseMatrix::copy_from (const ForwardIterator begin, template inline -SparseMatrix::Accessor::Accessor ( - const SparseMatrix* matrix, - unsigned int r, - unsigned short i) +SparseMatrix::const_iterator::Accessor:: +Accessor (const SparseMatrix* matrix, + const unsigned int r, + const unsigned short i) : matrix(matrix), a_row(r), @@ -1336,7 +1350,7 @@ SparseMatrix::Accessor::Accessor ( template inline unsigned int -SparseMatrix::Accessor::row() const +SparseMatrix::const_iterator::Accessor::row() const { return a_row; } @@ -1345,7 +1359,7 @@ SparseMatrix::Accessor::row() const template inline unsigned int -SparseMatrix::Accessor::column() const +SparseMatrix::const_iterator::Accessor::column() const { const SparsityPattern& pat = matrix->get_sparsity_pattern(); return pat.get_column_numbers()[pat.get_rowstart_indices()[a_row]+a_index]; @@ -1355,7 +1369,7 @@ SparseMatrix::Accessor::column() const template inline unsigned short -SparseMatrix::Accessor::index() const +SparseMatrix::const_iterator::Accessor::index() const { return a_index; } @@ -1365,7 +1379,7 @@ SparseMatrix::Accessor::index() const template inline number -SparseMatrix::Accessor::value() const +SparseMatrix::const_iterator::Accessor::value() const { return matrix->raw_entry(a_row, a_index); } @@ -1373,28 +1387,28 @@ SparseMatrix::Accessor::value() const template inline -SparseMatrix::const_iterator::const_iterator( - const SparseMatrix* matrix, - unsigned int r, - unsigned short i) +SparseMatrix::const_iterator:: +const_iterator(const SparseMatrix *matrix, + const unsigned int r, + const unsigned short i) : - Accessor(matrix, r, i) + accessor(matrix, r, i) {} template inline -typename SparseMatrix::const_iterator& +typename SparseMatrix::const_iterator & SparseMatrix::const_iterator::operator++ () { - Assert (this->a_row < this->matrix->m(), ExcIteratorPastEnd()); + Assert (accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd()); - ++this->a_index; - if (this->a_index >= - this->matrix->get_sparsity_pattern().row_length(this->a_row)) + ++accessor.a_index; + if (accessor.a_index >= + accessor.matrix->get_sparsity_pattern().row_length(accessor.a_row)) { - this->a_index = 0; - this->a_row++; + accessor.a_index = 0; + accessor.a_row++; } return *this; } @@ -1402,37 +1416,38 @@ SparseMatrix::const_iterator::operator++ () template inline -const typename SparseMatrix::Accessor& +const typename SparseMatrix::const_iterator::Accessor & SparseMatrix::const_iterator::operator* () const { - return *this; + return accessor; } template inline -const typename SparseMatrix::Accessor* +const typename SparseMatrix::const_iterator::Accessor * SparseMatrix::const_iterator::operator-> () const { - return this; + return &accessor; } template inline bool -SparseMatrix::const_iterator::operator == ( - const const_iterator& other) const +SparseMatrix::const_iterator:: +operator == (const const_iterator& other) const { - return (this->row() == other->row() && this->index() == other->index()); + return (accessor.row() == other.accessor.row() && + accessor.index() == other.accessor.index()); } template inline bool -SparseMatrix::const_iterator::operator != ( - const const_iterator& other) const +SparseMatrix::const_iterator:: +operator != (const const_iterator& other) const { return ! (*this == other); } @@ -1441,11 +1456,12 @@ SparseMatrix::const_iterator::operator != ( template inline bool -SparseMatrix::const_iterator::operator < ( - const const_iterator& other) const +SparseMatrix::const_iterator:: +operator < (const const_iterator& other) const { - return (this->row() < other->row() || - (this->row() == other->row() && this->index() < other->index())); + return (accessor.row() < other.accessor.row() || + (accessor.row() == other.accessor.row() && + accessor.index() < other.accessor.index())); } @@ -1457,6 +1473,7 @@ SparseMatrix::begin () const return const_iterator(this, 0, 0); } + template inline typename SparseMatrix::const_iterator @@ -1465,6 +1482,7 @@ SparseMatrix::end () const return const_iterator(this, m(), 0); } + template inline typename SparseMatrix::const_iterator diff --git a/deal.II/lac/include/lac/sparse_matrix_ez.h b/deal.II/lac/include/lac/sparse_matrix_ez.h index d744394ee4..e8309b8ee0 100644 --- a/deal.II/lac/include/lac/sparse_matrix_ez.h +++ b/deal.II/lac/include/lac/sparse_matrix_ez.h @@ -171,121 +171,136 @@ class SparseMatrixEZ : public Subscriptor }; public: - /** - * Accessor class for iterators - */ - class Accessor - { - public: - /** - * Constructor. Since we use - * accessors only for read - * access, a const matrix - * pointer is sufficient. - */ - Accessor (const SparseMatrixEZ*, - unsigned int row, - unsigned short index); - - /** - * Row number of the element - * represented by this - * object. - */ - unsigned int row() const; - - /** - * Index in row of the element - * represented by this - * object. - */ - unsigned short index() const; - - /** - * Column number of the - * element represented by - * this object. - */ - unsigned int column() const; - - /** - * Value of this matrix entry. - */ - number value() const; - - protected: - /** - * The matrix accessed. - */ - const SparseMatrixEZ* matrix; - - /** - * Current row number. - */ - unsigned int a_row; - - /** - * Current index in row. - */ - unsigned short a_index; - }; /** * STL conforming iterator. */ - class const_iterator : private Accessor - { - public: - /** - * Constructor. - */ - const_iterator(const SparseMatrixEZ*, - unsigned int row, - unsigned short index); + class const_iterator + { + private: + /** + * Accessor class for iterators + */ + class Accessor + { + public: + /** + * Constructor. Since we use + * accessors only for read + * access, a const matrix + * pointer is sufficient. + */ + Accessor (const SparseMatrixEZ *matrix, + const unsigned int row, + const unsigned short index); + + /** + * Row number of the element + * represented by this + * object. + */ + unsigned int row() const; + + /** + * Index in row of the element + * represented by this + * object. + */ + unsigned short index() const; + + /** + * Column number of the + * element represented by + * this object. + */ + unsigned int column() const; + + /** + * Value of this matrix entry. + */ + number value() const; + + protected: + /** + * The matrix accessed. + */ + const SparseMatrixEZ* matrix; + + /** + * Current row number. + */ + unsigned int a_row; + + /** + * Current index in row. + */ + unsigned short a_index; + + /** + * Make enclosing class a + * friend. + */ + friend class const_iterator; + }; + + public: + /** + * Constructor. + */ + const_iterator(const SparseMatrixEZ *matrix, + const unsigned int row, + const unsigned short index); - /** - * Prefix increment. - */ + /** + * Prefix increment. + */ const_iterator& operator++ (); - /** - * Postfix increment. - */ + /** + * Postfix increment. + */ const_iterator& operator++ (int); - /** - * Dereferencing operator. - */ + /** + * Dereferencing operator. + */ const Accessor& operator* () const; - /** - * Dereferencing operator. - */ + /** + * Dereferencing operator. + */ const Accessor* operator-> () const; - /** - * Comparison. True, if - * both iterators point to - * the same matrix - * position. - */ + /** + * Comparison. True, if + * both iterators point to + * the same matrix + * position. + */ bool operator == (const const_iterator&) const; - /** - * Inverse of @p{==}. - */ + /** + * Inverse of @p{==}. + */ bool operator != (const const_iterator&) const; - /** - * Comparison - * operator. Result is true - * if either the first row - * number is smaller or if - * the row numbers are - * equal and the first - * index is smaller. - */ + /** + * Comparison + * operator. Result is true + * if either the first row + * number is smaller or if + * the row numbers are + * equal and the first + * index is smaller. + */ bool operator < (const const_iterator&) const; - }; + + private: + /** + * Store an object of the + * accessor class. + */ + Accessor accessor; + }; /** * Type of matrix entries. In analogy to @@ -960,9 +975,6 @@ class SparseMatrixEZ : public Subscriptor * Increment when a row grows. */ unsigned int increment; - - friend class Accessor; - friend class const_iterator; }; @@ -999,10 +1011,10 @@ SparseMatrixEZ::RowInfo::RowInfo(unsigned int start) //----------------------------------------------------------------------// template inline -SparseMatrixEZ::Accessor::Accessor ( - const SparseMatrixEZ* matrix, - unsigned int r, - unsigned short i) +SparseMatrixEZ::const_iterator::Accessor:: +Accessor (const SparseMatrixEZ *matrix, + const unsigned int r, + const unsigned short i) : matrix(matrix), a_row(r), @@ -1013,7 +1025,7 @@ SparseMatrixEZ::Accessor::Accessor ( template inline unsigned int -SparseMatrixEZ::Accessor::row() const +SparseMatrixEZ::const_iterator::Accessor::row() const { return a_row; } @@ -1022,7 +1034,7 @@ SparseMatrixEZ::Accessor::row() const template inline unsigned int -SparseMatrixEZ::Accessor::column() const +SparseMatrixEZ::const_iterator::Accessor::column() const { return matrix->data[matrix->row_info[a_row].start+a_index].column; } @@ -1031,7 +1043,7 @@ SparseMatrixEZ::Accessor::column() const template inline unsigned short -SparseMatrixEZ::Accessor::index() const +SparseMatrixEZ::const_iterator::Accessor::index() const { return a_index; } @@ -1041,7 +1053,7 @@ SparseMatrixEZ::Accessor::index() const template inline number -SparseMatrixEZ::Accessor::value() const +SparseMatrixEZ::const_iterator::Accessor::value() const { return matrix->data[matrix->row_info[a_row].start+a_index].value; } @@ -1049,12 +1061,12 @@ SparseMatrixEZ::Accessor::value() const template inline -SparseMatrixEZ::const_iterator::const_iterator( - const SparseMatrixEZ* matrix, - unsigned int r, - unsigned short i) +SparseMatrixEZ::const_iterator:: +const_iterator(const SparseMatrixEZ *matrix, + const unsigned int r, + const unsigned short i) : - Accessor(matrix, r, i) + accessor(matrix, r, i) {} @@ -1063,13 +1075,13 @@ inline typename SparseMatrixEZ::const_iterator& SparseMatrixEZ::const_iterator::operator++ () { - Assert (this->a_row < this->matrix->m(), ExcIteratorPastEnd()); + Assert (accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd()); - ++(this->a_index); - if (this->a_index >= this->matrix->row_info[this->a_row].length) + ++(accessor.a_index); + if (accessor.a_index >= accessor.matrix->row_info[accessor.a_row].length) { - this->a_index = 0; - this->a_row++; + accessor.a_index = 0; + accessor.a_row++; } return *this; } @@ -1077,19 +1089,19 @@ SparseMatrixEZ::const_iterator::operator++ () template inline -const typename SparseMatrixEZ::Accessor& +const typename SparseMatrixEZ::const_iterator::Accessor& SparseMatrixEZ::const_iterator::operator* () const { - return *this; + return accessor; } template inline -const typename SparseMatrixEZ::Accessor* +const typename SparseMatrixEZ::const_iterator::Accessor* SparseMatrixEZ::const_iterator::operator-> () const { - return this; + return &accessor; } @@ -1099,15 +1111,16 @@ bool SparseMatrixEZ::const_iterator::operator == ( const const_iterator& other) const { - return (this->row() == other->row() && this->index() == other->index()); + return (accessor.row() == other.accessor.row() && + accessor.index() == other.accessor.index()); } template inline bool -SparseMatrixEZ::const_iterator::operator != ( - const const_iterator& other) const +SparseMatrixEZ::const_iterator:: +operator != (const const_iterator& other) const { return ! (*this == other); } @@ -1116,11 +1129,12 @@ SparseMatrixEZ::const_iterator::operator != ( template inline bool -SparseMatrixEZ::const_iterator::operator < ( - const const_iterator& other) const +SparseMatrixEZ::const_iterator:: +operator < (const const_iterator& other) const { - return (this->row() < other->row() || - (this->row() == other->row() && this->index() < other->index())); + return (accessor.row() < other.accessor.row() || + (accessor.row() == other.accessor.row() && + accessor.index() < other.accessor.index())); } -- 2.39.5