feat: add SCF format library and rename components to SCC prefix
- Introduce new SCF (SCC Format) library with header, implementation, and test files - SCF is a minimal executable/linkable format focused on internal linking with external symbol import/export abstraction - Rename lexer and lex_parser packages from 'smcc_' to 'scc_' prefix for consistency - Update hashmap implementation to use 'scc_' prefix for types and structures - Add build configuration for new format library with dependencies on libcore and libutils
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#define SCC_INIT_HASHMAP_SIZE (32)
|
||||
#endif
|
||||
|
||||
void hashmap_init(hashmap_t *ht) {
|
||||
void scc_hashtable_init(scc_hashtable_t *ht) {
|
||||
scc_vec_init(ht->entries);
|
||||
ht->count = 0;
|
||||
ht->tombstone_count = 0;
|
||||
@@ -21,17 +21,18 @@ static int next_power_of_two(int n) {
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
static hashmap_entry_t *find_entry(hashmap_t *ht, const void *key, u32 hash) {
|
||||
static scc_hashtable_entry_t *find_entry(scc_hashtable_t *ht, const void *key,
|
||||
u32 hash) {
|
||||
if (ht->entries.cap == 0)
|
||||
return NULL;
|
||||
|
||||
u32 index = hash & (ht->entries.cap - 1); // 容量是2的幂
|
||||
u32 probe = 0;
|
||||
|
||||
hashmap_entry_t *tombstone = NULL;
|
||||
scc_hashtable_entry_t *tombstone = NULL;
|
||||
|
||||
while (1) {
|
||||
hashmap_entry_t *entry = &scc_vec_at(ht->entries, index);
|
||||
scc_hashtable_entry_t *entry = &scc_vec_at(ht->entries, index);
|
||||
if (entry->state == ENTRY_EMPTY) {
|
||||
return tombstone ? tombstone : entry;
|
||||
}
|
||||
@@ -53,25 +54,27 @@ static hashmap_entry_t *find_entry(hashmap_t *ht, const void *key, u32 hash) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void adjust_capacity(hashmap_t *ht, int new_cap) {
|
||||
static void adjust_capacity(scc_hashtable_t *ht, usize new_cap) {
|
||||
new_cap = next_power_of_two(new_cap);
|
||||
Assert(new_cap >= ht->entries.cap);
|
||||
|
||||
SCC_VEC(hashmap_entry_t) old_entries;
|
||||
SCC_VEC(scc_hashtable_entry_t) old_entries;
|
||||
old_entries.data = ht->entries.data;
|
||||
old_entries.cap = ht->entries.cap;
|
||||
|
||||
// Not used size but for gdb python extention debug
|
||||
ht->entries.size = new_cap;
|
||||
ht->entries.cap = new_cap;
|
||||
ht->entries.data = scc_realloc(NULL, new_cap * sizeof(hashmap_entry_t));
|
||||
scc_memset(ht->entries.data, 0, new_cap * sizeof(hashmap_entry_t));
|
||||
ht->entries.data =
|
||||
scc_realloc(NULL, new_cap * sizeof(scc_hashtable_entry_t));
|
||||
scc_memset(ht->entries.data, 0, new_cap * sizeof(scc_hashtable_entry_t));
|
||||
|
||||
// rehash the all of the old data
|
||||
for (usize i = 0; i < old_entries.cap; i++) {
|
||||
hashmap_entry_t *entry = &scc_vec_at(old_entries, i);
|
||||
scc_hashtable_entry_t *entry = &scc_vec_at(old_entries, i);
|
||||
if (entry->state == ENTRY_ACTIVE) {
|
||||
hashmap_entry_t *dest = find_entry(ht, entry->key, entry->hash);
|
||||
scc_hashtable_entry_t *dest =
|
||||
find_entry(ht, entry->key, entry->hash);
|
||||
*dest = *entry;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +83,7 @@ static void adjust_capacity(hashmap_t *ht, int new_cap) {
|
||||
ht->tombstone_count = 0;
|
||||
}
|
||||
|
||||
void *hashmap_set(hashmap_t *ht, const void *key, void *value) {
|
||||
void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value) {
|
||||
if (ht->count + ht->tombstone_count >= ht->entries.cap * 0.75) {
|
||||
int new_cap = ht->entries.cap < SCC_INIT_HASHMAP_SIZE
|
||||
? SCC_INIT_HASHMAP_SIZE
|
||||
@@ -89,7 +92,7 @@ void *hashmap_set(hashmap_t *ht, const void *key, void *value) {
|
||||
}
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
hashmap_entry_t *entry = find_entry(ht, key, hash);
|
||||
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
|
||||
|
||||
void *old_value = NULL;
|
||||
if (entry->state == ENTRY_ACTIVE) {
|
||||
@@ -107,21 +110,21 @@ void *hashmap_set(hashmap_t *ht, const void *key, void *value) {
|
||||
return old_value;
|
||||
}
|
||||
|
||||
void *hashmap_get(hashmap_t *ht, const void *key) {
|
||||
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key) {
|
||||
if (ht->entries.cap == 0)
|
||||
return NULL;
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
hashmap_entry_t *entry = find_entry(ht, key, hash);
|
||||
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
|
||||
return (entry && entry->state == ENTRY_ACTIVE) ? entry->value : NULL;
|
||||
}
|
||||
|
||||
void *hashmap_del(hashmap_t *ht, const void *key) {
|
||||
void *scc_hashtable_del(scc_hashtable_t *ht, const void *key) {
|
||||
if (ht->entries.cap == 0)
|
||||
return NULL;
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
hashmap_entry_t *entry = find_entry(ht, key, hash);
|
||||
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
|
||||
|
||||
if (entry == NULL || entry->state != ENTRY_ACTIVE)
|
||||
return NULL;
|
||||
@@ -133,15 +136,16 @@ void *hashmap_del(hashmap_t *ht, const void *key) {
|
||||
return value;
|
||||
}
|
||||
|
||||
void hashmap_drop(hashmap_t *ht) {
|
||||
void scc_hashtable_drop(scc_hashtable_t *ht) {
|
||||
scc_vec_free(ht->entries);
|
||||
ht->count = 0;
|
||||
ht->tombstone_count = 0;
|
||||
}
|
||||
|
||||
void hashmap_foreach(hashmap_t *ht, hashmap_iter_fn iter_func, void *context) {
|
||||
void scc_hashmap_foreach(scc_hashtable_t *ht, scc_hashtable_iter_fn iter_func,
|
||||
void *context) {
|
||||
for (usize i = 0; i < ht->entries.cap; i++) {
|
||||
hashmap_entry_t *entry = &scc_vec_at(ht->entries, i);
|
||||
scc_hashtable_entry_t *entry = &scc_vec_at(ht->entries, i);
|
||||
if (entry->state == ENTRY_ACTIVE) {
|
||||
if (!iter_func(entry->key, entry->value, context)) {
|
||||
break; // enable callback function terminal the iter
|
||||
|
||||
Reference in New Issue
Block a user