From 746dddc0673d7261f19b1e056e90e6e3a49ef33a Mon Sep 17 00:00:00 2001 From: reduz Date: Fri, 13 May 2022 15:04:37 +0200 Subject: Replace most uses of Map by HashMap * Map is unnecessary and inefficient in almost every case. * Replaced by the new HashMap. * Renamed Map to RBMap and Set to RBSet for cases that still make sense (order matters) but use is discouraged. There were very few cases where replacing by HashMap was undesired because keeping the key order was intended. I tried to keep those (as RBMap) as much as possible, but might have missed some. Review appreciated! --- core/string/optimized_translation.cpp | 6 +++--- core/string/translation.cpp | 34 +++++++++++++++++----------------- core/string/translation.h | 18 +++++++++--------- 3 files changed, 29 insertions(+), 29 deletions(-) (limited to 'core/string') diff --git a/core/string/optimized_translation.cpp b/core/string/optimized_translation.cpp index 93429744fc..07302cc8c3 100644 --- a/core/string/optimized_translation.cpp +++ b/core/string/optimized_translation.cpp @@ -53,7 +53,7 @@ void OptimizedTranslation::generate(const Ref &p_from) { int size = Math::larger_prime(keys.size()); Vector>> buckets; - Vector> table; + Vector> table; Vector hfunc_table; Vector compressed; @@ -108,7 +108,7 @@ void OptimizedTranslation::generate(const Ref &p_from) { for (int i = 0; i < size; i++) { const Vector> &b = buckets[i]; - Map &t = table.write[i]; + HashMap &t = table.write[i]; if (b.size() == 0) { continue; @@ -147,7 +147,7 @@ void OptimizedTranslation::generate(const Ref &p_from) { int btindex = 0; for (int i = 0; i < size; i++) { - const Map &t = table[i]; + const HashMap &t = table[i]; if (t.size() == 0) { htw[i] = 0xFFFFFFFF; //nothing continue; diff --git a/core/string/translation.cpp b/core/string/translation.cpp index d6d361b5f1..c64f815563 100644 --- a/core/string/translation.cpp +++ b/core/string/translation.cpp @@ -95,12 +95,12 @@ StringName Translation::get_message(const StringName &p_src_text, const StringNa WARN_PRINT("Translation class doesn't handle context. Using context in get_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class"); } - const Map::Element *E = translation_map.find(p_src_text); + HashMap::ConstIterator E = translation_map.find(p_src_text); if (!E) { return StringName(); } - return E->get(); + return E->value; } StringName Translation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const { @@ -215,12 +215,12 @@ static _character_accent_pair _character_to_accented[] = { Vector TranslationServer::locale_script_info; -Map TranslationServer::language_map; -Map TranslationServer::script_map; -Map TranslationServer::locale_rename_map; -Map TranslationServer::country_name_map; -Map TranslationServer::variant_map; -Map TranslationServer::country_rename_map; +HashMap TranslationServer::language_map; +HashMap TranslationServer::script_map; +HashMap TranslationServer::locale_rename_map; +HashMap TranslationServer::country_name_map; +HashMap TranslationServer::variant_map; +HashMap TranslationServer::country_rename_map; void TranslationServer::init_locale_info() { // Init locale info. @@ -452,8 +452,8 @@ String TranslationServer::get_locale_name(const String &p_locale) const { Vector TranslationServer::get_all_languages() const { Vector languages; - for (const Map::Element *E = language_map.front(); E; E = E->next()) { - languages.push_back(E->key()); + for (const KeyValue &E : language_map) { + languages.push_back(E.key); } return languages; @@ -466,8 +466,8 @@ String TranslationServer::get_language_name(const String &p_language) const { Vector TranslationServer::get_all_scripts() const { Vector scripts; - for (const Map::Element *E = script_map.front(); E; E = E->next()) { - scripts.push_back(E->key()); + for (const KeyValue &E : script_map) { + scripts.push_back(E.key); } return scripts; @@ -480,8 +480,8 @@ String TranslationServer::get_script_name(const String &p_script) const { Vector TranslationServer::get_all_countries() const { Vector countries; - for (const Map::Element *E = country_name_map.front(); E; E = E->next()) { - countries.push_back(E->key()); + for (const KeyValue &E : country_name_map) { + countries.push_back(E.key); } return countries; @@ -507,7 +507,7 @@ String TranslationServer::get_locale() const { Array TranslationServer::get_loaded_locales() const { Array locales; - for (const Set>::Element *E = translations.front(); E; E = E->next()) { + for (const RBSet>::Element *E = translations.front(); E; E = E->next()) { const Ref &t = E->get(); ERR_FAIL_COND_V(t.is_null(), Array()); String l = t->get_locale(); @@ -530,7 +530,7 @@ Ref TranslationServer::get_translation_object(const String &p_local Ref res; int best_score = 0; - for (const Set>::Element *E = translations.front(); E; E = E->next()) { + for (const RBSet>::Element *E = translations.front(); E; E = E->next()) { const Ref &t = E->get(); ERR_FAIL_COND_V(t.is_null(), nullptr); String l = t->get_locale(); @@ -599,7 +599,7 @@ StringName TranslationServer::_get_message_from_translations(const StringName &p StringName res; int best_score = 0; - for (const Set>::Element *E = translations.front(); E; E = E->next()) { + for (const RBSet>::Element *E = translations.front(); E; E = E->next()) { const Ref &t = E->get(); ERR_FAIL_COND_V(t.is_null(), p_message); String l = t->get_locale(); diff --git a/core/string/translation.h b/core/string/translation.h index ded6ed5925..f58f6f91a2 100644 --- a/core/string/translation.h +++ b/core/string/translation.h @@ -41,7 +41,7 @@ class Translation : public Resource { RES_BASE_EXTENSION("translation"); String locale = "en"; - Map translation_map; + HashMap translation_map; virtual Vector _get_message_list() const; virtual Dictionary _get_messages() const; @@ -74,7 +74,7 @@ class TranslationServer : public Object { String locale = "en"; String fallback; - Set> translations; + RBSet> translations; Ref tool_translation; Ref doc_translation; @@ -111,16 +111,16 @@ class TranslationServer : public Object { String name; String script; String default_country; - Set supported_countries; + RBSet supported_countries; }; static Vector locale_script_info; - static Map language_map; - static Map script_map; - static Map locale_rename_map; - static Map country_name_map; - static Map country_rename_map; - static Map variant_map; + static HashMap language_map; + static HashMap script_map; + static HashMap locale_rename_map; + static HashMap country_name_map; + static HashMap country_rename_map; + static HashMap variant_map; void init_locale_info(); -- cgit v1.2.3