util/IntrusiveHashSet: add template argument GetKey

This is a big simplification of all IntrusiveHashSet users: instead of
having to implement multiple overloads of Hash and Equal, the
IntrusiveHashSet class can first extract the effective key from an
item and then pass it to the Hash/Equal functions.
This commit is contained in:
Max Kellermann
2023-08-02 22:04:00 +02:00
committed by Max Kellermann
parent 78801f303e
commit 83a6cb804b
4 changed files with 33 additions and 81 deletions

View File

@@ -52,26 +52,10 @@ class RemoteTagCache final {
void OnRemoteTag(Tag &&tag) noexcept override;
void OnRemoteTagError(std::exception_ptr e) noexcept override;
struct Hash : std::hash<std::string> {
using std::hash<std::string>::operator();
struct GetUri {
[[gnu::pure]]
std::size_t operator()(const Item &item) const noexcept {
return std::hash<std::string>::operator()(item.uri);
}
};
struct Equal {
[[gnu::pure]]
bool operator()(const Item &a,
const Item &b) const noexcept {
return a.uri == b.uri;
}
[[gnu::pure]]
bool operator()(const std::string &a,
const Item &b) const noexcept {
return a == b.uri;
std::string_view operator()(const Item &item) const noexcept {
return item.uri;
}
};
};
@@ -101,7 +85,9 @@ class RemoteTagCache final {
ItemList invoke_list;
IntrusiveHashSet<Item, 127,
IntrusiveHashSetOperators<Item::Hash, Item::Equal>,
IntrusiveHashSetOperators<std::hash<std::string_view>,
std::equal_to<std::string_view>,
Item::GetUri>,
IntrusiveHashSetBaseHookTraits<Item>,
true> map;