DUNE-FEM (unstable)

utility.hh
1#ifndef DUNE_FEM_COMMON_UTILITY_HH
2#define DUNE_FEM_COMMON_UTILITY_HH
3
4#include <algorithm>
5#include <type_traits>
6
7
8namespace Dune
9{
10
11 namespace Std
12 {
13
14 //
15 // Set of operations which can performed for an arbitrary number of arguments.
16 // Examples:
17 //
18 // sum( 5, 6, 12, .... )
19 // And( true, true, false, ... )
20 //
21 // or for constant expressions if i... is an integer sequence:
22 //
23 // sum( std::tuple_element< i, Tuple >::type::value ... )
24 //
25
26
27 // Arithmetical operations
28
29 // sum
30 // ---
31
32 template< class T, std::enable_if_t< std::is_arithmetic< std::decay_t< T > >::value, int > = 0 >
33 static constexpr std::decay_t< T > sum ( T a )
34 {
35 return a;
36 }
37
38 template< class T, T a >
39 static constexpr std::decay_t< T > sum ( std::integral_constant< T, a > )
40 {
41 return a;
42 }
43
44 template< class T, std::enable_if_t< std::is_enum< std::decay_t< T > >::value, int > = 0 >
45 static constexpr std::underlying_type_t< std::decay_t< T > > sum ( T a )
46 {
47 return a;
48 }
49
50 template< class T, class ... U >
51 static constexpr auto sum ( T a, U ... b )
52 {
53 return a + sum( b ... );
54 }
55
56
57 // sub
58 // ---
59
60 template< class T >
61 static constexpr T sub ( T a )
62 {
63 return a;
64 }
65
66 template< class T, class ... U >
67 static constexpr T sub ( T a, U ... b )
68 {
69 return a - sub( b ... );
70 }
71
72
73 // max
74 // ---
75
76 template< class T >
77 static constexpr T max ( T a )
78 {
79 return a;
80 }
81
82 template< class T, class ... U >
83 static constexpr T max ( T a, U ... b )
84 {
85 return a > max( b ... )? a : max( b ... );
86 }
87
88
89 // min
90 // ---
91
92 template< class T >
93 static constexpr T min ( T a )
94 {
95 return a;
96 }
97
98 template< class T, class ... U >
99 static constexpr T min ( T a, U ... b )
100 {
101 return a < min( b ... )? a : min( b ... );
102 }
103
104
105 // Logical operations
106
107 // Or
108 // --
109
110 static constexpr bool Or ()
111 {
112 return false;
113 }
114
115 template < class ... U >
116 static constexpr bool Or ( bool a, U ... b )
117 {
118 return a || Or( b ... );
119 }
120
121
122 // And
123 // ---
124
125 static constexpr bool And ()
126 {
127 return true;
128 }
129
130 template< class B, class ... U >
131 static constexpr bool And ( B a, U ... b )
132 {
133 return a && And( b... );
134 }
135
136
137
138 // are_all_same
139 // ------------
140
141 //
142 // is true_type if all types in the parameter pack are the same.
143 // similar to std::is_same
144 //
145
146 template< class ... T >
147 struct are_all_same;
148
149 template< class T >
150 struct are_all_same< T > : public std::true_type {};
151
152 template< class U, class V, class ... T >
153 struct are_all_same< U, V, T ... >
154 : public std::integral_constant< bool, std::is_same< U, V >::value &&are_all_same< V, T ... >::value >
155 {};
156
157 // std::is_pod got deprecated
158
159 template<class T>
160 struct is_pod
161 : public std::integral_constant<bool, std::is_standard_layout<T>::value && std::is_trivial<T>::value>
162 {};
163
164 } // Std
165
166 namespace Fem
167 {
168
169 namespace detail {
170
172 template <class Obj, int defaultValue = -1 >
174 {
175 private:
176 template <typename T, typename = int>
177 struct CheckPointSetId : public std::false_type { };
178
179 template <typename T>
180 struct CheckPointSetId<T, decltype((void) T::pointSetId, 0)> : public std::true_type { };
181
182 template <class T, bool>
183 struct SelectValue { static const int value = defaultValue; };
184
185 template <class T>
186 struct SelectValue< T, true > { static const int value = T::pointSetId; };
187 public:
188 static constexpr int value = SelectValue< Obj, CheckPointSetId< Obj >::value >::value;
189 };
190
192 template <class SFS, bool defaultValue = false >
194 {
195 private:
196 template <typename T, typename = int>
197 struct CheckCodegenSFS : public std::false_type { };
198
199 template <typename T>
200 struct CheckCodegenSFS<T, decltype((void) T::codegenShapeFunctionSet, 0)> : public std::true_type { };
201
202 template <class T, bool>
203 struct SelectValue { static const bool value = defaultValue; };
204
205 template <class T>
206 struct SelectValue< T, true > { static const bool value = T::codegenShapeFunctionSet; };
207 public:
208 static constexpr int value = SelectValue< SFS, CheckCodegenSFS< SFS >::value >::value;
209 };
210 } // end namespace detail
211 } // end namespace Fem
212
213} // namespace Dune
214
215#endif // #ifndef DUNE_FEM_COMMON_UTILITY_HH
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
Dune namespace.
Definition: alignedallocator.hh:13
selects SFS::codegenShapeFunctionSet if available, otherwise defaultValue (default is false)
Definition: utility.hh:194
selects Obj::pointSetId if available, otherwise defaultValue (default is -1)
Definition: utility.hh:174
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)