- brute force solver need own back propagation
[libfirm] / vector.c
index 30991e2..c643cd5 100644 (file)
--- a/vector.c
+++ b/vector.c
@@ -1,10 +1,57 @@
+/*
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
+/**
+ * @file
+ * @brief   PBQP vector.
+ * @date    02.10.2008
+ * @author  Sebastian Buchwald
+ * @version $Id$
+ */
+#include "config.h"
+
 #include <string.h>
 
 #include "adt/array.h"
 
-#include "pbqp_t.h"
 #include "vector.h"
 
+num pbqp_add(num x, num y)
+{
+       if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS;
+
+       num res = x + y;
+
+       /* No positive overflow. */
+       assert(x < 0 || y < 0 || res >= x);
+       assert(x < 0 || y < 0 || res >= y);
+
+       /* No negative overflow. */
+       assert(x > 0 || y > 0 || res <= x);
+       assert(x > 0 || y > 0 || res <= y);
+
+       /* Result is not infinity.*/
+       assert(res < INF_COSTS);
+
+       return res;
+}
+
 vector *vector_alloc(pbqp *pbqp, unsigned length)
 {
        assert(length > 0);
@@ -38,13 +85,8 @@ void vector_add(vector *sum, vector *summand)
        len = sum->len;
 
        for (i = 0; i < len; ++i) {
-               if (sum->entries[i].data == INF_COSTS) continue;
-
-               if (summand->entries[i].data == INF_COSTS) {
-                       sum->entries[i].data = INF_COSTS;
-               } else {
-                       sum->entries[i].data += summand->entries[i].data;
-               }
+               sum->entries[i].data = pbqp_add(sum->entries[i].data,
+                               summand->entries[i].data);
        }
 }
 
@@ -54,8 +96,8 @@ void vector_set(vector *vec, unsigned index, num value)
        vec->entries[index].data = value;
 }
 
-#if EXT_GRS_DEBUG
-void vector_set_description(vector *vec, unsigned index, char *name)
+#if KAPS_ENABLE_VECTOR_NAMES
+void vector_set_description(vector *vec, unsigned index, const char *name)
 {
        assert(index < vec->len);
        vec->entries[index].name = name;
@@ -72,13 +114,7 @@ void vector_add_value(vector *vec, num value)
        len = vec->len;
 
        for (index = 0; index < len; ++index) {
-               if (vec->entries[index].data == INF_COSTS) continue;
-
-               if (value == INF_COSTS) {
-                       vec->entries[index].data = INF_COSTS;
-               } else {
-                       vec->entries[index].data += value;
-               }
+               vec->entries[index].data = pbqp_add(vec->entries[index].data, value);
        }
 }
 
@@ -95,13 +131,7 @@ void vector_add_matrix_col(vector *vec, pbqp_matrix *mat, unsigned col_index)
        len = vec->len;
 
        for (index = 0; index < len; ++index) {
-               if (vec->entries[index].data == INF_COSTS) continue;
-
-               if (mat->entries[index * mat->cols + col_index] == INF_COSTS) {
-                       vec->entries[index].data = INF_COSTS;
-               } else {
-                       vec->entries[index].data += mat->entries[index * mat->cols + col_index];
-               }
+               vec->entries[index].data = pbqp_add(vec->entries[index].data, mat->entries[index * mat->cols + col_index]);
        }
 }
 
@@ -118,14 +148,31 @@ void vector_add_matrix_row(vector *vec, pbqp_matrix *mat, unsigned row_index)
        len = vec->len;
 
        for (index = 0; index < len; ++index) {
-               if (vec->entries[index].data == INF_COSTS) continue;
+               vec->entries[index].data = pbqp_add(vec->entries[index].data,
+                               mat->entries[row_index * mat->cols + index]);
+       }
+}
+
+num vector_get_min(vector *vec)
+{
+       unsigned index;
+       unsigned len;
+       num      min = INF_COSTS;
+
+       assert(vec);
+
+       len = vec->len;
+       assert(len > 0);
+
+       for (index = 0; index < len; ++index) {
+               num elem = vec->entries[index].data;
 
-               if (mat->entries[row_index * mat->cols + index] == INF_COSTS) {
-                       vec->entries[index].data = INF_COSTS;
-               } else {
-                       vec->entries[index].data += mat->entries[row_index * mat->cols + index];
+               if (elem < min) {
+                       min = elem;
                }
        }
+
+       return min;
 }
 
 unsigned vector_get_min_index(vector *vec)