From: guido Date: Wed, 11 Sep 2002 16:06:51 +0000 (+0000) Subject: SparseMatrix::const_iterator X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08508b7aea77a60e451dc21f24ada825f4827473;p=dealii-svn.git SparseMatrix::const_iterator git-svn-id: https://svn.dealii.org/trunk@6392 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/2002/c-3-4.html b/deal.II/doc/news/2002/c-3-4.html index ea1b4e814c..09a398dbfa 100644 --- a/deal.II/doc/news/2002/c-3-4.html +++ b/deal.II/doc/news/2002/c-3-4.html @@ -221,6 +221,17 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK

lac

    +
  1. New: class SparseMatrix has an + STL-conforming const_iterator and + functions begin() and end() for looping through all existing + entries. Furthermore, begin(row) and + end(row) allow looping through all + entries of a single line. +
    + (GK 2002/09/11) +

    +
  2. New: Classes SparsityPattern and SparseMatrix now have functions class SparseMatrix : 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 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; + + 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); + + /** + * Prefix increment. + */ + const_iterator& operator++ (); + + /** + * Postfix increment. + */ + const_iterator& operator++ (int); + + /** + * Dereferencing operator. + */ + const Accessor& operator* () const; + + /** + * Dereferencing operator. + */ + const Accessor* operator-> () const; + + /** + * Comparison. True, if + * both iterators point to + * the same matrix + * position. + */ + bool operator == (const const_iterator&) const; + /** + * 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. + */ + bool operator < (const const_iterator&) const; + }; + /** * Type of matrix entries. In analogy to * the STL container classes. @@ -719,6 +835,28 @@ class SparseMatrix : public Subscriptor */ const SparsityPattern & get_sparsity_pattern () const; + /** + * STL-like iterator with the + * first entry. + */ + const_iterator begin () const; + + /** + * Final iterator. + */ + const_iterator end () const; + + /** + * STL-like iterator with the + * first entry of row @p{r}. + */ + const_iterator begin (unsigned int r) const; + + /** + * Final iterator of row @p{r}. + */ + const_iterator end (unsigned int r) const; + /** * Print the matrix to the given * stream, using the format @@ -1180,6 +1318,172 @@ SparseMatrix::copy_from (const ForwardIterator begin, }; +//----------------------------------------------------------------------// + +template +inline +SparseMatrix::Accessor::Accessor ( + const SparseMatrix* matrix, + unsigned int r, + unsigned short i) + : + matrix(matrix), + a_row(r), + a_index(i) +{} + + +template +inline +unsigned int +SparseMatrix::Accessor::row() const +{ + return a_row; +} + + +template +inline +unsigned int +SparseMatrix::Accessor::column() const +{ + const SparsityPattern& pat = matrix->get_sparsity_pattern(); + return pat.get_column_numbers()[pat.get_rowstart_indices()[a_row]+a_index]; +} + + +template +inline +unsigned short +SparseMatrix::Accessor::index() const +{ + return a_index; +} + + + +template +inline +number +SparseMatrix::Accessor::value() const +{ + return matrix->raw_entry(a_row, a_index); +} + + +template +inline +SparseMatrix::const_iterator::const_iterator( + const SparseMatrix* matrix, + unsigned int r, + unsigned short i) + : + Accessor(matrix, r, i) +{} + + +template +inline +typename SparseMatrix::const_iterator& +SparseMatrix::const_iterator::operator++ () +{ + Assert (a_row < matrix->m(), ExcIteratorPastEnd()); + + ++a_index; + if (a_index >= matrix->get_sparsity_pattern().row_length(a_row)) + { + a_index = 0; + a_row++; + } + return *this; +} + + +template +inline +const typename SparseMatrix::Accessor& +SparseMatrix::const_iterator::operator* () const +{ + return *this; +} + + +template +inline +const typename SparseMatrix::Accessor* +SparseMatrix::const_iterator::operator-> () const +{ + return this; +} + + +template +inline +bool +SparseMatrix::const_iterator::operator == ( + const const_iterator& other) const +{ + return (row() == other->row() && index() == other->index()); +} + + +template +inline +bool +SparseMatrix::const_iterator::operator != ( + const const_iterator& other) const +{ + return ! (*this == other); +} + + +template +inline +bool +SparseMatrix::const_iterator::operator < ( + const const_iterator& other) const +{ + return (row() < other->row() || + (row() == other->row() && index() < other->index())); +} + + +template +inline +typename SparseMatrix::const_iterator +SparseMatrix::begin () const +{ + return const_iterator(this, 0, 0); +} + +template +inline +typename SparseMatrix::const_iterator +SparseMatrix::end () const +{ + return const_iterator(this, m(), 0); +} + +template +inline +typename SparseMatrix::const_iterator +SparseMatrix::begin (unsigned int r) const +{ + Assert (r +inline +typename SparseMatrix::const_iterator +SparseMatrix::end (unsigned int r) const +{ + Assert (r::const_iterator::const_iterator( template inline -SparseMatrixEZ::const_iterator& +typename SparseMatrixEZ::const_iterator& SparseMatrixEZ::const_iterator::operator++ () { Assert (a_row < matrix->m(), ExcIteratorPastEnd()); @@ -1071,7 +1071,7 @@ SparseMatrixEZ::const_iterator::operator++ () template inline -const SparseMatrixEZ::Accessor& +const typename SparseMatrixEZ::Accessor& SparseMatrixEZ::const_iterator::operator* () const { return *this; @@ -1080,7 +1080,7 @@ SparseMatrixEZ::const_iterator::operator* () const template inline -const SparseMatrixEZ::Accessor* +const typename SparseMatrixEZ::Accessor* SparseMatrixEZ::const_iterator::operator-> () const { return this; @@ -1137,9 +1137,9 @@ unsigned int SparseMatrixEZ::n () const template inline -const SparseMatrixEZ::Entry* SparseMatrixEZ::locate ( - const unsigned int row, - const unsigned int col) const +const typename SparseMatrixEZ::Entry* +SparseMatrixEZ::locate ( const unsigned int row, + const unsigned int col) const { Assert (row::Entry* SparseMatrixEZ::locate ( template inline -SparseMatrixEZ::Entry* SparseMatrixEZ::allocate ( - const unsigned int row, - const unsigned int col) +typename SparseMatrixEZ::Entry* +SparseMatrixEZ::allocate (const unsigned int row, + const unsigned int col) { Assert (row::operator() (const unsigned int i, template inline -SparseMatrixEZ::const_iterator +typename SparseMatrixEZ::const_iterator SparseMatrixEZ::begin () const { return const_iterator(this, 0, 0); @@ -1312,7 +1312,7 @@ SparseMatrixEZ::begin () const template inline -SparseMatrixEZ::const_iterator +typename SparseMatrixEZ::const_iterator SparseMatrixEZ::end () const { return const_iterator(this, m(), 0); @@ -1320,7 +1320,7 @@ SparseMatrixEZ::end () const template inline -SparseMatrixEZ::const_iterator +typename SparseMatrixEZ::const_iterator SparseMatrixEZ::begin (unsigned int r) const { Assert (r::begin (unsigned int r) const template inline -SparseMatrixEZ::const_iterator +typename SparseMatrixEZ::const_iterator SparseMatrixEZ::end (unsigned int r) const { Assert (r"); +#ifdef DEBUG + check_iterator(A); +#endif SparseMatrixEZ E(dim,dim,row_length,2); deallog << "Assemble" << std::endl; diff --git a/tests/lac/sparse_matrices.checked b/tests/lac/sparse_matrices.checked index 7b9387f15b..15d81282aa 100644 --- a/tests/lac/sparse_matrices.checked +++ b/tests/lac/sparse_matrices.checked @@ -20,6 +20,75 @@ DEAL:5-SparseMatrix:RichardsonT::Starting DEAL:5-SparseMatrix:RichardsonT::Failure step 10 DEAL::GrowingVectorMemory:Overall allocated vectors: 16 DEAL::GrowingVectorMemory:Maximum allocated vectors: 2 +DEAL:: 0 0 0 5.00 +DEAL:: 0 1 1 -2.00 +DEAL:: 0 4 2 -1.00 +DEAL:: 1 1 0 5.00 +DEAL:: 1 0 1 -1.00 +DEAL:: 1 2 2 -2.00 +DEAL:: 1 5 3 -1.00 +DEAL:: 2 2 0 5.00 +DEAL:: 2 1 1 -1.00 +DEAL:: 2 3 2 -2.00 +DEAL:: 2 6 3 -1.00 +DEAL:: 3 3 0 5.00 +DEAL:: 3 2 1 -1.00 +DEAL:: 3 7 2 -1.00 +DEAL:: 4 4 0 5.00 +DEAL:: 4 0 1 -1.00 +DEAL:: 4 5 2 -2.00 +DEAL:: 4 8 3 -1.00 +DEAL:: 5 5 0 5.00 +DEAL:: 5 1 1 -1.00 +DEAL:: 5 4 2 -1.00 +DEAL:: 5 6 3 -2.00 +DEAL:: 5 9 4 -1.00 +DEAL:: 6 6 0 5.00 +DEAL:: 6 2 1 -1.00 +DEAL:: 6 5 2 -1.00 +DEAL:: 6 7 3 -2.00 +DEAL:: 6 10 4 -1.00 +DEAL:: 7 7 0 5.00 +DEAL:: 7 3 1 -1.00 +DEAL:: 7 6 2 -1.00 +DEAL:: 7 11 3 -1.00 +DEAL:: 8 8 0 5.00 +DEAL:: 8 4 1 -1.00 +DEAL:: 8 9 2 -2.00 +DEAL:: 8 12 3 -1.00 +DEAL:: 9 9 0 5.00 +DEAL:: 9 5 1 -1.00 +DEAL:: 9 8 2 -1.00 +DEAL:: 9 10 3 -2.00 +DEAL:: 9 13 4 -1.00 +DEAL:: 10 10 0 5.00 +DEAL:: 10 6 1 -1.00 +DEAL:: 10 9 2 -1.00 +DEAL:: 10 11 3 -2.00 +DEAL:: 10 14 4 -1.00 +DEAL:: 11 11 0 5.00 +DEAL:: 11 7 1 -1.00 +DEAL:: 11 10 2 -1.00 +DEAL:: 11 15 3 -1.00 +DEAL:: 12 12 0 5.00 +DEAL:: 12 8 1 -1.00 +DEAL:: 12 13 2 -2.00 +DEAL:: 13 13 0 5.00 +DEAL:: 13 9 1 -1.00 +DEAL:: 13 12 2 -1.00 +DEAL:: 13 14 3 -2.00 +DEAL:: 14 14 0 5.00 +DEAL:: 14 10 1 -1.00 +DEAL:: 14 13 2 -1.00 +DEAL:: 14 15 3 -2.00 +DEAL:: 15 15 0 5.00 +DEAL:: 15 11 1 -1.00 +DEAL:: 15 14 2 -1.00 +DEAL::Repeat row 2 +DEAL:: 2 2 0 5.00 +DEAL:: 2 1 1 -1.00 +DEAL:: 2 3 2 -2.00 +DEAL:: 2 6 3 -1.00 DEAL::Assemble DEAL:5-SparseMatrixEZ:Richardson::Starting DEAL:5-SparseMatrixEZ:Richardson::Failure step 10