From 4a70732278fcf0d0e69c3eee4ba955c4094f4eee Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Mon, 8 Oct 2018 15:12:14 +0200 Subject: [PATCH] Add explicit conversion of standard template containers One can write Array<T> a = convert_to_array(c); or Array<const T> a = convert_to_array(c); where c is a container such as std::vector<T>, std::set<T>, ... --- src/utils/Array.hpp | 15 ++++++++++++++- tests/test_Array.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/utils/Array.hpp b/src/utils/Array.hpp index 9c65e52ec..ae899cc3e 100644 --- a/src/utils/Array.hpp +++ b/src/utils/Array.hpp @@ -7,6 +7,7 @@ #include <PastisAssert.hpp> #include <Kokkos_CopyViews.hpp> +#include <algorithm> template <typename DataType> class Array @@ -66,7 +67,7 @@ class Array // ensures that const is not lost through copy static_assert(((std::is_const<DataType2>() and std::is_const<DataType>()) or not std::is_const<DataType2>()), - "Cannot assign Array of const to Array of non-const"); + "Cannot assign Array of const to Array of non-const"); m_values = array.m_values; return *this; } @@ -105,4 +106,16 @@ class Array ~Array() = default; }; +template <typename Container> +PASTIS_INLINE +Array<typename Container::value_type> convert_to_array(const Container& given_vector) +{ + using DataType = typename Container::value_type; + Array<std::remove_const_t<DataType>> array(given_vector.size()); + if (given_vector.size()>0) { + std::copy(begin(given_vector), end(given_vector), &(array[0])); + } + return array; +} + #endif // ARRAY_HPP diff --git a/tests/test_Array.cpp b/tests/test_Array.cpp index 29257385c..c2ec98c99 100644 --- a/tests/test_Array.cpp +++ b/tests/test_Array.cpp @@ -4,6 +4,9 @@ #include <Array.hpp> #include <Types.hpp> +#include <vector> +#include <set> + // Instantiate to ensure full coverage is performed template class Array<int>; @@ -140,6 +143,48 @@ TEST_CASE("Array", "[utils]") { (c[8] == 2) and (c[9] == 2))); } + SECTION("checking for std container conversion") { + { + std::vector<int> v{1,2,5,3}; + { + Array<int> v_array = convert_to_array(v); + + REQUIRE(v_array.size() == v.size()); + REQUIRE(((v_array[0] == 1) and (v_array[1] == 2) and + (v_array[2] == 5) and (v_array[3] == 3))); + } + + { + Array<const int> v_array = convert_to_array(v); + + REQUIRE(v_array.size() == v.size()); + REQUIRE(((v_array[0] == 1) and (v_array[1] == 2) and + (v_array[2] == 5) and (v_array[3] == 3))); + } + } + { + std::vector<int> w; + { + Array<int> w_array = convert_to_array(w); + REQUIRE(w_array.size() == 0); + } + { + Array<const int> w_array = convert_to_array(w); + REQUIRE(w_array.size() == 0); + } + } + + { + std::set<int> s{4,2,5,3,1}; + Array<int> s_array = convert_to_array(s); + + REQUIRE(s_array.size() == s.size()); + REQUIRE(((s_array[0] == 1) and (s_array[1] == 2) and + (s_array[2] == 3) and (s_array[3] == 4) and + (s_array[4] == 5))); + } + } + #ifndef NDEBUG SECTION("checking for bounds violation") { REQUIRE_THROWS_AS(a[10], AssertError); -- GitLab