util/IntrusiveHashSet: add concept checks to *Operators

This requires adding another template argument and reordering the others.
This commit is contained in:
Max Kellermann 2024-04-02 17:37:31 +02:00 committed by Max Kellermann
parent 91ca502e10
commit 6a99f20828
5 changed files with 25 additions and 19 deletions

View File

@ -85,9 +85,9 @@ class RemoteTagCache final {
IntrusiveHashSet<
Item, 127,
IntrusiveHashSetOperators<std::hash<std::string_view>,
std::equal_to<std::string_view>,
Item::GetUri>,
IntrusiveHashSetOperators<Item, Item::GetUri,
std::hash<std::string_view>,
std::equal_to<std::string_view>>,
IntrusiveHashSetBaseHookTraits<Item>,
IntrusiveHashSetOptions{.constant_time_size = true}> map;

View File

@ -32,9 +32,9 @@ class InputCacheManager {
IntrusiveList<InputCacheItem> items_by_time;
IntrusiveHashSet<InputCacheItem, 127,
IntrusiveHashSetOperators<std::hash<std::string_view>,
std::equal_to<std::string_view>,
ItemGetUri>> items_by_uri;
IntrusiveHashSetOperators<InputCacheItem, ItemGetUri,
std::hash<std::string_view>,
std::equal_to<std::string_view>>> items_by_uri;
public:
explicit InputCacheManager(const InputCacheConfig &config) noexcept;

View File

@ -78,9 +78,9 @@ TagPoolItem::Create(TagType type,
}
static IntrusiveHashSet<TagPoolItem, 16384,
IntrusiveHashSetOperators<TagPoolKey::Hash,
std::equal_to<TagPoolKey>,
TagPoolItem::GetKey>,
IntrusiveHashSetOperators<TagPoolItem, TagPoolItem::GetKey,
TagPoolKey::Hash,
std::equal_to<TagPoolKey>>,
IntrusiveHashSetMemberHookTraits<&TagPoolItem::hash_set_hook>,
IntrusiveHashSetOptions{.zero_initialized = true}> tag_pool;

View File

@ -8,6 +8,7 @@
#include <algorithm> // for std::all_of()
#include <array>
#include <concepts> // for std::regular_invocable
#include <numeric> // for std::accumulate()
struct IntrusiveHashSetOptions {
@ -94,8 +95,11 @@ struct IntrusiveHashSetMemberHookTraits {
* @param GetKey a function object which extracts the "key" part of an
* item
*/
template<typename Hash, typename Equal,
typename GetKey=std::identity>
template<typename T,
std::regular_invocable<const T &> GetKey,
std::regular_invocable<std::invoke_result_t<GetKey, const T &>> Hash,
std::predicate<std::invoke_result_t<GetKey, const T &>,
std::invoke_result_t<GetKey, const T &>> Equal>
struct IntrusiveHashSetOperators {
using hasher = Hash;
using key_equal = Equal;

View File

@ -39,7 +39,8 @@ TEST(IntrusiveHashSet, Basic)
IntItem a{1}, b{2}, c{3}, d{4}, e{5}, f{1};
IntrusiveHashSet<IntItem, 3,
IntrusiveHashSetOperators<IntItem::Hash,
IntrusiveHashSetOperators<IntItem, std::identity,
IntItem::Hash,
IntItem::Equal>> set;
{
@ -124,7 +125,8 @@ TEST(IntrusiveHashSet, Multi)
IntItem a{1}, b{2}, c{3}, d{4}, e{5}, f{1};
IntrusiveHashSet<IntItem, 3,
IntrusiveHashSetOperators<IntItem::Hash,
IntrusiveHashSetOperators<IntItem, std::identity,
IntItem::Hash,
IntItem::Equal>> set;
set.insert(a);
@ -179,15 +181,15 @@ TEST(IntrusiveHashSet, Tag)
TaggedItem one{1, 11}, two{2, 22};
IntrusiveHashSet<TaggedItem, 3,
IntrusiveHashSetOperators<std::hash<int>,
std::equal_to<int>,
GetA>,
IntrusiveHashSetOperators<TaggedItem, GetA,
std::hash<int>,
std::equal_to<int>>,
IntrusiveHashSetBaseHookTraits<TaggedItem, A>> a;
IntrusiveHashSet<TaggedItem, 3,
IntrusiveHashSetOperators<std::hash<int>,
std::equal_to<int>,
GetB>,
IntrusiveHashSetOperators<TaggedItem, GetB,
std::hash<int>,
std::equal_to<int>>,
IntrusiveHashSetBaseHookTraits<TaggedItem, B>> b;
EXPECT_TRUE(a.empty());