DUNE-ACFEM (unstable)

subtuple.hh
1#ifndef __DUNE_ACFEM_MPL_SUBTUPLE_HH__
2#define __DUNE_ACFEM_MPL_SUBTUPLE_HH__
3
4#include "access.hh"
5#include "mask.hh"
6#include "sort.hh"
7#include "filter.hh"
8#include "generators.hh"
9
10namespace Dune {
11
12 namespace ACFem {
13
34 namespace {
35 // Helper namespace for insertAt()
36
38 template<class T, class Tuple, std::size_t... Front, std::size_t... Back>
39 auto insertAtInsertOne(T t, Tuple&& tuple, std::index_sequence<Front...>, std::index_sequence<Back...>)
40 {
41 return std::make_tuple(get<Front>(tuple)..., t, get<Back+sizeof...(Front)>(tuple)...);
42 }
43
45 template<std::size_t N, class PosSeq, class DataTuple, class SrcTuple,
46 std::enable_if_t<(N == PosSeq::size()), int> = 0>
47 auto insertAtHelper(PosSeq pos, const DataTuple& data, const SrcTuple& tuple, TrueType)
48 {
49 return tuple;
50 }
51
67 template<std::size_t N, class PosSeq, class DataTuple, class SrcTuple,
68 std::enable_if_t<(N < PosSeq::size()), int> = 0>
69 auto insertAtHelper(PosSeq pos, const DataTuple& data, const SrcTuple& tuple, TrueType)
70 {
71 constexpr std::size_t size = std::tuple_size<SrcTuple>::value;
72 constexpr std::size_t index = Get<N, PosSeq>::value;
73 using Front = MakeIndexSequence<index>;
74 using Back = MakeIndexSequence<size - index>;
75
76 return insertAtHelper<N+1>(pos, data, insertAtInsertOne(get<N>(data), tuple, Front{}, Back{}), trueType());
77 }
78
79 template<std::size_t N, class PosSeq, class DataTuple, class SrcTuple>
80 auto insertAtHelper(PosSeq pos, const DataTuple& data, const SrcTuple& tuple, FalseType)
81 {
82 using Sort = SortSequence<PosSeq>;
83 using SortedPos = typename Sort::Result;
84 using Perm = typename Sort::Permutation;
85
86 return insertAtHelper<N>(SortedPos{}, permute(Perm{}, data), tuple, trueType());
87 }
88
89 }
90
103 template<std::size_t... Pos, class SrcTuple, class DataTuple, bool AssumeSorted = true,
104 // Enable for "things" like tuples. We need std::get
105 std::enable_if_t<AllowsGet<SrcTuple>::value && AllowsGet<DataTuple>::value, int> = 0>
106 auto insertAt(SrcTuple&& src, DataTuple&& data, BoolConstant<AssumeSorted> = BoolConstant<AssumeSorted>{})
107 {
108 return insertAtHelper<0>(std::index_sequence<Pos...>{}, std::forward<DataTuple>(data), std::forward<SrcTuple>(src), BoolConstant<AssumeSorted>{});
109 }
110
112 template<std::size_t Where, class SrcTuple, class Data, bool AssumeSorted = true,
113 // Enable for "things" like tuples. We need std::get
114 std::enable_if_t<AllowsGet<SrcTuple>::value && !AllowsGet<Data>::value, int> = 0>
116 {
117 return insertAtHelper<0>(IndexSequence<Where>{}, std::forward_as_tuple(data), std::forward<SrcTuple>(src), BoolConstant<AssumeSorted>{});
118 }
119
121 template<std::size_t... Pos, class SrcTuple, class DataTuple, bool AssumeSorted = true,
122 // Enable for "things" like tuples. We need std::get
123 std::enable_if_t<AllowsGet<SrcTuple>::value && AllowsGet<DataTuple>::value, int> = 0>
125 {
126 return insertAt<Pos...>(std::forward<SrcTuple>(src), std::forward<DataTuple>(data), BoolConstant<AssumeSorted>{});
127 }
128
130 template<std::size_t Where, class SrcTuple, class Data, bool AssumeSorted = true,
131 // Enable for "things" like tuples. We need std::get
132 std::enable_if_t<AllowsGet<SrcTuple>::value && !AllowsGet<Data>::value, int> = 0>
134 {
135 return insertAt<Where>(std::forward<SrcTuple>(src), std::forward<Data>(data), BoolConstant<AssumeSorted>{});
136 }
137
151 template<std::size_t... I, class TupleLike,
152 std::enable_if_t<IsTupleLike<TupleLike>::value, int> = 0>
153 auto subTuple(const TupleLike& t, IndexSequence<I...> = IndexSequence<I...>{}) {
154 return std::make_tuple(std::get<I>(t)...);
155 }
156
160 template<std::size_t... I, class TupleLike,
161 std::enable_if_t<IsTupleLike<TupleLike>::value, int> = 0>
163 return std::forward_as_tuple(std::get<I>(t)...);
164 }
165
167 template<class T, std::size_t... I>
168 using SubTuple = decltype(subTuple<I...>(std::declval<T>()));
169
185 template<std::size_t mask, typename... T>
186 auto maskTuple(const std::tuple<T...>& t, IndexConstant<mask> = IndexConstant<mask>{})
187 {
188 return subTuple(t, MaskSequence<mask>());
189 }
190
192 template<std::size_t mask, class T>
193 using MaskTuple = decltype(maskTuple<mask>(std::declval<T>()));
194
200 template<class Tuple, template<class...> class F>
202 {
203 template<class T, T, std::size_t N>
204 using Apply = F<TupleElement<N, Tuple> >;
205 };
206
223 template<template<class T, class...> class F, class Tuple>
224 auto filteredTuple(Tuple&& t)
225 {
226 using ComponentSequence = MakeSequenceFor<Tuple>;
227 using SliceSequence = FilteredSequence<HasPropertyFunctor<Tuple, F>, ComponentSequence>;
228 return forwardSubTuple(std::forward<Tuple>(t), SliceSequence{});
229 }
230
232 template<template<class T, class...> class F, class Tuple>
233 using FilteredTuple = std::decay_t<decltype(filteredTuple<F>(std::declval<Tuple>()))>;
234
236 template<class T, class Tuple, std::enable_if_t<IsTupleLike<Tuple>::value, int> = 0>
237 constexpr auto addFront(T&& t, Tuple&& tuple)
238 {
239 return std::tuple_cat(std::tuple<T>(std::forward<T>(t)), std::forward<Tuple>(tuple));
240 }
241
243 template<class T, class Tuple, std::enable_if_t<IsTupleLike<Tuple>::value, int> = 0>
244 constexpr auto pushBack(T&& t, Tuple&& tuple)
245 {
246 return std::tuple_cat(std::forward<Tuple>(tuple), std::tuple<T>(std::forward<T>(t)));
247 }
248
250 template<class T, class Tuple>
251 using AddFrontTuple = std::decay_t<decltype(addFront(std::declval<T>(), std::declval<Tuple>()))>;
252
254 template<class T, class Tuple>
255 using PushBackTuple = std::decay_t<decltype(pushBack(std::declval<T>(), std::declval<Tuple>()))>;
256
257 template<class... Tuple>
258 using TupleCat = std::decay_t<decltype(std::tuple_cat(std::declval<Tuple>()...))>;
259
261
263
265
266 } // ACFem::
267
268} // Dune::
269
270#endif // __DUNE_ACFEM_MPL_SUBTUPLE_HH__
constexpr decltype(auto) get(T &&t, IndexConstant< I >=IndexConstant< I >{})
Access to the i-the element.
Definition: access.hh:129
constexpr auto pushBack(Sequence< T, I... >, IntConstant< V >=IntConstant< V >{})
Add V at the end of the sequece.
Definition: access.hh:302
constexpr std::size_t size()
Gives the number of elements in tuple-likes and std::integer_sequence.
Definition: size.hh:73
TransformSequence< Seq, Sequence< typename Seq::value_type >, IdentityFunctor, UnaryFilterFunctor< F, typename Seq::value_type > > FilteredSequence
Create a new sequence by filtering out certain elements.
Definition: filter.hh:115
MakeIndexSequence< size< T >()> MakeSequenceFor
Make a simple index sequence of the size of the argument, which may be anything for which size<T>() e...
Definition: generators.hh:41
MakeSequence< std::size_t, N, Offset, Stride, Repeat > MakeIndexSequence
Make a sequence of std::size_t elements.
Definition: generators.hh:34
constexpr auto addFront(T &&t, Tuple &&tuple)
Add an element to the front.
Definition: subtuple.hh:237
auto subTuple(const TupleLike &t, IndexSequence< I... >=IndexSequence< I... >{})
Extract a sub-tuple by explicit indexing.
Definition: subtuple.hh:153
auto filteredTuple(Tuple &&t)
Generate a sub-tuple of the elements where F<TupleElement<N, Tuple> > is derived from std::true_type,...
Definition: subtuple.hh:224
std::decay_t< decltype(pushBack(std::declval< T >(), std::declval< Tuple >()))> PushBackTuple
Type of the tuple where a T is added to the end.
Definition: subtuple.hh:255
std::decay_t< decltype(filteredTuple< F >(std::declval< Tuple >()))> FilteredTuple
Generate the type of the return value of filteredTuple().
Definition: subtuple.hh:233
auto maskTuple(const std::tuple< T... > &t, IndexConstant< mask >=IndexConstant< mask >{})
Generate a sub-tuple with the elements at the positions where a bit in a bit-mask is set.
Definition: subtuple.hh:186
decltype(maskTuple< mask >(std::declval< T >())) MaskTuple
Define the type a sub-tuple generated by the give bit-mask.
Definition: subtuple.hh:193
auto insertAt(SrcTuple &&src, DataTuple &&data, BoolConstant< AssumeSorted >=BoolConstant< AssumeSorted >{})
Insert the elements of data at positions given by pos into src in turn.
Definition: subtuple.hh:106
auto forwardSubTuple(const TupleLike &t, IndexSequence< I... >=IndexSequence< I... >{})
Like subTuble() but forward the arguments, possibly in order to be expanded as parameters to another ...
Definition: subtuple.hh:162
std::decay_t< decltype(addFront(std::declval< T >(), std::declval< Tuple >()))> AddFrontTuple
Type of the tuple where a T is added at the front.
Definition: subtuple.hh:251
decltype(subTuple< I... >(std::declval< T >())) SubTuple
Define the type of a sub-tuple given by an index-pack.
Definition: subtuple.hh:168
constexpr auto permute(Seq, IndexSequence< Ps... >=IndexSequence< Ps... >{})
Permute the given sequence.
Definition: permutation.hh:116
Sequence< std::size_t, V... > IndexSequence
Sequence of std::size_t values.
Definition: types.hh:64
Constant< bool, V > BoolConstant
Short-cut for integral constant of type bool.
Definition: types.hh:48
Constant< std::size_t, V > IndexConstant
Short-cut for integral constant of type std::size_t.
Definition: types.hh:44
BoolConstant< false > FalseType
Alias for std::false_type.
Definition: types.hh:110
BoolConstant< true > TrueType
Alias for std::true_type.
Definition: types.hh:107
constexpr auto trueType()
Generate a TrueType.
Definition: types.hh:113
Definition: subtuple.hh:202
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)