Reference documentation for deal.II version 9.1.0-pre
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.md at
12 // the top level directory of deal.II.
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 
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 
73 class BlockMask
74 {
75 public:
82  BlockMask() = default;
83 
93  BlockMask(const std::vector<bool> &block_mask);
94 
103  BlockMask(const unsigned int n_blocks, const bool initializer);
104 
113  unsigned int
114  size() const;
115 
129  bool operator[](const unsigned int block_index) const;
130 
140  bool
141  represents_n_blocks(const unsigned int n) const;
142 
156  unsigned int
157  n_selected_blocks(const unsigned int overall_number_of_blocks =
159 
166  unsigned int
167  first_selected_block(const unsigned int overall_number_of_blocks =
169 
175  bool
177 
182  BlockMask
183  operator|(const BlockMask &mask) const;
184 
189  BlockMask operator&(const BlockMask &mask) const;
190 
194  bool
195  operator==(const BlockMask &mask) const;
196 
200  bool
201  operator!=(const BlockMask &mask) const;
202 
207  std::size_t
208  memory_consumption() const;
209 
210 private:
214  std::vector<bool> block_mask;
215 
216  // make the output operator a friend so it can access
217  // the block_mask array
218  friend std::ostream &
219  operator<<(std::ostream &out, const BlockMask &mask);
220 };
221 
222 
233 std::ostream &
234 operator<<(std::ostream &out, const BlockMask &mask);
235 
236 
237 // -------------------- inline functions ---------------------
238 
239 inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
240  : block_mask(block_mask)
241 {}
242 
243 
244 inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
245  : block_mask(n_blocks, initializer)
246 {}
247 
248 
249 inline unsigned int
251 {
252  return block_mask.size();
253 }
254 
255 
256 inline bool BlockMask::operator[](const unsigned int block_index) const
257 {
258  // if the mask represents the all-block mask
259  // then always return true
260  if (block_mask.size() == 0)
261  return true;
262  else
263  {
264  // otherwise check the validity of the index and
265  // return whatever is appropriate
266  Assert(block_index < block_mask.size(),
267  ExcIndexRange(block_index, 0, block_mask.size()));
268  return block_mask[block_index];
269  }
270 }
271 
272 
273 inline bool
274 BlockMask::represents_n_blocks(const unsigned int n) const
275 {
276  return ((block_mask.size() == 0) || (block_mask.size() == n));
277 }
278 
279 
280 inline unsigned int
281 BlockMask::n_selected_blocks(const unsigned int n) const
282 {
283  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
284  AssertDimension(n, size());
285 
286  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
287  if (block_mask.size() == 0)
288  return real_n;
289  else
290  {
291  AssertDimension(real_n, block_mask.size());
292  unsigned int c = 0;
293  for (unsigned int i = 0; i < block_mask.size(); ++i)
294  if (block_mask[i] == true)
295  ++c;
296  return c;
297  }
298 }
299 
300 
301 inline unsigned int
302 BlockMask::first_selected_block(const unsigned int n) const
303 {
304  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
305  AssertDimension(n, size());
306 
307  if (block_mask.size() == 0)
308  return 0;
309  else
310  {
311  for (unsigned int c = 0; c < block_mask.size(); ++c)
312  if (block_mask[c] == true)
313  return c;
314 
315  Assert(false, ExcMessage("No block is selected at all!"));
317  }
318 }
319 
320 
321 
322 inline bool
324 {
325  return (block_mask.size() == 0);
326 }
327 
328 
329 
330 inline BlockMask
331 BlockMask::operator|(const BlockMask &mask) const
332 {
333  // if one of the two masks denotes the all-block mask,
334  // then return the other one
335  if (block_mask.size() == 0)
336  return mask;
337  else if (mask.block_mask.size() == 0)
338  return *this;
339  else
340  {
341  // if both masks have individual entries set, form
342  // the combination of the two
343  AssertDimension(block_mask.size(), mask.block_mask.size());
344  std::vector<bool> new_mask(block_mask.size());
345  for (unsigned int i = 0; i < block_mask.size(); ++i)
346  new_mask[i] = (block_mask[i] || mask.block_mask[i]);
347 
348  return new_mask;
349  }
350 }
351 
352 
353 inline BlockMask BlockMask::operator&(const BlockMask &mask) const
354 {
355  // if one of the two masks denotes the all-block mask,
356  // then return the other one
357  if (block_mask.size() == 0)
358  return mask;
359  else if (mask.block_mask.size() == 0)
360  return *this;
361  else
362  {
363  // if both masks have individual entries set, form
364  // the combination of the two
365  AssertDimension(block_mask.size(), mask.block_mask.size());
366  std::vector<bool> new_mask(block_mask.size());
367  for (unsigned int i = 0; i < block_mask.size(); ++i)
368  new_mask[i] = (block_mask[i] && mask.block_mask[i]);
369 
370  return new_mask;
371  }
372 }
373 
374 
375 inline bool
377 {
378  return block_mask == mask.block_mask;
379 }
380 
381 
382 inline bool
384 {
385  return block_mask != mask.block_mask;
386 }
387 
388 
389 DEAL_II_NAMESPACE_CLOSE
390 
391 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:173
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1366
bool operator!=(const BlockMask &mask) const
Definition: block_mask.h:383
unsigned int size() const
Definition: block_mask.h:250
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
Definition: block_mask.cc:25
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:302
std::size_t memory_consumption() const
Definition: block_mask.cc:47
BlockMask()=default
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
bool operator==(const BlockMask &mask) const
Definition: block_mask.h:376
bool operator[](const unsigned int block_index) const
Definition: block_mask.h:256
bool represents_the_all_selected_mask() const
Definition: block_mask.h:323
BlockMask operator&(const BlockMask &mask) const
Definition: block_mask.h:353
std::vector< bool > block_mask
Definition: block_mask.h:214
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:281
bool represents_n_blocks(const unsigned int n) const
Definition: block_mask.h:274
BlockMask operator|(const BlockMask &mask) const
Definition: block_mask.h:331