From 0d1e2e967ef937d0bd9316068fd7b529c0ad7ba9 Mon Sep 17 00:00:00 2001 From: bangerth Date: Sun, 12 Feb 2012 17:15:36 +0000 Subject: [PATCH] Allow access to all vectors using both operator() and operator[] to make it simpler to write templates that also work with std::vector, for example. git-svn-id: https://svn.dealii.org/trunk@25043 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 13 ++++ .../include/deal.II/lac/block_vector_base.h | 37 +++++++++- deal.II/include/deal.II/lac/parallel_vector.h | 72 +++++++++++++++---- .../include/deal.II/lac/petsc_vector_base.h | 47 ++++++++++-- .../deal.II/lac/trilinos_vector_base.h | 38 +++++++++- deal.II/include/deal.II/lac/vector.h | 36 +++++++++- 6 files changed, 223 insertions(+), 20 deletions(-) diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 8b1dbc2213..920122e574 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -34,6 +34,19 @@ inconvenience this causes.

General

    +
  1. New: It has long been a nuisance that the deal.II vector classes +could only be accessed using operator() whereas the C++ +std::vector class required operator[]. 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 operator[] +for matrices since this operator can only take a single argument. +
    +In any case, all deal.II vector classes now support both kinds of +access operators interchangeably. +
    +(Wolfgang Bangerth, 2012/02/12) +
  2. 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. diff --git a/deal.II/include/deal.II/lac/block_vector_base.h b/deal.II/include/deal.II/lac/block_vector_base.h index a0111deb66..fa5d70a9ae 100644 --- a/deal.II/include/deal.II/lac/block_vector_base.h +++ b/deal.II/include/deal.II/lac/block_vector_base.h @@ -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::operator() (const unsigned int i) return components[local_index.first](local_index.second); } + + +template +inline +typename BlockVectorBase::value_type +BlockVectorBase::operator[] (const unsigned int i) const +{ + return operator()(i); +} + + + +template +inline +typename BlockVectorBase::reference +BlockVectorBase::operator[] (const unsigned int i) +{ + return operator()(i); +} + #endif // DOXYGEN DEAL_II_NAMESPACE_CLOSE diff --git a/deal.II/include/deal.II/lac/parallel_vector.h b/deal.II/include/deal.II/lac/parallel_vector.h index 5ce6bd152c..868280976d 100644 --- a/deal.II/include/deal.II/lac/parallel_vector.h +++ b/deal.II/include/deal.II/lac/parallel_vector.h @@ -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 + inline + Number + Vector::operator[] (const types::global_dof_index global_index) const + { + return operator()(global_index); + } + + + + template + inline + Number& + Vector::operator[] (const types::global_dof_index global_index) + { + return operator()(global_index); + } + + + template inline Number @@ -1854,7 +1902,7 @@ namespace parallel template inline bool - Vector::partitioners_are_compatible + Vector::partitioners_are_compatible (const Utilities::MPI::Partitioner &part) const { return partitioner->is_compatible (part); diff --git a/deal.II/include/deal.II/lac/petsc_vector_base.h b/deal.II/include/deal.II/lac/petsc_vector_base.h index a271f7e32c..2402d057e9 100644 --- a/deal.II/include/deal.II/lac/petsc_vector_base.h +++ b/deal.II/include/deal.II/lac/petsc_vector_base.h @@ -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 el() 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 } diff --git a/deal.II/include/deal.II/lac/trilinos_vector_base.h b/deal.II/include/deal.II/lac/trilinos_vector_base.h index d646ec54b6..7458489511 100644 --- a/deal.II/include/deal.II/lac/trilinos_vector_base.h +++ b/deal.II/include/deal.II/lac/trilinos_vector_base.h @@ -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 el() command. + * + * Exactly the same as operator(). + */ + TrilinosScalar + operator [] (const unsigned int index) const; + /** * Return the value of the vector * entry 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, diff --git a/deal.II/include/deal.II/lac/vector.h b/deal.II/include/deal.II/lac/vector.h index cd4b0180f4..b5885e18c3 100644 --- a/deal.II/include/deal.II/lac/vector.h +++ b/deal.II/include/deal.II/lac/vector.h @@ -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::operator() (const unsigned int i) +template +inline +Number Vector::operator[] (const unsigned int i) const +{ + return operator()(i); +} + + + +template +inline +Number& Vector::operator[] (const unsigned int i) +{ + return operator()(i); +} + + + template inline Vector & Vector::operator *= (const Number factor) -- 2.39.5