diff --git a/common/include/villas/list.h b/common/include/villas/list.h index 154bf754a..c6cf19145 100644 --- a/common/include/villas/list.h +++ b/common/include/villas/list.h @@ -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 diff --git a/common/lib/list.cpp b/common/lib/list.cpp index 355f74ee3..fe769fde7 100644 --- a/common/lib/list.cpp +++ b/common/lib/list.cpp @@ -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); +}