79 lines
3.7 KiB
C
79 lines
3.7 KiB
C
#ifndef INCLUDED_C_SEPARATECHAININGHASHST_H
|
|
#define INCLUDED_C_SEPARATECHAININGHASHST_H
|
|
|
|
#ifndef INCLUDED_C_TYPES_H
|
|
#include <c_Types.h>
|
|
#endif /*INCLUDED_C_TYPES_H*/
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
// Forward declaration of internal node structure
|
|
typedef struct c_SCHashNode {
|
|
struct c_SCHashNode* next; // Pointer to next node in the chain
|
|
// Payload layout: key block followed immediately by the value block in memory
|
|
} c_SCHashNode_t;
|
|
|
|
// Separate Chaining Hash ST Context Structure
|
|
typedef struct {
|
|
c_SCHashNode_t** buckets; // Array of linked list head pointers
|
|
c_size_t num_buckets; // Total number of buckets (M)
|
|
c_size_t key_size; // Size of each key in bytes
|
|
c_size_t val_size; // Size of each value in bytes
|
|
c_size_t size; // Total number of key-value pairs (N)
|
|
|
|
uint32_t (*hash_fn)(const void* key, c_size_t key_size); // Custom hash function
|
|
int (*key_compar)(const void*, const void*); // Key comparison rule pointer
|
|
} c_SeparateChainingHashST_t;
|
|
|
|
typedef struct {
|
|
c_SeparateChainingHashST_t* st; // Reference link to backing hash table container
|
|
c_size_t curr_bucket; // Active index tracking variable inside the flat array
|
|
c_SCHashNode_t* curr_node; // Head cursor tracking elements inside linked list buckets
|
|
void* last_returned; // Pointer caching the key payload returned by Next()
|
|
} c_SeparateChainingHashSTKeyIter_t;
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
// --- Internal Helper Accessors ---
|
|
C_STATIC_FORCE_INLINE
|
|
void* c_SCHash_NodeKey(c_SCHashNode_t* node) {
|
|
if (node == NULL) return NULL;
|
|
return (void*)((char*)node + sizeof(c_SCHashNode_t));
|
|
}
|
|
|
|
C_STATIC_FORCE_INLINE
|
|
void* c_SCHash_NodeVal(c_SCHashNode_t* node, c_size_t key_size) {
|
|
if (!node) return NULL;
|
|
return (void*)((char*)node + sizeof(c_SCHashNode_t) + key_size);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
c_err_t c_SeparateChainingHashST_Init(c_SeparateChainingHashST_t* st, c_size_t num_buckets,
|
|
c_size_t key_size, c_size_t val_size,
|
|
uint32_t (*hash_fn)(const void*, c_size_t),
|
|
int (*key_compar)(const void*, const void*));
|
|
|
|
void c_SeparateChainingHashST_Destroy(c_SeparateChainingHashST_t* st);
|
|
|
|
c_bool_t c_SeparateChainingHashST_Contains(const c_SeparateChainingHashST_t* st, const void* key);
|
|
c_err_t c_SeparateChainingHashST_Put(c_SeparateChainingHashST_t* st, const void* key, const void* val);
|
|
void* c_SeparateChainingHashST_Get(const c_SeparateChainingHashST_t* st, const void* key);
|
|
c_err_t c_SeparateChainingHashST_Delete(c_SeparateChainingHashST_t* st, const void* key);
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
c_err_t c_SeparateChainingHashSTKeyIter_Init(c_SeparateChainingHashSTKeyIter_t* iter,
|
|
const c_SeparateChainingHashST_t* st);
|
|
void c_SeparateChainingHashSTKeyIter_Destroy(c_SeparateChainingHashSTKeyIter_t* iter);
|
|
c_bool_t c_SeparateChainingHashSTKeyIter_HasNext(const c_SeparateChainingHashSTKeyIter_t* iter);
|
|
void* c_SeparateChainingHashSTKeyIter_Get(c_SeparateChainingHashSTKeyIter_t* iter);
|
|
void* c_SeparateChainingHashSTKeyIter_Next(c_SeparateChainingHashSTKeyIter_t* iter);
|
|
c_err_t c_SeparateChainingHashSTKeyIter_Remove(c_SeparateChainingHashSTKeyIter_t* iter) ;
|
|
|
|
#endif /*INCLUDED_C_SEPARATECHAININGHASHST_H*/
|