00001
00002
00003
00004
00005
00006 #ifndef CSV1_
00007 #define CSV1_
00008
00009 #include "eleve/config.hpp"
00010
00011 #include <string>
00012 #include <map>
00013 #include <list>
00014 #include <set>
00015 #include <string>
00016 #include <stdexcept>
00017
00018 #include <boost/bimap.hpp>
00019 #include <boost/any.hpp>
00020 #include <boost/multi_array.hpp>
00021 #include <boost/function.hpp>
00022 #include <boost/regex.hpp>
00023
00024 #include "eleve/src/typing0.hpp"
00025
00026 namespace eepgwde { namespace detail {
00027
00031
00033 typedef std::map<std::string, boost::any *> record_t;
00034
00040 class DataFrame {
00041 public:
00042
00043 enum part_t { ROW, COLUMN };
00044
00049 typedef boost::bimap<int, std::string> attr_pos_t;
00050
00052 typedef attr_pos_t::left_map::const_iterator l_itr;
00053
00055 typedef attr_pos_t::right_map::const_iterator r_itr;
00056
00057 typedef boost::multi_array_types::index_range range;
00058 typedef boost::multi_array<boost::any, 2> ma_any_t;
00059
00061 typedef ma_any_t::array_view<2>::type payload_t;
00062
00064 typedef ma_any_t::array_view<1>::type column_t;
00065
00067 typedef payload_t::iterator iterator0;
00069 typedef column_t::iterator iterator1;
00070
00072 attr_pos_t header;
00073
00075 ma_any_t & frame;
00076
00078 column_t column(const std::string name0)
00079 throw (std::domain_error);
00080
00082 column_t column(int position0)
00083 throw (std::out_of_range);
00084
00086 Typing::types0_t::value_type column_t0(int position0)
00087 throw (std::out_of_range);
00088
00090 Typing::types0_t::value_type column_t0(const std::string name0)
00091 throw (std::domain_error);
00092
00094 Typing::types0_t & types0();
00095
00099 template <typename T>
00100 void render(column_t & view0,
00101 const boost::any
00102 null_ = (eepgwde::detail::null0<boost::any>())() )
00103 throw(std::domain_error) {
00104 for (iterator1 i = view0.begin(); i != view0.end(); ++i) {
00105 renderer<T>(*i, null_, 1, 0);
00106 }
00107 }
00108
00112 template <typename T>
00113 void render(const std::string colname0)
00114 throw(std::domain_error) {
00115 column_t myview = this->column(colname0);
00116 this->render<T>(myview);
00117 }
00118
00120 void render(const std::string colname0) throw(std::domain_error);
00121
00123 void render() throw(std::domain_error);
00124
00126 explicit DataFrame(ma_any_t & frame_,
00127 std::string overrides0 = std::string(""))
00128 throw(std::domain_error);
00129
00130 const std::string & overrides() const;
00131
00132 DataFrame(const DataFrame & df) throw(std::domain_error);
00133 DataFrame(DataFrame & df) throw(std::domain_error);
00134
00135 DataFrame & operator=(const DataFrame & t);
00136
00140 ma_any_t * add(ma_any_t *);
00141
00143 ma_any_t * add(DataFrame::ma_any_t *, const std::string & name0);
00144
00146 ma_any_t * add(DataFrame::ma_any_t *,
00147 const std::set<std::string> & names);
00148
00149 virtual ~DataFrame() throw();
00150
00151 #ifndef NDEBUG
00152
00154 int counter() const;
00155
00157 template <typename T>
00158 void simple(column_t & view0,
00159 const boost::any
00160 null_ = (eepgwde::detail::null0<boost::any>())() )
00161 throw(std::domain_error) {
00162 for (iterator1 i = view0.begin(); i != view0.end(); ++i) {
00163 std::cerr << counter() << " "
00164 << (*i).type().name()
00165 << std::endl;
00166 }
00167 eepgwde::daemon::watcher = 1;
00168 }
00169
00170 template <typename T>
00171 void simple(std::string colname0) throw(std::domain_error) {
00172 DataFrame::column_t myview = this->column(colname0);
00173 simple<T>(myview);
00174 }
00175
00176 #endif
00177
00179
00181 struct columnar {
00182 const DataFrame::column_t & view_;
00183 columnar(DataFrame::column_t & view) :
00184 view_(view) {};
00185 columnar(const DataFrame::column_t & view) :
00186 view_(view) {};
00187
00188 operator std::list<boost::any> ();
00189
00190 template <typename T>
00191 operator std::vector<T> () {
00192 std::vector<T> r;
00193 std::list<boost::any> l(*this);
00194 int fails=0;
00195
00196 r = * eepgwde::detail::render<T>(&r, &l, fails);
00197 return r;
00198 }
00199 };
00200
00204 struct columnar1 : public columnar {
00205 columnar1(DataFrame::column_t & view) :
00206 columnar(view) {};
00207 columnar1(const DataFrame::column_t & view) :
00208 columnar(view) {};
00209
00210 template <typename T>
00211 operator std::vector<T> () {
00212 std::vector<T> r = columnar(*this);
00213 sort(r.begin(), r.end());
00214 return std::vector<T>(r.begin(), unique(r.begin(), r.end()));
00215 }
00216 };
00217
00222 struct rowar {
00223 iterator0 view_;
00224 const DataFrame * owner_;
00225 rowar(const DataFrame * owner, iterator0 view) :
00226 owner_(owner), view_(view) {};
00227
00229 operator std::list<boost::any> ();
00231 operator record_t ();
00232 };
00233
00236 struct headar : public rowar {
00237 const Typing::mappings_t & mappings;
00238
00239 headar(const DataFrame * owner,
00240 const Typing::mappings_t & mappings) :
00241 rowar(owner, owner->frame.begin() ), mappings(mappings) {}
00242
00244 operator attr_pos_t () const;
00245
00248 };
00249
00253 struct Record {
00255 mutable DataFrame *df_;
00257 mutable DataFrame::iterator0 itr_;
00259 mutable eepgwde::detail::record_t r_;
00261 std::list<boost::any> attrs0_;
00263 record_t attrs_;
00264
00265 Record(DataFrame *df,
00266 DataFrame::iterator0 itr,
00267 eepgwde::detail::record_t r) : df_(df), itr_(itr), r_(r) {}
00268
00270 template <typename T>
00271 T as(const std::string & key) const throw (std::domain_error & ) {
00272 T r0;
00273 boost::any o = *(r_)[key];
00274 if (!eepgwde::detail::as<T>(&r0, o)) {
00275 eepgwde::daemon::watcher = 1;
00276 std::string mesg("as: failed: " + key + " " + o.type().name() );
00277 throw std::domain_error(mesg);
00278 }
00279 return r0;
00280 }
00281
00282 record_t * attrs() {
00283 return &attrs_;
00284 }
00285
00288 virtual void attributes(const std::string &) {
00289 return;
00290 }
00291 };
00292
00296 struct Comparor {
00297 const boost::regex & e;
00298 Comparor(const boost::regex & e) : e(e) {}
00299
00301 virtual bool compare(DataFrame::Record & r) = 0;
00302 };
00303
00306 ma_any_t * add(DataFrame::ma_any_t *,
00307 const std::vector<Record *> & xtra_attrs,
00308 std::set<std::string> names = std::set<std::string>());
00309
00313 struct payloader {
00314 DataFrame & df_;
00315
00317 payloader(DataFrame & df) : df_(df) {}
00318
00320 operator std::vector<DataFrame::iterator0> () {
00321 std::vector<DataFrame::iterator0> result;
00322
00323 for (DataFrame::iterator0 i=df_.payload->begin();
00324 i != df_.payload->end(); ++i)
00325 result.push_back(i);
00326
00327 return result;
00328 }
00329
00331 operator std::vector<eepgwde::detail::record_t> () {
00332 std::vector<eepgwde::detail::record_t>
00333 result;
00334
00335 for (DataFrame::iterator0 i=df_.payload->begin();
00336 i != df_.payload->end(); ++i) {
00337 eepgwde::detail::record_t r = DataFrame::rowar(&df_, i);
00338 result.push_back(r);
00339 }
00340 return result;
00341 }
00342
00344
00350 template <typename T>
00351 operator std::vector<T> () {
00352 std::vector<T> result;
00353
00354 for (DataFrame::iterator0 i=df_.payload->begin();
00355 i != df_.payload->end(); ++i) {
00356 eepgwde::detail::record_t r = DataFrame::rowar(&df_, i);
00357 result.push_back( T(&df_, i, r) );
00358 }
00359 return result;
00360 }
00361
00364 template <typename T>
00365 DataFrame::ma_any_t
00366 as(T b0, T e0, int headings=0) {
00367 std::vector<typename T::value_type> r(b0, e0);
00368 std::vector<DataFrame::Record * > s =
00369 pointerize<std::vector< DataFrame::Record *>, std::vector<typename T::value_type> >(r);
00370
00371 DataFrame::ma_any_t sorted0 = as0(s, headings);
00372 return sorted0;
00373 }
00374
00377 template <typename T>
00378 DataFrame::ma_any_t
00379 as(std::vector<T> & r, int headings=0) {
00380 std::vector<DataFrame::Record * > s =
00381 pointerize<std::vector< DataFrame::Record *>, std::vector<T> >(r);
00382 return as0(s, headings);
00383 }
00384
00386 DataFrame::ma_any_t
00387 as0(std::vector< DataFrame::Record * > & r, int headings);
00388
00389 };
00390
00391 boost::shared_ptr<eepgwde::detail::DataFrame::payload_t> payload;
00392
00393 class Impl;
00394 friend class Impl;
00395
00396 protected:
00397 std::auto_ptr<eepgwde::detail::DataFrame::Impl> impl;
00398 };
00399
00400 std::ostream& operator<< (std::ostream& os,
00401 const eepgwde::detail::DataFrame::attr_pos_t::left_map & l);
00402
00404
00406 std::ostream& operator<< (std::ostream& o,
00407 eepgwde::detail::DataFrame::ma_any_t & frame);
00408
00410
00411 #ifndef NDEBUG
00412
00413 void call(DataFrame & df);
00414 #endif
00415
00416 }}
00417
00418 #endif