Continued implementation of PBQP solver: Fixed errors and warnings.
authorSebastian Buchwald <Sebastian.Buchwald@kit.edu>
Fri, 3 Oct 2008 00:03:53 +0000 (00:03 +0000)
committerSebastian Buchwald <Sebastian.Buchwald@kit.edu>
Fri, 3 Oct 2008 00:03:53 +0000 (00:03 +0000)
[r22425]

12 files changed:
kaps.c
kaps.h
matrix.c
matrix.h
matrix_t.h
pbqp_edge.c
pbqp_edge.h
pbqp_node.c
pbqp_t.h
vector.c
vector.h
vector_t.h

diff --git a/kaps.c b/kaps.c
index 0729fbe..b3beff6 100644 (file)
--- a/kaps.c
+++ b/kaps.c
@@ -1,10 +1,42 @@
+#include "adt/array.h"
+
 #include "kaps.h"
+#include "pbqp_edge_t.h"
+#include "pbqp_node.h"
+#include "pbqp_node_t.h"
+#include "vector.h"
 
-static pbqp_node *get_node(pbqp *pbqp, int index)
+pbqp_node *get_node(pbqp *pbqp, int index)
 {
        return pbqp->nodes[index];
 }
 
+pbqp_edge *get_edge(pbqp *pbqp, int src_index, int tgt_index)
+{
+       int i;
+       int len;
+
+       if (src_index < tgt_index) {
+               return get_edge(pbqp, tgt_index, src_index);
+       }
+
+       pbqp_node *src_node = get_node(pbqp, src_index);
+       assert(src_node);
+       pbqp_node *tgt_node = get_node(pbqp, tgt_index);
+       assert(tgt_node);
+
+       len = ARR_LEN(src_node->edges);
+
+       for (i = 0; i < len; ++i) {
+               pbqp_edge *cur_edge = src_node->edges[i];
+               if (cur_edge->tgt == tgt_node) {
+                       return cur_edge;
+               }
+       }
+
+       return NULL;
+}
+
 pbqp *alloc_pbqp(int number_nodes)
 {
        pbqp* pbqp = xmalloc(sizeof(*pbqp));
@@ -15,6 +47,8 @@ pbqp *alloc_pbqp(int number_nodes)
        pbqp->num_nodes = number_nodes;
        pbqp->nodes = obstack_alloc(&pbqp->obstack, number_nodes
                        * sizeof(*pbqp->nodes));
+
+       return pbqp;
 }
 
 void free_pbqp(pbqp *pbqp)
@@ -33,3 +67,14 @@ void add_node_costs(pbqp *pbqp, int node_index, vector *costs)
                vector_add(node->costs, costs);
        }
 }
+
+void add_edge_costs(pbqp *pbqp, int src_index, int tgt_index, matrix *costs)
+{
+       pbqp_edge *edge = get_edge(pbqp, src_index, tgt_index);
+
+       if (edge == NULL) {
+               edge = alloc_edge(pbqp, src_index, tgt_index, costs);
+       } else {
+               matrix_add(edge->costs, costs);
+       }
+}
diff --git a/kaps.h b/kaps.h
index ac50f08..3419177 100644 (file)
--- a/kaps.h
+++ b/kaps.h
@@ -23,4 +23,7 @@ void add_node_costs(pbqp *pbqp, int node_index, vector *costs);
  */
 void add_edge_costs(pbqp *pbqp, int src_index, int tgt_index, matrix *costs);
 
+pbqp_edge *get_edge(pbqp *pbqp, int src_index, int tgt_index);
+pbqp_node *get_node(pbqp *pbqp, int index);
+
 #endif /* KAPS_KAPS_H */
index e69de29..029514d 100644 (file)
--- a/matrix.c
+++ b/matrix.c
@@ -0,0 +1,21 @@
+#include "assert.h"
+
+#include "pbqp_t.h"
+#include "matrix.h"
+
+matrix *matrix_copy(pbqp *pbqp, matrix *m)
+{
+       int i;
+       int len;
+       matrix *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy));
+
+       assert(copy);
+
+       len = m->rows * m->cols;
+
+       for (i = 0; i < len; ++i) {
+               copy->entries[i] = m->entries[i];
+       }
+
+       return copy;
+}
index e69de29..4e2351d 100644 (file)
--- a/matrix.h
+++ b/matrix.h
@@ -0,0 +1,8 @@
+#ifndef KAPS_MATRIX_H
+#define KAPS_MATRIX_H
+
+#include "matrix_t.h"
+
+matrix *matrix_copy(pbqp *pbqp, matrix *m);
+
+#endif /* KAPS_MATRIX_H */
index 38ee3b8..f1435c4 100644 (file)
@@ -1,13 +1,12 @@
 #ifndef KAPS_MATRIX_T_H
 #define KAPS_MATRIX_T_H
 
