dune-common 2.12-git
Loading...
Searching...
No Matches
std/impl/indices.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_IMPL_INDICES_HH
6#define DUNE_COMMON_STD_IMPL_INDICES_HH
7
8#include <array>
9#include <span>
10#include <type_traits>
11
13
14namespace Dune::Std::Impl {
15
16template <class Tensor, class Index>
17[[nodiscard]] constexpr bool indexInDimension (const Tensor& tensor,
18 typename Tensor::rank_type r,
19 const Index& index) noexcept
20{
21 using index_type = typename Tensor::index_type;
22 if constexpr(std::is_signed_v<std::remove_cv_t<Index>>)
23 if (index < 0)
24 return false;
25 return index_type(index) < tensor.extent(r);
26}
27
29template <class Tensor, class... Indices>
30[[nodiscard]] constexpr bool indexInIndexSpace (const Tensor& tensor, const Indices&... indices) noexcept
31{
32 return unpackIntegerSequence([&](auto... ii) {
33 return (indexInDimension(tensor, ii, indices) && ...);
34 }, std::make_index_sequence<sizeof...(Indices)>{});
35}
36
38template <class Tensor, class Index>
39[[nodiscard]] constexpr bool indexInIndexSpace (
40 const Tensor& tensor,
41 const std::array<Index,Tensor::extents_type::rank()>& indices) noexcept
42{
43 return unpackIntegerSequence([&](auto... ii) {
44 return (indexInDimension(tensor, ii, indices[ii]) && ...);
45 }, std::make_index_sequence<Tensor::extents_type::rank()>{});
46}
47
49template <class Tensor, class Index>
50[[nodiscard]] constexpr bool indexInIndexSpace (
51 const Tensor& tensor,
52 std::span<Index,Tensor::extents_type::rank()> indices) noexcept
53{
54 return unpackIntegerSequence([&](auto... ii) {
55 return (indexInDimension(tensor, ii, indices[ii]) && ...);
56 }, std::make_index_sequence<Tensor::extents_type::rank()>{});
57}
58
59} // end namespace Dune::Std::Impl
60
61#endif // DUNE_COMMON_STD_IMPL_INDICES_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