Make VEC_GET take an index along with the type
Since most use cases require indexing the data directly, and the macro implies you're retrieving data from it, may as well take the index. If you wanted a pointer to that data, &VEC_GET(vec, index, type) works just fine.
This commit is contained in:
4
alisp.h
4
alisp.h
@@ -76,8 +76,8 @@ typedef struct Vector
|
||||
|
||||
static_assert(sizeof(vec_t) == 64, "vec_t has to be 64 bytes as part of SBO");
|
||||
|
||||
#define VEC_GET(V, T) ((T *)vec_data(V))
|
||||
#define VEC_SIZE(V, T) ((V)->size / (sizeof(T)))
|
||||
#define VEC_GET(V, I, T) (((T *)vec_data(V))[I])
|
||||
#define VEC_SIZE(V, T) ((V)->size / (sizeof(T)))
|
||||
|
||||
void vec_init(vec_t *, u64);
|
||||
void vec_free(vec_t *);
|
||||
|
||||
@@ -41,22 +41,22 @@ char *sym_table_find(sym_table_t *table, sv_t sv)
|
||||
// TODO: Deal with resizing this when table->count > table->size / 2
|
||||
u64 index = djb2(sv) & (table->capacity - 1);
|
||||
|
||||
for (sv_t comp = VEC_GET(&table->entries, sv_t)[index]; comp.data; index += 1,
|
||||
for (sv_t comp = VEC_GET(&table->entries, index, sv_t); comp.data; index += 1,
|
||||
index = index & (table->capacity - 1),
|
||||
comp = VEC_GET(&table->entries, sv_t)[index])
|
||||
comp = VEC_GET(&table->entries, index, sv_t))
|
||||
// Is it present in the table?
|
||||
if (sv.size == comp.size && strncmp(sv.data, comp.data, sv.size) == 0)
|
||||
break;
|
||||
|
||||
// we couldn't find it in our linear search (worst case scenario)
|
||||
if (!VEC_GET(&table->entries, sv_t)[index].data)
|
||||
if (!VEC_GET(&table->entries, index, sv_t).data)
|
||||
{
|
||||
sv_t newsv = sv_copy(sv);
|
||||
VEC_GET(&table->entries, sv_t)[index] = newsv;
|
||||
VEC_GET(&table->entries, index, sv_t) = newsv;
|
||||
++table->count;
|
||||
}
|
||||
|
||||
return VEC_GET(&table->entries, sv_t)[index].data;
|
||||
return VEC_GET(&table->entries, index, sv_t).data;
|
||||
}
|
||||
|
||||
void sym_table_cleanup(sym_table_t *table)
|
||||
@@ -65,7 +65,7 @@ void sym_table_cleanup(sym_table_t *table)
|
||||
sv_t current = {0};
|
||||
for (u64 i = 0; i < table->capacity; ++i)
|
||||
{
|
||||
current = VEC_GET(&table->entries, sv_t)[i];
|
||||
current = VEC_GET(&table->entries, i, sv_t);
|
||||
if (current.data)
|
||||
free(current.data);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ void sys_cleanup(sys_t *sys)
|
||||
// Iterate through each cons currently allocated and free them
|
||||
for (size_t i = 0; i < VEC_SIZE(&sys->conses, lisp_t **); ++i)
|
||||
{
|
||||
lisp_t *allocated = VEC_GET(&sys->conses, lisp_t *)[i];
|
||||
lisp_t *allocated = VEC_GET(&sys->conses, i, lisp_t *);
|
||||
switch (get_tag(allocated))
|
||||
{
|
||||
case TAG_CONS:
|
||||
|
||||
Reference in New Issue
Block a user