Finished edge allocation.
[libfirm] / pbqp_edge.c
1 #include "adt/array.h"
2 #include "assert.h"
3
4 #include "kaps.h"
5 #include "matrix.h"
6 #include "pbqp_edge.h"
7 #include "pbqp_edge_t.h"
8 #include "pbqp_node.h"
9 #include "pbqp_node_t.h"
10 #include "pbqp_t.h"
11
12 pbqp_edge *alloc_edge(pbqp *pbqp, int src_index, int tgt_index, matrix *costs)
13 {
14         if (tgt_index < src_index) {
15                 return alloc_edge(pbqp, tgt_index, src_index, costs);
16         }
17
18         pbqp_edge *edge = obstack_alloc(&pbqp->obstack, sizeof(*edge));
19         assert(edge);
20
21         pbqp_node *src_node = get_node(pbqp, src_index);
22         assert(src_node);
23         edge->src = src_node;
24
25         pbqp_node *tgt_node = get_node(pbqp, tgt_index);
26         assert(tgt_node);
27         edge->tgt = tgt_node;
28
29         edge->costs = matrix_copy(pbqp, costs);
30
31         /*
32          * Connect edge with incident nodes. Since the edge is allocated, we know
33          * that it don't appear in the edge lists of the nodes.
34          */
35         ARR_APP1(pbqp_edge *, src_node->edges, edge);
36         edge->src = src_node;
37         ARR_APP1(pbqp_edge *, tgt_node->edges, edge);
38         edge->tgt = tgt_node;
39
40         return edge;
41 }