1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

list: added vlist_filter() to remove elements which dont match a condition

This commit is contained in:
Steffen Vogel 2019-04-16 07:59:28 +02:00
parent 3de52ec60f
commit d24ae46dca
2 changed files with 20 additions and 0 deletions

View file

@ -133,6 +133,9 @@ ssize_t vlist_index(struct vlist *l, void *value);
/** Extend the list to the given length by filling new slots with given value. */
void vlist_extend(struct vlist *l, size_t len, void *val);
/** Remove all elements for which the callback returns a non-zero return code. */
void vlist_filter(struct vlist *l, dtor_cb_t cb);
#ifdef __cplusplus
}
#endif

View file

@ -299,3 +299,20 @@ void vlist_extend(struct vlist *l, size_t len, void *val)
while (vlist_length(l) < len)
vlist_push(l, val);
}
void vlist_filter(struct vlist *l, dtor_cb_t cb)
{
size_t u, j;
pthread_mutex_lock(&l->lock);
for (i = 0, j = 0; i < vlist_length(l); i++) {
void *e = vlist_at(l, i);
if (!cb(e))
l->array[j++] = e;
}
l->length = j;
pthread_mutex_unlock(&l->lock);
}