Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/common/heterogeneous_containers.h
4211 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
/**
5
* Provides a map template which doesn't require heap allocations for lookups.
6
*/
7
8
#pragma once
9
10
#include "types.h"
11
#include <map>
12
#include <set>
13
#include <string>
14
#include <unordered_map>
15
#include <unordered_set>
16
#include <version>
17
18
namespace detail {
19
struct transparent_string_hash
20
{
21
using is_transparent = void;
22
23
std::size_t operator()(const std::string_view& v) const { return std::hash<std::string_view>{}(v); }
24
std::size_t operator()(const std::string& s) const { return std::hash<std::string>{}(s); }
25
std::size_t operator()(const char* s) const { return operator()(std::string_view(s)); }
26
};
27
28
struct transparent_string_equal
29
{
30
using is_transparent = void;
31
32
bool operator()(const std::string& lhs, const std::string_view& rhs) const { return lhs == rhs; }
33
bool operator()(const std::string& lhs, const std::string& rhs) const { return lhs == rhs; }
34
bool operator()(const std::string& lhs, const char* rhs) const { return lhs == rhs; }
35
bool operator()(const std::string_view& lhs, const std::string& rhs) const { return lhs == rhs; }
36
bool operator()(const char* lhs, const std::string& rhs) const { return lhs == rhs; }
37
};
38
39
struct transparent_string_less
40
{
41
using is_transparent = void;
42
43
bool operator()(const std::string& lhs, const std::string_view& rhs) const { return lhs < rhs; }
44
bool operator()(const std::string& lhs, const std::string& rhs) const { return lhs < rhs; }
45
bool operator()(const std::string& lhs, const char* rhs) const { return lhs < rhs; }
46
bool operator()(const std::string_view& lhs, const std::string& rhs) const { return lhs < rhs; }
47
bool operator()(const char* lhs, const std::string& rhs) const { return lhs < rhs; }
48
};
49
} // namespace detail
50
51
template<typename ValueType>
52
using StringMap = std::map<std::string, ValueType, detail::transparent_string_less>;
53
template<typename ValueType>
54
using StringMultiMap = std::multimap<std::string, ValueType, detail::transparent_string_less>;
55
using StringSet = std::set<std::string, detail::transparent_string_less>;
56
using StringMultiSet = std::multiset<std::string, detail::transparent_string_less>;
57
58
#if defined(__cpp_lib_generic_unordered_lookup) && __cpp_lib_generic_unordered_lookup >= 201811L
59
template<typename ValueType>
60
using UnorderedStringMap =
61
std::unordered_map<std::string, ValueType, detail::transparent_string_hash, detail::transparent_string_equal>;
62
template<typename ValueType>
63
using UnorderedStringMultimap =
64
std::unordered_multimap<std::string, ValueType, detail::transparent_string_hash, detail::transparent_string_equal>;
65
using UnorderedStringSet =
66
std::unordered_set<std::string, detail::transparent_string_hash, detail::transparent_string_equal>;
67
using UnorderedStringMultiSet =
68
std::unordered_multiset<std::string, detail::transparent_string_hash, detail::transparent_string_equal>;
69
70
template<typename ValueType>
71
using PreferUnorderedStringMap = UnorderedStringMap<ValueType>;
72
template<typename ValueType>
73
using PreferUnorderedStringMultimap = UnorderedStringMultimap<ValueType>;
74
using PreferUnorderedStringSet = UnorderedStringSet;
75
using PreferUnorderedStringMultiSet = UnorderedStringMultiSet;
76
#else
77
78
#pragma message "__cpp_lib_generic_unordered_lookup is missing, performance will be slower."
79
80
// GCC 10 doesn't support generic_unordered_lookup...
81
template<typename ValueType>
82
using PreferUnorderedStringMap = StringMap<ValueType>;
83
template<typename ValueType>
84
using PreferUnorderedStringMultimap = StringMultiMap<ValueType>;
85
using PreferUnorderedStringSet = StringSet;
86
using PreferUnorderedStringMultiSet = StringMultiSet;
87
88
#endif
89
90