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

Private GIT Repository
Convert clock_type to enum class.
[loba.git] / cost_func.cpp
1 #include <algorithm>
2 #include <numeric>
3 #include <iterator>
4 #include <sstream>
5 #include <stdexcept>
6
7 #include "cost_func.h"
8
9 cost_func::cost_func(const char* param)
10 {
11     std::istringstream paramstream(param);
12     std::string token;
13     while (std::getline(paramstream, token, ',')) {
14         std::istringstream str(token);
15         double f;
16         if ((str >> f) && str.eof())
17             factors.push_back(f);
18         else
19             throw std::invalid_argument("cost_func(): "
20                                         "cannot parse \"" + token + "\"");
21     }
22     if (factors.empty())
23         throw std::invalid_argument("cost_func(): no factor");
24 }
25
26 cost_func::~cost_func()
27 {
28 }
29
30 double cost_func::operator()(double amount) const
31 {
32     return std::accumulate(++factors.begin(), factors.end(), factors.front(),
33                            [&amount](double a, double b) {
34                                return amount * a + b;
35                            });
36 }
37
38 std::string cost_func::to_string()
39 {
40     std::ostringstream oss;
41     std::copy(factors.begin(), --factors.end(),
42               std::ostream_iterator<double>(oss, ", "));
43     oss << factors.back();
44     return oss.str();
45 }
46
47 // Local Variables:
48 // mode: c++
49 // End: