1#ifndef CDA_RAIL_MULTIARRAY_HPP
2#define CDA_RAIL_MULTIARRAY_HPP
42 template <
typename... Args>
44 auto to_size_t_checked = []<
typename T0>(T0 v) ->
size_t {
46 static_assert(std::is_integral_v<V>,
"Indices/extents must be integral");
47 if constexpr (std::is_signed_v<V>) {
49 throw std::invalid_argument(
"Negative index/extent is not allowed.");
52 return static_cast<size_t>(v);
104 template <
typename... Args>
explicit MultiArray(Args... args) {
105 init(make_index_vector(args...));
127 return m_data.at(flat_index(make_index_vector(args...)));
140 template <
typename... Args> [[nodiscard]] T&
at(Args... args) {
141 return m_data.at(flat_index(make_index_vector(args...)));
154 template <
typename... Args> [[nodiscard]]
const T&
at(Args... args)
const {
155 return m_data.at(flat_index(make_index_vector(args...)));
177 [[nodiscard]]
size_t size()
const {
return m_data.size(); }
185 [[nodiscard]]
size_t dimensions()
const {
return m_shape.size(); }
188 [[nodiscard]]
const T*
begin()
const {
return m_data.begin(); }
190 [[nodiscard]]
const T*
end()
const {
return m_data.end(); }
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.");
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());
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);
250 for (
const auto& d : dims) {
253 m_data.delete_and_resize(cap);
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