Reference documentation for deal.II version 9.1.0-pre
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  TableIndices();
55 
64  explicit TableIndices(const std::size_t index0);
65 
74  TableIndices(const std::size_t index0, const std::size_t index1);
75 
84  TableIndices(const std::size_t index0,
85  const std::size_t index1,
86  const std::size_t index2);
87 
96  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  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  std::size_t operator[](const unsigned int i) const;
144 
148  std::size_t &operator[](const unsigned int i);
149 
153  bool
154  operator==(const TableIndices<N> &other) const;
155 
159  bool
160  operator!=(const TableIndices<N> &other) const;
161 
166  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 template <int N>
191 {
192  for (unsigned int i = 0; i < N; ++i)
193  indices[i] = 0;
194 }
195 
196 
197 
198 template <int N>
199 TableIndices<N>::TableIndices(const std::size_t index0)
200 {
201  static_assert(
202  N == 1, "This constructor is only available for TableIndices<1> objects.");
203  indices[0] = index0;
204 }
205 
206 
207 
208 template <int N>
209 TableIndices<N>::TableIndices(const std::size_t index0,
210  const std::size_t index1)
211 {
212  static_assert(
213  N == 2, "This constructor is only available for TableIndices<2> objects.");
214  indices[0] = index0;
215  indices[1] = index1;
216 }
217 
218 
219 
220 template <int N>
221 TableIndices<N>::TableIndices(const std::size_t index0,
222  const std::size_t index1,
223  const std::size_t index2)
224 {
225  static_assert(
226  N == 3, "This constructor is only available for TableIndices<3> objects.");
227  indices[0] = index0;
228  indices[1] = index1;
229  indices[2] = index2;
230 }
231 
232 
233 
234 template <int N>
235 TableIndices<N>::TableIndices(const std::size_t index0,
236  const std::size_t index1,
237  const std::size_t index2,
238  const std::size_t index3)
239 {
240  static_assert(
241  N == 4, "This constructor is only available for TableIndices<4> objects.");
242  indices[0] = index0;
243  indices[1] = index1;
244  indices[2] = index2;
245  indices[3] = index3;
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 {
257  static_assert(
258  N == 5, "This constructor is only available for TableIndices<5> objects.");
259  indices[0] = index0;
260  indices[1] = index1;
261  indices[2] = index2;
262  indices[3] = index3;
263  indices[4] = index4;
264 }
265 
266 
267 
268 template <int N>
269 TableIndices<N>::TableIndices(const std::size_t index0,
270  const std::size_t index1,
271  const std::size_t index2,
272  const std::size_t index3,
273  const std::size_t index4,
274  const std::size_t index5,
275  const std::size_t index6,
276  const std::size_t index7,
277  const std::size_t index8)
278 {
279  switch (N)
280  {
281  case 1:
283  ExcMessage("more than N index values provided"));
284  DEAL_II_FALLTHROUGH;
285  case 2:
287  ExcMessage("more than N index values provided"));
288  DEAL_II_FALLTHROUGH;
289  case 3:
291  ExcMessage("more than N index values provided"));
292  DEAL_II_FALLTHROUGH;
293  case 4:
295  ExcMessage("more than N index values provided"));
296  DEAL_II_FALLTHROUGH;
297  case 5:
299  ExcMessage("more than N index values provided"));
300  DEAL_II_FALLTHROUGH;
301  case 6:
303  ExcMessage("more than N index values provided"));
304  DEAL_II_FALLTHROUGH;
305  case 7:
307  ExcMessage("more than N index values provided"));
308  DEAL_II_FALLTHROUGH;
309  case 8:
311  ExcMessage("more than N index values provided"));
312  break;
313  default:;
314  }
315 
316  // Always access "indices" with indices modulo N to avoid bogus compiler
317  // warnings (although such access is always in dead code...
318  switch (N)
319  {
320  default:
321  // For TableIndices of size 10 or larger als default initialize the
322  // remaining indices to numbers::invalid_unsigned_int:
323  for (unsigned int i = 0; i < N; ++i)
325  DEAL_II_FALLTHROUGH;
326  case 9:
327  indices[8 % N] = index8;
328  DEAL_II_FALLTHROUGH;
329  case 8:
330  indices[7 % N] = index7;
331  DEAL_II_FALLTHROUGH;
332  case 7:
333  indices[6 % N] = index6;
334  DEAL_II_FALLTHROUGH;
335  case 6:
336  indices[5 % N] = index5;
337  DEAL_II_FALLTHROUGH;
338  case 5:
339  indices[4 % N] = index4;
340  DEAL_II_FALLTHROUGH;
341  case 4:
342  indices[3 % N] = index3;
343  DEAL_II_FALLTHROUGH;
344  case 3:
345  indices[2 % N] = index2;
346  DEAL_II_FALLTHROUGH;
347  case 2:
348  indices[1 % N] = index1;
349  DEAL_II_FALLTHROUGH;
350  case 1:
351  indices[0 % N] = index0;
352  }
353 }
354 
355 
356 template <int N>
357 inline std::size_t TableIndices<N>::operator[](const unsigned int i) const
358 {
359  AssertIndexRange(i, N);
360  return indices[i];
361 }
362 
363 
364 template <int N>
365 inline std::size_t &TableIndices<N>::operator[](const unsigned int i)
366 {
367  AssertIndexRange(i, N);
368  return indices[i];
369 }
370 
371 
372 template <int N>
373 inline bool
375 {
376  for (unsigned int i = 0; i < N; ++i)
377  if (indices[i] != other.indices[i])
378  return false;
379  return true;
380 }
381 
382 
383 template <int N>
384 inline bool
386 {
387  return !(*this == other);
388 }
389 
390 
391 template <int N>
392 inline void
394 {
395  std::sort(std::begin(indices), std::end(indices));
396 }
397 
398 
399 template <int N>
400 template <class Archive>
401 inline void
402 TableIndices<N>::serialize(Archive &ar, const unsigned int)
403 {
404  ar &indices;
405 }
406 
407 
414 template <int N>
415 std::ostream &
416 operator<<(std::ostream &out, const TableIndices<N> &indices)
417 {
418  out << '[';
419  for (unsigned int i = 0; i < N; ++i)
420  {
421  out << indices[i];
422  if (i + 1 != N)
423  out << ',';
424  }
425  out << ']';
426 
427  return out;
428 }
429 
430 
431 DEAL_II_NAMESPACE_CLOSE
432 
433 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:173
std::size_t operator[](const unsigned int i) const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1407
void serialize(Archive &ar, const unsigned int version)
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
bool operator!=(const TableIndices< N > &other) const
std::size_t indices[N]
bool operator==(const TableIndices< N > &other) const