LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
MArray.hpp
Go to the documentation of this file.
1 
9 #ifndef LLU_CONTAINERS_MARRAY_HPP_
10 #define LLU_CONTAINERS_MARRAY_HPP_
11 
12 #include <initializer_list>
13 #include <ostream>
14 #include <type_traits>
15 #include <utility>
16 #include <vector>
17 
20 #include "LLU/LibraryData.h"
21 #include "LLU/Utilities.hpp"
22 
23 namespace LLU {
24 
35  template<typename T>
36  class MArray : public IterableContainer<T> {
37  template<typename>
38  friend class MArray;
39 
40  public:
41  MArray() = default;
42 
47  explicit MArray(MArrayDimensions d) : dims(std::move(d)) {}
48 
54  template<typename U>
55  explicit MArray(const MArray<U>& other) : dims(other.dims) {}
56 
60  mint rank() const noexcept {
61  return dims.rank();
62  }
63 
67  [[nodiscard]] bool empty() const noexcept {
68  return dims.flatCount() == 0;
69  }
70 
74  mint dimension(mint index) const {
75  return dims.get(index);
76  }
77 
81  const MArrayDimensions& dimensions() const {
82  return dims;
83  }
84 
86 
91  T& operator[](const std::vector<mint>& indices) {
92  return (*this)[dims.getIndex(indices)];
93  }
94 
99  const T& operator[](const std::vector<mint>& indices) const {
100  return (*this)[dims.getIndex(indices)];
101  }
102 
108  T& at(mint index);
109 
115  const T& at(mint index) const;
116 
122  T& at(const std::vector<mint>& indices);
123 
129  const T& at(const std::vector<mint>& indices) const;
130 
131  private:
133  MArrayDimensions dims;
134 
135  mint getSize() const noexcept override {
136  return dims.flatCount();
137  }
138  };
139 
140  template<typename T>
141  T& MArray<T>::at(mint index) {
142  return (*this)[dims.getIndexChecked(index)];
143  }
144 
145  template<typename T>
146  const T& MArray<T>::at(mint index) const {
147  return (*this)[dims.getIndexChecked(index)];
148  }
149 
150  template<typename T>
151  T& MArray<T>::at(const std::vector<mint>& indices) {
152  return (*this)[dims.getIndexChecked(indices)];
153  }
154 
155  template<typename T>
156  const T& MArray<T>::at(const std::vector<mint>& indices) const {
157  return (*this)[dims.getIndexChecked(indices)];
158  }
159 
166  template<typename T>
167  std::ostream& operator<<(std::ostream& os, const MArray<T>& c) {
168  os << "{ ";
169  for (auto elem : c) {
170  os << elem << " ";
171  }
172  os << "}";
173  return os;
174  }
175 
176 } /* namespace LLU */
177 
178 #endif /* LLU_CONTAINERS_MARRAY_HPP_ */
LLU::MArray::operator[]
T & operator[](const std::vector< mint > &indices)
Get a reference to the data element at given position in a multidimensional container.
Definition: MArray.hpp:91
LLU::IterableContainer
Abstract class that provides iterators (c/r/begin and c/r/end methods) and subscript operator for any...
Definition: IterableContainer.hpp:20
LLU::MArrayDimensions::flatCount
mint flatCount() const noexcept
Get total number of elements.
Definition: MArrayDimensions.h:126
LLU::MArray::at
T & at(mint index)
Get a reference to the data element at given position with bound checking.
Definition: MArray.hpp:141
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LibraryData.h
LLU::MArrayDimensions::get
const std::vector< mint > & get() const noexcept
Get container dimensions in the form of const& to std::vector.
Definition: MArrayDimensions.h:86
LLU::MArrayDimensions
Helper class that carries meta-information about container's size and dimensions.
Definition: MArrayDimensions.h:23
LLU::MArray::at
const T & at(mint index) const
Get a constant reference to the data element at given position with bound checking.
Definition: MArray.hpp:146
LLU::MArrayDimensions::getIndex
mint getIndex(const std::vector< mint > &indices) const
Convert coordinates of an element in a multidimensional MArray to the corresponding index in a flat l...
Definition: MArrayDimensions.cpp:66
LLU::MArray::MArray
MArray(MArrayDimensions d)
Create new MArray given the dimensions object.
Definition: MArray.hpp:47
LLU::MArray
This is a class template, where template parameter T is the type of data elements....
Definition: MArray.hpp:36
LLU::MArray::MArray
MArray(const MArray< U > &other)
Converts given MArray of type U into MArray of type T.
Definition: MArray.hpp:55
LLU::MArray::dimensions
const MArrayDimensions & dimensions() const
Get a const reference to dimensions object.
Definition: MArray.hpp:81
LLU::MArrayDimensions::rank
mint rank() const noexcept
Get container rank.
Definition: MArrayDimensions.h:69
LLU::MArray::operator[]
const T & operator[](const std::vector< mint > &indices) const
Get a constant reference to the data element at given position in a multidimensional container.
Definition: MArray.hpp:99
LLU::MArrayDimensions::getIndexChecked
mint getIndexChecked(const std::vector< mint > &indices) const
Check if given coordinates are valid for this container.
Definition: MArrayDimensions.cpp:46
LLU::MArray::dimension
mint dimension(mint index) const
Get dimension value at position index.
Definition: MArray.hpp:74
LLU::MArray::empty
bool empty() const noexcept
Check whether container is empty.
Definition: MArray.hpp:67
LLU::MArray::at
T & at(const std::vector< mint > &indices)
Get a reference to the data element at given position in a multidimensional container.
Definition: MArray.hpp:151
LLU::MArray::rank
mint rank() const noexcept
Get container rank.
Definition: MArray.hpp:60
LLU::MArray::at
const T & at(const std::vector< mint > &indices) const
Get a constant reference to the data element at given position in a multidimensional container.
Definition: MArray.hpp:156
IterableContainer.hpp
Implementation of the IterableContainer class.
MArrayDimensions.h
LLU::operator<<
std::ostream & operator<<(std::ostream &os, const MArray< T > &c)
Insertion operator to allow pretty-printing of MArray.
Definition: MArray.hpp:167
Utilities.hpp
Short but generally useful functions.