Reference documentation for deal.II version 9.0.0
block_mask.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2017 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 () = default;
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
234 BlockMask::BlockMask(const std::vector<bool> &block_mask)
235  :
236  block_mask (block_mask)
237 {}
238 
239 
240 inline
241 BlockMask::BlockMask(const unsigned int n_blocks,
242  const bool initializer)
243  :
244  block_mask (n_blocks, initializer)
245 {}
246 
247 
248 inline
249 unsigned int
251 {
252  return block_mask.size();
253 }
254 
255 
256 inline
257 bool
258 BlockMask::operator [](const unsigned int block_index) const
259 {
260  // if the mask represents the all-block mask
261  // then always return true
262  if (block_mask.size() == 0)
263  return true;
264  else
265  {
266  // otherwise check the validity of the index and
267  // return whatever is appropriate
268  Assert (block_index < block_mask.size(),
269  ExcIndexRange (block_index, 0, block_mask.size()));
270  return block_mask[block_index];
271  }
272 }
273 
274 
275 inline
276 bool
277 BlockMask::represents_n_blocks(const unsigned int n) const
278 {
279  return ((block_mask.size() == 0)
280  ||
281  (block_mask.size() == n));
282 }
283 
284 
285 inline
286 unsigned int
287 BlockMask::n_selected_blocks(const unsigned int n) const
288 {
289  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
290  AssertDimension (n, size());
291 
292  const unsigned int real_n = (n != numbers::invalid_unsigned_int
293  ?
294  n
295  :
296  size());
297  if (block_mask.size() == 0)
298  return real_n;
299  else
300  {
301  AssertDimension (real_n, block_mask.size());
302  unsigned int c = 0;
303  for (unsigned int i=0; i<block_mask.size(); ++i)
304  if (block_mask[i] == true)
305  ++c;
306  return c;
307  }
308 }
309 
310 
311 inline
312 unsigned int
313 BlockMask::first_selected_block(const unsigned int n) const
314 {
315  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
316  AssertDimension (n, size());
317 
318  if (block_mask.size() == 0)
319  return 0;
320  else
321  {
322  for (unsigned int c=0; c<block_mask.size(); ++c)
323  if (block_mask[c] == true)
324  return c;
325 
326  Assert (false, ExcMessage ("No block is selected at all!"));
328  }
329 }
330 
331 
332 
333 inline
334 bool
336 {
337  return (block_mask.size() == 0);
338 }
339 
340 
341 
342 inline
343 BlockMask
345 {
346  // if one of the two masks denotes the all-block mask,
347  // then return the other one
348  if (block_mask.size() == 0)
349  return mask;
350  else if (mask.block_mask.size() == 0)
351  return *this;
352  else
353  {
354  // if both masks have individual entries set, form
355  // the combination of the two
356  AssertDimension(block_mask.size(), mask.block_mask.size());
357  std::vector<bool> new_mask (block_mask.size());
358  for (unsigned int i=0; i<block_mask.size(); ++i)
359  new_mask[i] = (block_mask[i] || mask.block_mask[i]);
360 
361  return new_mask;
362  }
363 }
364 
365 
366 inline
367 BlockMask
368 BlockMask::operator & (const BlockMask &mask) const
369 {
370  // if one of the two masks denotes the all-block mask,
371  // then return the other one
372  if (block_mask.size() == 0)
373  return mask;
374  else if (mask.block_mask.size() == 0)
375  return *this;
376  else
377  {
378  // if both masks have individual entries set, form
379  // the combination of the two
380  AssertDimension(block_mask.size(), mask.block_mask.size());
381  std::vector<bool> new_mask (block_mask.size());
382  for (unsigned int i=0; i<block_mask.size(); ++i)
383  new_mask[i] = (block_mask[i] && mask.block_mask[i]);
384 
385  return new_mask;
386  }
387 }
388 
389 
390 inline
391 bool
393 {
394  return block_mask == mask.block_mask;
395 }
396 
397 
398 inline
399 bool
401 {
402  return block_mask != mask.block_mask;
403 }
404 
405 
406 DEAL_II_NAMESPACE_CLOSE
407 
408 #endif
bool represents_the_all_selected_mask() const
Definition: block_mask.h:335
static const unsigned int invalid_unsigned_int
Definition: types.h:173
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1248
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:392
BlockMask()=default
static ::ExceptionBase & ExcMessage(std::string arg1)
BlockMask operator|(const BlockMask &mask) const
Definition: block_mask.h:344
#define Assert(cond, exc)
Definition: exceptions.h:1142
bool represents_n_blocks(const unsigned int n) const
Definition: block_mask.h:277
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:163
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:313
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:287
bool operator[](const unsigned int block_index) const
Definition: block_mask.h:258
unsigned int size() const
Definition: block_mask.h:250
bool operator!=(const BlockMask &mask) const
Definition: block_mask.h:400
std::size_t memory_consumption() const
Definition: block_mask.cc:47