Make sym_table_find return the c-string in question directly
I think we fall into a few traps if we return an sv_t directly. Think intent is clearer by returning the c-string directly.
This commit is contained in:
2
base.h
2
base.h
@@ -77,7 +77,7 @@ typedef struct
|
|||||||
|
|
||||||
u64 djb2(sv_t string);
|
u64 djb2(sv_t string);
|
||||||
void sym_table_init(sym_table_t *table);
|
void sym_table_init(sym_table_t *table);
|
||||||
sv_t sym_table_find(sym_table_t *table, sv_t sv);
|
char *sym_table_find(sym_table_t *table, sv_t sv);
|
||||||
void sym_table_cleanup(sym_table_t *table);
|
void sym_table_cleanup(sym_table_t *table);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
4
main.c
4
main.c
@@ -50,8 +50,8 @@ int main(void)
|
|||||||
|
|
||||||
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
||||||
{
|
{
|
||||||
sv_t sv = sym_table_find(&table, SV(words[i], strlen(words[i])));
|
char *ptr = sym_table_find(&table, SV(words[i], strlen(words[i])));
|
||||||
printf("%s => %p\n", words[i], sv.data);
|
printf("%s => %p\n", words[i], ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
sym_table_cleanup(&table);
|
sym_table_cleanup(&table);
|
||||||
|
|||||||
19
symtable.c
19
symtable.c
@@ -32,7 +32,7 @@ void sym_table_init(sym_table_t *table)
|
|||||||
vec_make((void **)&table->entries, table->capacity * sizeof(*table->entries));
|
vec_make((void **)&table->entries, table->capacity * sizeof(*table->entries));
|
||||||
}
|
}
|
||||||
|
|
||||||
sv_t sym_table_find(sym_table_t *table, sv_t sv)
|
char *sym_table_find(sym_table_t *table, sv_t sv)
|
||||||
{
|
{
|
||||||
// TODO: Deal with resizing this when table->count > table->size / 2
|
// TODO: Deal with resizing this when table->count > table->size / 2
|
||||||
u64 index = djb2(sv) & (table->capacity - 1);
|
u64 index = djb2(sv) & (table->capacity - 1);
|
||||||
@@ -41,21 +41,26 @@ sv_t sym_table_find(sym_table_t *table, sv_t sv)
|
|||||||
index = index & (table->capacity - 1), comp = table->entries[index])
|
index = index & (table->capacity - 1), comp = table->entries[index])
|
||||||
// Is it present in the table?
|
// Is it present in the table?
|
||||||
if (sv.size == comp.size && strncmp(sv.data, comp.data, sv.size) == 0)
|
if (sv.size == comp.size && strncmp(sv.data, comp.data, sv.size) == 0)
|
||||||
return comp;
|
break;
|
||||||
|
|
||||||
// Otherwise we need to duplicate and make it permanently interned
|
// we couldn't find it in our linear search (worst case scenario)
|
||||||
sv_t newsv = sv_copy(sv);
|
if (!table->entries[index].data)
|
||||||
table->entries[index] = newsv;
|
{
|
||||||
++table->count;
|
sv_t newsv = sv_copy(sv);
|
||||||
|
table->entries[index] = newsv;
|
||||||
|
++table->count;
|
||||||
|
}
|
||||||
|
|
||||||
return newsv;
|
return table->entries[index].data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sym_table_cleanup(sym_table_t *table)
|
void sym_table_cleanup(sym_table_t *table)
|
||||||
{
|
{
|
||||||
|
// kill the data
|
||||||
for (u64 i = 0; i < table->capacity; ++i)
|
for (u64 i = 0; i < table->capacity; ++i)
|
||||||
if (table->entries[i].data)
|
if (table->entries[i].data)
|
||||||
free(table->entries[i].data);
|
free(table->entries[i].data);
|
||||||
|
// kill the container
|
||||||
vec_free((void **)&table->entries);
|
vec_free((void **)&table->entries);
|
||||||
memset(table, 0, sizeof(*table));
|
memset(table, 0, sizeof(*table));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user