]> AND Private Git Repository - loba.git/blobdiff - cost_func.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Use sig_atomic_t for opt::exit_request.
[loba.git] / cost_func.cpp
index 23e6a53c140a53def6f39898b3a410aaea1a2033..12c20f6a2cd88824de1645f4c7650f4f6488c6a9 100644 (file)
@@ -1,57 +1,50 @@
 #include <algorithm>
 #include <algorithm>
-#include <cstdlib>
-#include <cstring>
+#include <tr1/functional>
+#include <numeric>
 #include <iterator>
 #include <sstream>
 #include <iterator>
 #include <sstream>
+#include <stdexcept>
+
 #include "cost_func.h"
 
 cost_func::cost_func(const char* param)
 {
 #include "cost_func.h"
 
 cost_func::cost_func(const char* param)
 {
-    int len = strlen(param);
-    char tmpbuf[len + 1];
-    char* tmp = tmpbuf;
-    memcpy(tmp, param, len + 1);
-    degree = std::count(tmp, tmp + len, ',');
-    factor = new double[degree + 1];
-    for (int i = degree ; i > 0 ; i--) {
-        char* next = strchr(tmp, ',');
-        *next++ = '\0';
-        std::istringstream(tmp) >> factor[i];
-        tmp = next;
+    std::istringstream paramstream(param);
+    std::string token;
+    while (std::getline(paramstream, token, ',')) {
+        std::istringstream str(token);
+        double f;
+        if ((str >> f) && str.eof())
+            factors.push_back(f);
+        else
+            throw std::invalid_argument("cost_func(): "
+                                        "cannot parse \"" + token + "\"");
     }
     }
-    std::istringstream(tmp) >> factor[0];
+    if (factors.empty())
+        throw std::invalid_argument("cost_func(): no factor");
 }
 
 cost_func::~cost_func()
 {
 }
 
 cost_func::~cost_func()
 {
-    delete[] factor;
-}
-
-cost_func& cost_func::operator=(const cost_func& ref)
-{
-    if (&ref != this) {
-        degree = ref.degree;
-        delete[] factor;
-        factor = new double[degree + 1];
-        memcpy(factor, ref.factor, (degree + 1) * sizeof *factor);
-    }
-    return *this;
 }
 
 double cost_func::operator()(double amount) const
 {
 }
 
 double cost_func::operator()(double amount) const
 {
-    double result = factor[degree];
-    for (int i = degree - 1; i >= 0 ; i--)
-        result = amount * result + factor[i];
-    return result;
+    using std::tr1::bind;
+    using std::tr1::placeholders::_1;
+    using std::tr1::placeholders::_2;
+    return std::accumulate(++factors.begin(), factors.end(), factors.front(),
+                           bind(std::plus<double>(),
+                                bind(std::multiplies<double>(), amount, _1),
+                                _2));
 }
 
 std::string cost_func::to_string()
 {
     std::ostringstream oss;
 }
 
 std::string cost_func::to_string()
 {
     std::ostringstream oss;
-    std::reverse_copy(factor + 1, factor + degree + 1,
-                      std::ostream_iterator<double>(oss, ", "));
-    oss << factor[0];
+    std::copy(factors.begin(), --factors.end(),
+              std::ostream_iterator<double>(oss, ", "));
+    oss << factors.back();
     return oss.str();
 }
 
     return oss.str();
 }