dune-common 2.12-git
Loading...
Searching...
No Matches
submdspan.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_STD_SUBMDSPAN_HH
6#define DUNE_COMMON_STD_SUBMDSPAN_HH
7
8#include <array>
9#include <tuple>
10#include <type_traits>
11
17
76namespace Dune::Std {
77
84template <class IndexType, std::size_t... exts, class... SliceSpecifiers,
85 std::enable_if_t<(sizeof...(SliceSpecifiers) == sizeof...(exts)), int> = 0>
86constexpr auto subextents (const extents<IndexType,exts...>& src, SliceSpecifiers... slices)
87{
88 using sub_extents_type = Impl::SubextentsType<extents<IndexType,exts...>,SliceSpecifiers...>;
89 std::array<IndexType,sub_extents_type::rank()> sub_exts{};
90 std::size_t j = 0;
91 auto tuple = std::tuple<SliceSpecifiers...>{slices...};
92 Dune::unpackIntegerSequence([&](auto... k) {
93 auto add = [&](auto kk) {
94 constexpr std::size_t i = decltype(kk)::value;
95 using slice_type = std::tuple_element_t<i,decltype(tuple)>;
96 if constexpr(Impl::is_kept_slice_v<slice_type>)
97 sub_exts[j++] = Impl::slice_extent<i>(src, std::get<i>(tuple));
98 };
100 }, std::index_sequence_for<SliceSpecifiers...>{});
101 return sub_extents_type{sub_exts};
102}
103
110template <class Mapping, class... SliceSpecifiers,
111 std::enable_if_t<(sizeof...(SliceSpecifiers) == Mapping::extents_type::rank()), int> = 0>
112constexpr auto submdspan_mapping (const Mapping& mapping, SliceSpecifiers... slices)
113{
114 auto tuple = std::tuple<SliceSpecifiers...>{slices...};
115 return Impl::make_sub_mapping(mapping, tuple, std::index_sequence_for<SliceSpecifiers...>{});
116}
117
125template <class ElementType, class Extents, class LayoutPolicy, class AccessorPolicy,
126 class... SliceSpecifiers,
127 std::enable_if_t<(sizeof...(SliceSpecifiers) == Extents::rank()), int> = 0>
129 SliceSpecifiers... slices)
130{
131 auto result = submdspan_mapping(src.mapping(), slices...);
132 using mapping_type = decltype(result.mapping);
133 using accessor_type = typename AccessorPolicy::offset_policy;
135 src.accessor().offset(src.data_handle(), result.offset),
136 result.mapping,
137 accessor_type(src.accessor())};
138}
139
140} // end namespace Dune::Std
141
142#endif // DUNE_COMMON_STD_SUBMDSPAN_HH
decltype(auto) constexpr unpackIntegerSequence(F &&f, std::integer_sequence< I, i... > sequence)
Unpack an std::integer_sequence<I,i...> to std::integral_constant<I,i>...
Definition indices.hh:124
constexpr auto submdspan_mapping(const Mapping &mapping, SliceSpecifiers... slices)
Create the mapping and source-data offset produced by slicing a layout mapping.
Definition submdspan.hh:112
constexpr auto subextents(const extents< IndexType, exts... > &src, SliceSpecifiers... slices)
Return the extents produced by slicing a source extents object.
Definition submdspan.hh:86
constexpr auto submdspan(const mdspan< ElementType, Extents, LayoutPolicy, AccessorPolicy > &src, SliceSpecifiers... slices)
Create an mdspan view by slicing an existing mdspan.
Definition submdspan.hh:128
Namespace for features backported from new C++ standards.
Definition algorithm.hh:19
Multidimensional index space with dynamic and static extents.
Definition extents.hh:55
A multi-dimensional non-owning array view.
Definition mdspan.hh:67
constexpr const accessor_type & accessor() const noexcept
The accessor policy object.
Definition mdspan.hh:296
constexpr const mapping_type & mapping() const noexcept
Index mapping of a layout policy.
Definition mdspan.hh:293
constexpr const data_handle_type & data_handle() const noexcept
The pointer to the underlying flat sequence.
Definition mdspan.hh:299