]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
const_iterator in FullMatrix
authorguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 18 Jul 2003 12:16:48 +0000 (12:16 +0000)
committerguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 18 Jul 2003 12:16:48 +0000 (12:16 +0000)
git-svn-id: https://svn.dealii.org/trunk@7879 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/include/lac/full_matrix.h
tests/lac/full_matrix.cc
tests/results/i686-pc-linux-gnu+gcc3.2/lac/full_matrix.output

index ad2f93748bc06505763e334a94fd1cdff8cb0fcc..456a3c97ae98d72e65727777f4da16e0ccdb0bef 100644 (file)
@@ -68,6 +68,129 @@ template<typename number>
 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<number> *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<number>* 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<number> *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<number>::copy_from (const MATRIX& M)
     el(entry->row(), entry->column()) = entry->value();
 }
 
+//----------------------------------------------------------------------//
+
+
+template <typename number>
+inline
+FullMatrix<number>::const_iterator::Accessor::
+Accessor (const FullMatrix<number>* matrix,
+          const unsigned int r,
+          const unsigned int c)
+               :
+               matrix(matrix),
+               a_row(r),
+               a_col(c)
+{}
+
+
+template <typename number>
+inline
+unsigned int
+FullMatrix<number>::const_iterator::Accessor::row() const
+{
+  return a_row;
+}
+
+
+template <typename number>
+inline
+unsigned int
+FullMatrix<number>::const_iterator::Accessor::column() const
+{
+  return a_col;
+}
+
+
+template <typename number>
+inline
+number
+FullMatrix<number>::const_iterator::Accessor::value() const
+{
+  return matrix->el(a_row, a_col);
+}
+
+
+template <typename number>
+inline
+FullMatrix<number>::const_iterator::
+const_iterator(const FullMatrix<number> *matrix,
+               const unsigned int r,
+               const unsigned int c)
+               :
+               accessor(matrix, r, c)
+{}
+
+
+template <typename number>
+inline
+typename FullMatrix<number>::const_iterator &
+FullMatrix<number>::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 <typename number>
+inline
+const typename FullMatrix<number>::const_iterator::Accessor &
+FullMatrix<number>::const_iterator::operator* () const
+{
+  return accessor;
+}
+
+
+template <typename number>
+inline
+const typename FullMatrix<number>::const_iterator::Accessor *
+FullMatrix<number>::const_iterator::operator-> () const
+{
+  return &accessor;
+}
+
+
+template <typename number>
+inline
+bool
+FullMatrix<number>::const_iterator::
+operator == (const const_iterator& other) const
+{
+  return (accessor.row() == other.accessor.row() &&
+          accessor.column() == other.accessor.column());
+}
+
+
+template <typename number>
+inline
+bool
+FullMatrix<number>::const_iterator::
+operator != (const const_iterator& other) const
+{
+  return ! (*this == other);
+}
+
+
+template <typename number>
+inline
+bool
+FullMatrix<number>::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 <typename number>
+inline
+typename FullMatrix<number>::const_iterator
+FullMatrix<number>::begin () const
+{
+  return const_iterator(this, 0, 0);
+}
+
+
+template <typename number>
+inline
+typename FullMatrix<number>::const_iterator
+FullMatrix<number>::end () const
+{
+  return const_iterator(this, m(), 0);
+}
+
+
+template <typename number>
+inline
+typename FullMatrix<number>::const_iterator
+FullMatrix<number>::begin (const unsigned int r) const
+{
+  Assert (r<m(), ExcIndexRange(r,0,m()));
+  return const_iterator(this, r, 0);
+}
+
+
+
+template <typename number>
+inline
+typename FullMatrix<number>::const_iterator
+FullMatrix<number>::end (const unsigned int r) const
+{
+  Assert (r<m(), ExcIndexRange(r,0,m()));
+  return const_iterator(this, r+1, 0);
+}
 
-/*----------------------------   fullmatrix.h     ---------------------------*/
 
 #endif
-/*----------------------------   fullmatrix.h     ---------------------------*/
+
index d206038dda067b2faf6a9612c019604941177d31..fb0334fa99d28d2faed051bf1d1f53e9f06d827d 100644 (file)
@@ -50,6 +50,28 @@ main ()
 
   FullMatrix<double> T(3,3,entries);
   T.print_formatted(logfile, 0, false);
+
+  FullMatrix<double>::const_iterator it = T.begin();
+  FullMatrix<double>::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)
     {
index 19d911e0cf02708446a979b396cf8b439d1389ba..1066e76b2cc021cd466a64e3f1212332e4f7e0c5 100644 (file)
@@ -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):

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.