]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Bugfix: Do not reinitialize in move operator
authorMatthias Maier <tamiko@43-1.org>
Mon, 4 May 2015 11:04:40 +0000 (13:04 +0200)
committerMatthias Maier <tamiko@43-1.org>
Mon, 4 May 2015 11:53:23 +0000 (13:53 +0200)
include/deal.II/lac/block_vector.h
tests/lac/block_vector_move_operations.cc [new file with mode: 0644]
tests/lac/block_vector_move_operations.with_cxx11=on.output [new file with mode: 0644]
tests/lac/vector_move_operations.cc
tests/lac/vector_move_operations.with_cxx11=on.output
tests/trilinos/vector_move_operations.cc
tests/trilinos/vector_move_operations.with_cxx11=on.output

index 95e7bbf2d3b3dbf9ea11046c13b07cfe30862ecb..a1a9eaac36c89b5380db4f63d00cfe5e7c9eef52 100644 (file)
@@ -204,8 +204,7 @@ public:
 #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.
+   * @p v by efficiently swapping the internal data structures.
    *
    * @note This operator is only available if deal.II is configured with
    * C++11 support.
@@ -430,9 +429,6 @@ BlockVector<Number>::operator= (BlockVector<Number> &&v)
 {
   swap(v);
 
-  // be nice and reset v to zero
-  v.reinit(0, 0, false);
-
   return *this;
 }
 #endif
diff --git a/tests/lac/block_vector_move_operations.cc b/tests/lac/block_vector_move_operations.cc
new file mode 100644 (file)
index 0000000..68a8fa1
--- /dev/null
@@ -0,0 +1,63 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2012 - 2015 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/lac/block_vector.h>
+
+#define PRINTME(name, var) \
+  deallog << "Block vector: " name << ":" << std::endl; \
+  for (unsigned int i = 0; i < var.n_blocks(); ++i) \
+    deallog << "[block " << i << " ]  " << var.block(i);
+
+int main()
+{
+  initlog();
+
+  {
+    BlockVector<double> temp(5, 2);
+    BlockVector<double> w(std::move(temp));
+    PRINTME("move constructor", w);
+  }
+
+  deallog << std::endl;
+
+  {
+    BlockVector<double> u(5, 2);
+    for (unsigned int i = 0; i < 5; ++i)
+      for (unsigned int j = 0; j < 2; ++j)
+        u.block(i)[j] = (double)(10 * i + j);
+
+    PRINTME("BlockVector", u);
+
+    BlockVector<double> v;
+    v = u;
+    PRINTME("copy assignemnt", v);
+    PRINTME("old object", u);
+
+    v = 0.;
+    v = std::move(u);
+    PRINTME("move assignemnt", v);
+    deallog << "old object size: " << u.n_blocks() << std::endl;
+
+    // and swap again with different sizes
+    BlockVector<double> w;
+    w = std::move(v);
+    PRINTME("move assignemnt", w);
+    deallog << "old object size: " << v.n_blocks() << std::endl;
+  }
+}
+
+
diff --git a/tests/lac/block_vector_move_operations.with_cxx11=on.output b/tests/lac/block_vector_move_operations.with_cxx11=on.output
new file mode 100644 (file)
index 0000000..e69de29
index 9fbc51378a00fd4db66b037efee3a55d1aa246d9..951c5003c4f62ce2faf5c4a9a50cc0863620747a 100644 (file)
 
 #include "../tests.h"
 #include <deal.II/lac/vector.h>
-#include <deal.II/lac/block_vector.h>
-
-#define PRINTME(name, var) \
-  deallog << "Block vector: " name << ":" << std::endl; \
-  for (unsigned int i = 0; i < var.n_blocks(); ++i) \
-    deallog << "[block " << i << " ]  " << var.block(i);
 
 int main()
 {
   initlog();
 
   {
-    Vector<double> w(Vector<double>(10));
+    Vector<double> temp(10);
+    Vector<double> w(std::move(temp));
     deallog << "move constructor:  " << w;
   }
 
@@ -55,38 +50,5 @@ int main()
     deallog << "move assignment: " << u;
     deallog << "old object size: " << v.size() << std::endl;
   }
-
-  deallog << std::endl;
-
-  {
-    BlockVector<double> w(BlockVector<double>(5, 2));
-    PRINTME("move constructor", w);
-  }
-
-  deallog << std::endl;
-
-  {
-    BlockVector<double> u(5, 2);
-    for (unsigned int i = 0; i < 5; ++i)
-      for (unsigned int j = 0; j < 2; ++j)
-        u.block(i)[j] = (double)(10 * i + j);
-
-    PRINTME("BlockVector", u);
-
-    BlockVector<double> v;
-    v = u;
-    PRINTME("copy assignemnt", v);
-    PRINTME("old object", u);
-
-    v = std::move(u);
-    PRINTME("move assignemnt", v);
-    deallog << "old object size: " << u.n_blocks() << std::endl;
-
-    // and swap again with different sizes
-    u = std::move(v);
-    PRINTME("move assignemnt", u);
-    deallog << "old object size: " << v.n_blocks() << std::endl;
-  }
 }
 
-
index 29f001490dd1d6247b548140af031c241c4d9022..25f8614359f9b58d739ec2c0e2a42be56d8a2196 100644 (file)
@@ -8,43 +8,3 @@ DEAL::move assignment: 1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8
 DEAL::old object size: 0
 DEAL::move assignment: 1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 10.0000 
 DEAL::old object size: 0
