Skip to content

Commit e5f4886

Browse files
committed
mutation through restriction
1 parent b6ad0c0 commit e5f4886

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

include/boost/multi/restriction.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ struct bind_front_t {
108108

109109
// bind_front_t(multi::index idx, Proj& proj) : idx_{idx}, proj_{proj} {}
110110
template<class... Args>
111-
BOOST_MULTI_HD constexpr auto operator()(Args&&... rest) const noexcept { return proj_(idx_, std::forward<Args>(rest)...); }
111+
BOOST_MULTI_HD constexpr auto operator()(Args&&... rest) const& noexcept { return proj_(idx_, std::forward<Args>(rest)...); }
112+
113+
template<class... Args>
114+
BOOST_MULTI_HD constexpr auto operator()(Args&&... rest)& noexcept { return proj_(idx_, std::forward<Args>(rest)...); }
115+
116+
template<class... Args>
117+
BOOST_MULTI_HD constexpr auto operator()(Args&&... rest)&& noexcept { return proj_(idx_, std::forward<Args>(rest)...); }
112118
};
113119

114120
template<dimensionality_type D, class Proj>
@@ -419,6 +425,18 @@ class restriction : std::conditional_t<std::is_reference_v<Proj>, detail::non_co
419425
}
420426
}
421427

428+
BOOST_MULTI_HD constexpr auto operator[](index idx) & -> decltype(auto) {
429+
// assert( extent().contains(idx) );
430+
if constexpr(D != 1) {
431+
// auto ll = [idx, proj = proj_](auto... rest) { return proj(idx, rest...); };
432+
// return restriction<D - 1, decltype(ll)>(extents_t<D - 1>(xs_.base().tail()), ll);
433+
// return [idx, proj = proj_](auto... rest) noexcept { return proj(idx, rest...); } ^ extents_t<D - 1>(xs_.base().tail());
434+
return bind_front_t<Proj>{idx, proj_} ^ extents_t<D - 1>(xs_.base().tail());
435+
} else {
436+
return proj_(idx);
437+
}
438+
}
439+
422440
BOOST_MULTI_HD constexpr auto operator[](index idx) const& -> decltype(auto) {
423441
// assert( extent().contains(idx) );
424442
if constexpr(D != 1) {

test/flattened.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ auto main() -> int { // NOLINT(readability-function-cognitive-complexity,bugpro
314314
BOOST_TEST( arr.elements()[2] == 2 );
315315
// BOOST_TEST( arr.elements()[3] = 22 ); // doesn't compile, good
316316
}
317+
{
318+
multi::array<double, 3> arr = {
319+
{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}},
320+
{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}},
321+
{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}},
322+
{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}}
323+
};
324+
325+
BOOST_TEST( arr.flattened().size() == 8 );
326+
}
317327

318328
return boost::report_errors();
319329
}

test/restrictions.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <boost/multi/array.hpp>
99
#include <boost/multi/elementwise.hpp>
1010

11+
#include <boost/multi/detail/what.hpp>
12+
1113
#include <algorithm> // IWYU pragma: keep // for std::equal
1214
#include <cmath> // for std::abs
1315
#include <concepts> // for constructible_from // NOLINT(misc-include-cleaner) // IWYU pragma: keep
@@ -300,6 +302,30 @@ auto main() -> int {
300302
multi::array<double, 3> arr;
301303
arr = [](auto i, auto j, auto k) { return static_cast<double>(i + j + k); } ^ multi::extents_t<3>(2, 3, 4);
302304
}
305+
{
306+
std::vector<int> vec = {1, 2, 3};
307+
308+
auto&& arr = [&vec](auto i) -> int& { return vec[i];} ^ multi::extents_t<1>(vec.size());
309+
310+
// multi::detail::what(arr[1], arr[1][1]);
311+
arr[1] = 99;
312+
313+
BOOST_TEST( vec[1] == 99 );
314+
}
315+
316+
// {
317+
// std::vector<std::vector<int>> vvec = {
318+
// {1, 2, 3},
319+
// {4, 5, 6}
320+
// };
321+
322+
// auto&& arr = [p = &vvec](auto i, auto j) -> int& { return (*p)[i][j];} ^ multi::extents_t<2>(vvec.size(), vvec.front().size());
323+
324+
// // multi::detail::what(arr[1], arr[1][1]);
325+
// arr[1][1] = 99;
326+
327+
// BOOST_TEST( vvec[1][1] == 99 );
328+
// }
303329

304330
return boost::report_errors();
305331
}

0 commit comments

Comments
 (0)