Developer documentation

dynamicexp.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of realroot kernel. *
3 * Author(s): B. Mourrain, GALAAD, INRIA *
4 **********************************************************************
5 $Id: dynamic_exp.hpp,v 1.1 2005/07/11 11:17:56 mourrain Exp $
6 **********************************************************************/
7 #ifndef realroot_dynamic_exp_hpp
8 #define realroot_dynamic_exp_hpp
9 
10 #include <stdlib.h>
11 #include <iostream>
12 #include <string>
13 #include <iterator>
14 #include <cassert>
15 //#include <synaps/arithm/REF.hpp>
16 //#include <synaps/base/COUNT.hpp>
17 
18 #include <realroot/array.hpp>
19 
20 //======================================================================
21 namespace mmx {
22 //======================================================================
24 template<class E=int>
25 struct dynamic_exp {
26 
27  typedef E value_type;
28  typedef unsigned int size_type;
29  typedef E* iterator;
30  typedef iterator const_iterator;
31  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
32  typedef std::reverse_iterator<iterator> reverse_iterator;
33 
34  typedef E exponent_t;
35  typedef int degree_t;
37 
38  // constructors
39  dynamic_exp():_size(0),_tab(NULL){}
41  // cout <<"Copy "<<d<<" ";
42  if (_size) {
44  memcpy(_tab,d._tab,sizeof(E)*_size);
45  }
46  else
47  _tab = NULL;
48  // cout <<*this<<endl;
49  }
50 
51  dynamic_exp(int s): _size(s){
52  // cout <<"C "<<s<<endl;
54  for(int i=0;i<s;i++) _tab[i]=(exponent_t)0;
55  // cout <<*this<<endl;
56  }
57 
58  dynamic_exp(int s, E *t): _size(s){
59  assert(s <= sizeof(t)/sizeof(E));
61  for(int i=0;i<s;i++) _tab[i]=t[i];
62  }
63 
64 // template<class S>
65 // self_t & operator =(const VAL<S> & M)
66 // {
67 // if (this == &(M.rep())) { return *this; }
68 // assign(*this,M.rep());
69 // return *this;
70 // }
71 
72  // destructor
73  ~dynamic_exp(){if (_tab) delete [] _tab;}
74 
75  size_type size() const {return _size;}
76 
77  iterator begin() {return iterator(_tab); }
78  const_iterator begin() const {return const_iterator(_tab); }
79  iterator end() {return iterator(_tab+_size); }
80  const_iterator end() const {return const_iterator(_tab+_size); }
81 
82  reverse_iterator rbegin() {return reverse_iterator(_tab+_size); }
83  const_reverse_iterator rbegin() const {return reverse_iterator(_tab+_size); }
84  reverse_iterator rend() {return reverse_iterator(_tab); }
85  const_reverse_iterator rend() const {return reverse_iterator(_tab); }
86 
87  E operator[] ( size_type i) const {
88  // if(i<0) cout <<"("<<*this<<"["<<i<<"])";
89  return (i<_size?_tab[i]:0);
90  }
91  E & operator[] (size_type i) { assert(i<_size);return _tab[i];}
92 
93  // affectation operator
94  self_t& operator = (const self_t & A) {
95  if (&A == this) { return *this; }
96  if (_tab != NULL) {
97  // cout << "sz = " << sizeof( _tab) << " " << _size << endl;
98  // for (int kk = 0; kk < _size; ++kk) {
99  // cout << _tab[kk] << " ";
100  // } cout << endl;
101  delete [] _tab;
102  }
103  _size = A._size;
104  if (_size) {
106  memcpy(_tab,A._tab,sizeof(E)*_size);
107  }
108  else
109  _tab = NULL;
110  return *this;
111  }
112 
113  // self_t & operator+= (const self_t & B);
114 
115  void reserve(size_type s)
116  {
117  if (!s) {
118  if (_size) { delete [] _tab; }
119  _size = 0;
120  _tab = NULL;
121  }
122  else if(_tab==NULL) {
124  //for(size_type i=0;i<s;i++) _tab[i]=(exponent_t)0;
125  }
126  else {
127  delete [] _tab;
129  _size=s;
130  }
131  }
132  void resize(size_type s)
133  {
134  if(s<=_size)
135  _size = s;
136  else{
137  E* _tmp =_tab;
139  for(size_type i =0;i<_size;i++) _tab[i]=_tmp[i];
140  for(size_type i =_size;i<s;i++) _tab[i]=0;
141  delete [] _tmp;
142  _size=s;
143  }
144  }
145 
146 
147  void init(int s)
148  {
149  if (_size) { delete [] _tab; }
150  _size= s;
152  }
153 
154  self_t& set_expt(size_type i, value_type d);
155 
156  friend bool operator == (const dynamic_exp & A, const dynamic_exp & B) {
157  int i,minsize=std::min(A._size,B._size);
158  for(i=0;i<minsize;i++)
159  if(A._tab[i]!=B._tab[i]) return 0;
160  if(A._size>B._size)
161  {
162  for(;i<(int)A._size;i++)
163  if(A._tab[i]) return 0;
164  }
165  else
166  {
167  for(;i<(int)B._size;i++)
168  if(B._tab[i]) return 0;
169 
170  }
171  // if (A._size == B._size)
172  // return memcmp(A._tab,B._tab,sizeof(E)*A._size) == 0;
173  return 1;
174  }
175  friend bool operator != (const dynamic_exp & A, const dynamic_exp & B) {
176  return !(A==B);
177  }
178  // Data
179  size_type _size;
180  E* _tab;
181 
182  static E* alloc(unsigned int N)
183  {return (new E[N]);}
184  //{return (E *)malloc(sizeof(E)*N);}
185  void clear() {delete[] _tab;}
186  //{free(_tab); }
187 
188 };
189 //----------------------------------------------------------------------
190  template<class E> inline
192 {
193  typename dynamic_exp<E>::degree_t d(0);
194  for (typename dynamic_exp<E>::size_type i = 0; i < t._size; ++i) d+=t[i];
195  return(d);
196 }
197 //----------------------------------------------------------------------
198 template<class E>
199 std::ostream & operator << (std::ostream & os, const dynamic_exp<E> & t) {
200  bool first=true;
201  if(t._size)
202  for (typename dynamic_exp<E>::size_type i = 0; i < t._size; ++i)
203  {
204  if (t[i] == 1){
205  if(first) first=false; else os <<"*";
206  os <<'X'<< i;
207  }else if (t[i] == 0);
208  else {
209  if(first) first=false; else os <<"*";
210  os <<"X"<< i << '^' <<(int)(t[i]);
211  }
212  }
213  return os;
214 }
215 //----------------------------------------------------------------------
216  template<class E>
218 {
219  assert(i<_size);
220  (*this)[i]=d;
221 
222  // while(_tab[_size-1]==0 && _size>0) _size--;
223 
224  // if (i < 0);
225  // else if (i+1 < _size)
226  // _tab[i] = d;
227  // else if (i >= _size && d) {
228  // E* aux = dynamic_exp<E>::alloc(i+1);
229  // memset(aux,0,sizeof(E)*(i+1));
230  // memcpy(aux,_tab,sizeof(E)*_size);
231  // //memset(aux+size,0,i-size);
232  // aux[i] = d;
233  // delete [] _tab;
234  // _tab = aux;
235  // //realloc(_tab,sizeof(E)*(i+1));
236  // //memset(_tab+size,0,i-size);
237  // //_tab[i] = d;
238  // _size = i+1;
239  // }
240  // else if (i >= _size && !d);
241  // else if (d)
242  // _tab[i] = d;
243  // else {
244  // int j = i - 1;
245  // while (j >= 0 && !_tab[j]) --j;
246  // _size = j+1;
247  // if (j < 0) {
248  // clear();
249  // _tab = NULL;
250  // }
251  // else {
252  // E * aux = dynamic_exp<E>::alloc(j+1);
253  // memcpy(aux,_tab,sizeof(E)*(j+1));
254  // clear();
255  // _tab = aux;
256  // }
257  // }
258  return *this;
259 }
260 //----------------------------------------------------------------------
261 template<class E>
262 int lvar (const dynamic_exp<E> & A) {
263  int r=A._size-1;
264  while(r>=0 && (A[r]==0)) r--;
265  return r;
266 }
267 //----------------------------------------------------------------------
268 template<class E>
269 void erase (dynamic_exp<E> & A) {
270  A.clear();
271  A._tab = NULL;
272  A._size = 0;
273 }
274 //----------------------------------------------------------------------//
275 // template <class E>
276 // inline VAL<OP<'+',dynamic_exp<E>,dynamic_exp<E> > >
277 // operator+(const dynamic_exp<E> & a, const dynamic_exp<E> & b)
278 // {
279 // return VAL<OP<'+',dynamic_exp<E>,dynamic_exp<E> > >
280 // (OP<'+',dynamic_exp<E>,dynamic_exp<E> >(a,b));
281 // }
282 //----------------------------------------------------------------------//
283 // template <class E> inline
284 // void assign(dynamic_exp<E> & a,
285 // const OP<'+',dynamic_exp<E>,dynamic_exp<E> > & M)
286 // {
287 // add(a,M.op1,M.op2);
288 // }
289 //----------------------------------------------------------------------//
290 template<class E>
291 void add (dynamic_exp<E> & r,
292  const dynamic_exp<E> & A, const dynamic_exp<E> & B);
293 //----------------------------------------------------------------------//
294 template<class E>
295 void add (dynamic_exp<E> & r,
296  const dynamic_exp<E> & A,
297  const dynamic_exp<E> & B)
298 {
299  r.init(std::max(A.size(),B.size()) );
300  array::add(r,A,B);
301 }
302 //======================================================================
303 } // namespace mmx
304 //======================================================================
305 #endif // realroot_dynamic_exp_hpp
306 
E value_type
Definition: dynamicexp.hpp:27
int degree_t
Definition: dynamicexp.hpp:35
self_t & set_expt(size_type i, value_type d)
Definition: dynamicexp.hpp:217
reverse_iterator rbegin()
Definition: dynamicexp.hpp:82
int lvar(const dynamic_exp< E > &A)
Definition: dynamicexp.hpp:262
dynamic_exp< E >::degree_t degree(const dynamic_exp< E > &t)
Definition: dynamicexp.hpp:191
size_type _size
Definition: dynamicexp.hpp:179
friend bool operator!=(const dynamic_exp &A, const dynamic_exp &B)
Definition: dynamicexp.hpp:175
void clear()
Definition: dynamicexp.hpp:185
iterator begin()
Definition: dynamicexp.hpp:77
const_iterator begin() const
Definition: dynamicexp.hpp:78
TMPL int N(const MONOMIAL &v)
Definition: monomial_glue.hpp:60
const_iterator end() const
Definition: dynamicexp.hpp:80
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: dynamicexp.hpp:31
iterator const_iterator
Definition: dynamicexp.hpp:30
const_reverse_iterator rbegin() const
Definition: dynamicexp.hpp:83
friend bool operator==(const dynamic_exp &A, const dynamic_exp &B)
Definition: dynamicexp.hpp:156
void reserve(size_type s)
Definition: dynamicexp.hpp:115
void resize(size_type s)
Definition: dynamicexp.hpp:132
Dynamic exponent.
Definition: dynamicexp.hpp:25
#define min(a, b)
Definition: parser_def.c:475
reverse_iterator rend()
Definition: dynamicexp.hpp:84
static E * alloc(unsigned int N)
Definition: dynamicexp.hpp:182
dynamic_exp< E > self_t
Definition: dynamicexp.hpp:36
void init(int s)
Definition: dynamicexp.hpp:147
size_type size() const
Definition: dynamicexp.hpp:75
~dynamic_exp()
Definition: dynamicexp.hpp:73
void add(V &a, const W &b)
Addition of two vectors.
Definition: array.hpp:154
self_t & operator=(const self_t &A)
Definition: dynamicexp.hpp:94
unsigned int size_type
Definition: dynamicexp.hpp:28
const_reverse_iterator rend() const
Definition: dynamicexp.hpp:85
dynamic_exp()
Definition: dynamicexp.hpp:39
dynamic_exp(int s, E *t)
Definition: dynamicexp.hpp:58
dynamic_exp(const dynamic_exp< E > &d)
Definition: dynamicexp.hpp:40
E * iterator
Definition: dynamicexp.hpp:29
E operator[](size_type i) const
Definition: dynamicexp.hpp:87
iterator end()
Definition: dynamicexp.hpp:79
E exponent_t
Definition: dynamicexp.hpp:34
Interval< T, r > max(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:135
Definition: array.hpp:12
dynamic_exp(int s)
Definition: dynamicexp.hpp:51
void add(dynamic_exp< E > &r, const dynamic_exp< E > &A, const dynamic_exp< E > &B)
Definition: dynamicexp.hpp:295
std::reverse_iterator< iterator > reverse_iterator
Definition: dynamicexp.hpp:32
#define assert(expr, msg)
Definition: shared_object.hpp:57
void erase(dynamic_exp< E > &A)
Definition: dynamicexp.hpp:269
E * _tab
Definition: dynamicexp.hpp:180
Home