-DEAL::
-DEAL::Block vector: move constructor:
-DEAL::[block 0 ]  0.00000 0.00000 
-DEAL::[block 1 ]  0.00000 0.00000 
-DEAL::[block 2 ]  0.00000 0.00000 
-DEAL::[block 3 ]  0.00000 0.00000 
-DEAL::[block 4 ]  0.00000 0.00000 
-DEAL::
-DEAL::Block vector: BlockVector:
-DEAL::[block 0 ]  0.00000 1.00000 
-DEAL::[block 1 ]  10.0000 11.0000 
-DEAL::[block 2 ]  20.0000 21.0000 
-DEAL::[block 3 ]  30.0000 31.0000 
-DEAL::[block 4 ]  40.0000 41.0000 
-DEAL::Block vector: copy assignemnt:
-DEAL::[block 0 ]  0.00000 1.00000 
-DEAL::[block 1 ]  10.0000 11.0000 
-DEAL::[block 2 ]  20.0000 21.0000 
-DEAL::[block 3 ]  30.0000 31.0000 
-DEAL::[block 4 ]  40.0000 41.0000 
-DEAL::Block vector: old object:
-DEAL::[block 0 ]  0.00000 1.00000 
-DEAL::[block 1 ]  10.0000 11.0000 
-DEAL::[block 2 ]  20.0000 21.0000 
-DEAL::[block 3 ]  30.0000 31.0000 
-DEAL::[block 4 ]  40.0000 41.0000 
-DEAL::Block vector: move assignemnt:
-DEAL::[block 0 ]  0.00000 1.00000 
-DEAL::[block 1 ]  10.0000 11.0000 
-DEAL::[block 2 ]  20.0000 21.0000 
-DEAL::[block 3 ]  30.0000 31.0000 
-DEAL::[block 4 ]  40.0000 41.0000 
-DEAL::old object size: 0
-DEAL::Block vector: move assignemnt:
-DEAL::[block 0 ]  0.00000 1.00000 
-DEAL::[block 1 ]  10.0000 11.0000 
-DEAL::[block 2 ]  20.0000 21.0000 
-DEAL::[block 3 ]  30.0000 31.0000 
-DEAL::[block 4 ]  40.0000 41.0000 
-DEAL::old object size: 0
index 1558c2d1c5b8084763284402a9e63da825610336..97e495da8613907b37a8e7169bab5035c06285f8 100644 (file)
@@ -45,11 +45,14 @@ int main (int argc, char **argv)
     IndexSet local_owned(10);
     local_owned.add_range (0,10);
 
-    TrilinosWrappers::MPI::Vector u(local_owned, MPI_COMM_WORLD);
-    for (unsigned int i = 0; i < u.size(); ++i)
-      u[i] = (double)(i+1);
+    TrilinosWrappers::MPI::Vector temp(local_owned, MPI_COMM_WORLD);
+    for (unsigned int i = 0; i < temp.size(); ++i)
+      temp[i] = (double)(i+1);
 
-    PRINTME("Vector", u);
+    PRINTME("Vector", temp);
+
+    TrilinosWrappers::MPI::Vector u(std::move(temp));
+    PRINTME("move constructor", u);
 
     TrilinosWrappers::MPI::Vector v;
     v = u;
@@ -72,12 +75,15 @@ int main (int argc, char **argv)
         index.add_range(0,2);
       }
 
-    TrilinosWrappers::MPI::BlockVector u(local_owned, MPI_COMM_WORLD);
+    TrilinosWrappers::MPI::BlockVector temp(local_owned, MPI_COMM_WORLD);
     for (unsigned int i = 0; i < 5; ++i)
       for (unsigned int j = 0; j < 2; ++j)
-        u.block(i)[j] = (double)(10 * i + j);
+        temp.block(i)[j] = (double)(10 * i + j);
+
+    PRINTBLOCK("BlockVector", temp);
 
-    PRINTBLOCK("BlockVector", u);
+    TrilinosWrappers::MPI::BlockVector u(std::move(temp));
+    PRINTME("move constructor", u);
 
     TrilinosWrappers::MPI::BlockVector v;
     v = u;
index ab9bb97986a31d1b53a48bf92ae0d10f5283e97b..5cee7746072ffa3f497c83136620e5391d9b4f3c 100644 (file)
@@ -1,6 +1,8 @@
 
 DEAL::Vector: Vector:
 DEAL::1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 10.0000 
+DEAL::Vector: move constructor:
+DEAL::1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 10.0000 
 DEAL::Vector: copy assignemnt:
 DEAL::1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 10.0000 
 DEAL::Vector: old object:
@@ -15,6 +17,12 @@ DEAL::[block 1 ]  10.0000 11.0000
 DEAL::[block 2 ]  20.0000 21.0000 
 DEAL::[block 3 ]  30.0000 31.0000 
 DEAL::[block 4 ]  40.0000 41.0000 
+DEAL::Block vector: move constructor:
+DEAL::[block 0 ]  0.00000 1.00000 
+DEAL::[block 1 ]  10.0000 11.0000 
+DEAL::[block 2 ]  20.0000 21.0000 
+DEAL::[block 3 ]  30.0000 31.0000 
+DEAL::[block 4 ]  40.0000 41.0000 
 DEAL::Block vector: copy assignemnt:
 DEAL::[block 0 ]  0.00000 1.00000 
 DEAL::[block 1 ]  10.0000 11.0000 

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.