]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Implement better swap function.
authorkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 26 May 2013 17:03:17 +0000 (17:03 +0000)
committerkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 26 May 2013 17:03:17 +0000 (17:03 +0000)
git-svn-id: https://svn.dealii.org/trunk@29616 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/base/table.h
deal.II/source/fe/fe_q_base.cc
deal.II/source/fe/fe_system.cc

index 83fa7444469ee3c4bbd2e5896fb29e8c61c48db2..32a88b0146f7bbc9f1c8ff874d2ecec4d3bfa4ba 100644 (file)
@@ -634,15 +634,27 @@ public:
   operator () (const TableIndices<N> &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 <tt>swap(u,v)</tt> that
+   * simply calls <tt>u.swap(v)</tt>, again in analogy to standard functions.
+   */
+  void swap (TableBase<N,T> &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 <class Archive>
   void serialize (Archive &ar, const unsigned int version);
@@ -2077,6 +2089,17 @@ TableBase<N,T>::fill (const T2 *entries)
 
 
 
+template <int N, typename T>
+inline
+void
+TableBase<N,T>::swap (TableBase<N,T> &v)
+{
+  values.swap(v.values);
+  std::swap (table_size, v.table_size);
+}
+
+
+
 template <int N, typename T>
 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 <int N, typename T>
+inline
+void swap (TableBase<N,T> &u, TableBase<N,T> &v)
+{
+  u.swap (v);
+}
+
 DEAL_II_NAMESPACE_CLOSE
 
 #endif
index 43d46d3dc93da1a4e2ea1858bb82447e393af973..a8a966b7b5ebabd46e99c1f9a51cbf30b261706f 100644 (file)
@@ -1173,8 +1173,8 @@ FE_Q_Base<POLY,dim,spacedim>
 #endif
 
       // swap matrices
-      std::swap(const_cast<FullMatrix<double> &>
-                (this->prolongation[refinement_case-1][child]), prolongate);
+      prolongate.swap(const_cast<FullMatrix<double> &>
+                      (this->prolongation[refinement_case-1][child]));
     }
 
   // finally return the matrix
@@ -1305,9 +1305,8 @@ FE_Q_Base<POLY,dim,spacedim>
         }
 
       // swap matrices
-      std::swap(const_cast<FullMatrix<double> &>
-                (this->restriction[refinement_case-1][child]), restriction);
-
+      restriction.swap(const_cast<FullMatrix<double> &>
+                       (this->restriction[refinement_case-1][child]));
     }
 
   return this->restriction[refinement_case-1][child];
index 5d12365cc95c98bb80efbccd81ca65cf442971f3..4a1966ff07abdf2bd69c7b4937c3995a6843142c 100644 (file)
@@ -669,8 +669,8 @@ FESystem<dim,spacedim>
                 restriction(i,j) = (*base_matrices[base])(base_index_i,base_index_j);
               }
 
-          std::swap(restriction, const_cast<FullMatrix<double> &>
-                    (this->restriction[refinement_case-1][child]));
+          restriction.swap(const_cast<FullMatrix<double> &>
+                           (this->restriction[refinement_case-1][child]));
         }
     }
 
@@ -734,8 +734,8 @@ FESystem<dim,spacedim>
                   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<FullMatrix<double> &>
-                    (this->prolongation[refinement_case-1][child]));
+          prolongate.swap(const_cast<FullMatrix<double> &>
+                          (this->prolongation[refinement_case-1][child]));
         }
     }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.