From 2ef1a7ef4193b37b89b780e536ef05e440a17226 Mon Sep 17 00:00:00 2001 From: wolf Date: Mon, 29 Mar 2004 19:46:27 +0000 Subject: [PATCH] Implement write access to elements through iterators/accessors/references. git-svn-id: https://svn.dealii.org/trunk@8892 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/sparse_matrix.h | 161 +++++++++++++++++++++++- 1 file changed, 156 insertions(+), 5 deletions(-) diff --git a/deal.II/lac/include/lac/sparse_matrix.h b/deal.II/lac/include/lac/sparse_matrix.h index c376bddf9a..3d983d70b1 100644 --- a/deal.II/lac/include/lac/sparse_matrix.h +++ b/deal.II/lac/include/lac/sparse_matrix.h @@ -121,6 +121,72 @@ namespace internals template class Accessor : public SparsityPatternIterators::Accessor { + class Reference + { + private: + /** + * Constructor. Made private so + * as to only allow friend + * classes access to it. + */ + Reference (const Accessor *accessor); + public: + + /** + * Conversion operator to the + * data type of the matrix. + */ + operator number () const; + + /** + * Set the element of the matrix + * we presently point to to @p n. + */ + const Reference & operator = (const number n) const; + + /** + * Add @p n to the element of the + * matrix we presently point to. + */ + const Reference & operator += (const number n) const; + + /** + * Subtract @p n from the element + * of the matrix we presently + * point to. + */ + const Reference & operator -= (const number n) const; + + /** + * Multiply the element of the + * matrix we presently point to + * by @p n. + */ + const Reference & operator *= (const number n) const; + + /** + * Divide the element of the + * matrix we presently point to + * by @p n. + */ + const Reference & operator /= (const number n) const; + + private: + /** + * Pointer to the accessor that + * denotes which element we + * presently point to. + */ + const Accessor *accessor; + + /** + * Make the accessor class a + * friend so as to allow it to + * generate objects of this type. + */ + friend class Accessor; + }; + public: /** * Typedef for the type (including @@ -141,7 +207,7 @@ namespace internals * returned as a read- and writable * reference. */ - number & value() const; + Reference value() const; private: /** @@ -1711,6 +1777,90 @@ namespace internals + template + inline + Accessor::Reference:: + Reference (const Accessor *accessor) + : + accessor (accessor) + {} + + + + template + inline + Accessor::Reference::operator number() const + { + return accessor->matrix->raw_entry(accessor->a_row, + accessor->a_index); + } + + + + template + inline + const typename Accessor::Reference & + Accessor::Reference::operator = (const number n) const + { +//TODO: one could optimize this by not going again through the mapping from row/col index to global index + accessor->matrix->set (accessor->row(), accessor->column(), n); + return *this; + } + + + + template + inline + const typename Accessor::Reference & + Accessor::Reference::operator += (const number n) const + { +//TODO: one could optimize this by not going again through the mapping from row/col index to global index + accessor->matrix->set (accessor->row(), accessor->column(), + static_cast(*this) + n); + return *this; + } + + + + template + inline + const typename Accessor::Reference & + Accessor::Reference::operator -= (const number n) const + { +//TODO: one could optimize this by not going again through the mapping from row/col index to global index + accessor->matrix->set (accessor->row(), accessor->column(), + static_cast(*this) - n); + return *this; + } + + + + template + inline + const typename Accessor::Reference & + Accessor::Reference::operator *= (const number n) const + { +//TODO: one could optimize this by not going again through the mapping from row/col index to global index + accessor->matrix->set (accessor->row(), accessor->column(), + static_cast(*this)*n); + return *this; + } + + + + template + inline + const typename Accessor::Reference & + Accessor::Reference::operator /= (const number n) const + { +//TODO: one could optimize this by not going again through the mapping from row/col index to global index + accessor->matrix->set (accessor->row(), accessor->column(), + static_cast(*this)/n); + return *this; + } + + + template inline Accessor:: @@ -1727,14 +1877,15 @@ namespace internals template inline - number & + typename Accessor::Reference Accessor::value() const { - return matrix->raw_entry(a_row, a_index); + return this; } - - + + + template inline Iterator:: -- 2.39.5