+#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));
pbqp->num_nodes = number_nodes;
pbqp->nodes = obstack_alloc(&pbqp->obstack, number_nodes
* sizeof(*pbqp->nodes));
+
+ return pbqp;
}
void free_pbqp(pbqp *pbqp)
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);
+ }
+}
*/
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 */
+#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;
+}
+#ifndef KAPS_MATRIX_H
+#define KAPS_MATRIX_H
+
+#include "matrix_t.h"
+
+matrix *matrix_copy(pbqp *pbqp, matrix *m);
+
+#endif /* KAPS_MATRIX_H */
#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 */
+#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;
+}
+#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 */
assert(node);
node->edges = NEW_ARR_F(pbqp_edge *, 0);
- node->costs = vector_copy(costs);
+ node->costs = vector_copy(pbqp, costs);
return node;
}
#include <limits.h>
+#include "adt/obstack.h"
+
#include "matrix_t.h"
#include "vector_t.h"
typedef struct pbqp_edge pbqp_edge;
typedef struct pbqp_node pbqp_node;
+typedef struct pbqp pbqp;
static const num INF_COST = INT_MAX;
{
int i;
int len;
- vector *copy = obstack_alloc(pbqp->obstack, sizeof(*copy));
+ vector *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy));
assert(copy);
#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 */
#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 */