File: multiarray.h

package info (click to toggle)
martchus-cpp-utilities 5.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: cpp: 12,679; awk: 18; ansic: 12; makefile: 10
file content (192 lines) | stat: -rw-r--r-- 8,670 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef CPP_UTILITIES_MULTI_ARRAY_H
#define CPP_UTILITIES_MULTI_ARRAY_H

#include <array>
#include <tuple>
#include <vector>

namespace CppUtilities {

/// \cond
namespace Detail {
template <class Tuple, std::size_t N> struct DimensionsHelper {
    static std::size_t requiredSize(const Tuple &dimensionSizes)
    {
        return DimensionsHelper<Tuple, N - 1>::requiredSize(dimensionSizes) * static_cast<std::size_t>(std::get<N - 1>(dimensionSizes));
    }
    static std::size_t offset(const Tuple &dimensions, const Tuple &indices, std::size_t factor)
    {
        return DimensionsHelper<Tuple, N - 1>::offset(dimensions, indices, factor * static_cast<std::size_t>(std::get<N - 1>(dimensions)))
            + (factor * static_cast<std::size_t>(std::get<N - 1>(indices)));
    }
};
template <class Tuple> struct DimensionsHelper<Tuple, 1> {
    static std::size_t requiredSize(const Tuple &dimensionSizes)
    {
        return static_cast<std::size_t>(std::get<0>(dimensionSizes));
    }
    static std::size_t offset(const Tuple &, const Tuple &indices, std::size_t factor)
    {
        return factor * static_cast<std::size_t>(std::get<0>(indices));
    }
};
} // namespace Detail
/// \endcond

/// \brief The VectorBasedMultiArray struct allows using an std::vector with custom allocator as underlying container for the MultiArray class.
template <typename Allocator> struct VectorBasedMultiArray {
    template <typename T> using Type = std::vector<T, Allocator>;
    template <typename T> static constexpr Type<T> init(std::size_t requiredSize)
    {
        return Type<T>(requiredSize);
    }
};

/// \brief The VectorBasedMultiArray struct allows using an std::vector as underlying container for the MultiArray class.
template <> struct VectorBasedMultiArray<void> {
    template <typename T> using Type = std::vector<T, std::allocator<T>>;
    template <typename T> static constexpr Type<T> init(std::size_t requiredSize)
    {
        return Type<T>(requiredSize);
    }
};

/// \brief The ArrayBasedMultiArray struct allows using a fixed size array as underlying container for the MultiArray class.
template <std::size_t size> struct ArrayBasedMultiArray {
    template <typename T> using Type = std::array<T, size>;
    template <typename T> static constexpr Type<T> init(std::size_t)
    {
        return Type<T>();
    }
};

/// \brief The NoneOwningMultiArray struct allows using a caller-managed buffer array as underlying container for the MultiArray class.
struct NoneOwningMultiArray {
    template <typename T> using Type = T *;
    template <typename T> static constexpr Type<T> init(std::size_t)
    {
        return nullptr;
    }
};

/// \brief The MultiArray class provides an *N*-dimensional array.
/// \tparam T Specifies the type of the data the MultiArray is supposed to contain.
/// \tparam UnderlyingContainer Specifies the type of the underlying container to use.
/// \tparam Dimensions Specifies the types used to store the limit/size of the dimensions. Must be safely castable to std::size_t.
template <typename T, typename UnderlyingContainer, typename... Dimensions> class MultiArray {
public:
    MultiArray(Dimensions... dimensionSizes);
    std::size_t totalSize() const;
    static constexpr std::size_t dimensionCount();
    template <std::size_t index> std::size_t dimensionSize() const;
    T &at(Dimensions... indices);
    const T &at(Dimensions... indices) const;
    T *data();
    const T *data() const;
    typename UnderlyingContainer::template Type<T> &buffer();

private:
    using HelperType = Detail::DimensionsHelper<std::tuple<Dimensions...>, dimensionCount()>;
    const std::tuple<Dimensions...> m_dims;
    const std::size_t m_size;
    typename UnderlyingContainer::template Type<T> m_buff;
};

/// \brief Constructs a new *N*-dimensional array. The sizes for the dimensions are passed as arguments and must be greater than zero.
/// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes.
/// \sa makeMultiArray(), makeFixedSizeMultiArray() and makeNoneOwningMultiArray() for more convenient construction
template <typename T, typename UnderlyingContainer, typename... Dimensions>
MultiArray<T, UnderlyingContainer, Dimensions...>::MultiArray(Dimensions... dimensionSizes)
    : m_dims(std::make_tuple(dimensionSizes...))
    , m_size(HelperType::requiredSize(m_dims))
    , m_buff(UnderlyingContainer::template init<T>(m_size))
{
}

/// \brief Returns the total number of elements.
template <typename T, typename UnderlyingContainer, typename... Dimensions>
std::size_t MultiArray<T, UnderlyingContainer, Dimensions...>::totalSize() const
{
    return m_size;
}

/// \brief Returns the number of dimensions for that type of array.
template <typename T, typename UnderlyingContainer, typename... Dimensions>
constexpr std::size_t MultiArray<T, UnderlyingContainer, Dimensions...>::dimensionCount()
{
    return std::tuple_size<std::tuple<Dimensions...>>::value;
}

/// \brief Returns the number of elements in the specified dimension.
template <typename T, typename UnderlyingContainer, typename... Dimensions>
template <std::size_t index>
std::size_t MultiArray<T, UnderlyingContainer, Dimensions...>::dimensionSize() const
{
    return static_cast<std::size_t>(std::get<index>(m_dims));
}

/// \brief Returns the element at the position specified via \a indices.
/// \remarks The number of \a indices must equal dimensionCount().
template <typename T, typename UnderlyingContainer, typename... Dimensions>
T &MultiArray<T, UnderlyingContainer, Dimensions...>::at(Dimensions... indices)
{
    return m_buff[HelperType::offset(m_dims, std::make_tuple(indices...), 1)];
}

/// \brief Returns the element at the position specified via \a indices.
/// \remarks The number of \a indices must equal dimensionCount().
template <typename T, typename UnderlyingContainer, typename... Dimensions>
const T &MultiArray<T, UnderlyingContainer, Dimensions...>::at(Dimensions... indices) const
{
    return m_buff[HelperType::offset(m_dims, std::make_tuple(indices...), 1)];
}

/// \brief Returns a pointer to the raw data.
/// \remarks Intended for debugging purposes only. The underlying data structure might change in future versions.
template <typename T, typename UnderlyingContainer, typename... Dimensions> T *MultiArray<T, UnderlyingContainer, Dimensions...>::data()
{
    return m_buff.data();
}

/// \brief Returns a pointer to the raw data.
/// \remarks Intended for debugging purposes only. The underlying data structure might change in future versions.
template <typename T, typename UnderlyingContainer, typename... Dimensions> const T *MultiArray<T, UnderlyingContainer, Dimensions...>::data() const
{
    return m_buff.data();
}

/// \brief Allows accessing the underlying buffer directly.
/// \remarks Assign the custom buffer using this method when using NoneOwningMultiArray as UnderlyingContainer.
template <typename T, typename UnderlyingContainer, typename... Dimensions>
typename UnderlyingContainer::template Type<T> &MultiArray<T, UnderlyingContainer, Dimensions...>::buffer()
{
    return m_buff;
}

/// \brief Constructs a new *N*-dimensional array using an std::vector with std::allocator as underlying container.
///        The sizes for the dimensions are passed as arguments.
/// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes.
template <typename ValueType, typename... DimensionSizes> inline auto makeMultiArray(DimensionSizes... dimensionSizes)
{
    return MultiArray<ValueType, VectorBasedMultiArray<void>, DimensionSizes...>(dimensionSizes...);
}

/// \brief Constructs a new *N*-dimensional array using a fixed size array as underlying container.
///        The sizes for the dimensions are passed as arguments.
/// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes.
template <typename ValueType, std::size_t size, typename... DimensionSizes> inline auto makeFixedSizeMultiArray(DimensionSizes... dimensionSizes)
{
    return MultiArray<ValueType, ArrayBasedMultiArray<size>, DimensionSizes...>(dimensionSizes...);
}

/// \brief Constructs a new *N*-dimensional array using a caller-managed buffer as underlying container.
///        The sizes for the dimensions are passed as arguments.
/// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes.
template <typename ValueType, typename... DimensionSizes> inline auto makeNoneOwningMultiArray(DimensionSizes... dimensionSizes)
{
    return MultiArray<ValueType, NoneOwningMultiArray, DimensionSizes...>(dimensionSizes...);
}

} // namespace CppUtilities

#endif // CPP_UTILITIES_MULTI_ARRAY_H