DUNE-ACFEM (unstable)

functor.hh
1#ifndef __DUNE_ACFEM_FUNCTOR_HH__
2#define __DUNE_ACFEM_FUNCTOR_HH__
3
4#include <cstddef>
5#include <utility>
6#include <tuple>
7
8namespace Dune {
9
10 namespace ACFem {
11
20 template<class LocalArray, class GlobalArray>
22 {
23 explicit ExtractFunctor(LocalArray& localArray,
24 const GlobalArray& globalArray)
25 : localArray_(localArray), globalArray_(globalArray)
26 {}
27
28 template<class GlobalKey>
29 void operator()(const std::size_t local, const GlobalKey &global) const
30 {
31 localArray_[local] = globalArray_[global];
32 }
33
34 private:
35 LocalArray& localArray_;
36 const GlobalArray& globalArray_;
37 };
38
43 template<class T, class GlobalArray>
44 struct ExtractFunctor<T *, GlobalArray>
45 {
46 explicit ExtractFunctor(T *localArray,
47 const GlobalArray& globalArray)
48 : localArray_(localArray), globalArray_(globalArray)
49 {}
50
51 template<class GlobalKey>
52 void operator()(const std::size_t local, const GlobalKey &global) const
53 {
54 localArray_[local] = globalArray_[global];
55 }
56
57 private:
58 T* localArray_;
59 const GlobalArray& globalArray_;
60 };
61
65 template<class FunctorOne, class FunctorTwo>
67 {
68 explicit PairFunctor(FunctorOne&& one, FunctorTwo&& two)
69 : one_(one), two_(two)
70 {}
71
72 template<class Value>
73 void operator()(const std::size_t local, const Value& value) const
74 {
75 one_(local, value);
76 two_(local, value);
77 }
78
79 private:
80 FunctorOne& one_;
81 FunctorTwo& two_;
82 };
83
90 template<class FunctorOne, class FunctorTwo>
91 PairFunctor<FunctorOne, FunctorTwo> makePairFunctor(FunctorOne&& func1, FunctorTwo&& func2)
92 {
93 return PairFunctor<FunctorOne, FunctorTwo>(std::forward<FunctorOne>(func1),
94 std::forward<FunctorTwo>(func2));
95 }
96
101 template<class... Types>
103 {
104 typedef std::tuple<Types&...> TupleType;
105
106 TupleFunctor(const TupleType& arg)
107 : functorTuple_(arg)
108 {}
109
110 template<class Value>
111 void operator()(const std::size_t local, const Value& value) const
112 {
113 FunctorHelper<TupleType, std::tuple_size<TupleType>::value-1>::apply(
114 functorTuple_, local, value);
115 }
116
117 private:
118 template<class Tuple, unsigned index>
119 struct FunctorHelper
120 {
121 template<class Value>
122 static void apply(const Tuple& tuple, const std::size_t local, const Value& value)
123 {
124 FunctorHelper<Tuple, index - 1>::apply(tuple, local, value);
125 std::get<index>(tuple)(local, value);
126 }
127 };
128 template<class Tuple>
129 struct FunctorHelper<Tuple, 0>
130 {
131 template<class Value>
132 static void apply(const Tuple& tuple, const std::size_t local, const Value& value)
133 {
134 std::get<0>(tuple)(local, value);
135 }
136 };
137
138 const TupleType functorTuple_;
139 };
140
145 template<class... Types>
146 TupleFunctor<Types...> makeTupleFunctor(Types&&... args)
147 {
148 //return TupleFunctor<Types...>(std::tuple<Types&&...>(std::forward<Types>(args)...));
149 return TupleFunctor<Types...>(std::forward_as_tuple(std::forward<Types>(args)...));
150 }
151
153
154 } // ACFem::
155
156} // Dune::
157
158#endif // __DUNE_ACFEM_FUNCTOR_HH__
TupleFunctor< Types... > makeTupleFunctor(Types &&... args)
Combine several functors into one.
Definition: functor.hh:146
PairFunctor< FunctorOne, FunctorTwo > makePairFunctor(FunctorOne &&func1, FunctorTwo &&func2)
Combine two functors into one.
Definition: functor.hh:91
constexpr auto one(T &&t)
Use the one fraction as canonical zero element for scalars.
Definition: constantoperations.hh:88
A functor which extracts values from a global array and copies them to a local array.
Definition: functor.hh:22
Generate a compound functor out of two functors.
Definition: functor.hh:67
Generate a compound functor out of a tuple of functors.
Definition: functor.hh:103
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)