Health-GPS  1.2.2.0
Global Health Policy Simulation model (Health-GPS)
map2d.h
Go to the documentation of this file.
1 #pragma once
2 #include <map>
3 
4 namespace hgps {
5 
10 template <class TROW, class TCOL, class TCell> class Map2d {
11  public:
13  using IteratorType = typename std::map<TROW, std::map<TCOL, TCell>>::iterator;
14 
16  using ConstIteratorType = typename std::map<TROW, std::map<TCOL, TCell>>::const_iterator;
17 
19  Map2d() = default;
20 
23  Map2d(std::map<TROW, std::map<TCOL, TCell>> &&data) noexcept : table_{std::move(data)} {}
24 
27  bool empty() const noexcept { return table_.empty(); }
28 
31  std::size_t size() const noexcept { return rows() * columns(); }
32 
35  std::size_t rows() const noexcept { return table_.size(); }
36 
39  std::size_t columns() const noexcept {
40  if (table_.size() > 0) {
41 
42  // Assuming that inner map has same size.
43  return table_.begin()->second.size();
44  }
45 
46  return 0;
47  }
48 
52  bool contains(const TROW &row_key) const { return table_.contains(row_key); }
53 
58  bool contains(const TROW &row_key, const TCOL &col_key) const {
59  if (table_.contains(row_key)) {
60  return table_.at(row_key).contains(col_key);
61  }
62 
63  return false;
64  }
65 
69  std::map<TCOL, TCell> &row(const TROW &row_key) { return table_.at(row_key); }
70 
74  const std::map<TCOL, TCell> &row(const TROW &row_key) const { return table_.at(row_key); }
75 
80  TCell &at(const TROW &row_key, const TCOL &col_key) { return table_.at(row_key).at(col_key); }
81 
86  const TCell &at(const TROW &row_key, const TCOL &col_key) const {
87  return table_.at(row_key).at(col_key);
88  }
89 
92  IteratorType begin() noexcept { return table_.begin(); }
93 
96  IteratorType end() noexcept { return table_.end(); }
97 
100  ConstIteratorType begin() const noexcept { return table_.cbegin(); }
101 
104  ConstIteratorType end() const noexcept { return table_.cend(); }
105 
108  ConstIteratorType cbegin() const noexcept { return table_.cbegin(); }
109 
112  ConstIteratorType cend() const noexcept { return table_.cend(); }
113 
114  private:
115  std::map<TROW, std::map<TCOL, TCell>> table_;
116 };
117 } // namespace hgps
Defines the two-dimensional map (lookup table) data type.
Definition: map2d.h:10
typename std::map< TROW, std::map< TCOL, TCell > >::iterator IteratorType
Map row iterator.
Definition: map2d.h:13
std::size_t size() const noexcept
Gets the total size of the container dataset.
Definition: map2d.h:31
Map2d()=default
Initialises a new instance of the Map2d class.
const std::map< TCOL, TCell > & row(const TROW &row_key) const
Gets a read-row dataset.
Definition: map2d.h:74
bool contains(const TROW &row_key) const
Determines whether the container contains a row.
Definition: map2d.h:52
bool empty() const noexcept
Determine whether the container is empty.
Definition: map2d.h:27
IteratorType begin() noexcept
Gets an iterator to the beginning of the map.
Definition: map2d.h:92
TCell & at(const TROW &row_key, const TCOL &col_key)
Gets a value at a cell by row and column with bounds checking.
Definition: map2d.h:80
bool contains(const TROW &row_key, const TCOL &col_key) const
Determines whether the container contains a value.
Definition: map2d.h:58
ConstIteratorType end() const noexcept
Gets an read-only iterator to the element following the element of the map.
Definition: map2d.h:104
typename std::map< TROW, std::map< TCOL, TCell > >::const_iterator ConstIteratorType
Read-only map iterator.
Definition: map2d.h:16
ConstIteratorType cbegin() const noexcept
Gets an read-only iterator to the beginning of the map.
Definition: map2d.h:108
Map2d(std::map< TROW, std::map< TCOL, TCell >> &&data) noexcept
Initialises a new instance of the Map2d class.
Definition: map2d.h:23
std::map< TCOL, TCell > & row(const TROW &row_key)
Gets a row dataset.
Definition: map2d.h:69
std::size_t rows() const noexcept
Gets the number of rows.
Definition: map2d.h:35
ConstIteratorType begin() const noexcept
Gets an read-only iterator to the beginning of the map.
Definition: map2d.h:100
std::size_t columns() const noexcept
Gets the number of columns.
Definition: map2d.h:39
ConstIteratorType cend() const noexcept
Gets an read-only iterator to the element following the element of the map.
Definition: map2d.h:112
const TCell & at(const TROW &row_key, const TCOL &col_key) const
Gets a read-only value at a cell by row and column with bounds checking.
Definition: map2d.h:86
IteratorType end() noexcept
Gets an iterator to the element following the element of the map.
Definition: map2d.h:96
Top-level namespace for Health-GPS C++ API.
Definition: analysis_definition.h:8