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:
committed by
Max Kellermann
parent
78801f303e
commit
83a6cb804b
33
src/input/cache/Manager.cxx
vendored
33
src/input/cache/Manager.cxx
vendored
@@ -11,37 +11,10 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
inline std::size_t
|
||||
InputCacheManager::ItemHash::operator()(std::string_view uri) const noexcept
|
||||
inline std::string_view
|
||||
InputCacheManager::ItemGetUri::operator()(const InputCacheItem &item) const noexcept
|
||||
{
|
||||
return std::hash<std::string_view>{}(uri);
|
||||
}
|
||||
|
||||
inline std::size_t
|
||||
InputCacheManager::ItemHash::operator()(const InputCacheItem &item) const noexcept
|
||||
{
|
||||
return std::hash<std::string_view>{}(item.GetUri());
|
||||
}
|
||||
|
||||
inline bool
|
||||
InputCacheManager::ItemEqual::operator()(const InputCacheItem &a,
|
||||
std::string_view b) const noexcept
|
||||
{
|
||||
return a.GetUri() == b;
|
||||
}
|
||||
|
||||
inline bool
|
||||
InputCacheManager::ItemEqual::operator()(std::string_view a,
|
||||
const InputCacheItem &b) const noexcept
|
||||
{
|
||||
return a == b.GetUri();
|
||||
}
|
||||
|
||||
inline bool
|
||||
InputCacheManager::ItemEqual::operator()(const InputCacheItem &a,
|
||||
const InputCacheItem &b) const noexcept
|
||||
{
|
||||
return a.GetUri() == b.GetUri();
|
||||
return item.GetUri();
|
||||
}
|
||||
|
||||
InputCacheManager::InputCacheManager(const InputCacheConfig &config) noexcept
|
||||
|
||||
25
src/input/cache/Manager.hxx
vendored
25
src/input/cache/Manager.hxx
vendored
@@ -24,32 +24,17 @@ class InputCacheManager {
|
||||
|
||||
size_t total_size = 0;
|
||||
|
||||
struct ItemHash {
|
||||
struct ItemGetUri {
|
||||
[[gnu::pure]]
|
||||
std::size_t operator()(std::string_view uri) const noexcept;
|
||||
|
||||
[[gnu::pure]]
|
||||
std::size_t operator()(const InputCacheItem &item) const noexcept;
|
||||
};
|
||||
|
||||
struct ItemEqual {
|
||||
[[gnu::pure]]
|
||||
bool operator()(const InputCacheItem &a,
|
||||
std::string_view b) const noexcept;
|
||||
|
||||
[[gnu::pure]]
|
||||
bool operator()(std::string_view a,
|
||||
const InputCacheItem &b) const noexcept;
|
||||
|
||||
[[gnu::pure]]
|
||||
bool operator()(const InputCacheItem &a,
|
||||
const InputCacheItem &b) const noexcept;
|
||||
std::string_view operator()(const InputCacheItem &item) const noexcept;
|
||||
};
|
||||
|
||||
IntrusiveList<InputCacheItem> items_by_time;
|
||||
|
||||
IntrusiveHashSet<InputCacheItem, 127,
|
||||
IntrusiveHashSetOperators<ItemHash, ItemEqual>> items_by_uri;
|
||||
IntrusiveHashSetOperators<std::hash<std::string_view>,
|
||||
std::equal_to<std::string_view>,
|
||||
ItemGetUri>> items_by_uri;
|
||||
|
||||
public:
|
||||
explicit InputCacheManager(const InputCacheConfig &config) noexcept;
|
||||
|
||||
Reference in New Issue
Block a user