diff options
author | Spartan322 <Megacake1234@gmail.com> | 2023-10-17 14:21:24 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-10-17 14:21:24 +0200 |
commit | ed15f46d03f50b93b4cf2bb58da7217e3150f86d (patch) | |
tree | 0b541649c2593a8aa649dd47a645137442b0d305 | |
parent | d4f3231d53da69d98312523fbd9ffee31f3aeaf1 (diff) |
Change `has_identifier` to use `map::contains`
Change `has_index` to compare using size
Change `get_item_identifiers` to use vector ctor over reserve
Change `expect_item_decimal_map` to use move for fixed_point_t
Add shortcut to to `add_item` when calling `duplicate_ignore_callback`
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.hpp | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 1a03e75..f8ece3e 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -135,9 +135,14 @@ namespace OpenVic { return false; } const std::string_view new_identifier = GetIdentifier(GetPointer(item)); - value_type const* old_item = get_item_by_identifier(new_identifier); - if (old_item != nullptr) { - return duplicate_callback(name, new_identifier); + if (duplicate_callback && + duplicate_callback.target<bool(std::string_view, std::string_view)>() == duplicate_ignore_callback) { + if (has_identifier(new_identifier)) return true; + } else { + value_type const* old_item = get_item_by_identifier(new_identifier); + if (old_item != nullptr) { + return duplicate_callback(name, new_identifier); + } } identifier_index_map.emplace(new_identifier, items.size()); items.push_back(std::move(item)); @@ -217,18 +222,17 @@ GETTERS #undef GETTERS bool has_identifier(std::string_view identifier) const { - return get_item_by_identifier(identifier) != nullptr; + return identifier_index_map.contains(identifier); } bool has_index(size_t index) const { - return get_item_by_index(index) != nullptr; + return index < size(); } REF_GETTERS(items) std::vector<std::string_view> get_item_identifiers() const { - std::vector<std::string_view> identifiers; - identifiers.reserve(items.size()); + std::vector<std::string_view> identifiers(items.size()); for (typename decltype(identifier_index_map)::value_type const& entry : identifier_index_map) { identifiers.push_back(entry.first); } @@ -241,7 +245,7 @@ GETTERS bool ret = expect_item_dictionary([&map](value_type const& key, ast::NodeCPtr value) -> bool { fixed_point_t val; const bool ret = NodeTools::expect_fixed_point(NodeTools::assign_variable_callback(val))(value); - map.emplace(&key, val); + map.emplace(&key, std::move(val)); return ret; })(node); ret &= callback(std::move(map)); |