C++ main module for gpm Package  1.0
MATH_Variable.h
Go to the documentation of this file.
1 #ifndef MATH_Variable_H
2 #define MATH_Variable_H
3 
4 #include "CORE_Object.h"
5 #include "CORE_ListPointers.h"
6 
16 class MATH_Variable : public CORE_Object {
17  SP_OBJECT(MATH_Variable);
18 
19  // ATTRIBUTES
20 public:
21  static const tFlag BOUND_POINT[];
22  static const tFlag BOUND_SIZE[];
23 
24  //TYPE
25  static const tFlag UNDEFINED;
26  static const tFlag REAL;
27  static const tFlag BOOLEAN;
28  static const tFlag INTEGER;
29  static const tFlag STRING;
30 private:
31 
32  double *mAllValues;
33  double *mValues;
34  int mFirstIndex;
35  int mCapacity;
36  int mSize;
37  int mType;
38 
39 
40 protected:
41  // METHODS
42 
43  // CONSTRUCTORS
44 
47  MATH_Variable(const int& cap);
48 
49 
50 
51  // DESTRUCTORS
52 
53 
56  virtual ~MATH_Variable(void);
57 
58 private:
59 
60 public:
61 
62 
65  static inline SP::MATH_Variable New() {
66  SP::MATH_Variable p(new MATH_Variable(512),MATH_Variable::Delete());
67  p->setThis(p);
68  return p;
69  }
72  static inline SP::MATH_Variable New(const int& cap) {
73  SP::MATH_Variable p(new MATH_Variable(cap),MATH_Variable::Delete());
74  p->setThis(p);
75  return p;
76  }
77 
78 
79  //operator
82  inline const double& operator[](const int& index) const {
83  ASSERT_IN(mFirstIndex+index<mCapacity);
84  return mValues[index];
85  }
88  inline double& operator[](const int& index) {
89  ASSERT_IN(mFirstIndex+index<mCapacity);
90  return mValues[index];
91  }
92 
93 
94  // SET methods
95 
98  virtual void copy(const MATH_Variable& v);
99 
102  inline void clear() {
103  mFirstIndex=0;
104  mSize=0;
105  mValues=&mAllValues[0];
106  mType=UNDEFINED;
107  }
114  tBoolean setSize(const int& n);
115 
118  inline void setFirstIndex(const int& index) {
119  if (mFirstIndex+mSize>=mCapacity) setSize(mSize);
120  mFirstIndex=index;
121  mValues=&mAllValues[index];
122  }
125  inline void setStartIndex(const int& index) {
126  if (mFirstIndex+mSize>=mCapacity) setSize(mSize);
127  mFirstIndex=index;
128  mValues=&mAllValues[index];
129  }
132  inline void setValue(const int& index,const double& v) {
133  setSize(index+1);
134  mValues[index]=v;
135  }
138  void setValue(const tString& symbol);
141  inline void setValue(const double& v) {
142  mType=REAL;
143  setSize(1);
144  mValues[0]=v;
145  }
148  inline void setValue(const double* v,const int& n) {
149  mType=REAL;
150  setSize(n);
151  for (int i=0;i<n;i++) mValues[i]=v[i];
152  }
155  inline void setValue(const long double* v,const int& n) {
156  mType=REAL;
157  setSize(n);
158  for (int i=0;i<n;i++) mValues[i]=(double)v[i];
159  }
162  inline void setValue(const int& v) {
163  mType=INTEGER;
164  setSize(1);
165  mValues[0]=(double)v;
166  }
169  inline void setValue(const int* v,const int& n) {
170  mType=INTEGER;
171  setSize(n);
172  for (int i=0;i<n;i++) mValues[i]=(double) v[i];
173  }
176  inline void setValue(const tBoolean& v) {
177  mType=BOOLEAN;
178  setSize(1);
179  mValues[0]=((int)v)%2;
180  }
183  inline void setType(const tFlag& type) {
184  mType=type;
185  }
186  // GET
189  inline void getValue(double* v,int& n) const {
190  n=(mSize<n)?mSize:n;
191  for (int i=0;i<n;i++) v[i]=mValues[i];
192  }
195  inline void getValue(int* v,int& n) const {
196  n=(mSize<n)?mSize:n;
197  for (int i=0;i<n;i++) v[i]=(int)mValues[i];
198  }
201  inline void getValue(const int& index,int& v) const {
202  double vd;
203  vd=mValues[index];
204  v=(int) vd;
205  }
208  inline void getValue(const int& index,double& v) const {
209  v=mValues[index];
210  }
211 
214  inline void getValue(double& v) const {
215  getValue(0,v);
216  }
219  inline void getValue(int& v) const {
220  getValue(0,v);
221  }
224  inline void getValue(tString& v) const {
225  v.resize(mSize);
226  for (int i=0;i<mSize;i++) {
227  v[i]=(char) mValues[i];
228  }
229  }
232  inline tFlag getType() const{
233  return mType;
234  }
237  inline int getSize() const{
238  return mSize;
239  }
240 
243  inline int getFirstIndex() const {
244  return mFirstIndex;
245  }
248  inline int getStartIndex() const {
249  return mFirstIndex;
250  }
251 
254  inline int getCapacity() const {
255  return mCapacity;
256  }
257 
258  // CONVERTER
259 
262  inline static tBoolean isNumericalType(const tFlag& type) {
263  return ((type==REAL)||(type==INTEGER)|| (type==BOOLEAN));
264  };
267  inline tBoolean isNumericalType() const {
268  return isNumericalType(mType);
269  };
272  inline tBoolean isBoolean() const {
273  return (mType==BOOLEAN);
274  };
277  inline tBoolean isInteger() const {
278  return (mType==INTEGER);
279  };
282  inline tBoolean isReal() const {
283  return (mType==REAL);
284  };
287  inline tBoolean isString() const {
288  return (mType==STRING);
289  };
290 
293  tBoolean isEqual(const MATH_Variable& var,const tReal& eps) const;
296  inline tBoolean isEqual(const MATH_Variable& var) const {
297  return isEqual(var,1.e-12);
298  }
299 
302  inline static int toInteger(const double& v) {
303  return (int) (round(v));
304  };
307  inline static tBoolean toBoolean(const double& v) {
308  return (tBoolean) (toInteger(v)%2);
309  };
310 
313  virtual tString toString() const;
314 
315 
316 };
317 
318 #endif
DEFINE_SVPTR(MATH_Variable)
void setValue(const double *v, const int &n)
set the value
Definition: MATH_Variable.h:148
void getValue(int &v) const
get value
Definition: MATH_Variable.h:219
tBoolean isEqual(const MATH_Variable &var, const tReal &eps) const
return true if the variable are equal
Definition: MATH_Variable.cpp:108
tBoolean isEqual(const MATH_Variable &var) const
return true if the variable are equal
Definition: MATH_Variable.h:296
This class decribes a variable class for evaluation.
Definition: MATH_Variable.h:16
static const tFlag REAL
Definition: MATH_Variable.h:26
static int toInteger(const double &v)
to int
Definition: MATH_Variable.h:302
static SP::MATH_Variable New()
create a node expression
Definition: MATH_Variable.h:65
void setValue(const double &v)
set the value
Definition: MATH_Variable.h:141
tBoolean isString() const
retrun true if the type is string
Definition: MATH_Variable.h:287
tBoolean setSize(const int &n)
set the size of the variable be careful of the used of operator[] : the values may be reallocated whi...
Definition: MATH_Variable.cpp:37
virtual void copy(const MATH_Variable &v)
copy the variable
Definition: MATH_Variable.cpp:28
tBoolean isReal() const
retrun true if the type is real
Definition: MATH_Variable.h:282
tBoolean isBoolean() const
retrun true if the type is boolean
Definition: MATH_Variable.h:272
void setValue(const long double *v, const int &n)
set the value
Definition: MATH_Variable.h:155
MATH_Variable(const int &cap)
create
Definition: MATH_Variable.cpp:15
#define tBoolean
Definition: types.h:35
double & operator[](const int &index)
operator get for writing
Definition: MATH_Variable.h:88
int getFirstIndex() const
get the first index
Definition: MATH_Variable.h:243
void clear()
clear the variable
Definition: MATH_Variable.h:102
void setType(const tFlag &type)
set the type
Definition: MATH_Variable.h:183
static tBoolean toBoolean(const double &v)
to boolean
Definition: MATH_Variable.h:307
static SP::MATH_Variable New(const int &cap)
create a node expression
Definition: MATH_Variable.h:72
void setValue(const int &v)
set the value
Definition: MATH_Variable.h:162
void getValue(const int &index, int &v) const
get value
Definition: MATH_Variable.h:201
static tBoolean isNumericalType(const tFlag &type)
retrun true if the type is numerical
Definition: MATH_Variable.h:262
static const tFlag BOOLEAN
Definition: MATH_Variable.h:27
void setValue(const int *v, const int &n)
set the value
Definition: MATH_Variable.h:169
void getValue(double &v) const
get value
Definition: MATH_Variable.h:214
int getSize() const
get the size
Definition: MATH_Variable.h:237
virtual tString toString() const
print the environment class in a string
Definition: MATH_Variable.cpp:123
void getValue(int *v, int &n) const
get value: n is the real size of v the max size is the value of n at INPUT
Definition: MATH_Variable.h:195
tBoolean isNumericalType() const
retrun true if the type is numerical
Definition: MATH_Variable.h:267
void getValue(const int &index, double &v) const
get value
Definition: MATH_Variable.h:208
static const tFlag BOUND_SIZE[]
Definition: MATH_Variable.h:22
virtual ~MATH_Variable(void)
destroy
Definition: MATH_Variable.cpp:24
int getStartIndex() const
get the first index
Definition: MATH_Variable.h:248
void setStartIndex(const int &index)
set the start index
Definition: MATH_Variable.h:125
abstract base class for most classes.
Definition: CORE_Object.h:30
const double & operator[](const int &index) const
operator get for reading
Definition: MATH_Variable.h:82
void getValue(tString &v) const
get value
Definition: MATH_Variable.h:224
#define tString
Definition: types.h:36
static const tFlag STRING
Definition: MATH_Variable.h:29
DEFINE_SPTR(MATH_Variable)
void setFirstIndex(const int &index)
set the first index
Definition: MATH_Variable.h:118
void setValue(const int &index, const double &v)
set the value
Definition: MATH_Variable.h:132
void getValue(double *v, int &n) const
get value : n is the real size of v the max size is the value of n at INPUT
Definition: MATH_Variable.h:189
int getCapacity() const
return the capacity of the variable
Definition: MATH_Variable.h:254
static const tFlag BOUND_POINT[]
Definition: MATH_Variable.h:21
tFlag getType() const
get the type
Definition: MATH_Variable.h:232
tBoolean isInteger() const
retrun true if the type is integer
Definition: MATH_Variable.h:277
static const tFlag INTEGER
Definition: MATH_Variable.h:28
#define tReal
Definition: types.h:18
#define ASSERT_IN(a)
Definition: types.h:82
void setValue(const tBoolean &v)
set the value
Definition: MATH_Variable.h:176
static const tFlag UNDEFINED
Definition: MATH_Variable.h:25
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14