]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
SparseMatrix::const_iterator
authorguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 11 Sep 2002 16:06:51 +0000 (16:06 +0000)
committerguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 11 Sep 2002 16:06:51 +0000 (16:06 +0000)
git-svn-id: https://svn.dealii.org/trunk@6392 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/2002/c-3-4.html
deal.II/lac/include/lac/sparse_matrix.h
deal.II/lac/include/lac/sparse_matrix_ez.h
tests/lac/sparse_matrices.cc
tests/lac/sparse_matrices.checked

index ea1b4e814c88ae4c1f37b2b3d44a5d982cd30a75..09a398dbfacaaa44006b4f3572b568d82b2e4535 100644 (file)
@@ -221,6 +221,17 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK
 <h3>lac</h3>
 
 <ol>
+  <li> <p> New: class <code class="class">SparseMatrix</code> has an
+  STL-conforming <code class="class">const_iterator</code> and
+  functions <code class="member">begin()</code> and <code
+  class="member">end()</code> for looping through all existing
+  entries. Furthermore, <code class="member">begin(row)</code> and
+  <code class="member">end(row)</code> allow looping through all
+  entries of a single line.
+  <br>
+  (GK 2002/09/11)
+  </p>
+
   <li> <p>
        New: Classes <code class="class">SparsityPattern</code> and <code
        class="class">SparseMatrix</code> now have functions <code 
index 2f0e5d0d2535c5df3c79b034a2db5222a65c50d6..29c71926bcf59e175cbc371ec672c478868e379b 100644 (file)
@@ -44,6 +44,122 @@ template <typename number>
 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<number>*,
+                 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<number>* 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<number>*,
+                      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<number>::copy_from (const ForwardIterator begin,
 };
 
 
+//----------------------------------------------------------------------//
+
+template <typename number>
+inline
+SparseMatrix<number>::Accessor::Accessor (
+  const SparseMatrix<number>* matrix,
+  unsigned int r,
+  unsigned short i)
+               :
+               matrix(matrix),
+               a_row(r),
+               a_index(i)
+{}
+
+
+template <typename number>
+inline
+unsigned int
+SparseMatrix<number>::Accessor::row() const
+{
+  return a_row;
+}
+
+
+template <typename number>
+inline
+unsigned int
+SparseMatrix<number>::Accessor::column() const
+{
+  const SparsityPattern& pat = matrix->get_sparsity_pattern();
+  return pat.get_column_numbers()[pat.get_rowstart_indices()[a_row]+a_index];
+}
+
+
+template <typename number>
+inline
+unsigned short
+SparseMatrix<number>::Accessor::index() const
+{
+  return a_index;
+}
+
+
+
+template <typename number>
+inline
+number
+SparseMatrix<number>::Accessor::value() const
+{
+  return matrix->raw_entry(a_row, a_index);
+}
+
+
+template <typename number>
+inline
+SparseMatrix<number>::const_iterator::const_iterator(
+  const SparseMatrix<number>* matrix,
+  unsigned int r,
+  unsigned short i)
+               :
+               Accessor(matrix, r, i)
+{}
+
+
+template <typename number>
+inline
+typename SparseMatrix<number>::const_iterator&
+SparseMatrix<number>::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 <typename number>
+inline
+const typename SparseMatrix<number>::Accessor&
+SparseMatrix<number>::const_iterator::operator* () const
+{
+  return *this;
+}
+
+
+template <typename number>
+inline
+const typename SparseMatrix<number>::Accessor*
+SparseMatrix<number>::const_iterator::operator-> () const
+{
+  return this;
+}
+
+
+template <typename number>
+inline
+bool
+SparseMatrix<number>::const_iterator::operator == (
+  const const_iterator& other) const
+{
+  return (row() == other->row() && index() == other->index());
+}
+
+
+template <typename number>
+inline
+bool
+SparseMatrix<number>::const_iterator::operator != (
+  const const_iterator& other) const
+{
+  return ! (*this == other);
+}
+
+
+template <typename number>
+inline
+bool
+SparseMatrix<number>::const_iterator::operator < (
+  const const_iterator& other) const
+{
+  return (row() < other->row() ||
+         (row() == other->row() && index() < other->index()));
+}
+
+
+template <typename number>
+inline
+typename SparseMatrix<number>::const_iterator
+SparseMatrix<number>::begin () const
+{
+  return const_iterator(this, 0, 0);
+}
+
+template <typename number>
+inline
+typename SparseMatrix<number>::const_iterator
+SparseMatrix<number>::end () const
+{
+  return const_iterator(this, m(), 0);
+}
+
+template <typename number>
+inline
+typename SparseMatrix<number>::const_iterator
+SparseMatrix<number>::begin (unsigned int r) const
+{
+  Assert (r<m(), ExcIndexRange(r,0,m()));
+  return const_iterator(this, r, 0);
+}
+
+template <typename number>
+inline
+typename SparseMatrix<number>::const_iterator
+SparseMatrix<number>::end (unsigned int r) const
+{
+  Assert (r<m(), ExcIndexRange(r,0,m()));
+  return const_iterator(this, r+1, 0);
+}
+
+
+
 
 /*----------------------------   sparse_matrix.h     ---------------------------*/
 
