From baed7fa42aeaee35d0634d5bbcd86e7cc8b816e3 Mon Sep 17 00:00:00 2001 From: guido Date: Fri, 23 May 2003 12:56:12 +0000 Subject: [PATCH] construct only valid iterators git-svn-id: https://svn.dealii.org/trunk@7672 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/sparse_matrix_ez.h | 62 +++++++++++++++------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/deal.II/lac/include/lac/sparse_matrix_ez.h b/deal.II/lac/include/lac/sparse_matrix_ez.h index 1a7dfff8fc..1ce408f6ae 100644 --- a/deal.II/lac/include/lac/sparse_matrix_ez.h +++ b/deal.II/lac/include/lac/sparse_matrix_ez.h @@ -786,7 +786,7 @@ class SparseMatrixEZ : public Subscriptor /** * STL-like iterator with the - * first entry. + * first existing entry. */ const_iterator begin () const; @@ -797,12 +797,17 @@ class SparseMatrixEZ : public Subscriptor /** * STL-like iterator with the - * first entry of row @p{r}. + * first entry of row @p{r}. If + * this row is empty, the result + * is @p{end(r)}, which does NOT + * point into row @p{r}.. */ const_iterator begin (const unsigned int r) const; /** - * Final iterator of row @p{r}. + * Final iterator of row + * @p{r}. The result may be + * different from @p{end()}! */ const_iterator end (const unsigned int r) const; @@ -1166,7 +1171,31 @@ const_iterator(const SparseMatrixEZ *matrix, const unsigned short i) : accessor(matrix, r, i) -{} +{ + // Finish if this is the end() + if (r==accessor.matrix->m() && i==0) return; + + // Make sure we never construct an + // iterator pointing to a + // non-existing entry + + // If the index points beyond the + // end of the row, try the next + // row. + if (accessor.a_index >= accessor.matrix->row_info[accessor.a_row].length) + { + do + { + ++accessor.a_row; + } + // Beware! If the next row is + // empty, iterate until a + // non-empty row is found or we + // hit the end of the matrix. + while (accessor.a_row < accessor.matrix->m() + && accessor.matrix->row_info[accessor.a_row].length == 0); + } +} template @@ -1175,12 +1204,15 @@ typename SparseMatrixEZ::const_iterator& SparseMatrixEZ::const_iterator::operator++ () { Assert (accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd()); - + + // Increment column index ++(accessor.a_index); + // If index exceeds number of + // entries in this row, proceed + // with next row. if (accessor.a_index >= accessor.matrix->row_info[accessor.a_row].length) { accessor.a_index = 0; - // Do this loop to avoid // elements in empty rows do @@ -1463,7 +1495,8 @@ inline typename SparseMatrixEZ::const_iterator SparseMatrixEZ::begin () const { - return const_iterator(this, 0, 0); + const_iterator result(this, 0, 0); + return result; } template @@ -1480,7 +1513,8 @@ typename SparseMatrixEZ::const_iterator SparseMatrixEZ::begin (const unsigned int r) const { Assert (r @@ -1489,19 +1523,7 @@ typename SparseMatrixEZ::const_iterator SparseMatrixEZ::end (const unsigned int r) const { Assert (r