From: David Wells Date: Sat, 19 Jan 2019 03:31:32 +0000 (-0500) Subject: Add Vector::data and AlignedVector::data. X-Git-Tag: v9.1.0-rc1~424^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0dfca4b74263123e2cf62acc05a57ce5d5de5f5d;p=dealii.git Add Vector::data and AlignedVector::data. This improves compatibility with std::vector. --- diff --git a/include/deal.II/base/aligned_vector.h b/include/deal.II/base/aligned_vector.h index 23cba357e2..2a17623ec4 100644 --- a/include/deal.II/base/aligned_vector.h +++ b/include/deal.II/base/aligned_vector.h @@ -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::const_reference AlignedVector:: +template +inline typename AlignedVector::pointer +AlignedVector::data() +{ + return data_begin; +} + + + +template +inline typename AlignedVector::const_pointer +AlignedVector::data() const +{ + return data_begin; +} + + + template inline typename AlignedVector::iterator AlignedVector::begin() diff --git a/include/deal.II/lac/vector.h b/include/deal.II/lac/vector.h index 4ba39fcbd9..085c655f27 100644 --- a/include/deal.II/lac/vector.h +++ b/include/deal.II/lac/vector.h @@ -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 vector<> class of the * C++ standard library by returning iterators to the start and end of the @@ -1108,6 +1120,24 @@ Vector::in_local_range(const size_type) const +template +inline typename Vector::pointer +Vector::data() +{ + return values.get(); +} + + + +template +inline typename Vector::const_pointer +Vector::data() const +{ + return values.get(); +} + + + template inline typename Vector::iterator Vector::begin() diff --git a/tests/base/aligned_vector_data.cc b/tests/base/aligned_vector_data.cc new file mode 100644 index 0000000000..032fea30f2 --- /dev/null +++ b/tests/base/aligned_vector_data.cc @@ -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 + +#include "../tests.h" + + +template +void +test_array_empty(Container &array) +{ + AssertThrow(array.begin() == array.end(), ExcInternalError()); + AssertThrow(array.end() == array.data(), ExcInternalError()); + AssertThrow(array.data() == nullptr, ExcInternalError()); +} + + + +template +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 +void +test() +{ + AlignedVector array; + const AlignedVector &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(); + test(); + test>(); + + deallog << "OK" << std::endl; +} diff --git a/tests/base/aligned_vector_data.output b/tests/base/aligned_vector_data.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/aligned_vector_data.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/lac/vector_data.cc b/tests/lac/vector_data.cc new file mode 100644 index 0000000000..6883af44dc --- /dev/null +++ b/tests/lac/vector_data.cc @@ -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 +#include + +#include "../tests.h" + + +template +void +test_array_empty(Container &array) +{ + AssertThrow(array.begin() == array.end(), ExcInternalError()); + AssertThrow(array.end() == array.data(), ExcInternalError()); + AssertThrow(array.data() == nullptr, ExcInternalError()); +} + + + +template +void +test_array_single(Container &array) +{ + AssertThrow(array.begin() + 1 == array.end(), ExcInternalError()); + AssertThrow(array.end() - 1 == array.data(), ExcInternalError()); +} + + + +template +void +test() +{ + Vector array; + const Vector &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(); + test(); + test(); + + deallog << "OK" << std::endl; +} diff --git a/tests/lac/vector_data.output b/tests/lac/vector_data.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/lac/vector_data.output @@ -0,0 +1,2 @@ + +DEAL::OK