107#include <type_traits>
113#if ALEPH_HAS_STD_MDSPAN
120#if ALEPH_HAS_STD_MDSPAN
124template <
class IndexType,
size_t... Exts>
125using extents = std::extents<IndexType, Exts...>;
127template <
class IndexType,
size_t Rank>
128using dextents = std::dextents<IndexType, Rank>;
130using layout_right = std::layout_right;
131using layout_left = std::layout_left;
133template <
class ElementType>
134using default_accessor = std::default_accessor<ElementType>;
151template <
class T,
class Extents,
class LayoutPolicy = std::layout_right,
152 class AccessorPolicy = std::default_accessor<T>>
160 using impl_type = std::mdspan<T, Extents, LayoutPolicy, AccessorPolicy>;
164 template <
class,
class,
class,
class>
169 using layout_type =
typename impl_type::layout_type;
173 using value_type =
typename impl_type::value_type;
174 using index_type =
typename impl_type::index_type;
175 using rank_type =
typename impl_type::rank_type;
177 using reference =
typename impl_type::reference;
179 constexpr mdspan() noexcept = default;
182 constexpr
mdspan &operator=(const
mdspan &) noexcept = default;
183 constexpr
mdspan &operator=(
mdspan &&) noexcept = default;
211 template <
class... IndexTypes>
212 requires(
sizeof...(IndexTypes) >= 1
and
213 (std::is_convertible_v<IndexTypes, index_type>
and ...))
219 template <
class OtherIndexType>
220 requires std::is_convertible_v<const OtherIndexType &, index_type>
222 const std::array<OtherIndexType, extents_type::rank_dynamic()> &dyn) noexcept
227 template <
class OtherT,
class OtherAccessor>
228 requires(not std::is_same_v<OtherT, T>
and std::is_convertible_v<OtherT (*)[],
T (*)[]>)
229 constexpr mdspan(
const mdspan<OtherT, Extents, LayoutPolicy, OtherAccessor> &other) noexcept
233 [[nodiscard]]
static constexpr rank_type rank() noexcept {
return impl_type::rank(); }
235 [[nodiscard]]
constexpr const extents_type &
extents() const noexcept {
return impl_.extents(); }
236 [[nodiscard]]
constexpr const mapping_type &
mapping() const noexcept {
return impl_.mapping(); }
240 [[nodiscard]]
constexpr index_type size() const noexcept {
return impl_.size(); }
241 [[nodiscard]]
constexpr bool empty() const noexcept {
return impl_.empty(); }
249 template <
class... Indices>
250 requires(
sizeof...(Indices) == extents_type::rank())
251 [[nodiscard]]
constexpr reference operator()(Indices... idxs)
const
253 return impl_[
static_cast<index_type>(idxs)...];
261 template <
class... Indices>
262 requires(
sizeof...(Indices) == extents_type::rank())
263 [[nodiscard]]
constexpr reference at(Indices... idxs)
const
268 <<
"Aleph::mdspan::at(): index[" <<
r <<
"]=" << idx[
r] <<
" out of [0, "
270 return impl_[
static_cast<index_type>(idxs)...];
280namespace mdspan_detail
282template <
size_t... Exts>
283inline constexpr size_t count_dynamic = (
size_t{0} + ... + (Exts ==
dynamic_extent ? 1 : 0));
301template <
class IndexType,
size_t... Exts>
304 static_assert(std::is_integral_v<IndexType>,
305 "Aleph::extents: IndexType must be an integral type");
307 static constexpr std::array<size_t,
sizeof...(Exts)>
Static{Exts...};
309 std::array<IndexType,
sizeof...(Exts)>
values_{};
322 return mdspan_detail::count_dynamic<Exts...>;
341 template <
class... IndexTypes>
342 requires(
sizeof...(IndexTypes) == mdspan_detail::count_dynamic<Exts...>
and
343 (std::is_convertible_v<IndexTypes, IndexType>
and ...))
344 constexpr explicit extents(IndexTypes... dyn)
noexcept
346 const std::array<IndexType,
sizeof...(IndexTypes)> given{
static_cast<IndexType
>(dyn)...};
357 template <
class OtherIndexType>
358 requires std::is_convertible_v<const OtherIndexType &, IndexType>
360 const std::array<OtherIndexType, mdspan_detail::count_dynamic<Exts...>> &dyn)
noexcept
365 :
static_cast<IndexType
>(
Static[
r]);
376 return a.values_ == b.values_;
381namespace mdspan_detail
384template <
class IndexType,
size_t... Is>
385struct dextents_helper
387 using type = extents<IndexType, ((void) Is, dynamic_extent)...>;
395template <
class IndexType,
size_t... Is>
396dextents_helper<IndexType, Is...> dextents_of(std::index_sequence<Is...>);
401template <
class IndexType,
size_t Rank>
403 typename decltype(mdspan_detail::dextents_of<IndexType>(
404 std::make_index_sequence<Rank>{}))::type;
414 template <
class Extents>
436 template <
class... Indices>
437 requires(
sizeof...(Indices) == Extents::rank())
438 [[nodiscard]]
constexpr index_type operator()(Indices... idxs)
const noexcept
468 template <
class Extents>
490 template <
class... Indices>
491 requires(
sizeof...(Indices) == Extents::rank())
492 [[nodiscard]]
constexpr index_type operator()(Indices... idxs)
const noexcept
496 for (
rank_type ri = 0; ri < Extents::rank(); ++ri)
524template <
class ElementType>
534 template <class OtherElementType>
535 requires
std::is_convertible_v<OtherElementType (*)[],
element_type (*)[]>
568template <
class T,
class Extents,
class LayoutPolicy = layout_right,
569 class AccessorPolicy = default_accessor<T>>
581 using mapping_type =
typename layout_type::template mapping<extents_type>;
611 template <class OtherT, class OtherAccessor>
612 requires(not
std::is_same_v<OtherT,
T>
and std::is_convertible_v<OtherT (*)[],
T (*)[]>)
613 constexpr
mdspan(const
mdspan<OtherT, Extents, LayoutPolicy, OtherAccessor> &other) noexcept
648 template <
class... IndexTypes>
649 requires(
sizeof...(IndexTypes) >= 1
and
650 (std::is_convertible_v<IndexTypes, index_type>
and ...))
662 template <
class OtherIndexType>
663 requires std::is_convertible_v<const OtherIndexType &, index_type>
665 const std::array<OtherIndexType, extents_type::rank_dynamic()> &dyn) noexcept
672 [[nodiscard]]
static constexpr rank_type rank() noexcept {
return extents_type::rank(); }
680 return map_.extents().extent(
r);
710 [[nodiscard]]
constexpr bool empty() const noexcept
726 template <
class... Indices>
727 requires(
sizeof...(Indices) == extents_type::rank())
728 [[nodiscard]]
constexpr reference operator()(Indices... idxs)
const
739 template <
class... Indices>
740 requires(
sizeof...(Indices) == extents_type::rank())
746 <<
"Aleph::mdspan::at(): index[" <<
r <<
"]=" << idx[
r] <<
" out of [0, "
C++20/23 compatibility layer: feature detection + Aleph:: polyfills.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
static constexpr rank_type rank_dynamic() noexcept
Number of axes whose extent is dynamic_extent (compile-time constant).
std::make_unsigned_t< IndexType > size_type
constexpr index_type extent(rank_type r) const noexcept
Run-time size of axis r.
static constexpr rank_type rank() noexcept
Number of axes (compile-time constant).
std::array< IndexType, sizeof...(Exts)> values_
constexpr extents(IndexTypes... dyn) noexcept
Construct providing a value for each dynamic axis, in rank order.
friend constexpr bool operator==(const extents &a, const extents &b) noexcept
static constexpr std::array< size_t, sizeof...(Exts)> Static
constexpr extents(const std::array< OtherIndexType, mdspan_detail::count_dynamic< Exts... > > &dyn) noexcept
Construct providing every dynamic axis's value packed in an array.
static constexpr size_t static_extent(rank_type r) noexcept
Compile-time size of axis r, or dynamic_extent if axis r is dynamic.
constexpr extents() noexcept
All-static-or-default-initialised extents (dynamic axes start at 0).
Mapping from column-major multi-indices to linear offsets.
constexpr index_type required_span_size() const noexcept
typename Extents::index_type index_type
static constexpr bool is_always_exhaustive() noexcept
typename Extents::rank_type rank_type
static constexpr bool is_always_unique() noexcept
static constexpr bool is_always_strided() noexcept
constexpr const extents_type & extents() const noexcept
constexpr mapping() noexcept=default
Mapping from row-major multi-indices to linear offsets.
static constexpr bool is_always_strided() noexcept
constexpr mapping() noexcept=default
static constexpr bool is_always_exhaustive() noexcept
constexpr index_type required_span_size() const noexcept
constexpr const extents_type & extents() const noexcept
typename Extents::index_type index_type
static constexpr bool is_always_unique() noexcept
typename Extents::rank_type rank_type
Non-owning view of a contiguous buffer as a multidimensional grid.
constexpr mdspan(data_handle_type p, const extents_type &e) noexcept
View p through the shape e.
constexpr data_handle_type data_handle() const noexcept
Underlying pointer to the first element (nullptr for a default-built view).
static constexpr rank_type rank() noexcept
Number of axes (compile-time constant).
constexpr index_type extent(rank_type r) const noexcept
Run-time size of axis r.
constexpr mdspan(data_handle_type p, IndexTypes... exts) noexcept
View p as a grid with the given per-axis sizes.
typename accessor_type::reference reference
std::remove_cv_t< T > value_type
constexpr mdspan(data_handle_type p, const mapping_type &m, const accessor_type &a) noexcept
View p through mapping m, reading via accessor a.
typename extents_type::index_type index_type
constexpr const accessor_type & accessor() const noexcept
The element-access policy of this view.
constexpr mdspan(data_handle_type p, const std::array< OtherIndexType, extents_type::rank_dynamic()> &dyn) noexcept
View p as a grid whose dynamic axes are packed in an array.
constexpr mdspan() noexcept=default
Empty view: data_handle() == nullptr, every extent is 0.
constexpr data_handle_type data() const noexcept
See data_handle(); mirrors the common Array/std::vector-style name.
typename accessor_type::data_handle_type data_handle_type
constexpr const mapping_type & mapping() const noexcept
The index-to-offset mapping (layout + shape) of this view.
AccessorPolicy accessor_type
typename extents_type::rank_type rank_type
constexpr mdspan(data_handle_type p, const mapping_type &m) noexcept
View p through an already-built mapping.
constexpr reference at(Indices... idxs) const
Bounds-checked element access.
constexpr bool empty() const noexcept
Checks if the view has no elements.
typename layout_type::template mapping< extents_type > mapping_type
constexpr index_type size() const noexcept
Total element count: the product of every extent(r).
constexpr const extents_type & extents() const noexcept
The shape of this view.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Main namespace for Aleph-w library functions.
constexpr size_t dynamic_extent
Sentinel marking an axis whose extent is only known at run time.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
typename decltype(mdspan_detail::dextents_of< IndexType >(std::make_index_sequence< Rank >{}))::type dextents
extents<IndexType, ...> with all Rank axes dynamic — the common case.
Trivial accessor policy: access(p, i) is just p[i].
constexpr reference access(data_handle_type p, size_t i) const noexcept
Access an element from a pointer and an offset.
ElementType * data_handle_type
constexpr data_handle_type offset(data_handle_type p, size_t i) const noexcept
constexpr default_accessor() noexcept=default
Column-major layout: the first axis varies fastest (Fortran convention).
Row-major layout: the last axis varies fastest (C convention).
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)