Loading [MathJax]/extensions/TeX/newcommand.js
 deal.II version GIT relicensing-2846-g6fb608615f 2025-03-15 04:10:00+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\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
component_mask.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2012 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_fe_component_mask_h
16#define dealii_fe_component_mask_h
17
18#include <deal.II/base/config.h>
19
22#include <deal.II/base/types.h>
23
24#include <algorithm>
25#include <iosfwd>
26#include <vector>
27
29
30
31
82{
83public:
90 ComponentMask() = default;
91
95 template <typename = void>
97 "Implicit conversions from std::vector<bool> to ComponentMask are deprecated!")
98 ComponentMask(const std::vector<bool> &block_mask);
99
109 explicit ComponentMask(const std::vector<bool> &component_mask);
110
119 ComponentMask(const unsigned int n_components, const bool initializer);
120
124 void
125 set(const unsigned int index, const bool value);
126
135 unsigned int
136 size() const;
137
151 bool
152 operator[](const unsigned int component_index) const;
153
163 bool
164 represents_n_components(const unsigned int n) const;
165
179 unsigned int
180 n_selected_components(const unsigned int overall_number_of_components =
181 numbers::invalid_unsigned_int) const;
182
189 unsigned int
190 first_selected_component(const unsigned int overall_number_of_components =
191 numbers::invalid_unsigned_int) const;
192
198 bool
200
206 operator|(const ComponentMask &mask) const;
207
213 operator&(const ComponentMask &mask) const;
214
218 bool
219 operator==(const ComponentMask &mask) const;
220
224 bool
225 operator!=(const ComponentMask &mask) const;
226
231 std::size_t
232 memory_consumption() const;
233
238 "The number of selected components in a mask "
239 "must be greater than zero.");
240
241private:
245 std::vector<bool> component_mask;
246
247 // make the output operator a friend so it can access
248 // the component_mask array
249 friend std::ostream &
250 operator<<(std::ostream &out, const ComponentMask &mask);
251};
252
253
264std::ostream &
265operator<<(std::ostream &out, const ComponentMask &mask);
266
267#ifndef DOXYGEN
268// -------------------- inline functions ---------------------
269
270template <typename>
271inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
273{}
274
275
276inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
277 : component_mask(component_mask)
278{}
279
280
281inline ComponentMask::ComponentMask(const unsigned int n_components,
282 const bool initializer)
283 : component_mask(n_components, initializer)
284{}
285
286
287inline unsigned int
289{
290 return component_mask.size();
291}
292
293
294inline void
295ComponentMask::set(const unsigned int index, const bool value)
296{
297 AssertIndexRange(index, component_mask.size());
299}
300
301
302inline bool
303ComponentMask::operator[](const unsigned int component_index) const
304{
305 // if the mask represents the all-component mask
306 // then always return true
307 if (component_mask.empty())
308 return true;
309 else
310 {
311 // otherwise check the validity of the index and
312 // return whatever is appropriate
313 AssertIndexRange(component_index, component_mask.size());
314 return component_mask[component_index];
315 }
316}
317
318
319inline bool
320ComponentMask::represents_n_components(const unsigned int n) const
321{
322 return ((component_mask.empty()) || (component_mask.size() == n));
323}
324
325
326inline unsigned int
327ComponentMask::n_selected_components(const unsigned int n) const
328{
329 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
330 AssertDimension(n, size());
331
332 const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
333 if (component_mask.empty())
334 return real_n;
335 else
336 {
337 AssertDimension(real_n, component_mask.size());
338 return std::count_if(component_mask.begin(),
339 component_mask.end(),
340 [](const bool selected) { return selected; });
341 }
342}
343
344
345inline unsigned int
346ComponentMask::first_selected_component(const unsigned int n) const
347{
348 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
349 AssertDimension(n, size());
350
351 if (component_mask.empty())
352 return 0;
353 else
354 {
355 for (unsigned int c = 0; c < component_mask.size(); ++c)
356 if (component_mask[c] == true)
357 return c;
358
359 Assert(false, ExcMessage("No component is selected at all!"));
361 }
362}
363
364
365
366inline bool
368{
369 return (component_mask.empty());
370}
371
372
373
374inline ComponentMask
376{
377 // if one of the two masks denotes the all-component mask,
378 // then return the other one
379 if (component_mask.empty())
380 return mask;
381 else if (mask.component_mask.empty())
382 return *this;
383 else
384 {
385 // if both masks have individual entries set, form
386 // the combination of the two
387 AssertDimension(component_mask.size(), mask.component_mask.size());
388 std::vector<bool> new_mask(component_mask.size());
389 for (unsigned int i = 0; i < component_mask.size(); ++i)
390 new_mask[i] = (component_mask[i] || mask.component_mask[i]);
391
392 return ComponentMask(new_mask);
393 }
394}
395
396
397inline ComponentMask
399{
400 // if one of the two masks denotes the all-component mask,
401 // then return the other one
402 if (component_mask.empty())
403 return mask;
404 else if (mask.component_mask.empty())
405 return *this;
406 else
407 {
408 // if both masks have individual entries set, form
409 // the combination of the two
410 AssertDimension(component_mask.size(), mask.component_mask.size());
411 std::vector<bool> new_mask(component_mask.size());
412 for (unsigned int i = 0; i < component_mask.size(); ++i)
413 new_mask[i] = (component_mask[i] && mask.component_mask[i]);
414
415 return ComponentMask(new_mask);
416 }
417}
418
419
420inline bool
422{
423 return component_mask == mask.component_mask;
424}
425
426
427inline bool
429{
430 return component_mask != mask.component_mask;
431}
432#endif // DOXYGEN
433
434
436
437#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:40
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:284
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:41
#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:232
STL namespace.