Reference documentation for deal.II version 8.5.1
block_mask.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2015 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_block_mask_h
17 #define dealii__fe_block_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 
71 class BlockMask
72 {
73 public:
80  BlockMask ();
81 
91  BlockMask (const std::vector<bool> &block_mask);
92 
101  BlockMask (const unsigned int n_blocks,
102  const bool initializer);
103 
112  unsigned int size () const;
113 
127  bool operator[] (const unsigned int block_index) const;
128 
138  bool
139  represents_n_blocks (const unsigned int n) const;
140 
154  unsigned int
155  n_selected_blocks (const unsigned int overall_number_of_blocks = numbers::invalid_unsigned_int) const;
156 
163  unsigned int
164  first_selected_block (const unsigned int overall_number_of_blocks = numbers::invalid_unsigned_int) const;
165 
171  bool
173 
178  BlockMask operator | (const BlockMask &mask) const;
179 
184  BlockMask operator & (const BlockMask &mask) const;
185 
189  bool operator== (const BlockMask &mask) const;
190 
194  bool operator!= (const BlockMask &mask) const;
195 
200  std::size_t
201  memory_consumption () const;
202 
203 private:
207  std::vector<bool> block_mask;
208 
209  // make the output operator a friend so it can access
210  // the block_mask array
211  friend
212  std::ostream &operator << (std::ostream &out,
213  const BlockMask &mask);
214 };
215 
216 
227 std::ostream &operator << (std::ostream &out,
228  const BlockMask &mask);
229 
230 
231 // -------------------- inline functions ---------------------
232 
233 inline
235 {}
236 
237 
238 inline
239 BlockMask::BlockMask(const std::vector<bool> &block_mask)
240  :
241  block_mask (block_mask)
242 {}
243 
244 
245 inline
246 BlockMask::BlockMask(const unsigned int n_blocks,
247  const bool initializer)
248  :
249  block_mask (n_blocks, initializer)
250 {}
251 
252 
253 inline
254 unsigned int
256 {
257  return block_mask.size();
258 }
259 
260 
261 inline
262 bool
263 BlockMask::operator [](const unsigned int block_index) const
264 {
265  // if the mask represents the all-block mask
266  // then always return true
267  if (block_mask.size() == 0)
268  return true;
269  else
270  {
271  // otherwise check the validity of the index and
272  // return whatever is appropriate
273  Assert (block_index < block_mask.size(),
274  ExcIndexRange (block_index, 0, block_mask.size()));
275  return block_mask[block_index];
276  }
277 }
278 
279 
280 inline
281 bool
282 BlockMask::represents_n_blocks(const unsigned int n) const
283 {
284  return ((block_mask.size() == 0)
285  ||
286  (block_mask.size() == n));
287 }
288 
289 
290 inline
291 unsigned int
292 BlockMask::n_selected_blocks(const unsigned int n) const
293 {
294  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
295  AssertDimension (n, size());
296 
297  const unsigned int real_n = (n != numbers::invalid_unsigned_int
298  ?
299  n
300  :
301  size());
302  if (block_mask.size() == 0)
303  return real_n;
304  else
305  {
306  AssertDimension (real_n, block_mask.size());
307  unsigned int c = 0;
308  for (unsigned int i=0; i<block_mask.size(); ++i)
309  if (block_mask[i] == true)
310  ++c;
311  return c;
312  }
313 }
314 
315 
316 inline
317 unsigned int
318 BlockMask::first_selected_block(const unsigned int n) const
319 {
320  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
321  AssertDimension (n, size());
322 
323  if (block_mask.size() == 0)
324  return 0;
325  else
326  {
327  for (unsigned int c=0; c<block_mask.size(); ++c)
328  if (block_mask[c] == true)
329  return c;
330 
331  Assert (false, ExcMessage ("No block is selected at all!"));
333  }
334 }
335 
336 
337 
338 inline
339 bool
341 {
342  return (block_mask.size() == 0);
343 }
344 
345 
346 
347 inline
348 BlockMask
350 {
351  // if one of the two masks denotes the all-block mask,
352  // then return the other one
353  if (block_mask.size() == 0)
354  return mask;
355  else if (mask.block_mask.size() == 0)
356  return *this;
357  else
358  {
359  // if both masks have individual entries set, form
360  // the combination of the two
361  AssertDimension(block_mask.size(), mask.block_mask.size());
362  std::vector<bool> new_mask (block_mask.size());
363  for (unsigned int i=0; i<block_mask.size(); ++i)
364  new_mask[i] = (block_mask[i] || mask.block_mask[i]);
365 
366  return new_mask;
367  }
368 }
369 
370 
371 inline
372 BlockMask
373 BlockMask::operator & (const BlockMask &mask) const
374 {
375  // if one of the two masks denotes the all-block mask,
376  // then return the other one
377  if (block_mask.size() == 0)
378  return mask;
379  else if (mask.block_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(block_mask.size(), mask.block_mask.size());
386  std::vector<bool> new_mask (block_mask.size());
387  for (unsigned int i=0; i<block_mask.size(); ++i)
388  new_mask[i] = (block_mask[i] && mask.block_mask[i]);
389 
390  return new_mask;
391  }
392 }
393 
394 
395 inline
396 bool
398 {
399  return block_mask == mask.block_mask;
400 }
401 
402 
403 inline
404 bool
406 {
407  return block_mask != mask.block_mask;
408 }
409 
410 
411 DEAL_II_NAMESPACE_CLOSE
412 
413 #endif
bool represents_the_all_selected_mask() const
Definition: block_mask.h:340
static const unsigned int invalid_unsigned_int
Definition: types.h:170
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1146
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
Definition: block_mask.cc:24
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
BlockMask operator&(const BlockMask &mask) const
bool operator==(const BlockMask &mask) const
Definition: block_mask.h:397
static ::ExceptionBase & ExcMessage(std::string arg1)
BlockMask operator|(const BlockMask &mask) const
Definition: block_mask.h:349
#define Assert(cond, exc)
Definition: exceptions.h:313
bool represents_n_blocks(const unsigned int n) const
Definition: block_mask.h:282
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
std::vector< bool > block_mask
Definition: block_mask.h:207
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:318
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:292
bool operator[](const unsigned int block_index) const
Definition: block_mask.h:263
unsigned int size() const
Definition: block_mask.h:255
bool operator!=(const BlockMask &mask) const
Definition: block_mask.h:405
std::size_t memory_consumption() const
Definition: block_mask.cc:47