LRT13  1.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
Util.h
1 #ifndef LRT_UTILS_H_
2 #define LRT_UTILS_H_
3 
4 #include <string>
5 #include <math.h>
6 #include <sstream>
7 
8 #include "Defines.h"
9 
19 using namespace std;
20 
21 class Util
22 {
23 public:
31  template<typename T> static inline T Clamp(T val, T min, T max)
32  {
33  if (val > max)
34  return max;
35  else if (val < min)
36  return min;
37  else
38  return val;
39  }
40 
50  template<typename T> static inline T Rescale(T val, T min0, T max0, T min1, T max1)
51  {
52  if (max0 == min0)
53  return min1;
54  val = Clamp<T> (val, min0, max0);
55  return (val - min0) * (max1 - min1) / (max0 - min0) + min1;
56  }
57 
63  template<typename T> static inline std::string ToString(T val)
64  {
65  std::stringstream ss;
66  ss << val;
67  return ss.str();
68  }
69 
75  template<typename T> static inline int Sign(T val)
76  {
77  if (val > 0)
78  return 1;
79  else if (val < 0)
80  return -1;
81  return 0;
82  }
83 
89  template<typename T> static inline T Abs(T val)
90  {
91  if (val < 0)
92  return -val;
93  return val;
94  }
95 
104  template<typename T> static void MinMaxMean(T val[], int n, T* minOut,
105  T* maxOut, T* meanOut)
106  {
107  if (n == 0)
108  {
109  *minOut = 0;
110  *maxOut = 0;
111  *meanOut = 0;
112  return;
113  }
114 
115  T min = val[0], max = val[0];
116  T mean = 0;
117  for (int i = 0; i < n; ++i)
118  {
119  mean += val[i];
120  if (val[i] < min)
121  min = val[i];
122  if (val[i] > max)
123  max = val[i];
124  }
125  mean /= n;
126 
127  *minOut = min;
128  *maxOut = max;
129  *meanOut = mean;
130  }
131 
138  template<typename T> static inline T AddDeadband(T raw, T deadbandSize)
139  {
140  if (Util::Abs<T>(raw) < deadbandSize)
141  return 0;
142  return Util::Sign<T>(raw) * Util::Rescale<T>(Util::Abs<T>(raw),
143  deadbandSize, 1, 0, 1);
144  }
145 
152  template<typename T>
153  static inline T ValWithAbsMax(T val1, T val2)
154  {
155  if (Util::Abs<T>(val1) > Util::Abs<T>(val2))
156  return val1;
157  return val2;
158  }
159 
166  template<typename T>
167  static inline T Max(T val1, T val2)
168  {
169  if (val1 > val2)
170  return val1;
171  return val2;
172  }
173 
180  template<typename T>
181  static inline T Min(T val1, T val2)
182  {
183  if (val1 < val2)
184  return val1;
185  return val2;
186  }
187 
194  template<typename T>
195  static inline T MinAbs(T val1, T val2)
196  {
197  if (fabs(val1) < fabs(val2))
198  return val1;
199  return val2;
200  }
201 
208  template<typename T>
209  static inline T PowPreseveSign(T val, int power)
210  {
211  T result = pow(val, power);
212  if (Sign<T> (val) == Sign<T> (result))
213  return result;
214  else
215  return -result;
216  }
217 
218  template<typename T>
219  static inline T lexical_cast(string value)
220  {
221  T temp;
222  stringstream sstream(value);
223  sstream >> std::boolalpha >> temp;
224  return temp;
225  }
226 
227  template<typename T>
228  static inline string lexical_cast(T value)
229  {
230  return std::string(static_cast<ostringstream*>(&(ostringstream() << value))->str());
231  }
232 
236  template<class A, class B>
237  static bool DeleteMapFirst(pair<A, B> x)
238  {
239  if(x.first == NULL)
240  return true;
241 
242  DELETE(x.first);
243 
244  return true;
245  }
246 
250  template<class A, class B>
251  static bool DeleteMapSecond(pair<A, B> x)
252  {
253  if(x.second == NULL)
254  return true;
255 
256  DELETE(x.second);
257 
258  return true;
259  }
260 
264  template<class A, class B>
265  static bool DeleteMapBoth(pair<A, B> x)
266  {
267  if(x.first != NULL)
268  {
269  DELETE(x.first);
270  }
271 
272  if(x.second != NULL)
273  {
274  DELETE(x.second);
275  }
276 
277  return true;
278  }
279 
280 
284  template<class A>
285  static bool DeleteVector(A x)
286  {
287  if(x == NULL)
288  return true;
289 
290  DELETE(x);
291 
292  return true;
293  }
294 
298  static void Die();
299 
304  static void Die(const char* message);
305 
312  static bool Assert(bool test, const char* message);
313 };
314 
315 #endif