00001
00002
00003
00004
00005
00006
00007 #ifndef MMX_SEQ_HPP
00008 #define MMX_SEQ_HPP
00009
00010 # include <string.h>
00011 # include <vector>
00012 # include <algorithm>
00013 # include <realroot/parser.hpp>
00014 # include <realroot/shared_object.hpp>
00015 # include <realroot/assign.hpp>
00016 # include <realroot/array.hpp>
00017
00018
00019 namespace mmx {
00020
00021
00022 template<class C>
00023 std::ostream & operator<<(std::ostream & os, const std::vector<C> & V)
00024 {
00025 return mmx::array::print(os,V);
00026 }
00027
00028 template<class C, class S> struct Seq;
00029
00030 namespace let {
00031 template<class C,class R, class X, class S>
00032 void assign(Seq<C,R> & r, const Seq<X,S> & x);
00033 template<class C,class R, class S>
00034 void assign(Seq<C,R> & r, const Seq<C,S> & x);
00035 };
00036
00038
00058 template <class C, class R=std::vector<C> >
00059 struct Seq {
00060
00061 shared_object<R> data;
00062 R & rep() {return (*data);}
00063 const R & rep() const {return (*data);}
00064
00065 typedef typename R::size_type size_type;
00066 typedef C value_type;
00067 typedef C value_t;
00068 typedef typename R::iterator iterator;
00069 typedef typename R::const_iterator const_iterator;
00070 typedef typename R::reverse_iterator reverse_iterator;
00071 typedef typename R::const_reverse_iterator const_reverse_iterator;
00072 typedef Seq<C,R> self_t;
00073
00074 Seq(): data() {}
00075 Seq(const R & r):data(r){}
00076 Seq(size_type s) {rep().resize(s);}
00077 Seq(size_type s, value_type* t) {rep()=R(t,t+s);}
00078 Seq(iterator b,iterator e) {rep()=R(b,e);}
00079 Seq(size_type m, const char * str);
00080 Seq(char* str);
00081
00082
00083 Seq(const self_t & r): data(r.data) {}
00084 template<class X, class S>
00085 Seq(const Seq<X,S> & P) {using namespace let; assign(*this,P);}
00086
00087 iterator begin() {return rep().begin();}
00088 iterator end() {return rep().end();}
00089 const_iterator begin() const {return rep().begin();}
00090 const_iterator end() const {return rep().end();}
00091 reverse_iterator rbegin() {return rep().rbegin();}
00092 reverse_iterator rend() {return rep().rend();}
00093 const_reverse_iterator rbegin() const {return rep().rbegin();}
00094 const_reverse_iterator rend () const {return rep().rbegin();}
00095
00096
00097
00098 self_t & operator=(const self_t & V){data=V.data; return *this;}
00099 template<class X, class S>
00100 self_t & operator=(const Seq<X,S> & V) {
00101 using namespace let;
00102 assign(*this,V);
00103 return *this;}
00104
00105
00106 value_type & operator[] (size_type i) {return rep()[i];}
00107 const value_type & operator[] (size_type i) const {return rep()[i];}
00108
00109 const value_type & front() const { assert(this->size()>0); return rep()[0];}
00110 self_t & push_back(const C& x) { rep().push_back(x); return *this; }
00111
00112 self_t & erase(size_type i) {
00113
00114 rep().erase(rep().begin()+i);
00115 return *this; }
00116
00117 self_t & clear() { rep().clear();
00118 return *this; }
00119
00120 size_type search(const C& x) {
00121 for (size_type i=0;i< this->size();++i) {
00122 if ( this->rep()[i]== x)
00123 return i;
00124 }
00125
00126 return (size_type(-1) );
00127 }
00128
00129 bool member(const C& x) {
00130 for (size_type i=0;i< this->size();++i)
00131 if ( this->rep()[i]== x)
00132 return true;
00133 return false;
00134 }
00135
00136 void sort() {
00137 std::sort( this->rep().begin(), this->rep().end() );
00138 }
00139
00140 C min() {
00141 return *std::min_element( this->rep().begin(), this->rep().end());
00142 }
00143
00144 C max() {
00145 return *std::max_element( this->rep().begin(), this->rep().end());
00146 }
00147
00148 template<class Compare>
00149 void sort(Compare comp) {
00150 std::sort( this->rep().begin(), this->rep().end(),comp);
00151 }
00152
00153 template<class Compare>
00154 C min(Compare comp) {
00155 return *std::min_element( this->rep().begin(), this->rep().end(),comp);
00156 }
00157
00158 template<class Compare>
00159 C max(Compare comp) {
00160 return *std::max_element( this->rep().begin(), this->rep().end(),comp);
00161 }
00162
00163 size_type size() const {return rep().size();}
00164
00165 bool empty() const {return (rep().size()==0);}
00166 void resize(size_type i) { rep().resize(i); }
00167
00169 void reverse() { std::reverse (rep().begin( ), rep().end( ) ); }
00170 self_t reversed() const { self_t s(*this); s.reverse(); return s; }
00171
00173 self_t operator,(const self_t & x) const;
00174
00176 self_t operator,(const C & x) const;
00177
00178 self_t operator << (const self_t & s)
00179 {
00180 for(const_iterator it=s.begin(); it!=s.end();++it)
00181 this->push_back(*it);
00182 return *this;
00183 }
00184
00185 ~Seq () {}
00186
00187 };
00188
00189
00190 template<class C, class R> inline
00191 const C& read(const Seq<C,R>& m, unsigned i) { return m[i]; }
00192
00193 template<class C, class R>
00194 bool eq (const Seq<C,R>& v1, const Seq<C,R>& v2) {return v1==v2;}
00195 template<class C, class R>
00196 bool neq(const Seq<C,R>& v1, const Seq<C,R>& v2) {return v1!=v2;}
00197
00198 template<class C, class R>
00199 unsigned hash (const Seq<C,R>& v)
00200 {
00201 register unsigned i, h= 214365, n= v.size();
00202 for (i=0; i<n; i++)
00203 h= (h<<1) ^ (h<<5) ^ (h>>27) ^ hash(v[i]);
00204 return h;
00205 }
00206 template<class C, class R>
00207 unsigned soft_hash (const Seq<C,R>& m) {return hash(m);}
00208
00209 template<class C, class R>
00210 typename Seq<C,R>::const_iterator iterate(const Seq<C,R>& m) {return m.begin();}
00211
00212
00213 template<class C, class R>
00214 Seq<C,R> Seq<C,R>::operator,(const Seq<C,R> & x) const
00215 {
00216 self_t r(rep());
00217 for(const_iterator it=x.begin(); it!=x.end();++it)
00218 r.push_back(*it);
00219 return r;
00220 }
00221
00222 template<class C, class R>
00223 Seq<C,R> Seq<C,R>::operator,(const C & x) const
00224 {
00225 self_t r(rep());
00226 r.push_back(x);
00227 return r;
00228 }
00229
00230 template<class C,class R> inline
00231 Seq<C,R>& operator<<(Seq<C,R> & r, const C& x)
00232 {
00233 r.push_back(x); return r;
00234 }
00235
00236
00237 template <class C, class R>
00238 bool operator==(const Seq<C,R>& a, const Seq<C,R>& b)
00239 {return a.rep()==b.rep();}
00240 template <class C, class R>
00241 bool operator!=(const Seq<C,R>& a, const Seq<C,R>& b) {return !(a==b);}
00242
00243
00244
00245
00246
00248 template<class C, class R>
00249 std::ostream & operator<<(std::ostream & os, const Seq<C,R> & s)
00250 {
00251 if(s.size()){
00252 typename Seq<C,R>::const_iterator it=s.begin(); os<<*it; ++it;
00253 for( ;it!= s.end();++it) os <<", "<<*it;
00254 }
00255 return os;
00256 }
00257
00258
00263 template<class C,class R> inline
00264 std::istream & operator>>(std::istream & is, Seq<C,R> & V)
00265 {
00266 typedef typename R::size_type size_type;
00267 size_type s;
00268 is >> s;
00269 V.rep().resize(s);
00270 for(size_type i=0; i< s; ++i) is >> V[i];
00271 return(is);
00272 }
00273
00274 template<class C,class R>
00275 Seq<C,R> operator+(Seq<C,R> a, const Seq<C,R> & b)
00276 {
00277 Seq<C,R> r=a;
00278 for(typename Seq<C,R>::const_iterator it=b.begin();it!= b.end();++it)
00279 r.push_back(*it);
00280 return r;
00281 }
00282
00283
00284 template<class C,class R>
00285 Seq<C,R> adds( Seq<C,R> a, const Seq<C,R> & b)
00286 {
00287
00288 Seq<C,R> r=a;
00289
00290 typename Seq<C,R>::const_iterator it2= b.begin();
00291
00292 for(typename Seq<C,R>::iterator it=r.begin();it!= r.end();++it)
00293 {
00294 *it += *it2;
00295 ++it2;
00296 }
00297
00298 return r;
00299 }
00300
00301
00302
00303 template<class C,class R>
00304 Seq<C,R> mul( C b, Seq<C,R> a)
00305 {
00306 Seq<C,R> r=a;
00307 for(typename Seq<C,R>::iterator it=r.begin();it!= r.end();++it)
00308 *it= b * *it ;
00309 return r;
00310 }
00311
00312
00313 template<class C,class R>
00314 Seq<C,R> operator/( Seq<C,R> a, const C b)
00315 {
00316 Seq<C,R> r=a;
00317 for(typename Seq<C,R>::iterator it=r.begin();it!= r.end();++it)
00318 *it= *it / b;
00319 return r;
00320 }
00321
00322
00323 namespace let {
00324
00325 template<class C,class R, class S>
00326 void assign(Seq<C,R> & r, const Seq<C,S> & x)
00327 {
00328 r = Seq<C,R>();
00329 for(typename Seq<C,S>::const_iterator it=x.begin();it!= x.end();++it)
00330 r.push_back(*it);
00331 }
00332
00333 template<class C,class R, class X, class S>
00334 void assign(Seq<C,R> & r, const Seq<X,S> & x)
00335 {
00336 r = Seq<C,R>();
00337 for(typename Seq<X,S>::const_iterator it=x.begin();it!= x.end();++it)
00338 {
00339 C tmp;
00340 assign(tmp,*it);
00341 r.push_back(tmp);
00342 };
00343 }
00344
00345 }
00346
00347 template <class C,class R>
00348 Seq<C,R>::Seq (char * s): data()
00349 {
00350 rep().resize(0);
00351 synaps_input = s;
00352 synaps_inputptr = synaps_input;
00353 synaps_inputlim = s + strlen(s);
00354 C term;
00355 bool shift = true;
00356 int Ask;
00357
00358 for (;;) {
00359 Ask = yylex();
00360 if (shift && Ask == BLIST) {
00361 shift = false ;
00362 }
00363 else if (Ask == ELIST) {
00364 break;
00365 }
00366 else {
00367 let::assign(term,yylval);
00368 this->push_back(term);
00369 }
00370 }
00371 }
00372
00373
00374 struct ShapeForeachContainerBase {};
00375
00376 template <typename T> class ShapeForeachContainer : public ShapeForeachContainerBase {
00377 public:
00378 inline ShapeForeachContainer(const T& t): c(t), brk(0), i(c.begin()), e(c.end()) {} ;
00379 const T c ;
00380 mutable int brk ;
00381 mutable typename T::const_iterator i, e ;
00382 inline bool condition() const {
00383 return (!brk++ && i != e) ;
00384 }
00385 } ;
00386
00387 template <typename T> inline T *qForeachpointer(const T &) {
00388 return 0 ;
00389 }
00390
00391 template <typename T> inline ShapeForeachContainer<T> qForeachContainerNew(const T& t) {
00392 return ShapeForeachContainer<T>(t) ;
00393 }
00394
00395 template <typename T> inline const ShapeForeachContainer<T> *qForeachContainer(const ShapeForeachContainerBase *base, const T *) {
00396 return static_cast<const ShapeForeachContainer<T> *>(base) ;
00397 }
00398 # ifndef Shape_FOREACH
00399 # define Shape_FOREACH(variable, container) \
00400 for (const ShapeForeachContainerBase &_container_ = qForeachContainerNew(container); \
00401 qForeachContainer(&_container_, true ? 0 : qForeachpointer(container))->condition(); \
00402 ++qForeachContainer(&_container_, true ? 0 : qForeachpointer(container))->i) \
00403 for (variable = *qForeachContainer(&_container_, true ? 0 : qForeachpointer(container))->i; \
00404 qForeachContainer(&_container_, true ? 0 : qForeachpointer(container))->brk; \
00405 --qForeachContainer(&_container_, true ? 0 : qForeachpointer(container))->brk)
00406
00407
00408
00409 #ifndef foreach
00410 # define foreach Shape_FOREACH
00411 #endif
00412 #endif
00413
00414
00415 }
00416
00417 #endif // MMX_SEQ_HPP
00418