Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<mdspan>: Implement multidimensional subscript operator #3689

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions stl/inc/mdspan
Original file line number Diff line number Diff line change
Expand Up @@ -935,14 +935,15 @@ public:
constexpr mdspan& operator=(const mdspan&) = default;
constexpr mdspan& operator=(mdspan&&) = default;

// TRANSITION, P2128R6 (Multidimensional subscript operator)
#ifdef __clang__ // TRANSITION, P2128R6
JMazurkiewicz marked this conversation as resolved.
Show resolved Hide resolved
template <class... _OtherIndexTypes>
requires (is_convertible_v<_OtherIndexTypes, index_type> && ...)
&& (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...)
&& (sizeof...(_OtherIndexTypes) == rank())
_NODISCARD constexpr reference operator()(_OtherIndexTypes... _Indices) const {
_NODISCARD constexpr reference operator[](_OtherIndexTypes... _Indices) const {
return _Acc.access(_Ptr, static_cast<size_t>(_Map(static_cast<index_type>(_STD move(_Indices))...)));
}
#endif // __clang__

template <class _OtherIndexType>
requires is_convertible_v<const _OtherIndexType&, index_type>
Expand All @@ -955,7 +956,7 @@ public:
requires is_convertible_v<const _OtherIndexType&, index_type>
&& is_nothrow_constructible_v<index_type, const _OtherIndexType&>
_NODISCARD constexpr reference operator[](const array<_OtherIndexType, rank()>& _Indices) const {
return _Index_impl(_Indices, make_index_sequence<rank()>{});
return _Index_impl(span{_Indices}, make_index_sequence<rank()>{});
}

_NODISCARD constexpr size_type size() const noexcept {
Expand Down Expand Up @@ -1026,9 +1027,13 @@ public:
}

private:
template <class _IndexContainer, size_t... _Idx>
_NODISCARD constexpr reference _Index_impl(_IndexContainer&& _Indices, index_sequence<_Idx...>) const {
return _Acc.access(_Ptr, _Map(_STD as_const(_STD forward<_IndexContainer>(_Indices)[_Idx])...));
template <class _OtherIndexType, size_t... _Seq>
_NODISCARD constexpr reference _Index_impl(span<_OtherIndexType, rank()> _Indices, index_sequence<_Seq...>) const {
#ifdef __clang__ // TRANSITION, P2128R6
return this->operator[](_STD as_const(_Indices[_Seq])...);
JMazurkiewicz marked this conversation as resolved.
Show resolved Hide resolved
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
return _Acc.access(_Ptr, static_cast<size_t>(_Map(static_cast<index_type>(_STD as_const(_Indices[_Seq]))...)));
#endif // ^^^ !defined(__clang__) ^^^
}

data_handle_type _Ptr{};
Expand Down
6 changes: 4 additions & 2 deletions tests/std/tests/P0009R18_mdspan/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,10 @@ void mdspan_tests_observers() {
static_assert(!mds.is_exhaustive());
static_assert(mds.is_strided());

static_assert(mds(1, 0) == 1);
static_assert(mds(1, 2) == 7);
#ifdef __clang__ // TRANSITION, P2128R6
static_assert(mds[1, 0] == 1);
static_assert(mds[1, 2] == 7);
#endif // __clang__

static_assert(mds[array{0, 1}] == 3);
static_assert(mds[array{1, 1}] == 4);
Expand Down
82 changes: 56 additions & 26 deletions tests/std/tests/P0009R18_mdspan_layout_left/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,51 +302,81 @@ constexpr void check_correctness() {
const array values{0, 1, 2};
mdspan<const int, extents<int, 3>, layout_left> vec{values.data()};

// TRANSITION, use operator[]
assert(vec(0) == 0);
assert(vec(1) == 1);
assert(vec(2) == 2);
#ifdef __clang__ // TRANSITION, P2128R6
assert(vec[0] == 0);
assert(vec[1] == 1);
assert(vec[2] == 2);
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert(vec[array{0}] == 0);
assert(vec[array{1}] == 1);
assert(vec[array{2}] == 2);
#endif // ^^^ !defined(__clang__) ^^^
}

{ // 3x2 matrix with column-major order
const array values{0, 1, 2, 3, 4, 5};
mdspan<const int, extents<int, 3, 2>, layout_left> matrix{values.data()};

// TRANSITION, use operator[]
assert(matrix(0, 0) == 0);
assert(matrix(1, 0) == 1);
assert(matrix(2, 0) == 2);
assert(matrix(0, 1) == 3);
assert(matrix(1, 1) == 4);
assert(matrix(2, 1) == 5);
#ifdef __clang__ // TRANSITION, P2128R6
assert((matrix[0, 0] == 0));
assert((matrix[1, 0] == 1));
assert((matrix[2, 0] == 2));
assert((matrix[0, 1] == 3));
assert((matrix[1, 1] == 4));
assert((matrix[2, 1] == 5));
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert((matrix[array{0, 0}] == 0));
assert((matrix[array{1, 0}] == 1));
assert((matrix[array{2, 0}] == 2));
assert((matrix[array{0, 1}] == 3));
assert((matrix[array{1, 1}] == 4));
assert((matrix[array{2, 1}] == 5));
#endif // ^^^ !defined(__clang__) ^^^
}

{ // 3x2x4 tensor
const array values{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
mdspan<const int, dextents<size_t, 3>, layout_left> tensor{values.data(), 3, 2, 4};

// TRANSITION, use operator[]
assert(tensor(0, 0, 0) == 0);
assert(tensor(2, 0, 0) == 2);
assert(tensor(1, 1, 1) == 10);
assert(tensor(0, 0, 3) == 18);
assert(tensor(2, 1, 2) == 17);
assert(tensor(2, 1, 3) == 23);
#ifdef __clang__ // TRANSITION, P2128R6
assert((tensor[0, 0, 0] == 0));
assert((tensor[2, 0, 0] == 2));
assert((tensor[1, 1, 1] == 10));
assert((tensor[0, 0, 3] == 18));
assert((tensor[2, 1, 2] == 17));
assert((tensor[2, 1, 3] == 23));
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert((tensor[array{0, 0, 0}] == 0));
assert((tensor[array{2, 0, 0}] == 2));
assert((tensor[array{1, 1, 1}] == 10));
assert((tensor[array{0, 0, 3}] == 18));
assert((tensor[array{2, 1, 2}] == 17));
assert((tensor[array{2, 1, 3}] == 23));
#endif // ^^^ !defined(__clang__) ^^^
}

{ // 2x3x2x3 tensor
const array values{0, 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};
mdspan<const int, extents<long, 2, 3, dynamic_extent, dynamic_extent>, layout_left> tensor{values.data(), 2, 3};

// TRANSITION, use operator[]
assert(tensor(0, 0, 0, 0) == 0);
assert(tensor(1, 0, 0, 0) == 1);
assert(tensor(0, 1, 1, 0) == 8);
assert(tensor(0, 0, 0, 1) == 12);
assert(tensor(0, 0, 0, 2) == 24);
assert(tensor(0, 2, 0, 2) == 28);
assert(tensor(1, 2, 1, 2) == 35);
#ifdef __clang__ // TRANSITION, P2128R6
assert((tensor[0, 0, 0, 0] == 0));
assert((tensor[1, 0, 0, 0] == 1));
assert((tensor[0, 1, 1, 0] == 8));
assert((tensor[0, 0, 0, 1] == 12));
assert((tensor[0, 0, 0, 2] == 24));
assert((tensor[0, 2, 0, 2] == 28));
assert((tensor[1, 2, 1, 2] == 35));
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert((tensor[array{0, 0, 0, 0}] == 0));
assert((tensor[array{1, 0, 0, 0}] == 1));
assert((tensor[array{0, 1, 1, 0}] == 8));
assert((tensor[array{0, 0, 0, 1}] == 12));
assert((tensor[array{0, 0, 0, 2}] == 24));
assert((tensor[array{0, 2, 0, 2}] == 28));
assert((tensor[array{1, 2, 1, 2}] == 35));
#endif // ^^^ !defined(__clang__) ^^^
}
}

Expand Down
129 changes: 87 additions & 42 deletions tests/std/tests/P0009R18_mdspan_layout_right/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ constexpr void check_members(const extents<IndexType, Extents...>& ext, index_se
static_assert(is_nothrow_constructible_v<Mapping2, Mapping>);
// Other tests are defined in 'check_construction_from_other_right_mapping' function
}
#endif
#endif // __clang__

{ // Check construction from layout_left::mapping
using LeftMapping = layout_left::mapping<Ext>;
Expand Down Expand Up @@ -303,66 +303,111 @@ constexpr void check_correctness() {
const array vals{2, 1, 0};
mdspan<const int, extents<int, 3>, layout_right> vec{vals.data()};

// TRANSITION, use operator[]
assert(vec(0) == 2);
assert(vec(1) == 1);
assert(vec(2) == 0);
#ifdef __clang__ // TRANSITION, P2128R6
assert(vec[0] == 2);
assert(vec[1] == 1);
assert(vec[2] == 0);
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert(vec[array{0}] == 2);
assert(vec[array{1}] == 1);
assert(vec[array{2}] == 0);
#endif // ^^^ !defined(__clang__) ^^^
}

{ // 4x3 matrix with row-major order
const array vals{11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
mdspan<const int, extents<int, 4, 3>, layout_right> matrix{vals.data()};

// TRANSITION, use operator[]
assert(matrix(0, 0) == 11);
assert(matrix(0, 2) == 9);
assert(matrix(1, 1) == 7);
assert(matrix(2, 0) == 5);
assert(matrix(2, 2) == 3);
assert(matrix(3, 1) == 1);
#ifdef __clang__ // TRANSITION, P2128R6
assert((matrix[0, 0] == 11));
assert((matrix[0, 2] == 9));
assert((matrix[1, 1] == 7));
assert((matrix[2, 0] == 5));
assert((matrix[2, 2] == 3));
assert((matrix[3, 1] == 1));
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert((matrix[array{0, 0}] == 11));
assert((matrix[array{0, 2}] == 9));
assert((matrix[array{1, 1}] == 7));
assert((matrix[array{2, 0}] == 5));
assert((matrix[array{2, 2}] == 3));
assert((matrix[array{3, 1}] == 1));
#endif // ^^^ !defined(__clang__) ^^^
}

{ // 4x3x2 tensor
const array vals{23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
mdspan<const int, dextents<size_t, 3>, layout_right> tensor{vals.data(), 4, 3, 2};

// TRANSITION, use operator[]
assert(tensor(0, 0, 0) == 23);
assert(tensor(0, 0, 1) == 22);
assert(tensor(0, 1, 0) == 21);
assert(tensor(0, 1, 1) == 20);
assert(tensor(1, 0, 0) == 17);
assert(tensor(1, 0, 1) == 16);
assert(tensor(1, 1, 0) == 15);
assert(tensor(1, 1, 1) == 14);
assert(tensor(2, 2, 1) == 6);
assert(tensor(3, 2, 1) == 0);
#ifdef __clang__ // TRANSITION, P2128R6
assert((tensor[0, 0, 0] == 23));
assert((tensor[0, 0, 1] == 22));
assert((tensor[0, 1, 0] == 21));
assert((tensor[0, 1, 1] == 20));
assert((tensor[1, 0, 0] == 17));
assert((tensor[1, 0, 1] == 16));
assert((tensor[1, 1, 0] == 15));
assert((tensor[1, 1, 1] == 14));
assert((tensor[2, 2, 1] == 6));
assert((tensor[3, 2, 1] == 0));
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert((tensor[array{0, 0, 0}] == 23));
assert((tensor[array{0, 0, 1}] == 22));
assert((tensor[array{0, 1, 0}] == 21));
assert((tensor[array{0, 1, 1}] == 20));
assert((tensor[array{1, 0, 0}] == 17));
assert((tensor[array{1, 0, 1}] == 16));
assert((tensor[array{1, 1, 0}] == 15));
assert((tensor[array{1, 1, 1}] == 14));
assert((tensor[array{2, 2, 1}] == 6));
assert((tensor[array{3, 2, 1}] == 0));
#endif // ^^^ !defined(__clang__) ^^^
}

{ // 3x2x3x2 tensor
const array vals{35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12,
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
mdspan<const int, extents<long, 3, dynamic_extent, 3, dynamic_extent>, layout_right> tensor{vals.data(), 2, 2};

// TRANSITION, use operator[]
assert(tensor(0, 0, 0, 0) == 35);
assert(tensor(0, 0, 0, 1) == 34);
assert(tensor(0, 0, 1, 0) == 33);
assert(tensor(0, 0, 1, 1) == 32);
assert(tensor(0, 1, 0, 0) == 29);
assert(tensor(0, 1, 0, 1) == 28);
assert(tensor(0, 1, 1, 0) == 27);
assert(tensor(0, 1, 1, 1) == 26);
assert(tensor(1, 0, 0, 0) == 23);
assert(tensor(1, 0, 0, 1) == 22);
assert(tensor(1, 0, 1, 0) == 21);
assert(tensor(1, 0, 1, 1) == 20);
assert(tensor(1, 1, 0, 0) == 17);
assert(tensor(1, 1, 0, 1) == 16);
assert(tensor(1, 1, 1, 0) == 15);
assert(tensor(1, 1, 1, 1) == 14);
assert(tensor(2, 0, 2, 0) == 7);
assert(tensor(2, 1, 2, 1) == 0);
#ifdef __clang__ // TRANSITION, P2128R6
assert((tensor[0, 0, 0, 0] == 35));
assert((tensor[0, 0, 0, 1] == 34));
assert((tensor[0, 0, 1, 0] == 33));
assert((tensor[0, 0, 1, 1] == 32));
assert((tensor[0, 1, 0, 0] == 29));
assert((tensor[0, 1, 0, 1] == 28));
assert((tensor[0, 1, 1, 0] == 27));
assert((tensor[0, 1, 1, 1] == 26));
assert((tensor[1, 0, 0, 0] == 23));
assert((tensor[1, 0, 0, 1] == 22));
assert((tensor[1, 0, 1, 0] == 21));
assert((tensor[1, 0, 1, 1] == 20));
assert((tensor[1, 1, 0, 0] == 17));
assert((tensor[1, 1, 0, 1] == 16));
assert((tensor[1, 1, 1, 0] == 15));
assert((tensor[1, 1, 1, 1] == 14));
assert((tensor[2, 0, 2, 0] == 7));
assert((tensor[2, 1, 2, 1] == 0));
#else // ^^^ defined(__clang__) / !defined(__clang__) vvv
assert((tensor[array{0, 0, 0, 0}] == 35));
assert((tensor[array{0, 0, 0, 1}] == 34));
assert((tensor[array{0, 0, 1, 0}] == 33));
assert((tensor[array{0, 0, 1, 1}] == 32));
assert((tensor[array{0, 1, 0, 0}] == 29));
assert((tensor[array{0, 1, 0, 1}] == 28));
assert((tensor[array{0, 1, 1, 0}] == 27));
assert((tensor[array{0, 1, 1, 1}] == 26));
assert((tensor[array{1, 0, 0, 0}] == 23));
assert((tensor[array{1, 0, 0, 1}] == 22));
assert((tensor[array{1, 0, 1, 0}] == 21));
assert((tensor[array{1, 0, 1, 1}] == 20));
assert((tensor[array{1, 1, 0, 0}] == 17));
assert((tensor[array{1, 1, 0, 1}] == 16));
assert((tensor[array{1, 1, 1, 0}] == 15));
assert((tensor[array{1, 1, 1, 1}] == 14));
assert((tensor[array{2, 0, 2, 0}] == 7));
assert((tensor[array{2, 1, 2, 1}] == 0));
#endif // ^^^ !defined(__clang__) ^^^
}
}

Expand Down
Loading