Reference documentation for deal.II version Git 01d13113a1 2020-07-10 09:14:01 +0200
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
component_mask.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2020 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_fe_component_mask_h
17 #define dealii_fe_component_mask_h
18 
19 #include <deal.II/base/config.h>
20 
23 
24 #include <algorithm>
25 #include <iosfwd>
26 #include <vector>
27 
29 
30 
31 
82 {
83 public:
90  ComponentMask() = default;
91 
101  ComponentMask(const std::vector<bool> &component_mask);
102 
111  ComponentMask(const unsigned int n_components, const bool initializer);
112 
116  void
117  set(const unsigned int index, const bool value);
118 
127  unsigned int
128  size() const;
129 
143  bool operator[](const unsigned int component_index) const;
144 
154  bool
155  represents_n_components(const unsigned int n) const;
156 
170  unsigned int
171  n_selected_components(const unsigned int overall_number_of_components =
173 
180  unsigned int
181  first_selected_component(const unsigned int overall_number_of_components =
183 
189  bool
191 
197  operator|(const ComponentMask &mask) const;
198 
203  ComponentMask operator&(const ComponentMask &mask) const;
204 
208  bool
209  operator==(const ComponentMask &mask) const;
210 
214  bool
215  operator!=(const ComponentMask &mask) const;
216 
221  std::size_t
222  memory_consumption() const;
223 
228  "The number of selected components in a mask "
229  "must be greater than zero.");
230 
231 private:
235  std::vector<bool> component_mask;
236 
237  // make the output operator a friend so it can access
238  // the component_mask array
239  friend std::ostream &
240  operator<<(std::ostream &out, const ComponentMask &mask);
241 };
242 
243 
254 std::ostream &
255 operator<<(std::ostream &out, const ComponentMask &mask);
256 
257 #ifndef DOXYGEN
258 // -------------------- inline functions ---------------------
259 
260 inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
261  : component_mask(component_mask)
262 {}
263 
264 
265 inline ComponentMask::ComponentMask(const unsigned int n_components,
266  const bool initializer)
267  : component_mask(n_components, initializer)
268 {}
269 
270 
271 inline unsigned int
272 ComponentMask::size() const
273 {
274  return component_mask.size();
275 }
276 
277 
278 inline 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 bool ComponentMask::operator[](const unsigned int component_index) const
287 {
288  // if the mask represents the all-component mask
289  // then always return true
290  if (component_mask.size() == 0)
291  return true;
292  else
293  {
294  // otherwise check the validity of the index and
295  // return whatever is appropriate
296  AssertIndexRange(component_index, component_mask.size());
297  return component_mask[component_index];
298  }
299 }
300 
301 
302 inline bool
303 ComponentMask::represents_n_components(const unsigned int n) const
304 {
305  return ((component_mask.size() == 0) || (component_mask.size() == n));
306 }
307 
308 
309 inline unsigned int
310 ComponentMask::n_selected_components(const unsigned int n) const
311 {
312  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
313  AssertDimension(n, size());
314 
315  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
316  if (component_mask.size() == 0)
317  return real_n;
318  else
319  {
320  AssertDimension(real_n, component_mask.size());
321  return std::count_if(component_mask.begin(),
322  component_mask.end(),
323  [](const bool selected) { return selected; });
324  }
325 }
326 
327 
328 inline unsigned int
329 ComponentMask::first_selected_component(const unsigned int n) const
330 {
331  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
332  AssertDimension(n, size());
333 
334  if (component_mask.size() == 0)
335  return 0;
336  else
337  {
338  for (unsigned int c = 0; c < component_mask.size(); ++c)
339  if (component_mask[c] == true)
340  return c;
341 
342  Assert(false, ExcMessage("No component is selected at all!"));
344  }
345 }
346 
347 
348 
349 inline bool
351 {
352  return (component_mask.size() == 0);
353 }
354 
355 
356 
357 inline ComponentMask
358 ComponentMask::operator|(const ComponentMask &mask) const
359 {
360  // if one of the two masks denotes the all-component mask,
361  // then return the other one
362  if (component_mask.size() == 0)
363  return mask;
364  else if (mask.component_mask.size() == 0)
365  return *this;
366  else
367  {
368  // if both masks have individual entries set, form
369  // the combination of the two
370  AssertDimension(component_mask.size(), mask.component_mask.size());
371  std::vector<bool> new_mask(component_mask.size());
372  for (unsigned int i = 0; i < component_mask.size(); ++i)
373  new_mask[i] = (component_mask[i] || mask.component_mask[i]);
374 
375  return new_mask;
376  }
377 }
378 
379 
380 inline ComponentMask ComponentMask::operator&(const ComponentMask &mask) const
381 {
382  // if one of the two masks denotes the all-component mask,
383  // then return the other one
384  if (component_mask.size() == 0)
385  return mask;
386  else if (mask.component_mask.size() == 0)
387  return *this;
388  else
389  {
390  // if both masks have individual entries set, form
391  // the combination of the two
392  AssertDimension(component_mask.size(), mask.component_mask.size());
393  std::vector<bool> new_mask(component_mask.size());
394  for (unsigned int i = 0; i < component_mask.size(); ++i)
395  new_mask[i] = (component_mask[i] && mask.component_mask[i]);
396 
397  return new_mask;
398  }
399 }
400 
401 
402 inline bool
403 ComponentMask::operator==(const ComponentMask &mask) const
404 {
405  return component_mask == mask.component_mask;
406 }
407 
408 
409 inline bool
410 ComponentMask::operator!=(const ComponentMask &mask) const
411 {
412  return component_mask != mask.component_mask;
413 }
414 #endif // DOXYGEN
415 
416 
418 
419 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:191
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1560
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:1628
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:1403
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:493
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:363
static ::ExceptionBase & ExcNoComponentSelected()
unsigned int size() const
std::size_t memory_consumption() const
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:362
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()=default
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)
ComponentMask operator &(const ComponentMask &mask) const