From: Denis Davydov Date: Tue, 2 Apr 2019 19:56:32 +0000 (+0200) Subject: add default constructor for ArrayView and a reinit() method X-Git-Tag: v9.1.0-rc1~224^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c5217c15fa235e5d2ecabee4f6c5a35959f1e704;p=dealii.git add default constructor for ArrayView and a reinit() method --- diff --git a/doc/news/changes/minor/20190402DenisDavydov b/doc/news/changes/minor/20190402DenisDavydov new file mode 100644 index 0000000000..a2d7537d67 --- /dev/null +++ b/doc/news/changes/minor/20190402DenisDavydov @@ -0,0 +1,3 @@ +New: Add ArraView::ArrayView() and ArrayView::reinit(value_type *, const std::size_t). +
+(Denis Davydov, 2019/04/02) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 130e693f4b..e51f984b63 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -93,6 +93,11 @@ public: */ using const_iterator = const ElementType *; + /** + * Default constructor. + */ + ArrayView(); + /** * Constructor. * @@ -159,6 +164,28 @@ public: */ ArrayView(std::vector::type> &vector); + /** + * Reinitialize a view. + * + * @param[in] starting_element A pointer to the first element of the array + * this object should represent. + * @param[in] n_elements The length (in elements) of the chunk of memory + * this object should represent. + * + * @note The object that is constructed from these arguments has no + * knowledge how large the object into which it points really is. As a + * consequence, whenever you call ArrayView::operator[], the array view can + * check that the given index is within the range of the view, but it can't + * check that the view is indeed a subset of the valid range of elements of + * the underlying object that allocated that range. In other words, you need + * to ensure that the range of the view specified by the two arguments to + * this constructor is in fact a subset of the elements of the array into + * which it points. The appropriate way to do this is to use the + * make_array_view() functions. + */ + void + reinit(value_type *starting_element, const std::size_t n_elements); + /** * Compare two ArrayView objects of the same type. Two objects are considered * equal if they have the same size and the same starting pointer. @@ -252,12 +279,12 @@ private: * A pointer to the first element of the range of locations in memory that * this object represents. */ - value_type *const starting_element; + value_type *starting_element; /** * The length of the array this object represents. */ - const std::size_t n_elements; + std::size_t n_elements; friend class ArrayView; }; @@ -305,6 +332,14 @@ namespace internal +template +inline ArrayView::ArrayView() + : starting_element(nullptr) + , n_elements(0) +{} + + + template inline ArrayView::ArrayView( value_type * starting_element, @@ -322,6 +357,16 @@ inline ArrayView::ArrayView( +template +inline void +ArrayView::reinit(value_type *starting_element, + const std::size_t n_elements) +{ + *this = ArrayView(starting_element, n_elements); +} + + + template inline ArrayView::ArrayView( const ArrayView::type, MemorySpaceType> diff --git a/tests/base/array_view_14.cc b/tests/base/array_view_14.cc new file mode 100644 index 0000000000..2e026c0a87 --- /dev/null +++ b/tests/base/array_view_14.cc @@ -0,0 +1,55 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 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 +// same as array_view_01.cc but uses default constructor and reinit() + +#include + +#include "../tests.h" + + +void +test() +{ + std::vector v(10); + + ArrayView a; + a.reinit(&v[4], 3); // writable view + a[2] = 42; + + Assert(a[2] == 42, ExcInternalError()); + Assert(v[6] == 42, ExcInternalError()); + + ArrayView a2; + a2.reinit(&v[4], 3); // readable view + Assert(a2[2] == 42, ExcInternalError()); + + ArrayView a3(a); // readable view, converted from 'a' + Assert(a3[2] == 42, ExcInternalError()); + + deallog << "OK" << std::endl; +} + + + +int +main() +{ + initlog(); + + test(); +} diff --git a/tests/base/array_view_14.output b/tests/base/array_view_14.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/array_view_14.output @@ -0,0 +1,2 @@ + +DEAL::OK