data_structures

data structures in C
Log | Files | Refs | Feed

commit 39c309324a7a42a7aa6032461664c265f6be9b74
parent 69ddc53db121d077471eea3a8675028d7d0a747d
Author: Jenny Doe <tng@soykaf.me>
Date:   Fri, 22 Mar 2019 17:03:10 +0100

added: set_union()

Diffstat:
Mset.c | 36+++++++++++++++++++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/set.c b/set.c @@ -45,7 +45,7 @@ set_remove(Set * set, void **data) return list_rem_next(set->set, NULL, data); do { - if (t->next == NULL) /* reached tail */ + if (t->next == NULL) /* reached tail */ break; if (set->match(t->next->data, *data) == 0) @@ -69,3 +69,37 @@ set_is_member(Set * set, const void *data) return (1); } + +/* + * UNTESTED !! + * + * set1 == NULL is a shortcut for using set_union() to clone a set (without + * copying its individual members!) + */ + +int +set_union(Set * setu, const Set * set1, const Set * set2) +{ + if (set1 != NULL && set1->set->size != 0) { + SetElmt *t = set1->set->head; + if (set_insert(setu, t->data) != 0) + return (1); + while ((t = t->next) != NULL) { + if (set_insert(setu, t->data) != 0) + return (1); + } + } + + if (set2->set->size != 0) { + SetElmt *t = set2->set->head; + if (set_insert(setu, t->data) != 0) + return (1); + while ((t = t->next) != NULL) { + if (set_insert(setu, t->data) != 0) + return (1); + } + } + + return (0); +} +