]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a simple iterator interface to ArrayView.
authorDavid Wells <wellsd2@rpi.edu>
Thu, 27 Jul 2017 03:09:01 +0000 (23:09 -0400)
committerDavid Wells <wellsd2@rpi.edu>
Thu, 27 Jul 2017 04:16:47 +0000 (00:16 -0400)
include/deal.II/base/array_view.h
tests/base/array_view_08.cc [new file with mode: 0644]
tests/base/array_view_08.output [new file with mode: 0644]

index 5b0d394d99aeb25b6d067c60d616ec846e5f0a4e..b62234234be3c6a3ed8051a42d73f37a22bf132c 100644 (file)
@@ -76,6 +76,16 @@ public:
    */
   typedef ElementType value_type;
 
+  /**
+   * A typedef for iterators pointing into the array.
+   */
+  typedef value_type *iterator;
+
+  /**
+   * A typedef for const iterators pointing into the array.
+   */
+  typedef const ElementType *const_iterator;
+
   /**
    * Default constructor. Creates an invalid view that does not point to
    * anything at all.
@@ -121,6 +131,26 @@ public:
    */
   std::size_t size() const;
 
+  /**
+   * Return an iterator pointing to the beginning of the array view.
+   */
+  iterator begin();
+
+  /**
+   * Return an iterator pointing to one past the end of the array view.
+   */
+  iterator end();
+
+  /**
+   * Return a constant iterator pointing to the beginning of the array view.
+   */
+  const_iterator begin() const;
+
+  /**
+   * Return a constant iterator pointing to one past the end of the array view.
+   */
+  const_iterator end() const;
+
   /**
    * Return a reference to the $i$th element of the range represented by the
    * current object.
@@ -137,12 +167,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 *const starting_element;
 
   /**
    * The length of the array this object represents.
    */
-  const std::size_t  n_elements;
+  const std::size_t n_elements;
 
   friend class ArrayView<const ElementType>;
 };
@@ -191,6 +221,40 @@ ArrayView<ElementType>::size() const
   return n_elements;
 }
 
+template <typename ElementType>
+inline
+typename ArrayView<ElementType>::iterator
+ArrayView<ElementType>::begin()
+{
+  return starting_element;
+}
+
+
+template <typename ElementType>
+inline
+typename ArrayView<ElementType>::iterator
+ArrayView<ElementType>::end()
+{
+  return starting_element + n_elements;
+}
+
+template <typename ElementType>
+inline
+typename ArrayView<ElementType>::const_iterator
+ArrayView<ElementType>::begin() const
+{
+  return starting_element;
+}
+
+
+template <typename ElementType>
+inline
+typename ArrayView<ElementType>::const_iterator
+ArrayView<ElementType>::end() const
+{
+  return starting_element + n_elements;
+}
+
 
 template <typename ElementType>
 inline
diff --git a/tests/base/array_view_08.cc b/tests/base/array_view_08.cc
new file mode 100644 (file)
index 0000000..54f6396
--- /dev/null
@@ -0,0 +1,81 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 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.
+//
+// ---------------------------------------------------------------------
+
+
+// test ArrayView::begin and ArrayView::end
+
+#include "../tests.h"
+
+#include <deal.II/base/array_view.h>
+
+#include <type_traits>
+
+template <typename T>
+constexpr
+bool
+is_const_reference()
+{
+  return std::is_reference<T>::value &&
+         std::is_const<typename std::remove_reference<T>::type>::value;
+}
+
+void test ()
+{
+  // test for a mutable view of a mutable vector
+  {
+    std::vector<int> v (10);
+    ArrayView<int> a (&v[4], 3);
+    AssertThrow (a.begin() == &v[4], ExcInternalError());
+    AssertThrow (a.end() == &v[7], ExcInternalError());
+
+    static_assert(std::is_reference<decltype(*a.begin())>::value, "type should be a reference");
+    static_assert(std::is_reference<decltype(*a.end())>::value, "type should be a reference");
+    static_assert(!is_const_reference<decltype(*a.begin())>(), "type should not be const");
+    static_assert(!is_const_reference<decltype(*a.end())>(), "type should not be const");
+  }
+
+  // and an immutable view of a mutable vector
+  {
+    std::vector<int> v (10);
+    const ArrayView<const int> a (&v[4], 3);
+    AssertThrow (a.begin() == &v[4], ExcInternalError());
+    AssertThrow (a.end() == &v[7], ExcInternalError());
+
+    static_assert(is_const_reference<decltype(*a.begin())>(), "type should be const reference");
+    static_assert(is_const_reference<decltype(*a.end())>(), "type should be const reference");
+  }
+
+  // and an immutable view of an immutable vector
+  {
+    const std::vector<int> v (10, 42);
+    const ArrayView<const int> a (&v[4], 3);
+    AssertThrow (a.begin() == &v[4], ExcInternalError());
+    AssertThrow (a.end() == &v[7], ExcInternalError());
+
+    static_assert(is_const_reference<decltype(*a.begin())>(), "type should be const reference");
+    static_assert(is_const_reference<decltype(*a.end())>(), "type should be const reference");
+  }
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main()
+{
+  initlog();
+
+  test ();
+}
diff --git a/tests/base/array_view_08.output b/tests/base/array_view_08.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.