Developer documentation

Seq.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of the realroot kernel. *
3 * Author(s): B. Mourrain, GALAAD, INRIA *
4 **********************************************************************
5 $Id: Seq.hpp,v 1.3 2005/09/28 06:40:51 mourrain Exp $
6 **********************************************************************/
7 #ifndef MMX_SEQ_HPP
8 #define MMX_SEQ_HPP
9 //----------------------------------------------------------------------
10 # include <string.h>
11 # include <vector>
12 # include <algorithm>
13 # include <realroot/parser.hpp>
15 # include <realroot/assign.hpp>
16 # include <realroot/array.hpp>
17 //----------------------------------------------------------------------
18 
19 namespace mmx {
20 
21 // Print operator for std::vector
22 template<class C>
23 std::ostream & operator<<(std::ostream & os, const std::vector<C> & V)
24 {
25  return mmx::array::print(os,V);
26 }
27 
28 template<class C, class S> struct Seq;
29 
30 namespace let {
31  template<class C,class R, class X, class S>
32  void assign(Seq<C,R> & r, const Seq<X,S> & x);
33  template<class C,class R, class S>
34  void assign(Seq<C,R> & r, const Seq<C,S> & x);
35 };
36 
38 
58 template <class C, class R=std::vector<C> >
59 struct Seq {
60 
62  R & rep() {return (*data);}
63  const R & rep() const {return (*data);}
64 
65  typedef typename R::size_type size_type;
66  typedef C value_type;
67  typedef C value_t;
68  typedef typename R::iterator iterator;
69  typedef typename R::const_iterator const_iterator;
70  typedef typename R::reverse_iterator reverse_iterator;
71  typedef typename R::const_reverse_iterator const_reverse_iterator;
72  typedef Seq<C,R> self_t;
73 
74  Seq(): data() {}
75  Seq(const R & r):data(r){}
76  Seq(size_type s) {rep().resize(s);}
77  Seq(size_type s, value_type* t) {rep()=R(t,t+s);}
78  Seq(iterator b,iterator e) {rep()=R(b,e);}
79  Seq(size_type m, const char * str);
80  Seq(char* str);
81 
82  // Copy constructor
83  Seq(const self_t & r): data(r.data) {}
84  template<class X, class S>
85  Seq(const Seq<X,S> & P) {using namespace let; assign(*this,P);}
86 
87  iterator begin() {return rep().begin();}
88  iterator end() {return rep().end();}
89  const_iterator begin() const {return rep().begin();}
90  const_iterator end() const {return rep().end();}
91  reverse_iterator rbegin() {return rep().rbegin();}
92  reverse_iterator rend() {return rep().rend();}
93  const_reverse_iterator rbegin() const {return rep().rbegin();}
94  const_reverse_iterator rend () const {return rep().rbegin();}
95 
96 
97  // Assignement
98  self_t & operator=(const self_t & V){data=V.data; return *this;}
99  template<class X, class S>
100  self_t & operator=(const Seq<X,S> & V) {
101  using namespace let;
102  assign(*this,V);
103  return *this;}
104  //self_t & operator=(const Seq<X,S> & V) {assign(*this,V); return *this;}
105 
106  value_type & operator[] (size_type i) {return rep()[i];}
107  const value_type & operator[] (size_type i) const {return rep()[i];}
108 
109  const value_type & front() const { assert(this->size()>0); return rep()[0];}
110  self_t & push_back(const C& x) { rep().push_back(x); return *this; }
111 
112  self_t & erase(size_type i) {
113  //if (i>size_type(-1) && i< this->size() )
114  rep().erase(rep().begin()+i);
115  return *this; }
116 
117  self_t & clear() { rep().clear();
118  return *this; }
119 
120  size_type search(const C& x) {
121  for (size_type i=0;i< this->size();++i) {
122  if ( this->rep()[i]== x)
123  return i;
124  }
125  //std::cout<<"Seq: "<<x<<" not found"<<std::endl;
126  return (size_type(-1) );
127  }
128 
129  bool member(const C& x) {
130  for (size_type i=0;i< this->size();++i)
131  if ( this->rep()[i]== x)
132  return true;
133  return false;
134  }
135 
136  void sort() {
137  std::sort( this->rep().begin(), this->rep().end() );
138  }
139 
140  void insert(const size_type& i, const C& c) {
141  this->rep().insert(this->rep().begin()+i, c);
142  }
143  C min() {
144  return *std::min_element( this->rep().begin(), this->rep().end());
145  }
146 
147  C max() {
148  return *std::max_element( this->rep().begin(), this->rep().end());
149  }
150 
151  template<class Compare>
152  void sort(Compare comp) {
153  std::sort( this->rep().begin(), this->rep().end(),comp);
154  }
155 
156  template<class Compare>
157  C min(Compare comp) {
158  return *std::min_element( this->rep().begin(), this->rep().end(),comp);
159  }
160 
161  template<class Compare>
162  C max(Compare comp) {
163  return *std::max_element( this->rep().begin(), this->rep().end(),comp);
164  }
165 
166  size_type size() const {return rep().size();}
167 
168  bool empty() const {return (rep().size()==0);}
169  void resize(size_type i) { rep().resize(i); }
170 
171  C back() const { return rep().back(); }
172  void pop_back() { rep().pop_back(); }
173 
175  void reverse() { std::reverse (rep().begin( ), rep().end( ) ); }
176  self_t reversed() const { self_t s(*this); s.reverse(); return s; }
177 
179  self_t operator,(const self_t & x) const;
180 
182  self_t operator,(const C & x) const;
183 
184  self_t operator << (const self_t & s)
185  {
186  for(const_iterator it=s.begin(); it!=s.end();++it)
187  this->push_back(*it);
188  return *this;
189  }
190 
191  ~Seq () {}
192 
193 };
194 
195 //====================================================================
196 template<class C, class R> inline
197 const C& read(const Seq<C,R>& m, unsigned i) { return m[i]; }
198 
199 template<class C, class R>
200 bool eq (const Seq<C,R>& v1, const Seq<C,R>& v2) {return v1==v2;}
201 template<class C, class R>
202 bool neq(const Seq<C,R>& v1, const Seq<C,R>& v2) {return v1!=v2;}
203 
204 template<class C, class R>
205 unsigned hash (const Seq<C,R>& v)
206 {
207  unsigned i, h= 214365, n= v.size();
208  for (i=0; i<n; i++)
209  h= (h<<1) ^ (h<<5) ^ (h>>27) ^ hash(v[i]);
210  return h;
211 }
212 template<class C, class R>
213 unsigned soft_hash (const Seq<C,R>& m) {return hash(m);}
214 
215 template<class C, class R>
216 typename Seq<C,R>::const_iterator iterate(const Seq<C,R>& m) {return m.begin();}
217 
218 //--------------------------------------------------------------------
219 template<class C, class R>
220 Seq<C,R> Seq<C,R>::operator,(const Seq<C,R> & x) const
221 {
222  self_t r(rep());
223  for(const_iterator it=x.begin(); it!=x.end();++it)
224  r.push_back(*it);
225  return r;
226 }
227 //--------------------------------------------------------------------
228 template<class C, class R>
230 {
231  self_t r(rep());
232  r.push_back(x);
233  return r;
234 }
235 //--------------------------------------------------------------------
236 template<class C,class R> inline
237 Seq<C,R>& operator<<(Seq<C,R> & r, const C& x)
238 {
239  r.push_back(x); return r;
240 }
241 
242 //--------------------------------------------------------------------
243 template <class C, class R>
244 bool operator==(const Seq<C,R>& a, const Seq<C,R>& b)
245  {return a.rep()==b.rep();}
246 template <class C, class R>
247 bool operator!=(const Seq<C,R>& a, const Seq<C,R>& b) {return !(a==b);}
248 
249 
250 //======================================================================
251 // INPUT OUTPUT
252 //======================================================================
254 template<class C, class R>
255 std::ostream & operator<<(std::ostream & os, const Seq<C,R> & s)
256 {
257  if(s.size()){
258  typename Seq<C,R>::const_iterator it=s.begin(); os<<*it; ++it;
259  for( ;it!= s.end();++it) os <<", "<<*it;
260  }
261  return os;
262 }
263 
264 //----------------------------------------------------------------------
269 template<class C,class R> inline
270 std::istream & operator>>(std::istream & is, Seq<C,R> & V)
271 {
272  typedef typename R::size_type size_type;
273  size_type s;
274  is >> s;
275  V.rep().resize(s);
276  for(size_type i=0; i< s; ++i) is >> V[i];
277  return(is);
278 }
279 //======================================================================
280 template<class C,class R>
282 {
283  Seq<C,R> r=a;
284  for(typename Seq<C,R>::const_iterator it=b.begin();it!= b.end();++it)
285  r.push_back(*it);
286  return r;
287 }
288 
289 //======================================================================
290 template<class C,class R>
292 {
293  //assert( a.size()==b.size() );
294  Seq<C,R> r=a;
295 
296  typename Seq<C,R>::const_iterator it2= b.begin();
297 
298  for(typename Seq<C,R>::iterator it=r.begin();it!= r.end();++it)
299  {
300  *it += *it2;
301  ++it2;
302  }
303 
304  return r;
305 }
306 
307 
308 //======================================================================
309 template<class C,class R>
311 {
312  Seq<C,R> r=a;
313  for(typename Seq<C,R>::iterator it=r.begin();it!= r.end();++it)
314  *it= b * *it ;
315  return r;
316 }
317 
318 //======================================================================
319 template<class C,class R>
321 {
322  Seq<C,R> r=a;
323  for(typename Seq<C,R>::iterator it=r.begin();it!= r.end();++it)
324  *it= *it / b;
325  return r;
326 }
327 
328 //======================================================================
329 namespace let {
330 
331  template<class C,class R, class S>
332  void assign(Seq<C,R> & r, const Seq<C,S> & x)
333  {
334  r = Seq<C,R>();
335  for(typename Seq<C,S>::const_iterator it=x.begin();it!= x.end();++it)
336  r.push_back(*it);
337  }
338 
339  template<class C,class R, class X, class S>
340  void assign(Seq<C,R> & r, const Seq<X,S> & x)
341  {
342  r = Seq<C,R>();
343  for(typename Seq<X,S>::const_iterator it=x.begin();it!= x.end();++it)
344  {
345  C tmp;
346  assign(tmp,*it);
347  r.push_back(tmp);
348  };
349  }
350 
351 }
352 //----------------------------------------------------------------------
353 template <class C,class R>
354 Seq<C,R>::Seq (char * s): data()
355 {
356  rep().resize(0);
357  synaps_input = s;
359  synaps_inputlim = s + strlen(s);
360  C term;
361  bool shift = true;
362  int Ask;
363 
364  for (;;) {
365  Ask = yylex();
366  if (shift && Ask == BLIST) {
367  shift = false ;
368  }
369  else if (Ask == ELIST) {
370  break;
371  }
372  else {
373  let::assign(term,yylval);
374  this->push_back(term);
375  }
376  }
377 }
378 //--------------------------------------------------------------------
379 } //namespace mmx
380 //--------------------------------------------------------------------
381 #ifndef MMX_FOREACH
383 
384 template <typename T> class MmxForeachContainer : public MmxForeachContainerBase {
385 public:
386  inline MmxForeachContainer(const T& t): c(t), brk(0), i(c.begin()), e(c.end()) {} ;
387  const T c ;
388  mutable int brk ;
389  mutable typename T::const_iterator i, e ;
390  inline bool condition() const {
391  return (!brk++ && i != e) ;
392  }
393 } ;
394 
395 template <typename T>
396 inline T * mmxForeachpointer(const T &) {
397  return 0 ;
398 }
399 
400 template <typename T>
403  return MmxForeachContainer<T>(t) ;
404 }
405 
406 template <typename T> inline const MmxForeachContainer<T> *mmxForeachContainer(const MmxForeachContainerBase *base, const T *) {
407  return static_cast<const MmxForeachContainer<T> *>(base) ;
408 }
409 
410 # define MMX_FOREACH(variable, container) \
411  if(0) {} else for (const MmxForeachContainerBase & _container_ = mmxForeachContainerNew(container); \
412  mmxForeachContainer(&_container_, true ? 0 : mmxForeachpointer(container))->condition(); \
413  ++mmxForeachContainer(&_container_, true ? 0 : mmxForeachpointer(container))->i) \
414  for (variable = *mmxForeachContainer(&_container_, true ? 0 : mmxForeachpointer(container))->i; \
415  mmxForeachContainer(&_container_, true ? 0 : mmxForeachpointer(container))->brk; \
416  --mmxForeachContainer(&_container_, true ? 0 : mmxForeachpointer(container))->brk)
417 
418 # endif
419 
420 #ifndef foreach
421 # define foreach MMX_FOREACH
422 #endif
423 
424 //====================================================================
425 #endif // MMX_SEQ_HPP
426 
iterator end()
Definition: Seq.hpp:88
bool operator==(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:88
C max()
Definition: Seq.hpp:147
const_reverse_iterator rend() const
Definition: Seq.hpp:94
size_type search(const C &x)
Definition: Seq.hpp:120
Sequence of terms with reference counter.
Definition: Seq.hpp:28
bool eq(const Seq< C, R > &v1, const Seq< C, R > &v2)
Definition: Seq.hpp:200
self_t & operator=(const self_t &V)
Definition: Seq.hpp:98
void resize(size_type i)
Definition: Seq.hpp:169
~Seq()
Definition: Seq.hpp:191
const C & b
Definition: Interval_glue.hpp:25
const C & read(const Seq< C, R > &m, unsigned i)
Definition: Seq.hpp:197
int yylex(void)
void shift(IntervalData< RT, Poly > &ID, const RT &a)
Definition: contfrac_intervaldata.hpp:257
C min()
Definition: Seq.hpp:143
self_t & operator=(const Seq< X, S > &V)
Definition: Seq.hpp:100
self_t operator<<(const self_t &s)
Definition: Seq.hpp:184
char * yylval
Definition: parser.hpp:31
Seq()
Definition: Seq.hpp:74
R::iterator iterator
Definition: Seq.hpp:68
const_reverse_iterator rbegin() const
Definition: Seq.hpp:93
Seq(const Seq< X, S > &P)
Definition: Seq.hpp:85
self_t & erase(size_type i)
Definition: Seq.hpp:112
bool member(const C &x)
Definition: Seq.hpp:129
void sort()
Definition: Seq.hpp:136
self_t & push_back(const C &x)
Definition: Seq.hpp:110
MmxForeachContainer(const T &t)
Definition: Seq.hpp:386
const_iterator begin() const
Definition: Seq.hpp:89
iterator begin()
Definition: Seq.hpp:87
void insert(const size_type &i, const C &c)
Definition: Seq.hpp:140
extended< NT > operator/(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:111
self_t & clear()
Definition: Seq.hpp:117
value_type & operator[](size_type i)
Definition: Seq.hpp:106
T * mmxForeachpointer(const T &)
Definition: Seq.hpp:396
void reverse(V &v, int n)
Definition: array.hpp:115
Seq(const R &r)
Definition: Seq.hpp:75
Seq(size_type s)
Definition: Seq.hpp:76
char * synaps_inputlim
Definition: parser.hpp:37
TMPL unsigned hash(const Monomial &v)
Definition: monomial_glue.hpp:26
void mul(Interval< C, r > &a, const C &x)
Definition: Interval_fcts.hpp:259
const MmxForeachContainer< T > * mmxForeachContainer(const MmxForeachContainerBase *base, const T *)
Definition: Seq.hpp:406
extended< NT > operator+(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:122
Definition: shared_object.hpp:64
Seq(size_type s, value_type *t)
Definition: Seq.hpp:77
Seq(iterator b, iterator e)
Definition: Seq.hpp:78
const value_type & front() const
Definition: Seq.hpp:109
char * synaps_input
Definition: parser.hpp:34
size_type size() const
Definition: Seq.hpp:166
#define ELIST
Definition: parser.hpp:24
C value_t
Definition: Seq.hpp:67
Seq< C, R >::const_iterator iterate(const Seq< C, R > &m)
Definition: Seq.hpp:216
R::reverse_iterator reverse_iterator
Definition: Seq.hpp:70
void assign(double &r, const scalar< MPF > &z)
Definition: scalar_floating.hpp:537
C min(Compare comp)
Definition: Seq.hpp:157
Definition: Seq.hpp:384
R::const_reverse_iterator const_reverse_iterator
Definition: Seq.hpp:71
Definition: polynomial.hpp:37
char * synaps_inputptr
Definition: monomial.hpp:310
bool neq(const Seq< C, R > &v1, const Seq< C, R > &v2)
Definition: Seq.hpp:202
C back() const
Definition: Seq.hpp:171
C max(Compare comp)
Definition: Seq.hpp:162
void sort(Compare comp)
Definition: Seq.hpp:152
T::const_iterator i
Definition: Seq.hpp:389
const_iterator end() const
Definition: Seq.hpp:90
bool condition() const
Definition: Seq.hpp:390
R & rep()
Definition: Seq.hpp:62
reverse_iterator rbegin()
Definition: Seq.hpp:91
std::istream & operator>>(std::istream &is, scalar< MPF > &b)
Definition: scalar_floating.hpp:453
const C & c
Definition: Interval_glue.hpp:45
double C
Definition: solver_mv_fatarcs.cpp:16
R::const_iterator const_iterator
Definition: Seq.hpp:69
const R & rep() const
Definition: Seq.hpp:63
bool operator!=(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:95
Definition: Seq.hpp:382
#define BLIST
Definition: parser.hpp:23
Seq(const self_t &r)
Definition: Seq.hpp:83
reverse_iterator rend()
Definition: Seq.hpp:92
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
self_t operator,(const self_t &x) const
Concatenate two sequences.
C value_type
Definition: Seq.hpp:66
int brk
Definition: Seq.hpp:388
TMPL unsigned soft_hash(const Monomial &m)
Definition: monomial_glue.hpp:33
shared_object< R > data
Definition: Seq.hpp:61
Definition: array.hpp:12
R::size_type size_type
Definition: Seq.hpp:65
Seq< C, R > adds(Seq< C, R > a, const Seq< C, R > &b)
Definition: Seq.hpp:291
bool empty() const
Definition: Seq.hpp:168
void reverse()
Reverse list.
Definition: Seq.hpp:175
MmxForeachContainer< T > mmxForeachContainerNew(const T &t)
Definition: Seq.hpp:402
OS & print(OS &os, const R &v)
Output function for general vectors: [v1, v2, ...].
Definition: array.hpp:39
self_t reversed() const
Definition: Seq.hpp:176
void pop_back()
Definition: Seq.hpp:172
#define assert(expr, msg)
Definition: shared_object.hpp:57
Seq< C, R > self_t
Definition: Seq.hpp:72
Home