idxset: Add set contains() function

This is functionally equivalent to get_by_data(s, p, NULL) == p, but
with a more obvious name and form because some existing code is instead
manually iterating through idxsets to check for existence of an item.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/596>
This commit is contained in:
Alper Nebi Yasak 2021-06-26 12:05:17 +03:00
parent def8eb074e
commit fb63e58931
2 changed files with 17 additions and 0 deletions

View file

@ -258,6 +258,20 @@ void* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx) {
return e->data;
}
bool pa_idxset_contains(pa_idxset *s, const void *p) {
unsigned hash;
struct idxset_entry *e;
pa_assert(s);
hash = s->hash_func(p) % NBUCKETS;
if (!(e = data_scan(s, hash, p)))
return false;
return e->data == p;
}
void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx) {
struct idxset_entry *e;
unsigned hash;

View file

@ -66,6 +66,9 @@ void* pa_idxset_get_by_index(pa_idxset*s, uint32_t idx);
/* Get the entry by its data. The index is returned in *idx */
void* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx);
/* Return true if item is in idxset */
bool pa_idxset_contains(pa_idxset *s, const void *p);
/* Similar to pa_idxset_get_by_index(), but removes the entry from the idxset. */
void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx);