RM should work now, theoretically.
[libfirm] / vector.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 vector.
23  * @date    02.10.2008
24  * @author  Sebastian Buchwald
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "adt/array.h"
32
33 #include "vector.h"
34
35 num pbqp_add(num x, num y)
36 {
37         if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS;
38
39         num res = x + y;
40
41         /* No positive overflow. */
42         assert(x < 0 || y < 0 || res >= x);
43         assert(x < 0 || y < 0 || res >= y);
44
45         /* No negative overflow. */
46         assert(x > 0 || y > 0 || res <= x);
47         assert(x > 0 || y > 0 || res <= y);
48
49         /* Result is not infinity.*/
50         assert(res < INF_COSTS);
51
52         return res;
53 }
54
55 vector *vector_alloc(pbqp *pbqp, unsigned length)
56 {
57         assert(length > 0);
58         vector *vec = obstack_alloc(&pbqp->obstack, sizeof(*vec) + sizeof(*vec->entries) * length);
59         assert(vec);
60
61         vec->len = length;
62         memset(vec->entries, 0, sizeof(*vec->entries) * length);
63
64         return vec;
65 }
66
67 vector *vector_copy(pbqp *pbqp, vector *v)
68 {
69         unsigned  len  = v->len;
70         vector   *copy = obstack_copy(&pbqp->obstack, v, sizeof(*copy) + sizeof(*copy->entries) * len);
71         assert(copy);
72
73         return copy;
74 }
75
76 void vector_add(vector *sum, vector *summand)
77 {
78         int i;
79         int len;
80
81         assert(sum);
82         assert(summand);
83         assert(sum->len == summand->len);
84
85         len = sum->len;
86
87         for (i = 0; i < len; ++i) {
88                 sum->entries[i].data = pbqp_add(sum->entries[i].data,
89                                 summand->entries[i].data);
90         }
91 }
92
93 void vector_set(vector *vec, unsigned index, num value)
94 {
95         assert(index < vec->len);
96         vec->entries[index].data = value;
97 }
98
99 #if KAPS_ENABLE_VECTOR_NAMES
100 void vector_set_description(vector *vec, unsigned index, const char *name)
101 {
102         assert(index < vec->len);
103         vec->entries[index].name = name;
104 }
105 #endif
106
107 void vector_add_value(vector *vec, num value)
108 {
109         unsigned index;
110         unsigned len;
111
112         assert(vec);
113
114         len = vec->len;
115
116         for (index = 0; index < len; ++index) {
117                 vec->entries[index].data = pbqp_add(vec->entries[index].data, value);
118         }
119 }
120
121 void vector_add_matrix_col(vector *vec, pbqp_matrix *mat, unsigned col_index)
122 {
123         unsigned index;
124         unsigned len;
125
126         assert(vec);
127         assert(mat);
128         assert(vec->len == mat->rows);
129         assert(col_index < mat->cols);
130
131         len = vec->len;
132
133         for (index = 0; index < len; ++index) {
134                 vec->entries[index].data = pbqp_add(vec->entries[index].data, mat->entries[index * mat->cols + col_index]);
135         }
136 }
137
138 void vector_add_matrix_row(vector *vec, pbqp_matrix *mat, unsigned row_index)
139 {
140         unsigned index;
141         unsigned len;
142
143         assert(vec);
144         assert(mat);
145         assert(vec->len == mat->cols);
146         assert(row_index < mat->rows);
147
148         len = vec->len;
149
150         for (index = 0; index < len; ++index) {
151                 vec->entries[index].data = pbqp_add(vec->entries[index].data,
152                                 mat->entries[row_index * mat->cols + index]);
153         }
154 }
155
156 num vector_get_min(vector *vec)
157 {
158         unsigned index;
159         unsigned len;
160         num      min = INF_COSTS;
161
162         assert(vec);
163
164         len = vec->len;
165         assert(len > 0);
166
167         for (index = 0; index < len; ++index) {
168                 num elem = vec->entries[index].data;
169
170                 if (elem < min) {
171                         min = elem;
172                 }
173         }
174
175         return min;
176 }
177
178 unsigned vector_get_min_index(vector *vec)
179 {
180         unsigned index;
181         unsigned len;
182         unsigned min_index = 0;
183         num      min       = INF_COSTS;
184
185         assert(vec);
186
187         len = vec->len;
188         assert(len > 0);
189
190         for (index = 0; index < len; ++index) {
191                 num elem = vec->entries[index].data;
192
193                 if (elem < min) {
194                         min = elem;
195                         min_index = index;
196                 }
197         }
198
199         return min_index;
200 }