IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
tuple.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <tuple>
18#include <type_traits>
19#include <vector>
20
21namespace iganet::utils {
22
25template <class T> struct is_tuple : std::false_type {};
26
27template <class... Ts> struct is_tuple<std::tuple<Ts...>> : std::true_type {};
29
30template <class T> inline constexpr bool is_tuple_v = is_tuple<T>::value;
31
34template <class T> struct is_tuple_of_tuples : std::false_type {};
35
36template <class... Ts>
37struct is_tuple_of_tuples<std::tuple<Ts...>>
38 : std::common_type_t<is_tuple<Ts>...> {};
40
43
45template <class T>
47
50template <typename... Tuples> struct tuple_cat;
51
52template <> struct tuple_cat<> {
53 using type = std::tuple<>;
54};
55
56template <typename... Ts, typename... Tuples>
57struct tuple_cat<std::tuple<Ts...>, Tuples...> {
58 using type = decltype(std::tuple_cat(
59 std::declval<std::tuple<Ts...>>(),
60 std::declval<typename tuple_cat<Tuples...>::type>()));
61};
62
63template <typename T, typename... Tuples> struct tuple_cat<T, Tuples...> {
64 using type = decltype(std::tuple_cat(
65 std::declval<std::tuple<T>>(),
66 std::declval<typename tuple_cat<Tuples...>::type>()));
67};
69
71template <typename... Tuples> using tuple_cat_t = tuple_cat<Tuples...>::type;
72
74template <typename... Tuples>
75inline constexpr auto tuple_cat_v = tuple_cat<Tuples...>::value;
76
79template <typename... Tensors>
80torch::Tensor cat_tuple_into_tensor(const std::tuple<Tensors...> &tensors,
81 int64_t dim = 0) {
82 std::vector<torch::Tensor> vec;
83 vec.reserve(sizeof...(Tensors));
84 std::apply([&](const auto &...tensor) { (vec.emplace_back(tensor), ...); },
85 tensors);
86
87 return torch::cat(vec, dim);
88}
89
93template <typename... Tensors, typename Func>
94torch::Tensor cat_tuple_into_tensor(const std::tuple<Tensors...> &tensors,
95 Func &&func, int64_t dim = 0) {
96 std::vector<torch::Tensor> vec;
97 vec.reserve(sizeof...(Tensors));
98 std::apply(
99 [&](const auto &...tensor) {
100 (vec.emplace_back(std::invoke(func, tensor)), ...);
101 },
102 tensors);
103
104 return torch::cat(vec, dim);
105}
106
108template <std::size_t N, typename T>
109constexpr auto repeat_tuple(const T &value) {
110 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
111 return std::tuple{((void)Is, value)...};
112 }(std::make_index_sequence<N>{});
113}
114
117template <std::size_t I = 0, typename... Tensors, typename FuncSize,
118 typename FuncAssign>
119void slice_tensor_into_tuple(std::tuple<Tensors...> &tuple,
120 const torch::Tensor &tensor, FuncSize &&funcSize,
121 FuncAssign &&funcAssign, int64_t &offset,
122 int64_t dim = 0) {
123 if constexpr (I < sizeof...(Tensors)) {
124 auto &t = std::get<I>(tuple);
125 auto size = std::forward<FuncSize>(funcSize)(t);
126 std::forward<FuncAssign>(funcAssign)(
127 t, tensor.slice(dim, offset, offset + size));
128 offset += size;
129 slice_tensor_into_tuple<I + 1>(tuple, tensor, funcSize, funcAssign, offset,
130 dim);
131 }
132}
133
134template <typename... Tensors, typename FuncSize, typename FuncAssign>
135void slice_tensor_into_tuple(std::tuple<Tensors...> &tuple,
136 const torch::Tensor &tensor, FuncSize &&funcSize,
137 FuncAssign &&funcAssign, int64_t dim = 0) {
138 int64_t offset = 0;
139 slice_tensor_into_tuple(tuple, tensor, funcSize, funcAssign, offset, dim);
140}
142
143} // namespace iganet::utils
Definition blocktensor.hpp:24
std::tuple<> type
Definition tuple.hpp:53
decltype(std::tuple_cat(std::declval< std::tuple< Ts... > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:60
constexpr auto is_tuple_of_tuples_v
Alias for is_tuple_of_tuples::value.
Definition tuple.hpp:46
constexpr auto repeat_tuple(const T &value)
Returns a std::tuple object with N replications of the given value.
Definition tuple.hpp:109
void slice_tensor_into_tuple(std::tuple< Tensors... > &tuple, const torch::Tensor &tensor, FuncSize &&funcSize, FuncAssign &&funcAssign, int64_t &offset, int64_t dim=0)
Slices the given tensor into the objects of the std::tuple.
Definition tuple.hpp:119
constexpr auto tuple_cat_v
Alias for tuple_cat::value.
Definition tuple.hpp:75
torch::Tensor cat_tuple_into_tensor(const std::tuple< Tensors... > &tensors, int64_t dim=0)
Concatenates the entries of a std::tuple object into a single Torch tensor along the given dimension.
Definition tuple.hpp:80
decltype(std::tuple_cat(std::declval< std::tuple< T > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:66
tuple_cat< Tuples... >::type tuple_cat_t
Alias for tuple_cat::type.
Definition tuple.hpp:71
is_tuple_of_tuples< T >::type is_tuple_of_tuples_t
Alias for is_tuple_of_tuples::type.
Definition tuple.hpp:42
constexpr bool is_tuple_v
Definition tuple.hpp:30
Type trait for concatenating std::tuples.
Definition tuple.hpp:50
STL namespace.
Type trait for std::tuple<std::tuple> type.
Definition tuple.hpp:34
Type trait for std::tuple type.
Definition tuple.hpp:25