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