From: Matthias Maier Date: Fri, 1 May 2015 19:47:51 +0000 (+0200) Subject: Add move constructor and operator to Vector and BlockVector X-Git-Tag: v8.3.0-rc1~203^2~6 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6cd88227176278d7982af5bb13c9d5da0573dc9;p=dealii.git Add move constructor and operator to Vector and BlockVector --- diff --git a/include/deal.II/lac/block_vector.h b/include/deal.II/lac/block_vector.h index 0690e456da..6b1ac989cf 100644 --- a/include/deal.II/lac/block_vector.h +++ b/include/deal.II/lac/block_vector.h @@ -99,12 +99,24 @@ public: const size_type block_size = 0); /** - * Copy-Constructor. Dimension set to that of V, all components are copied - * from V + * Copy Constructor. Dimension set to that of @p v, all components are + * copied from @p v. */ BlockVector (const BlockVector &V); +#ifdef DEAL_II_WITH_CXX11 + /** + * Move constructor. Creates a new vector by stealing the internal data + * of the vector @p v. + * + * @note This constructor is only available if deal.II is configured with + * C++11 support. + */ + BlockVector (BlockVector &&v); +#endif + + #ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG /** * Copy constructor taking a BlockVector of another data type. This will @@ -180,27 +192,39 @@ public: * Copy operator: fill all components of the vector with the given scalar * value. */ - BlockVector &operator = (const value_type s); + BlockVector &operator= (const value_type s); /** * Copy operator for arguments of the same type. Resize the present vector * if necessary. */ - BlockVector & - operator= (const BlockVector &V); + BlockVector & + operator= (const BlockVector &v); + +#ifdef DEAL_II_WITH_CXX11 + /** + * Move the given vector. This operator replaces the present vector with + * @p v by efficiently swapping the internal data structures. @p v is + * left empty. + * + * @note This operator is only available if deal.II is configured with + * C++11 support. + */ + BlockVector &operator= (BlockVector &&v); +#endif /** * Copy operator for template arguments of different types. Resize the * present vector if necessary. */ template - BlockVector & + BlockVector & operator= (const BlockVector &V); /** * Copy a regular vector into a block vector. */ - BlockVector & + BlockVector & operator= (const Vector &V); #ifdef DEAL_II_WITH_TRILINOS @@ -208,9 +232,10 @@ public: * A copy constructor from a Trilinos block vector to a deal.II block * vector. */ - BlockVector & + BlockVector & operator= (const TrilinosWrappers::BlockVector &V); #endif + /** * Reinitialize the BlockVector to contain n_blocks blocks of size * block_size each. @@ -378,12 +403,12 @@ BlockVector::BlockVector (const std::vector &block_sizes, template inline BlockVector & -BlockVector::operator = (const value_type s) +BlockVector::operator= (const value_type s) { AssertIsFinite(s); - BaseClass::operator = (s); + BaseClass::operator= (s); return *this; } @@ -392,21 +417,37 @@ BlockVector::operator = (const value_type s) template inline BlockVector & -BlockVector::operator = (const BlockVector &v) +BlockVector::operator= (const BlockVector &v) { reinit (v, true); - BaseClass::operator = (v); + BaseClass::operator= (v); return *this; } +#ifdef DEAL_II_WITH_CXX11 +template +inline +BlockVector & +BlockVector::operator= (BlockVector &&v) +{ + swap(v); + // be nice and reset v to zero + v.reinit(0, 0, false); + + return *this; +} +#endif + + + template inline BlockVector & -BlockVector::operator = (const Vector &v) +BlockVector::operator= (const Vector &v) { - BaseClass::operator = (v); + BaseClass::operator= (v); return *this; } @@ -416,10 +457,10 @@ template template inline BlockVector & -BlockVector::operator = (const BlockVector &v) +BlockVector::operator= (const BlockVector &v) { reinit (v, true); - BaseClass::operator = (v); + BaseClass::operator= (v); return *this; } diff --git a/include/deal.II/lac/block_vector.templates.h b/include/deal.II/lac/block_vector.templates.h index 37fe84c8f2..b223c6c203 100644 --- a/include/deal.II/lac/block_vector.templates.h +++ b/include/deal.II/lac/block_vector.templates.h @@ -61,6 +61,17 @@ BlockVector::BlockVector (const BlockVector &v) } +#ifdef DEAL_II_WITH_CXX11 +template +BlockVector::BlockVector (BlockVector &&v) + { + swap(v); + // be nice and reset v to zero + v.reinit(0, 0, false); + } +#endif + + #ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG template @@ -151,9 +162,9 @@ BlockVector::~BlockVector () template inline BlockVector & -BlockVector::operator = (const TrilinosWrappers::BlockVector &v) +BlockVector::operator= (const TrilinosWrappers::BlockVector &v) { - BaseClass::operator = (v); + BaseClass::operator= (v); return *this; } #endif diff --git a/include/deal.II/lac/vector.h b/include/deal.II/lac/vector.h index 21de7dc933..318486ea5d 100644 --- a/include/deal.II/lac/vector.h +++ b/include/deal.II/lac/vector.h @@ -156,7 +156,7 @@ public: Vector (); /** - * Copy-constructor. Sets the dimension to that of the given vector, and + * Copy constructor. Sets the dimension to that of the given vector, and * copies all elements. * * We would like to make this constructor explicit, but standard containers @@ -164,6 +164,17 @@ public: */ Vector (const Vector &v); +#ifdef DEAL_II_WITH_CXX11 + /** + * Move constructor. Creates a new vector by stealing the internal data + * of the vector @p v. + * + * @note This constructor is only available if deal.II is configured with + * C++11 support. + */ + Vector (Vector &&v); +#endif + #ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG /** @@ -325,7 +336,7 @@ public: * * @dealiiOperationIsMultithreaded */ - Vector &operator = (const Number s); + Vector &operator= (const Number s); /** * Copy the given vector. Resize the present vector if necessary. @@ -334,6 +345,18 @@ public: */ Vector &operator= (const Vector &v); +#ifdef DEAL_II_WITH_CXX11 + /** + * Move the given vector. This operator replaces the present vector with + * @p v by efficiently swapping the internal data structures. @p v is + * left empty. + * + * @note This operator is only available if deal.II is configured with + * C++11 support. + */ + Vector &operator= (Vector &&v); +#endif + /** * Copy the given vector. Resize the present vector if necessary. * @@ -354,7 +377,7 @@ public: * during configuration time. */ Vector & - operator = (const PETScWrappers::Vector &v); + operator= (const PETScWrappers::Vector &v); /** * Another copy operator: copy the values from a parallel PETSc wrapper @@ -367,7 +390,7 @@ public: * the other jobs do something else. */ Vector & - operator = (const PETScWrappers::MPI::Vector &v); + operator= (const PETScWrappers::MPI::Vector &v); #endif @@ -384,7 +407,7 @@ public: * the other jobs do something else. */ Vector & - operator = (const TrilinosWrappers::MPI::Vector &v); + operator= (const TrilinosWrappers::MPI::Vector &v); /** * Another copy operator: copy the values from a sequential Trilinos wrapper @@ -392,7 +415,7 @@ public: * during configuration time. */ Vector & - operator = (const TrilinosWrappers::Vector &v); + operator= (const TrilinosWrappers::Vector &v); #endif /** @@ -401,7 +424,7 @@ public: * of different sizes makes not much sense anyway. */ template - bool operator == (const Vector &v) const; + bool operator== (const Vector &v) const; /** * Test for inequality. This function assumes that the present vector and @@ -1077,7 +1100,7 @@ namespace internal template inline Vector & -Vector::operator = (const Vector &v) +Vector::operator= (const Vector &v) { dealii::internal::Vector::copy_vector (v, *this); return *this; @@ -1085,11 +1108,27 @@ Vector::operator = (const Vector &v) +#ifdef DEAL_II_WITH_CXX11 +template +inline +Vector & +Vector::operator= (Vector &&v) +{ + swap(v); + // be nice and reset v to zero + v.reinit(0, false); + + return *this; +} +#endif + + + template template inline Vector & -Vector::operator = (const Vector &v) +Vector::operator= (const Vector &v) { internal::Vector::copy_vector (v, *this); return *this; @@ -1333,7 +1372,7 @@ void Vector::save (Archive &ar, const unsigned int) const { // forward to serialization function in the base class. - ar &static_cast(*this); + ar &static_cast(*this); ar &vec_size &max_vec_size ; ar &boost::serialization::make_array(val, max_vec_size); diff --git a/include/deal.II/lac/vector.templates.h b/include/deal.II/lac/vector.templates.h index eef2276507..a4f7c4ac34 100644 --- a/include/deal.II/lac/vector.templates.h +++ b/include/deal.II/lac/vector.templates.h @@ -558,7 +558,6 @@ namespace internal - template Vector::Vector (const Vector &v) : @@ -575,8 +574,20 @@ Vector::Vector (const Vector &v) } -#ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG +#ifdef DEAL_II_WITH_CXX11 +template +Vector::Vector (Vector &&v) +{ + swap(v); + // be nice and reset v to zero + v.reinit(0, false); +} +#endif + + + +#ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG template template Vector::Vector (const Vector &v) @@ -592,11 +603,10 @@ Vector::Vector (const Vector &v) std::copy (v.begin(), v.end(), begin()); } } - #endif -#ifdef DEAL_II_WITH_PETSC +#ifdef DEAL_II_WITH_PETSC template Vector::Vector (const PETScWrappers::Vector &v) @@ -647,6 +657,7 @@ Vector::Vector (const PETScWrappers::MPI::Vector &v) #endif + #ifdef DEAL_II_WITH_TRILINOS template @@ -707,6 +718,7 @@ Vector::Vector (const TrilinosWrappers::Vector &v) #endif + template template void Vector::reinit (const Vector &v, @@ -715,18 +727,6 @@ void Vector::reinit (const Vector &v, reinit (v.size(), fast); } -// Moved to vector.h as an inline function by Luca Heltai on -// 2009/04/12 to prevent strange compiling errors, after making swap -// virtual. -// template -// void -// Vector::swap (Vector &v) -// { -// std::swap (vec_size, v.vec_size); -// std::swap (max_vec_size, v.max_vec_size); -// std::swap (val, v.val); -// } - template @@ -830,7 +830,7 @@ namespace internal template Vector & -Vector::operator = (const Number s) +Vector::operator= (const Number s) { AssertIsFinite(s); if (s != Number()) @@ -852,7 +852,7 @@ Vector::operator = (const Number s) #ifdef DEAL_II_BOOST_BIND_COMPILER_BUG template <> Vector > & -Vector >::operator = (const std::complex s) +Vector >::operator= (const std::complex s) { AssertIsFinite(s); if (s != std::complex()) @@ -1769,7 +1769,7 @@ void Vector::ratio (const Vector &a, template Vector & -Vector::operator = (const BlockVector &v) +Vector::operator= (const BlockVector &v) { if (v.size() != vec_size) reinit (v.size(), true); @@ -1788,7 +1788,7 @@ Vector::operator = (const BlockVector &v) template Vector & -Vector::operator = (const PETScWrappers::Vector &v) +Vector::operator= (const PETScWrappers::Vector &v) { if (v.size() != vec_size) reinit (v.size(), true); @@ -1815,7 +1815,7 @@ Vector::operator = (const PETScWrappers::Vector &v) template Vector & -Vector::operator = (const PETScWrappers::MPI::Vector &v) +Vector::operator= (const PETScWrappers::MPI::Vector &v) { // do this in a two-stage process: // first convert to a sequential petsc @@ -1833,7 +1833,7 @@ Vector::operator = (const PETScWrappers::MPI::Vector &v) template Vector & -Vector::operator = (const TrilinosWrappers::MPI::Vector &v) +Vector::operator= (const TrilinosWrappers::MPI::Vector &v) { // Generate a localized version // of the Trilinos vectors and @@ -1848,7 +1848,7 @@ Vector::operator = (const TrilinosWrappers::MPI::Vector &v) template Vector & -Vector::operator = (const TrilinosWrappers::Vector &v) +Vector::operator= (const TrilinosWrappers::Vector &v) { if (v.size() != vec_size) reinit (v.size(), true); @@ -1871,7 +1871,7 @@ Vector::operator = (const TrilinosWrappers::Vector &v) template template bool -Vector::operator == (const Vector &v) const +Vector::operator== (const Vector &v) const { Assert (vec_size!=0, ExcEmptyObject()); Assert (vec_size == v.size(), ExcDimensionMismatch(vec_size, v.size()));