| 1 | #cxx . . $ `<file>/misc/multiarray.h`/ifndef CPP_UTILITIES_MULTI_ARRAY_H |
| 2 | #define CPP_UTILITIES_MULTI_ARRAY_Hcxx . . $ `misc/multiarray.h:2:9`! |
| 3 | |
| 4 | #include <array> |
| 5 | #include <tuple> |
| 6 | #include <vector> |
| 7 | |
| 8 | namespace CppUtilitiescxx . . $ CppUtilities/ { |
| 9 | |
| 10 | /// \cond |
| 11 | namespace Detailcxx . . $ CppUtilities/Detail/ { |
| 12 | template <class Tuple, stdcxx . . $ std/::size_tcxx . . $ std/size_t# N> struct DimensionsHelpercxx . . $ CppUtilities/Detail/DimensionsHelper# { |
| 13 | static stdcxx . . $ std/::size_tcxx . . $ std/size_t# requiredSizecxx . . $ CppUtilities/Detail/DimensionsHelper#requiredSize(c6bf0951926639cd).(const Tuple &dimensionSizes) |
| 14 | { |
| 15 | return DimensionsHelpercxx . . $ CppUtilities/Detail/DimensionsHelper#<Tuple, N - 1>::requiredSize(dimensionSizes) * static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(stdcxx . . $ std/::getcxx . . $ std/get(5aabc72a970aa7ce).<N - 1>(dimensionSizes)); |
| 16 | } |
| 17 | static stdcxx . . $ std/::size_tcxx . . $ std/size_t# offsetcxx . . $ CppUtilities/Detail/DimensionsHelper#offset(e02e53361c09e59b).(const Tuple &dimensions, const Tuple &indices, stdcxx . . $ std/::size_tcxx . . $ std/size_t# factor) |
| 18 | { |
| 19 | return DimensionsHelpercxx . . $ CppUtilities/Detail/DimensionsHelper#<Tuple, N - 1>::offset(dimensions, indices, factor * static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(stdcxx . . $ std/::getcxx . . $ std/get(5aabc72a970aa7ce).<N - 1>(dimensions))) |
| 20 | + (factor * static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(stdcxx . . $ std/::getcxx . . $ std/get(5aabc72a970aa7ce).<N - 1>(indices))); |
| 21 | } |
| 22 | }; |
| 23 | template <class Tuple> struct DimensionsHelpercxx . . $ CppUtilities/Detail/DimensionsHelper#<Tuple, 1> { |
| 24 | static stdcxx . . $ std/::size_tcxx . . $ std/size_t# requiredSizecxx . . $ CppUtilities/Detail/DimensionsHelper#requiredSize(c6bf0951926639cd).(const Tuple &dimensionSizes) |
| 25 | { |
| 26 | return static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(stdcxx . . $ std/::getcxx . . $ std/get(5aabc72a970aa7ce).<0>(dimensionSizes)); |
| 27 | } |
| 28 | static stdcxx . . $ std/::size_tcxx . . $ std/size_t# offsetcxx . . $ CppUtilities/Detail/DimensionsHelper#offset(e02e53361c09e59b).(const Tuple &, const Tuple &indices, stdcxx . . $ std/::size_tcxx . . $ std/size_t# factor) |
| 29 | { |
| 30 | return factor * static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(stdcxx . . $ std/::getcxx . . $ std/get(5aabc72a970aa7ce).<0>(indices)); |
| 31 | } |
| 32 | }; |
| 33 | } // namespace Detail |
| 34 | /// \endcond |
| 35 | |
| 36 | /// \brief The VectorBasedMultiArray struct allows using an std::vector with custom allocator as underlying container for the MultiArray class. |
| 37 | template <typename Allocator> struct VectorBasedMultiArraycxx . . $ CppUtilities/VectorBasedMultiArray# { |
| 38 | template <typename T> using Typecxx . . $ CppUtilities/VectorBasedMultiArray#Type# = stdcxx . . $ std/::vectorcxx . . $ std/vector#<T, Allocator>; |
| 39 | template <typename T> static constexpr Typecxx . . $ CppUtilities/VectorBasedMultiArray#Type#<T> initcxx . . $ CppUtilities/VectorBasedMultiArray#init(c5eaf6ceb578656).(stdcxx . . $ std/::size_tcxx . . $ std/size_t# requiredSize) |
| 40 | { |
| 41 | return Typecxx . . $ CppUtilities/VectorBasedMultiArray#Type#<T>(requiredSize); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | /// \brief The VectorBasedMultiArray struct allows using an std::vector as underlying container for the MultiArray class. |
| 46 | template <> struct VectorBasedMultiArraycxx . . $ CppUtilities/VectorBasedMultiArray#<void> { |
| 47 | template <typename T> using Typecxx . . $ CppUtilities/VectorBasedMultiArray#Type# = stdcxx . . $ std/::vectorcxx . . $ std/vector#<T, stdcxx . . $ std/::allocatorcxx . . $ std/allocator#<T>>; |
| 48 | template <typename T> static constexpr Typecxx . . $ CppUtilities/VectorBasedMultiArray#Type#<T> initcxx . . $ CppUtilities/VectorBasedMultiArray#init(c2171b9e3fb6d522).(stdcxx . . $ std/::size_tcxx . . $ std/size_t# requiredSize) |
| 49 | { |
| 50 | return Typecxx . . $ CppUtilities/VectorBasedMultiArray#Type#<T>(requiredSize); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | /// \brief The ArrayBasedMultiArray struct allows using a fixed size array as underlying container for the MultiArray class. |
| 55 | template <stdcxx . . $ std/::size_tcxx . . $ std/size_t# size> struct ArrayBasedMultiArraycxx . . $ CppUtilities/ArrayBasedMultiArray# { |
| 56 | template <typename T> using Typecxx . . $ CppUtilities/ArrayBasedMultiArray#Type# = stdcxx . . $ std/::arraycxx . . $ std/array#<T, size>; |
| 57 | template <typename T> static constexpr Typecxx . . $ CppUtilities/ArrayBasedMultiArray#Type#<T> initcxx . . $ CppUtilities/ArrayBasedMultiArray#init(f2e395163f377e6f).(stdcxx . . $ std/::size_tcxx . . $ std/size_t#) |
| 58 | { |
| 59 | return Typecxx . . $ CppUtilities/ArrayBasedMultiArray#Type#<T>(); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | /// \brief The NoneOwningMultiArray struct allows using a caller-managed buffer array as underlying container for the MultiArray class. |
| 64 | struct NoneOwningMultiArraycxx . . $ CppUtilities/NoneOwningMultiArray# { |
| 65 | template <typename T> using Typecxx . . $ CppUtilities/NoneOwningMultiArray#Type# = T *; |
| 66 | template <typename T> static constexpr Typecxx . . $ CppUtilities/NoneOwningMultiArray#Type#<T> initcxx . . $ CppUtilities/NoneOwningMultiArray#init(60a54806b2015313).(stdcxx . . $ std/::size_tcxx . . $ std/size_t#) |
| 67 | { |
| 68 | return nullptr; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | /// \brief The MultiArray class provides an *N*-dimensional array. |
| 73 | /// \tparam T Specifies the type of the data the MultiArray is supposed to contain. |
| 74 | /// \tparam UnderlyingContainer Specifies the type of the underlying container to use. |
| 75 | /// \tparam Dimensions Specifies the types used to store the limit/size of the dimensions. Must be safely castable to std::size_t. |
| 76 | template <typename T, typename UnderlyingContainer, typename... Dimensions> class MultiArraycxx . . $ CppUtilities/MultiArray# { |
| 77 | public: |
| 78 | MultiArraycxx . . $ CppUtilities/MultiArray#`MultiArray<T, UnderlyingContainer, Dimensions...>`(d2c08e3b9b4d853a).(Dimensions... dimensionSizes); |
| 79 | stdcxx . . $ std/::size_tcxx . . $ std/size_t# totalSizecxx . . $ CppUtilities/MultiArray#totalSize(fe4d2d262d4164a8).() const; |
| 80 | static constexpr stdcxx . . $ std/::size_tcxx . . $ std/size_t# dimensionCountcxx . . $ CppUtilities/MultiArray#dimensionCount(f708dce9a8856c70).(); |
| 81 | template <stdcxx . . $ std/::size_tcxx . . $ std/size_t# index> stdcxx . . $ std/::size_tcxx . . $ std/size_t# dimensionSizecxx . . $ CppUtilities/MultiArray#dimensionSize(fe4d2d262d4164a8).() const; |
| 82 | T &atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(Dimensions... indices); |
| 83 | const T &atcxx . . $ CppUtilities/MultiArray#at(a8920a8db9f94b37).(Dimensions... indices) const; |
| 84 | T *datacxx . . $ CppUtilities/MultiArray#data(155dc7f103cdb9db).(); |
| 85 | const T *datacxx . . $ CppUtilities/MultiArray#data(1016ee5406d91ab9).() const; |
| 86 | typename UnderlyingContainer::template Type<T> &buffercxx . . $ CppUtilities/MultiArray#buffer(ff14a2e0facd5b9d).(); |
| 87 | |
| 88 | private: |
| 89 | using HelperTypecxx . . $ CppUtilities/MultiArray#HelperType# = Detailcxx . . $ CppUtilities/Detail/::DimensionsHelpercxx . . $ CppUtilities/Detail/DimensionsHelper#<stdcxx . . $ std/::tuplecxx . . $ std/tuple#<Dimensions...>, dimensionCountcxx . . $ CppUtilities/MultiArray#dimensionCount(f708dce9a8856c70).()>; |
| 90 | const stdcxx . . $ std/::tuplecxx . . $ std/tuple#<Dimensions...> m_dimscxx . . $ CppUtilities/MultiArray#m_dims.; |
| 91 | const stdcxx . . $ std/::size_tcxx . . $ std/size_t# m_sizecxx . . $ CppUtilities/MultiArray#m_size.; |
| 92 | typename UnderlyingContainer::template Type<T> m_buffcxx . . $ CppUtilities/MultiArray#m_buff.; |
| 93 | }; |
| 94 | |
| 95 | /// \brief Constructs a new *N*-dimensional array. The sizes for the dimensions are passed as arguments and must be greater than zero. |
| 96 | /// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes. |
| 97 | /// \sa makeMultiArray(), makeFixedSizeMultiArray() and makeNoneOwningMultiArray() for more convenient construction |
| 98 | template <typename T, typename UnderlyingContainer, typename... Dimensions> |
| 99 | MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::MultiArraycxx . . $ CppUtilities/MultiArray#`MultiArray<T, UnderlyingContainer, Dimensions...>`(d2c08e3b9b4d853a).(Dimensions... dimensionSizes) |
| 100 | : m_dimscxx . . $ CppUtilities/MultiArray#m_dims.(stdcxx . . $ std/::make_tuplecxx . . $ std/make_tuple(3964edbfd7e8599f).(dimensionSizes...)) |
| 101 | , m_sizecxx . . $ CppUtilities/MultiArray#m_size.(HelperTypecxx . . $ CppUtilities/MultiArray#HelperType#::requiredSize(m_dimscxx . . $ CppUtilities/MultiArray#m_dims.)) |
| 102 | , m_buffcxx . . $ CppUtilities/MultiArray#m_buff.(UnderlyingContainer::template init<T>(m_sizecxx . . $ CppUtilities/MultiArray#m_size.)) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | /// \brief Returns the total number of elements. |
| 107 | template <typename T, typename UnderlyingContainer, typename... Dimensions> |
| 108 | stdcxx . . $ std/::size_tcxx . . $ std/size_t# MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::totalSizecxx . . $ CppUtilities/MultiArray#totalSize(fe4d2d262d4164a8).() const |
| 109 | { |
| 110 | return m_sizecxx . . $ CppUtilities/MultiArray#m_size.; |
| 111 | } |
| 112 | |
| 113 | /// \brief Returns the number of dimensions for that type of array. |
| 114 | template <typename T, typename UnderlyingContainer, typename... Dimensions> |
| 115 | constexpr stdcxx . . $ std/::size_tcxx . . $ std/size_t# MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::dimensionCountcxx . . $ CppUtilities/MultiArray#dimensionCount(f708dce9a8856c70).() |
| 116 | { |
| 117 | return stdcxx . . $ std/::tuple_sizecxx . . $ std/tuple_size#<std::tuple<Dimensions...>>::value; |
| 118 | } |
| 119 | |
| 120 | /// \brief Returns the number of elements in the specified dimension. |
| 121 | template <typename T, typename UnderlyingContainer, typename... Dimensions> |
| 122 | template <stdcxx . . $ std/::size_tcxx . . $ std/size_t# index> |
| 123 | stdcxx . . $ std/::size_tcxx . . $ std/size_t# MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::dimensionSizecxx . . $ CppUtilities/MultiArray#dimensionSize(fe4d2d262d4164a8).() const |
| 124 | { |
| 125 | return static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(stdcxx . . $ std/::getcxx . . $ std/get(5aabc72a970aa7ce).<index>(m_dimscxx . . $ CppUtilities/MultiArray#m_dims.)); |
| 126 | } |
| 127 | |
| 128 | /// \brief Returns the element at the position specified via \a indices. |
| 129 | /// \remarks The number of \a indices must equal dimensionCount(). |
| 130 | template <typename T, typename UnderlyingContainer, typename... Dimensions> |
| 131 | T &MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::atcxx . . $ CppUtilities/MultiArray#at(505f80d3d9d61de5).(Dimensions... indices) |
| 132 | { |
| 133 | return m_buffcxx . . $ CppUtilities/MultiArray#m_buff.[HelperTypecxx . . $ CppUtilities/MultiArray#HelperType#::offset(m_dimscxx . . $ CppUtilities/MultiArray#m_dims., stdcxx . . $ std/::make_tuplecxx . . $ std/make_tuple(3964edbfd7e8599f).(indices...), 1)]; |
| 134 | } |
| 135 | |
| 136 | /// \brief Returns the element at the position specified via \a indices. |
| 137 | /// \remarks The number of \a indices must equal dimensionCount(). |
| 138 | template <typename T, typename UnderlyingContainer, typename... Dimensions> |
| 139 | const T &MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::atcxx . . $ CppUtilities/MultiArray#at(a8920a8db9f94b37).(Dimensions... indices) const |
| 140 | { |
| 141 | return m_buffcxx . . $ CppUtilities/MultiArray#m_buff.[HelperTypecxx . . $ CppUtilities/MultiArray#HelperType#::offset(m_dimscxx . . $ CppUtilities/MultiArray#m_dims., stdcxx . . $ std/::make_tuplecxx . . $ std/make_tuple(3964edbfd7e8599f).(indices...), 1)]; |
| 142 | } |
| 143 | |
| 144 | /// \brief Returns a pointer to the raw data. |
| 145 | /// \remarks Intended for debugging purposes only. The underlying data structure might change in future versions. |
| 146 | template <typename T, typename UnderlyingContainer, typename... Dimensions> T *MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::datacxx . . $ CppUtilities/MultiArray#data(155dc7f103cdb9db).() |
| 147 | { |
| 148 | return m_buffcxx . . $ CppUtilities/MultiArray#m_buff..data(); |
| 149 | } |
| 150 | |
| 151 | /// \brief Returns a pointer to the raw data. |
| 152 | /// \remarks Intended for debugging purposes only. The underlying data structure might change in future versions. |
| 153 | template <typename T, typename UnderlyingContainer, typename... Dimensions> const T *MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::datacxx . . $ CppUtilities/MultiArray#data(1016ee5406d91ab9).() const |
| 154 | { |
| 155 | return m_buffcxx . . $ CppUtilities/MultiArray#m_buff..data(); |
| 156 | } |
| 157 | |
| 158 | /// \brief Allows accessing the underlying buffer directly. |
| 159 | /// \remarks Assign the custom buffer using this method when using NoneOwningMultiArray as UnderlyingContainer. |
| 160 | template <typename T, typename UnderlyingContainer, typename... Dimensions> |
| 161 | typename UnderlyingContainer::template Type<T> &MultiArraycxx . . $ CppUtilities/MultiArray#<T, UnderlyingContainer, Dimensions...>::buffercxx . . $ CppUtilities/MultiArray#buffer(ff14a2e0facd5b9d).() |
| 162 | { |
| 163 | return m_buffcxx . . $ CppUtilities/MultiArray#m_buff.; |
| 164 | } |
| 165 | |
| 166 | /// \brief Constructs a new *N*-dimensional array using an std::vector with std::allocator as underlying container. |
| 167 | /// The sizes for the dimensions are passed as arguments. |
| 168 | /// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes. |
| 169 | template <typename ValueType, typename... DimensionSizes> inline auto makeMultiArraycxx . . $ CppUtilities/makeMultiArray(1fd8e6172299e45f).(DimensionSizes... dimensionSizes) |
| 170 | { |
| 171 | return MultiArraycxx . . $ CppUtilities/MultiArray#<ValueType, VectorBasedMultiArraycxx . . $ CppUtilities/VectorBasedMultiArray#<void>, DimensionSizes...>(dimensionSizes...); |
| 172 | } |
| 173 | |
| 174 | /// \brief Constructs a new *N*-dimensional array using a fixed size array as underlying container. |
| 175 | /// The sizes for the dimensions are passed as arguments. |
| 176 | /// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes. |
| 177 | template <typename ValueType, stdcxx . . $ std/::size_tcxx . . $ std/size_t# size, typename... DimensionSizes> inline auto makeFixedSizeMultiArraycxx . . $ CppUtilities/makeFixedSizeMultiArray(6918f5b3c54bb242).(DimensionSizes... dimensionSizes) |
| 178 | { |
| 179 | return MultiArraycxx . . $ CppUtilities/MultiArray#<ValueType, ArrayBasedMultiArraycxx . . $ CppUtilities/ArrayBasedMultiArray#<size>, DimensionSizes...>(dimensionSizes...); |
| 180 | } |
| 181 | |
| 182 | /// \brief Constructs a new *N*-dimensional array using a caller-managed buffer as underlying container. |
| 183 | /// The sizes for the dimensions are passed as arguments. |
| 184 | /// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes. |
| 185 | template <typename ValueType, typename... DimensionSizes> inline auto makeNoneOwningMultiArraycxx . . $ CppUtilities/makeNoneOwningMultiArray(1fd8e6172299e45f).(DimensionSizes... dimensionSizes) |
| 186 | { |
| 187 | return MultiArraycxx . . $ CppUtilities/MultiArray#<ValueType, NoneOwningMultiArraycxx . . $ CppUtilities/NoneOwningMultiArray#, DimensionSizes...>(dimensionSizes...); |
| 188 | } |
| 189 | |
| 190 | } // namespace CppUtilities |
| 191 | |
| 192 | #endif // CPP_UTILITIES_MULTI_ARRAY_H |