Add hash set implementation
Release / release (push) Successful in 4s

This commit is contained in:
2026-09-23 22:34:23 +01:00
parent 20e890964f
commit 68640dc5e2
12 changed files with 590 additions and 44 deletions
+105
View File
@@ -0,0 +1,105 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef SET_H
#define SET_H
#include "../table/table.h"
#include "../../../common/misc/misc_utils.h"
#include "../../../common/platform/platform.h"
#ifdef WP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP
typedef WpHashTable WpHashSet;
#ifdef WP_PLATFORM_CPP
#define wpHashSet(TYPE, U8_STREAM_ENCODER, ITEM_EQ_TEST, CAPACITY) \
(WpHashSet{ \
WP_HASH_TABLE_DEFAULT_HASHER, \
U8_STREAM_ENCODER, \
ITEM_EQ_TEST, \
wpArrayWithCapacity(TYPE, wpMiscUtilsU64RoundUpPow2(CAPACITY), WP_ARRAY_INIT_NONE), \
NULL, \
wpArrayWithCapacity(WpHashBucket, wpMiscUtilsU64RoundUpPow2(CAPACITY), WP_ARRAY_INIT_FILLED), \
WP_HASH_TABLE_MAGIC, \
wpMiscUtilsU64RoundUpPow2(CAPACITY) - 1, \
0.0f, \
false, \
})
#define wpHashSetCustomHasher(TYPE, HASHER, U8_STREAM_ENCODER, ITEM_EQ_TEST, CAPACITY) \
(WpHashSet{ \
HASHER, \
U8_STREAM_ENCODER, \
ITEM_EQ_TEST, \
wpArrayWithCapacity(TYPE, wpMiscUtilsU64RoundUpPow2(CAPACITY), WP_ARRAY_INIT_NONE), \
NULL, \
wpArrayWithCapacity(WpHashBucket, wpMiscUtilsU64RoundUpPow2(CAPACITY), WP_ARRAY_INIT_FILLED), \
WP_HASH_TABLE_MAGIC, \
wpMiscUtilsU64RoundUpPow2(CAPACITY) - 1, \
0.0f, \
false, \
})
#else
#define wpHashSet(TYPE, U8_STREAM_ENCODER, ITEM_EQ_TEST, CAPACITY) \
((WpHashSet){ \
.hasher = WP_HASH_TABLE_DEFAULT_HASHER, \
.encoder = U8_STREAM_ENCODER, \
.key_eq = ITEM_EQ_TEST, \
.keys = wpArrayWithCapacity(TYPE, wpMiscUtilsU64RoundUpPow2(CAPACITY), \
WP_ARRAY_INIT_NONE), \
.buckets = wpArrayWithCapacity(WpHashBucket, wpMiscUtilsU64RoundUpPow2(CAPACITY), \
WP_ARRAY_INIT_FILLED), \
.magic = WP_HASH_TABLE_MAGIC, \
.bucket_mask = wpMiscUtilsU64RoundUpPow2(CAPACITY) - 1, \
})
#define wpHashSetCustomHasher(TYPE, HASHER, U8_STREAM_ENCODER, ITEM_EQ_TEST, CAPACITY) \
((WpHashSet){ \
.hasher = HASHER, \
.encoder = U8_STREAM_ENCODER, \
.key_eq = ITEM_EQ_TEST, \
.keys = wpArrayWithCapacity(TYPE, wpMiscUtilsU64RoundUpPow2(CAPACITY), \
WP_ARRAY_INIT_NONE), \
.buckets = wpArrayWithCapacity(WpHashBucket, wpMiscUtilsU64RoundUpPow2(CAPACITY), \
WP_ARRAY_INIT_FILLED), \
.magic = WP_HASH_TABLE_MAGIC, \
.bucket_mask = wpMiscUtilsU64RoundUpPow2(CAPACITY) - 1, \
})
#endif
#define wpHashSetAlloc(TYPE, ALLOCATOR_PTR, U8_STREAM_ENCODER, ITEM_EQ_TEST, CAPACITY) \
hashSetAlloc(ALLOCATOR_PTR, U8_STREAM_ENCODER, ITEM_EQ_TEST, CAPACITY, sizeof(TYPE))
#define wpHashSetCustomHasherAlloc(TYPE, ALLOCATOR_PTR, HASHER, U8_STREAM_ENCODER, ITEM_EQ_TEST, \
CAPACITY) \
hashSetAllocWithCustomHasher(ALLOCATOR_PTR, HASHER, U8_STREAM_ENCODER, ITEM_EQ_TEST, CAPACITY, \
sizeof(TYPE))
#define wpHashSetFind(TYPE, SET_PTR, ITEM_PTR) hashSetFind(SET_PTR, ITEM_PTR, sizeof(TYPE))
#define wpHashSetInsertCapped(TYPE, SET_PTR, ITEM_PTR) \
hashSetInsertCapped(SET_PTR, ITEM_PTR, sizeof(TYPE))
#define wpHashSetInsertAlloc(TYPE, ALLOCATOR_PTR, SET_PTR, ITEM_PTR) \
hashSetInsertAlloc(ALLOCATOR_PTR, SET_PTR, ITEM_PTR, sizeof(TYPE))
#define wpHashSetRemove(TYPE, SET_PTR, ITEM_PTR) hashSetRemove(SET_PTR, ITEM_PTR, sizeof(TYPE))
#define wpHashSetItems(TYPE, SET_PTR) ((TYPE *)hashTableKeys(SET_PTR, sizeof(TYPE)))
#define wpHashSetCapacity(SET_PTR) hashTableCapacity(SET_PTR)
#define wpHashSetCount(SET_PTR) hashTableCount(SET_PTR)
#define wpHashSetItemSize(SET_PTR) hashTableKeySize(SET_PTR)
#define wpHashSetClear(TYPE, SET_PTR) hashTableClear(SET_PTR, sizeof(TYPE), 0)
#define wpHashSetDealloc(TYPE, ALLOCATOR_PTR, SET_PTR) \
hashTableDealloc(ALLOCATOR_PTR, SET_PTR, sizeof(TYPE), 0)
WpHashSet hashSetAlloc(const WpAllocator *allocator, WpU8StreamEncoder encoder,
WpKeyEqualTest key_eq, u64 capacity, u64 key_size);
WpHashSet hashSetAllocWithCustomHasher(const WpAllocator *allocator, WpHasher hasher,
WpU8StreamEncoder encoder, WpKeyEqualTest key_eq,
u64 capacity, u64 key_size);
WpHashLookupResult hashSetFind(WpHashSet *set, void *item, u64 item_size);
void hashSetInsertCapped(WpHashSet *set, void *item, u64 item_size);
void hashSetInsertAlloc(const WpAllocator *allocator, WpHashSet *set, void *item,
u64 item_size);
void hashSetRemove(WpHashSet *set, void *item, u64 item_size);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WP_PLATFORM_CPP
#endif // !SET_H