Empirical
functions.h
Go to the documentation of this file.
1 
12 #ifndef EMP_FUNCTIONS_H
13 #define EMP_FUNCTIONS_H
14 
15 #include <ctime>
16 #include <functional>
17 #include <iostream>
18 #include <type_traits>
19 #include <sstream>
20 
21 #include "../base/assert.h"
22 #include "../base/vector.h"
23 
24 #include "const.h"
25 #include "math.h"
26 
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; \
36  }
37 
38 namespace emp {
39 
41  static inline double TimeFun(std::function<void()> test_fun) {
42  std::clock_t start_time = std::clock();
43  test_fun();
44  std::clock_t tot_time = std::clock() - start_time;
45  return 1000.0 * ((double) tot_time) / (double) CLOCKS_PER_SEC;
46  }
47 
49  inline bool Toggle(bool & in_bool) { return (in_bool = !in_bool); }
50 
52  inline constexpr bool AllTrue() { return true; }
53  template <typename... Ts>
54  inline bool AllTrue(bool result, Ts... OTHER) {
55  return result && AllTrue(OTHER...);
56  }
57 
59  inline constexpr bool AnyTrue() { return false; }
60  template <typename... Ts>
61  inline bool AnyTrue(bool result, Ts... OTHER) {
62  return result || AnyTrue(OTHER...);
63  }
64 
66  template <typename T>
67  static inline emp::vector<T> BuildRange(T min, T max, T step=1) {
68  emp_assert(max > min);
69  size_t vsize = (size_t) ((max-min) / step) + 1;
70  emp::vector<T> out_v(vsize);
71  size_t pos = 0;
72  for (T i = min; i < max; i += step) {
73  out_v[pos++] = i;
74  }
75  return out_v;
76  }
77 
78 
80  template <typename T, size_t N>
81  constexpr size_t GetSize(T (&)[N]) { return N; }
82 
84  static size_t UniqueVal() {
85  static size_t val = 0;
86  emp_assert(val < MaxValue<size_t>() && "Ran out of unique values in size_t!");
87  return val++;
88  }
89 
92  static inline std::string UniqueName(const std::string & prefix="",
93  const std::string & postfix="") {
94  std::stringstream ss;
95  ss << prefix << UniqueVal() << postfix;
96  return ss.str();
97  }
98 }
99 
100 #endif
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&#39;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