Mercurial Hosting > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
143:436b87d4b992 | 144:b32947b002da |
---|---|
1 #include "utils.hpp" | 1 #include "utils.hpp" |
2 | |
3 #include <boost/foreach.hpp> | |
2 | 4 |
3 #include <iostream> | 5 #include <iostream> |
4 #include <fstream> | 6 #include <fstream> |
7 #include <sstream> | |
5 | 8 |
6 using namespace std; | 9 using namespace std; |
10 | |
11 vector<vector<float> > loadNumbers(const string& filename, const string& separator /* = " " */) { | |
12 ifstream in(filename.c_str()); | |
13 ::openCheck(in, filename, "loadNumbers"); | |
14 vector<vector<float> > result; | |
15 | |
16 while (!in.eof()) { | |
17 string line = ::getlineComment(in); | |
18 vector<string> tokens; | |
19 ::split(tokens, line, separator); | |
20 vector<float> numbers; | |
21 BOOST_FOREACH(string s, tokens) | |
22 numbers.push_back(::toInt(s)); | |
23 if (!numbers.empty()) | |
24 result.push_back(numbers); | |
25 } | |
26 | |
27 return result; | |
28 } | |
29 | |
30 int toInt(const std::string& s) { int i; fromString(i, s); return i;} //atoi | |
31 | |
32 float toFloat(const std::string& s) { float x; fromString(x, s); return x;}// lexical_cast<float>(s) | |
33 | |
34 double toDouble(const std::string& s) { double x; fromString(x, s); return x;} | |
35 | |
36 string getlineComment(ifstream& f) { | |
37 string s; | |
38 getline(f, s); | |
39 while ((!f.eof()) && (s[0] == ::commentChar)) { | |
40 getline(f, s); | |
41 } | |
42 | |
43 if (s[0] == ::commentChar) | |
44 s.clear(); | |
45 return s; | |
46 } | |
7 | 47 |
8 void openWriteScientificPrecision(ofstream& out, const string& filename, const int& precision) { | 48 void openWriteScientificPrecision(ofstream& out, const string& filename, const int& precision) { |
9 ::openWritePrecision(out, filename, precision); | 49 ::openWritePrecision(out, filename, precision); |
10 out.setf(ios::scientific); | 50 out.setf(ios::scientific); |
11 } | 51 } |