dune-fem  2.4.1-rc
utility.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_COMMON_UTILITY_HH
2 #define DUNE_FEM_COMMON_UTILITY_HH
3 
4 #include <algorithm>
5 #include <type_traits>
6 
7 
8 namespace 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 >
33  static constexpr T sum ( T a )
34  {
35  return a;
36  }
37 
38  template< class T, class ... U >
39  static constexpr T sum ( T a, U ... b )
40  {
41  return a + sum( b ... );
42  }
43 
44 
45  // sub
46  // ---
47 
48  template< class T >
49  static constexpr T sub ( T a )
50  {
51  return a;
52  }
53 
54  template< class T, class ... U >
55  static constexpr T sub ( T a, U ... b )
56  {
57  return a - sub( b ... );
58  }
59 
60 
61  // max
62  // ---
63 
64  template< class T >
65  static constexpr T max ( T a )
66  {
67  return a;
68  }
69 
70  template< class T, class ... U >
71  static constexpr T max ( T a, U ... b )
72  {
73  return a > max( b ... )? a : max( b ... );
74  }
75 
76 
77  // min
78  // ---
79 
80  template< class T >
81  static constexpr T min ( T a )
82  {
83  return a;
84  }
85 
86  template< class T, class ... U >
87  static constexpr T min ( T a, U ... b )
88  {
89  return a < min( b ... )? a : min( b ... );
90  }
91 
92 
93  // Logical operations
94 
95  // Or
96  // --
97 
98  static constexpr bool Or ( bool a )
99  {
100  return a;
101  }
102 
103  template < class ... U >
104  static constexpr bool Or ( bool a, U ... b )
105  {
106  return a || Or( b ... );
107  }
108 
109 
110  // And
111  // ---
112 
113  static constexpr bool And ( bool a )
114  {
115  return a;
116  }
117 
118  template< class B, class ... U >
119  static constexpr bool And ( B a, U ... b )
120  {
121  return a && And( b... );
122  }
123 
124 
125 
126  // are_all_same
127  // ------------
128 
129  //
130  // is true_type if all types in the parameter pack are the same.
131  // similar to std::is_same
132  //
133 
134  template< class ... T >
135  struct are_all_same;
136 
137  template< class T >
138  struct are_all_same< T > : public std::true_type {};
139 
140  template< class U, class V, class ... T >
141  struct are_all_same< U, V, T ... >
142  : public std::integral_constant< bool, std::is_same< U, V >::value &&are_all_same< V, T ... >::value >
143  {};
144 
145  } // Std
146 
147 } // namespace Dune
148 
149 #endif // #ifndef DUNE_FEM_COMMON_UTILITY_HH
static constexpr T min(T a)
Definition: utility.hh:81
Definition: utility.hh:135
static constexpr T max(T a)
Definition: utility.hh:65
static constexpr T sum(T a)
Definition: utility.hh:33
static constexpr T sub(T a)
Definition: utility.hh:49
Definition: coordinate.hh:4
static constexpr bool Or(bool a)
Definition: utility.hh:98
static constexpr bool And(bool a)
Definition: utility.hh:113