basic: add set_equal() helper
This commit is contained in:
@@ -2036,3 +2036,35 @@ int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **
|
||||
*ret = TAKE_PTR(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool set_equal(Set *a, Set *b) {
|
||||
void *p;
|
||||
|
||||
/* Checks whether each entry of 'a' is also in 'b' and vice versa, i.e. the two sets contain the same
|
||||
* entries */
|
||||
|
||||
if (a == b)
|
||||
return true;
|
||||
|
||||
if (set_isempty(a) && set_isempty(b))
|
||||
return true;
|
||||
|
||||
if (set_size(a) != set_size(b)) /* Cheap check that hopefully catches a lot of inequality cases
|
||||
* already */
|
||||
return false;
|
||||
|
||||
SET_FOREACH(p, a)
|
||||
if (!set_contains(b, p))
|
||||
return false;
|
||||
|
||||
/* If we have the same hashops, then we don't need to check things backwards given we compared the
|
||||
* size and that all of a is in b. */
|
||||
if (a->b.hash_ops == b->b.hash_ops)
|
||||
return true;
|
||||
|
||||
SET_FOREACH(p, b)
|
||||
if (!set_contains(a, p))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -152,3 +152,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free_free);
|
||||
#define _cleanup_set_free_free_ _cleanup_(set_free_freep)
|
||||
|
||||
int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **ret);
|
||||
|
||||
bool set_equal(Set *a, Set *b);
|
||||
|
||||
Reference in New Issue
Block a user