Reference documentation for deal.II version 8.1.0
component_mask.h
1 // ---------------------------------------------------------------------
2 // @f$Id: component_mask.h 31527 2013-11-03 09:58:45Z maier @f$
3 //
4 // Copyright (C) 2009 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__fe_component_mask_h
18 #define __deal2__fe_component_mask_h
19 
20 #include <deal.II/base/config.h>
22 #include <deal.II/base/memory_consumption.h>
23 
24 #include <vector>
25 #include <iosfwd>
26 
28 
29 
30 
67 {
68 public:
76  ComponentMask ();
77 
88  ComponentMask (const std::vector<bool> &component_mask);
89 
98  ComponentMask (const unsigned int n_components,
99  const bool initializer);
100 
110  unsigned int size () const;
111 
127  bool operator[] (const unsigned int component_index) const;
128 
140  bool
141  represents_n_components (const unsigned int n) const;
142 
158  unsigned int
159  n_selected_components (const unsigned int overall_number_of_components = numbers::invalid_unsigned_int) const;
160 
168  unsigned int
169  first_selected_component (const unsigned int overall_number_of_components = numbers::invalid_unsigned_int) const;
170 
177  bool
179 
185  ComponentMask operator | (const ComponentMask &mask) const;
186 
192  ComponentMask operator & (const ComponentMask &mask) const;
193 
197  bool operator== (const ComponentMask &mask) const;
198 
202  bool operator!= (const ComponentMask &mask) const;
203 
209  std::size_t
210  memory_consumption () const;
211 
215  DeclException0 (ExcNoComponentSelected);
216 
217 private:
221  std::vector<bool> component_mask;
222 
223  // make the output operator a friend so it can access
224  // the component_mask array
225  friend
226  std::ostream &operator << (std::ostream &out,
227  const ComponentMask &mask);
228 };
229 
230 
243 std::ostream &operator << (std::ostream &out,
244  const ComponentMask &mask);
245 
246 
247 // -------------------- inline functions ---------------------
248 
249 inline
251 {}
252 
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 bool
279 ComponentMask::operator [](const unsigned int component_index) const
280 {
281  // if the mask represents the all-component mask
282  // then always return true
283  if (component_mask.size() == 0)
284  return true;
285  else
286  {
287  // otherwise check the validity of the index and
288  // return whatever is appropriate
289  Assert (component_index < component_mask.size(),
290  ExcIndexRange (component_index, 0, 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
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
ComponentMask operator&(const ComponentMask &mask) const
bool operator!=(const ComponentMask &mask) const
static const unsigned int invalid_unsigned_int
Definition: types.h:191
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:858
DeclException0(ExcNoComponentSelected)
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
std::vector< bool > component_mask
::ExceptionBase & ExcMessage(std::string arg1)
bool represents_n_components(const unsigned int n) const
#define Assert(cond, exc)
Definition: exceptions.h:299
unsigned int size() const
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
bool represents_the_all_selected_mask() const
unsigned int first_selected_component(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
OS & operator<<(OS &o, const Event &e)
Definition: event.h:304
bool operator[](const unsigned int component_index) const
std::size_t memory_consumption() const
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
bool operator==(const ComponentMask &mask) const
ComponentMask operator|(const ComponentMask &mask) const
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)