Health-GPS  1.2.2.0
Global Health Policy Simulation model (Health-GPS)
interval.h
Go to the documentation of this file.
1 #pragma once
2 #include "forward_type.h"
3 #include "string_util.h"
4 #include <fmt/format.h>
5 
6 namespace hgps::core {
7 
10 template <Numerical TYPE> class Interval {
11  public:
13  Interval() = default;
14 
18  explicit Interval(TYPE lower_value, TYPE upper_value)
19  : lower_{lower_value}, upper_{upper_value} {}
20 
23  TYPE lower() const noexcept { return lower_; }
24 
27  TYPE upper() const noexcept { return upper_; }
28 
31  TYPE length() const noexcept { return upper_ - lower_; }
32 
36  bool contains(TYPE value) const noexcept {
37  if (lower_ < upper_) {
38  return lower_ <= value && value <= upper_;
39  }
40 
41  return lower_ >= value && value >= upper_;
42  }
43 
47  bool contains(Interval<TYPE> &other) const noexcept {
48  return contains(other.lower_) && contains(other.upper_);
49  }
50 
53  std::string to_string() const noexcept { return fmt::format("{}-{}", lower_, upper_); }
54 
58  auto operator<=>(const Interval<TYPE> &rhs) const = default;
59 
60  private:
61  TYPE lower_;
62  TYPE upper_;
63 };
64 
67 
70 
73 
79 IntegerInterval parse_integer_interval(const std::string_view &value,
80  const std::string_view delims = "-");
81 
87 FloatInterval parse_float_interval(const std::string_view &value,
88  const std::string_view delims = "-");
89 
95 DoubleInterval parse_double_interval(const std::string_view &value,
96  const std::string_view delims = "-");
97 } // namespace hgps::core
Numeric interval representation data type.
Definition: interval.h:10
auto operator<=>(const Interval< TYPE > &rhs) const =default
Compare two Interval instances.
bool contains(Interval< TYPE > &other) const noexcept
Determines whether an Interval is inside this instance interval.
Definition: interval.h:47
std::string to_string() const noexcept
Convert this instance to a string representation.
Definition: interval.h:53
TYPE upper() const noexcept
Gets the interval upper bound.
Definition: interval.h:27
TYPE length() const noexcept
Gets the interval length.
Definition: interval.h:31
Interval()=default
Initialises a new instance of the Interval class.
TYPE lower() const noexcept
Gets the interval lower bound.
Definition: interval.h:23
Interval(TYPE lower_value, TYPE upper_value)
Initialises a new instance of the Interval class.
Definition: interval.h:18
bool contains(TYPE value) const noexcept
Determines whether a value is in the Interval.
Definition: interval.h:36
Top-level namespace for Health-GPS Core C++ API.
Definition: analysis.h:7
FloatInterval parse_float_interval(const std::string_view &value, const std::string_view delims)
Converts the string representation to its FloatInterval equivalent.
Definition: interval.cpp:16
DoubleInterval parse_double_interval(const std::string_view &value, const std::string_view delims)
Converts the string representation to its DoubleInterval equivalent.
Definition: interval.cpp:26
@ other
Common diseases excluding cancers.
IntegerInterval parse_integer_interval(const std::string_view &value, const std::string_view delims)
Converts the string representation to its IntegerInterval equivalent.
Definition: interval.cpp:5