Added clamp function. Added random generation macros. Added MIN and MAX macros. Added very simple uniform random numbers generator (Park Miller). Updated Chronometer so that any string can be displayed.
This commit is contained in:
parent
b6f1ad5573
commit
8ecc9a2518
1 changed files with 35 additions and 3 deletions
38
utils.hpp
38
utils.hpp
|
|
@ -11,12 +11,41 @@
|
|||
std::cout << "\n";}
|
||||
#define PRINT_STR(x) std::cout << (x) << "\n"
|
||||
|
||||
#define RAND_A_B(a, b) ((double(rand())/RAND_MAX)*(b-a) + a)
|
||||
#define RAND_0_1() (double(rand())/RAND_MAX)
|
||||
|
||||
#define MIN(a, b) ((a) < (b)) ? (a) : (b)
|
||||
#define MAX(a, b) ((a) > (b)) ? (a) : (b)
|
||||
|
||||
template<typename T, typename T2> auto min(const T & a, const T2 & b) -> decltype(a | b) { return (a < b) ? T(a) : T(b); }
|
||||
template<typename T, typename T2> auto max(const T & a, const T2 & b) -> decltype(a | b) { return (a < b) ? T(b) : T(a); }
|
||||
|
||||
/// To be sure that floating point numbers won't be casted to integers in ::abs()...
|
||||
template<typename T> T sureAbs(const T & x) { return (x < T(0)) ? -x : x; }
|
||||
|
||||
/// Clamps the input so that it remains within the interval [a, b]
|
||||
template<typename T>
|
||||
T clamp(const T & x, const T & a, const T & b)
|
||||
{
|
||||
if(x < a)
|
||||
return a;
|
||||
if(x > b)
|
||||
return b;
|
||||
return x;
|
||||
}
|
||||
|
||||
/// VERY simple uniform random numbers generator (Park Miller)
|
||||
///
|
||||
/// http://www.cems.uwe.ac.uk/~irjohnso/coursenotes/ufeen8-15-m/p1192-parkmiller.pdf
|
||||
/// http://www0.cs.ucl.ac.uk/staff/ucacbbl/ftp/papers/langdon_2009_CIGPU.pdf
|
||||
template<int x=1>
|
||||
int intrnd(int & seed) // 1 <= seed < m
|
||||
{
|
||||
const int a = 16807; //ie 7**5
|
||||
const int m = 2147483647; //ie 2**31-1
|
||||
seed = (long(seed) * a) % m;
|
||||
return seed;
|
||||
}
|
||||
|
||||
/*
|
||||
/// Generic operator<< for ostream, (to print std::vector, etd::list, std::map, etc). Can generate conflicts with specialized operator<< overloads.
|
||||
|
|
@ -118,12 +147,15 @@ struct Chronometer
|
|||
return double(resolution_ratio.num)/resolution_ratio.den;
|
||||
}
|
||||
|
||||
/// Measures the time elapsed between the creation of the object and its destruction, and prints the result in std::cout.
|
||||
void MeasureTimeElapsed() const
|
||||
/// Measures the time elapsed between the creation of the object and its destruction, and prints the result in std::cout. If displayname = "", the name stored in the object is used.
|
||||
void MeasureTimeElapsed(const std::string & displayname = "") const
|
||||
{
|
||||
// measure time since object creation and print it in the console
|
||||
const std::string *newname = &name;
|
||||
if(displayname.size())
|
||||
newname = &displayname;
|
||||
std::cout.precision(16);
|
||||
std::cout << name.c_str() << "\t: " << GetTime() << " s (+/- " << GetResolution() << " s)" << std::endl;// use of c_str() so that there is no ambiguity with the generic operator<<
|
||||
std::cout << newname->c_str() << "\t: " << GetTime() << " s (+/- " << GetResolution() << " s)" << std::endl;// use of c_str() so that there is no ambiguity with the generic operator<<
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue