Adapt to latest libfirm.
[libfirm] / pbqp_edge.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   PBQP edges.
23  * @date    02.10.2008
24  * @author  Sebastian Buchwald
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "adt/array.h"
30 #include "assert.h"
31
32 #include "kaps.h"
33 #include "matrix.h"
34 #include "pbqp_edge.h"
35 #include "pbqp_edge_t.h"
36 #include "pbqp_node.h"
37 #include "pbqp_node_t.h"
38 #include "pbqp_t.h"
39
40 pbqp_edge *alloc_edge(pbqp *pbqp, int src_index, int tgt_index, pbqp_matrix *costs)
41 {
42         int transpose = 0;
43
44         if (tgt_index < src_index) {
45                 int tmp = src_index;
46                 src_index = tgt_index;
47                 tgt_index = tmp;
48
49                 transpose = 1;
50         }
51
52         pbqp_edge *edge = obstack_alloc(&pbqp->obstack, sizeof(*edge));
53         assert(edge);
54
55         pbqp_node *src_node = get_node(pbqp, src_index);
56         assert(src_node);
57
58         pbqp_node *tgt_node = get_node(pbqp, tgt_index);
59         assert(tgt_node);
60
61         if (transpose) {
62                 edge->costs = pbqp_matrix_copy_and_transpose(pbqp, costs);
63         } else {
64                 edge->costs = pbqp_matrix_copy(pbqp, costs);
65         }
66
67         /*
68          * Connect edge with incident nodes. Since the edge is allocated, we know
69          * that it don't appear in the edge lists of the nodes.
70          */
71         ARR_APP1(pbqp_edge *, src_node->edges, edge);
72         edge->src = src_node;
73         ARR_APP1(pbqp_edge *, tgt_node->edges, edge);
74         edge->tgt = tgt_node;
75         edge->bucket_index = UINT_MAX;
76
77         return edge;
78 }
79
80 void delete_edge(pbqp_edge *edge)
81 {
82         pbqp_node  *src_node;
83         pbqp_node  *tgt_node;
84
85         assert(edge);
86
87         src_node = edge->src;
88         tgt_node = edge->tgt;
89         assert(src_node);
90         assert(tgt_node);
91
92         disconnect_edge(src_node, edge);
93         disconnect_edge(tgt_node, edge);
94 }
95
96 pbqp_edge *pbqp_edge_deep_copy(pbqp *pbqp, pbqp_edge *edge,
97                 pbqp_node *src_node, pbqp_node *tgt_node)
98 {
99         pbqp_edge *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy));
100         assert(copy);
101         assert(src_node);
102         assert(tgt_node);
103
104         copy->costs = pbqp_matrix_copy(pbqp, edge->costs);
105
106         /* Connect edge with incident nodes. */
107         copy->src = src_node;
108         copy->tgt = tgt_node;
109         copy->bucket_index = UINT_MAX;
110
111         return copy;
112 }