2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
24 * @author Sebastian Buchwald
31 #include "adt/array.h"
35 num pbqp_add(num x, num y)
37 if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS;
41 /* No positive overflow. */
42 assert(x < 0 || y < 0 || res >= x);
43 assert(x < 0 || y < 0 || res >= y);
45 /* No negative overflow. */
46 assert(x > 0 || y > 0 || res <= x);
47 assert(x > 0 || y > 0 || res <= y);
49 /* Result is not infinity.*/
50 assert(res < INF_COSTS);
55 vector *vector_alloc(pbqp *pbqp, unsigned length)
58 vector *vec = obstack_alloc(&pbqp->obstack, sizeof(*vec) + sizeof(*vec->entries) * length);
62 memset(vec->entries, 0, sizeof(*vec->entries) * length);
67 vector *vector_copy(pbqp *pbqp, vector *v)
69 unsigned len = v->len;
70 vector *copy = obstack_copy(&pbqp->obstack, v, sizeof(*copy) + sizeof(*copy->entries) * len);
76 void vector_add(vector *sum, vector *summand)
83 assert(sum->len == summand->len);
87 for (i = 0; i < len; ++i) {
88 sum->entries[i].data = pbqp_add(sum->entries[i].data,
89 summand->entries[i].data);
93 void vector_set(vector *vec, unsigned index, num value)
95 assert(index < vec->len);
96 vec->entries[index].data = value;
99 #if KAPS_ENABLE_VECTOR_NAMES
100 void vector_set_description(vector *vec, unsigned index, const char *name)
102 assert(index < vec->len);
103 vec->entries[index].name = name;
107 void vector_add_value(vector *vec, num value)
116 for (index = 0; index < len; ++index) {
117 vec->entries[index].data = pbqp_add(vec->entries[index].data, value);
121 void vector_add_matrix_col(vector *vec, pbqp_matrix *mat, unsigned col_index)
128 assert(vec->len == mat->rows);
129 assert(col_index < mat->cols);
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]);
138 void vector_add_matrix_row(vector *vec, pbqp_matrix *mat, unsigned row_index)
145 assert(vec->len == mat->cols);
146 assert(row_index < mat->rows);
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]);
156 num vector_get_min(vector *vec)
167 for (index = 0; index < len; ++index) {
168 num elem = vec->entries[index].data;
178 unsigned vector_get_min_index(vector *vec)
182 unsigned min_index = 0;
190 for (index = 0; index < len; ++index) {
191 num elem = vec->entries[index].data;