Health-GPS 3.0.0.0
Global Health Policy Simulation model (Health-GPS)
Loading...
Searching...
No Matches
array2d.h
Go to the documentation of this file.
1#pragma once
2#include "forward_type.h"
3
4#include <algorithm>
5#include <fmt/format.h>
6#include <memory>
7#include <sstream>
8#include <stdexcept>
9#include <vector>
10
11namespace hgps::core {
12
15template <Numerical TYPE> class Array2D {
16 public:
18 Array2D() = default;
19
24 Array2D(const size_t nrows, const size_t ncols)
25 : rows_{nrows}, columns_{ncols}, data_(nrows * ncols) {
26 if (nrows <= 0 || ncols <= 0) {
27 throw std::invalid_argument("Invalid array constructor with 0 size");
28 }
29
30 data_.shrink_to_fit();
31 }
32
38 Array2D(const size_t nrows, const size_t ncols, TYPE value) : Array2D(nrows, ncols) {
39 fill(value);
40 }
41
47 Array2D(const size_t nrows, const size_t ncols, std::vector<TYPE> &values)
48 : Array2D(nrows, ncols) {
49
50 if (values.size() != size()) {
51 throw std::invalid_argument(fmt::format(
52 "Array size and values size mismatch: {} vs. given {}.", size(), values.size()));
53 }
54
55 std::copy(values.begin(), values.end(), data_.begin());
56 }
57
60 size_t size() const noexcept { return rows_ * columns_; }
61
64 size_t rows() const noexcept { return rows_; }
65
68 size_t columns() const noexcept { return columns_; }
69
75 TYPE &operator()(size_t row, size_t column) {
76 check_boundaries(row, column);
77 return data_[row * columns_ + column];
78 }
79
85 const TYPE &operator()(size_t row, size_t column) const {
86 check_boundaries(row, column);
87 return data_[row * columns_ + column];
88 }
89
92 void fill(TYPE value) { std::fill(data_.begin(), data_.end(), value); }
93
95 void clear() { fill(TYPE{}); }
96
99 std::vector<TYPE> to_vector() { return std::vector<TYPE>(data_); }
100
103 std::string to_string() noexcept {
104 std::stringstream ss;
105 ss << "Array: " << rows_ << "x" << columns_ << "\n";
106 for (size_t i = 0; i < rows_; i++) {
107 ss << "{";
108 for (size_t j = 0; j < columns_; j++) {
109 ss << operator()(i, j);
110 if ((j + 1) != columns_)
111 ss << ", ";
112 }
113
114 ss << "}\n";
115 }
116
117 return ss.str();
118 }
119
120 private:
121 size_t rows_{};
122 size_t columns_{};
123 std::vector<TYPE> data_;
124
125 void check_boundaries(size_t row, size_t column) const {
126 if (row >= rows_) {
127 throw std::out_of_range(
128 fmt::format("Row {} is out of array bounds [0, {}).", row, rows_));
129 }
130
131 if (column >= columns_) {
132 throw std::out_of_range(
133 fmt::format("Column {} is out of array bounds [0, {}).", column, columns_));
134 }
135 }
136};
137
140
143
146} // namespace hgps::core
Defines a contiguous storage for two-dimensional numerical data in row-major format.
Definition array2d.h:15
const TYPE & operator()(size_t row, size_t column) const
Gets a read-only value from the array.
Definition array2d.h:85
std::string to_string() noexcept
Creates a string representation of the array data.
Definition array2d.h:103
Array2D(const size_t nrows, const size_t ncols, std::vector< TYPE > &values)
Initialises new instance of the Array2D class with custom values.
Definition array2d.h:47
std::vector< TYPE > to_vector()
Creates a vector copy of the array data.
Definition array2d.h:99
void fill(TYPE value)
Fill the array data with a default value.
Definition array2d.h:92
Array2D()=default
Initialises new instance of the Array2D class with no storage.
size_t size() const noexcept
Gets the total size of the array storage.
Definition array2d.h:60
TYPE & operator()(size_t row, size_t column)
Gets a value from the array.
Definition array2d.h:75
size_t rows() const noexcept
Gets the number of rows in the array.
Definition array2d.h:64
Array2D(const size_t nrows, const size_t ncols)
Initialises new instance of the Array2D class.
Definition array2d.h:24
Array2D(const size_t nrows, const size_t ncols, TYPE value)
Initialises new instance of the Array2D class with custom default value.
Definition array2d.h:38
void clear()
Clears the array data with the default type value.
Definition array2d.h:95
size_t columns() const noexcept
Gets the number of coluns in the array.
Definition array2d.h:68
Top-level namespace for Health-GPS Core C++ API.
Definition analysis.h:7