Reference documentation for deal.II version 8.5.1
component_mask.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2015 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__fe_component_mask_h
17 #define dealii__fe_component_mask_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/memory_consumption.h>
22 
23 #include <vector>
24 #include <iosfwd>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 
29 
68 {
69 public:
76  ComponentMask ();
77 
87  ComponentMask (const std::vector<bool> &component_mask);
88 
97  ComponentMask (const unsigned int n_components,
98  const bool initializer);
99 
103  void set (const unsigned int index, const bool value);
104 
113  unsigned int size () const;
114 
128  bool operator[] (const unsigned int component_index) const;
129 
139  bool
140  represents_n_components (const unsigned int n) const;
141 
155  unsigned int
156  n_selected_components (const unsigned int overall_number_of_components = numbers::invalid_unsigned_int) const;
157 
164  unsigned int
165  first_selected_component (const unsigned int overall_number_of_components = numbers::invalid_unsigned_int) const;
166 
172  bool
174 
179  ComponentMask operator | (const ComponentMask &mask) const;
180 
185  ComponentMask operator & (const ComponentMask &mask) const;
186 
190  bool operator== (const ComponentMask &mask) const;
191 
195  bool operator!= (const ComponentMask &mask) const;
196 
201  std::size_t
202  memory_consumption () const;
203 
208  "The number of selected components in a mask "
209  "must be greater than zero.");
210 
211 private:
215  std::vector<bool> component_mask;
216 
217  // make the output operator a friend so it can access
218  // the component_mask array
219  friend
220  std::ostream &operator << (std::ostream &out,
221  const ComponentMask &mask);
222 };
223 
224 
235 std::ostream &operator << (std::ostream &out,
236  const ComponentMask &mask);
237 
238 
239 // -------------------- inline functions ---------------------
240 
241 inline
243 {}
244 
245 
246 inline
247 ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
248  :
249  component_mask (component_mask)
250 {}
251 
252 
253 inline
255  const bool initializer)
256  :
257  component_mask (n_components, initializer)
258 {}
259 
260 
261 inline
262 unsigned int
264 {
265  return component_mask.size();
266 }
267 
268 
269 inline
270 void
271 ComponentMask::set(const unsigned int index, const bool value)
272 {
273  AssertIndexRange(index, component_mask.size());
274  component_mask[index] = value;
275 }
276 
277 
278 inline
279 bool
280 ComponentMask::operator [](const unsigned int component_index) const
281 {
282  // if the mask represents the all-component mask
283  // then always return true
284  if (component_mask.size() == 0)
285  return true;
286  else
287  {
288  // otherwise check the validity of the index and
289  // return whatever is appropriate
290  AssertIndexRange (component_index, component_mask.size());
291  return component_mask[component_index];
292  }
293 }
294 
295 
296 inline
297 bool
298 ComponentMask::represents_n_components(const unsigned int n) const
299 {
300  return ((component_mask.size() == 0)
301  ||
302  (component_mask.size() == n));
303 }
304 
305 
306 inline
307 unsigned int
308 ComponentMask::n_selected_components(const unsigned int n) const
309 {
310  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
311  AssertDimension (n, size());
312 
313  const unsigned int real_n = (n != numbers::invalid_unsigned_int
314  ?
315  n
316  :
317  size());
318  if (component_mask.size() == 0)
319  return real_n;
320  else
321  {
322  AssertDimension (real_n, component_mask.size());
323  unsigned int c = 0;
324  for (unsigned int i=0; i<component_mask.size(); ++i)
325  if (component_mask[i] == true)
326  ++c;
327  return c;
328  }
329 }
330 
331 
332 inline
333 unsigned int
334 ComponentMask::first_selected_component(const unsigned int n) const
335 {
336  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
337  AssertDimension (n, size());
338 
339  if (component_mask.size() == 0)
340  return 0;
341  else
342  {
343  for (unsigned int c=0; c<component_mask.size(); ++c)
344  if (component_mask[c] == true)
345  return c;
346 
347  Assert (false, ExcMessage ("No component is selected at all!"));
349  }
350 }
351 
352 
353 
354 inline
355 bool
357 {
358  return (component_mask.size() == 0);
359 }
360 
361 
362 
363 inline
366 {
367  // if one of the two masks denotes the all-component mask,
368  // then return the other one
369  if (component_mask.size() == 0)
370  return mask;
371  else if (mask.component_mask.size() == 0)
372  return *this;
373  else
374  {
375  // if both masks have individual entries set, form
376  // the combination of the two
377  AssertDimension(component_mask.size(), mask.component_mask.size());
378  std::vector<bool> new_mask (component_mask.size());
379  for (unsigned int i=0; i<component_mask.size(); ++i)
380  new_mask[i] = (component_mask[i] || mask.component_mask[i]);
381 
382  return new_mask;
383  }
384 }
385 
386 
387 inline
389 ComponentMask::operator & (const ComponentMask &mask) const
390 {
391  // if one of the two masks denotes the all-component mask,
392  // then return the other one
393  if (component_mask.size() == 0)
394  return mask;
395  else if (mask.component_mask.size() == 0)
396  return *this;
397  else
398  {
399  // if both masks have individual entries set, form
400  // the combination of the two
401  AssertDimension(component_mask.size(), mask.component_mask.size());
402  std::vector<bool> new_mask (component_mask.size());
403  for (unsigned int i=0; i<component_mask.size(); ++i)
404  new_mask[i] = (component_mask[i] && mask.component_mask[i]);
405 
406  return new_mask;
407  }
408 }
409 
410 
411 inline
412 bool
414 {
415  return component_mask == mask.component_mask;
416 }
417 
418 
419 inline
420 bool
422 {
423  return component_mask != mask.component_mask;
424 }
425 
426 
427 
428 DEAL_II_NAMESPACE_CLOSE
429 
430 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:170
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1146
bool operator==(const ComponentMask &mask) const
bool operator[](const unsigned int component_index) const
bool operator!=(const ComponentMask &mask) const
std::vector< bool > component_mask
#define AssertIndexRange(index, range)
Definition: exceptions.h:1170
ComponentMask operator|(const ComponentMask &mask) const
bool represents_n_components(const unsigned int n) const
bool represents_the_all_selected_mask() const
void set(const unsigned int index, const bool value)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:313
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:553
static ::ExceptionBase & ExcNoComponentSelected()
unsigned int size() const
std::size_t memory_consumption() const
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
unsigned int first_selected_component(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
ComponentMask operator&(const ComponentMask &mask) const
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)