Skip to content

Commit

Permalink
sset: New function sset_intersect().
Browse files Browse the repository at this point in the history
This will acquire its first user in an upcoming commit.

Signed-off-by: Ben Pfaff <[email protected]>
Acked-by: Andy Zhou <[email protected]>
  • Loading branch information
blp committed Aug 26, 2015
1 parent b8b00f0 commit a4686ba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/sset.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ sset_at_position(const struct sset *set, uint32_t *bucketp, uint32_t *offsetp)
return SSET_NODE_FROM_HMAP_NODE(hmap_node);
}

/* Replaces 'a' by the intersection of 'a' and 'b'. That is, removes from 'a'
* all of the strings that are not also in 'b'. */
void
sset_intersect(struct sset *a, const struct sset *b)
{
const char *name, *next;

SSET_FOR_EACH_SAFE (name, next, a) {
if (!sset_contains(b, name)) {
sset_delete(a, SSET_NODE_FROM_NAME(name));
}
}
}

/* Returns a null-terminated array of pointers to the strings in 'set', in no
* particular order. The caller must free the returned array when it is no
* longer needed, but the strings in the array belong to 'set' and thus must
Expand Down
3 changes: 3 additions & 0 deletions lib/sset.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ bool sset_equals(const struct sset *, const struct sset *);
struct sset_node *sset_at_position(const struct sset *,
uint32_t *bucketp, uint32_t *offsetp);

/* Set operations. */
void sset_intersect(struct sset *, const struct sset *);

/* Iteration macros. */
#define SSET_FOR_EACH(NAME, SSET) \
for ((NAME) = SSET_FIRST(SSET); \
Expand Down

0 comments on commit a4686ba

Please sign in to comment.