| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2015 - 2025 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_named_selection_h |
| 16 |
#define dealii_named_selection_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/algorithms/any_data.h> |
| 21 |
|
| 22 |
#include <deal.II/base/exceptions.h> |
| 23 |
|
| 24 |
#include <string> |
| 25 |
|
| 26 |
DEAL_II_NAMESPACE_OPEN |
| 27 |
|
| 28 |
/** |
| 29 |
* Select data from AnyData corresponding to the attached name. |
| 30 |
* |
| 31 |
* Given a list of names to search for (provided by add()), objects of this |
| 32 |
* class provide an index list of the selected data. |
| 33 |
*/ |
| 34 |
class NamedSelection |
| 35 |
{ |
| 36 |
public: |
| 37 |
/** |
| 38 |
* Add a new name to be searched for in @p data supplied in initialize(). |
| 39 |
* |
| 40 |
* @note Names will be added to the end of the current list. |
| 41 |
*/ |
| 42 |
void |
| 43 |
add(const std::string &name); |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Create the index vector pointing into the AnyData object. |
| 48 |
*/ |
| 49 |
void |
| 50 |
initialize(const AnyData &data); |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* The number of names in this object. This function may be used whether |
| 55 |
* initialize() was called before or not. |
| 56 |
*/ |
| 57 |
unsigned int |
| 58 |
size() const; |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Return the corresponding index in the AnyData object supplied to the last |
| 63 |
* initialize(). It is an error if initialize() has not been called before. |
| 64 |
* |
| 65 |
* Indices are in the same order as the calls to add(). |
| 66 |
*/ |
| 67 |
unsigned int |
| 68 |
operator()(unsigned int i) const; |
| 69 |
|
| 70 |
|
| 71 |
private: |
| 72 |
/** |
| 73 |
* The selected names. |
| 74 |
*/ |
| 75 |
std::vector<std::string> names; |
| 76 |
|
| 77 |
/** |
| 78 |
* The index map generated by initialize() and accessed by operator(). |
| 79 |
*/ |
| 80 |
std::vector<unsigned int> indices; |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
inline unsigned int |
| 85 |
NamedSelection::size() const |
| 86 |
{ |
| 87 |
return names.size(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
inline void |
| 92 |
NamedSelection::add(const std::string &s) |
| 93 |
{ |
| 94 |
names.push_back(s); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
inline unsigned int |
| 99 |
NamedSelection::operator()(unsigned int i) const |
| 100 |
{ |
| 101 |
Assert(indices.size() == names.size(), ExcNotInitialized()); |
| 102 |
|
| 103 |
AssertIndexRange(i, size()); |
| 104 |
|
| 105 |
return indices[i]; |
| 106 |
} |
| 107 |
|
| 108 |
DEAL_II_NAMESPACE_CLOSE |
| 109 |
|
| 110 |
#endif |