From 2fec521cc6bbe7b2cda0eef3b830acbfc8b68333 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 30 Apr 2023 23:59:54 +0100 Subject: Hashmaps instead of linear + better province hover --- extension/src/openvic2/Types.hpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'extension/src/openvic2/Types.hpp') diff --git a/extension/src/openvic2/Types.hpp b/extension/src/openvic2/Types.hpp index 1359fbe..e4a0e2d 100644 --- a/extension/src/openvic2/Types.hpp +++ b/extension/src/openvic2/Types.hpp @@ -1,9 +1,9 @@ #pragma once -#include #include #include #include +#include #include "Logger.hpp" @@ -74,9 +74,12 @@ namespace OpenVic2 { */ template::value>::type* = nullptr> class IdentifierRegistry { + using identifier_index_map_t = std::map; + const std::string name; std::vector items; bool locked = false; + identifier_index_map_t identifier_index_map; public: IdentifierRegistry(std::string const& new_name) : name(new_name) {} return_t add_item(T&& item) { @@ -89,6 +92,7 @@ namespace OpenVic2 { Logger::error("Cannot add item to the ", name, " registry - an item with the identifier \"", item.get_identifier(), "\" already exists!"); return FAILURE; } + identifier_index_map[item.get_identifier()] = items.size(); items.push_back(std::move(item)); return SUCCESS; } @@ -104,6 +108,7 @@ namespace OpenVic2 { return locked; } void reset() { + identifier_index_map.clear(); items.clear(); locked = false; } @@ -111,15 +116,13 @@ namespace OpenVic2 { return items.size(); } T* get_item_by_identifier(std::string const& identifier) { - if (!identifier.empty()) - for (T& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T const* get_item_by_identifier(std::string const& identifier) const { - if (!identifier.empty()) - for (T const& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T* get_item_by_index(size_t index) { -- cgit v1.2.3-56-ga3b1