-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
153 lines (119 loc) · 3.25 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef __utils_h__
#define __utils_h__
#include <iostream>
#include <limits>
#include <unordered_map>
#include <vector>
#include <memory>
#include <cmath>
//#if __has_include(<filesystem>)
//
//# include <filesystem>
//# define have_filesystem 1
//
//#endif
#ifdef _OPENMP
#include <omp.h>
#else
#include <chrono>
#endif
static constexpr double INF = std :: numeric_limits < double > :: infinity(); ///< shortcut to infinity value
static constexpr double epsilon = std :: numeric_limits < double > :: epsilon(); ///< shortcut to epsilon value
#if (defined(__GNUC__) && !defined(__clang__))
static constexpr double log_2 = std :: log(2); ///< shortcut to log(2)
static constexpr double log2_over_2 = std :: log(2) * .5; ///< shortcut to log(2)/2
#else
static constexpr double log_2 = 0.6931471805599453; ///< shortcut to log(2)
static constexpr double log2_over_2 = 0.34657359027997264311; ///< shortcut to log(2)/2
#endif
#ifndef M_PI
#define M_PI 3.141592653589793
#endif
#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
/**
* @brief sign function
*
* @param x value
*
*/
double sign (const double & x);
enum {scoping_ = 0L,
pseudo_,
free_scoping_,
standard_
};
enum {magP = 0L,
magT = 1
};
static std :: unordered_map < std :: string, long int > protocol
{
{"scoping", scoping_},
{"pseudo_reinforcement", pseudo_},
{"free_scoping", free_scoping_},
{"standard_reinforcement", standard_}
};
/**
* @brief split string to token
*
* @param txt input string
* @param del delimiter as string
*
* @returns std::vector<std::string> vector of token
*
*/
std :: vector < std :: string > split (const std :: string & txt, const std :: string & del);
#if ( ( __cplusplus < 201100 && !(_MSC_VER) ) || ( __GNUC__ == 4 && __GNUC_MINOR__ < 9) && !(__clang__) )
namespace std
{
/**
* @brief wrap for the older version of gcc anc clang
*
* @tparam T type of the pointer array
*
* @param size lenght of unique_ptr array
*
* @returns pointer array as unique_ptr (e.g. std :: unique_ptr < float[] > in modern c++)
*
*/
template < typename T >
std :: unique_ptr < T > make_unique ( std :: size_t size );
}
#endif
/**
* @brief check if a given file exists
*
* @param filename filename/path to check
*
* @returns bool true if the filename is found else false
*
*/
bool file_exists (const std :: string & filename);
/**
* @brief Evaluate current time
*
* @details this function use OpenMP API if the code is compiled with -fopenmp else it uses chrono functions
*
* @returns current time as double (OpenMP) or chrono
*
*/
#ifdef _OPENMP
double what_time_is_it_now ();
#else
std :: chrono :: time_point < std :: chrono :: high_resolution_clock > what_time_is_it_now ();
#endif
/**
* @brief Evaluate duration time
*
* @details this function is used in combination with what_time_is_it_now
*
* @tparam Time the type returned by what_time_is_it_now function
*
* @param start time evaluated by what_time_is_it_now function
*
* @returns evaluated time
*
*/
template < typename Time >
auto duration (const Time & start);
#endif // __utils_h__