5 #include <fmt/format.h>
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");
30 data_.shrink_to_fit();
38 Array2D(
const size_t nrows,
const size_t ncols, TYPE value) :
Array2D(nrows, ncols) {
47 Array2D(
const size_t nrows,
const size_t ncols, std::vector<TYPE> &values)
50 if (values.size() !=
size()) {
51 throw std::invalid_argument(fmt::format(
52 "Array size and values size mismatch: {} vs. given {}.",
size(), values.size()));
55 std::copy(values.begin(), values.end(), data_.begin());
60 size_t size() const noexcept {
return rows_ * columns_; }
64 size_t rows() const noexcept {
return rows_; }
68 size_t columns() const noexcept {
return columns_; }
76 check_boundaries(row, column);
77 return data_[row * columns_ + column];
85 const TYPE &
operator()(
size_t row,
size_t column)
const {
86 check_boundaries(row, column);
87 return data_[row * columns_ + column];
92 void fill(TYPE value) { std::fill(data_.begin(), data_.end(), value); }
99 std::vector<TYPE>
to_vector() {
return std::vector<TYPE>(data_); }
104 std::stringstream ss;
105 ss <<
"Array: " << rows_ <<
"x" << columns_ <<
"\n";
106 for (
size_t i = 0; i < rows_; i++) {
108 for (
size_t j = 0; j < columns_; j++) {
110 if ((j + 1) != columns_)
123 std::vector<TYPE> data_;
125 void check_boundaries(
size_t row,
size_t column)
const {
127 throw std::out_of_range(
128 fmt::format(
"Row {} is out of array bounds [0, {}).", row, rows_));
131 if (column >= columns_) {
132 throw std::out_of_range(
133 fmt::format(
"Column {} is out of array bounds [0, {}).", column, columns_));
Defines a contiguous storage for two-dimensional numerical data in row-major format.
Definition: array2d.h:15
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
TYPE & operator()(size_t row, size_t column)
Gets a value from the array.
Definition: array2d.h:75
void fill(TYPE value)
Fill the array data with a default value.
Definition: array2d.h:92
const TYPE & operator()(size_t row, size_t column) const
Gets a read-only value from the array.
Definition: array2d.h:85
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
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