This commit is contained in:
ZZY
2025-04-01 00:13:21 +08:00
parent 2b4857001c
commit 74f43a1ab7
79 changed files with 2271 additions and 2861 deletions

View File

@ -2,11 +2,11 @@
#define INIT_HASH_TABLE_SIZE (32)
void hashtable_init(hash_table_t* ht) {
void init_hashtable(hash_table_t* ht) {
vector_init(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
Assert(ht->key_cmp != NULL && ht->hash_func != NULL);
// Assert(ht->key_cmp != NULL && ht->hash_func != NULL);
}
static int next_power_of_two(int n) {
@ -127,3 +127,14 @@ void hashtable_destory(hash_table_t* ht) {
ht->count = 0;
ht->tombstone_count = 0;
}
void hashtable_foreach(hash_table_t* ht, hash_table_iter_func iter_func, void* context) {
for (rt_size_t i = 0; i < ht->entries.cap; i++) {
hash_entry_t* entry = &vector_at(ht->entries, i);
if (entry->state == ENTRY_ACTIVE) {
if (!iter_func(entry->key, entry->value, context)) {
break; // enable callback function terminal the iter
}
}
}
}

View File

@ -1,6 +1,7 @@
#ifndef __SMCC_HASHTABLE_H__
#define __SMCC_HASHTABLE_H__
#include <lib/core.h>
#include <lib/rt/rt_alloc.h>
#include "vector.h"
@ -29,11 +30,14 @@ typedef struct hash_table {
} hash_table_t;
// WARN you need set hash_func and key_cmp before use
void hashtable_init(hash_table_t* ht) ;
void init_hashtable(hash_table_t* ht) ;
void* hashtable_set(hash_table_t* ht, const void* key, void* value);
void* hashtable_get(hash_table_t* ht, const void* key);
void* hashtable_get(hash_table_t* ht, const void* key);
void* hashtable_del(hash_table_t* ht, const void* key);
void hashtable_destory(hash_table_t* ht);
typedef int (*hash_table_iter_func)(const void* key, void* value, void* context);
void hashtable_foreach(hash_table_t* ht, hash_table_iter_func iter_func, void* context);
#endif // __SMCC_HASHTABLE_H__

View File

@ -15,7 +15,7 @@
do { \
(vec).size = 0, \
(vec).cap = 0, \
(vec).data = NULL; \
(vec).data = 0; \
} while(0)
#define vector_push(vec, value) \