index a04a120a9d4d1b3dbe861cba5b134b1188d7d4d4..b296b54386f6cfe1c8bedbfe4670e7f13d946cf5 100644 (file)
@@ -1054,7 +1054,7 @@ SparseMatrixEZ<number>::const_iterator::const_iterator(
 
 template <typename number>
 inline
-SparseMatrixEZ<number>::const_iterator&
+typename SparseMatrixEZ<number>::const_iterator&
 SparseMatrixEZ<number>::const_iterator::operator++ ()
 {
   Assert (a_row < matrix->m(), ExcIteratorPastEnd());
@@ -1071,7 +1071,7 @@ SparseMatrixEZ<number>::const_iterator::operator++ ()
 
 template <typename number>
 inline
-const SparseMatrixEZ<number>::Accessor&
+const typename SparseMatrixEZ<number>::Accessor&
 SparseMatrixEZ<number>::const_iterator::operator* () const
 {
   return *this;
@@ -1080,7 +1080,7 @@ SparseMatrixEZ<number>::const_iterator::operator* () const
 
 template <typename number>
 inline
-const SparseMatrixEZ<number>::Accessor*
+const typename SparseMatrixEZ<number>::Accessor*
 SparseMatrixEZ<number>::const_iterator::operator-> () const
 {
   return this;
@@ -1137,9 +1137,9 @@ unsigned int SparseMatrixEZ<number>::n () const
 
 template <typename number>
 inline
-const SparseMatrixEZ<number>::Entry* SparseMatrixEZ<number>::locate (
-  const unsigned int row,
-  const unsigned int col) const
+const typename SparseMatrixEZ<number>::Entry*
+SparseMatrixEZ<number>::locate ( const unsigned int row,
+                                const unsigned int col) const
 {
   Assert (row<m(), ExcIndexRange(row,0,m()));
   Assert (col<n(), ExcIndexRange(col,0,n()));
@@ -1161,9 +1161,9 @@ const SparseMatrixEZ<number>::Entry* SparseMatrixEZ<number>::locate (
 
 template <typename number>
 inline
-SparseMatrixEZ<number>::Entry* SparseMatrixEZ<number>::allocate (
-  const unsigned int row,
-  const unsigned int col)
+typename SparseMatrixEZ<number>::Entry*
+SparseMatrixEZ<number>::allocate (const unsigned int row,
+                                 const unsigned int col)
 {
   Assert (row<m(), ExcIndexRange(row,0,m()));
   Assert (col<n(), ExcIndexRange(col,0,n()));
@@ -1304,7 +1304,7 @@ number SparseMatrixEZ<number>::operator() (const unsigned int i,
 
 template <typename number>
 inline
-SparseMatrixEZ<number>::const_iterator
+typename SparseMatrixEZ<number>::const_iterator
 SparseMatrixEZ<number>::begin () const
 {
   return const_iterator(this, 0, 0);
@@ -1312,7 +1312,7 @@ SparseMatrixEZ<number>::begin () const
 
 template <typename number>
 inline
-SparseMatrixEZ<number>::const_iterator
+typename SparseMatrixEZ<number>::const_iterator
 SparseMatrixEZ<number>::end () const
 {
   return const_iterator(this, m(), 0);
@@ -1320,7 +1320,7 @@ SparseMatrixEZ<number>::end () const
 
 template <typename number>
 inline
-SparseMatrixEZ<number>::const_iterator
+typename SparseMatrixEZ<number>::const_iterator
 SparseMatrixEZ<number>::begin (unsigned int r) const
 {
   Assert (r<m(), ExcIndexRange(r,0,m()));
@@ -1329,7 +1329,7 @@ SparseMatrixEZ<number>::begin (unsigned int r) const
 
 template <typename number>
 inline
-SparseMatrixEZ<number>::const_iterator
+typename SparseMatrixEZ<number>::const_iterator
 SparseMatrixEZ<number>::end (unsigned int r) const
 {
   Assert (r<m(), ExcIndexRange(r,0,m()));
index e2469c2cd4a4a5b2b6175d6b891efbe6af4954d4..241aadbd412aa0e2f4b57a2a9417c13880cec10e 100644 (file)
@@ -128,6 +128,9 @@ int main()
   deallog << "Assemble" << std::endl;
   testproblem.five_point(A, true);
   check_vmult_quadratic(A_res, A, "5-SparseMatrix<double>");
+#ifdef DEBUG
+  check_iterator(A);
+#endif
 
   SparseMatrixEZ<double> E(dim,dim,row_length,2);
   deallog << "Assemble" << std::endl;
index 7b9387f15b615ba34aca3729a44a03eb94ecb11b..15d81282aacbd8d208aaea3f6702d343c23bc51f 100644 (file)
@@ -20,6 +20,75 @@ DEAL:5-SparseMatrix<double>:RichardsonT::Starting
 DEAL:5-SparseMatrix<double>: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<double>:Richardson::Starting 
 DEAL:5-SparseMatrixEZ<double>:Richardson::Failure step 10 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.