6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_LAYOUT_CONSTANT_DIAGONAL_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_LAYOUT_CONSTANT_DIAGONAL_HPP_
14#include "arene/base/mdspan/detail/representable_cast.hpp"
15#include "arene/base/mdspan/detail/to_array.hpp"
17#include "arene/base/stdlib_choice/cstddef.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/integer_sequence.hpp"
20#include "arene/base/stdlib_choice/is_same.hpp"
27namespace layout_constant_diagonal_detail {
31template <
typename IndexType>
32extern constexpr IndexType max_required_span_size{2};
39template <
typename ExtentsType,
std::size_t... Indices>
41 return any_of({(extents.extent(Indices) ==
typename ExtentsType::index_type{})...});
49template <
typename ExtentsType>
50constexpr auto any_extents_are_zero(ExtentsType
const& extents)
noexcept ->
bool {
58template <
typename ExtentsType,
std::size_t... Indices>
60 return any_of({(ExtentsType::static_extent(Indices) ==
std::size_t{})...});
68template <
typename ExtentsType>
69constexpr auto any_static_extents_are_zero() noexcept ->
bool {
85template <
typename OffsetType,
typename IndexType>
86constexpr auto is_exhaustive(array<OffsetType, 1>
const&, array<IndexType, 1>
const& extents)
noexcept ->
bool {
87 return extents[0U] == IndexType{};
100template <
typename OffsetType,
typename IndexType, std::
size_t Rank>
101constexpr auto is_exhaustive(array<OffsetType, Rank>
const& offsets, array<IndexType, Rank>
const& extents)
noexcept
103 auto const dim_zero_extent = extents[0U];
104 if (dim_zero_extent == IndexType{}) {
107 bool has_exhaustive_extent{dim_zero_extent >= max_required_span_size<IndexType>};
108 IndexType min_overlap_position{mdspan_detail::representable_cast<IndexType>(offsets[0U])};
109 IndexType max_overlap_position{min_overlap_position + dim_zero_extent};
110 for (
std::size_t index{1U}; index < Rank; ++index) {
111 auto const this_extent = extents[index];
112 if (this_extent == IndexType{}) {
115 if (this_extent >= max_required_span_size<IndexType>) {
116 has_exhaustive_extent =
true;
118 IndexType
const min_value_this_extent{mdspan_detail::representable_cast<IndexType>(offsets[index])};
119 IndexType
const max_value_this_extent{min_value_this_extent + this_extent};
120 if (min_value_this_extent > min_overlap_position) {
121 min_overlap_position = min_value_this_extent;
123 if (max_value_this_extent < max_overlap_position) {
124 max_overlap_position = max_value_this_extent;
127 return (max_overlap_position > min_overlap_position) && has_exhaustive_extent;
140template <
typename OffsetExtentsType>
145 struct extents_tag {};
147 struct offsets_tag {};
151 template <
typename ExtentsType>
158 ,
ebo_holder<offsets_tag, OffsetExtentsType> {
161 static_assert(ExtentsType::rank() == OffsetExtentsType::rank(),
"The offsets and extents must have the same rank");
194 template <
typename FirstAccessIndex>
207 template <
std::size_t... DimensionIndices,
typename FirstAccessIndex,
typename... AccessIndices>
208 constexpr auto mapped_index(
210 FirstAccessIndex first_access_index,
211 AccessIndices... access_indices
214 mdspan_detail::representable_cast<index_type>(this->
offsets().
extent(0U)) +
215 mdspan_detail::representable_cast<index_type>(first_access_index)
218 {((mdspan_detail::representable_cast<index_type>(this->
offsets().
extent(DimensionIndices)) +
219 mdspan_detail::representable_cast<index_type>(access_indices)) == first_index)...}
232 : extents_base(source_extents),
239 : extents_base(source_extents),
240 offsets_base(offset_extents) {}
266 typename SfinaeExtentsType = ExtentsType,
281 typename SfinaeExtentsType = ExtentsType,
297 typename SfinaeExtentsType = ExtentsType,
304 return layout_constant_diagonal_detail::max_required_span_size<index_type>;
314 typename SfinaeExtentsType = ExtentsType,
321 if (layout_constant_diagonal_detail::any_extents_are_zero(
extents())) {
324 return layout_constant_diagonal_detail::max_required_span_size<index_type>;
334 typename SfinaeExtentsType = ExtentsType,
350 typename SfinaeExtentsType = ExtentsType,
368 typename SfinaeExtentsType = ExtentsType,
369 typename SfinaeOffsets = OffsetExtentsType,
377 constexpr bool result = layout_constant_diagonal_detail::is_exhaustive(
378 mdspan_detail::to_array(OffsetExtentsType{}),
379 mdspan_detail::to_array(ExtentsType{})
392 typename SfinaeExtentsType = ExtentsType,
393 typename SfinaeOffsets = OffsetExtentsType,
397 std::enable_if_t<(SfinaeExtentsType::rank_dynamic() != 0) || (SfinaeOffsets::rank_dynamic() != 0)>,
400 return layout_constant_diagonal_detail::is_exhaustive(
401 mdspan_detail::to_array(
offsets()),
402 mdspan_detail::to_array(this->
extents())
417 return required_span_size() < layout_constant_diagonal_detail::max_required_span_size<index_type>;
426 static constexpr auto is_strided() noexcept ->
bool {
return false; }
448 return (ExtentsType::rank() == 0U) || layout_constant_diagonal_detail::any_static_extents_are_zero<ExtentsType>();
457 return (ExtentsType::rank_dynamic() == 0U) && (OffsetExtentsType::rank_dynamic() == 0U) &&
477 constexpr auto offsets() const noexcept -> OffsetExtentsType const& {
489 return (lhs.extents() == rhs.extents()) && (lhs.offsets() == rhs.offsets());
constexpr ebo_holder() noexcept(std::is_nothrow_default_constructible< T >::value)
Definition ebo_holder.hpp:57
constexpr auto get_value(extents_tag) const &noexcept -> value_type const &
Definition ebo_holder.hpp:89
~mapping()=default
Default destructor.
constexpr auto extents() const noexcept -> ExtentsType const &
Obtain the extents of the layout.
Definition layout_constant_diagonal.hpp:473
static constexpr auto is_always_exhaustive() noexcept -> bool
Check if this is always an exhaustive layout.
Definition layout_constant_diagonal.hpp:456
constexpr auto is_exhaustive() const noexcept -> bool
Check if this layout is exhaustive.
Definition layout_constant_diagonal.hpp:338
constexpr mapping()=default
Construct the mapping with default-constructed extents and offsets.
mapping(mapping const &)=default
Default copy constructor.
friend constexpr auto operator==(mapping const &lhs, mapping const &rhs) noexcept -> bool
Equality comparison: two mappings are equal if they have the same offsets and extents.
Definition layout_constant_diagonal.hpp:488
constexpr mapping(extents_type const &source_extents) noexcept
Construct the mapping with the specified extents.
Definition layout_constant_diagonal.hpp:231
mapping(mapping &&)=default
Default move constructor.
friend constexpr auto operator!=(mapping const &lhs, mapping const &rhs) noexcept -> bool
Inequality comparison: two mappings are not equal if they have the different offsets or extents.
Definition layout_constant_diagonal.hpp:502
constexpr auto stride(rank_type) const noexcept -> index_type
Get the stride for the provided dimension. Provided for interface compatibility only.
Definition layout_constant_diagonal.hpp:435
layout_constant_diagonal layout_type
The layout mapping policy type for this mapping.
Definition layout_constant_diagonal.hpp:180
constexpr auto is_exhaustive() const noexcept -> bool
Check if this layout is exhaustive.
Definition layout_constant_diagonal.hpp:376
static constexpr auto is_always_strided() noexcept -> bool
Check if this is always a strided layout, which it is not.
Definition layout_constant_diagonal.hpp:429
auto operator=(mapping const &) -> mapping &=default
Default copy assignment operator.
constexpr mapping(extents_type const &source_extents, offsets_type const &offset_extents) noexcept
Construct the mapping with the specified extents and offsets.
Definition layout_constant_diagonal.hpp:238
static constexpr auto is_strided() noexcept -> bool
Check if this is a strided layout, which it is not.
Definition layout_constant_diagonal.hpp:426
typename extents_type::rank_type rank_type
The rank type for the extents.
Definition layout_constant_diagonal.hpp:178
ExtentsType extents_type
The type of the extents.
Definition layout_constant_diagonal.hpp:174
constexpr auto required_span_size() const noexcept -> index_type
The required span size for the underlying storage.
Definition layout_constant_diagonal.hpp:270
OffsetExtentsType offsets_type
The type of the offsets.
Definition layout_constant_diagonal.hpp:172
auto operator=(mapping &&) -> mapping &=default
Default move constructor.
typename extents_type::index_type index_type
The index type for the extents.
Definition layout_constant_diagonal.hpp:176
constexpr auto offsets() const noexcept -> OffsetExtentsType const &
Obtain the offsets of the layout.
Definition layout_constant_diagonal.hpp:477
static constexpr auto is_always_unique() noexcept -> bool
Check if this is always a unique layout.
Definition layout_constant_diagonal.hpp:447
constexpr auto operator()(Indices... indices) const noexcept -> index_type
Get the index from the underlying storage to use for the specified combination of indices.
Definition layout_constant_diagonal.hpp:467
constexpr auto is_unique() const noexcept -> bool
Check if there is exactly one index combination for each target element.
Definition layout_constant_diagonal.hpp:416
An mdspan layout mapping policy for a diagonal tensorlet.
Definition layout_constant_diagonal.hpp:141
A sum type which can hold either a value or an error. Similar to std::expected.
Definition result.hpp:104
#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 std::integral_constant< std::size_t, Extent > span< T, Extent >::extent
Definition span.hpp:984
constexpr bool is_extents_v
Helper variable to check if a provided type is an instantiation of extents.
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
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::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