4#include <unordered_map>
14template <
template <
class...>
class TMap,
class TRow,
class TCol,
class TCell>
class Map2d {
21 Map2d(TMap<TRow, TMap<TCol, TCell>> &&data) noexcept : table_{std::move(data)} {}
24 using IteratorType =
typename TMap<TRow, TMap<TCol, TCell>>::iterator;
55 bool empty() const noexcept {
return table_.empty(); }
61 bool empty(
const TRow &row_key)
const {
return table_.at(row_key).empty(); }
65 std::size_t
rows() const noexcept {
return table_.size(); }
71 std::size_t
columns(
const TRow &row_key)
const {
return table_.at(row_key).size(); }
77 bool contains(
const TRow &row_key)
const noexcept {
return table_.contains(row_key); }
83 bool contains(
const TRow &row_key,
const TCol &col_key)
const {
84 if (!table_.contains(row_key)) {
87 return table_.at(row_key).contains(col_key);
93 TMap<TCol, TCell> &
at(
const TRow &row_key) {
return table_.at(row_key); }
98 const TMap<TCol, TCell> &
at(
const TRow &row_key)
const {
return table_.at(row_key); }
104 TCell &
at(
const TRow &row_key,
const TCol &col_key) {
return table_.at(row_key).at(col_key); }
110 const TCell &
at(
const TRow &row_key,
const TCol &col_key)
const {
111 return table_.at(row_key).at(col_key);
117 template <
class... FArgs>
auto insert_row(FArgs &&...args)
noexcept {
118 return table_.insert(std::forward<FArgs>(args)...);
125 template <
class FRow,
class... FArgs>
auto insert(FRow &&row_key, FArgs &&...args)
noexcept {
126 if (!table_.contains(row_key)) {
127 table_.insert(row_key, TMap<TCol, TCell>{});
129 return table_.at(std::forward<FRow>(row_key)).insert(std::forward<FArgs>(args)...);
135 template <
class... FArgs>
auto emplace_row(FArgs &&...args)
noexcept {
136 return table_.emplace(std::forward<FArgs>(args)...);
143 template <
class FRow,
class... FArgs>
auto emplace(FRow &&row_key, FArgs &&...args)
noexcept {
144 if (!table_.contains(row_key)) {
145 table_.emplace(row_key, TMap<TCol, TCell>{});
147 return table_.at(std::forward<FRow>(row_key)).emplace(std::forward<FArgs>(args)...);
151 TMap<TRow, TMap<TCol, TCell>> table_;
155template <
class TRow,
class TCol,
class TCell>
159template <
class TRow,
class TCol,
class TCell>
Defines the two-dimensional map (lookup table) data type.
Definition map2d.h:14
ConstIteratorType cend() const noexcept
Gets an read-only iterator to the element following the element of the map.
Definition map2d.h:51
const TMap< TCol, TCell > & at(const TRow &row_key) const
Gets a read-only row dataset with bounds checking.
Definition map2d.h:98
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:110
IteratorType begin() noexcept
Gets an iterator to the beginning of the map.
Definition map2d.h:28
ConstIteratorType begin() const noexcept
Gets an read-only iterator to the beginning of the map.
Definition map2d.h:39
auto insert_row(FArgs &&...args) noexcept
Insert a row into the table.
Definition map2d.h:117
std::size_t rows() const noexcept
Gets the number of rows.
Definition map2d.h:65
auto emplace(FRow &&row_key, FArgs &&...args) noexcept
Emplace a column into a given row of the table.
Definition map2d.h:143
bool empty(const TRow &row_key) const
Determine whether a given row container is empty.
Definition map2d.h:61
ConstIteratorType cbegin() const noexcept
Gets an read-only iterator to the beginning of the map.
Definition map2d.h:47
typename TMap< TRow, TMap< TCol, TCell > >::iterator IteratorType
Map row iterator.
Definition map2d.h:24
auto insert(FRow &&row_key, FArgs &&...args) noexcept
Insert a column into a given row of the table.
Definition map2d.h:125
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:104
bool contains(const TRow &row_key) const noexcept
Determines whether the map contains a row.
Definition map2d.h:77
bool contains(const TRow &row_key, const TCol &col_key) const
Determines whether the map contains a value.
Definition map2d.h:83
IteratorType end() noexcept
Gets an iterator to the element following the element of the map.
Definition map2d.h:32
bool empty() const noexcept
Determine whether the container is empty.
Definition map2d.h:55
Map2d()=default
Initialises a new instance of the Map2d class.
TMap< TCol, TCell > & at(const TRow &row_key)
Gets a row dataset with bounds checking.
Definition map2d.h:93
auto emplace_row(FArgs &&...args) noexcept
Emplace a row into the table.
Definition map2d.h:135
typename TMap< TRow, TMap< TCol, TCell > >::const_iterator ConstIteratorType
Read-only map row iterator.
Definition map2d.h:35
std::size_t columns(const TRow &row_key) const
Gets the number of columns at a given row.
Definition map2d.h:71
ConstIteratorType end() const noexcept
Gets an read-only iterator to the element following the element of the map.
Definition map2d.h:43
Map2d(TMap< TRow, TMap< TCol, TCell > > &&data) noexcept
Initialises a new instance of the Map2d class from a standard C++ 2D mapping.
Definition map2d.h:21
Top-level namespace for Health-GPS Console host application.
Definition command_options.cpp:8