From e1496a87178d925277aceed0ebcbab06920e15ee Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Mon, 25 Dec 2023 02:42:11 -0500 Subject: Add `https://github.com/Tessil/ordered-map` Add is_specialization_of to Utility.hpp Add OpenVic::ordered_map and OpenVic::ordered_set Change `std::map` to `ordered_map` Change `std::set to use `ordered_set` Add `set_callback_pointer(tsl::ordered_set& set)` Add mutable_iterator to enable mutable value iterator for `tsl::ordered_map` Add std::hash implementation Enable deps/SCsub to expose dependency includes neccessary for inclusion --- src/openvic-simulation/dataloader/NodeTools.cpp | 17 ++++++++--------- src/openvic-simulation/dataloader/NodeTools.hpp | 15 +++++++++------ src/openvic-simulation/dataloader/Vic2PathSearch.cpp | 3 ++- 3 files changed, 19 insertions(+), 16 deletions(-) (limited to 'src/openvic-simulation/dataloader') diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index e68a185..1dbc99c 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -1,6 +1,7 @@ #include "NodeTools.hpp" #include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/utility/TslHelper.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -314,22 +315,20 @@ bool NodeTools::add_key_map_entry( } bool NodeTools::remove_key_map_entry(key_map_t& key_map, std::string_view key) { - const key_map_t::const_iterator it = key_map.find(key); - if (it != key_map.end()) { - key_map.erase(it); - return true; + if(key_map.erase(key) == 0) { + Logger::error("Failed to find dictionary key to remove: ", key); + return false; } - Logger::error("Failed to find dictionary key to remove: ", key); - return false; + return true; } key_value_callback_t NodeTools::dictionary_keys_callback(key_map_t& key_map, key_value_callback_t default_callback) { return [&key_map, default_callback](std::string_view key, ast::NodeCPtr value) -> bool { - const key_map_t::iterator it = key_map.find(key); + key_map_t::iterator it = key_map.find(key); if (it == key_map.end()) { return default_callback(key, value); } - dictionary_entry_t& entry = it->second; + dictionary_entry_t& entry = it.value(); if (++entry.count > 1 && !entry.can_repeat()) { Logger::error("Invalid repeat of dictionary key: ", key); return false; @@ -345,7 +344,7 @@ key_value_callback_t NodeTools::dictionary_keys_callback(key_map_t& key_map, key bool NodeTools::check_key_map_counts(key_map_t& key_map) { bool ret = true; - for (key_map_t::value_type& key_entry : key_map) { + for (auto key_entry : mutable_iterator(key_map)) { dictionary_entry_t& entry = key_entry.second; if (entry.must_appear() && entry.count < 1) { Logger::error("Mandatory dictionary key not present: ", key_entry.first); diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index c3eaf65..f3224aa 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -10,8 +10,11 @@ #include +#include + #include "openvic-simulation/types/Colour.hpp" #include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/OrderedContainers.hpp" #include "openvic-simulation/types/Vector.hpp" namespace OpenVic { @@ -20,10 +23,10 @@ namespace OpenVic { /* Template for map from strings to Ts, in which string_views can be * searched for without needing to be copied into a string */ template - using string_map_t = std::map>; + using string_map_t = ordered_map; /* String set type supporting heterogeneous key lookup */ - using string_set_t = std::set>; + using string_set_t = ordered_set; namespace NodeTools { @@ -137,7 +140,7 @@ namespace OpenVic { node_callback_t expect_dictionary(key_value_callback_t callback); struct dictionary_entry_t { - const enum class expected_count_t : uint8_t { + enum class expected_count_t : uint8_t { _MUST_APPEAR = 0b01, _CAN_REPEAT = 0b10, @@ -146,7 +149,7 @@ namespace OpenVic { ZERO_OR_MORE = _CAN_REPEAT, ONE_OR_MORE = _MUST_APPEAR | _CAN_REPEAT } expected_count; - const node_callback_t callback; + node_callback_t callback; size_t count; dictionary_entry_t(expected_count_t new_expected_count, node_callback_t new_callback) @@ -344,8 +347,8 @@ namespace OpenVic { }; } - template - Callback auto set_callback_pointer(std::set& set) { + template + Callback auto set_callback_pointer(tsl::ordered_set& set) { return [&set](T const& val) -> bool { set.insert(&val); return true; diff --git a/src/openvic-simulation/dataloader/Vic2PathSearch.cpp b/src/openvic-simulation/dataloader/Vic2PathSearch.cpp index 10bd08d..d3468e4 100644 --- a/src/openvic-simulation/dataloader/Vic2PathSearch.cpp +++ b/src/openvic-simulation/dataloader/Vic2PathSearch.cpp @@ -3,6 +3,7 @@ #include #include +#include "openvic-simulation/types/OrderedContainers.hpp" #include "openvic-simulation/utility/ConstexprIntToStr.hpp" #include "openvic-simulation/utility/Logger.hpp" @@ -331,7 +332,7 @@ fs::path Dataloader::search_for_game_path(fs::path hint_path) { }; using hint_path_t = fs::path; using game_path_t = fs::path; - static std::unordered_map _cached_paths; + static ordered_map _cached_paths; auto it = _cached_paths.find(hint_path); if (it != _cached_paths.end()) { -- cgit v1.2.3-56-ga3b1