Mercurial Hosting > traffic-intelligence
diff c/utils.cpp @ 144:b32947b002da
added the code to read matrices from text files
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 26 Aug 2011 19:38:11 -0400 |
parents | a52653dca25d |
children | 7bf8084e720f |
line wrap: on
line diff
--- a/c/utils.cpp Wed Aug 24 19:43:44 2011 -0400 +++ b/c/utils.cpp Fri Aug 26 19:38:11 2011 -0400 @@ -1,10 +1,50 @@ #include "utils.hpp" +#include <boost/foreach.hpp> + #include <iostream> #include <fstream> +#include <sstream> using namespace std; +vector<vector<float> > loadNumbers(const string& filename, const string& separator /* = " " */) { + ifstream in(filename.c_str()); + ::openCheck(in, filename, "loadNumbers"); + vector<vector<float> > result; + + while (!in.eof()) { + string line = ::getlineComment(in); + vector<string> tokens; + ::split(tokens, line, separator); + vector<float> numbers; + BOOST_FOREACH(string s, tokens) + numbers.push_back(::toInt(s)); + if (!numbers.empty()) + result.push_back(numbers); + } + + return result; +} + +int toInt(const std::string& s) { int i; fromString(i, s); return i;} //atoi + +float toFloat(const std::string& s) { float x; fromString(x, s); return x;}// lexical_cast<float>(s) + +double toDouble(const std::string& s) { double x; fromString(x, s); return x;} + +string getlineComment(ifstream& f) { + string s; + getline(f, s); + while ((!f.eof()) && (s[0] == ::commentChar)) { + getline(f, s); + } + + if (s[0] == ::commentChar) + s.clear(); + return s; +} + void openWriteScientificPrecision(ofstream& out, const string& filename, const int& precision) { ::openWritePrecision(out, filename, precision); out.setf(ios::scientific);