LCOV - code coverage report
Current view: top level - app/src/data/VISUM - OFormatDemand.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 65 68 95.6 %
Date: 2023-08-17 16:45:52 Functions: 9 9 100.0 %

          Line data    Source code
       1             : #include "data/VISUM/OFormatDemand.hpp"
       2             : 
       3             : #include <fstream>
       4             : #include <iostream>
       5             : 
       6             : using namespace std;
       7             : using namespace VISUM;
       8             : 
       9             : typedef OFormatDemand::Node Node;
      10             : typedef OFormatDemand::Flow Flow;
      11             : typedef OFormatDemand::Time Time;
      12             : 
      13             : const double HOUR_TO_SECOND   = 60 * 60;
      14             : const double MINUTE_TO_SECOND = 60;
      15             : 
      16        7309 : Time OFormatDemand::getFrom() const { return from; }
      17        7309 : Time OFormatDemand::getTo() const { return to; }
      18             : 
      19        7309 : void OFormatDemand::addDemand(Node u, Node v, Flow f) {
      20        7309 :     flows[u][v] += f;
      21        7309 : }
      22             : 
      23           1 : vector<Node> OFormatDemand::getStartNodes() const {
      24           1 :     vector<Node> ret;
      25           1 :     ret.reserve(flows.size());
      26             : 
      27         120 :     for(const auto &[u, _]: flows)
      28         119 :         ret.push_back(u);
      29             : 
      30           1 :     return ret;
      31             : }
      32             : 
      33         119 : vector<Node> OFormatDemand::getDestinations(Node u) const {
      34         119 :     const auto &dest = flows.at(u);
      35             : 
      36         119 :     vector<Node> ret;
      37         119 :     ret.reserve(dest.size());
      38             : 
      39        7428 :     for(const auto &[v, _]: dest)
      40        7309 :         ret.push_back(v);
      41             : 
      42         119 :     return ret;
      43             : }
      44             : 
      45        7309 : Flow OFormatDemand::getDemand(Node u, Node v) const {
      46        7309 :     return flows.at(u).at(v);
      47             : }
      48             : 
      49       14164 : void ignoreCommentsOFile(istream &is) {
      50       14164 :     string line;
      51       28330 :     while(is.peek() == '\r' || is.peek() == '\n' || is.peek() == '*')
      52       14166 :         getline(is, line);
      53       14164 : }
      54             : 
      55           2 : pair<int, int> parseTime(const string &t) {
      56           2 :     size_t i = t.find(".");
      57           2 :     return make_pair(
      58           0 :         stoi(t.substr(0, i)),
      59           4 :         stoi(t.substr(i + 1))
      60           4 :     );
      61             : }
      62             : 
      63           1 : OFormatDemand OFormatDemand::loadFromFile(const string &path) {
      64           1 :     OFormatDemand demand;
      65             : 
      66           1 :     ifstream is;
      67           1 :     is.exceptions(ios_base::failbit | ios_base::badbit);
      68           1 :     try {
      69           1 :         is.open(path);
      70           0 :     } catch(const ios_base::failure &e) {
      71           0 :         throw ios_base::failure("Could not open file " + path);
      72             :     }
      73           1 :     if(is.peek() != '$') throw ios_base::failure("File is not VISUM");
      74           1 :     is.ignore(1);
      75           1 :     if(is.peek() != 'O') throw ios_base::failure("File is not O-formatted");
      76             : 
      77           2 :     string line;
      78           1 :     getline(is, line);
      79           1 :     ignoreCommentsOFile(is);
      80             : 
      81           2 :     string fromStr, toStr;
      82           1 :     is >> fromStr >> toStr;
      83           1 :     pair<int, int>
      84           1 :         fromPair = parseTime(fromStr),
      85           1 :         toPair   = parseTime(toStr);
      86           1 :     demand.from =
      87           1 :         fromPair.first * HOUR_TO_SECOND + fromPair.second * MINUTE_TO_SECOND;
      88           1 :     demand.to =
      89           1 :         toPair.first * HOUR_TO_SECOND + toPair.second * MINUTE_TO_SECOND;
      90             : 
      91           1 :     ignoreCommentsOFile(is);
      92             : 
      93           1 :     is >> demand.factor;
      94             : 
      95           1 :     ignoreCommentsOFile(is);
      96             : 
      97           1 :     is.exceptions(ios_base::badbit);
      98             : 
      99           2 :     Node u, v;
     100       14162 :     Flow f;
     101       14162 :     while(is >> u >> v >> f) {
     102       14161 :         ignoreCommentsOFile(is);
     103             : 
     104       14161 : #pragma GCC diagnostic push
     105       14161 : #pragma GCC diagnostic ignored "-Wfloat-equal"
     106       14161 :         if(f == 0.0) continue;
     107        7309 : #pragma GCC diagnostic pop
     108             : 
     109       29236 :         demand.addDemand(u, v, f);
     110             :     }
     111             : 
     112           2 :     return demand;
     113             : }

Generated by: LCOV version 1.14