s/full\>/ful/.
[libfirm] / ir / adt / hungarian.c
index c38cb47..57cb265 100644 (file)
@@ -57,13 +57,8 @@ struct _hungarian_problem_t {
        DEBUG_ONLY(firm_dbg_module_t *dbg);
 };
 
-static inline void *get_init_mem(struct obstack *obst, size_t sz) {
-       void *p = obstack_alloc(obst, sz);
-       memset(p, 0, sz);
-       return p;
-}
-
-static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width) {
+static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width)
+{
        int i, j;
 
        fprintf(f , "\n");
@@ -77,14 +72,16 @@ static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width) {
        fprintf(f, "\n");
 }
 
-void hungarian_print_costmatrix(hungarian_problem_t *p, int width) {
+void hungarian_print_cost_matrix(hungarian_problem_t *p, int width)
+{
        hungarian_dump_f(stderr, p->cost, p->num_rows, p->num_cols, width);
 }
 
 /**
  * Create the object and allocate memory for the data structures.
  */
-hungarian_problem_t *hungarian_new(int rows, int cols, int match_type) {
+hungarian_problem_t *hungarian_new(int rows, int cols, int match_type)
+{
        int i;
        hungarian_problem_t *p = XMALLOCZ(hungarian_problem_t);
 
@@ -116,9 +113,9 @@ hungarian_problem_t *hungarian_new(int rows, int cols, int match_type) {
        }
 
        /* allocate space for cost matrix */
-       p->cost = (int **)get_init_mem(&p->obst, rows * sizeof(p->cost[0]));
+       p->cost = OALLOCNZ(&p->obst, int*, rows);
        for (i = 0; i < p->num_rows; i++)
-               p->cost[i] = (int *)get_init_mem(&p->obst, cols * sizeof(p->cost[0][0]));
+               p->cost[i] = OALLOCNZ(&p->obst, int, cols);
 
        return p;
 }
@@ -126,7 +123,8 @@ hungarian_problem_t *hungarian_new(int rows, int cols, int match_type) {
 /**
  * Prepare the cost matrix.
  */
-void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode) {
+void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode)
+{
        int i, j;
 
        if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) {
@@ -146,7 +144,8 @@ void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode) {
 /**
  * Set cost[left][right] to cost.
  */
-void hungarian_add(hungarian_problem_t *p, int left, int right, int cost) {
+void hungarian_add(hungarian_problem_t *p, int left, int right, int cost)
+{
        assert(p->num_rows > left  && "Invalid row selected.");
        assert(p->num_cols > right && "Invalid column selected.");
        assert(cost >= 0);
@@ -163,7 +162,8 @@ void hungarian_add(hungarian_problem_t *p, int left, int right, int cost) {
 /**
  * Set cost[left][right] to 0.
  */
-void hungarian_remv(hungarian_problem_t *p, int left, int right) {
+void hungarian_remv(hungarian_problem_t *p, int left, int right)
+{
        assert(p->num_rows > left  && "Invalid row selected.");
        assert(p->num_cols > right && "Invalid column selected.");
 
@@ -178,7 +178,8 @@ void hungarian_remv(hungarian_problem_t *p, int left, int right) {
 /**
  * Frees all allocated memory.
  */
-void hungarian_free(hungarian_problem_t* p) {
+void hungarian_free(hungarian_problem_t* p)
+{
        obstack_free(&p->obst, NULL);
        xfree(p);
 }
@@ -186,7 +187,8 @@ void hungarian_free(hungarian_problem_t* p) {
 /**
  * Do the assignment.
  */
-int hungarian_solve(hungarian_problem_t* p, int *assignment, int *final_cost, int cost_threshold) {
+int hungarian_solve(hungarian_problem_t* p, int *assignment, int *final_cost, int cost_threshold)
+{
        int i, j, m, n, k, l, s, t, q, unmatched, cost;
        int *col_mate;
        int *row_mate;
@@ -273,11 +275,11 @@ row_done: ;
                goto done;
 
        unmatched = t;
-       while (1) {
+       for (;;) {
                DBG((p->dbg, LEVEL_1, "Matched %d rows.\n", m - t));
                q = 0;
 
-               while (1) {
+               for (;;) {
                        while (q < t) {
                                /* Begin explore node q of the forest 19 */
                                k = unchosen_row[q];
@@ -349,7 +351,7 @@ row_done: ;
 breakthru:
                /* Begin update the matching 20 */
                DBG((p->dbg, LEVEL_1, "Breakthrough at node %d of %d.\n", q, t));
-               while (1) {
+               for (;;) {
                        j           = col_mate[k];
                        col_mate[k] = l;
                        row_mate[l] = k;
@@ -447,7 +449,8 @@ done:
        xfree(unchosen_row);
        xfree(col_mate);
 
-       *final_cost = cost;
+       if (final_cost != NULL)
+               *final_cost = cost;
 
        return 0;
 }