Reference documentation for deal.II version 8.5.1
table_indices.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2005 - 2016 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 #include <deal.II/base/std_cxx11/iterator.h>
23 
24 #include <algorithm>
25 #include <ostream>
26 
27 
28 DEAL_II_NAMESPACE_OPEN
29 
30 
40 template <int N>
42 {
43 public:
44 
48  TableIndices();
49 
63  TableIndices (const unsigned int index0,
64  const unsigned int index1 = numbers::invalid_unsigned_int,
65  const unsigned int index2 = numbers::invalid_unsigned_int,
66  const unsigned int index3 = numbers::invalid_unsigned_int,
67  const unsigned int index4 = numbers::invalid_unsigned_int,
68  const unsigned int index5 = numbers::invalid_unsigned_int,
69  const unsigned int index6 = numbers::invalid_unsigned_int,
70  const unsigned int index7 = numbers::invalid_unsigned_int,
71  const unsigned int index8 = numbers::invalid_unsigned_int);
72 
76  unsigned int operator[] (const unsigned int i) const;
77 
81  unsigned int &operator[] (const unsigned int i);
82 
86  bool operator == (const TableIndices<N> &other) const;
87 
91  bool operator != (const TableIndices<N> &other) const;
92 
97  void sort ();
98 
103  template <class Archive>
104  void serialize (Archive &ar, const unsigned int version);
105 
106 protected:
110  unsigned int indices[N];
111 };
112 
113 
114 
115 /* --------------------- Template and inline functions ---------------- */
116 
117 
118 template <int N>
120 {
121  Assert (N > 0, ExcMessage("Cannot create a TableIndices object of size 0"));
122 
123  for (unsigned int i=0; i<N; ++i)
124  indices[i] = 0;
125 }
126 
127 
128 template <int N>
129 TableIndices<N>::TableIndices(const unsigned int index0,
130  const unsigned int index1,
131  const unsigned int index2,
132  const unsigned int index3,
133  const unsigned int index4,
134  const unsigned int index5,
135  const unsigned int index6,
136  const unsigned int index7,
137  const unsigned int index8)
138 {
139  Assert (N > 0, ExcMessage("Cannot create a TableIndices object of size 0"));
140 
141  switch (N)
142  {
143  case 1: // fallthrough
144  Assert (index1 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
145  case 2: // fallthrough
146  Assert (index2 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
147  case 3: // fallthrough
148  Assert (index3 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
149  case 4: // fallthrough
150  Assert (index4 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
151  case 5: // fallthrough
152  Assert (index5 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
153  case 6: // fallthrough
154  Assert (index6 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
155  case 7: // fallthrough
156  Assert (index7 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
157  case 8: // fallthrough
158  Assert (index8 == numbers::invalid_unsigned_int, ExcMessage("more than N index values provided"));
159  default:
160  ;
161  }
162 
163  // Always access "indices" with indices modulo N to avoid bogus compiler
164  // warnings (although such access is always in dead code...
165  switch (N)
166  {
167  default:
168  // For TableIndices of size 10 or larger als default initialize the
169  // remaining indices to numbers::invalid_unsigned_int:
170  for (unsigned int i=0; i<N; ++i)
171  indices[i] = numbers::invalid_unsigned_int;
172  case 9: // fallthrough
173  indices[8 % N] = index8;
174  case 8: // fallthrough
175  indices[7 % N] = index7;
176  case 7: // fallthrough
177  indices[6 % N] = index6;
178  case 6: // fallthrough
179  indices[5 % N] = index5;
180  case 5: // fallthrough
181  indices[4 % N] = index4;
182  case 4: // fallthrough
183  indices[3 % N] = index3;
184  case 3: // fallthrough
185  indices[2 % N] = index2;
186  case 2: // fallthrough
187  indices[1 % N] = index1;
188  case 1: // fallthrough
189  indices[0 % N] = index0;
190  }
191 
192 }
193 
194 
195 template <int N>
196 inline
197 unsigned int
198 TableIndices<N>::operator [] (const unsigned int i) const
199 {
200  Assert (i < N, ExcIndexRange (i, 0, N));
201  return indices[i];
202 }
203 
204 
205 template <int N>
206 inline
207 unsigned int &
208 TableIndices<N>::operator [] (const unsigned int i)
209 {
210  Assert (i < N, ExcIndexRange (i, 0, N));
211  return indices[i];
212 }
213 
214 
215 template <int N>
216 inline
217 bool
219 {
220  for (unsigned int i=0; i<N; ++i)
221  if (indices[i] != other.indices[i])
222  return false;
223  return true;
224 }
225 
226 
227 template <int N>
228 inline
229 bool
231 {
232  return !(*this == other);
233 }
234 
235 
236 template <int N>
237 inline
238 void
240 {
241  std::sort(std_cxx11::begin(indices), std_cxx11::end(indices));
242 }
243 
244 
245 template <int N>
246 template <class Archive>
247 inline
248 void
249 TableIndices<N>::serialize (Archive &ar, const unsigned int)
250 {
251  ar &indices;
252 }
253 
254 
261 template <int N>
262 std::ostream &
263 operator << (std::ostream &out,
264  const TableIndices<N> &indices)
265 {
266  out << '[';
267  for (unsigned int i=0; i<N; ++i)
268  {
269  out << indices[i];
270  if (i+1 != N)
271  out << ',';
272  }
273  out << ']';
274 
275  return out;
276 }
277 
278 
279 DEAL_II_NAMESPACE_CLOSE
280 
281 #endif
unsigned int operator[](const unsigned int i) const
static const unsigned int invalid_unsigned_int
Definition: types.h:170
unsigned int indices[N]
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void serialize(Archive &ar, const unsigned int version)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:313
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
bool operator==(const TableIndices< N > &other) const
bool operator!=(const TableIndices< N > &other) const