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
component_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_component_mask_h
14#define dealii_fe_component_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
80{
81public:
88 ComponentMask() = default;
89
93 template <typename = void>
95 "Implicit conversions from std::vector<bool> to ComponentMask are deprecated!")
96 ComponentMask(const std::vector<bool> &block_mask);
97
107 explicit ComponentMask(const std::vector<bool> &component_mask);
108
117 ComponentMask(const unsigned int n_components, const bool initializer);
118
122 void
123 set(const unsigned int index, const bool value);
124
133 unsigned int
134 size() const;
135
149 bool
150 operator[](const unsigned int component_index) const;
151
161 bool
162 represents_n_components(const unsigned int n) const;
163
177 unsigned int
178 n_selected_components(const unsigned int overall_number_of_components =
179 numbers::invalid_unsigned_int) const;
180
187 unsigned int
188 first_selected_component(const unsigned int overall_number_of_components =
189 numbers::invalid_unsigned_int) const;
190
196 bool
198
204 operator|(const ComponentMask &mask) const;
205
211 operator&(const ComponentMask &mask) const;
212
216 bool
217 operator==(const ComponentMask &mask) const;
218
222 bool
223 operator!=(const ComponentMask &mask) const;
224
229 std::size_t
230 memory_consumption() const;
231
236 "The number of selected components in a mask "
237 "must be greater than zero.");
238
239private:
243 std::vector<bool> component_mask;
244
245 // make the output operator a friend so it can access
246 // the component_mask array
247 friend std::ostream &
248 operator<<(std::ostream &out, const ComponentMask &mask);
249};
250
251
262std::ostream &
263operator<<(std::ostream &out, const ComponentMask &mask);
264
265#ifndef DOXYGEN
266// -------------------- inline functions ---------------------
267
268template <typename>
269inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
271{}
272
273
274inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
275 : component_mask(component_mask)
276{}
277
278
279inline ComponentMask::ComponentMask(const unsigned int n_components,
280 const bool initializer)
281 : component_mask(n_components, initializer)
282{}
283
284
285inline unsigned int
287{
288 return component_mask.size();
289}
290
291
292inline void
293ComponentMask::set(const unsigned int index, const bool value)
294{
295 AssertIndexRange(index, component_mask.size());
297}
298
299
300inline bool
301ComponentMask::operator[](const unsigned int component_index) const
302{
303 // if the mask represents the all-component mask
304 // then always return true
305 if (component_mask.empty())
306 return true;
307 else
308 {
309 // otherwise check the validity of the index and
310 // return whatever is appropriate
311 AssertIndexRange(component_index, component_mask.size());
312 return component_mask[component_index];
313 }
314}
315
316
317inline bool
318ComponentMask::represents_n_components(const unsigned int n) const
319{
320 return ((component_mask.empty()) || (component_mask.size() == n));
321}
322
323
324inline unsigned int
325ComponentMask::n_selected_components(const unsigned int n) const
326{
327 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
328 AssertDimension(n, size());
329
330 const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
331 if (component_mask.empty())
332 return real_n;
333 else
334 {
335 AssertDimension(real_n, component_mask.size());
336 return std::count_if(component_mask.begin(),
337 component_mask.end(),
338 [](const bool selected) { return selected; });
339 }
340}
341
342
343inline unsigned int
344ComponentMask::first_selected_component(const unsigned int n) const
345{
346 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
347 AssertDimension(n, size());
348
349 if (component_mask.empty())
350 return 0;
351 else
352 {
353 for (unsigned int c = 0; c < component_mask.size(); ++c)
354 if (component_mask[c] == true)
355 return c;
356
357 Assert(false, ExcMessage("No component is selected at all!"));
359 }
360}
361
362
363
364inline bool
366{
367 return (component_mask.empty());
368}
369
370
371
372inline ComponentMask
374{
375 // if one of the two masks denotes the all-component mask,
376 // then return the other one
377 if (component_mask.empty())
378 return mask;
379 else if (mask.component_mask.empty())
380 return *this;
381 else
382 {
383 // if both masks have individual entries set, form
384 // the combination of the two
385 AssertDimension(component_mask.size(), mask.component_mask.size());
386 std::vector<bool> new_mask(component_mask.size());
387 for (unsigned int i = 0; i < component_mask.size(); ++i)
388 new_mask[i] = (component_mask[i] || mask.component_mask[i]);
389
390 return ComponentMask(new_mask);
391 }
392}
393
394
395inline ComponentMask
397{
398 // if one of the two masks denotes the all-component mask,
399 // then return the other one
400 if (component_mask.empty())
401 return mask;
402 else if (mask.component_mask.empty())
403 return *this;
404 else
405 {
406 // if both masks have individual entries set, form
407 // the combination of the two
408 AssertDimension(component_mask.size(), mask.component_mask.size());
409 std::vector<bool> new_mask(component_mask.size());
410 for (unsigned int i = 0; i < component_mask.size(); ++i)
411 new_mask[i] = (component_mask[i] && mask.component_mask[i]);
412
413 return ComponentMask(new_mask);
414 }
415}
416
417
418inline bool
420{
421 return component_mask == mask.component_mask;
422}
423
424
425inline bool
427{
428 return component_mask != mask.component_mask;
429}
430#endif // DOXYGEN
431
432
434
435#endif
bool operator[](const unsigned int component_index) const
bool represents_n_components(const unsigned int n) const
ComponentMask()=default
ComponentMask operator&(const ComponentMask &mask) const
void set(const unsigned int index, const bool value)
std::size_t memory_consumption() const
bool represents_the_all_selected_mask() const
bool operator!=(const ComponentMask &mask) const
std::vector< bool > component_mask
unsigned int size() const
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
ComponentMask operator|(const ComponentMask &mask) const
unsigned int first_selected_component(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
bool operator==(const ComponentMask &mask) 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)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcNoComponentSelected()
static ::ExceptionBase & ExcMessage(std::string arg1)
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
STL namespace.