From: danshapero Date: Wed, 18 May 2016 00:32:41 +0000 (-0700) Subject: Added move constructor to Table X-Git-Tag: v8.5.0-rc1~1032^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2610%2Fhead;p=dealii.git Added move constructor to Table --- diff --git a/include/deal.II/base/table.h b/include/deal.II/base/table.h index f7ea7c3a44..6ae774fa39 100644 --- a/include/deal.II/base/table.h +++ b/include/deal.II/base/table.h @@ -444,6 +444,15 @@ public: template TableBase (const TableBase &src); +#ifdef DEAL_II_WITH_CXX11 + /** + * Move constructor. Transfers the contents of another Table. + * + * @note This constructor is only available if deal.II is built with C++11. + */ + TableBase (TableBase &&src); +#endif + /** * Destructor. Free allocated memory. */ @@ -469,6 +478,16 @@ public: template TableBase &operator = (const TableBase &src); +#ifdef DEAL_II_WITH_CXX11 + /** + * Move assignment operator. Transfer all elements of src into the + * table. + * + * @note This operator is only available if deal.II is built with C++11. + */ + TableBase &operator = (TableBase &&src); +#endif + /** * Test for equality of two tables. */ @@ -1648,6 +1667,22 @@ TableBase::TableBase (const TableBase &src) +#ifdef DEAL_II_WITH_CXX11 + +template +TableBase::TableBase (TableBase &&src) + : + Subscriptor (std::move(src)), + values (std::move(src.values)), + table_size (src.table_size) +{ + src.table_size = TableIndices(); +} + +#endif + + + template template inline @@ -1845,6 +1880,26 @@ TableBase::operator = (const TableBase &m) } + +#ifdef DEAL_II_WITH_CXX11 + +template +inline +TableBase & +TableBase::operator = (TableBase &&m) +{ + static_cast(*this) = std::move(m); + values = std::move(m.values); + table_size = m.table_size; + m.table_size = TableIndices(); + + return *this; +} + +#endif + + + template inline bool diff --git a/tests/base/table_move.cc b/tests/base/table_move.cc new file mode 100644 index 0000000000..13fec59609 --- /dev/null +++ b/tests/base/table_move.cc @@ -0,0 +1,88 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2012 - 2016 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 +#include + +#include + +int main() +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.threshold_double(1.0e-10); + + const unsigned int entries[] = {1, 2, 3, 4, 5, 6}; + Table<2, unsigned int> t (2, 3); + t.fill (entries, true); + + deallog << "Sizes: " << t.size(0) << ", " << t.size(1) << std::endl; + deallog << "Contents:" << std::endl; + for (unsigned int i = 0; i < t.size(0); ++i) + { + for (unsigned int j = 0; j < t.size(1); ++j) + deallog << t[i][j] << ' '; + deallog << std::endl; + } + + Table<2, unsigned int> s = std::move(t); + + deallog << "Sizes of moved-to table: " + << s.size(0) << ", " << s.size(1) << std::endl; + deallog << "Sizes of moved-from table: " + << t.size(0) << ", " << t.size(1) << std::endl; + + deallog << "Contents of moved-to table:" << std::endl; + for (unsigned int i = 0; i < s.size(0); ++i) + { + for (unsigned int j = 0; j < s.size(1); ++j) + deallog << s[i][j] << ' '; + deallog << std::endl; + } + + const TableIndices<2> new_size(4, 2); + s.reinit (new_size); + const unsigned int new_entries[] = {1, 2, 3, 4, 5, 6, 7, 8}; + s.fill (new_entries, true); + + deallog << "Sizes of new table: " + << s.size(0) << ", " << s.size(1) << std::endl; + deallog << "Contents of new table:" << std::endl; + for (unsigned int i = 0; i < s.size(0); ++i) + { + for (unsigned int j = 0; j < s.size(1); ++j) + deallog << s[i][j] << ' '; + deallog << std::endl; + } + + t = std::move(s); + + deallog << "Sizes of moved-to table: " + << t.size(0) << ", " << t.size(1) << std::endl; + deallog << "Sizes of moved-from table: " + << s.size(0) << ", " << s.size(1) << std::endl; + + deallog << "Contents of moved-to table:" << std::endl; + for (unsigned int i = 0; i < t.size(0); ++i) + { + for (unsigned int j = 0; j < t.size(1); ++j) + deallog << t[i][j] << ' '; + deallog << std::endl; + } + + return 0; +} diff --git a/tests/base/table_move.with_cxx11=on.output b/tests/base/table_move.with_cxx11=on.output new file mode 100644 index 0000000000..d07355565f --- /dev/null +++ b/tests/base/table_move.with_cxx11=on.output @@ -0,0 +1,23 @@ + +DEAL::Sizes: 2, 3 +DEAL::Contents: +DEAL::1 2 3 +DEAL::4 5 6 +DEAL::Sizes of moved-to table: 2, 3 +DEAL::Sizes of moved-from table: 0, 0 +DEAL::Contents of moved-to table: +DEAL::1 2 3 +DEAL::4 5 6 +DEAL::Sizes of new table: 4, 2 +DEAL::Contents of new table: +DEAL::1 2 +DEAL::3 4 +DEAL::5 6 +DEAL::7 8 +DEAL::Sizes of moved-to table: 4, 2 +DEAL::Sizes of moved-from table: 0, 0 +DEAL::Contents of moved-to table: +DEAL::1 2 +DEAL::3 4 +DEAL::5 6 +DEAL::7 8