2424#include < cstring>
2525#include < functional>
2626#include < utility>
27+ #include < algorithm>
2728
2829namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
2930namespace MDSPAN_IMPL_PROPOSED_NAMESPACE {
3031namespace detail {
3132
3233template <class Extents , class F , class ArrayType >
33- constexpr void apply_fun_over_extents (const Extents &ext, F &fun,
34+ constexpr void apply_fun_over_extents (const Extents &ext, F && fun,
3435 ArrayType &indices,
3536 std::index_sequence<>) {
36- std::apply (fun, indices);
37+ std::apply (std::forward<F>( fun) , indices);
3738}
3839
3940template <class Extents , class F , class ArrayType , size_t R, size_t ... Ranks>
40- constexpr void apply_fun_over_extents (const Extents &ext, F &fun,
41+ constexpr void apply_fun_over_extents (const Extents &ext, F && fun,
4142 ArrayType &indices,
4243 std::index_sequence<R, Ranks...>) {
4344 using index_type = typename Extents::index_type;
4445 for (index_type i = 0 ; i < ext.extent (R); ++i) {
4546 indices[R] = i;
46- apply_fun_over_extents (ext, fun, indices, std::index_sequence<Ranks...>{});
47+ apply_fun_over_extents (ext, std::forward<F>( fun) , indices, std::index_sequence<Ranks...>{});
4748 }
4849}
4950
@@ -58,7 +59,7 @@ template <size_t N>
5859using make_reverse_index_sequence = typename make_reverse_index_sequence_impl<
5960 N, std::make_index_sequence<N>>::type;
6061
61- template <class SrcMDSpanType , class DstMDSpanType , typename Enabled = void >
62+ template <class SrcMDSpanType , class DstMDSpanType >
6263struct mdspan_copy_impl {
6364 using extents_type = typename DstMDSpanType::extents_type;
6465
@@ -71,16 +72,16 @@ struct mdspan_copy_impl {
7172 constexpr auto rank = extents_type::rank ();
7273 auto indices = std::array<typename extents_type::index_type, rank>{};
7374 apply_fun_over_extents (
74- ext, [&src, &dst](auto ... idxs) { dst ( idxs...) = src ( idxs...) ; },
75- indices, make_reverse_index_sequence <rank>{});
75+ ext, [&src, &dst](auto ... idxs) { dst[ idxs...] = src[ idxs...] ; },
76+ indices, std::make_index_sequence <rank>{});
7677 }
7778};
7879
79- template <class ElementType , class SrcExtents , class DstExtents >
80+ template <class ElementType , class SrcExtents , class SrcLayout , class DstExtents , class DstLayout >
81+ requires (SrcLayout::template mapping<SrcExtents>::is_always_exhaustive() && DstLayout::template mapping<DstExtents>::is_always_exhaustive())
8082struct mdspan_copy_impl <
81- mdspan<ElementType, SrcExtents, layout_left, default_accessor<ElementType>>,
82- mdspan<ElementType, DstExtents, layout_left, default_accessor<ElementType>>,
83- void > {
83+ mdspan<ElementType, SrcExtents, SrcLayout>,
84+ mdspan<ElementType, DstExtents, DstLayout>> {
8485 using extents_type = DstExtents;
8586 using src_mdspan_type = mdspan<ElementType, SrcExtents, layout_left,
8687 default_accessor<ElementType>>;
@@ -90,33 +91,15 @@ struct mdspan_copy_impl<
9091 static constexpr void copy_over_extents (const extents_type &ext,
9192 const src_mdspan_type &src,
9293 const dst_mdspan_type &dst) {
93- std::memcpy (dst.data_handle (), src.data_handle (), dst.mapping ().required_span_size () * sizeof (ElementType));
94- }
95- };
96-
97- template <class ElementType , class SrcExtents , class DstExtents >
98- struct mdspan_copy_impl <
99- mdspan<ElementType, SrcExtents, layout_right, default_accessor<ElementType>>,
100- mdspan<ElementType, DstExtents, layout_right, default_accessor<ElementType>>,
101- void > {
102- using extents_type = DstExtents;
103- using src_mdspan_type = mdspan<ElementType, SrcExtents, layout_left,
104- default_accessor<ElementType>>;
105- using dst_mdspan_type = mdspan<ElementType, DstExtents, layout_left,
106- default_accessor<ElementType>>;
107-
108- static constexpr void copy_over_extents (const extents_type &ext,
109- const src_mdspan_type &src,
110- const dst_mdspan_type &dst) {
111- std::memcpy (dst.data_handle (), src.data_handle (), dst.mapping ().required_span_size () * sizeof (ElementType));
94+ std::copy (src.data_handle (), src.data_handle () + src.mapping ().required_span_size (), dst.data_handle ());
11295 }
11396};
11497} // namespace detail
11598
11699template <class SrcElementType , class SrcExtents , class SrcLayoutPolicy ,
117100 class SrcAccessorPolicy , class DstElementType , class DstExtents ,
118101 class DstLayoutPolicy , class DstAccessorPolicy >
119- void copy (
102+ constexpr void copy (
120103 mdspan<SrcElementType, SrcExtents, SrcLayoutPolicy, SrcAccessorPolicy> src,
121104 mdspan<DstElementType, DstExtents, DstLayoutPolicy, DstAccessorPolicy>
122105 dst) {
0 commit comments