Skip to content

Commit

Permalink
Merge pull request #872 from acts-project/feat/more_tuples
Browse files Browse the repository at this point in the history
Add more `detray::tuple` features
  • Loading branch information
stephenswat authored Oct 28, 2024
2 parents a05a161 + 56cc6d8 commit d6bc161
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions core/include/detray/utils/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,27 @@ struct tuple<T, Ts...> {

DETRAY_HOST_DEVICE constexpr tuple(){};

constexpr tuple(const tuple &o) requires(
std::is_copy_constructible_v<T> &&
(std::is_copy_constructible_v<Ts> && ...)) = default;

template <typename U, typename... Us>
requires std::is_constructible_v<T, U &&>
&&std::is_constructible_v<tuple<Ts...>, Us &&...>
DETRAY_HOST_DEVICE explicit constexpr tuple(
const tuple<U, Us...> &o)
: v(o.v), r(o.r) {}

constexpr tuple(tuple &&o) noexcept
requires(std::is_move_constructible_v<T> &&
(std::is_move_constructible_v<Ts> && ...)) = default;

template <typename U, typename... Us>
requires std::is_constructible_v<T, U &&>
&&std::is_constructible_v<tuple<Ts...>, Us &&...>
DETRAY_HOST_DEVICE explicit constexpr tuple(tuple<U, Us...> &&o)
: v(std::move(o.v)), r(std::move(o.r)) {}

template <typename U, typename... Us>
requires std::is_constructible_v<T, U &&>
&&std::is_constructible_v<tuple<Ts...>, Us &&...> &&
Expand All @@ -40,10 +54,43 @@ struct tuple<T, Ts...> {
: v(std::forward<U>(_v)),
r(std::forward<Us>(_r)...) {}

constexpr ~tuple() noexcept = default;

constexpr tuple &operator=(const tuple &other) requires(
std::is_copy_assignable_v<T> &&
(std::is_copy_assignable_v<Ts> && ...)) = default;

template <typename U, typename... Us>
DETRAY_HOST_DEVICE constexpr tuple &
operator=(const tuple<U, Us...> &other) requires(
std::is_assignable_v<T &, const U &> &&
(std::is_assignable_v<Ts &, const Us &> && ...)) {
v = other.v;
r = other.r;
return *this;
}

constexpr tuple &operator=(tuple &&other) noexcept
requires(std::is_move_assignable_v<T> &&
(std::is_move_assignable_v<Ts> && ...)) = default;

template <typename U, typename... Us>
DETRAY_HOST_DEVICE constexpr tuple &operator=(
tuple<U, Us...> &&other) requires(std::is_assignable_v<T &, U> &&
(std::is_assignable_v<Ts &, Us> &&
...)) {
v = std::move(other.v);
r = std::move(other.r);
return *this;
}

T v;
tuple<Ts...> r;
};

template <typename T1, typename T2>
using pair = tuple<T1, T2>;

template <std::size_t I, typename... Ts>
DETRAY_HOST_DEVICE const auto &get(const detray::tuple<Ts...> &t) noexcept {
static_assert(I < sizeof...(Ts),
Expand Down Expand Up @@ -96,4 +143,17 @@ template <typename... Ts>
DETRAY_HOST_DEVICE constexpr detray::tuple<Ts &...> tie(Ts &... args) {
return detray::tuple<Ts &...>(args...);
}

template <typename... Ts>
DETRAY_HOST_DEVICE constexpr detray::tuple<std::decay_t<Ts>...> make_tuple(
Ts &&... vs) {
return detray::tuple<std::decay_t<Ts>...>(std::forward<Ts>(vs)...);
}

template <typename T1, typename T2>
DETRAY_HOST_DEVICE constexpr detray::pair<std::decay_t<T1>, std::decay_t<T2>>
make_pair(T1 &&v1, T2 &&v2) {
return detray::pair<std::decay_t<T1>, std::decay_t<T2>>(
std::forward<T1>(v1), std::forward<T2>(v2));
}
} // namespace detray

0 comments on commit d6bc161

Please sign in to comment.