Hex  1.0
Hydrogen-electron collision solver
 All Data Structures Namespaces Files Functions Variables Typedefs Macros Pages
misc.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * *
3  * / / / / __ \ \ / / *
4  * / /__ / / / _ \ \ \/ / *
5  * / ___ / | |/_/ / /\ \ *
6  * / / / / \_\ / / \ \ *
7  * *
8  * Jakub Benda (c) 2014 *
9  * Charles University in Prague *
10  * *
11 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 #ifndef HEX_MISC
14 #define HEX_MISC
15 
16 #include <exception>
17 #include <chrono>
18 #include <complex>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <iostream>
22 #include <fstream>
23 #include <limits>
24 
39 class exception : public std::exception
40 {
41 public:
42 
44  template <class ...Params> exception (Params ...p)
45  {
46  // get requested size
47  int size = snprintf(nullptr, 0, p...);
48 
49  // allocate buffer
50  message = new char [size + 1];
51 
52  // printf to the allocated storage
53  snprintf(message, size + 1, p...);
54  }
55 
57  virtual ~exception() noexcept
58  {
59  delete [] message;
60  }
61 
63  const char* what() const noexcept (true)
64  {
65  return message;
66  }
67 
68 private:
69 
71  char * message;
72 };
73 
74 #include <algorithm>
75 #include <functional>
76 #include <cctype>
77 #include <locale>
78 #include <type_traits>
79 
80 //
81 // Restricted pointers.
82 //
83 
84 #ifndef restrict
85 #ifdef __GNUC__
86  #define restrict __restrict
87 #else
88  #define restrict
89  #warning "Don't know how to use restricted pointers with this compiler. The resulting code will be slow."
90 #endif
91 #endif
92 
93 //
94 // Memory alignment.
95 // - supports intrinsic compiler optimization, mainly the usage of SIMD instructions (AVX etc.)
96 // - enabled only for Linux systems (those ought to posess "posix_memalign", which is used)
97 //
98 
99 #ifndef __linux__
100  #define NO_ALIGN
101 #endif
102 
103 //
104 // List all complex types.
105 //
106 
107 template <class T>
108 struct is_complex { static const bool value = false; };
109 template<> template <class T>
110 struct is_complex<std::complex<T>> { static const bool value = true; };
111 
112 //
113 // List all scalar types variables.
114 //
115 
116 #define declareTypeAsScalar(T) \
117  \
118 template <> struct is_scalar<T> \
119 { \
120  static const bool value = true; \
121 };
122 
123 // declare general type as non-scalar (if not complex)
124 template <class T> struct is_scalar
125 {
126  static const bool value = is_complex<T>::value;
127 };
128 
129 // explicitly list all basic scalar types
132 declareTypeAsScalar(float)
133 declareTypeAsScalar(double)
134 
135 //
136 // White space trimming.
137 //
138 
140 static inline std::string & ltrim (std::string & s)
141 {
142  s.erase
143  (
144  s.begin(),
145  std::find_if
146  (
147  s.begin(),
148  s.end(),
149  std::not1(std::ptr_fun<int, int>(std::isspace))
150  )
151  );
152  return s;
153 }
154 
156 static inline std::string & rtrim (std::string & s)
157 {
158  s.erase
159  (
160  std::find_if
161  (
162  s.rbegin(),
163  s.rend(),
164  std::not1(std::ptr_fun<int, int>(std::isspace))
165  ).base(), s.end()
166  );
167  return s;
168 }
169 
171 static inline std::string & trim (std::string & s)
172 {
173  return ltrim(rtrim(s));
174 }
175 
176 //
177 // Miscellaneous functions.
178 //
179 
181 template <class T> int signum (T x)
182 {
183  if (x < T(0))
184  return -1;
185  else if (x == T(0))
186  return 0;
187  else
188  return 1;
189 }
190 
192 
193 template <typename T> T mmin (T x)
194 {
195  return x;
196 }
197 template <typename T, class ...Params> T mmin (T x, Params ...p)
198 {
199  T y = mmin(p...);
200  return std::min(x, y);
201 }
203 
205 
206 template <typename T> T mmax (T x)
207 {
208  return x;
209 }
210 template <typename T, class ...Params> T mmax (T x, Params ...p)
211 {
212  T y = mmax(p...);
213  return std::max(x, y);
214 }
216 
218 template <class T> constexpr T const & larger_of (T const & a, T const & b)
219 {
220  return (a > b) ? a : b;
221 }
222 
233 template <class ...Params> char const * format (Params ...p)
234 {
235  static char text[1024];
236  snprintf(text, sizeof(text), p...);
237  return text;
238 }
239 
248 template <class T> T string_to (std::string str)
249 {
250  throw exception
251  (
252  "Conversion to \"%s\" not implemented.", typeid(T).name()
253  );
254 }
255 
263 template <> inline int string_to (std::string str)
264 {
265  // convert to int
266  char* tail; long val = strtol (str.c_str(), &tail, 10);
267 
268  // throw or return
269  if (*tail != 0x0)
270  throw exception ("The string \"%s\" cannot be converted to integer number.", str.c_str());
271  else
272  return val;
273 }
274 
282 template <> inline double string_to (std::string str)
283 {
284  // convert to float
285  char* tail; double val = strtod (str.c_str(), &tail);
286 
287  // throw or return
288  if (*tail != 0x0)
289  throw exception ("The string \"%s\" cannot be converted to real number.", str.c_str());
290  else
291  return val;
292 }
293 
309 template <class T> T read_next (std::ifstream & f)
310 {
311  // text buffer
312  std::string s;
313 
314  // while there is something in the file
315  while (not f.eof())
316  {
317  // read string (it won't start with white character)
318  f >> s;
319 
320  // check length (skip empty reads)
321  if (s.size() == 0)
322  continue;
323 
324  // check if it is a beginning of a comment
325  if (s.front() == '#')
326  {
327  // get the rest of the line
328  std::getline(f, s);
329  continue;
330  }
331 
332  // otherwise exit the loop (a valid entry was found)
333  break;
334  }
335 
336  // check for special character; exit if found
337  if (s == "*")
338  throw true;
339 
340  // convert entry to type T
341  T val = string_to<T>(s.c_str());
342 
343  // return
344  return val;
345 }
346 
360 class Timer
361 {
362  public:
363 
366  : start_(std::chrono::system_clock::now()) {}
367 
369  void reset ()
370  {
371  start_ = std::chrono::system_clock::now();
372  }
373 
375  unsigned seconds ()
376  {
377  std::chrono::system_clock::time_point end = std::chrono::system_clock::now(); // ? steady_clock
378  std::chrono::seconds secs = std::chrono::duration_cast<std::chrono::seconds>(end - start_);
379  return secs.count();
380  }
381 
383  unsigned milliseconds ()
384  {
385  std::chrono::system_clock::time_point end = std::chrono::system_clock::now(); // ? steady_clock
386  std::chrono::milliseconds misecs = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_);
387  return misecs.count();
388  }
389 
391  unsigned microseconds ()
392  {
393  std::chrono::system_clock::time_point end = std::chrono::system_clock::now(); // ? steady_clock
394  std::chrono::microseconds musecs = std::chrono::duration_cast<std::chrono::microseconds>(end - start_);
395  return musecs.count();
396  }
397 
398  private:
399 
401  mutable std::chrono::system_clock::time_point start_;
402 };
403 
404 #endif
T mmin(T x)
Many-argument &quot;min&quot; function.
Definition: misc.h:193
void reset()
Start timer.
Definition: misc.h:369
static const bool value
Definition: misc.h:108
T min(const ArrayView< T > a)
Minimal element of array.
Definition: arrays.h:1663
int size
Definition: misc.h:311
constexpr T const & larger_of(T const &a, T const &b)
Constant-expression max.
Definition: misc.h:218
int signum(T x)
Signum function.
Definition: misc.h:181
T mmax(T x)
Many-argument &quot;max&quot; function.
Definition: misc.h:206
#define declareTypeAsScalar(T)
Definition: misc.h:116
T string_to(std::string str)
Conversion of string to a type.
Definition: misc.h:248
exception exception::Params::::::p exception() noexcept
Destructor.
Definition: misc.h:57
snprintf(message, size+1, p...)
Timer()
Return object reference (singleton interface).
Definition: misc.h:365
class NodeIntegrator exception
Definition: misc.h:108
unsigned milliseconds()
Return elapsed time in milliseconds.
Definition: misc.h:383
Definition: misc.h:124
char const * format(Params...p)
printf-like formatting.
Definition: misc.h:233
T read_next(std::ifstream &f)
Read next entry from input stream.
Definition: misc.h:309
unsigned microseconds()
Return elapsed time in microseconds.
Definition: misc.h:391
const char * what() const noexcept(true)
Return pointer to the exception text.
Definition: misc.h:63
Constructor.
Definition: misc.h:44
Exception class.
Definition: misc.h:39
Timing class.
Definition: misc.h:360
T max(const ArrayView< T > a)
Maximal element of array.
Definition: arrays.h:1673
unsigned seconds()
Return elapsed time in seconds.
Definition: misc.h:375
static const bool value
Definition: misc.h:126