From 127e6847175b77f17704000658a47c025874a9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Mon, 14 Mar 2022 21:35:23 +0100 Subject: [PATCH] core: Fix regressions std::unordered_map objects std::unordered_map::emplace(std::pair &&value) and std::unordered_map::insert(std::pair &&value) actually discard the new value if a value with the same key already exists. --- src/core/bond_breakage/bond_breakage.cpp | 2 +- src/script_interface/GlobalContext.cpp | 2 +- .../auto_parameters/AutoParameters.hpp | 5 ++++- src/utils/include/utils/Cache.hpp | 10 +++++++++- src/utils/include/utils/Factory.hpp | 4 ++-- src/utils/include/utils/NumeratedContainer.hpp | 6 +++--- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/core/bond_breakage/bond_breakage.cpp b/src/core/bond_breakage/bond_breakage.cpp index 2081577d882..f63072afdef 100644 --- a/src/core/bond_breakage/bond_breakage.cpp +++ b/src/core/bond_breakage/bond_breakage.cpp @@ -43,7 +43,7 @@ namespace BondBreakage { static std::unordered_map> breakage_specs; void insert_spec(int key, std::shared_ptr obj) { - breakage_specs.insert({key, std::move(obj)}); + breakage_specs[key] = std::move(obj); } void erase_spec(int key) { breakage_specs.erase(key); } diff --git a/src/script_interface/GlobalContext.cpp b/src/script_interface/GlobalContext.cpp index 10480695e31..d9a762550ed 100644 --- a/src/script_interface/GlobalContext.cpp +++ b/src/script_interface/GlobalContext.cpp @@ -49,7 +49,7 @@ void GlobalContext::make_handle(ObjectId id, const std::string &name, ObjectRef so = m_node_local_context->make_shared( name, unpack(parameters, m_local_objects)); - m_local_objects.emplace(std::make_pair(id, std::move(so))); + m_local_objects[id] = std::move(so); } catch (Exception const &) { } } diff --git a/src/script_interface/auto_parameters/AutoParameters.hpp b/src/script_interface/auto_parameters/AutoParameters.hpp index 341eeee2a6c..e30e1701161 100644 --- a/src/script_interface/auto_parameters/AutoParameters.hpp +++ b/src/script_interface/auto_parameters/AutoParameters.hpp @@ -112,7 +112,10 @@ class AutoParameters : public Base { void add_parameters(std::vector &¶ms) { for (auto const &p : params) { - m_parameters.emplace(std::make_pair(p.name, p)); + if (m_parameters.count(p.name)) { + m_parameters.erase(p.name); + } + m_parameters.emplace(std::make_pair(p.name, std::move(p))); } } diff --git a/src/utils/include/utils/Cache.hpp b/src/utils/include/utils/Cache.hpp index 1f6e88ed7d8..b16a18186ee 100644 --- a/src/utils/include/utils/Cache.hpp +++ b/src/utils/include/utils/Cache.hpp @@ -110,11 +110,19 @@ template class Cache { * maximal size, a random element is removed before * putting the new one. */ template Value const *put(Key const &k, ValueRef &&v) { + auto check_for_k = true; + /* If there already is a value for k, overwriting it * will not increase the size, so we don't have to * make room. */ - if ((m_cache.size() >= m_max_size) && !has(k)) + if ((m_cache.size() >= m_max_size) && !has(k)) { drop_random_element(); + check_for_k = false; + } + + if (check_for_k && has(k)) { + m_cache.erase(k); + } typename map_type::const_iterator it; std::tie(it, std::ignore) = m_cache.emplace(k, std::forward(v)); diff --git a/src/utils/include/utils/Factory.hpp b/src/utils/include/utils/Factory.hpp index 4393c7a58f6..fc59d972d10 100644 --- a/src/utils/include/utils/Factory.hpp +++ b/src/utils/include/utils/Factory.hpp @@ -111,8 +111,8 @@ class Factory { * @param name Given name for the type, has to be unique in this Factory. */ template void register_new(const std::string &name) { - m_map.insert({name, []() { return pointer_type(new Derived()); }}); - m_type_map.insert({typeid(Derived), name}); + m_map[name] = []() { return pointer_type(new Derived()); }; + m_type_map[typeid(Derived)] = name; } /** diff --git a/src/utils/include/utils/NumeratedContainer.hpp b/src/utils/include/utils/NumeratedContainer.hpp index 9155027ed83..a6b37714be9 100644 --- a/src/utils/include/utils/NumeratedContainer.hpp +++ b/src/utils/include/utils/NumeratedContainer.hpp @@ -57,7 +57,7 @@ template class NumeratedContainer { explicit NumeratedContainer(std::initializer_list l) : NumeratedContainer() { for (auto const &e : l) { - m_container.insert(e); + m_container[e.first] = e.second; /* Remove the index from the index set if it exists. */ m_free_indices.erase(m_free_indices.find(e.first), m_free_indices.end()); } @@ -79,14 +79,14 @@ template class NumeratedContainer { */ index_type add(const T &c) { const index_type ind = get_index(); - m_container.emplace(std::make_pair(ind, c)); + m_container[ind] = c; return ind; } /** @overload */ index_type add(T &&c) { const index_type ind = get_index(); - m_container.emplace(std::make_pair(ind, std::move(c))); + m_container[ind] = std::move(c); return ind; }