Kepler's Torus
bounds.h
1 #ifndef CAPSTONE_BOUNDS_H
2 #define CAPSTONE_BOUNDS_H
3 
4 #include <stdexcept>
5 #include <functional>
6 
8 struct Bounds {
9  double const lower, upper;
10 
11  inline Bounds (double lower, double upper)
12  : lower {lower < upper ? lower : upper}
13  , upper {lower < upper ? upper : lower}
14  {
15  if (!std::isnormal (lower) && lower != 0.0)
16  throw std::logic_error ("Real boundary expected; got " + std::to_string (lower));
17  if (!std::isnormal (upper) && upper != 0.0)
18  throw std::logic_error ("Real boundary expected; got " + std::to_string (upper));
19  }
20 
22  [[nodiscard]] inline bool operator == (Bounds other) const {
23  return lower == other.lower && upper == other.upper;
24  }
25 
27  [[nodiscard]] inline bool contains (double val) const {
28  if (val == lower) return true;
29  return val >= lower && val < upper;
30  }
31 };
32 
33 template <>
35 struct std::hash <Bounds> {
36  inline std::size_t operator() (Bounds const & bounds) const noexcept {
37  auto hash_lower = std::hash <double> {}(bounds.lower);
38  auto hash_upper = std::hash <double> {}(bounds.upper);
39  /* Combines the two individual hashes as proposed on the corresponding cppreference.com page:
40  * https://en.cppreference.com/w/cpp/utility/hash (2022-06-06) */
41  return hash_lower ^ (hash_upper << 1);
42  }
43 };
44 
45 #endif //CAPSTONE_BOUNDS_H
46 
47 /* Copyright © 2022 Aaron Alef */
bool operator==(Bounds other) const
compare the given bounds for equality
Definition: bounds.h:22
Store lower inclusive and upper exclusive boundaries as container for interaction with random number ...
Definition: bounds.h:8
bool contains(double val) const
Check whether the given value is within this object&#39;s boundaries.
Definition: bounds.h:27