]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Allow access to all vectors using both operator() and operator[] to make it simpler...
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 12 Feb 2012 17:15:36 +0000 (17:15 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 12 Feb 2012 17:15:36 +0000 (17:15 +0000)
git-svn-id: https://svn.dealii.org/trunk@25043 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/lac/block_vector_base.h
deal.II/include/deal.II/lac/parallel_vector.h
deal.II/include/deal.II/lac/petsc_vector_base.h
deal.II/include/deal.II/lac/trilinos_vector_base.h
deal.II/include/deal.II/lac/vector.h

index 8b1dbc2213e23d0351591ec7622efa2141ac8cd3..920122e5743d0361e42a42cac2eaa1eaff65958f 100644 (file)
@@ -34,6 +34,19 @@ inconvenience this causes.
 <h3>General</h3>
 
 <ol>
+<li> New: It has long been a nuisance that the deal.II vector classes
+could only be accessed using <code>operator()</code> whereas the C++
+<code>std::vector</code> class required <code>operator[]</code>. This
+diminished the usefulness of template code. Historically, the reason
+was that the deal.II vector classes should use the same operator as
+the matrix classes, and C++ does not allow to use <code>operator[]</code>
+for matrices since this operator can only take a single argument.
+<br>
+In any case, all deal.II vector classes now support both kinds of
+access operators interchangeably.
+<br>
+(Wolfgang Bangerth, 2012/02/12)
+
 <li> Fixed: Linking shared libraries on PowerPC systems (e.g. on
 BlueGene systems) failed due to a miscommunication between compiler
 and linker. This is now worked around.
index a0111deb6653d05560523f51bb611f91650285e5..fa5d70a9aee0180cb7e08e978d9ddfab9ec41233 100644 (file)
@@ -1,7 +1,7 @@
 //---------------------------------------------------------------------------
 //    $Id$
 //
-//    Copyright (C) 2004, 2005, 2006, 2007, 2010, 2011 by the deal.II authors
+//    Copyright (C) 2004, 2005, 2006, 2007, 2010, 2011, 2012 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -864,6 +864,21 @@ class BlockVectorBase : public Subscriptor
                                      */
     reference operator() (const unsigned int i);
 
+                                    /**
+                                     * Access components, returns U(i).
+                                     *
+                                     * Exactly the same as operator().
+                                     */
+    value_type operator[] (const unsigned int i) const;
+
+                                    /**
+                                     * Access components, returns U(i)
+                                     * as a writeable reference.
+                                     *
+                                     * Exactly the same as operator().
+                                     */
+    reference operator[] (const unsigned int i);
+
                                     /**
                                      * Copy operator: fill all components of
                                      * the vector with the given scalar
@@ -2397,6 +2412,26 @@ BlockVectorBase<VectorType>::operator() (const unsigned int i)
   return components[local_index.first](local_index.second);
 }
 
+
+
+template <class VectorType>
+inline
+typename BlockVectorBase<VectorType>::value_type
+BlockVectorBase<VectorType>::operator[] (const unsigned int i) const
+{
+  return operator()(i);
+}
+
+
+
+template <class VectorType>
+inline
+typename BlockVectorBase<VectorType>::reference
+BlockVectorBase<VectorType>::operator[] (const unsigned int i)
+{
+  return operator()(i);
+}
+
 #endif // DOXYGEN
 
 DEAL_II_NAMESPACE_CLOSE
index 5ce6bd152c39f1d3d6014eb4cdbcd848249c8125..868280976d330be6605427b004f5fdcfd379f19d 100644 (file)
@@ -567,22 +567,50 @@ namespace parallel
                                         //@{
 
                                         /**
-                                         * Read access to the data in the position
-                                         * corresponding to @p global_index. The index
-                                         * must be either in the local range of the
-                                         * vector or be specified as a ghost index at
-                                         * construction.
+                                         * Read access to the data in the
+                                         * position corresponding to @p
+                                         * global_index. The index must be
+                                         * either in the local range of the
+                                         * vector or be specified as a ghost
+                                         * index at construction.
+                                         */
+       Number operator () (const types::global_dof_index global_index) const;
+
+                                        /**
+                                         * Read and write access to the data
+                                         * in the position corresponding to
+                                         * @p global_index. The index must be
+                                         * either in the local range of the
+                                         * vector or be specified as a ghost
+                                         * index at construction.
                                          */
-       Number operator() (const types::global_dof_index global_index) const;
+       Number& operator () (const types::global_dof_index global_index);
 
                                         /**
-                                         * Read and write access to the data in the
+                                         * Read access to the data in the
                                          * position corresponding to @p
-                                         * global_index. The index must be either in
-                                         * the local range of the vector or be
-                                         * specified as a ghost index at construction.
+                                         * global_index. The index must be
+                                         * either in the local range of the
+                                         * vector or be specified as a ghost
+                                         * index at construction.
+                                         *
+                                         * This function does the same thing
+                                         * as operator().
                                          */
-       Number& operator() (const types::global_dof_index global_index);
+       Number operator [] (const types::global_dof_index global_index) const;
+
+                                        /**
+                                         * Read and write access to the data
+                                         * in the position corresponding to
+                                         * @p global_index. The index must be
+                                         * either in the local range of the
+                                         * vector or be specified as a ghost
+                                         * index at construction.
+                                         *
+                                         * This function does the same thing
+                                         * as operator().
+                                         */
+       Number& operator [] (const types::global_dof_index global_index);
 
                                         /**
                                          * Read access to the data field specified by
@@ -1414,6 +1442,26 @@ namespace parallel
 
 
 
+    template <typename Number>
+    inline
+    Number
+    Vector<Number>::operator[] (const types::global_dof_index global_index) const
+    {
+      return operator()(global_index);
+    }
+
+
+
+    template <typename Number>
+    inline
+    Number&
+    Vector<Number>::operator[] (const types::global_dof_index global_index)
+    {
+      return operator()(global_index);
+    }
+
+
+
     template <typename Number>
     inline
     Number
@@ -1854,7 +1902,7 @@ namespace parallel
     template <typename Number>
     inline
     bool
-    Vector<Number>::partitioners_are_compatible 
+    Vector<Number>::partitioners_are_compatible
     (const Utilities::MPI::Partitioner &part) const
     {
       return partitioner->is_compatible (part);
index a271f7e32ceaad8a780e83bb6edc4209287da3ad..2402d057e9063e1e8a69a90b197d466ef8e04926 100644 (file)
@@ -1,7 +1,7 @@
 //---------------------------------------------------------------------------
 //    $Id$
 //
-//    Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 by the deal.II authors
+//    Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -277,7 +277,7 @@ namespace PETScWrappers
                                        * PETSc object in the destructor.
                                        */
       explicit VectorBase (const Vec & v);
-      
+
                                        /**
                                         * Destructor
                                         */
@@ -392,7 +392,7 @@ namespace PETScWrappers
                                        * elements.
                                        */
       bool has_ghost_elements() const;
-       
+
                                        /**
                                         * Provide access to a given element,
                                         * both read and write.
@@ -407,6 +407,25 @@ namespace PETScWrappers
       PetscScalar
       operator () (const unsigned int index) const;
 
+                                       /**
+                                        * Provide access to a given
+                                        * element, both read and write.
+                                       *
+                                       * Exactly the same as operator().
+                                        */
+      reference
+      operator [] (const unsigned int index);
+
+                                       /**
+                                        * Provide read-only access to an
+                                        * element. This is equivalent to
+                                        * the <code>el()</code> command.
+                                       *
+                                       * Exactly the same as operator().
+                                        */
+      PETScScalar
+      operator [] (const unsigned int index) const;
+
                                        /**
                                         * A collective set operation: instead
                                         * of setting individual elements of a
@@ -841,7 +860,7 @@ namespace PETScWrappers
                                        * the destructor.
                                        */
       bool attained_ownership;
-      
+
                                       /**
                                         * Collective set or add
                                         * operation: This function is
@@ -1127,7 +1146,7 @@ namespace PETScWrappers
   {
     return ghosted;
   }
-  
+
 
 
   inline
@@ -1147,6 +1166,24 @@ namespace PETScWrappers
   }
 
 
+
+  inline
+  internal::VectorReference
+  VectorBase::operator [] (const unsigned int index)
+  {
+    return operator()(index);
+  }
+
+
+
+  inline
+  PetscScalar
+  VectorBase::operator [] (const unsigned int index) const
+  {
+    return operator()(index);
+  }
+
+
 #endif // DOXYGEN
 }
 
index d646ec54b62c41467ff5467ba80e1e70931e5cc9..7458489511306c95dbe85c2b23d1c90ec4e3f9f6 100644 (file)
@@ -1,7 +1,7 @@
 //---------------------------------------------------------------------------
 //    $Id$
 //
-//    Copyright (C) 2008, 2009, 2010, 2011 by the deal.II authors
+//    Copyright (C) 2008, 2009, 2010, 2011, 2012 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -589,6 +589,25 @@ namespace TrilinosWrappers
       TrilinosScalar
        operator () (const unsigned int index) const;
 
+                                       /**
+                                        * Provide access to a given
+                                        * element, both read and write.
+                                       *
+                                       * Exactly the same as operator().
+                                        */
+      reference
+       operator [] (const unsigned int index);
+
+                                       /**
+                                        * Provide read-only access to an
+                                        * element. This is equivalent to
+                                        * the <code>el()</code> command.
+                                       *
+                                       * Exactly the same as operator().
+                                        */
+      TrilinosScalar
+       operator [] (const unsigned int index) const;
+
                                        /**
                                         * Return the value of the vector
                                         * entry <i>i</i>. Note that this
@@ -1151,6 +1170,23 @@ namespace TrilinosWrappers
 
 
 
+  inline
+  internal::VectorReference
+  VectorBase::operator [] (const unsigned int index)
+  {
+    return operator() (index);
+  }
+
+
+  inline
+  TrilinosScalar
+  VectorBase::operator [] (const unsigned int index) const
+  {
+    return operator() (index);
+  }
+
+
+
   inline
   void
   VectorBase::reinit (const VectorBase &v,
index cd4b0180f478821c3e53ea6dfbca592a068753e2..b5885e18c31047b9aa67b68afa55b0d66cf3edeb 100644 (file)
@@ -1,7 +1,7 @@
 //---------------------------------------------------------------------------
 //    $Id$
 //
-//    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 by the deal.II authors
+//    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -638,6 +638,22 @@ class Vector : public Subscriptor
                                      * as a writeable reference.
                                      */
     Number& operator() (const unsigned int i);
+
+                                    /**
+                                     * Access the value of the @p ith
+                                     * component.
+                                     *
+                                     * Exactly the same as operator().
+                                     */
+    Number operator[] (const unsigned int i) const;
+
+                                    /**
+                                     * Access the @p ith component
+                                     * as a writeable reference.
+                                     *
+                                     * Exactly the same as operator().
+                                     */
+    Number& operator[] (const unsigned int i);
                                     //@}
 
 
@@ -1309,6 +1325,24 @@ Number& Vector<Number>::operator() (const unsigned int i)
 
 
 
+template <typename Number>
+inline
+Number Vector<Number>::operator[] (const unsigned int i) const
+{
+  return operator()(i);
+}
+
+
+
+template <typename Number>
+inline
+Number& Vector<Number>::operator[] (const unsigned int i)
+{
+  return operator()(i);
+}
+
+
+
 template <typename Number>
 inline
 Vector<Number> & Vector<Number>::operator *= (const Number factor)

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.