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>: Cleanup mdspan class a little bit #3672

Merged
Merged
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
91 changes: 44 additions & 47 deletions stl/inc/mdspan
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,12 @@ public:
return extents_type::rank_dynamic();
}

_NODISCARD static constexpr size_t static_extent(const rank_type _Rank) noexcept {
return extents_type::static_extent(_Rank);
_NODISCARD static constexpr size_t static_extent(const rank_type _Idx) noexcept {
return extents_type::static_extent(_Idx);
}

_NODISCARD constexpr index_type extent(const rank_type _Idx) const noexcept {
return _Map.extents().extent(_Idx);
}

constexpr mdspan()
Expand All @@ -854,15 +858,15 @@ public:
&& (sizeof...(_OtherIndexTypes) == rank() || sizeof...(_OtherIndexTypes) == rank_dynamic())
&& is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>
constexpr explicit mdspan(data_handle_type _Ptr_, _OtherIndexTypes... _Exts)
: _Ptr{_STD move(_Ptr_)}, _Map{extents_type{static_cast<index_type>(_STD move(_Exts))...}} {}
: _Ptr(_STD move(_Ptr_)), _Map(extents_type{static_cast<index_type>(_STD move(_Exts))...}), _Acc() {}

template <class _OtherIndexType, size_t _Size>
requires is_convertible_v<_OtherIndexType, index_type>
&& is_nothrow_constructible_v<index_type, const _OtherIndexType&>
&& (_Size == rank() || _Size == rank_dynamic())
&& is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>
constexpr explicit(_Size != rank_dynamic()) mdspan(data_handle_type _Ptr_, span<_OtherIndexType, _Size>& _Exts)
: _Ptr{_Ptr_}, _Map{extents_type{_Exts}} {}
: _Ptr(_STD move(_Ptr_)), _Map(extents_type{_Exts}), _Acc() {}

template <class _OtherIndexType, size_t _Size>
requires is_convertible_v<const _OtherIndexType&, index_type>
Expand All @@ -871,18 +875,18 @@ public:
&& is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>
constexpr explicit(_Size != rank_dynamic())
mdspan(data_handle_type _Ptr_, const array<_OtherIndexType, _Size>& _Exts)
: _Ptr{_Ptr_}, _Map{extents_type{_Exts}} {}
: _Ptr(_STD move(_Ptr_)), _Map(extents_type{_Exts}), _Acc() {}

constexpr mdspan(data_handle_type _Ptr_, const extents_type& _Ext)
requires is_constructible_v<mapping_type, const extents_type&> && is_default_constructible_v<accessor_type>
: _Ptr{_Ptr_}, _Map{_Ext} {}
: _Ptr(_STD move(_Ptr_)), _Map(_Ext), _Acc() {}

constexpr mdspan(data_handle_type _Ptr_, const mapping_type& _Map_)
requires is_default_constructible_v<accessor_type>
: _Ptr{_Ptr_}, _Map{_Map_} {}
: _Ptr(_STD move(_Ptr_)), _Map(_Map_), _Acc() {}

constexpr mdspan(data_handle_type _Ptr_, const mapping_type& _Map_, const accessor_type& _Acc_)
: _Ptr{_Ptr_}, _Map{_Map_}, _Acc{_Acc_} {}
: _Ptr(_STD move(_Ptr_)), _Map(_Map_), _Acc(_Acc_) {}

template <class _OtherElementType, class _OtherExtents, class _OtherLayoutPolicy, class _OtherAccessor>
requires is_constructible_v<mapping_type, const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&>
Expand All @@ -891,7 +895,7 @@ public:
!is_convertible_v<const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&, mapping_type>
|| !is_convertible_v<const _OtherAccessor&, accessor_type>)
mdspan(const mdspan<_OtherElementType, _OtherExtents, _OtherLayoutPolicy, _OtherAccessor>& _Other)
: _Ptr{_Other._Ptr}, _Map{_Other._Map}, _Acc{_Other._Acc} {
: _Ptr(_Other._Ptr), _Map(_Other._Map), _Acc(_Other._Acc) {
static_assert(is_constructible_v<data_handle_type, const typename _OtherAccessor::data_handle_type&>,
"The data_handle_type must be constructible from const typename OtherAccessor::data_handle_type& (N4944 "
"[mdspan.mdspan.cons]/20.1).");
Expand Down Expand Up @@ -925,43 +929,16 @@ public:
return _Index_impl(_Indices, make_index_sequence<rank()>{});
}

_NODISCARD constexpr const extents_type& extents() const noexcept {
return _Map.extents();
}

_NODISCARD constexpr const data_handle_type& data_handle() const noexcept {
return _Ptr;
}

_NODISCARD constexpr const mapping_type& mapping() const noexcept {
return _Map;
}

_NODISCARD constexpr const accessor_type& accessor() const noexcept {
return _Acc;
}

_NODISCARD constexpr index_type extent(const rank_type _Rank) const noexcept {
const auto& _Ext = _Map.extents();
return _Ext.extent(_Rank);
}

_NODISCARD constexpr size_type size() const noexcept {
const auto& _Ext = _Map.extents();
size_type _Result = 1;
for (rank_type _Dim = 0; _Dim < rank(); ++_Dim) {
_Result *= _Ext.extent(_Dim);
}
return _Result;
return static_cast<size_type>(_Map.extents()._Fwd_prod_of_extents(rank()));
}

_NODISCARD constexpr bool empty() const noexcept {
for (rank_type _Dim = 0; _Dim < rank(); ++_Dim) {
if (_Map.extents().extent(_Dim) == 0) {
for (rank_type _Idx = 0; _Idx < rank(); ++_Idx) {
if (_Map.extents().extent(_Idx) == 0) {
return true;
}
}

return false;
}

Expand All @@ -971,32 +948,52 @@ public:
swap(_Left._Acc, _Right._Acc);
}

_NODISCARD static constexpr bool is_always_unique() {
_NODISCARD constexpr const extents_type& extents() const noexcept {
return _Map.extents();
}

_NODISCARD constexpr const data_handle_type& data_handle() const noexcept {
return _Ptr;
}

_NODISCARD constexpr const mapping_type& mapping() const noexcept {
return _Map;
}

_NODISCARD constexpr const accessor_type& accessor() const noexcept {
return _Acc;
}

_NODISCARD static constexpr bool is_always_unique() noexcept(
noexcept(mapping_type::is_always_unique())) /* strengthened */ {
return mapping_type::is_always_unique();
}

_NODISCARD static constexpr bool is_always_exhaustive() {
_NODISCARD static constexpr bool is_always_exhaustive() noexcept(
noexcept(mapping_type::is_always_exhaustive())) /* strengthened */ {
return mapping_type::is_always_exhaustive();
}

_NODISCARD static constexpr bool is_always_strided() {
_NODISCARD static constexpr bool is_always_strided() noexcept(
noexcept(mapping_type::is_always_strided())) /* strengthened */ {
return mapping_type::is_always_strided();
}

_NODISCARD constexpr bool is_unique() const {
_NODISCARD constexpr bool is_unique() const noexcept(noexcept(_Map.is_unique())) /* strengthened */ {
return _Map.is_unique();
}

_NODISCARD constexpr bool is_exhaustive() const {
_NODISCARD constexpr bool is_exhaustive() const noexcept(noexcept(_Map.is_exhaustive())) /* strengthened */ {
return _Map.is_exhaustive();
}

_NODISCARD constexpr bool is_strided() const {
_NODISCARD constexpr bool is_strided() const noexcept(noexcept(_Map.is_strided())) /* strengthened */ {
return _Map.is_strided();
}

_NODISCARD constexpr index_type stride(const size_t _Dim) const {
return _Map.stride(_Dim);
_NODISCARD constexpr index_type stride(const rank_type _Idx) const
noexcept(noexcept(_Map.stride(_Idx))) /* strengthened */ {
return _Map.stride(_Idx);
}

private:
Expand Down