Health-GPS 3.0.0.0
Global Health Policy Simulation model (Health-GPS)
Loading...
Searching...
No Matches
interval.h
Go to the documentation of this file.
1#pragma once
2
4#include "forward_type.h"
5#include "string_util.h"
6
7#include <algorithm>
8#include <fmt/format.h>
9
10namespace hgps::core {
11
14template <Numerical TYPE> class Interval {
15 public:
17 Interval() = default;
18
22 explicit Interval(TYPE lower_value, TYPE upper_value)
23 : lower_{lower_value}, upper_{upper_value} {
24 if (lower_ > upper_) {
25 throw HgpsException(fmt::format("Invalid interval: {}-{}", lower_, upper_));
26 }
27 }
28
31 TYPE lower() const noexcept { return lower_; }
32
35 TYPE upper() const noexcept { return upper_; }
36
39 TYPE length() const noexcept { return upper_ - lower_; }
40
44 bool contains(TYPE value) const noexcept { return lower_ <= value && value <= upper_; }
45
49 bool contains(Interval<TYPE> &other) const noexcept {
50 return contains(other.lower_) && contains(other.upper_);
51 }
52
56 TYPE clamp(TYPE value) const noexcept { return std::clamp(value, lower_, upper_); }
57
60 std::string to_string() const noexcept { return fmt::format("{}-{}", lower_, upper_); }
61
65 auto operator<=>(const Interval<TYPE> &rhs) const = default;
66
67 private:
68 TYPE lower_{};
69 TYPE upper_{};
70};
71
74
77
80
86IntegerInterval parse_integer_interval(const std::string_view &value,
87 const std::string_view delims = "-");
88
94FloatInterval parse_float_interval(const std::string_view &value,
95 const std::string_view delims = "-");
96
102DoubleInterval parse_double_interval(const std::string_view &value,
103 const std::string_view delims = "-");
104} // namespace hgps::core
HealthGPS base exception class, with source location information.
Definition exception.h:19
Numeric interval representation data type.
Definition interval.h:14
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:49
std::string to_string() const noexcept
Convert this instance to a string representation.
Definition interval.h:60
TYPE upper() const noexcept
Gets the interval upper bound.
Definition interval.h:35
TYPE clamp(TYPE value) const noexcept
Clamp a given value to the interval boundaries.
Definition interval.h:56
TYPE length() const noexcept
Gets the interval length.
Definition interval.h:39
Interval()=default
Initialises a new instance of the Interval class.
TYPE lower() const noexcept
Gets the interval lower bound.
Definition interval.h:31
Interval(TYPE lower_value, TYPE upper_value)
Initialises a new instance of the Interval class.
Definition interval.h:22
bool contains(TYPE value) const noexcept
Determines whether a value is in the Interval.
Definition interval.h:44
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