Reference documentation for deal.II version 9.0.0
component_mask.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 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_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 
81 {
82 public:
89  ComponentMask () = default;
90 
100  ComponentMask (const std::vector<bool> &component_mask);
101 
110  ComponentMask (const unsigned int n_components,
111  const bool initializer);
112 
116  void set (const unsigned int index, const bool value);
117 
126  unsigned int size () const;
127 
141  bool operator[] (const unsigned int component_index) const;
142 
152  bool
153  represents_n_components (const unsigned int n) const;
154 
168  unsigned int
169  n_selected_components (const unsigned int overall_number_of_components = numbers::invalid_unsigned_int) const;
170 
177  unsigned int
178  first_selected_component (const unsigned int overall_number_of_components = numbers::invalid_unsigned_int) const;
179 
185  bool
187 
192  ComponentMask operator | (const ComponentMask &mask) const;
193 
198  ComponentMask operator & (const ComponentMask &mask) const;
199 
203  bool operator== (const ComponentMask &mask) const;
204 
208  bool operator!= (const ComponentMask &mask) const;
209 
214  std::size_t
215  memory_consumption () const;
216 
221  "The number of selected components in a mask "
222  "must be greater than zero.");
223 
224 private:
228  std::vector<bool> component_mask;
229 
230  // make the output operator a friend so it can access
231  // the component_mask array
232  friend
233  std::ostream &operator << (std::ostream &out,
234  const ComponentMask &mask);
235 };
236 
237 
248 std::ostream &operator << (std::ostream &out,
249  const ComponentMask &mask);
250 
251 
252 // -------------------- inline functions ---------------------
253 
254 inline
255 ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
256  :
257  component_mask (component_mask)
258 {}
259 
260 
261 inline
263  const bool initializer)
264  :
265  component_mask (n_components, initializer)
266 {}
267 
268 
269 inline
270 unsigned int
272 {
273  return component_mask.size();
274 }
275 
276 
277 inline
278 void
279 ComponentMask::set(const unsigned int index, const bool value)
280 {
281  AssertIndexRange(index, component_mask.size());
282  component_mask[index] = value;
283 }
284 
285 
286 inline
287 bool
288 ComponentMask::operator [](const unsigned int component_index) const
289 {
290  // if the mask represents the all-component mask
291  // then always return true
292  if (component_mask.size() == 0)
293  return true;
294  else
295  {
296  // otherwise check the validity of the index and
297  // return whatever is appropriate
298  AssertIndexRange (component_index, component_mask.size());
299  return component_mask[component_index];
300  }
301 }
302 
303 
304 inline
305 bool
306 ComponentMask::represents_n_components(const unsigned int n) const
307 {
308  return ((component_mask.size() == 0)
309  ||
310  (component_mask.size() == n));
311 }
312 
313 
314 inline
315 unsigned int
316 ComponentMask::n_selected_components(const unsigned int n) const
317 {
318  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
319  AssertDimension (n, size());
320 
321  const unsigned int real_n = (n != numbers::invalid_unsigned_int
322  ?
323  n
324  :
325  size());
326  if (component_mask.size() == 0)
327  return real_n;
328  else
329  {
330  AssertDimension (real_n, component_mask.size());
331  unsigned int c = 0;
332  for (unsigned int i=0; i<component_mask.size(); ++i)
333  if (component_mask[i] == true)
334  ++c;
335  return c;
336  }
337 }
338 
339 
340 inline
341 unsigned int
342 ComponentMask::first_selected_component(const unsigned int n) const
343 {
344  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
345  AssertDimension (n, size());
346 
347  if (component_mask.size() == 0)
348  return 0;
349  else
350  {
351  for (unsigned int c=0; c<component_mask.size(); ++c)
352  if (component_mask[c] == true)
353  return c;
354 
355  Assert (false, ExcMessage ("No component is selected at all!"));
357  }
358 }
359 
360 
361 
362 inline
363 bool
365 {
366  return (component_mask.size() == 0);
367 }
368 
369 
370 
371 inline
374 {
375  // if one of the two masks denotes the all-component mask,
376  // then return the other one
377  if (component_mask.size() == 0)
378  return mask;
379  else if (mask.component_mask.size() == 0)
380  return *this;
381  else
382  {
383  // if both masks have individual entries set, form
384  // the combination of the two
385  AssertDimension(component_mask.size(), mask.component_mask.size());
386  std::vector<bool> new_mask (component_mask.size());
387  for (unsigned int i=0; i<component_mask.size(); ++i)
388  new_mask[i] = (component_mask[i] || mask.component_mask[i]);
389 
390  return new_mask;
391  }
392 }
393 
394 
395 inline
397 ComponentMask::operator & (const ComponentMask &mask) const
398 {
399  // if one of the two masks denotes the all-component mask,
400  // then return the other one
401  if (component_mask.size() == 0)
402  return mask;
403  else if (mask.component_mask.size() == 0)
404  return *this;
405  else
406  {
407  // if both masks have individual entries set, form
408  // the combination of the two
409  AssertDimension(component_mask.size(), mask.component_mask.size());
410  std::vector<bool> new_mask (component_mask.size());
411  for (unsigned int i=0; i<component_mask.size(); ++i)
412  new_mask[i] = (component_mask[i] && mask.component_mask[i]);
413 
414  return new_mask;
415  }
416 }
417 
418 
419 inline
420 bool
422 {
423  return component_mask == mask.component_mask;
424 }
425 
426 
427 inline
428 bool
430 {
431  return component_mask != mask.component_mask;
432 }
433 
434 
435 
436 DEAL_II_NAMESPACE_CLOSE
437 
438 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:173
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1248
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:1284
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:1142
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:335
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:163
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)
ComponentMask()=default
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)