realroot_doc 0.1.1
|
00001 /********************************************************************* 00002 * This file is part of the source code of realroot kernel. * 00003 * Author(s): B. Mourrain, GALAAD, INRIA * 00004 ********************************************************************** 00005 $Id: dynamic_exp.hpp,v 1.1 2005/07/11 11:17:56 mourrain Exp $ 00006 **********************************************************************/ 00007 #ifndef realroot_dynamic_exp_hpp 00008 #define realroot_dynamic_exp_hpp 00009 00010 #include <stdlib.h> 00011 #include <iostream> 00012 #include <string> 00013 #include <iterator> 00014 #include <cassert> 00015 //#include <synaps/arithm/REF.hpp> 00016 //#include <synaps/base/COUNT.hpp> 00017 00018 #include <realroot/array.hpp> 00019 00020 //====================================================================== 00021 namespace mmx { 00022 //====================================================================== 00024 template<class E=int> 00025 struct dynamic_exp { 00026 00027 typedef E value_type; 00028 typedef unsigned int size_type; 00029 typedef E* iterator; 00030 typedef iterator const_iterator; 00031 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00032 typedef std::reverse_iterator<iterator> reverse_iterator; 00033 00034 typedef E exponent_t; 00035 typedef int degree_t; 00036 typedef dynamic_exp<E> self_t; 00037 00038 // constructors 00039 dynamic_exp():_size(0),_tab(NULL){} 00040 dynamic_exp(const dynamic_exp<E> & d):_size(d._size){ 00041 // cout <<"Copy "<<d<<" "; 00042 if (_size) { 00043 _tab = dynamic_exp<E>::alloc(_size); 00044 memcpy(_tab,d._tab,sizeof(E)*_size); 00045 } 00046 else 00047 _tab = NULL; 00048 // cout <<*this<<endl; 00049 } 00050 00051 dynamic_exp(int s): _size(s){ 00052 // cout <<"C "<<s<<endl; 00053 _tab = dynamic_exp<E>::alloc(s); 00054 for(int i=0;i<s;i++) _tab[i]=(exponent_t)0; 00055 // cout <<*this<<endl; 00056 } 00057 00058 dynamic_exp(int s, E *t): _size(s){ 00059 assert(s <= sizeof(t)/sizeof(E)); 00060 _tab = dynamic_exp<E>::alloc(_size); 00061 for(int i=0;i<s;i++) _tab[i]=t[i]; 00062 } 00063 00064 // template<class S> 00065 // self_t & operator =(const VAL<S> & M) 00066 // { 00067 // if (this == &(M.rep())) { return *this; } 00068 // assign(*this,M.rep()); 00069 // return *this; 00070 // } 00071 00072 // destructor 00073 ~dynamic_exp(){if (_tab) delete [] _tab;} 00074 00075 size_type size() const {return _size;} 00076 00077 iterator begin() {return iterator(_tab); } 00078 const_iterator begin() const {return const_iterator(_tab); } 00079 iterator end() {return iterator(_tab+_size); } 00080 const_iterator end() const {return const_iterator(_tab+_size); } 00081 00082 reverse_iterator rbegin() {return reverse_iterator(_tab+_size); } 00083 const_reverse_iterator rbegin() const {return reverse_iterator(_tab+_size); } 00084 reverse_iterator rend() {return reverse_iterator(_tab); } 00085 const_reverse_iterator rend() const {return reverse_iterator(_tab); } 00086 00087 E operator[] ( size_type i) const { 00088 // if(i<0) cout <<"("<<*this<<"["<<i<<"])"; 00089 return (i<_size?_tab[i]:0); 00090 } 00091 E & operator[] (size_type i) { assert(i<_size);return _tab[i];} 00092 00093 // affectation operator 00094 self_t& operator = (const self_t & A) { 00095 if (&A == this) { return *this; } 00096 if (_tab != NULL) { 00097 // cout << "sz = " << sizeof( _tab) << " " << _size << endl; 00098 // for (int kk = 0; kk < _size; ++kk) { 00099 // cout << _tab[kk] << " "; 00100 // } cout << endl; 00101 delete [] _tab; 00102 } 00103 _size = A._size; 00104 if (_size) { 00105 _tab = dynamic_exp<E>::alloc(_size); 00106 memcpy(_tab,A._tab,sizeof(E)*_size); 00107 } 00108 else 00109 _tab = NULL; 00110 return *this; 00111 } 00112 00113 // self_t & operator+= (const self_t & B); 00114 00115 void reserve(size_type s) 00116 { 00117 if (!s) { 00118 if (_size) { delete [] _tab; } 00119 _size = 0; 00120 _tab = NULL; 00121 } 00122 else if(_tab==NULL) { 00123 _size = s;_tab = dynamic_exp<E>::alloc(s); 00124 //for(size_type i=0;i<s;i++) _tab[i]=(exponent_t)0; 00125 } 00126 else { 00127 delete [] _tab; 00128 _tab = dynamic_exp<E>::alloc(s); 00129 _size=s; 00130 } 00131 } 00132 void resize(size_type s) 00133 { 00134 if(s<=_size) 00135 _size = s; 00136 else{ 00137 E* _tmp =_tab; 00138 _tab = dynamic_exp<E>::alloc(s); 00139 for(size_type i =0;i<_size;i++) _tab[i]=_tmp[i]; 00140 for(size_type i =_size;i<s;i++) _tab[i]=0; 00141 delete [] _tmp; 00142 _size=s; 00143 } 00144 } 00145 00146 00147 void init(int s) 00148 { 00149 if (_size) { delete [] _tab; } 00150 _size= s; 00151 _tab = dynamic_exp<E>::alloc(s); 00152 } 00153 00154 self_t& set_expt(size_type i, value_type d); 00155 00156 friend bool operator == (const dynamic_exp & A, const dynamic_exp & B) { 00157 int i,minsize=std::min(A._size,B._size); 00158 for(i=0;i<minsize;i++) 00159 if(A._tab[i]!=B._tab[i]) return 0; 00160 if(A._size>B._size) 00161 { 00162 for(;i<(int)A._size;i++) 00163 if(A._tab[i]) return 0; 00164 } 00165 else 00166 { 00167 for(;i<(int)B._size;i++) 00168 if(B._tab[i]) return 0; 00169 00170 } 00171 // if (A._size == B._size) 00172 // return memcmp(A._tab,B._tab,sizeof(E)*A._size) == 0; 00173 return 1; 00174 } 00175 friend bool operator != (const dynamic_exp & A, const dynamic_exp & B) { 00176 return !(A==B); 00177 } 00178 // Data 00179 size_type _size; 00180 E* _tab; 00181 00182 static E* alloc(unsigned int N) 00183 {return (new E[N]);} 00184 //{return (E *)malloc(sizeof(E)*N);} 00185 void clear() {delete[] _tab;} 00186 //{free(_tab); } 00187 00188 }; 00189 //---------------------------------------------------------------------- 00190 template<class E> inline 00191 typename dynamic_exp<E>::degree_t degree(const dynamic_exp<E> & t) 00192 { 00193 typename dynamic_exp<E>::degree_t d(0); 00194 for (typename dynamic_exp<E>::size_type i = 0; i < t._size; ++i) d+=t[i]; 00195 return(d); 00196 } 00197 //---------------------------------------------------------------------- 00198 template<class E> 00199 std::ostream & operator << (std::ostream & os, const dynamic_exp<E> & t) { 00200 bool first=true; 00201 if(t._size) 00202 for (typename dynamic_exp<E>::size_type i = 0; i < t._size; ++i) 00203 { 00204 if (t[i] == 1){ 00205 if(first) first=false; else os <<"*"; 00206 os <<'X'<< i; 00207 }else if (t[i] == 0); 00208 else { 00209 if(first) first=false; else os <<"*"; 00210 os <<"X"<< i << '^' <<(int)(t[i]); 00211 } 00212 } 00213 return os; 00214 } 00215 //---------------------------------------------------------------------- 00216 template<class E> 00217 dynamic_exp<E>& dynamic_exp<E>::set_expt(size_type i, value_type d) 00218 { 00219 assert(i<_size); 00220 (*this)[i]=d; 00221 00222 // while(_tab[_size-1]==0 && _size>0) _size--; 00223 00224 // if (i < 0); 00225 // else if (i+1 < _size) 00226 // _tab[i] = d; 00227 // else if (i >= _size && d) { 00228 // E* aux = dynamic_exp<E>::alloc(i+1); 00229 // memset(aux,0,sizeof(E)*(i+1)); 00230 // memcpy(aux,_tab,sizeof(E)*_size); 00231 // //memset(aux+size,0,i-size); 00232 // aux[i] = d; 00233 // delete [] _tab; 00234 // _tab = aux; 00235 // //realloc(_tab,sizeof(E)*(i+1)); 00236 // //memset(_tab+size,0,i-size); 00237 // //_tab[i] = d; 00238 // _size = i+1; 00239 // } 00240 // else if (i >= _size && !d); 00241 // else if (d) 00242 // _tab[i] = d; 00243 // else { 00244 // int j = i - 1; 00245 // while (j >= 0 && !_tab[j]) --j; 00246 // _size = j+1; 00247 // if (j < 0) { 00248 // clear(); 00249 // _tab = NULL; 00250 // } 00251 // else { 00252 // E * aux = dynamic_exp<E>::alloc(j+1); 00253 // memcpy(aux,_tab,sizeof(E)*(j+1)); 00254 // clear(); 00255 // _tab = aux; 00256 // } 00257 // } 00258 return *this; 00259 } 00260 //---------------------------------------------------------------------- 00261 template<class E> 00262 int lvar (const dynamic_exp<E> & A) { 00263 int r=A._size-1; 00264 while(r>=0 && (A[r]==0)) r--; 00265 return r; 00266 } 00267 //---------------------------------------------------------------------- 00268 template<class E> 00269 void erase (dynamic_exp<E> & A) { 00270 A.clear(); 00271 A._tab = NULL; 00272 A._size = 0; 00273 } 00274 //----------------------------------------------------------------------// 00275 // template <class E> 00276 // inline VAL<OP<'+',dynamic_exp<E>,dynamic_exp<E> > > 00277 // operator+(const dynamic_exp<E> & a, const dynamic_exp<E> & b) 00278 // { 00279 // return VAL<OP<'+',dynamic_exp<E>,dynamic_exp<E> > > 00280 // (OP<'+',dynamic_exp<E>,dynamic_exp<E> >(a,b)); 00281 // } 00282 //----------------------------------------------------------------------// 00283 // template <class E> inline 00284 // void assign(dynamic_exp<E> & a, 00285 // const OP<'+',dynamic_exp<E>,dynamic_exp<E> > & M) 00286 // { 00287 // add(a,M.op1,M.op2); 00288 // } 00289 //----------------------------------------------------------------------// 00290 template<class E> 00291 void add (dynamic_exp<E> & r, 00292 const dynamic_exp<E> & A, const dynamic_exp<E> & B); 00293 //----------------------------------------------------------------------// 00294 template<class E> 00295 void add (dynamic_exp<E> & r, 00296 const dynamic_exp<E> & A, 00297 const dynamic_exp<E> & B) 00298 { 00299 r.init(std::max(A.size(),B.size()) ); 00300 array::add(r,A,B); 00301 } 00302 //====================================================================== 00303 } // namespace mmx 00304 //====================================================================== 00305 #endif // realroot_dynamic_exp_hpp 00306