version 3.10-dev
utility.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_COMMON_TYPETRAITS_UTILITY_HH
13#define DUMUX_COMMON_TYPETRAITS_UTILITY_HH
14
15#include <cstddef>
16#include <utility>
17
18namespace Dumux {
19
20/*
21 * \ingroup Typetraits
22 * \brief create a variadic template from indexed types
23 * \tparam V a variadic template that we want to create
24 * \tparam T an indexed type (type that gets an index as template parameter)
25 * \tparam U the list of indices
26 */
27template <template<typename... Args> class Variadic, template<std::size_t> class Indexed, class U>
29
30template <template<typename... Args> class Variadic, template<std::size_t> class Indexed, std::size_t... IndexSeq>
31struct makeFromIndexedType<Variadic, Indexed, std::index_sequence<IndexSeq...>>
32{
33 using type = Variadic<Indexed<IndexSeq>...>;
34};
35
36namespace Detail {
37 template <class Seq1, std::size_t offset, class Seq2> struct ConcatSeq;
38
39 template <std::size_t ... Is1, std::size_t offset, std::size_t ... Is2>
40 struct ConcatSeq<std::index_sequence<Is1...>, offset, std::index_sequence<Is2...>>
41 {
42 using type = std::index_sequence<Is1..., (offset + Is2)...>;
43 };
44}
45
46/*
47 * \ingroup Typetraits
48 * \brief create an integer sequence from 0 to n-1, omitting one specific number e
49 * \tparam n number of integers in complete sequence before omitting
50 * \tparam e value of integer to be omitted
51 *
52 * example: makeIncompleteIntegerSequence<3, 1> = [0, 2]
53 * example: makeIncompleteIntegerSequence<4, 4> = [0, 1, 2, 3]
54 *
55 * see https://stackoverflow.com/questions/27124920/compile-time-generate-integer-sequence-with-one-left-out for details
56 */
57template <std::size_t n, std::size_t e>
59 typename Detail::ConcatSeq<decltype(std::make_index_sequence<e>{}), e + 1, decltype(std::make_index_sequence<(n > e) ? (n - e - 1) : 0>{})>::type;
60
61/*
62 * \ingroup Typetraits
63 * \brief add an offset to an index sequence
64 * \tparam offset the offset
65 * \tparam is the index sequence
66 *
67 * see https://stackoverflow.com/a/35625414
68 */
69template <std::size_t offset, std::size_t ... is>
70constexpr std::index_sequence<(offset + is)...> addOffsetToIndexSequence(std::index_sequence<is...>)
71{ return {}; }
72
73/*
74 * \ingroup Typetraits
75 * \brief create an index sequence starting from an offset
76 * \tparam offset the offset
77 * \tparam n the length of the sequence
78 *
79 * example: makeIndexSequenceWithOffset<2, 3> = [2,3,4]
80 *
81 * see https://stackoverflow.com/a/35625414
82 */
83template <std::size_t offset, std::size_t n>
85{
86 return addOffsetToIndexSequence<offset>(std::make_index_sequence<n>{});
87}
88
89} // end namespace Dumux
90
91#endif
Definition: adapt.hh:17
constexpr std::index_sequence<(offset+is)... > addOffsetToIndexSequence(std::index_sequence< is... >)
Definition: utility.hh:70
constexpr auto makeIndexSequenceWithOffset()
Definition: utility.hh:84
typename Detail::ConcatSeq< decltype(std::make_index_sequence< e >{}), e+1, decltype(std::make_index_sequence<(n > e) ?(n - e - 1) :0 >{})>::type makeIncompleteIntegerSequence
Definition: utility.hh:59
std::index_sequence< Is1...,(offset+Is2)... > type
Definition: utility.hh:42
Definition: utility.hh:37
Variadic< Indexed< IndexSeq >... > type
Definition: utility.hh:33
Definition: utility.hh:28