From 0b653938ccd24ecfa1b927092cb8aeefbccdd74a Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Sat, 16 May 2020 21:57:28 +0200 Subject: [PATCH] Use unordered_map in KeywordTrie --- src/data/keyword.cpp | 15 ++++++++++++--- src/data/keyword.hpp | 4 ++-- src/script/functions/basic.cpp | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/data/keyword.cpp b/src/data/keyword.cpp index e9d6d7cf..adcb7ebe 100644 --- a/src/data/keyword.cpp +++ b/src/data/keyword.cpp @@ -9,6 +9,7 @@ #include #include #include +#include class KeywordTrie; DECLARE_POINTER_TYPE(KeywordParamValue); @@ -243,13 +244,21 @@ void Keyword::prepare(const vector& param_types, bool force) { // ----------------------------------------------------------------------------- : KeywordTrie +namespace std { + template <> struct hash { + inline size_t operator () (wxUniChar x) const { + return std::hash()(x.GetValue()); + } + }; +} + /// A node in a trie to match keywords class KeywordTrie { public: KeywordTrie(); ~KeywordTrie(); - map> children; ///< children after a given character + unordered_map> children; ///< children after a given character KeywordTrie* on_any_star; ///< children on /.*/ (owned or this) vector finished; ///< keywords that end in this node @@ -395,12 +404,12 @@ String KeywordDatabase::expand(const String& text, const ScriptValueP& match_condition, const ScriptValueP& expand_default, const ScriptValueP& combine_script, - Context& ctx) const { + Context& ctx, + KeywordUsageStatistics* stat) const { assert(combine_script); assert_tagged(text); // Clean up usage statistics - KeywordUsageStatistics* stat = keyword_usage_statistics(); Value* stat_key = value_being_updated(); if (stat && stat_key) { for (size_t i = stat->size() - 1 ; i + 1 > 0 ; --i) { // loop backwards diff --git a/src/data/keyword.hpp b/src/data/keyword.hpp index b9d9a4e8..6467de45 100644 --- a/src/data/keyword.hpp +++ b/src/data/keyword.hpp @@ -133,7 +133,6 @@ inline String type_name(const vector&) { /// Store keyword usage statistics here, using value_being_updated as the key typedef vector> KeywordUsageStatistics; -DECLARE_DYNAMIC_ARG(KeywordUsageStatistics*, keyword_usage_statistics); /// A database of keywords to allow for fast matching /** NOTE: keywords may not be altered after they are added to the database, @@ -162,8 +161,9 @@ public: * @param combine_script script function to combine keyword and reminder text in some way * @param case_sensitive case sensitive matching of keywords? * @param ctx context for evaluation of scripts + * @param stats where to put keyword statistics */ - String expand(const String& text, const ScriptValueP& match_condition, const ScriptValueP& expand_default, const ScriptValueP& combine_script, Context& ctx) const; + String expand(const String& text, const ScriptValueP& match_condition, const ScriptValueP& expand_default, const ScriptValueP& combine_script, Context& ctx, KeywordUsageStatistics* stats = nullptr) const; private: unique_ptr root; ///< Data structure for finding keywords diff --git a/src/script/functions/basic.cpp b/src/script/functions/basic.cpp index 84936995..5f01bd0f 100644 --- a/src/script/functions/basic.cpp +++ b/src/script/functions/basic.cpp @@ -672,9 +672,9 @@ SCRIPT_FUNCTION_WITH_DEP(expand_keywords) { db.add(set->game->keywords); } SCRIPT_OPTIONAL_PARAM_C_(CardP, card); - WITH_DYNAMIC_ARG(keyword_usage_statistics, card ? &card->keyword_usage : nullptr); try { - SCRIPT_RETURN(db.expand(input, match_condition, default_expand, combine, ctx)); + KeywordUsageStatistics* stat = card ? &card->keyword_usage : nullptr; + SCRIPT_RETURN(db.expand(input, match_condition, default_expand, combine, ctx, stat)); } catch (const Error& e) { throw ScriptError(_ERROR_2_("in function", e.what(), _("expand_keywords"))); }