Skip to content

Commit

Permalink
Replace FactorRange with built-in optional<pair<double,double>>.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesturner246 committed Jul 31, 2023
1 parent 16dcd3f commit 1c3a59e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/HealthGPS.Console/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ ModelInput create_model_input(core::DataTable &input_table, core::Country countr
if (item.range.empty()) {
mapping.emplace_back(item.name, item.level);
} else {
auto boundary = FactorRange{item.range[0], item.range[1]};
auto boundary = hgps::OptionalRange{{item.range[0], item.range[1]}};
mapping.emplace_back(item.name, item.level, boundary);
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/HealthGPS/mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@

namespace hgps {

MappingEntry::MappingEntry(std::string name, int level, FactorRange range)
: name_{std::move(name)}, name_key_{name_}, level_{level}, range_{range} {}
MappingEntry::MappingEntry(std::string name, int level, OptionalRange range)
: name_{std::move(name)}, name_key_{name_}, level_{level}, range_{std::move(range)} {}

const std::string &MappingEntry::name() const noexcept { return name_; }

int MappingEntry::level() const noexcept { return level_; }

const core::Identifier &MappingEntry::key() const noexcept { return name_key_; }

const FactorRange &MappingEntry::range() const noexcept { return range_; }
const OptionalRange &MappingEntry::range() const noexcept { return range_; }

double MappingEntry::get_bounded_value(const double &value) const noexcept {
if (range_.empty) {
return value;
if (range_.has_value()) {
return std::min(std::max(value, range_->first), range_->second);
}

return std::min(std::max(value, range_.minimum), range_.maximum);
return value;
}

inline bool operator>(const MappingEntry &lhs, const MappingEntry &rhs) {
Expand Down Expand Up @@ -74,4 +73,4 @@ std::vector<MappingEntry> HierarchicalMapping::at_level(int level) const noexcep
return result;
}

} // namespace hgps
} // namespace hgps
38 changes: 6 additions & 32 deletions src/HealthGPS/mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,8 @@ namespace hgps {
/// @brief The constant in the regression model presentation identifier
inline const core::Identifier InterceptKey = core::Identifier{"intercept"};

/// @brief Defines the risk factor allowed range data type
///
/// @details The factors range is defined from the fitted dataset and enforced
/// by the simulation algorithm. The default constructor, creates an empty range.
struct FactorRange {
/// @brief Initialises a new instance of the FactorRange structure
FactorRange() = default;

/// @brief Initialises a new instance of the FactorRange structure
/// @param min_value Minimum factor value
/// @param max_value Maximum factor value
/// @throws std::invalid_argument for minimum greater than the maximum value
FactorRange(double min_value, double max_value)
: empty{false}, minimum{min_value}, maximum{max_value} {
if (min_value > max_value) {
throw std::invalid_argument("Factor range minimum must not be greater than maximum.");
}
}

/// @brief Gets a value indicating whether the range is empty, no limits.
bool empty{true};

/// @brief The range minimum value
double minimum{};

/// @brief The range maximum value
double maximum{};
};
/// @brief Optional Range of doubles data type
using OptionalRange = std::optional<std::pair<double, double>>;

/// @brief Defines risk factor mapping entry data type
///
Expand All @@ -52,7 +26,7 @@ class MappingEntry {
/// @param name Risk factor name
/// @param level The hierarchical level
/// @param range The factor range
MappingEntry(std::string name, int level, FactorRange range = {});
MappingEntry(std::string name, int level, OptionalRange range = {});

/// @brief Gets the factor name
/// @return Factor name
Expand All @@ -68,7 +42,7 @@ class MappingEntry {

/// @brief Gets the factor allowed values range
/// @return Factor values range
const FactorRange &range() const noexcept;
const OptionalRange &range() const noexcept;

/// @brief Adjusts a value to the factor range, if provided
/// @param value The value to adjust
Expand All @@ -79,7 +53,7 @@ class MappingEntry {
std::string name_;
core::Identifier name_key_;
int level_{};
FactorRange range_;
OptionalRange range_;
};

/// @brief Defines the hierarchical model mapping data type
Expand Down Expand Up @@ -147,4 +121,4 @@ class HierarchicalMapping {
std::vector<MappingEntry> mapping_;
};

} // namespace hgps
} // namespace hgps

0 comments on commit 1c3a59e

Please sign in to comment.