// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) /** * Provides a map template which doesn't require heap allocations for lookups. */ #pragma once #include "types.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 template using StringMap = std::map; template using StringMultiMap = std::multimap; using StringSet = std::set; using StringMultiSet = std::multiset; #if defined(__cpp_lib_generic_unordered_lookup) && __cpp_lib_generic_unordered_lookup >= 201811L template using UnorderedStringMap = std::unordered_map; template using UnorderedStringMultimap = std::unordered_multimap; using UnorderedStringSet = std::unordered_set; using UnorderedStringMultiSet = std::unordered_multiset; template using PreferUnorderedStringMap = UnorderedStringMap; template using PreferUnorderedStringMultimap = UnorderedStringMultimap; using PreferUnorderedStringSet = UnorderedStringSet; using PreferUnorderedStringMultiSet = UnorderedStringMultiSet; #else #pragma message "__cpp_lib_generic_unordered_lookup is missing, performance will be slower." // GCC 10 doesn't support generic_unordered_lookup... template using PreferUnorderedStringMap = StringMap; template using PreferUnorderedStringMultimap = StringMultiMap; using PreferUnorderedStringSet = StringSet; using PreferUnorderedStringMultiSet = StringMultiSet; #endif