Reference documentation for deal.II version 9.1.0-pre
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.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 
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/memory_consumption.h>
23 
24 #include <iosfwd>
25 #include <vector>
26 
27 DEAL_II_NAMESPACE_OPEN
28 
29 
30 
83 {
84 public:
91  ComponentMask() = default;
92 
102  ComponentMask(const std::vector<bool> &component_mask);
103 
112  ComponentMask(const unsigned int n_components, const bool initializer);
113 
117  void
118  set(const unsigned int index, const bool value);
119 
128  unsigned int
129  size() const;
130 
144  bool operator[](const unsigned int component_index) const;
145 
155  bool
156  represents_n_components(const unsigned int n) const;
157 
171  unsigned int
172  n_selected_components(const unsigned int overall_number_of_components =
174 
181  unsigned int
182  first_selected_component(const unsigned int overall_number_of_components =
184 
190  bool
192 
198  operator|(const ComponentMask &mask) const;
199 
204  ComponentMask operator&(const ComponentMask &mask) const;
205 
209  bool
210  operator==(const ComponentMask &mask) const;
211 
215  bool
216  operator!=(const ComponentMask &mask) const;
217 
222  std::size_t
223  memory_consumption() const;
224 
229  "The number of selected components in a mask "
230  "must be greater than zero.");
231 
232 private:
236  std::vector<bool> component_mask;
237 
238  // make the output operator a friend so it can access
239  // the component_mask array
240  friend std::ostream &
241  operator<<(std::ostream &out, const ComponentMask &mask);
242 };
243 
244 
255 std::ostream &
256 operator<<(std::ostream &out, const ComponentMask &mask);
257 
258 
259 // -------------------- inline functions ---------------------
260 
261 inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
262  : component_mask(component_mask)
263 {}
264 
265 
266 inline ComponentMask::ComponentMask(const unsigned int n_components,
267  const bool initializer)
268  : component_mask(n_components, initializer)
269 {}
270 
271 
272 inline unsigned int
274 {
275  return component_mask.size();
276 }
277 
278 
279 inline void
280 ComponentMask::set(const unsigned int index, const bool value)
281 {
282  AssertIndexRange(index, component_mask.size());
283  component_mask[index] = value;
284 }
285 
286 
287 inline bool ComponentMask::operator[](const unsigned int component_index) const
288 {
289  // if the mask represents the all-component mask
290  // then always return true
291  if (component_mask.size() == 0)
292  return true;
293  else
294  {
295  // otherwise check the validity of the index and
296  // return whatever is appropriate
297  AssertIndexRange(component_index, component_mask.size());
298  return component_mask[component_index];
299  }
300 }
301 
302 
303 inline bool
304 ComponentMask::represents_n_components(const unsigned int n) const
305 {
306  return ((component_mask.size() == 0) || (component_mask.size() == n));
307 }
308 
309 
310 inline unsigned int
311 ComponentMask::n_selected_components(const unsigned int n) const
312 {
313  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
314  AssertDimension(n, size());
315 
316  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
317  if (component_mask.size() == 0)
318  return real_n;
319  else
320  {
321  AssertDimension(real_n, component_mask.size());
322  unsigned int c = 0;
323  for (unsigned int i = 0; i < component_mask.size(); ++i)
324  if (component_mask[i] == true)
325  ++c;
326  return c;
327  }
328 }
329 
330 
331 inline unsigned int
332 ComponentMask::first_selected_component(const unsigned int n) const
333 {
334  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
335  AssertDimension(n, size());
336 
337  if (component_mask.size() == 0)
338  return 0;
339  else
340  {
341  for (unsigned int c = 0; c < component_mask.size(); ++c)
342  if (component_mask[c] == true)
343  return c;
344 
345  Assert(false, ExcMessage("No component is selected at all!"));
347  }
348 }
349 
350 
351 
352 inline bool
354 {
355  return (component_mask.size() == 0);
356 }
357 
358 
359 
360 inline ComponentMask
362 {
363  // if one of the two masks denotes the all-component mask,
364  // then return the other one
365  if (component_mask.size() == 0)
366  return mask;
367  else if (mask.component_mask.size() == 0)
368  return *this;
369  else
370  {
371  // if both masks have individual entries set, form
372  // the combination of the two
373  AssertDimension(component_mask.size(), mask.component_mask.size());
374  std::vector<bool> new_mask(component_mask.size());
375  for (unsigned int i = 0; i < component_mask.size(); ++i)
376  new_mask[i] = (component_mask[i] || mask.component_mask[i]);
377 
378  return new_mask;
379  }
380 }
381 
382 
384 {
385  // if one of the two masks denotes the all-component mask,
386  // then return the other one
387  if (component_mask.size() == 0)
388  return mask;
389  else if (mask.component_mask.size() == 0)
390  return *this;
391  else
392  {
393  // if both masks have individual entries set, form
394  // the combination of the two
395  AssertDimension(component_mask.size(), mask.component_mask.size());
396  std::vector<bool> new_mask(component_mask.size());
397  for (unsigned int i = 0; i < component_mask.size(); ++i)
398  new_mask[i] = (component_mask[i] && mask.component_mask[i]);
399 
400  return new_mask;
401  }
402 }
403 
404 
405 inline bool
407 {
408  return component_mask == mask.component_mask;
409 }
410 
411 
412 inline bool
414 {
415  return component_mask != mask.component_mask;
416 }
417 
418 
419 
420 DEAL_II_NAMESPACE_CLOSE
421 
422 #endif
ComponentMask operator&(const ComponentMask &mask) const
bool operator!=(const ComponentMask &mask) const
static const unsigned int invalid_unsigned_int
Definition: types.h:173
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1366
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
std::vector< bool > component_mask
#define AssertIndexRange(index, range)
Definition: exceptions.h:1407
void set(const unsigned int index, const bool value)
bool represents_n_components(const unsigned int n) const
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:397
unsigned int size() const
static::ExceptionBase & ExcNoComponentSelected()
bool represents_the_all_selected_mask() const
unsigned int first_selected_component(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
bool operator[](const unsigned int component_index) const
std::size_t memory_consumption() const
bool operator==(const ComponentMask &mask) const
ComponentMask operator|(const ComponentMask &mask) const
ComponentMask()=default
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)