aboutsummaryrefslogtreecommitdiff
path: root/src/set.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/set.c')
-rw-r--r--src/set.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/set.c b/src/set.c
new file mode 100644
index 0000000..b84ae6a
--- /dev/null
+++ b/src/set.c
@@ -0,0 +1,40 @@
+#include <stdlib.h>
+
+#include "set.h"
+
+set *set_create()
+{
+ set *s = malloc(sizeof(set));
+ s->parent = s;
+ s->rank = 0;
+ return s;
+}
+
+void set_free(set *s)
+{
+ free(s);
+}
+
+set *set_find(set *s)
+{
+ if(s->parent == s)
+ return s;
+ s->parent = set_find(s->parent);
+ return s->parent;
+}
+
+void set_union(set *x, set *y)
+{
+ x = set_find(x);
+ y = set_find(y);
+
+ if(x == y) return;
+
+ if(x->rank > y->rank) {
+ y->parent = x;
+ } else {
+ x->parent = y;
+ }
+
+ if(x->rank == y->rank) x->rank++;
+}