dynarray: Add pa_dynarray_remove_by_data()

This commit is contained in:
Tanu Kaskinen 2015-01-07 16:56:49 +02:00 committed by David Henningsson
parent 9efe77c8e5
commit 7d3879c07f
2 changed files with 25 additions and 0 deletions

View file

@ -104,6 +104,26 @@ int pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i) {
return 0;
}
int pa_dynarray_remove_by_data(pa_dynarray *array, void *p) {
unsigned i;
pa_assert(array);
pa_assert(p);
/* Iterate backwards, with the assumption that recently appended entries
* are likely to be removed first. */
i = array->n_entries;
while (i > 0) {
i--;
if (array->data[i] == p) {
pa_dynarray_remove_by_index(array, i);
return 0;
}
}
return -PA_ERR_NOENTITY;
}
void *pa_dynarray_steal_last(pa_dynarray *array) {
pa_assert(array);

View file

@ -51,6 +51,11 @@ void *pa_dynarray_last(pa_dynarray *array);
/* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise. */
int pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i);
/* Returns -PA_ERR_NOENTITY if p is not found in the array, and zero
* otherwise. If the array contains multiple occurrencies of p, only one of
* them is removed (and it's unspecified which one). */
int pa_dynarray_remove_by_data(pa_dynarray *array, void *p);
/* Returns the removed item, or NULL if the array is empty. */
void *pa_dynarray_steal_last(pa_dynarray *array);