@@ -68,17 +68,53 @@ struct all<> { |
68 | 68 | static constexpr bool const value = true; |
69 | 69 | }; |
70 | 70 |
|
71 | | -// index_sequence |
| 71 | +// linearized_index |
72 | 72 |
|
73 | | -// https://stackoverflow.com/a/24481400/1944004 |
74 | | -template <std::size_t... I> |
75 | | -struct index_sequence {}; |
| 73 | +template <size_t n, size_t... N> |
| 74 | +struct linearized_index { |
| 75 | + template <typename... Idx> |
| 76 | + constexpr std::size_t operator()(Idx... idx) const { |
| 77 | + using unpack = std::size_t[]; |
| 78 | + return unpack{std::size_t(idx)...}[n] + |
| 79 | + unpack{std::size_t(N)...}[n] * |
| 80 | + linearized_index<n - 1, N...>{}(idx...); |
| 81 | + } |
| 82 | +}; |
76 | 83 |
|
77 | | -template <std::size_t N, std::size_t... I> |
78 | | -struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {}; |
| 84 | +template <size_t... N> |
| 85 | +struct linearized_index<0, N...> { |
| 86 | + template <typename... Idx> |
| 87 | + constexpr std::size_t operator()(Idx... idx) const { |
| 88 | + using unpack = std::size_t[]; |
| 89 | + return unpack{std::size_t(idx)...}[0]; |
| 90 | + } |
| 91 | +}; |
79 | 92 |
|
80 | | -template <std::size_t... I> |
81 | | -struct make_index_sequence<0, I...> : public index_sequence<I...> {}; |
| 93 | +// check_bounds |
| 94 | + |
| 95 | +template <size_t n, size_t... N> |
| 96 | +struct check_bounds { |
| 97 | + template <typename... Idx> |
| 98 | + constexpr bool operator()(Idx... idx) const { |
| 99 | + using unpack = std::size_t[]; |
| 100 | + return unpack{std::size_t(idx)...}[n] < unpack{std::size_t(N)...}[n] |
| 101 | + ? check_bounds<n - 1, N...>{}(idx...) |
| 102 | + : throw std::out_of_range("index out of bounds: " + |
| 103 | + std::to_string(n)); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +template <size_t... N> |
| 108 | +struct check_bounds<0, N...> { |
| 109 | + template <typename... Idx> |
| 110 | + constexpr bool operator()(Idx... idx) const { |
| 111 | + using unpack = std::size_t[]; |
| 112 | + return unpack{std::size_t(idx)...}[0] < unpack{std::size_t(N)...}[0] |
| 113 | + ? false |
| 114 | + : throw std::out_of_range("index out of bounds: " + |
| 115 | + std::to_string(0)); |
| 116 | + } |
| 117 | +}; |
82 | 118 |
|
83 | 119 | } // namespace meta |
84 | 120 |
|
@@ -106,37 +142,6 @@ class multi_array { |
106 | 142 | template <size_type i, size_type... pack> |
107 | 143 | using size_element = meta::pack_element<i, size_type, pack...>; |
108 | 144 |
|
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 | 145 | // Storage |
141 | 146 | value_type m_data[size_product<N...>::value]; |
142 | 147 |
|
@@ -177,44 +182,40 @@ class multi_array { |
177 | 182 | static_assert( |
178 | 183 | meta::all<std::is_convertible<Idx, size_type>::value...>::value, |
179 | 184 | "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]; |
| 185 | + return |
| 186 | +#ifndef NDEBUG |
| 187 | + meta::check_bounds<sizeof...(idx) - 1, N...>{}(idx...), |
| 188 | +#endif |
| 189 | + m_data[meta::linearized_index<sizeof...(idx) - 1, N...>{}(idx...)]; |
184 | 190 | } |
185 | 191 |
|
186 | 192 | template <typename... Idx> |
187 | | - CXX14_CONSTEXPR value_type operator()(Idx... idx) const noexcept { |
| 193 | + constexpr value_type operator()(Idx... idx) const noexcept { |
188 | 194 | static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch"); |
189 | 195 | static_assert( |
190 | 196 | meta::all<std::is_convertible<Idx, size_type>::value...>::value, |
191 | 197 | "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]; |
| 198 | + return |
| 199 | +#ifndef NDEBUG |
| 200 | + meta::check_bounds<sizeof...(idx) - 1, N...>{}(idx...), |
| 201 | +#endif |
| 202 | + m_data[meta::linearized_index<sizeof...(idx) - 1, N...>{}(idx...)]; |
196 | 203 | } |
197 | 204 |
|
198 | 205 | template <size_type... idx> |
199 | 206 | CXX14_CONSTEXPR reference get() noexcept { |
200 | 207 | static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch"); |
201 | 208 | static_assert(meta::all<size_less<idx, N>::value...>::value, |
202 | 209 | "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]; |
| 210 | + return this->operator()(idx...); |
207 | 211 | } |
208 | 212 |
|
209 | 213 | template <size_type... idx> |
210 | | - CXX14_CONSTEXPR value_type get() const noexcept { |
| 214 | + constexpr value_type get() const noexcept { |
211 | 215 | static_assert(sizeof...(idx) == sizeof...(N), "dimension mismatch"); |
212 | 216 | static_assert(meta::all<size_less<idx, N>::value...>::value, |
213 | 217 | "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 | + return this->operator()(idx...); |
218 | 219 | } |
219 | 220 |
|
220 | 221 | template <size_type i> |
@@ -226,8 +227,6 @@ class multi_array { |
226 | 227 | constexpr size_type size() const noexcept { |
227 | 228 | return size_product<N...>::value; |
228 | 229 | } |
229 | | - |
230 | | -#undef check_bounds |
231 | 230 | }; |
232 | 231 |
|
233 | 232 | } // namespace math |
|