Skip to content

Commit

Permalink
vector: define the count function
Browse files Browse the repository at this point in the history
Returns the number of elements in the vector.
More readable than drilling down into structures.
  • Loading branch information
matheusmoreira committed Dec 17, 2023
1 parent 026fed4 commit 3b50929
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/lone/value/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct lone_value lone_vector_create(struct lone_lisp *lone, size_t capacity);
│ │
╰────────────────────────────────────────────────────────────────────────╯ */

size_t lone_vector_count(struct lone_value vector);
void lone_vector_resize(struct lone_lisp *lone, struct lone_value vector, size_t new_capacity);
struct lone_value lone_vector_get_value_at(struct lone_lisp *lone, struct lone_value vector, size_t i);
struct lone_value lone_vector_get(struct lone_lisp *lone, struct lone_value vector, struct lone_value index);
Expand Down
7 changes: 6 additions & 1 deletion source/lone/value/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ struct lone_value lone_vector_create(struct lone_lisp *lone, size_t capacity)
return lone_value_from_heap_value(heap_value);
}

size_t lone_vector_count(struct lone_value vector)
{
return vector.as.heap_value->as.vector.count;
}

void lone_vector_resize(struct lone_lisp *lone, struct lone_value vector, size_t new_capacity)
{
struct lone_vector *actual = &vector.as.heap_value->as.vector;
Expand Down Expand Up @@ -74,7 +79,7 @@ void lone_vector_set(struct lone_lisp *lone, struct lone_value vector, struct lo

void lone_vector_push(struct lone_lisp *lone, struct lone_value vector, struct lone_value value)
{
lone_vector_set_value_at(lone, vector, vector.as.heap_value->as.vector.count, value);
lone_vector_set_value_at(lone, vector, lone_vector_count(vector), value);
}

void lone_vector_push_va_list(struct lone_lisp *lone, struct lone_value vector, size_t count, va_list arguments)
Expand Down

0 comments on commit 3b50929

Please sign in to comment.