From: David Wells Date: Thu, 27 Jul 2017 03:09:01 +0000 (-0400) Subject: Add a simple iterator interface to ArrayView. X-Git-Tag: v9.0.0-rc1~1373^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26ab992941913644b952586e3d3bf96317990789;p=dealii.git Add a simple iterator interface to ArrayView. --- diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 5b0d394d99..b62234234b 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -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; }; @@ -191,6 +221,40 @@ ArrayView::size() const return n_elements; } +template +inline +typename ArrayView::iterator +ArrayView::begin() +{ + return starting_element; +} + + +template +inline +typename ArrayView::iterator +ArrayView::end() +{ + return starting_element + n_elements; +} + +template +inline +typename ArrayView::const_iterator +ArrayView::begin() const +{ + return starting_element; +} + + +template +inline +typename ArrayView::const_iterator +ArrayView::end() const +{ + return starting_element + n_elements; +} + template inline diff --git a/tests/base/array_view_08.cc b/tests/base/array_view_08.cc new file mode 100644 index 0000000000..54f6396d45 --- /dev/null +++ b/tests/base/array_view_08.cc @@ -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 + +#include + +template +constexpr +bool +is_const_reference() +{ + return std::is_reference::value && + std::is_const::type>::value; +} + +void test () +{ + // test for a mutable view of a mutable vector + { + std::vector v (10); + ArrayView a (&v[4], 3); + AssertThrow (a.begin() == &v[4], ExcInternalError()); + AssertThrow (a.end() == &v[7], ExcInternalError()); + + static_assert(std::is_reference::value, "type should be a reference"); + static_assert(std::is_reference::value, "type should be a reference"); + static_assert(!is_const_reference(), "type should not be const"); + static_assert(!is_const_reference(), "type should not be const"); + } + + // and an immutable view of a mutable vector + { + std::vector v (10); + const ArrayView a (&v[4], 3); + AssertThrow (a.begin() == &v[4], ExcInternalError()); + AssertThrow (a.end() == &v[7], ExcInternalError()); + + static_assert(is_const_reference(), "type should be const reference"); + static_assert(is_const_reference(), "type should be const reference"); + } + + // and an immutable view of an immutable vector + { + const std::vector v (10, 42); + const ArrayView a (&v[4], 3); + AssertThrow (a.begin() == &v[4], ExcInternalError()); + AssertThrow (a.end() == &v[7], ExcInternalError()); + + static_assert(is_const_reference(), "type should be const reference"); + static_assert(is_const_reference(), "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 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/array_view_08.output @@ -0,0 +1,2 @@ + +DEAL::OK