Reference documentation for deal.II version 9.0.0
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 at
12 // the top level of the deal.II distribution.
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 #include <deal.II/base/exceptions.h>
22 
23 #include <algorithm>
24 #include <ostream>
25 #include <iterator>
26 
27 
28 DEAL_II_NAMESPACE_OPEN
29 
30 
42 template <int N>
44 {
45 public:
46  static_assert (N > 0,
47  "TableIndices objects need to represent at least one index.");
48 
49 
53  TableIndices();
54 
63  explicit TableIndices (const std::size_t index0);
64 
73  TableIndices (const std::size_t index0,
74  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 operator == (const TableIndices<N> &other) const;
154 
158  bool operator != (const TableIndices<N> &other) const;
159 
164  void sort ();
165 
170  template <class Archive>
171  void serialize (Archive &ar, const unsigned int version);
172 
173 protected:
177  std::size_t indices[N];
178 };
179 
180 
181 
182 /* --------------------- Template and inline functions ---------------- */
183 
184 
185 template <int N>
187 {
188  for (unsigned int i=0; i<N; ++i)
189  indices[i] = 0;
190 }
191 
192 
193 
194 template <int N>
195 TableIndices<N>::TableIndices(const std::size_t index0)
196 {
197  static_assert (N==1,
198  "This constructor is only available for TableIndices<1> objects.");
199  indices[0] = index0;
200 }
201 
202 
203 
204 template <int N>
205 TableIndices<N>::TableIndices(const std::size_t index0,
206  const std::size_t index1)
207 {
208  static_assert (N==2,
209  "This constructor is only available for TableIndices<2> objects.");
210  indices[0] = index0;
211  indices[1] = index1;
212 }
213 
214 
215 
216 template <int N>
217 TableIndices<N>::TableIndices(const std::size_t index0,
218  const std::size_t index1,
219  const std::size_t index2)
220 {
221  static_assert (N==3,
222  "This constructor is only available for TableIndices<3> objects.");
223  indices[0] = index0;
224  indices[1] = index1;
225  indices[2] = index2;
226 }
227 
228 
229 
230 template <int N>
231 TableIndices<N>::TableIndices(const std::size_t index0,
232  const std::size_t index1,
233  const std::size_t index2,
234  const std::size_t index3)
235 {
236  static_assert (N==4,
237  "This constructor is only available for TableIndices<4> objects.");
238  indices[0] = index0;
239  indices[1] = index1;
240  indices[2] = index2;
241  indices[3] = index3;
242 }
243 
244 
245 
246 template <int N>
247 TableIndices<N>::TableIndices(const std::size_t index0,
248  const std::size_t index1,
249  const std::size_t index2,
250  const std::size_t index3,
251  const std::size_t index4)
252 {
253  static_assert (N==5,
254  "This constructor is only available for TableIndices<5> objects.");
255  indices[0] = index0;
256  indices[1] = index1;
257  indices[2] = index2;
258  indices[3] = index3;
259  indices[4] = index4;
260 }
261 
262 
263 
264 template <int N>
265 TableIndices<N>::TableIndices(const std::size_t index0,
266  const std::size_t index1,
267  const std::size_t index2,
268  const std::size_t index3,
269  const std::size_t index4,
270  const std::size_t index5,
271  const std::size_t index6,
272  const std::size_t index7,
273  const std::size_t index8)
274 {
275  switch (N)
276  {
277  case 1:
278  Assert (index1 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
279  DEAL_II_FALLTHROUGH;
280  case 2:
281  Assert (index2 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
282  DEAL_II_FALLTHROUGH;
283  case 3:
284  Assert (index3 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
285  DEAL_II_FALLTHROUGH;
286  case 4:
287  Assert (index4 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
288  DEAL_II_FALLTHROUGH;
289  case 5:
290  Assert (index5 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
291  DEAL_II_FALLTHROUGH;
292  case 6:
293  Assert (index6 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
294  DEAL_II_FALLTHROUGH;
295  case 7:
296  Assert (index7 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
297  DEAL_II_FALLTHROUGH;
298  case 8:
299  Assert (index8 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
300  break;
301  default:
302  ;
303  }
304 
305  // Always access "indices" with indices modulo N to avoid bogus compiler
306  // warnings (although such access is always in dead code...
307  switch (N)
308  {
309  default:
310  // For TableIndices of size 10 or larger als default initialize the
311  // remaining indices to numbers::invalid_unsigned_int:
312  for (unsigned int i=0; i<N; ++i)
313  indices[i] = numbers::invalid_unsigned_int;
314  DEAL_II_FALLTHROUGH;
315  case 9:
316  indices[8 % N] = index8;
317  DEAL_II_FALLTHROUGH;
318  case 8:
319  indices[7 % N] = index7;
320  DEAL_II_FALLTHROUGH;
321  case 7:
322  indices[6 % N] = index6;
323  DEAL_II_FALLTHROUGH;
324  case 6:
325  indices[5 % N] = index5;
326  DEAL_II_FALLTHROUGH;
327  case 5:
328  indices[4 % N] = index4;
329  DEAL_II_FALLTHROUGH;
330  case 4:
331  indices[3 % N] = index3;
332  DEAL_II_FALLTHROUGH;
333  case 3:
334  indices[2 % N] = index2;
335  DEAL_II_FALLTHROUGH;
336  case 2:
337  indices[1 % N] = index1;
338  DEAL_II_FALLTHROUGH;
339  case 1:
340  indices[0 % N] = index0;
341  }
342 
343 }
344 
345 
346 template <int N>
347 inline
348 std::size_t
349 TableIndices<N>::operator [] (const unsigned int i) const
350 {
351  AssertIndexRange (i, N);
352  return indices[i];
353 }
354 
355 
356 template <int N>
357 inline
358 std::size_t &
359 TableIndices<N>::operator [] (const unsigned int i)
360 {
361  AssertIndexRange (i, N);
362  return indices[i];
363 }
364 
365 
366 template <int N>
367 inline
368 bool
370 {
371  for (unsigned int i=0; i<N; ++i)
372  if (indices[i] != other.indices[i])
373  return false;
374  return true;
375 }
376 
377 
378 template <int N>
379 inline
380 bool
382 {
383  return !(*this == other);
384 }
385 
386 
387 template <int N>
388 inline
389 void
391 {
392  std::sort(std::begin(indices), std::end(indices));
393 }
394 
395 
396 template <int N>
397 template <class Archive>
398 inline
399 void
400 TableIndices<N>::serialize (Archive &ar, const unsigned int)
401 {
402  ar &indices;
403 }
404 
405 
412 template <int N>
413 std::ostream &
414 operator << (std::ostream &out,
415  const TableIndices<N> &indices)
416 {
417  out << '[';
418  for (unsigned int i=0; i<N; ++i)
419  {
420  out << indices[i];
421  if (i+1 != N)
422  out << ',';
423  }
424  out << ']';
425 
426  return out;
427 }
428 
429 
430 DEAL_II_NAMESPACE_CLOSE
431 
432 #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:1284
void serialize(Archive &ar, const unsigned int version)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1142
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:163
bool operator==(const TableIndices< N > &other) const
std::size_t indices[N]
bool operator!=(const TableIndices< N > &other) const