TCPSPSuite
util.hpp
1 #ifndef UTIL_H
2 #define UTIL_H
3 
4 #include "generated_config.hpp"
5 #include <cstddef>
6 #include <array>
7 #include <functional>
8 
9 // consts-cast for range-based for on const objects
10 template<typename T> constexpr const T &as_const(T &t) noexcept { return t; }
11 
12 // static array size determination
13 // straight from emc++ ;)
14 template<typename T, std::size_t N>
15 constexpr std::size_t get_array_size(T (&)[N]) noexcept
16 {
17  return N;
18 }
19 
20 // Hashing std::array
21 // taken from https://stackoverflow.com/questions/8027368/are-there-no-specializations-of-stdhash-for-standard-containers
22 namespace std
23 {
24  template<typename T, size_t N>
25  struct hash<array<T, N> >
26  {
27  typedef array<T, N> argument_type;
28  typedef size_t result_type;
29 
30  result_type operator()(const argument_type& a) const
31  {
32  hash<T> hasher;
33  result_type h = 0;
34  for (result_type i = 0; i < N; ++i)
35  {
36  h = h * 31 + hasher(a[i]);
37  }
38  return h;
39  }
40  };
41 }
42 
43 // clang somehow treats log2 as non-constexpr, which sucks.
44 constexpr size_t clog2(size_t n)
45 {
46  return ( (n<2) ? 0 : 1+clog2(n/2));
47 }
48 
49 inline constexpr bool double_eq(double lhs, double rhs) {
50  return (lhs <= rhs + DOUBLE_DELTA) && (lhs >= rhs - DOUBLE_DELTA);
51 }
52 
53 #endif