File indexing completed on 2026-05-03 08:13:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef _LIBCPP___CXX03___MDSPAN_LAYOUT_RIGHT_H
0018 #define _LIBCPP___CXX03___MDSPAN_LAYOUT_RIGHT_H
0019
0020 #include <__cxx03/__assert>
0021 #include <__cxx03/__config>
0022 #include <__cxx03/__fwd/mdspan.h>
0023 #include <__cxx03/__mdspan/extents.h>
0024 #include <__cxx03/__type_traits/is_constructible.h>
0025 #include <__cxx03/__type_traits/is_convertible.h>
0026 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
0027 #include <__cxx03/__utility/integer_sequence.h>
0028 #include <__cxx03/cinttypes>
0029 #include <__cxx03/cstddef>
0030 #include <__cxx03/limits>
0031
0032 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0033 # pragma GCC system_header
0034 #endif
0035
0036 _LIBCPP_PUSH_MACROS
0037 #include <__cxx03/__undef_macros>
0038
0039 _LIBCPP_BEGIN_NAMESPACE_STD
0040
0041 #if _LIBCPP_STD_VER >= 23
0042
0043 template <class _Extents>
0044 class layout_right::mapping {
0045 public:
0046 static_assert(__mdspan_detail::__is_extents<_Extents>::value,
0047 "layout_right::mapping template argument must be a specialization of extents.");
0048
0049 using extents_type = _Extents;
0050 using index_type = typename extents_type::index_type;
0051 using size_type = typename extents_type::size_type;
0052 using rank_type = typename extents_type::rank_type;
0053 using layout_type = layout_right;
0054
0055 private:
0056 _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {
0057 if constexpr (extents_type::rank() == 0)
0058 return true;
0059
0060 index_type __prod = __ext.extent(0);
0061 for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
0062 bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
0063 if (__overflowed)
0064 return false;
0065 }
0066 return true;
0067 }
0068
0069 static_assert(extents_type::rank_dynamic() > 0 || __required_span_size_is_representable(extents_type()),
0070 "layout_right::mapping product of static extents must be representable as index_type.");
0071
0072 public:
0073
0074 _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept = default;
0075 _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
0076 _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) {
0077
0078
0079 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0080 __required_span_size_is_representable(__ext),
0081 "layout_right::mapping extents ctor: product of extents must be representable as index_type.");
0082 }
0083
0084 template <class _OtherExtents>
0085 requires(is_constructible_v<extents_type, _OtherExtents>)
0086 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
0087 mapping(const mapping<_OtherExtents>& __other) noexcept
0088 : __extents_(__other.extents()) {
0089
0090
0091 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0092 __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
0093 "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
0094 }
0095
0096 template <class _OtherExtents>
0097 requires(is_constructible_v<extents_type, _OtherExtents> && _OtherExtents::rank() <= 1)
0098 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
0099 mapping(const layout_left::mapping<_OtherExtents>& __other) noexcept
0100 : __extents_(__other.extents()) {
0101
0102
0103
0104
0105
0106
0107 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0108 __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
0109 "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
0110 }
0111
0112 template <class _OtherExtents>
0113 requires(is_constructible_v<extents_type, _OtherExtents>)
0114 _LIBCPP_HIDE_FROM_ABI constexpr explicit(extents_type::rank() > 0)
0115 mapping(const layout_stride::mapping<_OtherExtents>& __other) noexcept
0116 : __extents_(__other.extents()) {
0117 if constexpr (extents_type::rank() > 0) {
0118 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0119 ([&]() {
0120 using _CommonType = common_type_t<typename extents_type::index_type, typename _OtherExtents::index_type>;
0121 for (rank_type __r = 0; __r < extents_type::rank(); __r++)
0122 if (static_cast<_CommonType>(stride(__r)) != static_cast<_CommonType>(__other.stride(__r)))
0123 return false;
0124 return true;
0125 }()),
0126 "layout_right::mapping from layout_stride ctor: strides are not compatible with layout_right.");
0127 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0128 __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
0129 "layout_right::mapping from layout_stride ctor: other.required_span_size() must be representable as "
0130 "index_type.");
0131 }
0132 }
0133
0134 _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;
0135
0136
0137 _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }
0138
0139 _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {
0140 index_type __size = 1;
0141 for (size_t __r = 0; __r < extents_type::rank(); __r++)
0142 __size *= __extents_.extent(__r);
0143 return __size;
0144 }
0145
0146 template <class... _Indices>
0147 requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) &&
0148 (is_nothrow_constructible_v<index_type, _Indices> && ...))
0149 _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept {
0150
0151
0152
0153
0154 _LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...),
0155 "layout_right::mapping: out of bounds indexing");
0156 return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
0157 index_type __res = 0;
0158 ((__res = static_cast<index_type>(__idx) + __extents_.extent(_Pos) * __res), ...);
0159 return __res;
0160 }(make_index_sequence<sizeof...(_Indices)>());
0161 }
0162
0163 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }
0164 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; }
0165 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }
0166
0167 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }
0168 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; }
0169 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }
0170
0171 _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept
0172 requires(extents_type::rank() > 0)
0173 {
0174
0175
0176 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0177 __r < extents_type::rank(), "layout_right::mapping::stride(): invalid rank index");
0178 index_type __s = 1;
0179 for (rank_type __i = extents_type::rank() - 1; __i > __r; __i--)
0180 __s *= __extents_.extent(__i);
0181 return __s;
0182 }
0183
0184 template <class _OtherExtents>
0185 requires(_OtherExtents::rank() == extents_type::rank())
0186 _LIBCPP_HIDE_FROM_ABI friend constexpr bool
0187 operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept {
0188 return __lhs.extents() == __rhs.extents();
0189 }
0190
0191 private:
0192 _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{};
0193 };
0194
0195 #endif
0196
0197 _LIBCPP_END_NAMESPACE_STD
0198
0199 _LIBCPP_POP_MACROS
0200
0201 #endif