idxset: Add set comparison operations

Add isdisjoint(), issubset(), issuperset() and equals() functions that
element-wise compare two idxsets.

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-23 17:50:50 +03:00
parent fb63e58931
commit ec668ac44b
2 changed files with 46 additions and 0 deletions

View file

@ -470,6 +470,40 @@ bool pa_idxset_isempty(pa_idxset *s) {
return s->n_entries == 0;
}
bool pa_idxset_isdisjoint(pa_idxset *s, pa_idxset *t) {
struct idxset_entry *i;
pa_assert(s);
pa_assert(t);
for (i = s->iterate_list_head; i; i = i->iterate_next)
if (pa_idxset_contains(t, i->data))
return false;
return true;
}
bool pa_idxset_issubset(pa_idxset *s, pa_idxset *t) {
struct idxset_entry *i;
pa_assert(s);
pa_assert(t);
for (i = s->iterate_list_head; i; i = i->iterate_next)
if (!pa_idxset_contains(t, i->data))
return false;
return true;
}
bool pa_idxset_issuperset(pa_idxset *s, pa_idxset *t) {
return pa_idxset_issubset(t, s);
}
bool pa_idxset_equals(pa_idxset *s, pa_idxset *t) {
return pa_idxset_issubset(s, t) && pa_idxset_issuperset(s, t);
}
pa_idxset *pa_idxset_copy(pa_idxset *s, pa_copy_func_t copy_func) {
pa_idxset *copy;
struct idxset_entry *i;

View file

@ -107,6 +107,18 @@ unsigned pa_idxset_size(pa_idxset*s);
/* Return true of the idxset is empty */
bool pa_idxset_isempty(pa_idxset *s);
/* Return true if s and t have no entries in common */
bool pa_idxset_isdisjoint(pa_idxset *s, pa_idxset *t);
/* Return true if all entries in s are also in t */
bool pa_idxset_issubset(pa_idxset *s, pa_idxset *t);
/* Return true if all entries in t are also in s */
bool pa_idxset_issuperset(pa_idxset *s, pa_idxset *t);
/* Return true if s and t have all entries in common */
bool pa_idxset_equals(pa_idxset *s, pa_idxset *t);
/* Duplicate the idxset. This will not copy the actual indexes. If copy_func is
* set, each entry is copied using the provided function, otherwise a shallow
* copy will be made. */