deal.II version GIT relicensing-6809-ge913b9bb34 2026-09-25 17:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
block_mask.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2012 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_fe_block_mask_h
14#define dealii_fe_block_mask_h
15
16#include <deal.II/base/config.h>
17
20#include <deal.II/base/types.h>
21
22#include <algorithm>
23#include <iosfwd>
24#include <vector>
25
27
28
29
71{
72public:
79 BlockMask() = default;
80
84 template <typename = void>
86 "Implicit conversions from std::vector<bool> to BlockMask are deprecated!")
87 BlockMask(const std::vector<bool> &block_mask);
88
98 explicit BlockMask(const std::vector<bool> &block_mask);
99
108 BlockMask(const unsigned int n_blocks, const bool initializer);
109
118 unsigned int
119 size() const;
120
134 bool
135 operator[](const unsigned int block_index) const;
136
146 bool
147 represents_n_blocks(const unsigned int n) const;
148
162 unsigned int
163 n_selected_blocks(const unsigned int overall_number_of_blocks =
164 numbers::invalid_unsigned_int) const;
165
172 unsigned int
173 first_selected_block(const unsigned int overall_number_of_blocks =
174 numbers::invalid_unsigned_int) const;
175
181 bool
183
189 operator|(const BlockMask &mask) const;
190
196 operator&(const BlockMask &mask) const;
197
201 bool
202 operator==(const BlockMask &mask) const;
203
207 bool
208 operator!=(const BlockMask &mask) const;
209
214 std::size_t
215 memory_consumption() const;
216
217private:
221 std::vector<bool> block_mask;
222
223 // make the output operator a friend so it can access
224 // the block_mask array
225 friend std::ostream &
226 operator<<(std::ostream &out, const BlockMask &mask);
227};
228
229
240std::ostream &
241operator<<(std::ostream &out, const BlockMask &mask);
242
243
244#ifndef DOXYGEN
245// -------------------- inline functions ---------------------
246
247template <typename>
248inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
250{}
251
252
253inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
254 : block_mask(block_mask)
255{}
256
257
258inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
259 : block_mask(n_blocks, initializer)
260{}
261
262
263inline unsigned int
264BlockMask::size() const
265{
266 return block_mask.size();
267}
268
269
270inline bool
271BlockMask::operator[](const unsigned int block_index) const
272{
273 // if the mask represents the all-block mask
274 // then always return true
275 if (block_mask.empty())
276 return true;
277 else
278 {
279 // otherwise check the validity of the index and
280 // return whatever is appropriate
281 AssertIndexRange(block_index, block_mask.size());
282 return block_mask[block_index];
283 }
284}
285
286
287inline bool
288BlockMask::represents_n_blocks(const unsigned int n) const
289{
290 return ((block_mask.empty()) || (block_mask.size() == n));
291}
292
293
294inline unsigned int
295BlockMask::n_selected_blocks(const unsigned int n) const
296{
297 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
298 AssertDimension(n, size());
299
300 const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
301 if (block_mask.empty())
302 return real_n;
303 else
304 {
305 AssertDimension(real_n, block_mask.size());
306 return std::count_if(block_mask.begin(),
307 block_mask.end(),
308 [](const bool selected) { return selected; });
309 }
310}
311
312
313inline unsigned int
314BlockMask::first_selected_block(const unsigned int n) const
315{
316 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
317 AssertDimension(n, size());
318
319 if (block_mask.empty())
320 return 0;
321 else
322 {
323 for (unsigned int c = 0; c < block_mask.size(); ++c)
324 if (block_mask[c] == true)
325 return c;
326
327 Assert(false, ExcMessage("No block is selected at all!"));
329 }
330}
331
332
333
334inline bool
336{
337 return (block_mask.empty());
338}
339
340
341
342inline BlockMask
343BlockMask::operator|(const BlockMask &mask) const
344{
345 // if one of the two masks denotes the all-block mask,
346 // then return the other one
347 if (block_mask.empty())
348 return mask;
349 else if (mask.block_mask.empty())
350 return *this;
351 else
352 {
353 // if both masks have individual entries set, form
354 // the combination of the two
355 AssertDimension(block_mask.size(), mask.block_mask.size());
356 std::vector<bool> new_mask(block_mask.size());
357 for (unsigned int i = 0; i < block_mask.size(); ++i)
358 new_mask[i] = (block_mask[i] || mask.block_mask[i]);
359
360 return BlockMask(new_mask);
361 }
362}
363
364
365inline BlockMask
366BlockMask::operator&(const BlockMask &mask) const
367{
368 // if one of the two masks denotes the all-block mask,
369 // then return the other one
370 if (block_mask.empty())
371 return mask;
372 else if (mask.block_mask.empty())
373 return *this;
374 else
375 {
376 // if both masks have individual entries set, form
377 // the combination of the two
378 AssertDimension(block_mask.size(), mask.block_mask.size());
379 std::vector<bool> new_mask(block_mask.size());
380 for (unsigned int i = 0; i < block_mask.size(); ++i)
381 new_mask[i] = (block_mask[i] && mask.block_mask[i]);
382
383 return BlockMask(new_mask);
384 }
385}
386
387
388inline bool
389BlockMask::operator==(const BlockMask &mask) const
390{
391 return block_mask == mask.block_mask;
392}
393
394
395inline bool
396BlockMask::operator!=(const BlockMask &mask) const
397{
398 return block_mask != mask.block_mask;
399}
400#endif // DOXYGEN
401
403
404#endif
bool operator==(const BlockMask &mask) const
BlockMask operator&(const BlockMask &mask) const
BlockMask operator|(const BlockMask &mask) const
std::size_t memory_consumption() const
Definition block_mask.cc:50
bool represents_the_all_selected_mask() const
unsigned int size() const
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
BlockMask()=default
bool operator[](const unsigned int block_index) const
bool operator!=(const BlockMask &mask) const
std::vector< bool > block_mask
Definition block_mask.h:221
bool represents_n_blocks(const unsigned int n) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:295
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::enable_if_t< IsBlockVector< VectorType >::value, unsigned int > n_blocks(const VectorType &vector)
Definition operators.h:47
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
STL namespace.