From f243d49c467f46cc60fe3575b2ff26916e4e2cc7 Mon Sep 17 00:00:00 2001 From: Reza Rastak Date: Thu, 23 May 2019 00:17:15 -0700 Subject: [PATCH] conversion operator is written for table accessor tests created for packing and unpacking a matrix changelog added --- doc/news/changes/minor/20190524RezaRastak | 3 ++ include/deal.II/base/table.h | 29 +++++++++++++++ tests/lac/matrices_pack_unpack.cc | 44 +++++++++++++++++++++++ tests/lac/matrices_pack_unpack.output | 11 ++++++ 4 files changed, 87 insertions(+) create mode 100644 doc/news/changes/minor/20190524RezaRastak create mode 100644 tests/lac/matrices_pack_unpack.cc create mode 100644 tests/lac/matrices_pack_unpack.output diff --git a/doc/news/changes/minor/20190524RezaRastak b/doc/news/changes/minor/20190524RezaRastak new file mode 100644 index 0000000000..e3ab0d5e97 --- /dev/null +++ b/doc/news/changes/minor/20190524RezaRastak @@ -0,0 +1,3 @@ +Fix: FullMatrix can now be packed into std::vector and unpacked from it using std::copy and the begin() and end() iterators +
+(Reza Rastak, 2019/05/24) diff --git a/include/deal.II/base/table.h b/include/deal.II/base/table.h index f2a0d001d6..6c5fcfea33 100644 --- a/include/deal.II/base/table.h +++ b/include/deal.II/base/table.h @@ -918,6 +918,11 @@ namespace MatrixTableIterators const value_type & value() const; + /** + * Conversion operator that returns a constant reference to the element + */ + operator const value_type &() const; + /** * Return the row of the current entry. */ @@ -1042,6 +1047,11 @@ namespace MatrixTableIterators */ value_type & value() const; + + /** + * Conversion operator that returns a reference to the element + */ + operator value_type &(); }; /** @@ -2923,6 +2933,16 @@ namespace MatrixTableIterators + template + inline AccessorBase:: + operator const value_type &() const + { + assert_valid_linear_index(); + return this->container->values[linear_index]; + } + + + template inline typename AccessorBase::size_type AccessorBase::row() const @@ -3011,6 +3031,15 @@ namespace MatrixTableIterators + template + inline Accessor::operator value_type &() + { + this->assert_valid_linear_index(); + return this->container->values[this->linear_index]; + } + + + template inline Iterator::Iterator( const Accessor &a) diff --git a/tests/lac/matrices_pack_unpack.cc b/tests/lac/matrices_pack_unpack.cc new file mode 100644 index 0000000000..a0a7e81af6 --- /dev/null +++ b/tests/lac/matrices_pack_unpack.cc @@ -0,0 +1,44 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2005 - 2018 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.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + +// Copying from a matrix into std::vector and vice versa using iterators + +#include + +#include "../tests.h" + +int +main() +{ + initlog(); + + dealii::FullMatrix A(3, 4); + std::iota(A.begin(), A.end(), 1.0); + + deallog << "A =" << std::endl; + A.print(deallog); + + std::vector v(A.n_elements()); + std::copy(A.begin(), A.end(), v.begin()); + deallog << "v = " << std::endl; + deallog << v << std::endl; + + dealii::FullMatrix B(A.m(), A.n()); + std::copy(v.cbegin(), v.cend(), B.begin()); + deallog << "B =" << std::endl; + A.print(deallog); + + return 0; +} \ No newline at end of file diff --git a/tests/lac/matrices_pack_unpack.output b/tests/lac/matrices_pack_unpack.output new file mode 100644 index 0000000000..2c8d2476f8 --- /dev/null +++ b/tests/lac/matrices_pack_unpack.output @@ -0,0 +1,11 @@ + +DEAL::A = +DEAL::1.0 2.0 3.0 4.0 +DEAL::5.0 6.0 7.0 8.0 +DEAL::9.0 10. 11. 12. +DEAL::v = +DEAL::1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 10.0000 11.0000 12.0000 +DEAL::B = +DEAL::1.0 2.0 3.0 4.0 +DEAL::5.0 6.0 7.0 8.0 +DEAL::9.0 10. 11. 12. -- 2.39.5