| 1 |
// Copyright(C) 1999-2022 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include "Ioss_Field.h" |
| 8 |
#include "Ioss_FieldManager.h" |
| 9 |
#include "Ioss_Sort.h" |
| 10 |
#include "Ioss_Utils.h" |
| 11 |
#include <cassert> |
| 12 |
#include <cstddef> |
| 13 |
#include <string> |
| 14 |
|
| 15 |
#include "Ioss_CodeTypes.h" |
| 16 |
#include "robin_hash.h" |
| 17 |
|
| 18 |
/** \brief Add a field to the field manager. |
| 19 |
* |
| 20 |
* Assumes that a field with the same name does not already exist. |
| 21 |
* |
| 22 |
* \param[in] new_field The field to add |
| 23 |
* |
| 24 |
*/ |
| 25 |
void Ioss::FieldManager::add(const Ioss::Field &new_field) |
| 26 |
{ |
| 27 |
std::string key = Ioss::Utils::lowercase(new_field.get_name()); |
| 28 |
if (!exists(key)) { |
| 29 |
IOSS_FUNC_ENTER(m_); |
| 30 |
fields.insert(FieldValuePair(key, new_field)); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** \brief Checks if a field with a given name exists in the field manager. |
| 35 |
* |
| 36 |
* \param[in] field_name The name of the field to check for. |
| 37 |
* \returns True if the field exists in the field manager. |
| 38 |
* |
| 39 |
*/ |
| 40 |
bool Ioss::FieldManager::exists(const std::string &field_name) const |
| 41 |
{ |
| 42 |
IOSS_FUNC_ENTER(m_); |
| 43 |
const std::string key = Ioss::Utils::lowercase(field_name); |
| 44 |
return (fields.find(key) != fields.end()); |
| 45 |
} |
| 46 |
|
| 47 |
/** \brief Get a field from the field manager. |
| 48 |
* |
| 49 |
* \param[in] field_name The name of the field to get. |
| 50 |
* \returns The field object. |
| 51 |
* |
| 52 |
*/ |
| 53 |
Ioss::Field Ioss::FieldManager::get(const std::string &field_name) const |
| 54 |
{ |
| 55 |
IOSS_FUNC_ENTER(m_); |
| 56 |
const std::string key = Ioss::Utils::lowercase(field_name); |
| 57 |
auto iter = fields.find(key); |
| 58 |
assert(iter != fields.end()); |
| 59 |
return (*iter).second; |
| 60 |
} |
| 61 |
|
| 62 |
/** \brief Get a reference to a field from the field manager. |
| 63 |
* |
| 64 |
* \param[in] field_name The name of the field to get. |
| 65 |
* \returns A reference to the field object. |
| 66 |
* |
| 67 |
*/ |
| 68 |
const Ioss::Field &Ioss::FieldManager::getref(const std::string &field_name) const |
| 69 |
{ |
| 70 |
IOSS_FUNC_ENTER(m_); |
| 71 |
const std::string key = Ioss::Utils::lowercase(field_name); |
| 72 |
auto iter = fields.find(key); |
| 73 |
assert(iter != fields.end()); |
| 74 |
return (*iter).second; |
| 75 |
} |
| 76 |
|
| 77 |
/** \brief Remove a field from the field manager. |
| 78 |
* |
| 79 |
* Assumes that a field with the given name exists in the field manager. |
| 80 |
* |
| 81 |
* \param[in] field_name The name of the field to remove. |
| 82 |
*/ |
| 83 |
void Ioss::FieldManager::erase(const std::string &field_name) |
| 84 |
{ |
| 85 |
assert(exists(field_name)); |
| 86 |
IOSS_FUNC_ENTER(m_); |
| 87 |
const std::string key = Ioss::Utils::lowercase(field_name); |
| 88 |
auto iter = fields.find(key); |
| 89 |
if (iter != fields.end()) { |
| 90 |
fields.erase(iter); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** \brief Remove all fields of type `role` from the field manager. |
| 95 |
* |
| 96 |
* \param[in] role Remove all fields (if any) of type `role` |
| 97 |
*/ |
| 98 |
void Ioss::FieldManager::erase(Field::RoleType role) |
| 99 |
{ |
| 100 |
auto names = describe(role); |
| 101 |
IOSS_FUNC_ENTER(m_); |
| 102 |
|
| 103 |
for (const auto &field_name : names) { |
| 104 |
const std::string key = Ioss::Utils::lowercase(field_name); |
| 105 |
auto iter = fields.find(key); |
| 106 |
if (iter != fields.end()) { |
| 107 |
fields.erase(iter); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** \brief Get the names of all fields in the field manager. |
| 113 |
* |
| 114 |
* \returns names All field names in the field manager. |
| 115 |
* |
| 116 |
*/ |
| 117 |
Ioss::NameList Ioss::FieldManager::describe() const |
| 118 |
{ |
| 119 |
Ioss::NameList names; |
| 120 |
describe(&names); |
| 121 |
return names; |
| 122 |
} |
| 123 |
|
| 124 |
/** \brief Get the names of all fields in the field manager. |
| 125 |
* |
| 126 |
* \param[out] names All field names in the field manager. |
| 127 |
* \returns The number of fields extracted from the field manager. |
| 128 |
* |
| 129 |
*/ |
| 130 |
int Ioss::FieldManager::describe(NameList *names) const |
| 131 |
{ |
| 132 |
IOSS_FUNC_ENTER(m_); |
| 133 |
int the_count = 0; |
| 134 |
for (const auto &field : fields) { |
| 135 |
names->push_back(field.second.get_name()); |
| 136 |
the_count++; |
| 137 |
} |
| 138 |
if (the_count > 0) { |
| 139 |
Ioss::sort(names->begin(), names->end()); |
| 140 |
} |
| 141 |
return the_count; |
| 142 |
} |
| 143 |
|
| 144 |
/** \brief Get the names of all fields of a specified RoleType in the field manager. |
| 145 |
* |
| 146 |
* \param[in] role The role type (MESH, ATTRIBUTE, TRANSIENT, REDUCTION, etc.) |
| 147 |
* \returns names All field names of the specified RoleType in the field manager. |
| 148 |
* |
| 149 |
*/ |
| 150 |
Ioss::NameList Ioss::FieldManager::describe(Ioss::Field::RoleType role) const |
| 151 |
{ |
| 152 |
Ioss::NameList names; |
| 153 |
describe(role, &names); |
| 154 |
return names; |
| 155 |
} |
| 156 |
|
| 157 |
/** \brief Get the names of all fields of a specified RoleType in the field manager. |
| 158 |
* |
| 159 |
* \param[in] role The role type (MESH, ATTRIBUTE, TRANSIENT, REDUCTION, etc.) |
| 160 |
* \param[out] names All field names of the specified RoleType in the field manager. |
| 161 |
* \returns The number of fields extracted from the field manager. |
| 162 |
* |
| 163 |
*/ |
| 164 |
int Ioss::FieldManager::describe(Ioss::Field::RoleType role, NameList *names) const |
| 165 |
{ |
| 166 |
IOSS_FUNC_ENTER(m_); |
| 167 |
int the_count = 0; |
| 168 |
for (const auto &field : fields) { |
| 169 |
if (field.second.get_role() == role) { |
| 170 |
names->push_back(field.second.get_name()); |
| 171 |
the_count++; |
| 172 |
} |
| 173 |
} |
| 174 |
if (the_count > 0) { |
| 175 |
Ioss::sort(names->begin(), names->end()); |
| 176 |
} |
| 177 |
return the_count; |
| 178 |
} |
| 179 |
|
| 180 |
/** \brief Get the number of fields in the field manager. |
| 181 |
* |
| 182 |
* \returns The number of fields in the field manager. |
| 183 |
*/ |
| 184 |
size_t Ioss::FieldManager::count() const |
| 185 |
{ |
| 186 |
IOSS_FUNC_ENTER(m_); |
| 187 |
return fields.size(); |
| 188 |
} |