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 
90 template <int NUM>
92 {
93 public:
94  template <typename FUNC>
95  static INLINE void Do (FUNC f)
96  {
98  f(IC<NUM>());
99  }
100 };
101 
102 template <>
103 class Cl_Iterate<-1>
104 {
105 public:
106  template <typename FUNC>
107  static INLINE void Do (FUNC f) { }
108 };
109 
110 template <>
111 class Cl_Iterate<0>
112 {
113 public:
114  template <typename FUNC>
115  static INLINE void Do (FUNC f) { f(IC<0>()); }
116 };
117 
118 template <int NUM, typename FUNC>
119 INLINE void Iterate (FUNC f)
120 {
122 }
123 
124 
125 
126 
127 
128 
129 template <int NUM>
131 {
132 public:
133  template <typename FUNC>
134  static INLINE void Do (size_t nr, FUNC f)
135  {
136  if (nr == NUM)
137  f(IC<NUM>());
138  else
139  Cl_Switch<NUM-1>::Do(nr, f);
140  }
141 };
142 
143 template <>
144 class Cl_Switch<-1>
145 {
146 public:
147  template <typename FUNC>
148  static INLINE void Do (size_t nr, FUNC f) { }
149 };
150 
151 template <>
152 class Cl_Switch<0>
153 {
154 public:
155  template <typename FUNC>
156  static INLINE void Do (size_t nr, FUNC f)
157  {
158  // if (nr == 0)
159  f(IC<0>());
160  }
161 };
162 
163 template <int NUM, typename FUNC>
164 INLINE void Switch (size_t nr, FUNC f)
165 {
166  Cl_Switch<NUM-1>::Do(nr, f);
167 }
168 
169 
170 
171 
172 
173 }
174 
175 #endif
INLINE T min3(T a, T b, T c)
min of 3 values
Definition: templates.hpp:29
INLINE T max2(T a, T b)
max of 2 values
Definition: templates.hpp:22
Definition: templates.hpp:130
INLINE T max3(T a, T b, T c)
max of 3 values
Definition: templates.hpp:37
INLINE int sgn(T a)
sign of value (+1, 0, -1)
Definition: templates.hpp:47
INLINE T min2(T a, T b)
min of 2 values
Definition: templates.hpp:15
INLINE AutoDiffVec< D, SCAL > sqr(const AutoDiffVec< D, SCAL > &x)
AutoDiffVec times AutoDiffVec.
Definition: autodiff.hpp:250
Definition: templates.hpp:91
namespace for standard data types and algorithms.
Definition: ngstd.hpp:81
INLINE T pow3(const T a)
element to the third power
Definition: templates.hpp:61