From 35917e662efe131c3c84c9e7bea23e7b4379edea Mon Sep 17 00:00:00 2001 From: danshapero Date: Tue, 9 Aug 2016 11:00:14 -0700 Subject: [PATCH] Added move constructor to Triangulation --- doc/news/changes.h | 5 + include/deal.II/grid/tria.h | 10 ++ source/grid/tria.cc | 60 ++++++++- tests/grid/move_constructor.cc | 122 ++++++++++++++++++ .../move_constructor.with_cxx11=on.output | 30 +++++ 5 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 tests/grid/move_constructor.cc create mode 100644 tests/grid/move_constructor.with_cxx11=on.output diff --git a/doc/news/changes.h b/doc/news/changes.h index e5e4c546f6..e892bfc8d2 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -558,6 +558,11 @@ SparsityPattern. (Wolfgang Bangerth, 2016/07/08) +
  • New: A move constructor has been added to Triangulation. +
    + (Daniel Shapero, 2016/07/07) +
  • +
  • Fixed: The function DoFTools::dof_couplings_from_component_couplings for hp::FECollection arguments was compiled but not exported from the object file. This is now fixed. diff --git a/include/deal.II/grid/tria.h b/include/deal.II/grid/tria.h index c0991e1eb7..64f910d8c2 100644 --- a/include/deal.II/grid/tria.h +++ b/include/deal.II/grid/tria.h @@ -1516,6 +1516,16 @@ public: */ Triangulation (const Triangulation &t); +#ifdef DEAL_II_WITH_CXX11 + /** + * Move constructor. + * + * Create a new triangulation by stealing the internal data of another + * triangulation. + */ + Triangulation (Triangulation &&tria); +#endif + /** * Delete the object and all levels of the hierarchy. */ diff --git a/source/grid/tria.cc b/source/grid/tria.cc index 1f3629c7e0..9f7a9c3b33 100644 --- a/source/grid/tria.cc +++ b/source/grid/tria.cc @@ -8996,13 +8996,56 @@ Triangulation (const Triangulation &other) +#ifdef DEAL_II_WITH_CXX11 +template +Triangulation:: +Triangulation (Triangulation &&tria) + : + Subscriptor(tria), + smooth_grid(tria.smooth_grid), + periodic_face_pairs_level_0(std::move(tria.periodic_face_pairs_level_0)), + periodic_face_map(std::move(tria.periodic_face_map)), + levels(std::move(tria.levels)), + faces(std::move(tria.faces)), + vertices(std::move(tria.vertices)), + vertices_used(std::move(tria.vertices_used)), + manifold(std::move(tria.manifold)), + anisotropic_refinement(tria.anisotropic_refinement), + check_for_distorted_cells(tria.check_for_distorted_cells), + number_cache(tria.number_cache), + vertex_to_boundary_id_map_1d(tria.vertex_to_boundary_id_map_1d), + vertex_to_manifold_id_map_1d(tria.vertex_to_manifold_id_map_1d) +{ + for (unsigned int i=0; i(); + + tria.vertex_to_boundary_id_map_1d = nullptr; + tria.vertex_to_manifold_id_map_1d = nullptr; +} +#endif + + + template Triangulation::~Triangulation () { for (unsigned int i=0; i::~Triangulation () || (vertex_to_boundary_id_map_1d == 0), ExcInternalError()); - delete vertex_to_boundary_id_map_1d; + if (vertex_to_boundary_id_map_1d) + { + delete vertex_to_boundary_id_map_1d; + vertex_to_boundary_id_map_1d = 0; + } + // the vertex_to_manifold_id_map_1d field // should be unused except in 1d Assert ((dim == 1) || (vertex_to_manifold_id_map_1d == 0), ExcInternalError()); - delete vertex_to_manifold_id_map_1d; + if (vertex_to_manifold_id_map_1d) + { + delete vertex_to_manifold_id_map_1d; + vertex_to_manifold_id_map_1d = 0; + } } diff --git a/tests/grid/move_constructor.cc b/tests/grid/move_constructor.cc new file mode 100644 index 0000000000..6ee0c49ace --- /dev/null +++ b/tests/grid/move_constructor.cc @@ -0,0 +1,122 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2003 - 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 +#include +#include +#include +#include + +#include + + +template +void print_tria_info(const Triangulation &tria) +{ + deallog << (tria.n_active_cells() != 0) << ", " + << (tria.n_active_hexs() != 0) << ", " + << (tria.n_active_quads() != 0) << ", " + << (tria.n_active_lines() != 0) << ", " + << (tria.n_levels() != 0) << ", " + << (tria.n_vertices() != 0) << ", " + << (tria.get_periodic_face_map().size() != 0) << ", " + << (&tria.get_manifold(0) + == (const Manifold *)&Triangulation::straight_boundary) + << std::endl; +} + + +template +void test_hyper_cube() +{ + deallog << "Dimension: " << dim << std::endl; + + Triangulation tria; + GridGenerator::hyper_cube(tria, -1.0, 1.0); + tria.refine_global(2); + + print_tria_info(tria); + + Triangulation new_tria = std::move(tria); + print_tria_info(new_tria); + print_tria_info(tria); +} + + +template +void test_hyper_shell() +{ + deallog << "Dimension: " << dim << std::endl; + + Triangulation tria; + GridGenerator::hyper_ball(tria); + + const SphericalManifold boundary; + tria.set_all_manifold_ids_on_boundary(0); + tria.set_manifold(0, boundary); + + tria.refine_global(3); + print_tria_info(tria); + + Triangulation new_tria = std::move(tria); + print_tria_info(new_tria); + print_tria_info(tria); +} + + +template +void test_periodic_cube() +{ + deallog << "Dimension: " << dim << std::endl; + + Triangulation tria; + GridGenerator::hyper_cube(tria, -1.0, 1.0, true); + + using periodic_face_pair = + GridTools::PeriodicFacePair::cell_iterator>; + std::vector periodicity_vector; + GridTools::collect_periodic_faces(tria, 0, 1, 0, periodicity_vector); + tria.add_periodicity(periodicity_vector); + tria.refine_global(3); + + print_tria_info(tria); + + Triangulation new_tria = std::move(tria); + print_tria_info(new_tria); + print_tria_info(tria); +} + + +int main() +{ + initlog(); + + deallog << "Hyper-cube:" << std::endl; + test_hyper_cube<2>(); + test_hyper_cube<3>(); + + deallog << std::endl << "Hyper-shell:" << std::endl; + test_hyper_shell<2>(); + test_hyper_shell<3>(); + + deallog << std::endl << "Periodic hyper-cube:" << std::endl; + test_periodic_cube<2>(); + test_periodic_cube<3>(); + + return 0; +} diff --git a/tests/grid/move_constructor.with_cxx11=on.output b/tests/grid/move_constructor.with_cxx11=on.output new file mode 100644 index 0000000000..76860e1bb0 --- /dev/null +++ b/tests/grid/move_constructor.with_cxx11=on.output @@ -0,0 +1,30 @@ + +DEAL::Hyper-cube: +DEAL::Dimension: 2 +DEAL::1, 0, 1, 1, 1, 1, 0, 1 +DEAL::1, 0, 1, 1, 1, 1, 0, 1 +DEAL::0, 0, 0, 0, 0, 0, 0, 1 +DEAL::Dimension: 3 +DEAL::1, 1, 1, 1, 1, 1, 0, 1 +DEAL::1, 1, 1, 1, 1, 1, 0, 1 +DEAL::0, 0, 0, 0, 0, 0, 0, 1 +DEAL:: +DEAL::Hyper-shell: +DEAL::Dimension: 2 +DEAL::1, 0, 1, 1, 1, 1, 0, 0 +DEAL::1, 0, 1, 1, 1, 1, 0, 0 +DEAL::0, 0, 0, 0, 0, 0, 0, 1 +DEAL::Dimension: 3 +DEAL::1, 1, 1, 1, 1, 1, 0, 0 +DEAL::1, 1, 1, 1, 1, 1, 0, 0 +DEAL::0, 0, 0, 0, 0, 0, 0, 1 +DEAL:: +DEAL::Periodic hyper-cube: +DEAL::Dimension: 2 +DEAL::1, 0, 1, 1, 1, 1, 1, 1 +DEAL::1, 0, 1, 1, 1, 1, 1, 1 +DEAL::0, 0, 0, 0, 0, 0, 0, 1 +DEAL::Dimension: 3 +DEAL::1, 1, 1, 1, 1, 1, 1, 1 +DEAL::1, 1, 1, 1, 1, 1, 1, 1 +DEAL::0, 0, 0, 0, 0, 0, 0, 1 -- 2.39.5