Skip to content
Snippets Groups Projects
Commit 4a707322 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

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>, ...
parent 4e25cdc4
No related branches found
No related tags found
1 merge request!11Feature/mpi
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <PastisAssert.hpp> #include <PastisAssert.hpp>
#include <Kokkos_CopyViews.hpp> #include <Kokkos_CopyViews.hpp>
#include <algorithm>
template <typename DataType> template <typename DataType>
class Array class Array
...@@ -105,4 +106,16 @@ class Array ...@@ -105,4 +106,16 @@ class Array
~Array() = default; ~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 #endif // ARRAY_HPP
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include <Array.hpp> #include <Array.hpp>
#include <Types.hpp> #include <Types.hpp>
#include <vector>
#include <set>
// Instantiate to ensure full coverage is performed // Instantiate to ensure full coverage is performed
template class Array<int>; template class Array<int>;
...@@ -140,6 +143,48 @@ TEST_CASE("Array", "[utils]") { ...@@ -140,6 +143,48 @@ TEST_CASE("Array", "[utils]") {
(c[8] == 2) and (c[9] == 2))); (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 #ifndef NDEBUG
SECTION("checking for bounds violation") { SECTION("checking for bounds violation") {
REQUIRE_THROWS_AS(a[10], AssertError); REQUIRE_THROWS_AS(a[10], AssertError);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment