12 #ifndef EMP_FUNCTIONS_H 13 #define EMP_FUNCTIONS_H 18 #include <type_traits> 21 #include "../base/assert.h" 22 #include "../base/vector.h" 28 #define EMP_FUNCTION_TIMER(TEST_FUN) { \ 29 std::clock_t emp_start_time = std::clock(); \ 30 auto emp_result = TEST_FUN; \ 31 std::clock_t emp_tot_time = std::clock() - emp_start_time; \ 32 std::cout << "Time: " \ 33 << 1000.0 * ((double) emp_tot_time) / (double) CLOCKS_PER_SEC \ 34 << " ms" << std::endl; \ 35 std::cout << "Result: " << emp_result << std::endl; \ 41 static inline double TimeFun(std::function<
void()> test_fun) {
42 std::clock_t start_time = std::clock();
44 std::clock_t tot_time = std::clock() - start_time;
45 return 1000.0 * ((double) tot_time) / (double) CLOCKS_PER_SEC;
49 inline bool Toggle(
bool & in_bool) {
return (in_bool = !in_bool); }
52 inline constexpr
bool AllTrue() {
return true; }
53 template <
typename... Ts>
54 inline bool AllTrue(
bool result, Ts... OTHER) {
55 return result &&
AllTrue(OTHER...);
59 inline constexpr
bool AnyTrue() {
return false; }
60 template <
typename... Ts>
61 inline bool AnyTrue(
bool result, Ts... OTHER) {
62 return result ||
AnyTrue(OTHER...);
69 size_t vsize = (size_t) ((max-min) / step) + 1;
72 for (T i = min; i < max; i += step) {
80 template <
typename T,
size_t N>
81 constexpr
size_t GetSize(T (&)[N]) {
return N; }
85 static size_t val = 0;
86 emp_assert(val < MaxValue<size_t>() &&
"Ran out of unique values in size_t!");
92 static inline std::string
UniqueName(
const std::string & prefix=
"",
93 const std::string & postfix=
"") {
Useful mathematical functions (that are constexpr when possible.)
static emp::vector< T > BuildRange(T min, T max, T step=1)
Build a vector with a range of values from min to max at the provided step size.
Definition: functions.h:67
static size_t UniqueVal()
A function that will always return a unique value (and trip an assert if it can't...)
Definition: functions.h:84
bool Toggle(bool &in_bool)
Toggle an input bool.
Definition: functions.h:49
static double TimeFun(std::function< void()> test_fun)
A function timer that takes a functor an identifies how long it takes to complete when run...
Definition: functions.h:41
Commonly used constant values.
If we are in emscripten, make sure to include the header.
Definition: array.h:37
#define emp_assert(...)
Definition: assert.h:199
constexpr bool AnyTrue()
Combine bools to OR them all together.
Definition: functions.h:59
static std::string UniqueName(const std::string &prefix="", const std::string &postfix="")
Definition: functions.h:92
constexpr size_t GetSize(T(&)[N])
Determine the size of a built-in array.
Definition: functions.h:81
constexpr bool AllTrue()
Combine bools to AND them all together.
Definition: functions.h:52