Reference documentation for deal.II version Git 5f32ffafa8 2019-10-18 00:26:50 -0600
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
table_indices.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2005 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_table_indices_h
17 #define dealii_table_indices_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/exceptions.h>
23 
24 #include <algorithm>
25 #include <iterator>
26 #include <ostream>
27 
28 
29 DEAL_II_NAMESPACE_OPEN
30 
31 
43 template <int N>
45 {
46 public:
47  static_assert(N > 0,
48  "TableIndices objects need to represent at least one index.");
49 
50 
54  constexpr TableIndices() = default;
55 
64  constexpr explicit TableIndices(const std::size_t index0);
65 
74  constexpr TableIndices(const std::size_t index0, const std::size_t index1);
75 
84  constexpr TableIndices(const std::size_t index0,
85  const std::size_t index1,
86  const std::size_t index2);
87 
96  constexpr TableIndices(const std::size_t index0,
97  const std::size_t index1,
98  const std::size_t index2,
99  const std::size_t index3);
100 
109  constexpr TableIndices(const std::size_t index0,
110  const std::size_t index1,
111  const std::size_t index2,
112  const std::size_t index3,
113  const std::size_t index4);
114 
129  DEAL_II_DEPRECATED
130  TableIndices(const std::size_t index0,
131  const std::size_t index1,
132  const std::size_t index2,
133  const std::size_t index3,
134  const std::size_t index4,
135  const std::size_t index5,
136  const std::size_t index6 = numbers::invalid_unsigned_int,
137  const std::size_t index7 = numbers::invalid_unsigned_int,
138  const std::size_t index8 = numbers::invalid_unsigned_int);
139 
143  DEAL_II_CONSTEXPR std::size_t operator[](const unsigned int i) const;
144 
148  DEAL_II_CONSTEXPR std::size_t &operator[](const unsigned int i);
149 
153  constexpr bool
154  operator==(const TableIndices<N> &other) const;
155 
159  constexpr bool
160  operator!=(const TableIndices<N> &other) const;
161 
166  DEAL_II_CONSTEXPR void
167  sort();
168 
173  template <class Archive>
174  void
175  serialize(Archive &ar, const unsigned int version);
176 
177 protected:
181  std::size_t indices[N]{};
182 };
183 
184 
185 
186 /* --------------------- Template and inline functions ---------------- */
187 
188 
189 
190 template <int N>
191 constexpr TableIndices<N>::TableIndices(const std::size_t index0)
192  : indices{index0}
193 {
194  static_assert(
195  N == 1, "This constructor is only available for TableIndices<1> objects.");
196 }
197 
198 
199 
200 template <int N>
201 constexpr TableIndices<N>::TableIndices(const std::size_t index0,
202  const std::size_t index1)
203  : indices{index0, index1}
204 {
205  static_assert(
206  N == 2, "This constructor is only available for TableIndices<2> objects.");
207 }
208 
209 
210 
211 template <int N>
212 constexpr TableIndices<N>::TableIndices(const std::size_t index0,
213  const std::size_t index1,
214  const std::size_t index2)
215  : indices{index0, index1, index2}
216 {
217  static_assert(
218  N == 3, "This constructor is only available for TableIndices<3> objects.");
219 }
220 
221 
222 
223 template <int N>
224 constexpr TableIndices<N>::TableIndices(const std::size_t index0,
225  const std::size_t index1,
226  const std::size_t index2,
227  const std::size_t index3)
228  : indices{index0, index1, index2, index3}
229 {
230  static_assert(
231  N == 4, "This constructor is only available for TableIndices<4> objects.");
232 }
233 
234 
235 
236 template <int N>
237 constexpr TableIndices<N>::TableIndices(const std::size_t index0,
238  const std::size_t index1,
239  const std::size_t index2,
240  const std::size_t index3,
241  const std::size_t index4)
242  : indices{index0, index1, index2, index3, index4}
243 {
244  static_assert(
245  N == 5, "This constructor is only available for TableIndices<5> objects.");
246 }
247 
248 
249 
250 template <int N>
251 TableIndices<N>::TableIndices(const std::size_t index0,
252  const std::size_t index1,
253  const std::size_t index2,
254  const std::size_t index3,
255  const std::size_t index4,
256  const std::size_t index5,
257  const std::size_t index6,
258  const std::size_t index7,
259  const std::size_t index8)
260 {
261  switch (N)
262  {
263  case 1:
265  ExcMessage("more than N index values provided"));
266  DEAL_II_FALLTHROUGH;
267  case 2:
269  ExcMessage("more than N index values provided"));
270  DEAL_II_FALLTHROUGH;
271  case 3:
273  ExcMessage("more than N index values provided"));
274  DEAL_II_FALLTHROUGH;
275  case 4:
277  ExcMessage("more than N index values provided"));
278  DEAL_II_FALLTHROUGH;
279  case 5:
281  ExcMessage("more than N index values provided"));
282  DEAL_II_FALLTHROUGH;
283  case 6:
285  ExcMessage("more than N index values provided"));
286  DEAL_II_FALLTHROUGH;
287  case 7:
289  ExcMessage("more than N index values provided"));
290  DEAL_II_FALLTHROUGH;
291  case 8:
293  ExcMessage("more than N index values provided"));
294  break;
295  default:;
296  }
297 
298  // Always access "indices" with indices modulo N to avoid bogus compiler
299  // warnings (although such access is always in dead code...
300  switch (N)
301  {
302  default:
303  // For TableIndices of size 10 or larger als default initialize the
304  // remaining indices to numbers::invalid_unsigned_int:
305  for (unsigned int i = 0; i < N; ++i)
307  DEAL_II_FALLTHROUGH;
308  case 9:
309  indices[8 % N] = index8;
310  DEAL_II_FALLTHROUGH;
311  case 8:
312  indices[7 % N] = index7;
313  DEAL_II_FALLTHROUGH;
314  case 7:
315  indices[6 % N] = index6;
316  DEAL_II_FALLTHROUGH;
317  case 6:
318  indices[5 % N] = index5;
319  DEAL_II_FALLTHROUGH;
320  case 5:
321  indices[4 % N] = index4;
322  DEAL_II_FALLTHROUGH;
323  case 4:
324  indices[3 % N] = index3;
325  DEAL_II_FALLTHROUGH;
326  case 3:
327  indices[2 % N] = index2;
328  DEAL_II_FALLTHROUGH;
329  case 2:
330  indices[1 % N] = index1;
331  DEAL_II_FALLTHROUGH;
332  case 1:
333  indices[0 % N] = index0;
334  }
335 }
336 
337 
338 template <int N>
339 DEAL_II_CONSTEXPR inline std::size_t TableIndices<N>::
340  operator[](const unsigned int i) const
341 {
342  AssertIndexRange(i, N);
343  return indices[i];
344 }
345 
346 
347 template <int N>
348 DEAL_II_CONSTEXPR inline std::size_t &TableIndices<N>::
349  operator[](const unsigned int i)
350 {
351  AssertIndexRange(i, N);
352  return indices[i];
353 }
354 
355 
356 template <int N>
357 constexpr bool
359 {
360  return std::equal(std::begin(indices),
361  std::end(indices),
362  std::begin(other.indices));
363 }
364 
365 
366 template <int N>
367 constexpr bool
369 {
370  return !(*this == other);
371 }
372 
373 
374 template <int N>
375 DEAL_II_CONSTEXPR inline void
377 {
378  std::sort(std::begin(indices), std::end(indices));
379 }
380 
381 
382 template <int N>
383 template <class Archive>
384 inline void
385 TableIndices<N>::serialize(Archive &ar, const unsigned int)
386 {
387  ar &indices;
388 }
389 
390 
397 template <int N>
398 std::ostream &
399 operator<<(std::ostream &out, const TableIndices<N> &indices)
400 {
401  out << '[';
402  for (unsigned int i = 0; i < N; ++i)
403  {
404  out << indices[i];
405  if (i + 1 != N)
406  out << ',';
407  }
408  out << ']';
409 
410  return out;
411 }
412 
413 
414 DEAL_II_NAMESPACE_CLOSE
415 
416 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:187
DEAL_II_CONSTEXPR std::size_t operator[](const unsigned int i) const
constexpr bool operator!=(const TableIndices< N > &other) const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1637
constexpr bool operator==(const TableIndices< N > &other) const
void serialize(Archive &ar, const unsigned int version)
DEAL_II_CONSTEXPR void sort()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1407
constexpr TableIndices()=default
std::size_t indices[N]