8#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_LAYOUT_STRIDE_HPP_
9#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_LAYOUT_STRIDE_HPP_
17#include "arene/base/mdspan/detail/all_valid_submdspan_slice_types_for.hpp"
18#include "arene/base/mdspan/detail/layout_common.hpp"
19#include "arene/base/mdspan/detail/strided_mapping_base.hpp"
20#include "arene/base/mdspan/detail/submdspan_offset.hpp"
21#include "arene/base/mdspan/detail/submdspan_sub_strides.hpp"
27#include "arene/base/stdlib_choice/cstddef.hpp"
28#include "arene/base/stdlib_choice/enable_if.hpp"
29#include "arene/base/stdlib_choice/integer_sequence.hpp"
30#include "arene/base/stdlib_choice/integral_constant.hpp"
31#include "arene/base/stdlib_choice/is_constructible.hpp"
32#include "arene/base/stdlib_choice/is_convertible.hpp"
33#include "arene/base/stdlib_choice/is_same.hpp"
34#include "arene/base/stdlib_choice/move.hpp"
35#include "arene/base/stdlib_choice/numeric_limits.hpp"
44namespace layout_detail {
50template <
typename IndexType>
51constexpr auto precondition_all_strides_positive(span<IndexType const> strides)
noexcept ->
void {
52 for (
auto const& stride : strides) {
65 typename OutputIndexType,
66 typename InputIndexType,
69constexpr auto stride_cast(span<InputIndexType const, Rank> strides)
noexcept -> array<OutputIndexType, Rank> {
70 precondition_all_strides_positive<InputIndexType>(strides);
72 array<OutputIndexType, Rank> converted{};
74 converted[dim] =
static_cast<OutputIndexType
>(strides[dim]);
86 typename OutputIndexType,
87 typename InputIndexType,
90constexpr auto stride_cast(span<InputIndexType const, Rank>)
noexcept -> array<OutputIndexType, Rank> {
98template <
typename FromMapping,
typename ToMapping,
typename = constra
ints<>>
105template <
typename FromMapping,
typename ToMapping>
106class implicit_conversion_ok<
109 constraints<typename FromMapping::extents_type, typename ToMapping::extents_type>>
110 :
public std::integral_constant<
112 std::is_convertible<typename FromMapping::extents_type, typename ToMapping::extents_type>::value &&
113 (std::is_same<layout_left::mapping<typename FromMapping::extents_type>, FromMapping>::value ||
114 std::is_same<layout_right::mapping<typename FromMapping::extents_type>, FromMapping>::value ||
115 std::is_same<layout_stride::mapping<typename FromMapping::extents_type>, FromMapping>::value)> {};
122template <
typename Mapping, constra
ints<std::enable_if_t<Mapping::extents_type::rank() != 0U>> =
nullptr>
123constexpr auto extract_strides(Mapping
const& mapping)
noexcept
124 -> array<
typename Mapping::index_type, Mapping::extents_type::rank()> {
125 array<
typename Mapping::index_type, Mapping::extents_type::rank()> strides{};
126 for (
typename Mapping::rank_type dim{}; dim < Mapping::extents_type::rank(); ++dim) {
127 strides[dim] = mapping.stride(dim);
137template <
typename Mapping, constra
ints<std::enable_if_t<Mapping::extents_type::rank() == 0U>> =
nullptr>
138constexpr auto extract_strides(Mapping
const&)
noexcept -> array<typename Mapping::index_type, 0U> {
148template <
typename Mapping,
typename Mapping::rank_type... Dims>
150get_offset_of_mapping_impl(Mapping
const& mapping, std::integer_sequence<typename Mapping::rank_type, Dims...>)
noexcept
151 ->
typename Mapping::index_type {
153 if (mapping.required_span_size() ==
typename Mapping::index_type{}) {
158 return mapping((
static_cast<void>(Dims),
typename Mapping::index_type{})...);
167template <
typename Mapping, constra
ints<std::enable_if_t<Mapping::extents_type::rank() != 0U>> =
nullptr>
168constexpr auto get_offset_of_mapping(Mapping
const& mapping)
noexcept ->
typename Mapping::index_type {
169 return get_offset_of_mapping_impl(
179template <
typename Mapping, constra
ints<std::enable_if_t<Mapping::extents_type::rank() == 0U>> =
nullptr>
180constexpr auto get_offset_of_mapping(Mapping
const&)
noexcept ->
typename Mapping::index_type {
191template <
class Extents>
195 using base_type = layout_detail::strided_mapping_base<Extents>;
198 using typename base_type::extents_type;
199 using typename base_type::index_type;
200 using typename base_type::rank_type;
201 using typename base_type::size_type;
213 static constexpr auto passkey() noexcept -> layout_detail::check_bypasser {
return layout_detail::check_bypasser{}; }
227 extents_type
const& exts,
228 array<index_type, extents_type::rank()>
const& strds,
229 layout_detail::check_bypasser
231 : base_type(exts, strds) {}
236 : base_type(extents_type{}, layout_detail::right_strides(extents_type{})) {
248 class OtherIndexType,
252 constexpr mapping(extents_type
const& exts,
span<OtherIndexType, extents_type::rank()> strds) noexcept
253 : base_type(exts, layout_detail::stride_cast<index_type>(strds)) {
255 index_type
const span_size{this->required_span_size()};
258 auto const extent_size = layout_detail::extent_product(exts);
272 class OtherIndexType,
276 constexpr mapping(extents_type
const& exts,
array<OtherIndexType, extents_type::rank()>
const& strds) noexcept
277 :
mapping(exts,
span<OtherIndexType
const, extents_type::rank()>(strds)) {}
287 class StridedLayoutMapping,
295 constexpr mapping(StridedLayoutMapping
const& other) noexcept
297 extents_type(other.extents()),
298 layout_detail::stride_cast<index_type>(
299 span<
typename StridedLayoutMapping::index_type
const, StridedLayoutMapping::extents_type::rank()>(
300 layout_detail::extract_strides(other)
316 class StridedLayoutMapping,
323 constexpr explicit mapping(StridedLayoutMapping
const& other) noexcept
325 extents_type(other.extents()),
326 layout_detail::stride_cast<index_type>(
327 span<
typename StridedLayoutMapping::index_type
const, StridedLayoutMapping::extents_type::rank()>(
328 layout_detail::extract_strides(other)
334 ARENE_PRECONDITION(layout_detail::get_offset_of_mapping(other) ==
typename StridedLayoutMapping::index_type{});
337 using base_type::extents;
338 using base_type::required_span_size;
339 using base_type::stride;
340 using base_type::strides;
341 using base_type::operator();
343 using base_type::is_always_strided;
344 using base_type::is_always_unique;
345 using base_type::is_strided;
346 using base_type::is_unique;
358 return required_span_size() == layout_detail::extent_product(this->
extents()).value;
376 for (mapping::rank_type dim{}; dim < mapping::extents_type::rank(); ++dim) {
377 if (cmp_not_equal(left.extents().extent(dim), right.extents().extent(dim)) ||
378 cmp_not_equal(left.stride(dim), right.stride(dim))) {
383 return layout_detail::get_offset_of_mapping(right) ==
typename Right::index_type{};
399 return right == left;
414 return !(left == right);
430 return !(right == left);
463 class... CanonicalSliceSpecifiers,
465 std::enable_if_t<
sizeof...(CanonicalSliceSpecifiers) == extents_type::rank()>,
467 mdspan_detail::all_valid_submdspan_slice_types_for_v<extents_type, CanonicalSliceSpecifiers...>>> =
469 friend constexpr auto submdspan_mapping(SrcMapping
const& src, CanonicalSliceSpecifiers... slices)
noexcept
473 auto sub_strides = mdspan_detail::submdspan_sub_strides<index_type>(src, slices...);
474 auto offset = mdspan_detail::submdspan_offset(src, slices...);
475 auto sub_ext = submdspan_extents(src.extents(),
std::move(slices)...);
478 return {submapping{
std::move(sub_ext),
std::move(sub_strides), mapping::passkey()}, offset};
487template <
typename Extents>
A fixed-size array of T objects.
Definition array.hpp:77
represents a multidimensional index space
Definition extents.hpp:72
A mapping from a logical pack of indices into a single flat physical output index,...
Definition layout_stride.hpp:192
friend constexpr auto operator!=(Left const &left, mapping const &right) noexcept -> bool
Compare two strided mappings for inequality, of which at least one must be a layout_stride::mapping.
Definition layout_stride.hpp:429
friend constexpr auto operator!=(mapping const &left, Right const &right) noexcept -> bool
Compare two strided mappings for inequality, of which at least one must be a layout_stride::mapping.
Definition layout_stride.hpp:413
constexpr mapping(StridedLayoutMapping const &other) noexcept
Construct a mapping from another strided mapping; implicit for certain known-safe mapping types.
Definition layout_stride.hpp:295
friend constexpr auto operator==(Left const &left, mapping const &right) noexcept -> bool
Compare two strided mappings for equality, of which at least one must be a layout_stride::mapping.
Definition layout_stride.hpp:398
constexpr mapping(extents_type const &exts, array< OtherIndexType, extents_type::rank()> const &strds) noexcept
Construct a mapping with the given strides; they must be valid for a default-constructed extents_type...
Definition layout_stride.hpp:276
friend constexpr auto operator==(mapping const &left, Right const &right) noexcept -> bool
Compare two strided mappings for equality, of which at least one must be a layout_stride::mapping.
Definition layout_stride.hpp:375
static constexpr auto is_always_exhaustive() noexcept -> bool
Return whether or not this mapping is always exhaustive, i.e. every element is reachable using some i...
Definition layout_stride.hpp:351
constexpr auto is_exhaustive() const noexcept -> bool
Return whether or not this instance is exhaustive, i.e. every element is reachable using some indices...
Definition layout_stride.hpp:357
constexpr mapping(extents_type const &exts, array< index_type, extents_type::rank()> const &strds, layout_detail::check_bypasser) noexcept
Construct a mapping with the given strides, bypassing precondition checks.
Definition layout_stride.hpp:226
constexpr mapping() noexcept
Construct a default strided mapping for the given extents, which has the same layout as layout_right.
Definition layout_stride.hpp:235
layout_stride layout_type
The tag type of the layout used by this mapping.
Definition layout_stride.hpp:203
friend constexpr auto submdspan_mapping(SrcMapping const &src, CanonicalSliceSpecifiers... slices) noexcept -> submdspan_mapping_result< layout_stride::mapping< submdspan_subextents_type_t< extents_type, CanonicalSliceSpecifiers... > > >
compute submdspan mapping and offset for a layout_stride mapping
Definition layout_stride.hpp:469
constexpr mapping(extents_type const &exts, span< OtherIndexType, extents_type::rank()> strds) noexcept
Construct a mapping with the given strides; they must be valid for a default-constructed extents_type...
Definition layout_stride.hpp:252
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
constexpr bool is_layout_mapping_v
Helper trait to indicate if the given type has all the basic properties required of a layout mapping:
typename submdspan_subextents_type< Extents, SliceSpecifiers... >::type submdspan_subextents_type_t
determines the subextents type from an extents type and slice specifiers
Definition submdspan_subextents_type.hpp:124
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
bool_constant< false > false_type
An alias for bool_constant<false>
Definition integral_constant.hpp:79
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
typename integer_sequence_detail::make_integer_sequence< Type, Size >::type make_integer_sequence
Helper alias template to simplify creation of std::integer_sequence types with 0, 1,...
Definition integer_sequence.hpp:212
Provides backports of the safe integer comparison functions from C++20.
Specializations of submdspan_mapping_result are returned by overloads of submdspan_mapping.
Definition submdspan_mapping_result.hpp:18