From a76d6ae30358bb6691913bb2067a7f5d29f8f902 Mon Sep 17 00:00:00 2001 From: Sebastian Proell Date: Mon, 6 Mar 2023 16:52:26 +0100 Subject: [PATCH] make_array_view overload for AlignedVector --- doc/news/changes/minor/20230306Proell | 3 + include/deal.II/base/array_view.h | 71 +++++++++++++++++++++++ tests/base/array_view_19.cc | 83 +++++++++++++++++++++++++++ tests/base/array_view_19.output | 2 + 4 files changed, 159 insertions(+) create mode 100644 doc/news/changes/minor/20230306Proell create mode 100644 tests/base/array_view_19.cc create mode 100644 tests/base/array_view_19.output diff --git a/doc/news/changes/minor/20230306Proell b/doc/news/changes/minor/20230306Proell new file mode 100644 index 0000000000..ce0a5abb3b --- /dev/null +++ b/doc/news/changes/minor/20230306Proell @@ -0,0 +1,3 @@ +New: Provide overloads for `make_array_view` for `AlignedVector`. +
+(Sebastian Proell, 2023/03/06) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 31a5435823..05057a30c9 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -30,6 +30,9 @@ DEAL_II_NAMESPACE_OPEN // Forward declaration +template +class AlignedVector; + template class Table; @@ -1062,6 +1065,74 @@ make_array_view(const std::vector &vector, +/** + * Create a writable view to an entire AlignedVector object. See the + * documentation of the corresponding overload for std::vector for more + * information. + */ +template +inline ArrayView +make_array_view(AlignedVector &vector) +{ + return ArrayView(vector.data(), vector.size()); +} + + + +/** + * Create a read-only view to an entire AlignedVector object. See the + * documentation of the corresponding overload for std::vector for more + * information. + */ +template +inline ArrayView +make_array_view(const AlignedVector &vector) +{ + return ArrayView(vector.data(), vector.size()); +} + + + +/** + * Create a writable view to a part of an AlignedVector object. See the + * documentation of the corresponding overload for std::vector for more + * information. + */ +template +inline ArrayView +make_array_view(AlignedVector &vector, + const std::size_t starting_index, + const std::size_t size_of_view) +{ + Assert(starting_index + size_of_view <= vector.size(), + ExcMessage("The starting index and size of the view you want to " + "create would lead to a view that extends beyond the end " + "of the given vector.")); + return ArrayView(&vector[starting_index], size_of_view); +} + + + +/** + * Create a read-only view to a part of an AlignedVector object. See the + * documentation of the corresponding overload for std::vector for more + * information. + */ +template +inline ArrayView +make_array_view(const AlignedVector &vector, + const std::size_t starting_index, + const std::size_t size_of_view) +{ + Assert(starting_index + size_of_view <= vector.size(), + ExcMessage("The starting index and size of the view you want to " + "create would lead to a view that extends beyond the end " + "of the given vector.")); + return ArrayView(&vector[starting_index], size_of_view); +} + + + /** * Create a view to an entire std::array object. This is equivalent to * initializing an ArrayView object with a pointer to the first element and diff --git a/tests/base/array_view_19.cc b/tests/base/array_view_19.cc new file mode 100644 index 0000000000..d8a115cc94 --- /dev/null +++ b/tests/base/array_view_19.cc @@ -0,0 +1,83 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2015 - 2021 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. +// +// --------------------------------------------------------------------- + + +// test for class ArrayView with AlignedVector, using make_array_view + +#include +#include + +#include "../tests.h" + + +void +test() +{ + AlignedVector v(10, 1.0); + + { + ArrayView a(&v[4], 3); // writable view + a[2] = 42; + + Assert(a[2] == 42, ExcInternalError()); + Assert(v[6] == 42, ExcInternalError()); + } + + { + ArrayView a = make_array_view(v); // writable view + a[1] = 43; + + Assert(v[1] == 43, ExcInternalError()); + Assert(a[6] == 42, ExcInternalError()); + } + + { + ArrayView a2(&v[4], 3); // readable view + Assert(a2[2] == 42, ExcInternalError()); + } + + { + ArrayView a2 = make_array_view(v); // readable view + Assert(a2[1] == 43, ExcInternalError()); + Assert(a2[6] == 42, ExcInternalError()); + } + + { + // writable view on subrange + ArrayView a2 = make_array_view(v, 1, 7); + a2[0] = 44; + Assert(a2[0] == 44, ExcInternalError()); + Assert(a2[5] == 42, ExcInternalError()); + } + + { + // readable view on subrange + ArrayView a2 = make_array_view(v, 1, 7); + Assert(a2[0] == 44, ExcInternalError()); + Assert(a2[5] == 42, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + + +int +main() +{ + initlog(); + + test(); +} diff --git a/tests/base/array_view_19.output b/tests/base/array_view_19.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/array_view_19.output @@ -0,0 +1,2 @@ + +DEAL::OK -- 2.39.5