From ed15f46d03f50b93b4cf2bb58da7217e3150f86d Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Tue, 17 Oct 2023 08:21:24 -0400 Subject: 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` --- src/openvic-simulation/types/IdentifierRegistry.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/openvic-simulation') 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() == 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 get_item_identifiers() const { - std::vector identifiers; - identifiers.reserve(items.size()); + std::vector 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)); -- cgit v1.2.3-56-ga3b1