Add set_first.

It returns the first element of a set. If the set is empty, -1 (unsigned)
is returned.
This commit is contained in:
Bill Currie 2012-10-30 21:56:10 +09:00
parent 4cc8d4a04b
commit 8fb958603c
2 changed files with 12 additions and 0 deletions

View file

@ -59,6 +59,7 @@ int set_is_intersecting (const set_t *s1, const set_t *s2);
int set_is_equivalent (const set_t *s1, const set_t *s2);
int set_is_subset (const set_t *set, const set_t *sub);
int set_is_member (const set_t *set, unsigned x);
unsigned set_first (const set_t *set);
const char *set_as_string (const set_t *set);
//@}

View file

@ -245,6 +245,17 @@ set_is_member (const set_t *set, unsigned x)
return (set->map[x / BITS] & (1 << (x % BITS))) != 0;
}
unsigned
set_first (const set_t *set)
{
unsigned x;
for (x = 0; x < set->size; x++)
if (set_is_member (set, x))
return x;
return -1; // FIXME error?
}
const char *
set_as_string (const set_t *set)
{