60 lines
2.4 KiB
C
60 lines
2.4 KiB
C
#ifndef INCLUDED_C_HASHMAP_H
|
|
#define INCLUDED_C_HASHMAP_H
|
|
|
|
#ifndef INCLUDED_C_BASE_H
|
|
#include <c_Base.h>
|
|
#endif /*INCLUDED_C_BASE_H*/
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
typedef struct c_HashMapEntry_t {
|
|
void* key;
|
|
void* value;
|
|
struct c_HashMapEntry_t* next;
|
|
} c_HashMapEntry_t;
|
|
|
|
typedef uint32_t (*c_HashMap_Hash_f)(const void* key, int key_size);
|
|
typedef int (*c_HashMap_Compare_f)(const void* key1, const void* key2, int key_size);
|
|
|
|
typedef struct {
|
|
c_HashMapEntry_t** buckets; // Array of entry linked list head pointers
|
|
c_size_t capacity; // Number of buckets allocated
|
|
c_size_t size; // Number of active key-value pairs stored
|
|
int key_size; // Byte footprint of the key type
|
|
int value_size; // Byte footprint of the value type
|
|
c_HashMap_Hash_f hash; // User hash calculation function
|
|
c_HashMap_Compare_f compare;// User key comparison function
|
|
} c_HashMap_t;
|
|
|
|
typedef struct {
|
|
c_HashMap_t* map; // 繫結的雜湊表
|
|
c_size_t bucket_index; // 當前走訪的桶子索引 (Bucket Index)
|
|
c_HashMapEntry_t** entry; // 指向當前節點指標的指標,用於 O(1) 安全刪除
|
|
} c_HashMapKeyIter_t;
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
|
|
c_err_t c_HashMap_Init(c_HashMap_t* self, int key_size, int value_size, c_size_t initial_capacity,
|
|
c_HashMap_Hash_f hash, c_HashMap_Compare_f compare);
|
|
void c_HashMap_Destroy(c_HashMap_t* self);
|
|
|
|
c_err_t c_HashMap_Put(c_HashMap_t* self, const void* key, const void* value);
|
|
c_err_t c_HashMap_Get(c_HashMap_t* self, const void* key, void* out_value);
|
|
c_err_t c_HashMap_Remove(c_HashMap_t* self, const void* key);
|
|
c_bool_t c_HashMap_Contains(c_HashMap_t* self, const void* key);
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
void c_HashMapKeyIter_Init(c_HashMapKeyIter_t* self, c_HashMap_t* map);
|
|
c_bool_t c_HashMapKeyIter_HasNext(c_HashMapKeyIter_t* self);
|
|
void* c_HashMapKeyIter_Next(c_HashMapKeyIter_t* self);
|
|
void* c_HashMapKeyIter_Get(c_HashMapKeyIter_t* self);
|
|
void c_HashMapKeyIter_Remove(c_HashMapKeyIter_t* self);
|
|
|
|
#endif /*INCLUDED_C_HASHMAP_H*/
|