/* PCSX2 - PS2 Emulator for PCs * Copyright (C) 2002-2010 PCSX2 Dev Team * * PCSX2 is free software: you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Found- * ation, either version 3 of the License, or (at your option) any later version. * * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with PCSX2. * If not, see . */ /** * Provides a map template which doesn't require heap allocations for lookups. */ #pragma once #include "Pcsx2Defs.h" #include #include #include #include #include namespace detail { struct transparent_string_hash { using is_transparent = void; std::size_t operator()(const std::string_view& v) const { return std::hash{}(v); } std::size_t operator()(const std::string& s) const { return std::hash{}(s); } std::size_t operator()(const char* s) const { return operator()(std::string_view(s)); } }; struct transparent_string_equal { using is_transparent = void; bool operator()(const std::string& lhs, const std::string_view& rhs) const { return lhs == rhs; } bool operator()(const std::string& lhs, const std::string& rhs) const { return lhs == rhs; } bool operator()(const std::string& lhs, const char* rhs) const { return lhs == rhs; } bool operator()(const std::string_view& lhs, const std::string& rhs) const { return lhs == rhs; } bool operator()(const char* lhs, const std::string& rhs) const { return lhs == rhs; } }; struct transparent_string_less { using is_transparent = void; bool operator()(const std::string& lhs, const std::string_view& rhs) const { return lhs < rhs; } bool operator()(const std::string& lhs, const std::string& rhs) const { return lhs < rhs; } bool operator()(const std::string& lhs, const char* rhs) const { return lhs < rhs; } bool operator()(const std::string_view& lhs, const std::string& rhs) const { return lhs < rhs; } bool operator()(const char* lhs, const std::string& rhs) const { return lhs < rhs; } }; } // 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; template using UnorderedStringMultimap = std::unordered_multimap; using UnorderedStringSet = std::unordered_set; using UnorderedStringMultiSet = std::unordered_multiset; template __fi typename UnorderedStringMap::const_iterator UnorderedStringMapFind(const UnorderedStringMap& map, const KeyType& key) { return map.find(key); } template __fi typename UnorderedStringMap::iterator UnorderedStringMapFind(UnorderedStringMap& map, const KeyType& key) { return map.find(key); } template __fi typename UnorderedStringMultimap::const_iterator UnorderedStringMultiMapFind(const UnorderedStringMultimap& map, const KeyType& key) { return map.find(key); } template __fi std::pair::const_iterator, typename UnorderedStringMultimap::const_iterator> UnorderedStringMultiMapEqualRange(const UnorderedStringMultimap& map, const KeyType& key) { return map.equal_range(key); } template __fi typename UnorderedStringMultimap::iterator UnorderedStringMultiMapFind(UnorderedStringMultimap& map, const KeyType& key) { return map.find(key); } template __fi 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 __fi typename UnorderedStringMap::const_iterator UnorderedStringMapFind(const UnorderedStringMap& map, const KeyType& key) { return map.find(std::string(key)); } template __fi typename UnorderedStringMap::iterator UnorderedStringMapFind(UnorderedStringMap& map, const KeyType& key) { return map.find(std::string(key)); } template __fi typename UnorderedStringMultimap::const_iterator UnorderedStringMultiMapFind(const UnorderedStringMultimap& map, const KeyType& key) { return map.find(std::string(key)); } template __fi std::pair::const_iterator, typename UnorderedStringMultimap::const_iterator> UnorderedStringMultiMapEqualRange(const UnorderedStringMultimap& map, const KeyType& key) { return map.equal_range(std::string(key)); } template __fi typename UnorderedStringMultimap::iterator UnorderedStringMultiMapFind(UnorderedStringMultimap& map, const KeyType& key) { return map.find(std::string(key)); } template __fi 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;