NGSolve  5.3
templates.hpp
1 #ifndef FILE_NGSTD_TEMPLATES
2 #define FILE_NGSTD_TEMPLATES
3 
4 /*********************************************************************/
5 /* File: templates.hpp */
6 /* Author: Joachim Schoeberl */
7 /* Date: 25. Mar. 2000 */
8 /*********************************************************************/
9 
10 namespace ngstd
11 {
12 
14 template <class T>
15 INLINE T min2 (T a, T b)
16 {
17  return (a < b) ? a : b;
18 }
19 
21 template <class T>
22 INLINE T max2 (T a, T b)
23 {
24  return (a > b) ? a : b;
25 }
26 
28 template <class T>
29 INLINE T min3 (T a, T b, T c)
30 {
31  return (a < b) ? (a < c) ? a : c
32  : (b < c) ? b : c;
33 }
34 
36 template <class T>
37 INLINE T max3 (T a, T b, T c)
38 {
40  return (a > b) ? ((a > c) ? a : c)
41  : ((b > c) ? b : c);
42 }
43 
44 
46 template <class T>
47 INLINE int sgn (T a)
48 {
49  return (a > 0) ? 1 : ( ( a < 0) ? -1 : 0 );
50 }
51 
53 template <class T>
54 INLINE T sqr (const T a)
55 {
56  return a * a;
57 }
58 
60 template <class T>
61 INLINE T pow3 (const T a)
62 {
63  return a * a * a;
64 }
65 
66 
67 template <class T>
68 void SaveBin (ostream & ost, const T & val)
69 {
70  const char * cp = reinterpret_cast<const char*> (&val);
71  for (unsigned j = 0; j < sizeof(T); j++)
72  ost.put(cp[j]);
73 }
74 
75 
76 template <class T>
77 void LoadBin (istream & ist, T & val)
78 {
79  char * cp = reinterpret_cast<char*> (&val);
80  for (unsigned j = 0; j < sizeof(T); j++)
81  ist.get(cp[j]);
82 }
83 
84 
85 
86 
87 
88  /*
89 template <int NUM>
90 class Cl_Iterate
91 {
92 public:
93  template <typename FUNC>
94  static INLINE void Do (FUNC f)
95  {
96  Cl_Iterate<NUM-1>::Do(f);
97  f(IC<NUM>());
98  }
99 };
100 
101 template <>
102 class Cl_Iterate<-1>
103 {
104 public:
105  template <typename FUNC>
106  static INLINE void Do (FUNC f) { }
107 };
108 
109 template <>
110 class Cl_Iterate<0>
111 {
112 public:
113  template <typename FUNC>
114  static INLINE void Do (FUNC f) { f(IC<0>()); }
115 };
116 
117 template <int NUM, typename FUNC>
118 INLINE void Iterate (FUNC f)
119 {
120  Cl_Iterate<NUM-1>::Do(f);
121 }
122  */
123 
124 
125 template <int NUM, typename FUNC>
126 INLINE void Iterate (FUNC f)
127 {
128  if constexpr (NUM > 1) Iterate<NUM-1> (f);
129  if constexpr (NUM >= 1) f(IC<NUM-1>());
130 }
131 
132 
133 
134 
135 
136 
137 template <int NUM>
139 {
140 public:
141  template <typename FUNC>
142  static INLINE void Do (size_t nr, FUNC f)
143  {
144  if (nr == NUM)
145  f(IC<NUM>());
146  else
147  Cl_Switch<NUM-1>::Do(nr, f);
148  }
149 };
150 
151 template <>
152 class Cl_Switch<-1>
153 {
154 public:
155  template <typename FUNC>
156  static INLINE void Do (size_t /* nr */, FUNC /* f */) { }
157 };
158 
159 template <>
160 class Cl_Switch<0>
161 {
162 public:
163  template <typename FUNC>
164  static INLINE void Do (size_t /* nr */, FUNC f)
165  {
166  // if (nr == 0)
167  f(IC<0>());
168  }
169 };
170 
171 template <int NUM, typename FUNC>
172 INLINE void Switch (size_t nr, FUNC f)
173 {
174  Cl_Switch<NUM-1>::Do(nr, f);
175 }
176 
177 
178 
179 
180 
181 }
182 
183 #endif
ngstd::Cl_Switch
Definition: templates.hpp:138
ngstd::min2
INLINE T min2(T a, T b)
min of 2 values
Definition: templates.hpp:15
ngstd::max3
INLINE T max3(T a, T b, T c)
max of 3 values
Definition: templates.hpp:37
ngstd::max2
INLINE T max2(T a, T b)
max of 2 values
Definition: templates.hpp:22
ngstd::sgn
INLINE int sgn(T a)
sign of value (+1, 0, -1)
Definition: templates.hpp:47
ngstd
namespace for standard data types and algorithms.
Definition: ngstd.hpp:55
ngstd::min3
INLINE T min3(T a, T b, T c)
min of 3 values
Definition: templates.hpp:29
ngstd::sqr
INLINE AutoDiffVec< D, SCAL > sqr(const AutoDiffVec< D, SCAL > &x)
AutoDiffVec times AutoDiffVec.
Definition: autodiff.hpp:251
ngstd::pow3
INLINE T pow3(const T a)
element to the third power
Definition: templates.hpp:61