NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
array3d.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <vector>
7
10namespace nnl {
11
12namespace utl {
28template <typename T>
29class Array3D {
30 public:
31 Array3D() noexcept {}
39 Array3D(std::size_t z, std::size_t y, std::size_t x) : sz_(z), sy_(y), sx_(x) { data_.resize(sz_ * sy_ * sx_, T()); }
48 void Resize(std::size_t z, std::size_t y, std::size_t x) {
49 sz_ = z;
50 sy_ = y;
51 sx_ = x;
52 data_.resize(sz_ * sy_ * sx_, T{});
53 }
54
59 std::size_t SizeZ() const noexcept { return sz_; }
65 std::size_t SizeY() const noexcept { return sy_; }
71 std::size_t SizeX() const noexcept { return sx_; }
80 T& At(std::size_t z, std::size_t y, std::size_t x) {
81 std::size_t index = GetIndex(z, y, x);
82 if (index >= data_.size()) NNL_THROW(RangeError(NNL_SRCTAG("index out of range: " + std::to_string(index))));
83 return data_.at(index);
84 }
85
93 const T& At(std::size_t z, std::size_t y, std::size_t x) const {
94 std::size_t index = GetIndex(z, y, x);
95 if (index >= data_.size()) NNL_THROW(RangeError(NNL_SRCTAG("index out of range: " + std::to_string(index))));
96 return data_.at(index);
97 }
98
106 T& operator()(std::size_t z, std::size_t y, std::size_t x) noexcept {
107 NNL_EXPECTS_DBG(GetIndex(z, y, x) < data_.size());
108 return data_[GetIndex(z, y, x)];
109 }
110
118 const T& operator()(std::size_t z, std::size_t y, std::size_t x) const noexcept {
119 NNL_EXPECTS_DBG(GetIndex(z, y, x) < data_.size());
120 return data_[GetIndex(z, y, x)];
121 }
122
123 private:
124 inline std::size_t GetIndex(std::size_t z, std::size_t y, std::size_t x) const noexcept {
125 return x + y * sx_ + sx_ * sy_ * z;
126 }
127
128 std::vector<T> data_;
129 std::size_t sz_;
130 std::size_t sy_;
131 std::size_t sx_;
132};
133
134} // namespace utl
135} // namespace nnl
Defines macros for Design-by-Contract verification.
Defines the library-wide exception hierarchy and error handling macros.
#define NNL_EXPECTS_DBG(precondition)
Debug-only precondition check.
Definition contract.hpp:59
Exception thrown for out-of-range errors.
Definition exception.hpp:111
#define NNL_THROW(object)
Throws an exception or terminates the program if exceptions are disabled.
Definition exception.hpp:46
void Resize(std::size_t z, std::size_t y, std::size_t x)
Resizes the 3D array to specified dimensions.
Definition array3d.hpp:48
std::size_t SizeX() const noexcept
Returns the size of the array in the x dimension.
Definition array3d.hpp:71
const T & At(std::size_t z, std::size_t y, std::size_t x) const
Accesses an element at specified coordinates with bounds checking.
Definition array3d.hpp:93
std::size_t SizeZ() const noexcept
Returns the size of the array in the z dimension.
Definition array3d.hpp:59
std::size_t SizeY() const noexcept
Returns the size of the array in the y dimension.
Definition array3d.hpp:65
const T & operator()(std::size_t z, std::size_t y, std::size_t x) const noexcept
Accesses an element at specified coordinates (no bounds checking).
Definition array3d.hpp:118
T & At(std::size_t z, std::size_t y, std::size_t x)
Accesses an element at specified coordinates with bounds checking.
Definition array3d.hpp:80
T & operator()(std::size_t z, std::size_t y, std::size_t x) noexcept
Accesses an element at specified coordinates (no bounds checking).
Definition array3d.hpp:106
Array3D(std::size_t z, std::size_t y, std::size_t x)
Constructs a 3D array with specified dimensions.
Definition array3d.hpp:39
Definition exception.hpp:56