MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
MultiArray.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_MULTIARRAY_HPP
2#define CDA_RAIL_MULTIARRAY_HPP
3
4#include "FixedSizeVector.hpp"
5
6#include <sstream>
7#include <stdexcept>
8#include <type_traits>
9
10namespace cda_rail {
11
28template <typename T> class MultiArray {
29private:
31 FixedSizeVector<T> m_data;
32
42 template <typename... Args>
43 [[nodiscard]] static FixedSizeVector<size_t> make_index_vector(Args... args) {
44 auto to_size_t_checked = []<typename T0>(T0 v) -> size_t {
45 using V = T0;
46 static_assert(std::is_integral_v<V>, "Indices/extents must be integral");
47 if constexpr (std::is_signed_v<V>) {
48 if (v < 0) {
49 throw std::invalid_argument("Negative index/extent is not allowed.");
50 }
51 }
52 return static_cast<size_t>(v);
53 };
54 return FixedSizeVector<size_t>{to_size_t_checked(args)...};
55 }
56
64 void check_args(const FixedSizeVector<size_t>& args) const;
65
76 [[nodiscard]] size_t flat_index(const FixedSizeVector<size_t>& args) const;
77
86 void init(const FixedSizeVector<size_t>& dims);
87
88public:
89 /*
90 * CONSTRUCTORS
91 */
92
104 template <typename... Args> explicit MultiArray(Args... args) {
105 init(make_index_vector(args...));
106 }
107
108 /*
109 * ELEMENT ACCESS
110 */
111
126 template <typename... Args> T& operator()(Args... args) {
127 return m_data.at(flat_index(make_index_vector(args...)));
128 }
129
140 template <typename... Args> [[nodiscard]] T& at(Args... args) {
141 return m_data.at(flat_index(make_index_vector(args...)));
142 }
143
154 template <typename... Args> [[nodiscard]] const T& at(Args... args) const {
155 return m_data.at(flat_index(make_index_vector(args...)));
156 }
157
158 /*
159 * SHAPE / SIZE QUERIES
160 */
161
167 [[nodiscard]] const FixedSizeVector<size_t>& get_shape() const {
168 return m_shape;
169 }
170
177 [[nodiscard]] size_t size() const { return m_data.size(); }
178
185 [[nodiscard]] size_t dimensions() const { return m_shape.size(); }
186
188 [[nodiscard]] const T* begin() const { return m_data.begin(); }
190 [[nodiscard]] const T* end() const { return m_data.end(); }
191};
192
204template <typename T>
205void MultiArray<T>::check_args(const FixedSizeVector<size_t>& args) const {
206 if (m_shape.size() != args.size()) {
207 throw std::invalid_argument(
208 "Number of dimensions and number of arguments do not coincide.");
209 }
210 for (size_t i = 0; i < args.size(); ++i) {
211 if (args.at(i) >= m_shape.at(i)) {
212 std::stringstream ss;
213 ss << "Index " << args.at(i) << " is too large for dimension " << i;
214 throw std::out_of_range(ss.str());
215 }
216 }
217}
218
228template <typename T>
229size_t MultiArray<T>::flat_index(const FixedSizeVector<size_t>& args) const {
230 check_args(args);
231 size_t index = 0;
232 size_t multiplier = 1;
233 for (size_t i = args.size(); i-- > 0;) {
234 index += args.at(i) * multiplier;
235 multiplier *= m_shape.at(i);
236 }
237 return index;
238}
239
246template <typename T>
247void MultiArray<T>::init(const FixedSizeVector<size_t>& dims) {
248 m_shape = dims;
249 size_t cap = 1;
250 for (const auto& d : dims) {
251 cap *= d;
252 }
253 m_data.delete_and_resize(cap);
254}
255
256} // namespace cda_rail
257
258#endif // CDA_RAIL_MULTIARRAY_HPP
A heap-allocated, fixed-size array whose capacity cannot change after construction.
Definition FixedSizeVector.hpp:32
T & operator()(Args... args)
Checked multi-dimensional element access (non-const).
Definition MultiArray.hpp:126
const T & at(Args... args) const
Bounds-checked multi-dimensional element access (const).
Definition MultiArray.hpp:154
const FixedSizeVector< size_t > & get_shape() const
Provides the shape of the array.
Definition MultiArray.hpp:167
const T * begin() const
Iterates over all elements in row-major order.
Definition MultiArray.hpp:188
size_t dimensions() const
Gets the number of dimensions.
Definition MultiArray.hpp:185
size_t size() const
Computes the total number of elements in the array.
Definition MultiArray.hpp:177
const T * end() const
The end of the iteration, see begin().
Definition MultiArray.hpp:190
MultiArray(Args... args)
Constructs a MultiArray with the given dimension extents.
Definition MultiArray.hpp:104
T & at(Args... args)
Bounds-checked multi-dimensional element access (non-const).
Definition MultiArray.hpp:140
Definition CustomExceptions.hpp:15