summaryrefslogtreecommitdiffhomepage
path: root/ir/adt
diff options
context:
space:
mode:
authorMatthias Braun <matze@braunis.de>2015-09-07 11:26:29 +0200
committerMatthias Braun <matze@braunis.de>2015-09-07 19:58:32 +0200
commite0d4201140df26345b8b4faee8ded531aea84435 (patch)
tree373578a69f2be4e79991ba1af84c48f4f4fe15b9 /ir/adt
parent731fe730650fe6e4cc79775584f212512ca4b172 (diff)
bipartite: Cleanup
Diffstat (limited to 'ir/adt')
-rw-r--r--ir/adt/bipartite.c77
1 files changed, 35 insertions, 42 deletions
diff --git a/ir/adt/bipartite.c b/ir/adt/bipartite.c
index a06fbce..f60b6ff 100644
--- a/ir/adt/bipartite.c
+++ b/ir/adt/bipartite.c
@@ -16,61 +16,59 @@
#include "xmalloc.h"
struct bipartite_t {
- int n_left, n_right;
- bitset_t *adj[1];
+ unsigned n_left;
+ unsigned n_right;
+ bitset_t *adj[];
};
-bipartite_t *bipartite_new(int n_left, int n_right)
+bipartite_t *bipartite_new(unsigned const n_left, unsigned const n_right)
{
bipartite_t *gr = XMALLOCFZ(bipartite_t, adj, n_left);
- int i;
-
- gr->n_left = n_left;
+ gr->n_left = n_left;
gr->n_right = n_right;
- for (i = 0; i < n_left; ++i)
+ for (unsigned i = 0; i < n_left; ++i)
gr->adj[i] = bitset_malloc(n_right);
return gr;
}
-void bipartite_free(bipartite_t *gr)
+void bipartite_free(bipartite_t *const gr)
{
- int i;
- for (i = 0; i < gr->n_left; ++i)
+ for (unsigned i = 0; i < gr->n_left; ++i)
free(gr->adj[i]);
free(gr);
}
-void bipartite_add(bipartite_t *gr, int i, int j)
+void bipartite_add(bipartite_t *const gr, unsigned const i, unsigned const j)
{
assert(i < gr->n_left && j < gr->n_right);
bitset_set(gr->adj[i], j);
}
-void bipartite_remv(bipartite_t *gr, int i, int j)
+void bipartite_remv(bipartite_t *const gr, unsigned const i, unsigned const j)
{
assert(i < gr->n_left && j < gr->n_right);
bitset_clear(gr->adj[i], j);
}
-int bipartite_adj(const bipartite_t *gr, int i, int j)
+int bipartite_adj(bipartite_t const *const gr, unsigned const i,
+ unsigned const j)
{
assert(i < gr->n_left && j < gr->n_right);
return bitset_is_set(gr->adj[i], j);
}
-static int apply_alternating_path(const bipartite_t *gr, int *matching,
- bitset_t *matched_left, bitset_t *matched_right)
+static int apply_alternating_path(bipartite_t const *const gr,
+ int *const matching,
+ bitset_t *const matched_left,
+ bitset_t *const matched_right)
{
- int left, right;
- int done_something = 0;
- bitset_t *tmp = bitset_alloca(gr->n_right);
+ bool done_something = false;
+ bitset_t *const tmp = bitset_alloca(gr->n_right);
- for (left = 0; left < gr->n_left; ++left) {
+ for (unsigned left = 0; left < gr->n_left; ++left) {
bitset_t *left_adj = gr->adj[left];
- int i;
-
bitset_copy(tmp, left_adj);
if (matching[left] >= 0) {
@@ -82,14 +80,12 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
continue;
bitset_andnot(tmp, matched_right);
- right = bitset_next_set(tmp, 0);
-
- assert(right != -1);
+ unsigned right = bitset_next_set(tmp, 0);
+ assert(right != ~0u);
- /*
- We have to find another left node which has the old right one as a neighbor.
- This node must not be part of a matching
- */
+ /* We have to find another left node which has the old right one as
+ * a neighbor. This node must not be part of a matching */
+ unsigned i;
for (i = 0; i < gr->n_left; ++i)
if (i != left && bitset_is_set(gr->adj[i], old_right) && !bitset_is_set(matched_left, i))
break;
@@ -103,7 +99,7 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
matching[i] = old_right;
bitset_set(matched_left, i);
bitset_set(matched_right, right);
- done_something = 1;
+ done_something = true;
} else {
/* We have to create a new single edge */
assert(!bitset_is_set(matched_left, left));
@@ -112,44 +108,41 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
if (bitset_is_empty(tmp))
continue;
- right = bitset_next_set(tmp, 0);
+ unsigned right = bitset_next_set(tmp, 0);
assert(!bitset_is_set(matched_right, right));
matching[left] = right;
bitset_set(matched_left, left);
bitset_set(matched_right, right);
- done_something = 1;
+ done_something = true;
}
}
return done_something;
}
-void bipartite_matching(const bipartite_t *gr, int *matching)
+void bipartite_matching(bipartite_t const *const gr, int *const matching)
{
- bitset_t *matched_left = bitset_alloca(gr->n_left);
- bitset_t *matched_right = bitset_alloca(gr->n_right);
+ bitset_t *const matched_left = bitset_alloca(gr->n_left);
+ bitset_t *const matched_right = bitset_alloca(gr->n_right);
memset(matching, -1, gr->n_left * sizeof(int));
while (apply_alternating_path(gr, matching, matched_left, matched_right)) {
}
}
-void bipartite_dump_f(FILE *f, const bipartite_t *gr)
+void bipartite_dump_f(FILE *const f, bipartite_t const *gr)
{
- int i;
-
- for (i = 0; i < gr->n_left; ++i) {
+ for (unsigned i = 0; i < gr->n_left; ++i) {
fprintf(f, "%d: ", i);
bitset_fprint(f, gr->adj[i]);
fprintf(f, "\n");
}
}
-void bipartite_dump(const char *name, const bipartite_t *gr)
+void bipartite_dump(const char *const name, bipartite_t const *gr)
{
- FILE *f = fopen(name, "w");
-
- if (f) {
+ FILE *const f = fopen(name, "w");
+ if (f != NULL) {
bipartite_dump_f(f, gr);
fclose(f);
}