Borderbasix

dynamicexp.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of BORDERBASIX software. *
3 * (C) B. Mourrain, INRIA *
4 **********************************************************************
5 History:
6 $Id: dynamicexp.hpp,v 1.1.1.1 2006/10/06 08:01:40 trebuche Exp $
7 **********************************************************************/
8 #ifndef _dynamicexp_H_
9 #define _dynamicexp_H_
10 
11 #include <iostream>
12 #include <cstdlib>
13 #include <cstring>
14 
16 template<char X,class E=char>
17 struct dynamicexp {
18 
19  typedef E exponent_t;
20  typedef int degree_t;
22 
23  // constructors
24  dynamicexp():size(0),tab(NULL){}
26  if (size) {
27  tab = (E *)malloc(sizeof(E)*size);
28  memcpy(tab,d.tab,sizeof(E)*size);
29  }
30  else
31  tab = NULL;
32  }
33  dynamicexp(int s, E *t): size(s){
34  tab = (E *)malloc(sizeof(E)*size);
35  for(int i=0;i<s;i++) tab[i]=t[i];
36  }
37  dynamicexp(int s):size(s){tab = (E *)malloc(sizeof(E)*size);}
38  // destructor
39  ~dynamicexp(){if (tab) free(tab);}
40 
41  E operator[] (int i) const {
42  // if(i<0) cout <<"("<<*this<<"["<<i<<"])";
43  return (i<size &&i>=0?tab[i]:0); }
44 
45  // affectation operator
47  if (tab) free(tab);
48  size = A.size;
49  if (size) {
50  tab = (E *)malloc(sizeof(E)*size);
51  memcpy(tab,A.tab,sizeof(E)*size);
52  }
53  else
54  tab = NULL;
55  return *this;
56  }
57  self_t & operator+= (const self_t & B) ;
58 
59  void reserve(int s)
60  {
61  if (!s) {
62  size = 0;
63  tab = NULL;
64  }
65  else {
66  size = s;
67  tab = (E *)malloc(sizeof(E)*size);
68  memset(tab,0,sizeof(E)*s);
69  }
70  }
71 
72  self_t setExponent(int i, int d)
73  {
74  SDegree (*this,i,d);
75  return *this;
76  }
77 
78  friend bool operator == (const dynamicexp & A, const dynamicexp & B) {
79  if (A.size == B.size)
80  return memcmp(A.tab,B.tab,sizeof(E)*A.size) == 0;
81  return 0;
82  }
83  friend bool operator != (const dynamicexp & A, const dynamicexp & B) {
84  return !(A==B);}
85  // Data
86  int size;
87  E *tab;
88 };
89 
90 template<char X,class E> inline
92 {
93  typename dynamicexp<X,E>::degree_t d(0);
94  for (int i = 0; i < t.size; ++i) d+=t[i];
95  return(d);
96 }
97 
98 template<char X,class E>
99 std::ostream & operator << (std::ostream & os, const dynamicexp<X,E> & t) {
100  for (int i = 0; i < t.size; ++i)
101  {
102  if (t.tab[i] == 1)
103  os <<"*"<<X<< i;
104  else if (t.tab[i] == 0);
105  else
106  os <<"*"<<X << i << '^' <<(int)(t[i]);
107  }
108  return os;
109 }
110 
111 // template<char X,class E>
112 // void build(dynamicexp<X,E> & A, int i, int d) {
113 // if (!d) {
114 // A.size = 0;
115 // A.tab = NULL;
116 // }
117 // else {
118 // A.size = i+1;
119 // A.tab = (E *)malloc(sizeof(E)*A.size);
120 // memset(A.tab,0,sizeof(E)*i);
121 // A.tab[i] = d;
122 // }
123 // }
124 
125 template<char X,class E>
126 void build(dynamicexp<X,E> & A, int s, E *tab) {
127  A.size = s;
128  A.tab = (E *)malloc(sizeof(E)*s);
129  memcpy(A.tab,tab,sizeof(E)*s);
130 }
131 
132 // template<char X,class E>
133 // int GDegree (const dynamicexp<X,E> & A, int i) {
134 // if (0 <= i && i < A.size)
135 // return A.tab[i];
136 // return 0;
137 // }
138 
139 template<char X,class E>
140 void SDegree (dynamicexp<X,E> & A, int i, int d) {
141  if (i < 0);
142  else if (i < A.size-1)
143  A.tab[i] = d;
144  else if (i >= A.size && d) {
145  E * aux = (E *)malloc(sizeof(E)*(i+1));
146  memset(aux,0,sizeof(E)*(i+1));
147  memcpy(aux,A.tab,sizeof(E)*A.size);
148  // memset(aux+A.size,0,i-A.size);
149  aux[i] = d;
150  free(A.tab);
151  A.tab = aux;
152  //realloc(A.tab,sizeof(E)*(i+1));
153  //memset(A.tab+A.size,0,i-A.size);
154  //A.tab[i] = d;
155  A.size = i+1;
156  }
157  else if (i >= A.size && !d);
158  else if (d)
159  A.tab[i] = d;
160  else {
161  int j = i - 1;
162  while (j >= 0 && !A.tab[j]) --j;
163  A.size = j+1;
164  if (j < 0) {
165  free(A.tab);
166  A.tab = NULL;
167  }
168  else {
169  E * aux = (E *)malloc(sizeof(E)*(j+1));
170  memcpy(aux,A.tab,sizeof(E)*(j+1));
171  free(A.tab);
172  A.tab = aux;
173  }
174  }
175 }
176 
177 template<char X,class E>
178 int lvar (const dynamicexp<X,E> & A) {
179  return A.size-1;
180 }
181 
182 template<char X,class E>
183 void erase (dynamicexp<X,E> & A) {
184  free(A.tab);
185  A.tab = NULL;
186  A.size = 0;
187 }
188 
189 
190 template<char X,class E>
192  if (size >= B.size)
193  for (int i = 0; i < B.size; ++i)
194  tab[i] += B.tab[i];
195  else if (!size) {
196  tab = (E *) malloc(sizeof(E)*B.size);
197  memcpy(tab,B.tab,sizeof(E)*B.size);
198  size = B.size;
199  }
200  else {
201  E *aux;
202  aux = (E *)malloc(sizeof(E)*B.size);
203  memcpy(aux,B.tab,sizeof(E)*B.size);
204  for (int i = 0; i < size; ++i)
205  aux[i] += tab[i];
206  free(tab);
207  tab = aux;
208  size = B.size;
209  }
210  return *this;
211 }
212 template<char X,class E>
214  const dynamicexp<X,E> & A,
215  const dynamicexp<X,E> & B)
216 {
217  int N = std::max(A.size,B.size);
218  r.size = N;
219  r.tab = (E *) malloc(sizeof(E)*N);
220  if (!A.size)
221  memcpy(r.tab,B.tab,sizeof(E)*B.size);
222  else if(!B.size)
223  memcpy(r.tab,A.tab,sizeof(E)*A.size);
224  else if (A.size >= B.size){
225  int i = 0;
226  for (; i < B.size; ++i)
227  r.tab[i] = A.tab[i]+B.tab[i];
228  for(;i< A.size;i++)
229  r.tab[i] = A.tab[i];
230  }else{
231  int i = 0;
232  for (; i < A.size; ++i)
233  r.tab[i] = A.tab[i]+B.tab[i];
234  for(;i< B.size;i++)
235  r.tab[i] = B.tab[i];
236  }
237 }
238 #endif // //_dynamicexp_H_
~dynamicexp()
Definition: dynamicexp.hpp:39
E * tab
Definition: dynamicexp.hpp:87
#define max(a, b)
Definition: alp_f2c.H:167
dynamicexp(const dynamicexp< X, E > &d)
Definition: dynamicexp.hpp:25
int lvar(const dynamicexp< X, E > &A)
Definition: dynamicexp.hpp:178
void free(void *)
void reserve(int s)
Definition: dynamicexp.hpp:59
Dynamic exponent.
Definition: dynamicexp.hpp:17
void build(dynamicexp< X, E > &A, int s, E *tab)
Definition: dynamicexp.hpp:126
void * malloc(YYSIZE_T)
int size
Definition: dynamicexp.hpp:86
void add(dynamicexp< X, E > &r, const dynamicexp< X, E > &A, const dynamicexp< X, E > &B)
Definition: dynamicexp.hpp:213
if(!(yy_init))
Definition: lex.mpoly.c:689
MSKconetypee MSKrealt MSKint32t MSKint32t j
Definition: mosek.h:2421
dynamicexp(int s)
Definition: dynamicexp.hpp:37
self_t & operator+=(const self_t &B)
Definition: dynamicexp.hpp:191
friend bool operator!=(const dynamicexp &A, const dynamicexp &B)
Definition: dynamicexp.hpp:83
void SDegree(dynamicexp< X, E > &A, int i, int d)
Definition: dynamicexp.hpp:140
dynamicexp()
Definition: dynamicexp.hpp:24
dynamicexp< X, E >::degree_t Degree(const dynamicexp< X, E > &t)
Definition: dynamicexp.hpp:91
self_t setExponent(int i, int d)
Definition: dynamicexp.hpp:72
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
dynamicexp(int s, E *t)
Definition: dynamicexp.hpp:33
MSKrescodee r
Definition: mosek.h:2321
E operator[](int i) const
Definition: dynamicexp.hpp:41
E exponent_t
Definition: dynamicexp.hpp:19
dynamicexp & operator=(const dynamicexp &A)
Definition: dynamicexp.hpp:46
dynamicexp< X, E > self_t
Definition: dynamicexp.hpp:21
int degree_t
Definition: dynamicexp.hpp:20
friend bool operator==(const dynamicexp &A, const dynamicexp &B)
Definition: dynamicexp.hpp:78
void erase(dynamicexp< X, E > &A)
Definition: dynamicexp.hpp:183
Home  |  Download & InstallContributions