DUNE-ACFEM (unstable)

transform.hh
1#ifndef __DUNE_ACFEM_MPL_TRANSFORM_HH__
2#define __DUNE_ACFEM_MPL_TRANSFORM_HH__
3
4#include "mask.hh"
5#include "generators.hh"
6
7namespace Dune {
8
9 namespace ACFem {
10
31 // forward
32 struct IdentityFunctor;
33
34 // forward
35 struct AcceptAllFunctor;
36
37 // forward
38 template<class F, std::size_t... Index>
39 struct TransformAtIndexFunctor;
40
41 // forward
42 template<bool acceptEqual>
43 struct AcceptEqualFunctor;
44
45 namespace {
46 // helpers for TransformSequence template.
47
48 // Forward, specialized below.
49 template<class SeqIn,
50 class SeqOut,
51 class TransformFunctor,
52 class AcceptFunctor,
53 std::size_t N,
54 class SFINAE = void>
55 struct TransformSequenceRecursion;
56
60 template<class Tin, Tin I0, Tin... RestIn, class Tout, Tout... Out,
61 class F, class A, std::size_t N>
62 struct TransformSequenceRecursion<
63 Sequence<Tin, I0, RestIn...>,
64 Sequence<Tout, Out...>,
65 F, A, N,
66 std::enable_if_t<A::template Apply<Tin, I0, Tout, F::template Apply<Tin, I0, N>::value, N>::value> >
67 {
68 static const Tout V = F::template Apply<Tin, I0, N>::value;
69 using Type = typename TransformSequenceRecursion<Sequence<Tin, RestIn...>, Sequence<Tout, Out..., V>, F, A, N+1>::Type;
70 };
71
73 template<class Tin, Tin I0, Tin... RestIn, class Tout, Tout... Out,
74 class F, class A, std::size_t N>
75 struct TransformSequenceRecursion<
76 Sequence<Tin, I0, RestIn...>,
77 Sequence<Tout, Out...>,
78 F, A, N,
79 std::enable_if_t<!A::template Apply<Tin, I0, Tout, F::template Apply<Tin, I0, N>::value, N>::value> >
80 {
81 using Type = typename TransformSequenceRecursion<Sequence<Tin, RestIn...>, Sequence<Tout, Out...>, F, A, N+1>::Type;
82 };
83
85 template<class Tin, class Tout, Tout... Out, class F, class A, std::size_t N>
86 struct TransformSequenceRecursion<Sequence<Tin>, Sequence<Tout, Out...>, F, A, N>
87 {
88 using Type = Sequence<Tout, Out...>;
89 };
90
92 template<class SeqIn,
93 class SeqOut,
94 class TransformFunctor,
95 class AcceptFunctor,
96 class SFINAE = void>
97 struct TransformSequenceHelper
98 {
99 using Type = typename TransformSequenceRecursion<SeqIn, SeqOut, TransformFunctor, AcceptFunctor, 0UL>::Type;
100 };
101
103 template<class SeqIn, class SeqOutt, class TransformFunctor, class IndexSeq>
104 struct TransformSequenceExpander;
105
107 template<class Tin, Tin... In, class Tout, Tout... Out, class TransformFunctor, std::size_t... N>
108 struct TransformSequenceExpander<
109 Sequence<Tin, In...>, Sequence<Tout, Out...>, TransformFunctor, IndexSequence<N...> >
110 {
111 using Type = Sequence<Tout, Out..., TransformFunctor::template Apply<Tin, In, N>::value...>;
112 };
113
115 template<class Tin, Tin... In,
116 class SeqOut,
117 class TransformFunctor>
118 struct TransformSequenceHelper<
119 Sequence<Tin, In...>, SeqOut, TransformFunctor, AcceptAllFunctor>
120 {
121 using Type = typename TransformSequenceExpander<Sequence<Tin, In...>, SeqOut, TransformFunctor, MakeIndexSequence<sizeof...(In)> >::Type;
122 };
123
126 template<class Tin, class SeqOut, class TransformFunctor>
127 struct TransformSequenceHelper<Sequence<Tin>, SeqOut, TransformFunctor, AcceptAllFunctor>
128 {
129 using Type = SeqOut;
130 };
131
132 }
133
168 template<class SeqIn,
169 class SeqOut = Sequence<typename SeqIn::value_type>,
170 class TransformFunctor = IdentityFunctor,
171 class AcceptFunctor = AcceptAllFunctor>
172 using TransformSequence = typename TransformSequenceHelper<SeqIn, SeqOut, TransformFunctor, AcceptFunctor>::Type;
173
175 template<class Seq1, class Seq2>
177
178 namespace {
179 template<class... S>
180 struct SequenceCatHelper;
181
182 template<class S>
183 struct SequenceCatHelper<S>
184 {
185 using Type = S;
186 };
187
188 template<class S1, class S2, class... Rest>
189 struct SequenceCatHelper<S1, S2, Rest...>
190 {
191 using Type = TransformSequence<typename SequenceCatHelper<S2, Rest...>::Type, S1>;
192 };
193
194 template<class... S>
195 struct SequenceCatHelper2;
196
197 template<>
198 struct SequenceCatHelper2<>
199 {
200 using Type = IndexSequence<>;
201 };
202
203 template<class S>
204 struct SequenceCatHelper2<S>
205 {
206 using Type = S;
207 };
208
209 template<class I1, I1... Heads, class I2, I2... Tails, class... S>
210 struct SequenceCatHelper2<Sequence<I1, Heads...>, Sequence<I2, Tails...>, S...>
211 {
212 using IType = std::decay_t<decltype(std::declval<I1>()*std::declval<I2>())>;
213 using Type = typename SequenceCatHelper2<Sequence<IType, Heads..., Tails...>, S...>::Type;
214 };
215 }
216
218 template<class... S>
219 // using SequenceCat = typename SequenceCatHelper<S...>::Type;
220 using SequenceCat = typename SequenceCatHelper2<S...>::Type;
221
223 template<std::ptrdiff_t Offset, class T, T... Ts>
224 constexpr auto offsetSequence(Sequence<T, Ts...> = Sequence<T, Ts...>{}, IntConstant<Offset> = IntConstant<Offset>{})
225 {
226 return Sequence<T, (Ts+Offset)...>{};
227 }
228
229 template<std::ptrdiff_t Offset, class Seq>
230 using OffsetSequence = std::decay_t<decltype(offsetSequence<Offset>(Seq{}))>;
231
232 namespace {
233 template<std::size_t N, class Seq, class Result>
234 struct SequenceProdHelper;
235
236 template<std::size_t N, class T, T... I, T... Out>
237 struct SequenceProdHelper<N, Sequence<T, I...>, Sequence<T, Out...> >
238 {
239 using Type = typename SequenceProdHelper<N-1, Sequence<T, I...>, Sequence<T, Out..., I...> >::Type;
240 };
241
242 template<class T, T... I, T... Out>
243 struct SequenceProdHelper<0, Sequence<T, I...>, Sequence<T, Out...> >
244 {
245 using Type = Sequence<T, Out...>;
246 };
247
248 }
249
251 template<std::size_t N, class Seq>
252 using SequenceProd = typename SequenceProdHelper<N, Seq, Sequence<typename Seq::value_type> >::Type;
253
254 namespace {
255 template<std::size_t, class>
256 struct SequenceSpliceHelper;
257
258 template<std::size_t N, class T, T... V>
259 struct SequenceSpliceHelper<N, Sequence<T, V...> >
260 {
262 };
263 }
264
266 template<std::size_t count, class Seq>
267 using SequenceSplice = typename SequenceSpliceHelper<count, Seq>::Type;
268
272 template<class F, class Seq, class Tout = typename F::template Apply<typename Seq::value_type, 0, 0>::value_type>
274
278 //template<std::size_t Index, class F, class Seq, class Tout = typename F::template Apply<Index, typename Seq::value_type, Index>::value_type>
279
281 template<class F, class Seq>
283
288 template<class F, class Seq>
290
294 template<class F, class Seq>
296
301 template<class F, class Seq, std::size_t... Index>
303
316 {
317 template<class T, T V, std::size_t... N>
318 struct Apply
319 : Constant<T, V>
320 {};
321 };
322
324 template<std::size_t Stride, std::ptrdiff_t Offset = 0>
326 {
327 template<class T, T V, std::size_t... N>
328 struct Apply
329 : Constant<T, V*Stride+Offset>
330 {};
331 };
332
334 template<std::ptrdiff_t Offset>
336
338 template<std::size_t Stride>
340
346 {
347 template<class T, T mask, std::size_t N>
348 using Apply = Constant<T, N>;
349 };
350
355 template<class F, std::size_t... Index>
357 {
358 template<std::size_t N, class SFINAE = void>
359 struct ApplyHelper
360 {
361 template<class T, T V>
362 using Apply = typename IdentityFunctor::template Apply<T, V>;
363 };
364
365 template<std::size_t N>
366 struct ApplyHelper<N, std::enable_if_t<ContainsValue<IndexSequence<Index...>, N>::value> >
367 {
368 template<class T, T V>
369 using Apply = typename F::template Apply<T, V, IndexIn<IndexSequence<Index...>, N>::value>;
370 };
371
372 template<class T, T V, std::size_t N>
373 using Apply = typename ApplyHelper<N>::template Apply<T, V>;
374 };
375
378 {
379 template<class T, T V, std::size_t... N>
380 struct Apply
381 : BoolConstant<(V & 1) == 0>
382 {};
383 };
384
387 {
388 template<class T, T V, std::size_t... N>
389 struct Apply
390 : BoolConstant<(V & 1) != 0>
391 {};
392 };
393
395 template<std::size_t Bit>
397 {
398 template<class T, T mask, std::size_t... N>
399 struct Apply
400 : Constant<T, (mask | (1UL << Bit))>
401 {};
402 };
403
405 template<std::size_t Bit, std::size_t Shift>
406 struct AddBitShiftRightFunctor
407 {
408 template<class T, T mask, std::size_t... N>
409 struct Apply
410 : Constant<T, mask | ((mask & SequenceMask<Bit>::value) >> Shift)>
411 {};
412 };
413
415 template<std::size_t Bit, std::size_t Shift>
417 {
418 template<class T, T mask, std::size_t... N>
419 struct Apply
420 : Constant<T, (mask | (mask & SequenceMask<Bit>::value) << Shift)>
421 {};
422 };
423
427 template<class Values>
428 struct MapSequenceFunctor
429 {
430 template<class T, T V, std::size_t... N>
431 struct Apply
432 : Constant<T, Get<V, Values>::value>
433 {};
434 };
435
437 template<class I>
438 struct MapSequenceFunctor<Sequence<I> >
439 : IdentityFunctor
440 {};
441
443 template<class Values>
444 struct InverseMapSequenceFunctor
445 {
446 template<class T, T V, std::size_t... N>
447 struct Apply
448 : Constant<T, IndexIn<Values, V>::value>
449 {};
450 };
451
463 struct AcceptAllFunctor
464 {
465 template<class Tin, Tin, class Tout, Tout, std::size_t... N>
466 struct Apply
467 : TrueType
468 {};
469 };
470
474 template<bool acceptEqual>
475 struct AcceptEqualFunctor
476 {
477 template<class Tin, Tin V, class Tout, Tout FV, std::size_t... N>
478 struct Apply
479 : BoolConstant<(V == FV) == acceptEqual>
480 {};
481 };
482
486 template<class T, T GivenValue, bool acceptEqual = true>
487 struct AcceptInputValueFunctor
488 {
489 template<class Tin, Tin V, class Tout, Tout FV, std::size_t... N>
490 struct Apply
491 : BoolConstant<(V == GivenValue) == acceptEqual>
492 {};
493 };
494
499 template<class T, T Begin, T End>
500 struct AcceptInputInRangeFunctor
501 {
502 template<class Tin, Tin V, class Tout, Tout FV, std::size_t... N>
503 struct Apply
504 : BoolConstant<(V >= Begin && V < End)>
505 {};
506 };
507
509 template<class T, T P, T R>
510 struct AcceptModuloFunctor
511 {
512 template<class Tin, Tin V, class Tout, Tout FV, std::size_t... N>
513 struct Apply
514 : BoolConstant<(V % P == R)>
515 {};
516 };
517
521
523
525
526 } // ACFem::
527
528} // Dune::
529
530#endif // __DUNE_ACFEM_MPL_MASK_HH__
IndexConstant< IndexInHelper< 0, typename Seq::value_type, V, Seq >::value > IndexIn
Find the index of a given value in a sequence.
Definition: access.hh:279
MakeSequence< std::size_t, N, Offset, Stride, Repeat > MakeIndexSequence
Make a sequence of std::size_t elements.
Definition: generators.hh:34
TransformSequence< Seq, Sequence< Tout >, F > TransformedSequence
Transform given sequence.
Definition: transform.hh:273
typename SequenceCatHelper2< S... >::Type SequenceCat
Concatenate the given sequences, in order, to <S0, S1, ... >.
Definition: transform.hh:220
TransformSequence< Seq1, Seq2 > CatSequence
Concatenate the given sequences to <Seq2, Seq1>
Definition: transform.hh:176
TransformSequence< Seq, Seq, F > CatTransformedSequence
Transform only the element at the given position of the given sequence.
Definition: transform.hh:282
typename SequenceSpliceHelper< count, Seq >::Type SequenceSplice
Splice the sequence by repeating each value the given amount of times.
Definition: transform.hh:267
TransformSequence< Seq, Sequence< typename Seq::value_type >, TransformAtIndexFunctor< F, Index... >, AcceptAllFunctor > TransformOnly
Transform only those elements specified by the parameter pack Index.
Definition: transform.hh:302
TransformSequence< Seq, Seq, F, AcceptEqualFunctor< false > > CatTransformUnique
Concat the result of TransformUnique with the original sequence.
Definition: transform.hh:295
typename TransformSequenceHelper< SeqIn, SeqOut, TransformFunctor, AcceptFunctor >::Type TransformSequence
General sequence transformation alias.
Definition: transform.hh:172
constexpr auto offsetSequence(Sequence< T, Ts... >=Sequence< T, Ts... >{}, IntConstant< Offset >=IntConstant< Offset >{})
Simple offset operation without the overhead of TransformSequence.
Definition: transform.hh:224
typename SequenceProdHelper< N, Seq, Sequence< typename Seq::value_type > >::Type SequenceProd
Compute the "product" multi index of N identical copies.
Definition: transform.hh:252
TransformSequence< Seq, Sequence< typename Seq::value_type >, F, AcceptEqualFunctor< false > > TransformUnique
Apply the functor F::template value_type<I>::value to each element in Seq; add to the output/sequence...
Definition: transform.hh:289
Sequence< std::size_t, V... > IndexSequence
Sequence of std::size_t values.
Definition: types.hh:64
integral_constant< T, V > Constant
Short-cut for any integral constant.
Definition: types.hh:40
Constant< bool, V > BoolConstant
Short-cut for integral constant of type bool.
Definition: types.hh:48
std::integer_sequence< T, V... > Sequence
Sequence of any type of integer values.
Definition: types.hh:56
STL namespace.
Functor for TransformSequence: unconditionally accept.
Definition: transform.hh:464
Functor for TransformSequence: accept if result is either equal or different from original value.
Definition: transform.hh:476
Add given bit to all masks.
Definition: transform.hh:397
Add a set bit at left-shifted position.
Definition: transform.hh:417
Identity functor.
Definition: transform.hh:316
Index functor.
Definition: transform.hh:346
Transform to bool sequence of even indices.
Definition: transform.hh:378
Transform to bool sequence of even indices.
Definition: transform.hh:387
Multiply-offset functor.
Definition: transform.hh:326
Applies another functor only to the elements at the specified positions given by the Index parameter ...
Definition: transform.hh:357
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)