23 std::vector<std::string> attribute_names;
27 std::back_inserter(attribute_names),
28 [](
const auto& ptr) -> std::string {
36 attribute_names.erase(
38 attribute_names.begin(),
39 attribute_names.end(),
40 [](
const std::string& name) { return name.empty(); }),
41 attribute_names.end());
44 if (hdf5_w !=
nullptr) {
48 for (
const auto& attr_ptr : m_attributes) {
50 attr_ptr->serialize(dim, writer);
62 for (
const auto& [name, handle] : m_handles) {
63 ret[
"attr_handle_" + name] = std::hash<AttributeHandle>{}(handle);
72 std::map<std::string, const wmtk::utils::Hashable*> ret;
73 for (
const auto& [name, handle] : m_handles) {
74 const auto& attr = attribute(handle);
75 ret[
"attr_" + name] = &attr;
83 for (
auto& attr_ptr : m_attributes) {
92 for (
auto& attr_ptr : m_attributes) {
101 for (
auto& attr_ptr : m_attributes) {
102 if (
bool(attr_ptr)) {
110 for (
const auto& attr_ptr : m_attributes) {
111 if (
bool(attr_ptr)) {
112 attr_ptr->change_to_next_scope();
120 for (
const auto& attr_ptr : m_attributes) {
121 if (
bool(attr_ptr)) {
122 attr_ptr->change_to_previous_scope();
129 const std::string& name,
134 if (!replace && has_attribute(name)) {
136 "Cannot register attribute '{}' because it exists already. Set replace to true if you "
137 "want to overwrite the attribute",
144 if (replace && m_handles.find(name) != m_handles.end()) {
145 auto it = m_handles.find(name);
146 handle = it->second.
index();
148 handle = m_attributes.size();
149 m_attributes.emplace_back(
152 m_handles[name] = handle;
161 for (
const auto& a : m_attributes) {
163 assert(a->reserved_size() >= cap);
170 auto it = m_handles.find(name);
171 if (it == m_handles.end()) {
179 auto it = m_handles.find(name);
180 return it != m_handles.end() && bool(m_attributes[it->second.index()]);
192 for (
size_t j = 0; j < m_attributes.size(); ++j) {
193 const bool exists = bool(m_attributes[j]);
195 if (exists != o_exists) {
197 }
else if (exists && o_exists && !(*m_attributes[j] == *other.
m_attributes[j])) {
209 auto& attr_ptr = m_attributes[handle.
index()];
210 assert(
bool(attr_ptr));
211 auto& attr = *attr_ptr;
212 attr.set(std::move(val));
218 auto& attr_ptr = m_attributes[handle.
index()];
219 assert(
bool(attr_ptr));
220 return attr_ptr->reserved_size();
226 return m_reserved_size;
232 return active_attributes().size();
237 std::vector<AttributeHandle> handles;
238 handles.reserve(m_attributes.size());
239 for (
size_t j = 0; j < m_attributes.size(); ++j) {
240 if (
bool(m_attributes[j])) {
250 const size_t index = h.
index();
251 assert(index < m_attributes.size());
252 return bool(m_attributes[index]);
258 m_reserved_size = size;
259 for (
auto& attr_ptr : m_attributes) {
260 if (
bool(attr_ptr)) {
269 reserve(m_reserved_size + size);
275 if (size > m_reserved_size) {
293 auto& attr = m_attributes[attribute.
index()];
295 logger().warn(
"Attribute was already deleted.");
299 const std::string& name = attr->name();
300 m_handles.erase(name);
301 m_attributes[attribute.
index()].reset();
307 if (m_handles.size() > m_attributes.size()) {
309 "More handles than attributes. Handles: {}, attributes: {}",
311 m_attributes.size());
314 for (
const auto& [name, handle] : m_handles) {
315 if (handle.index() >= m_attributes.size()) {
317 "Handle index for `{}` is out of bounds for attributes vector. Handle index: {}, "
322 m_attributes.size());
325 if (!m_attributes[handle.index()]) {
328 const auto& attr = m_attributes[handle.index()];
329 if (attr->name() != name) {
331 "Attribute name is not the same as the name in the handles map. Attribute: {}, "
345 for (
const auto& [name, h] : m_handles) {
346 if (h.index() != handle.
index()) {
349 if (!m_attributes[handle.
index()]) {
352 const auto& attr_name = m_attributes[handle.
index()]->name();
353 if (attr_name != name) {
355 "Attribute name is not the same as the name in the handles map. Attribute: {}, "
365 logger().warn(
"Handle with index `{}` was not found in the handles map.", handle.
index());
372 size_t old_index = 0;
373 size_t new_index = 0;
374 std::vector<int64_t> old_to_new_id(m_attributes.size(), -1);
375 for (old_index = 0; old_index < m_attributes.size(); ++old_index) {
376 if (
bool(m_attributes[old_index])) {
377 old_to_new_id[old_index] = new_index;
378 m_attributes[new_index++] = std::move(m_attributes[old_index]);
381 m_attributes.resize(new_index);
384 for (
auto it = m_handles.begin(); it != m_handles.end(); ) {
385 if (int64_t new_ind = old_to_new_id[it->second.index()]; new_ind == -1) {
386 it = m_handles.erase(it);
388 it->second = new_ind;
396 for (
const auto& [key, value] : m_handles) {
397 if (value == handle) {
401 throw std::runtime_error(
"Could not find handle in TypedAttributeManager");
408 const std::string old_name = get_name(handle);
410 if (old_name == name) {
414 auto& attr = m_attributes[handle.
index()];
416 assert(attr->name() == old_name);
418 assert(!has_attribute(name));
420 attr->set_name(name);
421 m_handles[name] = m_handles[old_name];
422 m_handles.erase(old_name);
void write_attribute_names(int dim, const std::vector< std::string > &names)
Internal handle representation used by TypedAttributeManager.
int64_t index() const noexcept
Contains all attributes of type T for a single mesh.
void reserve(const int64_t size)
void pop_scope(bool apply_updates=true)
void rollback_current_scope()
std::map< std::string, AttributeHandle > m_handles
std::vector< std::unique_ptr< CachingAttribute< T > > > m_attributes
void serialize(const int dim, MeshWriter &writer) const
std::map< std::string, std::size_t > child_hashes() const override
void log_and_throw_error(const std::string &msg)
spdlog::logger & logger()
Retrieves the current logger.