feat(vector): foreach method
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user