feat(vector): foreach method

This commit is contained in:
2025-09-09 00:58:39 +02:00
parent 0974609339
commit 26f0887c5c
3 changed files with 37 additions and 2 deletions

View File

@@ -222,7 +222,7 @@ int vec_insert(vec_s *vec, size_t index, void *value) {
void *tmp = realloc(vec->data, next_power_two(vec->size + 1));
if (tmp == NULL) {
mtx_unlock(&vec->lock);
return -1;
}
vec->data = tmp;
@@ -281,3 +281,21 @@ int vec_set(vec_s *vec, size_t index, void *value) {
return 0;
}
int vec_foreach(vec_s *vec, void (*fefn)(void *)) {
if (vec == NULL || fefn == NULL) {
return -1;
}
if (mtx_lock(&vec->lock) != thrd_success) {
return -1;
}
for (size_t i = 0; i < vec->size; ++i) {
fefn((char *)vec->data + (i * vec->elem_size));
}
mtx_unlock(&vec->lock);
return 0;
}

View File

@@ -37,7 +37,8 @@ int vec_set(vec_s *vec, size_t index, void *value);
int vec_clear(vec_s *vec);
// void *vec_foreach(vec_s *, void (* fefn)(void *)) ??
int vec_foreach(vec_s *vec, void (*fefn)(void *));
// void *vec_sort(vec_s *, void (* sortfn)(void *a, void *b))
void vec_free(vec_s *vec);