dune-common  2.11
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 
6 #ifndef DUNE_COMMON_INDICES_HH
7 #define DUNE_COMMON_INDICES_HH
8 
9 #include <cstddef>
10 #include <stdexcept>
11 #include <type_traits>
12 #include <utility>
13 
14 namespace Dune
15 {
28  template<std::size_t i>
29  using index_constant = std::integral_constant<std::size_t, i>;
30 
31 
32 
49  namespace Indices
50  {
52  inline constexpr index_constant< 0> _0 = {};
53 
55  inline constexpr index_constant< 1> _1 = {};
56 
58  inline constexpr index_constant< 2> _2 = {};
59 
61  inline constexpr index_constant< 3> _3 = {};
62 
64  inline constexpr index_constant< 4> _4 = {};
65 
67  inline constexpr index_constant< 5> _5 = {};
68 
70  inline constexpr index_constant< 6> _6 = {};
71 
73  inline constexpr index_constant< 7> _7 = {};
74 
76  inline constexpr index_constant< 8> _8 = {};
77 
79  inline constexpr index_constant< 9> _9 = {};
80 
82  inline constexpr index_constant<10> _10 = {};
83 
85  inline constexpr index_constant<11> _11 = {};
86 
88  inline constexpr index_constant<12> _12 = {};
89 
91  inline constexpr index_constant<13> _13 = {};
92 
94  inline constexpr index_constant<14> _14 = {};
95 
97  inline constexpr index_constant<15> _15 = {};
98 
100  inline constexpr index_constant<16> _16 = {};
101 
103  inline constexpr index_constant<17> _17 = {};
104 
106  inline constexpr index_constant<18> _18 = {};
107 
109  inline constexpr index_constant<19> _19 = {};
110 
111  } // namespace Indices
112 
123  template<class F, class I, I... i>
124  decltype(auto) constexpr unpackIntegerSequence(F&& f, [[maybe_unused]] std::integer_sequence<I, i...> sequence)
125  {
126  return f(std::integral_constant<I, i>()...);
127  }
128 
129 
130  namespace Indices { inline namespace Literals
131  {
132  namespace Impl
133  {
134  // convert a single character into an unsigned integer
135  constexpr unsigned char2digit (const char c)
136  {
137  if (c >= '0' && c <= '9')
138  return unsigned(c) - unsigned('0');
139  else {
140  throw std::invalid_argument("Character is not a digit.");
141  return 0u;
142  }
143  }
144 
145  // convert a sequence of character digits into an unsigned integer
146  template <class T, char... digits>
147  constexpr T chars2number ()
148  {
149  const char arr[] = {digits...};
150  T result = 0;
151  T power = 1;
152  const T base = 10;
153 
154  const int N = sizeof...(digits);
155  for (int i = 0; i < N; ++i) {
156  char c = arr[N - 1 - i];
157  result+= char2digit(c) * power;
158  power *= base;
159  }
160 
161  return result;
162  }
163 
164  } //namespace Impl
165 
172  template <char... digits>
173  constexpr auto operator""_ic()
174  {
175  return std::integral_constant<std::size_t, Impl::chars2number<std::size_t,digits...>()>{};
176  }
177 
184  template <char... digits>
185  constexpr auto operator""_uc()
186  {
187  return std::integral_constant<unsigned, Impl::chars2number<unsigned,digits...>()>{};
188  }
189 
196  template <char... digits>
197  constexpr auto operator""_sc()
198  {
199  return std::integral_constant<int, Impl::chars2number<int,digits...>()>{};
200  }
201 
208  template <class T, T value>
209  constexpr auto operator- (std::integral_constant<T,value>)
210  {
211  return std::integral_constant<std::make_signed_t<T>, -value>{};
212  }
213 
214  }} //namespace Indices::Literals
215 } //namespace Dune
216 
217 #endif // DUNE_COMMON_INDICES_HH
constexpr index_constant< 9 > _9
Compile time index with value 9.
Definition: indices.hh:79
constexpr index_constant< 12 > _12
Compile time index with value 12.
Definition: indices.hh:88
constexpr Base power(Base m, Exponent p)
Power method for integer exponents.
Definition: math.hh:75
constexpr index_constant< 4 > _4
Compile time index with value 4.
Definition: indices.hh:64
constexpr index_constant< 6 > _6
Compile time index with value 6.
Definition: indices.hh:70
decltype(auto) constexpr unpackIntegerSequence(F &&f, [[maybe_unused]] std::integer_sequence< I, i... > sequence)
Unpack an std::integer_sequence<I,i...> to std::integral_constant<I,i>...
Definition: indices.hh:124
constexpr index_constant< 18 > _18
Compile time index with value 18.
Definition: indices.hh:106
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:29
constexpr index_constant< 11 > _11
Compile time index with value 11.
Definition: indices.hh:85
constexpr index_constant< 5 > _5
Compile time index with value 5.
Definition: indices.hh:67
constexpr auto operator-(std::integral_constant< T, value >)
Negation operator for integral constants.
Definition: indices.hh:209
I i
Definition: hybridmultiindex.hh:328
constexpr index_constant< 8 > _8
Compile time index with value 8.
Definition: indices.hh:76
constexpr index_constant< 16 > _16
Compile time index with value 16.
Definition: indices.hh:100
Dune namespace
Definition: alignedallocator.hh:12
constexpr index_constant< 17 > _17
Compile time index with value 17.
Definition: indices.hh:103
constexpr index_constant< 3 > _3
Compile time index with value 3.
Definition: indices.hh:61
constexpr index_constant< 7 > _7
Compile time index with value 7.
Definition: indices.hh:73
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:52
constexpr index_constant< 14 > _14
Compile time index with value 14.
Definition: indices.hh:94
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition: indices.hh:55
constexpr index_constant< 2 > _2
Compile time index with value 2.
Definition: indices.hh:58
STL namespace.
constexpr index_constant< 15 > _15
Compile time index with value 15.
Definition: indices.hh:97
constexpr index_constant< 13 > _13
Compile time index with value 13.
Definition: indices.hh:91
constexpr index_constant< 10 > _10
Compile time index with value 10.
Definition: indices.hh:82
constexpr index_constant< 19 > _19
Compile time index with value 19.
Definition: indices.hh:109