From 5b0979f58edc8c1e54a8478adef73cdfe88b6087 Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Sun, 27 Sep 2020 21:53:42 +0200 Subject: [PATCH] unordered_map for locale, and const functions where possible --- src/data/locale.cpp | 8 ++++---- src/data/locale.hpp | 8 ++++---- src/util/io/reader.hpp | 11 ++++++++++- src/util/io/writer.hpp | 14 +++++++++++--- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/data/locale.cpp b/src/data/locale.cpp index 059bbd3a..bbe39c5b 100644 --- a/src/data/locale.cpp +++ b/src/data/locale.cpp @@ -77,16 +77,16 @@ String identity(const String& key) { return key; } -String SubLocale::tr(const String& key, DefaultLocaleFun def) { - map::const_iterator it = translations.find(canonical_name_form(key)); +String SubLocale::tr(const String& key, DefaultLocaleFun def) const { + auto it = translations.find(canonical_name_form(key)); if (it == translations.end()) { return def(key); } else { return it->second; } } -String SubLocale::tr(const String& subcat, const String& key, DefaultLocaleFun def) { - map::const_iterator it = translations.find(subcat + _("_") + canonical_name_form(key)); +String SubLocale::tr(const String& subcat, const String& key, DefaultLocaleFun def) const { + auto it = translations.find(subcat + _("_") + canonical_name_form(key)); if (it == translations.end()) { return def(key); } else { diff --git a/src/data/locale.hpp b/src/data/locale.hpp index 8b5c8892..fcbb1f92 100644 --- a/src/data/locale.hpp +++ b/src/data/locale.hpp @@ -19,14 +19,14 @@ DECLARE_POINTER_TYPE(SubLocaleValidator); // ----------------------------------------------------------------------------- : Locale class -/// Translations of the texts of a game/stylesheet/symbolfont +/// Translations of the texts of a locale category or game/stylesheet/symbolfont class SubLocale : public IntrusivePtrBase { public: - map translations; + unordered_map translations; /// Translate a key, if not found, apply the default function to the key - String tr(const String& key, DefaultLocaleFun def); - String tr(const String& subcat, const String& key, DefaultLocaleFun def); + String tr(const String& key, DefaultLocaleFun def) const; + String tr(const String& subcat, const String& key, DefaultLocaleFun def) const; DECLARE_REFLECTION(); }; diff --git a/src/util/io/reader.hpp b/src/util/io/reader.hpp index c9f2df5d..d3058166 100644 --- a/src/util/io/reader.hpp +++ b/src/util/io/reader.hpp @@ -94,7 +94,8 @@ public: /// Reads a intrusive_ptr from the input stream template void handle(intrusive_ptr&); /// Reads a map from the input stream - template void handle(map&); + template void handle(map&); + template void handle(unordered_map&); /// Reads an IndexMap from the input stream, reads only keys that already exist in the map template void handle(IndexMap&); template void handle(DelayedIndexMaps&); @@ -239,6 +240,14 @@ void Reader::handle(map& m) { } } +template +void Reader::handle(unordered_map& m) { + while (enterAnyBlock()) { + handle_greedy(m[key]); + exitBlock(); + } +} + template void Reader::handle(IndexMap& m) { for (typename IndexMap::iterator it = m.begin() ; it != m.end() ; ++it) { diff --git a/src/util/io/writer.hpp b/src/util/io/writer.hpp index 771fa81e..5efb8130 100644 --- a/src/util/io/writer.hpp +++ b/src/util/io/writer.hpp @@ -57,6 +57,7 @@ public: template void handle(const intrusive_ptr&); /// Write a map to the output stream template void handle(const map&); + template void handle(const unordered_map&); /// Write an IndexMap to the output stream template void handle(const IndexMap&); template void handle(const DelayedIndexMaps&); @@ -111,15 +112,22 @@ void Writer::handle(const intrusive_ptr& pointer) { } template -void Writer::handle(const map& m) { - for (typename map::const_iterator it = m.begin() ; it != m.end() ; ++it) { +void Writer::handle(const map& m) { + for (auto it = m.begin(); it != m.end(); ++it) { + handle(it->first.c_str(), it->second); + } +} + +template +void Writer::handle(const unordered_map& m) { + for (auto it = m.begin(); it != m.end(); ++it) { handle(it->first.c_str(), it->second); } } template void Writer::handle(const IndexMap& m) { - for (typename IndexMap::const_iterator it = m.begin() ; it != m.end() ; ++it) { + for (auto it = m.begin() ; it != m.end() ; ++it) { handle(get_key_name(*it).c_str(), *it); } }