3.2-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_COMMON_TYPETRAITS_UTILITY_HH
25#define DUMUX_COMMON_TYPETRAITS_UTILITY_HH
26
27#include <cstddef>
28#include <utility>
29
30namespace Dumux {
31
32/*
33 * \ingroup TypeTraits
34 * \brief create a variadic template from indexed types
35 * \tparam V a variadic template that we want to create
36 * \tparam T an indexed type (type that gets an index as template parameter)
37 * \tparam U the list of indices
38 */
39template <template<typename... Args> class Variadic, template<std::size_t> class Indexed, class U>
41
42template <template<typename... Args> class Variadic, template<std::size_t> class Indexed, std::size_t... IndexSeq>
43struct makeFromIndexedType<Variadic, Indexed, std::index_sequence<IndexSeq...>>
44{
45 using type = Variadic<Indexed<IndexSeq>...>;
46};
47
48namespace Detail {
49 template <class Seq1, std::size_t offset, class Seq2> struct ConcatSeq;
50
51 template <std::size_t ... Is1, std::size_t offset, std::size_t ... Is2>
52 struct ConcatSeq<std::index_sequence<Is1...>, offset, std::index_sequence<Is2...>>
53 {
54 using type = std::index_sequence<Is1..., (offset + Is2)...>;
55 };
56}
57
58/*
59 * \ingroup TypeTraits
60 * \brief create an integer sequence from 0 to n-1, omitting one specific number e
61 * \tparam n number of integers in complete sequence before omitting
62 * \tparam e value of integer to be omitted
63 *
64 * example: makeIncompleteIntegerSequence<3, 1> = [0, 2]
65 * example: makeIncompleteIntegerSequence<4, 4> = [0, 1, 2, 3]
66 *
67 * see https://stackoverflow.com/questions/27124920/compile-time-generate-integer-sequence-with-one-left-out for details
68 */
69template <std::size_t n, std::size_t e>
71 typename Detail::ConcatSeq<decltype(std::make_index_sequence<e>{}), e + 1, decltype(std::make_index_sequence<(n > e) ? (n - e - 1) : 0>{})>::type;
72
73/*
74 * \ingroup TypeTraits
75 * \brief add an offset to an index sequence
76 * \tparam offset the offset
77 * \tparam is the index sequence
78 *
79 * see https://stackoverflow.com/a/35625414
80 */
81template <std::size_t offset, std::size_t ... is>
82constexpr std::index_sequence<(offset + is)...> addOffsetToIndexSequence(std::index_sequence<is...>)
83{ return {}; }
84
85/*
86 * \ingroup TypeTraits
87 * \brief create an index sequence starting from an offset
88 * \tparam offset the offset
89 * \tparam n the length of the sequence
90 *
91 * example: makeIndexSequenceWithOffset<2, 3> = [2,3,4]
92 *
93 * see https://stackoverflow.com/a/35625414
94 */
95template <std::size_t offset, std::size_t n>
97{
98 return addOffsetToIndexSequence<offset>(std::make_index_sequence<n>{});
99}
100
101} // end namespace Dumux
102
103#endif
Definition: adapt.hh:29
constexpr std::index_sequence<(offset+is)... > addOffsetToIndexSequence(std::index_sequence< is... >)
Definition: utility.hh:82
constexpr auto makeIndexSequenceWithOffset()
Definition: utility.hh:96
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:71
Definition: utility.hh:40
Variadic< Indexed< IndexSeq >... > type
Definition: utility.hh:45
Definition: utility.hh:49
std::index_sequence< Is1...,(offset+Is2)... > type
Definition: utility.hh:54