From e088ba8e1d68bce44356b3b6534e80562e701190 Mon Sep 17 00:00:00 2001 From: wolf Date: Fri, 8 Mar 2002 10:03:41 +0000 Subject: [PATCH] Allow access to vector2d objects using x[i][j] instead of only x(i,j). git-svn-id: https://svn.dealii.org/trunk@5551 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/vector2d.h | 232 ++++++++++++++++++++++++++- deal.II/doc/news/2002/c-3-3.html | 11 ++ tests/base/Makefile | 5 +- tests/base/vector2d.cc | 56 +++++++ tests/base/vector2d.checked | 10 ++ 5 files changed, 309 insertions(+), 5 deletions(-) create mode 100644 tests/base/vector2d.cc create mode 100644 tests/base/vector2d.checked diff --git a/deal.II/base/include/base/vector2d.h b/deal.II/base/include/base/vector2d.h index ba5826cbb9..61b4a15571 100644 --- a/deal.II/base/include/base/vector2d.h +++ b/deal.II/base/include/base/vector2d.h @@ -17,11 +17,15 @@ #include +template class vector2d; + + /** * Two-dimensional array of arbitrary data. * * This is an implementation of a two-dimensional array with access by - * pairs of coordinates. + * pairs of coordinates. Access is either by @p{x(i,j)} to keep with + * matrix notation, or @p{x[i][j]} in accordance to C-style arrays. * * The name is chosen to be @p{vector2d} to be conformant with the * standard C++ classes. This, although array would have been @@ -33,6 +37,108 @@ template class vector2d : public Subscriptor { public: + /** + * Object representing one row of + * a @ref{vector2d} object. It + * allows to access the elements + * through the + * @p{operator[]}. Since objects + * of this type are also + * generated through the + * @p{vector2d::operator[]}, this + * allows accessing the elements + * of @p{vector2d} objects just + * as those of two-dimensional + * C-style arrays. + * + * This class is used for + * constant @p{vector2d} + * objects. It only allows + * read-only access to the + * elements. + */ + class ConstRowAccessor + { + public: + /** + * Constructor. + */ + ConstRowAccessor (const vector2d &parent, + const unsigned int row); + + /** + * Access operator. + */ + T operator [] (const unsigned int column) const; + + protected: + /** + * Pointer to the parent + * object. Used only to check + * access indices for + * validity. + */ + const vector2d &parent; + + /** + * Pointer to the start of + * the row pointed to by this + * object. + */ + const T * const row_start; + }; + + + /** + * Object representing one row of + * a @ref{vector2d} object. It + * allows to access the elements + * through the + * @p{operator[]}. Since objects + * of this type are also + * generated through the + * @p{vector2d::operator[]}, this + * allows accessing the elements + * of @p{vector2d} objects just + * as those of two-dimensional + * C-style arrays. + * + * This class is used for + * non-constant @p{vector2d} + * objects. It allows read and + * write access to the elements. + */ + class NonConstRowAccessor + { + public: + /** + * Constructor. + */ + NonConstRowAccessor (vector2d &parent, + const unsigned int row); + + + /** + * Access operator. + */ + T & operator [] (const unsigned int column) const; + + private: + /** + * Pointer to the parent + * object. Used only to check + * access indices for + * validity. + */ + const vector2d &parent; + + /** + * Pointer to the start of + * the row pointed to by this + * object. + */ + T * const row_start; + }; /** * Constructor for a quadratic * @p{rows} by @p{rows} array. The @@ -145,6 +251,42 @@ class vector2d : public Subscriptor T & operator() (const unsigned int i, const unsigned int j); + /** + * Return an object representing + * a certain row of this + * array. This object in turn has + * an @p{operator[]}, so that the + * elements of this array can be + * accessed through @p{x[i][j]} + * as well as through @p{x(i,j)}. + * + * This function is for constant + * objects. The returned row + * representing object only + * allows read access to the + * elements of the row pointed + * to. + */ + ConstRowAccessor operator [] (const unsigned int row) const; + + /** + * Return an object representing + * a certain row of this + * array. This object in turn has + * an @p{operator[]}, so that the + * elements of this array can be + * accessed through @p{x[i][j]} + * as well as through @p{x(i,j)}. + * + * This function is for + * non-constant objects. The + * returned row representing + * object allows read and write + * access to the elements of the + * row pointed to. + */ + NonConstRowAccessor operator [] (const unsigned int row); + /** * Set all entries to their @@ -234,12 +376,65 @@ class vector2d : public Subscriptor /** * This is unfortunately needed. */ - template friend class FullMatrix; + template friend class FullMatrix; + + friend class ConstRowAccessor; + friend class NonConstRowAccessor; }; /* --------------------- Template and inline functions ---------------- */ +template +inline +vector2d::ConstRowAccessor:: +ConstRowAccessor (const vector2d &parent, + const unsigned int row) + : + parent (parent), + row_start(&parent.val[row*parent.n_cols()]) +{ + Assert (row < parent.n_rows(), ExcInternalError()); +}; + + +template +inline +T +vector2d::ConstRowAccessor:: +operator [] (const unsigned int column) const +{ + Assert (column < parent.n_cols(), ExcInternalError()); + return *(row_start+column); +}; + + + +template +inline +vector2d::NonConstRowAccessor:: +NonConstRowAccessor (vector2d &parent, + const unsigned int row) + : + parent (parent), + row_start(&parent.val[row*parent.n_cols()]) +{ + Assert (row < parent.n_rows(), ExcInternalError()); +}; + + +template +inline +T & +vector2d::NonConstRowAccessor:: +operator [] (const unsigned int column) const +{ + Assert (column < parent.n_cols(), ExcInternalError()); + return *(row_start+column); +}; + + + template inline vector2d::~vector2d () @@ -477,7 +672,8 @@ vector2d::operator() (const unsigned int i, const unsigned int j) const template -inline T & +inline +T & vector2d::operator() (const unsigned int i, const unsigned int j) { Assert (i::operator() (const unsigned int i, const unsigned int j) +template +inline +typename vector2d::ConstRowAccessor +vector2d::operator [] (const unsigned int row) const +{ + // Note: check for validity of row + // number is done in the + // constructor of the created + // object + return ConstRowAccessor(*this, row); +}; + + + +template +inline +typename vector2d::NonConstRowAccessor +vector2d::operator [] (const unsigned int row) +{ + // Note: check for validity of row + // number is done in the + // constructor of the created + // object + return NonConstRowAccessor(*this, row); +}; + + + + + template inline const T * diff --git a/deal.II/doc/news/2002/c-3-3.html b/deal.II/doc/news/2002/c-3-3.html index 8711433206..7506939c15 100644 --- a/deal.II/doc/news/2002/c-3-3.html +++ b/deal.II/doc/news/2002/c-3-3.html @@ -67,6 +67,17 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK

base

    +
  1. + New: The vector2d class now not only + allows access to elements through the operator()(unsingned int,unsigned int) + (i.e. matrix or Fortran style access), but also through nested + brackets via an operator[] + (i.e. like to a two-dimensional C-style array). +
    + (WB 2002/03/08) +

    +
  2. Changed: The function MultithreadInfo:: get_n_cpus now reports the proper number diff --git a/tests/base/Makefile b/tests/base/Makefile index 6e99a2c61a..b60f5bcd69 100644 --- a/tests/base/Makefile +++ b/tests/base/Makefile @@ -2,7 +2,7 @@ # Generated automatically from Makefile.in by configure. ############################################################ # $Id$ -# Copyright (C) 2000, 2001 by the deal.II authors +# Copyright (C) 2000, 2001, 2002 by the deal.II authors ############################################################ ############################################################ @@ -27,10 +27,11 @@ quadrature_test.exe: quadrature_test.go $(libraries) reference.exe : reference.go abort.go $(libraries) tensor.exe : tensor.go $(libraries) timer.exe : timer.go $(libraries) +vector2d.exe : vector2d.go $(libraries) auto_derivative_function.exe : auto_derivative_function.go $(libraries) -tests = logtest reference quadrature_test tensor timer polynomial1d polynomial_test auto_derivative_function +tests = logtest reference quadrature_test tensor timer polynomial1d polynomial_test auto_derivative_function vector2d ############################################################ diff --git a/tests/base/vector2d.cc b/tests/base/vector2d.cc new file mode 100644 index 0000000000..467399b07b --- /dev/null +++ b/tests/base/vector2d.cc @@ -0,0 +1,56 @@ +//---------------------------- $RCSfile$ --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 1998, 1999, 2000, 2001, 2002 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- $RCSfile$ --------------------------- + + +#include +#include +#include +#include +#include + +#include +#include + +const int entries[9] = { 11,12,13,21,22,23,31,32,33 }; + +int +main () +{ + std::ofstream logfile("vector2d.output"); + logfile.setf(std::ios::fixed); + logfile.precision(3); + deallog.attach(logfile); + deallog.depth_console(0); + + vector2d Td(3,3); + vector2d Ti(3,3); + + for (unsigned int i=0; i<9; ++i) + { + Td[i/3][i%3] = entries[i]; + Ti[i/3][i%3] = entries[i]; + }; + + for (unsigned int i=0; i<3; ++i) + for (unsigned int j=0; j<3; ++j) + { + Assert (Td[i][j] == Td(i,j), ExcInternalError()); + Assert (Ti[i][j] == Ti(i,j), ExcInternalError()); + Assert (Ti[i][j] == Td(i,j), ExcInternalError()); + + logfile << i << " " << j << " " << Td[i][j] << " ok" << std::endl; + }; +}; + + + diff --git a/tests/base/vector2d.checked b/tests/base/vector2d.checked new file mode 100644 index 0000000000..64879da495 --- /dev/null +++ b/tests/base/vector2d.checked @@ -0,0 +1,10 @@ + +0 0 11.000 ok +0 1 12.000 ok +0 2 13.000 ok +1 0 21.000 ok +1 1 22.000 ok +1 2 23.000 ok +2 0 31.000 ok +2 1 32.000 ok +2 2 33.000 ok -- 2.39.5