Health-GPS  1.2.2.0
Global Health Policy Simulation model (Health-GPS)
gender_table.h
Go to the documentation of this file.
1 #pragma once
2 #include <map>
3 #include <numeric>
4 
8 #include "monotonic_vector.h"
9 
10 namespace hgps {
11 
15 template <core::Numerical ROW, core::Numerical TYPE> class GenderTable {
16  public:
18  GenderTable() = default;
19 
25  GenderTable(const MonotonicVector<ROW> &rows, const std::vector<core::Gender> &cols,
26  core::Array2D<TYPE> &&values)
27  : table_{std::move(values)}, rows_index_{}, cols_index_{} {
28  if (rows.size() != table_.rows() || cols.size() != table_.columns()) {
29  throw std::invalid_argument("Lookup breakpoints and values size mismatch.");
30  }
31 
32  auto rows_count = static_cast<int>(rows.size());
33  for (auto index = 0; index < rows_count; index++) {
34  rows_index_.emplace(rows[index], index);
35  }
36 
37  auto cols_count = static_cast<int>(cols.size());
38  for (auto index = 0; index < cols_count; index++) {
39  cols_index_.emplace(cols[index], index);
40  }
41  }
42 
45  std::size_t size() const noexcept { return table_.size(); }
46 
49  std::size_t rows() const noexcept { return table_.rows(); }
50 
53  std::size_t columns() const noexcept { return table_.columns(); }
54 
57  bool empty() const noexcept { return rows_index_.empty() || cols_index_.empty(); }
58 
64  TYPE &at(const ROW row, const core::Gender gender) {
65  return table_(rows_index_.at(row), cols_index_.at(gender));
66  }
67 
73  const TYPE &at(const ROW row, const core::Gender gender) const {
74  return table_(rows_index_.at(row), cols_index_.at(gender));
75  }
76 
82  TYPE &operator()(const ROW row, const core::Gender gender) {
83  return table_(rows_index_.at(row), cols_index_.at(gender));
84  }
85 
91  const TYPE &operator()(const ROW row, const core::Gender gender) const {
92  return table_(rows_index_.at(row), cols_index_.at(gender));
93  }
94 
98  bool contains(const ROW row) const noexcept { return rows_index_.contains(row); }
99 
104  bool contains(const ROW row, const core::Gender gender) const noexcept {
105  if (rows_index_.contains(row)) {
106  return cols_index_.contains(gender);
107  }
108 
109  return false;
110  }
111 
112  private:
113  core::Array2D<TYPE> table_{};
114  std::map<ROW, int> rows_index_{};
115  std::map<core::Gender, int> cols_index_{};
116 };
117 
120 template <core::Numerical TYPE> class AgeGenderTable : public GenderTable<int, TYPE> {
121  public:
123  AgeGenderTable() = default;
124 
130  AgeGenderTable(const MonotonicVector<int> &rows, const std::vector<core::Gender> &cols,
131  core::Array2D<TYPE> &&values)
132  : GenderTable<int, TYPE>(rows, cols, std::move(values)) {}
133 };
134 
141 template <core::Numerical TYPE>
143  if (rows_range.lower() < 0 || rows_range.lower() >= rows_range.upper()) {
144  throw std::out_of_range("The 'range lower' value must be greater than zero and less than "
145  "the 'range upper' value.");
146  }
147 
148  auto rows = std::vector<int>(static_cast<size_t>(rows_range.length()) + 1);
149  std::iota(rows.begin(), rows.end(), rows_range.lower());
150 
151  auto cols = std::vector<core::Gender>{core::Gender::male, core::Gender::female};
152  auto data = core::Array2D<TYPE>(rows.size(), cols.size());
153  return GenderTable<int, TYPE>(MonotonicVector(rows), cols, std::move(data));
154 }
155 
161 template <core::Numerical TYPE>
163  if (age_range.lower() < 0 || age_range.lower() >= age_range.upper()) {
164  throw std::invalid_argument(
165  "The 'age lower' value must be greater than zero and less than the 'age upper' value.");
166  }
167 
168  auto rows = std::vector<int>(static_cast<std::size_t>(age_range.length()) + 1);
169  std::iota(rows.begin(), rows.end(), age_range.lower());
170 
171  auto cols = std::vector<core::Gender>{core::Gender::male, core::Gender::female};
172  auto data = core::Array2D<TYPE>(rows.size(), cols.size());
173  return AgeGenderTable<TYPE>(MonotonicVector(rows), cols, std::move(data));
174 }
175 
178 
181 
184 } // namespace hgps
Defines the age and gender lookup table data type.
Definition: gender_table.h:120
AgeGenderTable(const MonotonicVector< int > &rows, const std::vector< core::Gender > &cols, core::Array2D< TYPE > &&values)
Initialises a new instance of the AgeGenderTable class.
Definition: gender_table.h:130
AgeGenderTable()=default
Initialises a new instance of the AgeGenderTable class.
Defines the gender column lookup table data type.
Definition: gender_table.h:15
bool empty() const noexcept
Determine whether the lookup table is empty.
Definition: gender_table.h:57
std::size_t rows() const noexcept
Get the number of rows lookup breakpoints.
Definition: gender_table.h:49
const TYPE & at(const ROW row, const core::Gender gender) const
Gets a read-only value at a given row and column intersection.
Definition: gender_table.h:73
GenderTable()=default
Initialises a new instance of the GenderTable class.
bool contains(const ROW row, const core::Gender gender) const noexcept
Determines whether the lookup contains a value.
Definition: gender_table.h:104
std::size_t size() const noexcept
Gets the lookup table size.
Definition: gender_table.h:45
std::size_t columns() const noexcept
Get the number of columns lookup breakpoints.
Definition: gender_table.h:53
GenderTable(const MonotonicVector< ROW > &rows, const std::vector< core::Gender > &cols, core::Array2D< TYPE > &&values)
Initialises a new instance of the GenderTable class.
Definition: gender_table.h:25
bool contains(const ROW row) const noexcept
Determines whether the lookup contains a row.
Definition: gender_table.h:98
TYPE & operator()(const ROW row, const core::Gender gender)
Gets a value at a given row and column intersection.
Definition: gender_table.h:82
const TYPE & operator()(const ROW row, const core::Gender gender) const
Gets a read-only value at a given row and column intersection.
Definition: gender_table.h:91
TYPE & at(const ROW row, const core::Gender gender)
Gets a value at a given row and column intersection.
Definition: gender_table.h:64
Defines a monotonic vector container type.
Definition: monotonic_vector.h:42
Defines a contiguous storage for two-dimensional numerical data in row-major format.
Definition: array2d.h:15
Numeric interval representation data type.
Definition: interval.h:10
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
TYPE lower() const noexcept
Gets the interval lower bound.
Definition: interval.h:23
Gender
Enumerates gender types.
Definition: forward_type.h:18
Top-level namespace for Health-GPS C++ API.
Definition: analysis_definition.h:8
GenderTable< int, TYPE > create_integer_gender_table(const core::IntegerInterval &rows_range)
Creates an instance of the gender lookup table for integer rows breakpoints value.
Definition: gender_table.h:142
AgeGenderTable< TYPE > create_age_gender_table(const core::IntegerInterval &age_range)
Creates an instance of the age and gender lookup table.
Definition: gender_table.h:162
Global namespace.
Definition: jsonparser.h:88