From b56d06c65cc4bb7979f0d683f249427914d4ba40 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Sun, 15 Mar 2020 01:05:25 +0100 Subject: [PATCH] Enable std::max_element for VectorizedArray --- include/deal.II/base/vectorization.h | 31 ++++++++-- tests/base/vectorization_iterator_02.cc | 68 +++++++++++++++++++++ tests/base/vectorization_iterator_02.output | 3 + 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 tests/base/vectorization_iterator_02.cc create mode 100644 tests/base/vectorization_iterator_02.output diff --git a/include/deal.II/base/vectorization.h b/include/deal.II/base/vectorization.h index 0ffc4c538a..d5f4b30d51 100644 --- a/include/deal.II/base/vectorization.h +++ b/include/deal.II/base/vectorization.h @@ -101,26 +101,47 @@ public: * @param lane A pointer to the current lane. */ VectorizedArrayIterator(T &data, const unsigned int lane) - : data(data) + : data(&data) , lane(lane) {} + /** + * Compare for equality. + */ + bool + operator==(const VectorizedArrayIterator &other) const + { + Assert(this->data == other.data, + ExcMessage( + "You are trying to compare iterators into different arrays.")); + return this->lane == other.lane; + } + /** * Compare for inequality. */ bool operator!=(const VectorizedArrayIterator &other) const { + Assert(this->data == other.data, + ExcMessage( + "You are trying to compare iterators into different arrays.")); return this->lane != other.lane; } + /** + * Copy assignment. + */ + VectorizedArrayIterator & + operator=(const VectorizedArrayIterator &other) = default; + /** * Dereferencing operator (const version): returns the value of the current * lane. */ const typename T::value_type &operator*() const { - return data[lane]; + return (*data)[lane]; } @@ -133,7 +154,7 @@ public: typename T::value_type>::type & operator*() { - return data[lane]; + return (*data)[lane]; } /** @@ -150,9 +171,9 @@ public: private: /** - * Reference to the actual VectorizedArray. + * Pointer to the actual VectorizedArray. */ - T &data; + T *data; /** * Pointer to the current lane. diff --git a/tests/base/vectorization_iterator_02.cc b/tests/base/vectorization_iterator_02.cc new file mode 100644 index 0000000000..c0ed5367a3 --- /dev/null +++ b/tests/base/vectorization_iterator_02.cc @@ -0,0 +1,68 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 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 for std::max_element on VectorizedArray + +#include + +#include "../tests.h" + + +template +void +test_const(const VectorizedArray &vector) +{ + AssertDimension(*std::max_element(vector.begin(), vector.end()), + VectorizedArray::n_array_elements - 1); +} + +template +void +test_nonconst(VectorizedArray &vector) +{ + AssertDimension(*std::max_element(vector.begin(), vector.end()), + VectorizedArray::n_array_elements - 1); +} + +template +void +test() +{ + VectorizedArray vector; + + for (unsigned int v = 0; v < VectorizedArray::size(); ++v) + vector[v] = v; + + test_const(vector); + test_nonconst(vector); +} + + +int +main() +{ + initlog(); + + deallog.push("float"); + test(); + deallog << "OK" << std::endl; + deallog.pop(); + + deallog.push("double"); + test(); + deallog << "OK" << std::endl; + deallog.pop(); +} diff --git a/tests/base/vectorization_iterator_02.output b/tests/base/vectorization_iterator_02.output new file mode 100644 index 0000000000..76f093e48f --- /dev/null +++ b/tests/base/vectorization_iterator_02.output @@ -0,0 +1,3 @@ + +DEAL:float::OK +DEAL:double::OK -- 2.39.5