]> https://gitweb.dealii.org/ - dealii.git/commitdiff
add default constructor for ArrayView and a reinit() method 7885/head
authorDenis Davydov <davydden@gmail.com>
Tue, 2 Apr 2019 19:56:32 +0000 (21:56 +0200)
committerDenis Davydov <davydden@gmail.com>
Tue, 2 Apr 2019 22:05:06 +0000 (00:05 +0200)
doc/news/changes/minor/20190402DenisDavydov [new file with mode: 0644]
include/deal.II/base/array_view.h
tests/base/array_view_14.cc [new file with mode: 0644]
tests/base/array_view_14.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20190402DenisDavydov b/doc/news/changes/minor/20190402DenisDavydov
new file mode 100644 (file)
index 0000000..a2d7537
--- /dev/null
@@ -0,0 +1,3 @@
+New: Add ArraView::ArrayView() and ArrayView::reinit(value_type *, const std::size_t).
+<br>
+(Denis Davydov, 2019/04/02)
index 130e693f4bef88230d49655f281e6a30d3bf864a..e51f984b6346361311664f50719bc88794c8cd17 100644 (file)
@@ -93,6 +93,11 @@ public:
    */
   using const_iterator = const ElementType *;
 
+  /**
+   * Default constructor.
+   */
+  ArrayView();
+
   /**
    * Constructor.
    *
@@ -159,6 +164,28 @@ public:
    */
   ArrayView(std::vector<typename std::remove_cv<value_type>::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<const ElementType, MemorySpaceType>;
 };
@@ -305,6 +332,14 @@ namespace internal
 
 
 
+template <typename ElementType, typename MemorySpaceType>
+inline ArrayView<ElementType, MemorySpaceType>::ArrayView()
+  : starting_element(nullptr)
+  , n_elements(0)
+{}
+
+
+
 template <typename ElementType, typename MemorySpaceType>
 inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
   value_type *      starting_element,
@@ -322,6 +357,16 @@ inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
 
 
 
+template <typename ElementType, typename MemorySpaceType>
+inline void
+ArrayView<ElementType, MemorySpaceType>::reinit(value_type *starting_element,
+                                                const std::size_t n_elements)
+{
+  *this = ArrayView(starting_element, n_elements);
+}
+
+
+
 template <typename ElementType, typename MemorySpaceType>
 inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
   const ArrayView<typename std::remove_cv<value_type>::type, MemorySpaceType>
diff --git a/tests/base/array_view_14.cc b/tests/base/array_view_14.cc
new file mode 100644 (file)
index 0000000..2e026c0
--- /dev/null
@@ -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 <deal.II/base/array_view.h>
+
+#include "../tests.h"
+
+
+void
+test()
+{
+  std::vector<int> v(10);
+
+  ArrayView<int> a;
+  a.reinit(&v[4], 3); // writable view
+  a[2] = 42;
+
+  Assert(a[2] == 42, ExcInternalError());
+  Assert(v[6] == 42, ExcInternalError());
+
+  ArrayView<const int> a2;
+  a2.reinit(&v[4], 3); // readable view
+  Assert(a2[2] == 42, ExcInternalError());
+
+  ArrayView<const int> 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 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.