From 8ecc9a2518df62a9366c8c295b5ec46189c246a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me?= Date: Mon, 29 Apr 2019 16:54:41 +0200 Subject: [PATCH] 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. --- utils.hpp | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/utils.hpp b/utils.hpp index 6db340d..c7366b9 100644 --- a/utils.hpp +++ b/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 auto min(const T & a, const T2 & b) -> decltype(a | b) { return (a < b) ? T(a) : T(b); } template 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 T sureAbs(const T & x) { return (x < T(0)) ? -x : x; } +/// Clamps the input so that it remains within the interval [a, b] +template +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 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<< } };