core/set: Add function for copying values between sets
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
73f61e13d7
commit
09c87e14aa
|
@ -4,6 +4,14 @@
|
|||
|
||||
#include <core/set.h>
|
||||
|
||||
void set_copy(const struct set *lhs, struct set *rhs)
|
||||
{
|
||||
struct set_iter it_lhs;
|
||||
|
||||
set_for_each(lhs, &it_lhs)
|
||||
set_insert(rhs, it_lhs.it_val);
|
||||
}
|
||||
|
||||
void set_inline_intersect(const struct set *lhs, struct set *rhs)
|
||||
{
|
||||
struct set_iter it_rhs;
|
||||
|
|
|
@ -49,6 +49,9 @@ static inline unsigned int set_size(struct set *set)
|
|||
return g_hash_table_size(set->s_set);
|
||||
}
|
||||
|
||||
/* Copy values from set1 into set2. */
|
||||
void set_copy(const struct set *, struct set *);
|
||||
|
||||
/* Remove values from set2 that are not also in set1. */
|
||||
void set_inline_intersect(const struct set *, struct set *);
|
||||
|
||||
|
|
|
@ -40,6 +40,47 @@ void test_set()
|
|||
set_deinit(&set);
|
||||
}
|
||||
|
||||
void test_copy()
|
||||
{
|
||||
struct set set1 = SET_INIT();
|
||||
struct set set2 = SET_INIT();
|
||||
struct set set3 = SET_INIT();
|
||||
unsigned int i;
|
||||
|
||||
/* Test copy between two empty sets. */
|
||||
set_copy(&set1, &set2);
|
||||
test_equal(set_size(&set1), 0);
|
||||
test_equal(set_size(&set2), 0);
|
||||
|
||||
/* Add values 0 - 9 to set1, but keep set2 empty. */
|
||||
for (i = 0; i < 10; i++)
|
||||
set_insert(&set1, i);
|
||||
set_copy(&set1, &set2);
|
||||
test_equal(set_size(&set1), 10);
|
||||
test_equal(set_size(&set2), 10);
|
||||
|
||||
/* Set2 should now contain values 0 - 9. */
|
||||
for (i = 0; i < 10; i++) {
|
||||
test_loop_equal(set_has(&set2, i), (bool)true, i);
|
||||
} test_loop_passed();
|
||||
|
||||
/* Add values 10 - 19 to set3, copy into set2. */
|
||||
for (i = 10; i < 20; i++)
|
||||
set_insert(&set3, i);
|
||||
set_copy(&set3, &set2);
|
||||
test_equal(set_size(&set3), 10);
|
||||
test_equal(set_size(&set2), 20);
|
||||
|
||||
/* Set2 should now contain values 0 - 19. */
|
||||
for (i = 0; i < 20; i++) {
|
||||
test_loop_equal(set_has(&set2, i), (bool)true, i);
|
||||
} test_loop_passed();
|
||||
|
||||
set_deinit(&set1);
|
||||
set_deinit(&set2);
|
||||
set_deinit(&set3);
|
||||
}
|
||||
|
||||
void test_intersection()
|
||||
{
|
||||
struct set set1 = SET_INIT();
|
||||
|
@ -91,5 +132,6 @@ void test_intersection()
|
|||
|
||||
DECLARE_UNIT_TESTS(
|
||||
UNIT_TEST("Set", test_set),
|
||||
UNIT_TEST("Set Copy", test_copy),
|
||||
UNIT_TEST("Set Intersection", test_intersection),
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue