]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Check contiguity of iterators 5790/head
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Wed, 24 Jan 2018 08:50:45 +0000 (09:50 +0100)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Thu, 25 Jan 2018 13:25:52 +0000 (14:25 +0100)
include/deal.II/base/array_view.h
source/lac/vector.cc
tests/base/array_view_13.cc [new file with mode: 0644]
tests/base/array_view_13.debug.output [new file with mode: 0644]

index abbc7c308c0d6bfd58c6e723ea6cd86f734eaf14..6c3259179c59fce98925b64a955fd0f9ba2d9844 100644 (file)
@@ -573,6 +573,24 @@ make_array_view (std::vector<ElementType> &vector,
   return ArrayView<ElementType> (&vector[starting_index], size_of_view);
 }
 
+#ifndef DOXYGEN
+namespace internal
+{
+  namespace ArrayViewHelper
+  {
+    template<class Iterator>
+    bool is_contiguous(const Iterator &first, const Iterator &last)
+    {
+      const auto n = std::distance(first, last);
+      for (typename std::decay<decltype(n)>::type i = 0; i < n; ++i)
+        if (*(std::next(first, i)) != *(std::next(std::addressof(*first), i)))
+          return false;
+      return true;
+    }
+  }
+}
+#endif
+
 
 /**
  * Create an ArrayView that takes a pair of iterators as arguments. The type
@@ -585,6 +603,8 @@ make_array_view (std::vector<ElementType> &vector,
  * <code>boost::container::small_vector</code> or <code>std::vector</code> and
  * will not work correctly with, e.g.,
  * <code>boost::container::stable_vector</code> or <code>std::deque</code>.
+ * In debug mode, we check that the provided iterators represent contiguous
+ * memory indeed.
  *
  * @relates ArrayView
  */
@@ -597,6 +617,8 @@ make_array_view (const Iterator begin, const Iterator end)
                 "The provided iterator should be a random access iterator.");
   Assert(begin <= end,
          ExcMessage("The beginning of the array view should be before the end."));
+  Assert(internal::ArrayViewHelper::is_contiguous(begin, end),
+         ExcMessage("The provided range isn't contiguous in memory!"));
   // the reference type, not the value type, knows the constness of the iterator
   return ArrayView<typename std::remove_reference
          <typename std::iterator_traits<Iterator>::reference>::type>
index a1f97a6787e2966f4ba6f11c48c6d11f9b6e621d..0f1c13a1528880067ba4e792e2042b52a40023ec 100644 (file)
@@ -22,6 +22,7 @@ DEAL_II_NAMESPACE_OPEN
 // instantiate for integers:
 template class Vector<int>;
 template Vector<double> &Vector<double>::operator=<int> (const dealii::Vector<int> &);
+template bool Vector<int>::operator==<int> (dealii::Vector<int> const &) const;
 
 template
 void Vector<int>::reinit<double>(const Vector<double> &, const bool);
diff --git a/tests/base/array_view_13.cc b/tests/base/array_view_13.cc
new file mode 100644 (file)
index 0000000..d7447ec
--- /dev/null
@@ -0,0 +1,60 @@
+// ---------------------------------------------------------------------
+//
+// 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 for class ArrayView
+
+#include "../tests.h"
+
+#include <deal.II/base/array_view.h>
+
+template <typename T>
+void test (const T &t)
+{
+  try
+    {
+      make_array_view(std::begin(t), std::end(t));
+    }
+  catch (ExceptionBase &e)
+    {
+      deallog << e.get_exc_name() << std::endl;
+    }
+}
+
+int main()
+{
+  deal_II_exceptions::disable_abort_on_exception();
+  initlog();
+
+  auto d1 = std::deque<int>(2);
+  auto d2 = std::deque<int>(4000);
+  int c[] = { 1, 2, 3 };
+  auto a = std::array<int, 3> {{ 1, 2, 3 }};
+  auto s = std::string {"Hello world!"};
+  auto v = std::vector<int> { 1, 2, 3, };
+
+  deallog << "Testing std::deque<int>(2)" << std::endl;
+  test(d1);
+  deallog << "Testing std::deque<int>(4000)" << std::endl;
+  test(d2);
+  deallog << "Testing int c[3]{1,2,3}" << std::endl;
+  test(c);
+  deallog << "Testing std::array<int, 3> {{ 1,2,3}}" << std::endl;
+  test(a);
+  deallog << "Testing std::string {\"Hello world!\"}" << std::endl;
+  test(s);
+  deallog << "Testing std::vector<int>{1,2,3}" << std::endl;
+  test(v);
+}
diff --git a/tests/base/array_view_13.debug.output b/tests/base/array_view_13.debug.output
new file mode 100644 (file)
index 0000000..27b5e0a
--- /dev/null
@@ -0,0 +1,8 @@
+
+DEAL::Testing std::deque<int>(2)
+DEAL::Testing std::deque<int>(4000)
+DEAL::ExcMessage("The provided range isn't contiguous in memory!")
+DEAL::Testing int c[3]{1,2,3}
+DEAL::Testing std::array<int, 3> {{ 1,2,3}}
+DEAL::Testing std::string {"Hello world!"}
+DEAL::Testing std::vector<int>{1,2,3}

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.