|
| 1 | +#include <cassert> |
| 2 | +#include <iostream> |
| 3 | +#include <stdexcept> |
| 4 | +#include <string> |
| 5 | +#include <tuple> |
| 6 | +#include <type_traits> |
| 7 | + |
| 8 | +#if __cplusplus == 201402L |
| 9 | +#define CXX14_CONSTEXPR constexpr |
| 10 | +#else |
| 11 | +#define CXX14_CONSTEXPR |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace meta { |
| 15 | + |
| 16 | +// product |
| 17 | + |
| 18 | +template <typename T, T...> |
| 19 | +struct product; |
| 20 | + |
| 21 | +template <typename T, T dim0, T... dim> |
| 22 | +struct product<T, dim0, dim...> { |
| 23 | + static constexpr T const value = dim0 * product<T, dim...>::value; |
| 24 | +}; |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +struct product<T> { |
| 28 | + static constexpr T const value = 1; |
| 29 | +}; |
| 30 | + |
| 31 | +// less |
| 32 | + |
| 33 | +template <typename T, T a, T b> |
| 34 | +struct less { |
| 35 | + static constexpr bool const value = a < b; |
| 36 | +}; |
| 37 | + |
| 38 | +// pack_element |
| 39 | + |
| 40 | +template <std::size_t index, typename T, T head, T... pack> |
| 41 | +struct pack_element { |
| 42 | + static_assert(index < sizeof...(pack) + 1, "index out of bounds"); |
| 43 | + static constexpr T const value = pack_element<index - 1, T, pack...>::value; |
| 44 | +}; |
| 45 | + |
| 46 | +template <typename T, T head, T... pack> |
| 47 | +struct pack_element<0, T, head, pack...> { |
| 48 | + static constexpr T const value = head; |
| 49 | +}; |
| 50 | + |
| 51 | +// all |
| 52 | + |
| 53 | +template <bool...> |
| 54 | +struct all; |
| 55 | + |
| 56 | +template <bool... b> |
| 57 | +struct all<true, b...> { |
| 58 | + static constexpr bool const value = true && all<b...>::value; |
| 59 | +}; |
| 60 | + |
| 61 | +template <bool... b> |
| 62 | +struct all<false, b...> { |
| 63 | + static constexpr bool const value = false && all<b...>::value; |
| 64 | +}; |
| 65 | + |
| 66 | +template <> |
| 67 | +struct all<> { |
| 68 | + static constexpr bool const value = true; |
| 69 | +}; |
| 70 | + |
| 71 | +// index_sequence |
| 72 | + |
| 73 | +// https://stackoverflow.com/a/24481400/1944004 |
| 74 | +template <std::size_t... I> |
| 75 | +struct index_sequence {}; |
| 76 | + |
| 77 | +template <std::size_t N, std::size_t... I> |
| 78 | +struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {}; |
| 79 | + |
| 80 | +template <std::size_t... I> |
| 81 | +struct make_index_sequence<0, I...> : public index_sequence<I...> {}; |
| 82 | + |
| 83 | +} // namespace meta |
| 84 | + |
| 85 | +namespace math { |
| 86 | + |
| 87 | +template <typename T, std::size_t... N> |
| 88 | +class multi_array { |
| 89 | +public: |
| 90 | + using value_type = T; |
| 91 | + using size_type = std::size_t; |
| 92 | + using difference_type = std::ptrdiff_t; |
| 93 | + using reference = value_type &; |
| 94 | + using const_reference = const value_type &; |
| 95 | + using pointer = value_type *; |
| 96 | + using const_pointer = const value_type *; |
| 97 | + using iterator = pointer; |
| 98 | + |
| 99 | +private: |
| 100 | + template <size_type... s> |
| 101 | + using size_product = meta::product<size_type, s...>; |
| 102 | + |
| 103 | + template <size_type a, size_type b> |
| 104 | + using size_less = meta::less<size_type, a, b>; |
| 105 | + |
| 106 | + template <size_type i, size_type... pack> |
| 107 | + using size_element = meta::pack_element<i, size_type, pack...>; |
| 108 | + |
| 109 | + template <size_type... I, typename... Idx> |
| 110 | + CXX14_CONSTEXPR size_type linearized_index(meta::index_sequence<I...>, |
| 111 | + Idx... idx) const { |
| 112 | + size_type index = 0; |
| 113 | + using unpack = size_type[]; |
| 114 | + (void)unpack{0UL, |
| 115 | + ((void)(index = (index + unpack{size_type(idx)...}[I]) * |
| 116 | + size_element<I + 1, N...>::value), |
| 117 | + 0UL)...}; |
| 118 | + return index + unpack{size_type(idx)...}[sizeof...(idx) - 1]; |
| 119 | + } |
| 120 | + |
| 121 | +#ifndef NDEBUG |
| 122 | +#define check_bounds(a, b) check_bounds_impl(a, b) |
| 123 | +#else |
| 124 | +#define check_bounds(a, b) |
| 125 | +#endif // NDEBUG |
| 126 | + |
| 127 | + template <size_type... I, typename... Idx> |
| 128 | + CXX14_CONSTEXPR void check_bounds_impl(meta::index_sequence<I...>, |
| 129 | + Idx... idx) const { |
| 130 | + using unpack = size_type[]; |
| 131 | + (void)unpack{ |
| 132 | + 0UL, |
| 133 | + ((void)(unpack{size_type(idx)...}[I] < size_element<I, N...>::value |
| 134 | + ? 0 |
| 135 | + : throw std::out_of_range("index out of bounds: " + |
| 136 | + std::to_string(I))), |
| 137 | + 0UL)...}; |
| 138 | + } |
| 139 | + |
| 140 | + // Storage |
| 141 | + value_type m_data[size_product<N...>::value]; |
| 142 | + |
| 143 | +public: |
| 144 | + constexpr multi_array() {} |
| 145 | + |
| 146 | + template <typename... U> |
| 147 | + constexpr multi_array(U... data) : m_data{value_type(data)...} {} |
| 148 | + |
| 149 | + void fill(T &&initializer) { |
| 150 | + for (iterator it = begin(); it != end(); ++it) { |
| 151 | + *it = initializer; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + void swap(multi_array &other) { |
| 156 | + // noexcept(std::is_nothrow_swappable<value_type>::value) |
| 157 | + iterator oit = other.begin(); |
| 158 | + for (iterator it = begin(); it != end(); ++it, (void)++oit) { |
| 159 | + value_type tmp = std::move(*it); |
| 160 | + *it = std::move(*oit); |
| 161 | + *oit = std::move(tmp); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + constexpr iterator begin() const noexcept { return iterator(data()); } |
| 166 | + |
| 167 | + constexpr iterator end() const noexcept { |
| 168 | + return iterator(data() + size_product<N...>::value); |
| 169 | + } |
| 170 | + |
| 171 | + pointer data() noexcept { return m_data; } |
| 172 | + const_pointer data() const noexcept { return m_data; } |
| 173 | + |
| 174 | + template <typename... Idx> |
| 175 | + CXX14_CONSTEXPR reference operator()(Idx... idx) noexcept { |
| 176 | + static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch"); |
| 177 | + static_assert( |
| 178 | + meta::all<std::is_convertible<Idx, size_type>::value...>::value, |
| 179 | + "type mismatch"); |
| 180 | + check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...); |
| 181 | + size_type index = linearized_index( |
| 182 | + meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...); |
| 183 | + return m_data[index]; |
| 184 | + } |
| 185 | + |
| 186 | + template <typename... Idx> |
| 187 | + CXX14_CONSTEXPR value_type operator()(Idx... idx) const noexcept { |
| 188 | + static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch"); |
| 189 | + static_assert( |
| 190 | + meta::all<std::is_convertible<Idx, size_type>::value...>::value, |
| 191 | + "type mismatch"); |
| 192 | + check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...); |
| 193 | + size_type index = linearized_index( |
| 194 | + meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...); |
| 195 | + return m_data[index]; |
| 196 | + } |
| 197 | + |
| 198 | + template <size_type... idx> |
| 199 | + CXX14_CONSTEXPR reference get() noexcept { |
| 200 | + static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch"); |
| 201 | + static_assert(meta::all<size_less<idx, N>::value...>::value, |
| 202 | + "index out of bounds"); |
| 203 | + check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...); |
| 204 | + size_type index = linearized_index( |
| 205 | + meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...); |
| 206 | + return m_data[index]; |
| 207 | + } |
| 208 | + |
| 209 | + template <size_type... idx> |
| 210 | + CXX14_CONSTEXPR value_type get() const noexcept { |
| 211 | + static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch"); |
| 212 | + static_assert(meta::all<size_less<idx, N>::value...>::value, |
| 213 | + "index out of bounds"); |
| 214 | + check_bounds(meta::make_index_sequence<sizeof...(idx)>{}, idx...); |
| 215 | + size_type index = linearized_index( |
| 216 | + meta::make_index_sequence<sizeof...(idx) - 1>{}, idx...); |
| 217 | + return m_data[index]; |
| 218 | + } |
| 219 | + |
| 220 | + template <size_type i> |
| 221 | + constexpr size_type size() const noexcept { |
| 222 | + static_assert(i < sizeof...(N), "index out of bounds"); |
| 223 | + return size_element<i, N...>::value; |
| 224 | + } |
| 225 | + |
| 226 | + constexpr size_type size() const noexcept { |
| 227 | + return size_product<N...>::value; |
| 228 | + } |
| 229 | + |
| 230 | +#undef check_bounds |
| 231 | +}; |
| 232 | + |
| 233 | +} // namespace math |
| 234 | + |
| 235 | +#undef CXX14_CONSTEXPR |
| 236 | + |
| 237 | +int main() { |
| 238 | + math::multi_array<double, 2, 2, 2> a = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 239 | + |
| 240 | + for (std::size_t i = 0; i < a.size<0>(); ++i) { |
| 241 | + for (std::size_t j = 0; j < a.size<1>(); ++j) { |
| 242 | + for (std::size_t k = 0; k < a.size<2>(); ++k) { |
| 243 | + std::cout << a(i, j, k) << '\n'; |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + constexpr math::multi_array<double, 2, 2> b = { 0,0,0,1 }; |
| 249 | + static_assert(b(1,1) == 1, "!"); |
| 250 | + static_assert(b.get<1,1>() == 1, "!"); |
| 251 | +} |