]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add Vector::data and AlignedVector::data. 7615/head
authorDavid Wells <drwells@email.unc.edu>
Sat, 19 Jan 2019 03:31:32 +0000 (22:31 -0500)
committerDavid Wells <drwells@email.unc.edu>
Sat, 19 Jan 2019 03:46:15 +0000 (22:46 -0500)
This improves compatibility with std::vector.

include/deal.II/base/aligned_vector.h
include/deal.II/lac/vector.h
tests/base/aligned_vector_data.cc [new file with mode: 0644]
tests/base/aligned_vector_data.output [new file with mode: 0644]
tests/lac/vector_data.cc [new file with mode: 0644]
tests/lac/vector_data.output [new file with mode: 0644]

index 23cba357e244f372f6c3fd477fdd19f9bbe49f5f..2a17623ec4e6fb34475a1a6e031cc61a97277883 100644 (file)
@@ -267,6 +267,18 @@ public:
    */
   const_reference operator[](const size_type index) const;
 
+  /**
+   * Return a pointer to the underlying data buffer.
+   */
+  pointer
+  data();
+
+  /**
+   * Return a const pointer to the underlying data buffer.
+   */
+  const_pointer
+  data() const;
+
   /**
    * Return a read and write pointer to the beginning of the data array.
    */
@@ -1036,6 +1048,24 @@ inline typename AlignedVector<T>::const_reference AlignedVector<T>::
 
 
 
+template <typename T>
+inline typename AlignedVector<T>::pointer
+AlignedVector<T>::data()
+{
+  return data_begin;
+}
+
+
+
+template <typename T>
+inline typename AlignedVector<T>::const_pointer
+AlignedVector<T>::data() const
+{
+  return data_begin;
+}
+
+
+
 template <class T>
 inline typename AlignedVector<T>::iterator
 AlignedVector<T>::begin()
index 4ba39fcbd9c474b70965e0ec00348034faddf91a..085c655f27bd84f81d465f12e5409da9ed04a348 100644 (file)
@@ -555,6 +555,18 @@ public:
    */
   //@{
 
+  /**
+   * Return a pointer to the underlying data buffer.
+   */
+  pointer
+  data();
+
+  /**
+   * Return a const pointer to the underlying data buffer.
+   */
+  const_pointer
+  data() const;
+
   /**
    * Make the @p Vector class a bit like the <tt>vector<></tt> class of the
    * C++ standard library by returning iterators to the start and end of the
@@ -1108,6 +1120,24 @@ Vector<Number>::in_local_range(const size_type) const
 
 
 
+template <typename Number>
+inline typename Vector<Number>::pointer
+Vector<Number>::data()
+{
+  return values.get();
+}
+
+
+
+template <typename Number>
+inline typename Vector<Number>::const_pointer
+Vector<Number>::data() const
+{
+  return values.get();
+}
+
+
+
 template <typename Number>
 inline typename Vector<Number>::iterator
 Vector<Number>::begin()
diff --git a/tests/base/aligned_vector_data.cc b/tests/base/aligned_vector_data.cc
new file mode 100644 (file)
index 0000000..032fea3
--- /dev/null
@@ -0,0 +1,72 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2018 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 AlignedVector::data
+
+#include <deal.II/base/aligned_vector.h>
+
+#include "../tests.h"
+
+
+template <typename Container>
+void
+test_array_empty(Container &array)
+{
+  AssertThrow(array.begin() == array.end(), ExcInternalError());
+  AssertThrow(array.end() == array.data(), ExcInternalError());
+  AssertThrow(array.data() == nullptr, ExcInternalError());
+}
+
+
+
+template <typename Container>
+void
+test_array_single(Container &array)
+{
+  AssertThrow(array.begin() + 1 == array.end(), ExcInternalError());
+  AssertThrow(array.end() - 1 == array.data(), ExcInternalError());
+  AssertThrow(array.data() == &array[0], ExcInternalError());
+}
+
+
+
+template <typename T>
+void
+test()
+{
+  AlignedVector<T>        array;
+  const AlignedVector<T> &const_array = array;
+  test_array_empty(array);
+  test_array_empty(const_array);
+
+  array.push_back(T{});
+  test_array_single(array);
+  test_array_single(const_array);
+}
+
+
+
+int
+main()
+{
+  initlog();
+
+  test<int>();
+  test<long double>();
+  test<AlignedVector<double>>();
+
+  deallog << "OK" << std::endl;
+}
diff --git a/tests/base/aligned_vector_data.output b/tests/base/aligned_vector_data.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK
diff --git a/tests/lac/vector_data.cc b/tests/lac/vector_data.cc
new file mode 100644 (file)
index 0000000..6883af4
--- /dev/null
@@ -0,0 +1,72 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2018 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 Vector::data
+
+#include <deal.II/lac/vector.h>
+#include <deal.II/lac/vector.templates.h>
+
+#include "../tests.h"
+
+
+template <typename Container>
+void
+test_array_empty(Container &array)
+{
+  AssertThrow(array.begin() == array.end(), ExcInternalError());
+  AssertThrow(array.end() == array.data(), ExcInternalError());
+  AssertThrow(array.data() == nullptr, ExcInternalError());
+}
+
+
+
+template <typename Container>
+void
+test_array_single(Container &array)
+{
+  AssertThrow(array.begin() + 1 == array.end(), ExcInternalError());
+  AssertThrow(array.end() - 1 == array.data(), ExcInternalError());
+}
+
+
+
+template <typename T>
+void
+test()
+{
+  Vector<T>        array;
+  const Vector<T> &const_array = array;
+  test_array_empty(array);
+  test_array_empty(const_array);
+
+  array.reinit(1);
+  test_array_single(array);
+  test_array_single(const_array);
+}
+
+
+
+int
+main()
+{
+  initlog();
+
+  test<int>();
+  test<double>();
+  test<long double>();
+
+  deallog << "OK" << std::endl;
+}
diff --git a/tests/lac/vector_data.output b/tests/lac/vector_data.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.