6 #include <boost/numeric/ublas/vector.hpp>
7 #include <boost/thread/mutex.hpp>
9 #include <boost/filesystem.hpp>
11 #include "flame/util.h"
13 void numeric_table::readvec(std::vector<double> vec,
int numcol)
15 value_t tabletmp(
int(vec.size()/numcol),numcol);
16 for (
unsigned i=0; i < vec.size(); i++){
19 ii=int((i-jj)/numcol);
20 tabletmp(ii, jj) = vec[i];
22 this->table.swap(tabletmp);
25 void numeric_table::read(std::istream &strm)
27 typedef boost::numeric::ublas::vector<double> vector_t;
31 std::vector<double> values;
33 std::vector<vector_t> table;
36 while(std::getline(strm, rawline)) {
40 size_t cpos = rawline.find_first_not_of(
" \t");
41 if(cpos==rawline.npos)
44 cpos = rawline.find_last_not_of(
"\r\n");
45 if(cpos!=rawline.npos)
46 rawline = rawline.substr(0, cpos+1);
50 size_t P = rawline.find_first_not_of(
" \t", 1);
55 while(P!=rawline.npos) {
56 size_t E = rawline.find_first_of(
" \t", P);
58 const std::string& ent = rawline.substr(P, E==rawline.npos ? E : E-P);
61 P = rawline.find_first_not_of(
" \t", E);
69 std::istringstream lstrm(rawline);
75 values.push_back(val);
78 if(!lstrm.eof() && lstrm.fail())
79 throw std::runtime_error(SB()<<
"Error parsing data line "<<line<<
" '"<<rawline<<
"'");
81 if(!table.empty() && table.back().size()!=values.size())
82 throw std::runtime_error(SB()<<
"Line "<<line<<
" w/ different # of elements");
84 table.push_back(vector_t(values.size()));
86 std::copy(values.begin(),
88 table.back().begin());
92 if(!strm.eof() && strm.fail())
93 throw std::runtime_error(SB()<<
"Error parsing line "<<line<<
" '"<<rawline<<
"'");
94 else if(table.empty()) {
97 value_t result(table.size(), table.front().size());
99 for(
size_t r=0; r<result.size1(); r++) {
100 const vector_t& R=table[r];
101 for(
size_t c=0; c<result.size2(); c++) {
104 result.find2(2, r,0));
108 this->table.swap(result);
112 struct numeric_table_cache::Pvt {
117 typedef boost::shared_ptr<numeric_table> table_pointer;
121 typedef std::map<std::string, Value> cache_t;
125 numeric_table_cache::numeric_table_cache()
129 numeric_table_cache::~numeric_table_cache() {}
131 numeric_table_cache::table_pointer numeric_table_cache::fetch(
const std::string& path)
133 Pvt::Value::table_pointer ret;
135 boost::filesystem::path P(path);
137 throw std::logic_error(
"numeric_table_cache want's absolute paths");
139 std::time_t mtime = boost::filesystem::last_write_time(P);
141 boost::mutex::scoped_lock L(pvt->lock);
143 Pvt::cache_t::const_iterator it = pvt->cache.find(path);
144 if(it==pvt->cache.end() || mtime>it->second.lastmod) {
149 std::ifstream strm(path.c_str());
154 v.lastmod = boost::filesystem::last_write_time(P);
157 pvt->cache[path] = v;
159 ret = it->second.table;
165 void numeric_table_cache::clear()
167 boost::mutex::scoped_lock L(pvt->lock);
172 static numeric_table_cache ntc_single;
174 numeric_table_cache* numeric_table_cache::get()