00001
00008 #include "eleve/src/typing3.hpp"
00009 #include "eleve/src/typing1.hpp"
00010
00011 #include "csv1.hpp"
00012 #include "csv5.hpp"
00013
00014 #include <iostream>
00015 #include <stdexcept>
00016
00017 #if defined(CSV1_NOGEN)
00018
00019 namespace {
00020 using namespace eepgwde::detail;
00021
00022 fn1_ref_t render_dt(&eepgwde::detail::DataFrame::render<posixdate>);
00023 fn1_ref_t render_usdt(&eepgwde::detail::DataFrame::render<usdate>);
00024 fn1_ref_t render_eudt(&eepgwde::detail::DataFrame::render<eudate>);
00025
00026 fn1_ref_t render_s(&eepgwde::detail::DataFrame::render<std::string>);
00027 fn1_ref_t render_i(&eepgwde::detail::DataFrame::render<int>);
00028
00029 fn1_ref_t render_d(&eepgwde::detail::DataFrame::render<double>);
00030 fn1_ref_t render_rate(&eepgwde::detail::DataFrame::render<rate>);
00031 fn1_ref_t render_bps(&eepgwde::detail::DataFrame::render<bps>);
00032
00033 #ifndef NDEBUG
00034 fn1_ref_t simple_i(&eepgwde::detail::DataFrame::simple<int>);
00035 fn1_ref_t simple_d(&eepgwde::detail::DataFrame::simple<double>);
00036 #endif
00037
00038 }
00039
00040 namespace eepgwde { namespace detail {
00041
00042
00043
00044
00045 static render0_t render0s_[] = {
00046 { std::string("N7eepgwde6detail9posixdateE"), &render_dt },
00047 { std::string("Ss"), &render_s },
00048 { std::string("d"), &render_d },
00049 { std::string("i"), &render_i },
00050 { std::string(), 0 }
00051 };
00052
00053 static col_t defaults_attrs[] = {
00054 { std::string("PX_BID"), std::string("d") },
00055 { std::string("PX_ASK"), std::string("d") },
00056 { std::string("PX_LAST"), std::string("d") },
00057 { std::string(""), std::string("") }
00058 };
00059
00060 static col_t eurisc_attrs[] = {
00061 { std::string("PX"), std::string("Ss") },
00062 { std::string("X0"), std::string("Ss") },
00063 { std::string("X1"), std::string("d") },
00064 { std::string("X2"), std::string("i") },
00065 { std::string(""), std::string("") }
00066 };
00067
00068 static col_overrides_t overrides_attrs[] = {
00069 { std::string("eurisc"), eurisc_attrs }
00070 };
00071
00072
00073 }}
00074 #endif
00075
00076 namespace eepgwde { namespace detail {
00077
00078 #if !defined(CSV_NOGEN)
00079 extern col_t defaults_attrs[];
00080 extern render0_t render0s_[];
00081 extern col_overrides_t overrides_attrs[];
00082 #endif
00083
00089 struct DataFrame::Impl {
00090 friend class DataFrame;
00091
00092 int counter;
00093
00094 const std::string overrides0;
00095
00097 map_t defaults;
00098
00100 render0_map_t renderers;
00101
00107 map_overrides_t moverrides;
00108
00111 map_t overrides;
00112
00114 Typing::types0_t types0;
00115
00117
00123 Impl(const std::string & overrides0_) :
00124 counter(0), overrides0(overrides0_), types0(Typing().types0()) {
00125
00126 for (int i=0; render0s_[i].p; i++)
00127 renderers[ render0s_[i].name0 ] = render0s_[i].p;
00128
00129 for (int i=0; defaults_attrs[i].old_.size() > 0; i++)
00130 defaults[ defaults_attrs[i].old_ ] = defaults_attrs[i].new_;
00131
00132 for (int i=0; overrides_attrs[i].p; i++)
00133 moverrides[ overrides_attrs[i].name0 ] = overrides_attrs[i].p;
00134
00135 map_overrides_t::const_iterator i;
00136 if ( (i=moverrides.find(overrides0)) != moverrides.end()) {
00137 col_t * overrides_attrs = i->second;
00138 for (int i=0; overrides_attrs[i].old_.size() > 0; i++)
00139 overrides[ overrides_attrs[i].old_ ] = overrides_attrs[i].new_;
00140 }
00141 }
00142
00143 };
00144
00145 DataFrame::~DataFrame() throw() { }
00146
00147 Typing::types0_t & DataFrame::types0() {
00148 return impl->types0;
00149 }
00150
00152 Typing::types0_t::value_type DataFrame::column_t0(int position0)
00153 throw (std::out_of_range) {
00154 std::string s("index: " + boost::lexical_cast<std::string>(position0) );
00155
00156 l_itr i = header.left.find(position0);
00157 if (i == header.left.end()) throw std::out_of_range(s);
00158
00159 return this->column_t0(i->second);
00160 }
00161
00163 Typing::types0_t::value_type DataFrame::column_t0(const std::string colname0)
00164 throw (std::domain_error) {
00165 map_t::iterator render0;
00166 std::string s("index: ");
00167 s += colname0;
00168
00169 if ( (render0 = impl->overrides.find(colname0)) == impl->overrides.end() )
00170 if ( (render0 = impl->defaults.find(colname0)) == impl->defaults.end() )
00171 throw std::domain_error(s);
00172
00173 Typing::types0_t::iterator p;
00174 if ( (p = impl->types0.find(render0->second)) == impl->types0.end() )
00175 throw std::domain_error(s);
00176
00177 return *p;
00178 }
00179
00180 DataFrame::payload_t init0(DataFrame::ma_any_t & frame_) {
00181 if (frame_.shape()[0] == 0 &&
00182 frame_.shape()[1] == 0)
00183 return frame_[ boost::indices[DataFrame::range()][DataFrame::range()] ];
00184
00185 return frame_[ boost::indices[DataFrame::range().start(1)][DataFrame::range()] ];
00186 }
00187
00189
00192
00193 DataFrame::DataFrame(ma_any_t & frame_,
00194 std::string overrides0)
00195 throw(std::domain_error) :
00196 frame(frame_), impl(new Impl(overrides0)),
00197 payload( new payload_t(init0(frame_)) ) {
00198
00199 if (frame_.shape()[0] == 0 &&
00200 frame_.shape()[1] == 0) return;
00201
00202 payload_t myview =
00203 frame[ boost::indices[range() <= 0][range()] ];
00204
00205 DataFrame::ma_any_t::size_type rows0 = myview.shape()[0];
00206 DataFrame::ma_any_t::size_type cols0 = myview.shape()[1];
00207
00208 for (DataFrame::ma_any_t::size_type i = 0; i < rows0; ++i) {
00209 for (DataFrame::ma_any_t::size_type j = 0; j < cols0; ++j) {
00210 std::string name0(boost::any_cast<std::string>(myview[i][j]));
00211 header.insert( DataFrame::attr_pos_t::value_type(j, name0 ));
00212 }
00213 }
00214 }
00215
00216 DataFrame & DataFrame::operator=(const DataFrame & t) {
00217 if (this != &t) {
00218 boost::array<DataFrame::ma_any_t::index, 2>
00219 shape ={{ t.frame.shape()[0], t.frame.shape()[1] }};
00220 frame.resize(shape);
00221 frame = t.frame;
00222 std::auto_ptr<Impl> impl0(new Impl(t.impl->overrides0));
00223 impl = impl0;
00224 header = t.header;
00225 payload_t * p = new DataFrame::payload_t(*t.payload);
00226 payload = boost::shared_ptr<DataFrame::payload_t>(p);
00227 }
00228 return *this;
00229 }
00230
00231 DataFrame::DataFrame(const DataFrame & df) throw(std::domain_error) :
00232 frame(df.frame), impl(new Impl(df.impl->overrides0)),
00233 payload( new DataFrame::payload_t(init0(frame)) ),
00234 header(df.header) {
00235 }
00236
00237 DataFrame::DataFrame(DataFrame & df) throw(std::domain_error) :
00238 frame(df.frame), impl(new Impl(df.impl->overrides0)),
00239 payload( new DataFrame::payload_t(init0(frame)) ),
00240 header(df.header) {
00241 }
00242
00243 const std::string & DataFrame::overrides() const {
00244 return impl->overrides0;
00245 }
00246
00247 DataFrame::column_t
00248 DataFrame::column(int position0)
00249 throw (std::out_of_range) {
00250 try {
00251 DataFrame::column_t myview =
00252 (*payload)[ boost::indices[range()][position0] ];
00253 return myview;
00254 } catch (std::exception & e) {
00255 std::string s("index: ");
00256 s += boost::lexical_cast<std::string>(position0);
00257 throw std::out_of_range(s);
00258 }
00259 }
00260
00261 DataFrame::column_t
00262 DataFrame::column(const std::string name0)
00263 throw (std::domain_error) {
00264 std::string s("index: ");
00265 s += name0;
00266
00267 DataFrame::r_itr a = header.right.find(name0);
00268 if (a != header.right.end()) {
00269 ;
00270 } else {
00271 throw std::domain_error(s);
00272 }
00273 try {
00274 return column(a->second);
00275 } catch (std::out_of_range & e) {
00276 throw std::domain_error(s);
00277 }
00278 eepgwde::daemon::watcher = 4;
00279 return column(0);
00280 }
00281
00282 void DataFrame::render(const std::string colname0) throw(std::domain_error) {
00283 map_t::iterator render0;
00284 std::string s("index: ");
00285 s += colname0;
00286
00287 if ( (render0 = impl->overrides.find(colname0)) == impl->overrides.end() )
00288 if ( (render0 = impl->defaults.find(colname0)) == impl->defaults.end() )
00289 throw std::domain_error(s);
00290
00291 render0_map_t::iterator p = impl->renderers.find(render0->second);
00292
00293 if (p == impl->renderers.end())
00294 throw std::domain_error("no render function defined for " + s);
00295
00296 (*(p->second))(*this, colname0);
00297
00298 return;
00299 }
00300
00302
00304 void DataFrame::render() throw(std::domain_error) {
00305 DataFrame::r_itr n0;
00306 std::domain_error e1("");
00307 bool fail = 0;
00308
00309 for(n0 = header.right.begin();
00310 n0 != header.right.end(); ++n0) {
00311 try {
00312 render(n0->first);
00313 } catch (std::domain_error & e) {
00314 e1 = e;
00315 fail = 1;
00316 }
00317 }
00318
00319 if (fail) {
00320 throw e1;
00321 }
00322 }
00323
00325
00327 DataFrame::ma_any_t * DataFrame::add(ma_any_t * result) {
00328 std::list<boost::any> data0;
00329
00330 int rows0 = frame.shape()[0];
00331 int cols0 = frame.shape()[1];
00332
00333 for (iterator0 i = frame.begin(); i != frame.end(); ++i) {
00334 for(iterator1 ii = (*i).begin(); ii != (*i).end(); ++ii) {
00335 data0.push_back(*ii);
00336 }
00337 }
00338
00339 for (int i=0; i<=cols0; i++)
00340 data0.push_back(boost::any());
00341
00342 boost::array<DataFrame::ma_any_t::index, 2> shape0 ={{ rows0 + 1, cols0 }};
00343
00344 result->resize(shape0);
00345 result->assign(data0.begin(), data0.end());
00346
00347 return result;
00348 }
00349
00351
00353 DataFrame::ma_any_t * DataFrame::add(DataFrame::ma_any_t * result,
00354 const std::string & name0) {
00355 std::list<boost::any> data0;
00356
00357 int rows0 = frame.shape()[0];
00358 int cols0 = frame.shape()[1];
00359
00360 for (iterator0 i = frame.begin(); i != frame.end(); ++i) {
00361 for(iterator1 ii = (*i).begin(); ii != (*i).end(); ++ii) {
00362 data0.push_back(*ii);
00363 }
00364 if (i == frame.begin())
00365 data0.push_back(boost::any(name0));
00366 else
00367 data0.push_back(boost::any());
00368 }
00369
00370 boost::array<DataFrame::ma_any_t::index, 2> shape0 ={{ rows0, cols0 + 1 }};
00371
00372 result->resize(shape0);
00373 result->assign(data0.begin(), data0.end());
00374
00375 return result;
00376 }
00377
00379
00381 DataFrame::ma_any_t * DataFrame::add(DataFrame::ma_any_t * result,
00382 const std::set<std::string> & name0) {
00383 std::list<boost::any> data0;
00384
00385 int rows0 = frame.shape()[0];
00386 int cols0 = frame.shape()[1];
00387
00388 for (iterator0 i = frame.begin(); i != frame.end(); ++i) {
00389 for(iterator1 ii = (*i).begin(); ii != (*i).end(); ++ii) {
00390 data0.push_back(*ii);
00391 }
00392 if (i == frame.begin()) {
00393 for(std::set<std::string>::const_iterator i=name0.begin();
00394 i != name0.end(); ++i)
00395 data0.push_back(boost::any(*i));
00396 }
00397 else {
00398 for(std::set<std::string>::const_iterator i=name0.begin();
00399 i != name0.end(); ++i)
00400 data0.push_back(boost::any());
00401 }
00402 }
00403
00404 boost::array<DataFrame::ma_any_t::index, 2>
00405 shape0 ={{ rows0, cols0 + name0.size() }};
00406
00407 result->resize(shape0);
00408 result->assign(data0.begin(), data0.end());
00409
00410 return result;
00411 }
00412
00413 struct Collector {
00414 std::set<std::string> names;
00415
00416 void collect(const record_t * r) {
00417 for(record_t::const_iterator i=r->begin();
00418 i != r->end(); ++i) {
00419 names.insert(i->first);
00420 }
00421 }
00422 };
00423
00425
00427 DataFrame::ma_any_t *
00428 DataFrame::add(DataFrame::ma_any_t * result,
00429 const std::vector<DataFrame::Record *> & xtra,
00430 std::set<std::string> name0) {
00431 std::list<boost::any> data0;
00432
00433 if (! name0.size() > 0) {
00434 std::list<record_t *> attrs;
00435 back_insert_iterator< std::list<record_t *> > ii(attrs);
00436 transform(xtra.begin(), xtra.end(),
00437 ii,
00438 boost::bind(&DataFrame::Record::attrs,_1));
00439
00440 Collector c;
00441 for_each(attrs.begin(), attrs.end(),
00442 boost::bind(&Collector::collect,boost::ref(c),_1));
00443
00444 name0 = c.names;
00445 }
00446
00447 if (! name0.size() > 0) return 0;
00448
00449 int rows0 = frame.shape()[0];
00450 int cols0 = frame.shape()[1];
00451
00452 std::vector<DataFrame::Record *>::const_iterator
00453 ix = xtra.begin();
00454
00455 for (iterator0 i = frame.begin(); i != frame.end(); ++i) {
00456 for(iterator1 ii = (*i).begin(); ii != (*i).end(); ++ii) {
00457 data0.push_back(*ii);
00458 }
00459 if (i == frame.begin()) {
00460 for(std::set<std::string>::const_iterator i=name0.begin();
00461 i != name0.end(); ++i)
00462 data0.push_back(boost::any(*i));
00463 }
00464 else {
00465 DataFrame::Record * r = *ix;
00466 record_t & r0 = r->attrs_;
00467 for(std::set<std::string>::const_iterator i=name0.begin();
00468 i != name0.end(); ++i) {
00469 data0.push_back( * r0[*i] );
00470 }
00471 ++ix;
00472 }
00473 }
00474
00475 boost::array<DataFrame::ma_any_t::index, 2>
00476 shape0 ={{ rows0, cols0 + name0.size() }};
00477
00478 result->resize(shape0);
00479 result->assign(data0.begin(), data0.end());
00480
00481 return result;
00482 }
00483
00484 DataFrame::columnar::operator std::list<boost::any>() {
00485 std::list<boost::any> result(view_.size());
00486 copy(view_.begin(), view_.end(), result.begin());
00487 return result;
00488 }
00489
00490 DataFrame::rowar::operator std::list<boost::any>() {
00491 std::list<boost::any> data0;
00492 for(iterator1 ii = (*view_).begin(); ii != (*view_).end(); ++ii) {
00493 data0.push_back(*ii);
00494 }
00495 return data0;
00496 }
00497
00498 DataFrame::rowar::operator record_t () {
00499 record_t m;
00500
00501 DataFrame::iterator1 ii;
00502 DataFrame::l_itr lhi;
00503 for(ii = (*view_).begin(), lhi = owner_->header.left.begin();
00504 ii != (*view_).end() && lhi != owner_->header.left.end();
00505 ++ii, ++lhi) {
00506 m[lhi->second] = &(*ii);
00507 }
00508 return m;
00509 }
00510
00511 DataFrame::ma_any_t
00512 DataFrame::payloader::as0(std::vector< DataFrame::Record * > & r,
00513 int headings) {
00514 std::list<boost::any> data;
00515 int rows0 = 0, cols0 = 0;
00516 rows0 = r.size();
00517 cols0 = df_.frame.shape()[1];
00518
00519 if (!headings) {
00521 DataFrame::iterator0 ir = df_.frame.begin();
00522 for (DataFrame::iterator1 cr = ir->begin();
00523 cr != ir->end(); ++cr) {
00524 data.push_back(*cr);
00525 }
00526 } else {
00527 for(DataFrame::l_itr i=df_.header.left.begin();
00528 i != df_.header.left.end(); ++i) {
00529 data.push_back( boost::any( i->second ) );
00530 }
00531 }
00532
00533 for (std::vector<DataFrame::Record *>::iterator i = r.begin();
00534 i != r.end(); ++i) {
00535 DataFrame::iterator0 ir = (*i)->itr_;
00536 for (DataFrame::iterator1 cr = ir->begin();
00537 cr != ir->end(); ++cr) {
00538 data.push_back(*cr);
00539 }
00540 }
00541
00543 boost::array<DataFrame::ma_any_t::index, 2> shape1 ={{ rows0 + 1, cols0 }};
00544 ma_any_t t(shape1);
00545 t.assign(data.begin(), data.end());
00546
00547 return t;
00548 }
00549
00551 DataFrame::headar::operator DataFrame::attr_pos_t () const {
00552 DataFrame::attr_pos_t::right_map::iterator ri;
00553 attr_pos_t h0(owner_->header);
00554
00555 for(Typing::mappings_t::const_iterator i = mappings.begin();
00556 i != mappings.end(); ++i) {
00557 if ( (ri = h0.right.find(i->first)) != h0.right.end() ) {
00558 bool changed =
00559 h0.right.replace_key(ri, i->second);
00560 }
00561 }
00562
00563 return h0;
00564 }
00565
00567 static std::ostream & dumper(std::ostream & o,
00568 DataFrame::ma_any_t & frame) {
00569 for (DataFrame::iterator0 i = frame.begin(); i != frame.end(); ++i) {
00570 for(DataFrame::iterator1 ii = (*i).begin(); ii != (*i).end(); ++ii) {
00571 o << (*ii).type().name() << " " << as(*ii) << " ";
00572 }
00573 o << std::endl;
00574 }
00575 return o;
00576 }
00577
00579 std::ostream& operator<< (std::ostream& o, DataFrame::ma_any_t & frame) {
00580 return dumper(o, frame);
00581 }
00582
00583 std::ostream& operator<< (std::ostream& os,
00584 const eepgwde::detail::DataFrame::attr_pos_t::left_map & l) {
00585 for(DataFrame::l_itr i = l.begin(); i != l.end(); ++i)
00586 os << i->first << " <--> " << i->second << "\n";
00587 return os;
00588 }
00589
00590 #ifndef NDEBUG
00591 int DataFrame::counter() const {
00592 return impl->counter++;
00593 }
00594
00595 void call(DataFrame & df) {
00596 col_t t;
00597 df.render("PX");
00598 df.render("X1");
00599 df.render("PX_ASK");
00600 }
00601 #endif
00602
00603 }}
00604