diff --git a/src/common/heterogeneous_containers.h b/src/common/heterogeneous_containers.h index ad769f49c..b920d24dc 100644 --- a/src/common/heterogeneous_containers.h +++ b/src/common/heterogeneous_containers.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) /** @@ -47,8 +47,6 @@ struct transparent_string_less }; } // namespace detail -// This requires C++20, so fallback to ugly heap allocations if we don't have it. -#if __cplusplus >= 202002L template using UnorderedStringMap = std::unordered_map; @@ -60,95 +58,9 @@ using UnorderedStringSet = using UnorderedStringMultiSet = std::unordered_multiset; -template -ALWAYS_INLINE typename UnorderedStringMap::const_iterator -UnorderedStringMapFind(const UnorderedStringMap& map, const KeyType& key) -{ - return map.find(key); -} -template -ALWAYS_INLINE typename UnorderedStringMap::iterator -UnorderedStringMapFind(UnorderedStringMap& map, const KeyType& key) -{ - return map.find(key); -} -template -ALWAYS_INLINE typename UnorderedStringMultimap::const_iterator -UnorderedStringMultiMapFind(const UnorderedStringMultimap& map, const KeyType& key) -{ - return map.find(key); -} -template -ALWAYS_INLINE std::pair::const_iterator, - typename UnorderedStringMultimap::const_iterator> -UnorderedStringMultiMapEqualRange(const UnorderedStringMultimap& map, const KeyType& key) -{ - return map.equal_range(key); -} -template -ALWAYS_INLINE typename UnorderedStringMultimap::iterator -UnorderedStringMultiMapFind(UnorderedStringMultimap& map, const KeyType& key) -{ - return map.find(key); -} -template -ALWAYS_INLINE std::pair::iterator, - typename UnorderedStringMultimap::iterator> -UnorderedStringMultiMapEqualRange(UnorderedStringMultimap& map, const KeyType& key) -{ - return map.equal_range(key); -} -#else -template -using UnorderedStringMap = std::unordered_map; -template -using UnorderedStringMultimap = std::unordered_multimap; -using UnorderedStringSet = std::unordered_set; -using UnorderedStringMultiSet = std::unordered_multiset; - -template -ALWAYS_INLINE typename UnorderedStringMap::const_iterator -UnorderedStringMapFind(const UnorderedStringMap& map, const KeyType& key) -{ - return map.find(std::string(key)); -} -template -ALWAYS_INLINE typename UnorderedStringMap::iterator -UnorderedStringMapFind(UnorderedStringMap& map, const KeyType& key) -{ - return map.find(std::string(key)); -} -template -ALWAYS_INLINE typename UnorderedStringMultimap::const_iterator -UnorderedStringMultiMapFind(const UnorderedStringMultimap& map, const KeyType& key) -{ - return map.find(std::string(key)); -} -template -ALWAYS_INLINE std::pair::const_iterator, - typename UnorderedStringMultimap::const_iterator> -UnorderedStringMultiMapEqualRange(const UnorderedStringMultimap& map, const KeyType& key) -{ - return map.equal_range(std::string(key)); -} -template -ALWAYS_INLINE typename UnorderedStringMultimap::iterator -UnorderedStringMultiMapFind(UnorderedStringMultimap& map, const KeyType& key) -{ - return map.find(std::string(key)); -} -template -ALWAYS_INLINE std::pair::iterator, - typename UnorderedStringMultimap::iterator> -UnorderedStringMultiMapEqualRange(UnorderedStringMultimap& map, const KeyType& key) -{ - return map.equal_range(std::string(key)); -} -#endif - template using StringMap = std::map; template using StringMultiMap = std::multimap; using StringSet = std::set; -using StringMultiSet = std::multiset; +using StringMultiSet = std::multiset; \ No newline at end of file diff --git a/src/common/memory_settings_interface.cpp b/src/common/memory_settings_interface.cpp index e580599a1..2251a5d11 100644 --- a/src/common/memory_settings_interface.cpp +++ b/src/common/memory_settings_interface.cpp @@ -21,7 +21,7 @@ void MemorySettingsInterface::Clear() bool MemorySettingsInterface::GetIntValue(const char* section, const char* key, s32* value) const { - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit == m_sections.end()) return false; @@ -39,11 +39,11 @@ bool MemorySettingsInterface::GetIntValue(const char* section, const char* key, bool MemorySettingsInterface::GetUIntValue(const char* section, const char* key, u32* value) const { - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit == m_sections.end()) return false; - const auto iter = UnorderedStringMultiMapFind(sit->second, key); + const auto iter = sit->second.find(key); if (iter == sit->second.end()) return false; @@ -57,11 +57,11 @@ bool MemorySettingsInterface::GetUIntValue(const char* section, const char* key, bool MemorySettingsInterface::GetFloatValue(const char* section, const char* key, float* value) const { - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit == m_sections.end()) return false; - const auto iter = UnorderedStringMultiMapFind(sit->second, key); + const auto iter = sit->second.find(key); if (iter == sit->second.end()) return false; @@ -75,11 +75,11 @@ bool MemorySettingsInterface::GetFloatValue(const char* section, const char* key bool MemorySettingsInterface::GetDoubleValue(const char* section, const char* key, double* value) const { - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit == m_sections.end()) return false; - const auto iter = UnorderedStringMultiMapFind(sit->second, key); + const auto iter = sit->second.find(key); if (iter == sit->second.end()) return false; @@ -93,11 +93,11 @@ bool MemorySettingsInterface::GetDoubleValue(const char* section, const char* ke bool MemorySettingsInterface::GetBoolValue(const char* section, const char* key, bool* value) const { - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit == m_sections.end()) return false; - const auto iter = UnorderedStringMultiMapFind(sit->second, key); + const auto iter = sit->second.find(key); if (iter == sit->second.end()) return false; @@ -111,11 +111,11 @@ bool MemorySettingsInterface::GetBoolValue(const char* section, const char* key, bool MemorySettingsInterface::GetStringValue(const char* section, const char* key, std::string* value) const { - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit == m_sections.end()) return false; - const auto iter = UnorderedStringMultiMapFind(sit->second, key); + const auto iter = sit->second.find(key); if (iter == sit->second.end()) return false; @@ -156,7 +156,7 @@ void MemorySettingsInterface::SetStringValue(const char* section, const char* ke std::vector> MemorySettingsInterface::GetKeyValueList(const char* section) const { std::vector> output; - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); if (sit != m_sections.end()) { for (const auto& it : sit->second) @@ -168,7 +168,7 @@ std::vector> MemorySettingsInterface::GetKey void MemorySettingsInterface::SetKeyValueList(const char* section, const std::vector>& items) { - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); sit->second.clear(); for (const auto& [key, value] : items) sit->second.emplace(key, value); @@ -176,11 +176,11 @@ void MemorySettingsInterface::SetKeyValueList(const char* section, void MemorySettingsInterface::SetValue(const char* section, const char* key, std::string value) { - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); if (sit == m_sections.end()) sit = m_sections.emplace(std::make_pair(std::string(section), KeyMap())).first; - const auto range = UnorderedStringMultiMapEqualRange(sit->second, key); + const auto range = sit->second.equal_range(key); if (range.first == sit->second.end()) { sit->second.emplace(std::string(key), std::move(value)); @@ -203,10 +203,10 @@ std::vector MemorySettingsInterface::GetStringList(const char* sect { std::vector ret; - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit != m_sections.end()) { - const auto range = UnorderedStringMultiMapEqualRange(sit->second, key); + const auto range = sit->second.equal_range(key); for (auto iter = range.first; iter != range.second; ++iter) ret.emplace_back(iter->second); } @@ -216,11 +216,11 @@ std::vector MemorySettingsInterface::GetStringList(const char* sect void MemorySettingsInterface::SetStringList(const char* section, const char* key, const std::vector& items) { - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); if (sit == m_sections.end()) sit = m_sections.emplace(std::make_pair(std::string(section), KeyMap())).first; - const auto range = UnorderedStringMultiMapEqualRange(sit->second, key); + const auto range = sit->second.equal_range(key); for (auto iter = range.first; iter != range.second;) sit->second.erase(iter++); @@ -231,11 +231,11 @@ void MemorySettingsInterface::SetStringList(const char* section, const char* key bool MemorySettingsInterface::RemoveFromStringList(const char* section, const char* key, const char* item) { - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); if (sit == m_sections.end()) sit = m_sections.emplace(std::make_pair(std::string(section), KeyMap())).first; - const auto range = UnorderedStringMultiMapEqualRange(sit->second, key); + const auto range = sit->second.equal_range(key); bool result = false; for (auto iter = range.first; iter != range.second;) { @@ -255,11 +255,11 @@ bool MemorySettingsInterface::RemoveFromStringList(const char* section, const ch bool MemorySettingsInterface::AddToStringList(const char* section, const char* key, const char* item) { - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); if (sit == m_sections.end()) sit = m_sections.emplace(std::make_pair(std::string(section), KeyMap())).first; - const auto range = UnorderedStringMultiMapEqualRange(sit->second, key); + const auto range = sit->second.equal_range(key); for (auto iter = range.first; iter != range.second; ++iter) { if (iter->second == item) @@ -272,7 +272,7 @@ bool MemorySettingsInterface::AddToStringList(const char* section, const char* k bool MemorySettingsInterface::ContainsValue(const char* section, const char* key) const { - const auto sit = UnorderedStringMapFind(m_sections, section); + const auto sit = m_sections.find(section); if (sit == m_sections.end()) return false; @@ -281,18 +281,18 @@ bool MemorySettingsInterface::ContainsValue(const char* section, const char* key void MemorySettingsInterface::DeleteValue(const char* section, const char* key) { - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); if (sit == m_sections.end()) return; - const auto range = UnorderedStringMultiMapEqualRange(sit->second, key); + const auto range = sit->second.equal_range(key); for (auto iter = range.first; iter != range.second;) sit->second.erase(iter++); } void MemorySettingsInterface::ClearSection(const char* section) { - auto sit = UnorderedStringMapFind(m_sections, section); + auto sit = m_sections.find(section); if (sit == m_sections.end()) return; diff --git a/src/common/memory_settings_interface.h b/src/common/memory_settings_interface.h index c0a3a85f3..55e238f84 100644 --- a/src/common/memory_settings_interface.h +++ b/src/common/memory_settings_interface.h @@ -9,52 +9,52 @@ class MemorySettingsInterface final : public SettingsInterface { public: - MemorySettingsInterface(); - ~MemorySettingsInterface(); + MemorySettingsInterface(); + ~MemorySettingsInterface(); - bool Save() override; + bool Save() override; - void Clear() override; + void Clear() override; - bool GetIntValue(const char* section, const char* key, s32* value) const override; - bool GetUIntValue(const char* section, const char* key, u32* value) const override; - bool GetFloatValue(const char* section, const char* key, float* value) const override; - bool GetDoubleValue(const char* section, const char* key, double* value) const override; - bool GetBoolValue(const char* section, const char* key, bool* value) const override; - bool GetStringValue(const char* section, const char* key, std::string* value) const override; + bool GetIntValue(const char* section, const char* key, s32* value) const override; + bool GetUIntValue(const char* section, const char* key, u32* value) const override; + bool GetFloatValue(const char* section, const char* key, float* value) const override; + bool GetDoubleValue(const char* section, const char* key, double* value) const override; + bool GetBoolValue(const char* section, const char* key, bool* value) const override; + bool GetStringValue(const char* section, const char* key, std::string* value) const override; - void SetIntValue(const char* section, const char* key, s32 value) override; - void SetUIntValue(const char* section, const char* key, u32 value) override; - void SetFloatValue(const char* section, const char* key, float value) override; - void SetDoubleValue(const char* section, const char* key, double value) override; - void SetBoolValue(const char* section, const char* key, bool value) override; - void SetStringValue(const char* section, const char* key, const char* value) override; + void SetIntValue(const char* section, const char* key, s32 value) override; + void SetUIntValue(const char* section, const char* key, u32 value) override; + void SetFloatValue(const char* section, const char* key, float value) override; + void SetDoubleValue(const char* section, const char* key, double value) override; + void SetBoolValue(const char* section, const char* key, bool value) override; + void SetStringValue(const char* section, const char* key, const char* value) override; - std::vector> GetKeyValueList(const char* section) const override; - void SetKeyValueList(const char* section, const std::vector>& items) override; + std::vector> GetKeyValueList(const char* section) const override; + void SetKeyValueList(const char* section, const std::vector>& items) override; - bool ContainsValue(const char* section, const char* key) const override; - void DeleteValue(const char* section, const char* key) override; - void ClearSection(const char* section) override; + bool ContainsValue(const char* section, const char* key) const override; + void DeleteValue(const char* section, const char* key) override; + void ClearSection(const char* section) override; - std::vector GetStringList(const char* section, const char* key) const override; - void SetStringList(const char* section, const char* key, const std::vector& items) override; - bool RemoveFromStringList(const char* section, const char* key, const char* item) override; - bool AddToStringList(const char* section, const char* key, const char* item) override; + std::vector GetStringList(const char* section, const char* key) const override; + void SetStringList(const char* section, const char* key, const std::vector& items) override; + bool RemoveFromStringList(const char* section, const char* key, const char* item) override; + bool AddToStringList(const char* section, const char* key, const char* item) override; - // default parameter overloads - using SettingsInterface::GetBoolValue; - using SettingsInterface::GetDoubleValue; - using SettingsInterface::GetFloatValue; - using SettingsInterface::GetIntValue; - using SettingsInterface::GetStringValue; - using SettingsInterface::GetUIntValue; + // default parameter overloads + using SettingsInterface::GetBoolValue; + using SettingsInterface::GetDoubleValue; + using SettingsInterface::GetFloatValue; + using SettingsInterface::GetIntValue; + using SettingsInterface::GetStringValue; + using SettingsInterface::GetUIntValue; private: - using KeyMap = UnorderedStringMultimap; - using SectionMap = UnorderedStringMap; + using KeyMap = UnorderedStringMultimap; + using SectionMap = UnorderedStringMap; - void SetValue(const char* section, const char* key, std::string value); + void SetValue(const char* section, const char* key, std::string value); - SectionMap m_sections; + SectionMap m_sections; }; \ No newline at end of file diff --git a/src/core/game_database.cpp b/src/core/game_database.cpp index b187f5335..18f5b02e9 100644 --- a/src/core/game_database.cpp +++ b/src/core/game_database.cpp @@ -115,7 +115,7 @@ const GameDatabase::Entry* GameDatabase::GetEntryForId(const std::string_view& c EnsureLoaded(); - auto iter = UnorderedStringMapFind(s_code_lookup, code); + auto iter = s_code_lookup.find(code); return (iter != s_code_lookup.end()) ? &s_entries[iter->second] : nullptr; } @@ -1005,7 +1005,7 @@ bool GameDatabase::ParseJsonCodes(u32 index, const rapidjson::Value& value) } const std::string_view code(current_code.GetString(), current_code.GetStringLength()); - auto iter = UnorderedStringMapFind(s_code_lookup, code); + auto iter = s_code_lookup.find(code); if (iter != s_code_lookup.end()) { Log_WarningPrintf("Duplicate code '%.*s'", static_cast(code.size()), code.data()); diff --git a/src/core/game_list.cpp b/src/core/game_list.cpp index cd55dbf73..814cb3d4b 100644 --- a/src/core/game_list.cpp +++ b/src/core/game_list.cpp @@ -285,7 +285,7 @@ bool GameList::PopulateEntryFromPath(const std::string& path, Entry* entry) bool GameList::GetGameListEntryFromCache(const std::string& path, Entry* entry) { - auto iter = UnorderedStringMapFind(s_cache_map, path); + auto iter = s_cache_map.find(path); if (iter == s_cache_map.end()) return false; @@ -333,7 +333,7 @@ bool GameList::LoadEntriesFromCache(ByteStream* stream) ge.type = static_cast(type); ge.compatibility = static_cast(compatibility_rating); - auto iter = UnorderedStringMapFind(s_cache_map, ge.path); + auto iter = s_cache_map.find(ge.path); if (iter != s_cache_map.end()) iter->second = std::move(ge); else @@ -512,7 +512,7 @@ bool GameList::AddFileFromCache(const std::string& path, std::time_t timestamp, if (!GetGameListEntryFromCache(path, &entry) || entry.last_modified_time != timestamp) return false; - auto iter = UnorderedStringMapFind(played_time_map, entry.serial); + auto iter = played_time_map.find(entry.serial); if (iter != played_time_map.end()) { entry.last_played_time = iter->second.last_played_time; @@ -544,7 +544,7 @@ bool GameList::ScanFile(std::string path, std::time_t timestamp, std::unique_loc Log_WarningPrintf("Failed to write entry '%s' to cache", entry.path.c_str()); } - auto iter = UnorderedStringMapFind(played_time_map, entry.serial); + auto iter = played_time_map.find(entry.serial); if (iter != played_time_map.end()) { entry.last_played_time = iter->second.last_played_time; @@ -821,7 +821,7 @@ GameList::PlayedTimeMap GameList::LoadPlayedTimeMap(const std::string& path) if (!ParsePlayedTimeLine(line, serial, entry)) continue; - if (UnorderedStringMapFind(ret, serial) != ret.end()) + if (ret.find(serial) != ret.end()) { Log_WarningPrintf("Duplicate entry: '%s'", serial.c_str()); continue; diff --git a/src/util/host.cpp b/src/util/host.cpp index 2f49439cd..660c2217a 100644 --- a/src/util/host.cpp +++ b/src/util/host.cpp @@ -44,12 +44,12 @@ std::pair Host::LookupTranslationString(const std::string_view } s_translation_string_mutex.lock_shared(); - ctx_it = UnorderedStringMapFind(s_translation_string_map, context); + ctx_it = s_translation_string_map.find(context); if (UNLIKELY(ctx_it == s_translation_string_map.end())) goto add_string; - msg_it = UnorderedStringMapFind(ctx_it->second, msg); + msg_it = ctx_it->second.find(msg); if (UNLIKELY(msg_it == ctx_it->second.end())) goto add_string;