From b5660de2111112fc46033e6d3e4de5ebf062b473 Mon Sep 17 00:00:00 2001 From: kronbichler Date: Sun, 26 May 2013 17:03:17 +0000 Subject: [PATCH] Implement better swap function. git-svn-id: https://svn.dealii.org/trunk@29616 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/include/deal.II/base/table.h | 50 +++++++++++++++++++++++++--- deal.II/source/fe/fe_q_base.cc | 9 +++-- deal.II/source/fe/fe_system.cc | 8 ++--- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/deal.II/include/deal.II/base/table.h b/deal.II/include/deal.II/base/table.h index 83fa744446..32a88b0146 100644 --- a/deal.II/include/deal.II/base/table.h +++ b/deal.II/include/deal.II/base/table.h @@ -634,15 +634,27 @@ public: operator () (const TableIndices &indices) const; /** - * Determine an estimate for the - * memory consumption (in bytes) - * of this object. + * Swap the contents of this table and the other table @p v. One could do + * this operation with a temporary variable and copying over the data + * elements, but this function is significantly more efficient since it only + * swaps the pointers to the data of the two vectors and therefore does not + * need to allocate temporary storage and move data around. + * + * This function is analog to the the @p swap function of all C++ standard + * containers. Also, there is a global function swap(u,v) that + * simply calls u.swap(v), again in analogy to standard functions. + */ + void swap (TableBase &v); + + /** + * Determine an estimate for the memory consumption (in bytes) of this + * object. */ std::size_t memory_consumption () const; /** - * Write or read the data of this object to or - * from a stream for the purpose of serialization. + * Write or read the data of this object to or from a stream for the purpose + * of serialization. */ template void serialize (Archive &ar, const unsigned int version); @@ -2077,6 +2089,17 @@ TableBase::fill (const T2 *entries) +template +inline +void +TableBase::swap (TableBase &v) +{ + values.swap(v.values); + std::swap (table_size, v.table_size); +} + + + template inline std::size_t @@ -3168,6 +3191,23 @@ Table<7,T>::operator () (const TableIndices<7> &indices) #endif // DOXYGEN + + + +/** + * Global function @p swap which overloads the default implementation + * of the C++ standard library which uses a temporary object. The + * function simply exchanges the data of the two tables. + * + * @author Martin Kronbichler, 2013 + */ +template +inline +void swap (TableBase &u, TableBase &v) +{ + u.swap (v); +} + DEAL_II_NAMESPACE_CLOSE #endif diff --git a/deal.II/source/fe/fe_q_base.cc b/deal.II/source/fe/fe_q_base.cc index 43d46d3dc9..a8a966b7b5 100644 --- a/deal.II/source/fe/fe_q_base.cc +++ b/deal.II/source/fe/fe_q_base.cc @@ -1173,8 +1173,8 @@ FE_Q_Base #endif // swap matrices - std::swap(const_cast &> - (this->prolongation[refinement_case-1][child]), prolongate); + prolongate.swap(const_cast &> + (this->prolongation[refinement_case-1][child])); } // finally return the matrix @@ -1305,9 +1305,8 @@ FE_Q_Base } // swap matrices - std::swap(const_cast &> - (this->restriction[refinement_case-1][child]), restriction); - + restriction.swap(const_cast &> + (this->restriction[refinement_case-1][child])); } return this->restriction[refinement_case-1][child]; diff --git a/deal.II/source/fe/fe_system.cc b/deal.II/source/fe/fe_system.cc index 5d12365cc9..4a1966ff07 100644 --- a/deal.II/source/fe/fe_system.cc +++ b/deal.II/source/fe/fe_system.cc @@ -669,8 +669,8 @@ FESystem restriction(i,j) = (*base_matrices[base])(base_index_i,base_index_j); } - std::swap(restriction, const_cast &> - (this->restriction[refinement_case-1][child])); + restriction.swap(const_cast &> + (this->restriction[refinement_case-1][child])); } } @@ -734,8 +734,8 @@ FESystem base_index_j = this->system_to_base_table[j].second; prolongate(i,j) = (*base_matrices[base])(base_index_i,base_index_j); } - std::swap(prolongate, const_cast &> - (this->prolongation[refinement_case-1][child])); + prolongate.swap(const_cast &> + (this->prolongation[refinement_case-1][child])); } } -- 2.39.5