Reference documentation for deal.II version 8.1.0
block_mask.h
1 // ---------------------------------------------------------------------
2 // @f$Id: block_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_block_mask_h
18 #define __deal2__fe_block_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 
73 class BlockMask
74 {
75 public:
83  BlockMask ();
84 
95  BlockMask (const std::vector<bool> &block_mask);
96 
105  BlockMask (const unsigned int n_blocks,
106  const bool initializer);
107 
117  unsigned int size () const;
118 
134  bool operator[] (const unsigned int block_index) const;
135 
147  bool
148  represents_n_blocks (const unsigned int n) const;
149 
165  unsigned int
166  n_selected_blocks (const unsigned int overall_number_of_blocks = numbers::invalid_unsigned_int) const;
167 
175  unsigned int
176  first_selected_block (const unsigned int overall_number_of_blocks = numbers::invalid_unsigned_int) const;
177 
184  bool
186 
192  BlockMask operator | (const BlockMask &mask) const;
193 
199  BlockMask operator & (const BlockMask &mask) const;
200 
204  bool operator== (const BlockMask &mask) const;
205 
209  bool operator!= (const BlockMask &mask) const;
210 
216  std::size_t
217  memory_consumption () const;
218 
219 private:
223  std::vector<bool> block_mask;
224 
225  // make the output operator a friend so it can access
226  // the block_mask array
227  friend
228  std::ostream &operator << (std::ostream &out,
229  const BlockMask &mask);
230 };
231 
232 
245 std::ostream &operator << (std::ostream &out,
246  const BlockMask &mask);
247 
248 
249 // -------------------- inline functions ---------------------
250 
251 inline
253 {}
254 
255 
256 inline
257 BlockMask::BlockMask(const std::vector<bool> &block_mask)
258  :
259  block_mask (block_mask)
260 {}
261 
262 
263 inline
264 BlockMask::BlockMask(const unsigned int n_blocks,
265  const bool initializer)
266  :
267  block_mask (n_blocks, initializer)
268 {}
269 
270 
271 inline
272 unsigned int
274 {
275  return block_mask.size();
276 }
277 
278 
279 inline
280 bool
281 BlockMask::operator [](const unsigned int block_index) const
282 {
283  // if the mask represents the all-block mask
284  // then always return true
285  if (block_mask.size() == 0)
286  return true;
287  else
288  {
289  // otherwise check the validity of the index and
290  // return whatever is appropriate
291  Assert (block_index < block_mask.size(),
292  ExcIndexRange (block_index, 0, block_mask.size()));
293  return block_mask[block_index];
294  }
295 }
296 
297 
298 inline
299 bool
300 BlockMask::represents_n_blocks(const unsigned int n) const
301 {
302  return ((block_mask.size() == 0)
303  ||
304  (block_mask.size() == n));
305 }
306 
307 
308 inline
309 unsigned int
310 BlockMask::n_selected_blocks(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
316  ?
317  n
318  :
319  size());
320  if (block_mask.size() == 0)
321  return real_n;
322  else
323  {
324  AssertDimension (real_n, block_mask.size());
325  unsigned int c = 0;
326  for (unsigned int i=0; i<block_mask.size(); ++i)
327  if (block_mask[i] == true)
328  ++c;
329  return c;
330  }
331 }
332 
333 
334 inline
335 unsigned int
336 BlockMask::first_selected_block(const unsigned int n) const
337 {
338  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
339  AssertDimension (n, size());
340 
341  if (block_mask.size() == 0)
342  return 0;
343  else
344  {
345  for (unsigned int c=0; c<block_mask.size(); ++c)
346  if (block_mask[c] == true)
347  return c;
348 
349  Assert (false, ExcMessage ("No block is selected at all!"));
351  }
352 }
353 
354 
355 
356 inline
357 bool
359 {
360  return (block_mask.size() == 0);
361 }
362 
363 
364 
365 inline
366 BlockMask
368 {
369  // if one of the two masks denotes the all-block mask,
370  // then return the other one
371  if (block_mask.size() == 0)
372  return mask;
373  else if (mask.block_mask.size() == 0)
374  return *this;
375  else
376  {
377  // if both masks have individual entries set, form
378  // the combination of the two
379  AssertDimension(block_mask.size(), mask.block_mask.size());
380  std::vector<bool> new_mask (block_mask.size());
381  for (unsigned int i=0; i<block_mask.size(); ++i)
382  new_mask[i] = (block_mask[i] || mask.block_mask[i]);
383 
384  return new_mask;
385  }
386 }
387 
388 
389 inline
390 BlockMask
392 {
393  // if one of the two masks denotes the all-block mask,
394  // then return the other one
395  if (block_mask.size() == 0)
396  return mask;
397  else if (mask.block_mask.size() == 0)
398  return *this;
399  else
400  {
401  // if both masks have individual entries set, form
402  // the combination of the two
403  AssertDimension(block_mask.size(), mask.block_mask.size());
404  std::vector<bool> new_mask (block_mask.size());
405  for (unsigned int i=0; i<block_mask.size(); ++i)
406  new_mask[i] = (block_mask[i] && mask.block_mask[i]);
407 
408  return new_mask;
409  }
410 }
411 
412 
413 inline
414 bool
416 {
417  return block_mask == mask.block_mask;
418 }
419 
420 
421 inline
422 bool
424 {
425  return block_mask != mask.block_mask;
426 }
427 
428 
429 DEAL_II_NAMESPACE_CLOSE
430 
431 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:191
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:858
bool operator!=(const BlockMask &mask) const
Definition: block_mask.h:423
::ExceptionBase & ExcMessage(std::string arg1)
unsigned int size() const
Definition: block_mask.h:273
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:336
std::size_t memory_consumption() const
#define Assert(cond, exc)
Definition: exceptions.h:299
bool operator==(const BlockMask &mask) const
Definition: block_mask.h:415
bool operator[](const unsigned int block_index) const
Definition: block_mask.h:281
bool represents_the_all_selected_mask() const
Definition: block_mask.h:358
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::ostream & operator<<(std::ostream &out, const SymmetricTensor< 2, dim, Number > &t)
BlockMask operator&(const BlockMask &mask) const
Definition: block_mask.h:391
std::vector< bool > block_mask
Definition: block_mask.h:223
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:310
bool represents_n_blocks(const unsigned int n) const
Definition: block_mask.h:300
BlockMask operator|(const BlockMask &mask) const
Definition: block_mask.h:367