From 60df7cf5bb0e66492a14326c276819e165b7ccb0 Mon Sep 17 00:00:00 2001 From: guido Date: Fri, 18 Jul 2003 12:16:48 +0000 Subject: [PATCH] const_iterator in FullMatrix git-svn-id: https://svn.dealii.org/trunk@7879 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/full_matrix.h | 306 +++++++++++++++++- tests/lac/full_matrix.cc | 22 ++ .../lac/full_matrix.output | 12 + 3 files changed, 338 insertions(+), 2 deletions(-) diff --git a/deal.II/lac/include/lac/full_matrix.h b/deal.II/lac/include/lac/full_matrix.h index ad2f93748b..456a3c97ae 100644 --- a/deal.II/lac/include/lac/full_matrix.h +++ b/deal.II/lac/include/lac/full_matrix.h @@ -68,6 +68,129 @@ template class FullMatrix : public Table<2,number> { public: + /** + * STL conforming iterator. + */ + 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 FullMatrix *matrix, + const unsigned int row, + const unsigned int col); + + /** + * Row number of the element + * represented by this + * object. + */ + unsigned int row() 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 FullMatrix* matrix; + + /** + * Current row number. + */ + unsigned int a_row; + + /** + * Current column number. + */ + unsigned short a_col; + + /** + * Make enclosing class a + * friend. + */ + friend class const_iterator; + }; + + public: + /** + * Constructor. + */ + const_iterator(const FullMatrix *matrix, + const unsigned int row, + const unsigned int col); + + /** + * 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; + + private: + /** + * Store an object of the + * accessor class. + */ + Accessor accessor; + }; + /** * Constructor. Initialize the * matrix as a square matrix with @@ -237,6 +360,27 @@ class FullMatrix : public Table<2,number> */ bool all_zero () 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 (const unsigned int r) const; + + /** + * Final iterator of row @p{r}. + */ + const_iterator end (const unsigned int r) const; /** * Scale the entire matrix by a @@ -793,8 +937,166 @@ FullMatrix::copy_from (const MATRIX& M) el(entry->row(), entry->column()) = entry->value(); } +//----------------------------------------------------------------------// + + +template +inline +FullMatrix::const_iterator::Accessor:: +Accessor (const FullMatrix* matrix, + const unsigned int r, + const unsigned int c) + : + matrix(matrix), + a_row(r), + a_col(c) +{} + + +template +inline +unsigned int +FullMatrix::const_iterator::Accessor::row() const +{ + return a_row; +} + + +template +inline +unsigned int +FullMatrix::const_iterator::Accessor::column() const +{ + return a_col; +} + + +template +inline +number +FullMatrix::const_iterator::Accessor::value() const +{ + return matrix->el(a_row, a_col); +} + + +template +inline +FullMatrix::const_iterator:: +const_iterator(const FullMatrix *matrix, + const unsigned int r, + const unsigned int c) + : + accessor(matrix, r, c) +{} + + +template +inline +typename FullMatrix::const_iterator & +FullMatrix::const_iterator::operator++ () +{ + Assert (accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd()); + + ++accessor.a_col; + if (accessor.a_col >= accessor.matrix->n()) + { + accessor.a_col = 0; + accessor.a_row++; + } + return *this; +} + + +template +inline +const typename FullMatrix::const_iterator::Accessor & +FullMatrix::const_iterator::operator* () const +{ + return accessor; +} + + +template +inline +const typename FullMatrix::const_iterator::Accessor * +FullMatrix::const_iterator::operator-> () const +{ + return &accessor; +} + + +template +inline +bool +FullMatrix::const_iterator:: +operator == (const const_iterator& other) const +{ + return (accessor.row() == other.accessor.row() && + accessor.column() == other.accessor.column()); +} + + +template +inline +bool +FullMatrix::const_iterator:: +operator != (const const_iterator& other) const +{ + return ! (*this == other); +} + + +template +inline +bool +FullMatrix::const_iterator:: +operator < (const const_iterator& other) const +{ + return (accessor.row() < other.accessor.row() || + (accessor.row() == other.accessor.row() && + accessor.column() < other.accessor.column())); +} + + +template +inline +typename FullMatrix::const_iterator +FullMatrix::begin () const +{ + return const_iterator(this, 0, 0); +} + + +template +inline +typename FullMatrix::const_iterator +FullMatrix::end () const +{ + return const_iterator(this, m(), 0); +} + + +template +inline +typename FullMatrix::const_iterator +FullMatrix::begin (const unsigned int r) const +{ + Assert (r +inline +typename FullMatrix::const_iterator +FullMatrix::end (const unsigned int r) const +{ + Assert (r T(3,3,entries); T.print_formatted(logfile, 0, false); + + FullMatrix::const_iterator it = T.begin(); + FullMatrix::const_iterator end = T.end(); + while (it != end) + { + deallog << "Row " << it->row() + << "\tCol " << it->column() + << "\tVal " << it->value() + << std::endl; + ++it; + } + + it = T.begin(1); + end = T.end(1); + while (it != end) + { + deallog << "Row " << it->row() + << "\tCol " << it->column() + << "\tVal " << it->value() + << std::endl; + ++it; + } for (unsigned int i=1;i<10;++i) { diff --git a/tests/results/i686-pc-linux-gnu+gcc3.2/lac/full_matrix.output b/tests/results/i686-pc-linux-gnu+gcc3.2/lac/full_matrix.output index 19d911e0cf..1066e76b2c 100644 --- a/tests/results/i686-pc-linux-gnu+gcc3.2/lac/full_matrix.output +++ b/tests/results/i686-pc-linux-gnu+gcc3.2/lac/full_matrix.output @@ -2,6 +2,18 @@ 11. 12. 13. 21. 22. 23. 31. 32. 33. +DEAL::Row 0 Col 0 Val 11.000 +DEAL::Row 0 Col 1 Val 12.000 +DEAL::Row 0 Col 2 Val 13.000 +DEAL::Row 1 Col 0 Val 21.000 +DEAL::Row 1 Col 1 Val 22.000 +DEAL::Row 1 Col 2 Val 23.000 +DEAL::Row 2 Col 0 Val 31.000 +DEAL::Row 2 Col 1 Val 32.000 +DEAL::Row 2 Col 2 Val 33.000 +DEAL::Row 1 Col 0 Val 21.000 +DEAL::Row 1 Col 1 Val 22.000 +DEAL::Row 1 Col 2 Val 23.000 DEAL::Inverse(dim=1): DEAL::Inverse(dim=2): DEAL::Inverse(dim=3): -- 2.39.5