-struct matrix;
 typedef struct matrix matrix;
 
 struct matrix {
        int rows;
        int cols;
-       num entries[];
+       int entries[];
 };
 
 #endif /* KAPS_MATRIX_T_H */
index e69de29..6218306 100644 (file)
@@ -0,0 +1,33 @@
+#include "assert.h"
+
+#include "kaps.h"
+#include "matrix.h"
+#include "pbqp_edge.h"
+#include "pbqp_edge_t.h"
+#include "pbqp_node.h"
+#include "pbqp_node_t.h"
+#include "pbqp_t.h"
+
+pbqp_edge *alloc_edge(pbqp *pbqp, int src_index, int tgt_index, matrix *costs)
+{
+       if (tgt_index < src_index) {
+               return alloc_edge(pbqp, tgt_index, src_index, costs);
+       }
+
+       pbqp_edge *edge = obstack_alloc(&pbqp->obstack, sizeof(*edge));
+       assert(edge);
+
+       pbqp_node *src_node = get_node(pbqp, src_index);
+       assert(src_node);
+       edge->src = src_node;
+
+       pbqp_node *tgt_node = get_node(pbqp, tgt_index);
+       assert(tgt_node);
+       edge->tgt = tgt_node;
+
+       edge->costs = matrix_copy(pbqp, costs);
+
+       // TODO: connect edge with nodes
+
+       return edge;
+}
index e69de29..8679f17 100644 (file)
@@ -0,0 +1,8 @@
+#ifndef KAPS_PBQP_EDGE_H
+#define KAPS_PBQP_EDGE_H
+
+#include "pbqp_t.h"
+
+pbqp_edge *alloc_edge(pbqp *pbqp, int src_index, int tgt_index, matrix *costs);
+
+#endif /* KAPS_PBQP_EDGE_H */
index 4b6a3be..d36baff 100644 (file)
@@ -12,7 +12,7 @@ pbqp_node *alloc_node(pbqp *pbqp, vector *costs)
        assert(node);
 
        node->edges = NEW_ARR_F(pbqp_edge *, 0);
-       node->costs = vector_copy(costs);
+       node->costs = vector_copy(pbqp, costs);
 
        return node;
 }
index bb8a34c..2a79aff 100644 (file)
--- a/pbqp_t.h
+++ b/pbqp_t.h
@@ -3,6 +3,8 @@
 
 #include <limits.h>
 
+#include "adt/obstack.h"
+
 #include "matrix_t.h"
 #include "vector_t.h"
 
@@ -10,6 +12,7 @@ typedef int num;
 
 typedef struct pbqp_edge pbqp_edge;
 typedef struct pbqp_node pbqp_node;
+typedef struct pbqp      pbqp;
 
 static const num INF_COST = INT_MAX;
 
index e4456a8..ed7d1ca 100644 (file)
--- a/vector.c
+++ b/vector.c
@@ -6,7 +6,7 @@ vector *vector_copy(pbqp *pbqp, vector *v)
 {
        int i;
        int len;
-       vector *copy = obstack_alloc(pbqp->obstack, sizeof(*copy));
+       vector *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy));
 
        assert(copy);
 
index 05855d8..410bd3a 100644 (file)
--- a/vector.h
+++ b/vector.h
@@ -1,10 +1,12 @@
 #ifndef KAPS_VECTOR_H
 #define KAPS_VECTOR_H
 
+#include "vector_t.h"
+
 /* Copy the given vector. */
-vector *vector_copy(vector *v);
+vector *vector_copy(pbqp *pbqp, vector *v);
 
 /* sum += summand */
 void vector_add(vector *sum, vector *summand);
 
-#endif KAPS_VECTOR_H
+#endif /* KAPS_VECTOR_H */
index 9d6a93c..6262c01 100644 (file)
@@ -1,12 +1,14 @@
 #ifndef KAPS_VECTOR_T_H
 #define KAPS_VECTOR_T_H
 
+#include "pbqp_t.h"
+
 struct vector;
 typedef struct vector vector;
 
 struct vector {
        int len;
-       num entries[];
+       int entries[];
 };
 
 #endif /* KAPS_VECTOR_T_H */