13#include <Kokkos_Core.hpp>
15#include "ddc/detail/macros.hpp"
16#include "ddc/detail/type_seq.hpp"
28template <
class... Tags>
39template <
class... Tags>
40struct ToTypeSeq<DiscreteVector<Tags...>>
42 using type = TypeSeq<Tags...>;
48
49using DiscreteVectorElement = std::ptrdiff_t;
51template <
class QueryTag,
class... Tags>
53 DiscreteVector<Tags...>
const& tuple)
noexcept
55 return tuple.
template get<QueryTag>();
58template <
class QueryTag,
class... Tags>
59KOKKOS_FUNCTION constexpr DiscreteVectorElement&
get(DiscreteVector<Tags...>& tuple)
noexcept
61 return tuple.
template get<QueryTag>();
64template <
class QueryTag,
class... Tags>
66 DiscreteVector<Tags...>
const& tuple,
67 DiscreteVectorElement
const& default_value)
noexcept
69 return tuple.
template get_or<QueryTag>(default_value);
74template <
class... Tags>
80template <
class... Tags>
83 return DiscreteVector<Tags...>((-get<Tags>(x))...);
88template <
class... Tags,
class... OTags>
90 DiscreteVector<Tags...>
const& lhs,
91 DiscreteVector<OTags...>
const& rhs)
93 using detail::TypeSeq;
94 if constexpr (
sizeof...(Tags) >=
sizeof...(OTags)) {
95 static_assert(((type_seq_contains_v<TypeSeq<OTags>, TypeSeq<Tags...>>)&&...));
96 DiscreteVector<Tags...> result(lhs);
100 static_assert(((type_seq_contains_v<TypeSeq<Tags>, TypeSeq<OTags...>>)&&...));
101 DiscreteVector<OTags...> result(rhs);
107template <
class... Tags,
class... OTags>
109 DiscreteVector<Tags...>
const& lhs,
110 DiscreteVector<OTags...>
const& rhs)
112 using detail::TypeSeq;
113 if constexpr (
sizeof...(Tags) >=
sizeof...(OTags)) {
114 static_assert(((type_seq_contains_v<TypeSeq<OTags>, TypeSeq<Tags...>>)&&...));
115 DiscreteVector<Tags...> result(lhs);
119 static_assert(((type_seq_contains_v<TypeSeq<Tags>, TypeSeq<OTags...>>)&&...));
120 DiscreteVector<OTags...> result(-rhs);
126template <
class Tag,
class IntegralType,
class = std::enable_if_t<std::is_integral_v<IntegralType>>>
128 DiscreteVector<Tag>
const& lhs,
129 IntegralType
const& rhs)
131 return DiscreteVector<Tag>(get<Tag>(lhs) + rhs);
134template <
class IntegralType,
class Tag,
class = std::enable_if_t<std::is_integral_v<IntegralType>>>
136 IntegralType
const& lhs,
137 DiscreteVector<Tag>
const& rhs)
139 return DiscreteVector<Tag>(lhs + get<Tag>(rhs));
142template <
class Tag,
class IntegralType,
class = std::enable_if_t<std::is_integral_v<IntegralType>>>
144 DiscreteVector<Tag>
const& lhs,
145 IntegralType
const& rhs)
147 return DiscreteVector<Tag>(get<Tag>(lhs) - rhs);
150template <
class IntegralType,
class Tag,
class = std::enable_if_t<std::is_integral_v<IntegralType>>>
152 IntegralType
const& lhs,
153 DiscreteVector<Tag>
const& rhs)
155 return DiscreteVector<Tag>(lhs - get<Tag>(rhs));
163 class = std::enable_if_t<std::is_integral_v<IntegralType>>>
165 IntegralType
const& lhs,
166 DiscreteVector<Tags...>
const& rhs)
168 return DiscreteVector<Tags...>((lhs * get<Tags>(rhs))...);
171template <
class... QueryTags,
class... Tags>
173 DiscreteVector<Tags...>
const& arr)
noexcept
175 return DiscreteVector<QueryTags...>(arr);
178template <
class... QueryTags,
class... Tags>
180 DiscreteVector<Tags...>&& arr)
noexcept
182 return DiscreteVector<QueryTags...>(std::move(arr));
191 is_discrete_vector_v<HeadDVect> && (is_discrete_vector_v<TailDVects> && ...),
193KOKKOS_FUNCTION constexpr auto const&
take(HeadDVect
const& head, TailDVects
const&... tail)
195 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
196 if constexpr (type_seq_contains_v<detail::TypeSeq<QueryTag>, to_type_seq_t<HeadDVect>>) {
198 (!type_seq_contains_v<detail::TypeSeq<QueryTag>, to_type_seq_t<TailDVects>> && ...),
199 "ERROR: tag redundant");
202 static_assert(
sizeof...(TailDVects) > 0,
"ERROR: tag not found");
203 return take<QueryTag>(tail...);
211class DiscreteVectorConversionOperators
216class DiscreteVectorConversionOperators<DiscreteVector<Tag>>
219 KOKKOS_FUNCTION
constexpr operator DiscreteVectorElement
const &()
const noexcept
221 return static_cast<DiscreteVector<Tag>
const*>(
this)->m_values[0];
224 KOKKOS_FUNCTION
constexpr operator DiscreteVectorElement&()
noexcept
226 return static_cast<DiscreteVector<Tag>*>(
this)->m_values[0];
231template <
class... Tags>
232KOKKOS_FUNCTION
constexpr std::array<DiscreteVectorElement,
sizeof...(Tags)>& array(
233 DiscreteVector<Tags...>& v)
noexcept
239template <
class... Tags>
240KOKKOS_FUNCTION
constexpr std::array<DiscreteVectorElement,
sizeof...(Tags)>
const& array(
241 DiscreteVector<Tags...>
const& v)
noexcept
249
250
251
252template <
class... Tags>
253class DiscreteVector :
public detail::DiscreteVectorConversionOperators<DiscreteVector<Tags...>>
255 friend class detail::DiscreteVectorConversionOperators<DiscreteVector<Tags...>>;
257 friend KOKKOS_FUNCTION
constexpr std::array<DiscreteVectorElement,
sizeof...(Tags)>& detail::
258 array<Tags...>(DiscreteVector<Tags...>& v)
noexcept;
259 friend KOKKOS_FUNCTION
constexpr std::array<DiscreteVectorElement,
sizeof...(Tags)>
const&
260 detail::array<Tags...>(DiscreteVector<Tags...>
const& v)
noexcept;
262 using tags_seq = detail::TypeSeq<Tags...>;
265 std::array<DiscreteVectorElement,
sizeof...(Tags)> m_values;
270 return sizeof...(Tags);
276 KOKKOS_DEFAULTED_FUNCTION
constexpr DiscreteVector(DiscreteVector
const&) =
default;
278 KOKKOS_DEFAULTED_FUNCTION
constexpr DiscreteVector(DiscreteVector&&) =
default;
280 template <
class... DVects,
class = std::enable_if_t<(is_discrete_vector_v<DVects> && ...)>>
282 : m_values {take<Tags>(delems...).
template get<Tags>()...}
288 class = std::enable_if_t<(!is_discrete_vector_v<Params> && ...)>,
289 class = std::enable_if_t<(std::is_convertible_v<Params, DiscreteVectorElement> && ...)>,
290 class = std::enable_if_t<
sizeof...(Params) ==
sizeof...(Tags)>>
292 : m_values {
static_cast<DiscreteVectorElement>(params)...}
298 KOKKOS_DEFAULTED_FUNCTION
constexpr DiscreteVector&
operator=(DiscreteVector
const& other)
301 KOKKOS_DEFAULTED_FUNCTION
constexpr DiscreteVector&
operator=(DiscreteVector&& other) =
default;
303 template <
class... OTags>
305 DiscreteVector<OTags...>
const& other)
noexcept
307 m_values = other.m_values;
311 template <
class... OTags>
314 m_values = std::move(other.m_values);
318 template <
class... OTags>
321 return ((m_values[type_seq_rank_v<Tags, tags_seq>] == rhs.
template get<Tags>()) && ...);
324 template <
class... OTags>
327 return !(*
this == rhs);
330 template <
class QueryTag>
333 using namespace detail;
334 static_assert(in_tags_v<QueryTag, tags_seq>,
"requested Tag absent from DiscreteVector");
335 return m_values[type_seq_rank_v<QueryTag, tags_seq>];
338 template <
class QueryTag>
341 using namespace detail;
342 static_assert(in_tags_v<QueryTag, tags_seq>,
"requested Tag absent from DiscreteVector");
343 return m_values[type_seq_rank_v<QueryTag, tags_seq>];
346 template <
class QueryTag>
348 DiscreteVectorElement
const& default_value)
const&
350 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
351 if constexpr (in_tags_v<QueryTag, tags_seq>) {
352 return m_values[type_seq_rank_v<QueryTag, tags_seq>];
354 return default_value;
359 template <std::size_t N =
sizeof...(Tags)>
366 template <std::size_t N =
sizeof...(Tags),
class = std::enable_if_t<N == 1>>
373 template <std::size_t N =
sizeof...(Tags),
class = std::enable_if_t<N == 1>>
376 DiscreteVector
const tmp = *
this;
381 template <std::size_t N =
sizeof...(Tags),
class = std::enable_if_t<N == 1>>
388 template <std::size_t N =
sizeof...(Tags),
class = std::enable_if_t<N == 1>>
391 DiscreteVector
const tmp = *
this;
396 template <
class... OTags>
399 static_assert(((type_seq_contains_v<detail::TypeSeq<OTags>, tags_seq>)&&...));
400 ((m_values[type_seq_rank_v<OTags, tags_seq>] += rhs.
template get<OTags>()), ...);
406 std::size_t N =
sizeof...(Tags),
407 class = std::enable_if_t<N == 1>,
408 class = std::enable_if_t<std::is_integral_v<IntegralType>>>
415 template <
class... OTags>
418 static_assert(((type_seq_contains_v<detail::TypeSeq<OTags>, tags_seq>)&&...));
419 ((m_values[type_seq_rank_v<OTags, tags_seq>] -= rhs.
template get<OTags>()), ...);
425 std::size_t N =
sizeof...(Tags),
426 class = std::enable_if_t<N == 1>,
427 class = std::enable_if_t<std::is_integral_v<IntegralType>>>
434 template <
class... OTags>
437 static_assert(((type_seq_contains_v<detail::TypeSeq<OTags>, tags_seq>)&&...));
438 ((m_values[type_seq_rank_v<OTags, tags_seq>] *= rhs.
template get<OTags>()), ...);
445 DiscreteVector<Tag>
const& lhs,
446 DiscreteVector<Tag>
const& rhs)
448 return lhs.value() < rhs.value();
451template <
class Tag,
class IntegralType>
454 return lhs.value() < rhs;
457inline std::ostream& operator<<(std::ostream& out, DiscreteVector<>
const&)
463template <
class Head,
class... Tags>
464std::ostream& operator<<(std::ostream& out, DiscreteVector<Head, Tags...>
const& arr)
467 out << get<Head>(arr);
468 ((out <<
", " << get<Tags>(arr)), ...);
KOKKOS_FUNCTION constexpr DiscreteVector & operator--()
Definition: discrete_vector.hpp:382
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get_or(DiscreteVectorElement const &default_value) const &
Definition: discrete_vector.hpp:347
KOKKOS_FUNCTION constexpr std::enable_if_t< N==1, DiscreteVectorElement const & > value() const noexcept
Definition: discrete_vector.hpp:360
KOKKOS_FUNCTION constexpr DiscreteVector & operator+=(IntegralType const &rhs)
Definition: discrete_vector.hpp:409
KOKKOS_DEFAULTED_FUNCTION ~DiscreteVector()=default
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get() const noexcept
Definition: discrete_vector.hpp:339
KOKKOS_FUNCTION constexpr DiscreteVector & operator=(DiscreteVector< OTags... > const &other) noexcept
Definition: discrete_vector.hpp:304
KOKKOS_FUNCTION constexpr DiscreteVector operator--(int)
Definition: discrete_vector.hpp:389
KOKKOS_FUNCTION constexpr DiscreteVector & operator-=(DiscreteVector< OTags... > const &rhs)
Definition: discrete_vector.hpp:416
KOKKOS_FUNCTION constexpr DiscreteVector & operator-=(IntegralType const &rhs)
Definition: discrete_vector.hpp:428
KOKKOS_FUNCTION constexpr DiscreteVector operator++(int)
Definition: discrete_vector.hpp:374
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
Definition: discrete_vector.hpp:325
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector & operator=(DiscreteVector &&other)=default
KOKKOS_FUNCTION constexpr DiscreteVector & operator++()
Definition: discrete_vector.hpp:367
static KOKKOS_FUNCTION constexpr std::size_t size() noexcept
Definition: discrete_vector.hpp:268
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector()=default
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector(DiscreteVector const &)=default
KOKKOS_FUNCTION constexpr DiscreteVector(DVects const &... delems) noexcept
Definition: discrete_vector.hpp:281
KOKKOS_FUNCTION constexpr DiscreteVectorElement & get() noexcept
Definition: discrete_vector.hpp:331
KOKKOS_FUNCTION constexpr DiscreteVector & operator=(DiscreteVector< OTags... > &&other) noexcept
Definition: discrete_vector.hpp:312
KOKKOS_FUNCTION constexpr bool operator==(DiscreteVector< OTags... > const &rhs) const noexcept
Definition: discrete_vector.hpp:319
KOKKOS_FUNCTION constexpr DiscreteVector & operator*=(DiscreteVector< OTags... > const &rhs)
Definition: discrete_vector.hpp:435
KOKKOS_FUNCTION constexpr DiscreteVector(Params const &... params) noexcept
Definition: discrete_vector.hpp:291
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector(DiscreteVector &&)=default
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector & operator=(DiscreteVector const &other)=default
KOKKOS_FUNCTION constexpr DiscreteVector & operator+=(DiscreteVector< OTags... > const &rhs)
Definition: discrete_vector.hpp:397
Definition: aligned_allocator.hpp:11
KOKKOS_FUNCTION constexpr DiscreteVector< Tags... > operator+(DiscreteVector< Tags... > const &x)
Unary operators: +, -.
Definition: discrete_vector.hpp:75
KOKKOS_FUNCTION constexpr auto operator*(IntegralType const &lhs, DiscreteVector< Tags... > const &rhs)
external left binary operator: *
Definition: discrete_vector.hpp:164
KOKKOS_FUNCTION constexpr bool operator<(DiscreteVector< Tag > const &lhs, IntegralType const &rhs)
Definition: discrete_vector.hpp:452
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get_or(DiscreteVector< Tags... > const &tuple, DiscreteVectorElement const &default_value) noexcept
Definition: discrete_vector.hpp:65
KOKKOS_FUNCTION constexpr auto operator-(DiscreteVector< Tags... > const &lhs, DiscreteVector< OTags... > const &rhs)
Definition: discrete_vector.hpp:108
KOKKOS_FUNCTION constexpr DiscreteVectorElement & get(DiscreteVector< Tags... > &tuple) noexcept
Definition: discrete_vector.hpp:59
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator-(DiscreteVector< Tag > const &lhs, IntegralType const &rhs)
Definition: discrete_vector.hpp:143
constexpr bool is_discrete_vector_v
Definition: discrete_vector.hpp:34
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get(DiscreteVector< Tags... > const &tuple) noexcept
Definition: discrete_vector.hpp:52
KOKKOS_FUNCTION constexpr DiscreteVector< Tags... > operator-(DiscreteVector< Tags... > const &x)
Definition: discrete_vector.hpp:81
KOKKOS_FUNCTION constexpr DiscreteVector< QueryTags... > select(DiscreteVector< Tags... > const &arr) noexcept
Definition: discrete_vector.hpp:172
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator+(IntegralType const &lhs, DiscreteVector< Tag > const &rhs)
Definition: discrete_vector.hpp:135
KOKKOS_FUNCTION constexpr auto operator+(DiscreteVector< Tags... > const &lhs, DiscreteVector< OTags... > const &rhs)
Internal binary operators: +, -.
Definition: discrete_vector.hpp:89
KOKKOS_FUNCTION constexpr auto const & take(HeadDVect const &head, TailDVects const &... tail)
Returns a reference towards the DiscreteVector that contains the QueryTag.
Definition: discrete_vector.hpp:193
KOKKOS_FUNCTION constexpr bool operator<(DiscreteVector< Tag > const &lhs, DiscreteVector< Tag > const &rhs)
Definition: discrete_vector.hpp:444
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator-(IntegralType const &lhs, DiscreteVector< Tag > const &rhs)
Definition: discrete_vector.hpp:151
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator+(DiscreteVector< Tag > const &lhs, IntegralType const &rhs)
Definition: discrete_vector.hpp:127
KOKKOS_FUNCTION constexpr DiscreteVector< QueryTags... > select(DiscreteVector< Tags... > &&arr) noexcept
Definition: discrete_vector.hpp:179
Definition: discrete_vector.hpp:25