From d6ea96cf1eb581550c2825b297250f4b720a71ae Mon Sep 17 00:00:00 2001 From: wolf Date: Fri, 31 May 2002 06:36:11 +0000 Subject: [PATCH] Return a reference from the vector2d accessors in any case, since the objects stored may be large (e.g. Tensor<2,dim> in 3d), and let the compiler figure out how to optimize this for the case of scalar values. Introduce iterators for the row accessors. git-svn-id: https://svn.dealii.org/trunk@5949 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/vector2d.h | 169 +++++++++++++++++++++++++-- deal.II/doc/news/2002/c-3-3.html | 10 ++ tests/base/vector2d.cc | 4 + 3 files changed, 174 insertions(+), 9 deletions(-) diff --git a/deal.II/base/include/base/vector2d.h b/deal.II/base/include/base/vector2d.h index 6f36ab5e25..ee903918ba 100644 --- a/deal.II/base/include/base/vector2d.h +++ b/deal.II/base/include/base/vector2d.h @@ -61,6 +61,26 @@ class vector2d : public Subscriptor class ConstRowAccessor { public: + /** + * Typedef iterator and other + * types to the elements of + * this row. Since this class + * represents rows of + * constant objects, + * @p{iterator} and + * @p{const_iterator} have + * the same type. + */ + typedef const T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type* iterator; + typedef const value_type* const_iterator; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + /** * Constructor. */ @@ -70,7 +90,20 @@ class vector2d : public Subscriptor /** * Access operator. */ - T operator [] (const unsigned int column) const; + const_reference operator [] (const unsigned int column) const; + + /** + * Return an iterator to the + * first element of this row. + */ + const_iterator begin () const; + + /** + * Return an interator to the + * element past the end of + * this row. + */ + const_iterator end () const; protected: /** @@ -112,6 +145,26 @@ class vector2d : public Subscriptor class NonConstRowAccessor { public: + /** + * Typedef constant and + * non-constant iterator + * types to the elements of + * this row, as well as all + * the other types usually + * required for the standard + * library algorithms. + */ + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type* iterator; + typedef const value_type* const_iterator; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + + /** * Constructor. */ @@ -122,7 +175,36 @@ class vector2d : public Subscriptor /** * Access operator. */ - T & operator [] (const unsigned int column) const; + reference operator [] (const unsigned int column) const; + + /** + * Return an iterator to the + * first element of this + * row. Constant version. + */ + const_iterator begin () const; + + /** + * Return an iterator to the + * first element of this + * row. Non-constant version. + */ + iterator begin (); + + /** + * Return an interator to the + * element past the end of + * this row. Constant version. + */ + const_iterator end () const; + + /** + * Return an interator to the + * element past the end of + * this row. Non-constant + * version. + */ + iterator end (); private: /** @@ -240,10 +322,19 @@ class vector2d : public Subscriptor /** * Return the value of the - * element at position @p{(i,j)}. + * element at position @p{(i,j)} + * as a constant reference. + * + * We return the requested value + * as a constant reference rather + * than by value since this + * object may hold data types + * that may be large, and we + * don't know here whether + * copying is expensive or not. */ - T operator() (const unsigned int i, - const unsigned int j) const; + const T & operator() (const unsigned int i, + const unsigned int j) const; /** * Return a read-write reference to @@ -325,14 +416,23 @@ class vector2d : public Subscriptor /** * Return the value of the - * element @p{(i,j)}. + * element @p{(i,j)} as a + * read-only reference. * * This function does no bounds * checking and is only to be * used internally and in * functions already checked. + * + * We return the requested value + * as a constant reference rather + * than by value since this + * object may hold data types + * that may be large, and we + * don't know here whether + * copying is expensive or not. */ - T el (const unsigned int i, const unsigned int j) const; + const T & el (const unsigned int i, const unsigned int j) const; /** * Direct read-only access to @@ -411,6 +511,26 @@ operator [] (const unsigned int column) const +template +inline +typename vector2d::ConstRowAccessor::const_iterator +vector2d::ConstRowAccessor::begin () const +{ + return row_start; +}; + + + +template +inline +typename vector2d::ConstRowAccessor::const_iterator +vector2d::ConstRowAccessor::end () const +{ + return row_start+parent.n_cols(); +}; + + + template inline vector2d::NonConstRowAccessor:: @@ -424,6 +544,7 @@ NonConstRowAccessor (vector2d &parent, }; + template inline T & @@ -436,6 +557,36 @@ operator [] (const unsigned int column) const +template +inline +typename vector2d::NonConstRowAccessor::iterator +vector2d::NonConstRowAccessor::begin () +{ + return row_start; +}; + + + +template +inline +typename vector2d::NonConstRowAccessor::const_iterator +vector2d::NonConstRowAccessor::end () const +{ + return row_start+parent.n_cols(); +}; + + + +template +inline +typename vector2d::NonConstRowAccessor::iterator +vector2d::NonConstRowAccessor::end () +{ + return row_start+parent.n_cols(); +}; + + + template inline vector2d::~vector2d () @@ -653,7 +804,7 @@ vector2d::el (const unsigned int i, const unsigned int j) template -inline T +inline const T & vector2d::el (const unsigned int i, const unsigned int j) const { return val[i*n_cols()+j]; @@ -662,7 +813,7 @@ vector2d::el (const unsigned int i, const unsigned int j) const template -inline T +inline const T & vector2d::operator() (const unsigned int i, const unsigned int j) const { Assert (ibase
    +
  1. + New: The vector2d row accessor + classes now have member functions begin and end + which allow iterating over the elements of a row of such an + object. +
    + (WB 2002/05/30) +

    +
  2. New: The Legendre and LagrangeEquidistant classes now have diff --git a/tests/base/vector2d.cc b/tests/base/vector2d.cc index 467399b07b..87174a9d5f 100644 --- a/tests/base/vector2d.cc +++ b/tests/base/vector2d.cc @@ -48,6 +48,10 @@ main () Assert (Ti[i][j] == Ti(i,j), ExcInternalError()); Assert (Ti[i][j] == Td(i,j), ExcInternalError()); + Assert (Td[i].begin()+j == Td(i,j), ExcInternalError()); + Assert (Ti[i].begin()+j == Ti(i,j), ExcInternalError()); + Assert (Ti[i].begin()+j == Td(i,j), ExcInternalError()); + logfile << i << " " << j << " " << Td[i][j] << " ok" << std::endl; }; }; -- 2.39.5