synaps/base/Seq.h

00001 /*********************************************************************
00002 *   This file is part of the source code of the SYNAPS kernel.       *
00003 *   Author(s): B. Mourrain, GALAAD, INRIA                            *
00004 **********************************************************************
00005 $Id: Seq.h,v 1.3 2005/09/28 06:40:51 mourrain Exp $
00006 **********************************************************************/
00007 #ifndef SYNAPS_UTIL_SEQ_H
00008 #define SYNAPS_UTIL_SEQ_H
00009 //----------------------------------------------------------------------
00010 #include <synaps/init.h>
00011 #include <vector>
00012 #include <synaps/base/parser.h>
00013 #include <synaps/base/Range.h>
00014 #include <synaps/base/seq1d.h>
00015 #include <synaps/base/shared_object.h>
00016 #include <synaps/arithm/let.h>
00017 //----------------------------------------------------------------------
00018 
00019 __BEGIN_NAMESPACE_SYNAPS
00021 
00041 template <class C, class R=std::vector<C> >
00042 struct Seq {
00043 
00044   shared_object<R>  data;
00045   R &       rep()       {return (*data);}
00046   const R & rep() const {return (*data);}
00047 
00048   typedef typename R::size_type              size_type;
00049   typedef C                                  value_type;
00050   typedef typename R::iterator               iterator;
00051   typedef typename R::const_iterator         const_iterator;
00052   typedef typename R::reverse_iterator       reverse_iterator;
00053   typedef typename R::const_reverse_iterator const_reverse_iterator;
00054   typedef Seq<C,R>                           self_t;
00055 
00056   Seq(): data() {}
00057   Seq(const R & r):data(r){}
00058   Seq(size_type s) {rep().resize(s);}
00059   Seq(size_type s, value_type* t) {rep()=R(t,t+s);}
00060   Seq(iterator b,iterator e) {rep()=R(b,e);}
00061   Seq(size_type m, const char * str);
00062   Seq(char* str);
00063 
00064   // Copy constructor
00065   Seq(const self_t & r): data(r.data) {}
00066   template<class X, class S>
00067   Seq(const Seq<X,S> & P) {let::assign(*this,P);}
00068 
00069   iterator               begin()        {return rep().begin();}
00070   iterator               end()          {return rep().end();}
00071   const_iterator         begin()  const {return rep().begin();}
00072   const_iterator         end()    const {return rep().end();}
00073   reverse_iterator       rbegin()       {return rep.rbegin();}
00074   reverse_iterator       rend()         {return rep.rend();}
00075   const_reverse_iterator rbegin() const {return rep.rbegin();}
00076   const_reverse_iterator rend  () const {return rep.rbegin();}
00077 
00078   // Assignement
00079   self_t & operator=(const self_t & V){data=V.data; return *this;}
00080   template<class X, class S>
00081   self_t & operator=(const Seq<X,S> & V) {let::assign(*this,V); return *this;}
00082   //self_t & operator=(const Seq<X,S> & V) {assign(*this,V); return *this;}
00083 
00084   value_type & operator[] (size_type i)       {return rep()[i];}
00085   value_type   operator[] (size_type i) const {return rep()[i];}
00086 
00087   Seq<value_type, seq1d<value_type,iterator> > operator[](const Range & I)
00088     {
00089       typedef seq1d<value_type,iterator>  rep_type;
00090       return Seq<value_type, rep_type>
00091         (rep_type(I.i2-I.i1+1,begin()+I.i1,begin()+I.i2+1));
00092     };
00093 
00094   self_t & push_back(const C& x)  { rep().push_back(x); return *this; }
00095 
00096   size_type size() const {return rep().size();}
00097 
00098 
00099   bool  empty()  const {return (rep().size()==0);}
00100   void resize(size_type i) { rep().resize(i); }
00101 
00103   self_t operator,(const self_t & x) const;
00104 
00106   self_t operator,(const C & x) const;
00107 
00108   self_t operator << (const self_t & s)
00109   {
00110     for(const_iterator it=s.begin(); it!=s.end();++it) 
00111       this->push_back(*it);
00112     return *this;
00113   }
00114 
00115   ~Seq () {}
00116 
00117 };
00118 //--------------------------------------------------------------------
00119 template<class C, class R>
00120 Seq<C,R> Seq<C,R>::operator,(const Seq<C,R> & x) const
00121 {
00122   self_t r(rep());
00123   for(const_iterator it=x.begin(); it!=x.end();++it)
00124     r.push_back(*it);
00125   return r;
00126 }
00127 //--------------------------------------------------------------------
00128 template<class C, class R>
00129 Seq<C,R> Seq<C,R>::operator,(const C & x) const
00130 {
00131   self_t r(rep());
00132   r.push_back(x);
00133   return r;
00134 }
00135 //--------------------------------------------------------------------
00136 template <class C, class R>
00137 bool operator==(const Seq<C,R>& a, const Seq<C,R>& b)
00138    {return a.rep()==b.rep();}
00139 template <class C, class R>
00140 bool operator!=(const Seq<C,R>& a, const Seq<C,R>& b) {return !(a==b);}
00141 //======================================================================
00142 // INPUT OUTPUT
00143 //======================================================================
00145 template<class C, class R>
00146 std::ostream & operator<<(std::ostream & os, const Seq<C,R> & s)
00147 {
00148   if(s.size()){
00149     typename Seq<C,R>::const_iterator it=s.begin(); os<<*it; ++it;
00150     for( ;it!= s.end();++it) os <<", "<<*it;
00151   }
00152   return os;
00153 }
00154 //----------------------------------------------------------------------
00159 template<class C,class R>  inline
00160 std::istream & operator>>(std::istream & is, Seq<C,R> & V)
00161 {
00162    typedef typename R::size_type size_type;
00163    size_type s;
00164    is >> s;
00165    V.rep().resize(s);
00166    for(size_type i=0; i< s; ++i) is >> V[i];
00167    return(is);
00168 }
00169 //======================================================================
00170 template<class C,class R>
00171 Seq<C,R> operator+(Seq<C,R> a, const Seq<C,R> & b)
00172 {
00173   Seq<C,R> r=a;
00174   for(typename Seq<C,R>::const_iterator it=b.begin();it!= b.end();++it)
00175       r.push_back(*it);
00176   return r;
00177 }
00178 
00179 //======================================================================
00180 namespace let {
00181   template<class C,class R, class S>
00182   void assign(Seq<C,R> & r, const Seq<C,S> & x)
00183   {
00184     r = Seq<C,R>();
00185     for(typename Seq<C,S>::const_iterator it=x.begin();it!= x.end();++it)
00186       r.push_back(*it);
00187   }
00188 }
00189 //----------------------------------------------------------------------
00190 template <class C,class R>
00191 Seq<C,R>::Seq (char * s): data()
00192 {
00193   rep().resize(0);
00194   synaps_input = s;
00195   synaps_inputptr = synaps_input;
00196   synaps_inputlim = s + strlen(s);
00197   C term;
00198   bool shift = true;
00199   int Ask;
00200 
00201   for (;;) {
00202     Ask = yylex();
00203     if (shift && Ask == BLIST) {
00204       shift = false ;
00205     }
00206     else if (Ask == ELIST) {
00207       break;
00208     }
00209     else {
00210       let::assign(term,yylval);
00211       this->push_back(term);
00212     }
00213   }
00214 }
00215 
00216 //--------------------------------------------------------------------
00217 __END_NAMESPACE_SYNAPS
00218 /********************************************************************/
00219 #endif // SYNAPS_BASE_SEQ_H
00220 

SYNAPS DOCUMENTATION
logo