6 #include <boost/numeric/ublas/vector.hpp>
7 #include <boost/thread/mutex.hpp>
8 #define BOOST_FILESYSTEM_DYN_LINK
9 #include <boost/filesystem.hpp>
11 #include "flame/util.h"
13 void numeric_table::read(std::istream &strm)
15 typedef boost::numeric::ublas::vector<double> vector_t;
19 std::vector<double> values;
21 std::vector<vector_t> table;
24 while(std::getline(strm, rawline)) {
28 size_t cpos = rawline.find_first_not_of(
" \t");
29 if(cpos==rawline.npos)
32 cpos = rawline.find_last_not_of(
"\r\n");
33 if(cpos!=rawline.npos)
34 rawline = rawline.substr(0, cpos+1);
38 size_t P = rawline.find_first_not_of(
" \t", 1);
43 while(P!=rawline.npos) {
44 size_t E = rawline.find_first_of(
" \t", P);
46 const std::string& ent = rawline.substr(P, E==rawline.npos ? E : E-P);
49 P = rawline.find_first_not_of(
" \t", E);
57 std::istringstream lstrm(rawline);
63 values.push_back(val);
66 if(!lstrm.eof() && lstrm.fail())
67 throw std::runtime_error(SB()<<
"Error parsing data line "<<line<<
" '"<<rawline<<
"'");
69 if(!table.empty() && table.back().size()!=values.size())
70 throw std::runtime_error(SB()<<
"Line "<<line<<
" w/ different # of elements");
72 table.push_back(vector_t(values.size()));
74 std::copy(values.begin(),
76 table.back().begin());
80 if(!strm.eof() && strm.fail())
81 throw std::runtime_error(SB()<<
"Error parsing line "<<line<<
" '"<<rawline<<
"'");
82 else if(table.empty()) {
85 value_t result(table.size(), table.front().size());
87 for(
size_t r=0; r<result.size1(); r++) {
88 const vector_t& R=table[r];
89 for(
size_t c=0; c<result.size2(); c++) {
92 result.find2(2, r,0));
96 this->table.swap(result);
100 struct numeric_table_cache::Pvt {
105 typedef boost::shared_ptr<numeric_table> table_pointer;
109 typedef std::map<std::string, Value> cache_t;
113 numeric_table_cache::numeric_table_cache()
117 numeric_table_cache::~numeric_table_cache() {}
119 numeric_table_cache::table_pointer numeric_table_cache::fetch(
const std::string& path)
121 Pvt::Value::table_pointer ret;
123 boost::filesystem::path P(path);
125 throw std::logic_error(
"numeric_table_cache want's absolute paths");
127 std::time_t mtime = boost::filesystem::last_write_time(P);
129 boost::mutex::scoped_lock L(pvt->lock);
131 Pvt::cache_t::const_iterator it = pvt->cache.find(path);
132 if(it==pvt->cache.end() || mtime>it->second.lastmod) {
137 std::ifstream strm(path.c_str());
142 v.lastmod = boost::filesystem::last_write_time(P);
145 pvt->cache[path] = v;
147 ret = it->second.table;
153 void numeric_table_cache::clear()
155 boost::mutex::scoped_lock L(pvt->lock);
160 static numeric_table_cache ntc_single;
162 numeric_table_cache* numeric_table_cache::get()