C++ main module for mmsd Package  1.0
LAP_Vector.h
Go to the documentation of this file.
1 #ifndef LAP_Vector_H
2 #define LAP_Vector_H
3 
4 #include "LAP_Object.h"
5 #include "LAP_View.h"
6 
7 
15 template<class T>
16 class LAP_Vector: public virtual LAP_Object {
17 
18 public:
19 
20  //for sorting methods
21  static const tFlag ASCENT;
22  static const tFlag DESCENT;
23 private:
24 
25 
26  // capacity of the values
27  tLVectorIndex mCapacity;
28 
29  // values of vector
30  T* mValues;
31 
32  //view of the vector
33  LAP_View mView;
34 
35  // boolean than indicates if the mValues pointer must be destroyed at reallocation or deleting
36  tBoolean mIsVectorReference;
37 
38 
39 
40  // METHODS
41 
42 
43 protected:
44  // CONSTRUCTORS
45 
49  mValues=null;
50  mCapacity=0;
51  mIsVectorReference=false;
52  }
53 
54 
55 
56 
57  // DESTRUCTORS
60  virtual ~LAP_Vector() {
61  deleteVector();
62  };
63 
64 private:
65  void deleteVector() {
66  if (!mIsVectorReference) {
67  if (mValues!=null) {
68  delete[] mValues;
69  mValues=null;
70  mView.reset();
71  mCapacity=0;
72  }
73 
74  }
75  }
76 
77  // -----------------------
78  // shared pointer operator
79  // ------------------------
80 public:
83  void getSharedPointer(boost::shared_ptr<LAP_Vector<T> >& p){
84  SP::CORE_Object r;
86  p=boost::dynamic_pointer_cast<LAP_Vector<T> >(r);
87  };
90  void getSharedPointer( boost::shared_ptr<const LAP_Vector<T> >& p) const{
91  SPC::CORE_Object r;
93  p=boost::dynamic_pointer_cast<const LAP_Vector<T> >(r);
94  };
95 private:
98  inline boost::shared_ptr<LAP_Vector<T> > getThis() {
99  boost::shared_ptr<LAP_Vector<T> > p;
100  getSharedPointer(p);
101  return p;
102  };
105  inline boost::shared_ptr<const LAP_Vector<T> > getThis() const {
106  boost::shared_ptr<const LAP_Vector<T> > p;
107  getSharedPointer(p);
108  return p;
109  };
110 
111  //OPERATOR ACCESSORS
112  //=================
113 
114 public:
115  // operators get the i-th value of the vector without view
118  inline const T& operator[](const tLVectorIndex& i) const {
119  if (i>=mCapacity)
120  throw LAP_Exception("math/linalg/core","LAP_Vector<T>[]","index "+CORE_Integer::toString(i)+" out of bounds [0,"+CORE_Integer::toString(mCapacity)+"[");
121 
122  return mValues[i];
123  };
124  // operators get the i-th value of the vector without view
127  inline T& operator[](const tLVectorIndex& i) {
128  if (i>=mCapacity)
129  throw LAP_Exception("math/linalg/core","LAP_Vector<T>[]","index "+CORE_Integer::toString(i)+" out of bounds [0,"+CORE_Integer::toString(mCapacity)+"[");
130 
131  return mValues[i];
132 
133  };
134 
135  // operators get the i-th element of the view of the vector
138  inline T& operator()(const tLVectorIndex& i) {
139  return (*this)[mView.getStart()+mView.getIncrement()*i];
140  };
141  // operators get the i-th element of the view of the vector
144  inline const T& operator()(const tLVectorIndex& i) const {
145  return (*this)[mView.getStart()+mView.getIncrement()*i];
146  };
147 
148 
149 
150  //computation operators
151  //=====================
154  void add(const T& s) {
155  tLVectorIndex i,n=getSize();
156  const tLVectorIncrement &inc=getIncrement();
157  T *ts=&(*this)(0);
158  for (i=0;i<n;i++) {
159  *ts+=s;
160  ts+=inc;
161  }
162  }
163 
166  inline void sub(const T& s) {
167  tLVectorIndex i,n=getSize();
168  const tLVectorIncrement& inc=getIncrement();
169  T *ts=&(*this)(0);
170  for (i=0;i<n;i++) {
171  *ts-=s;
172  ts+=inc;
173  }
174  };
175 
178  inline void power(const T& s) {
179  tLVectorIndex i,n=getSize();
180  T *ts=&(*this)(0);
181  const tLVectorIncrement& inc=getIncrement();
182  if (s==1) return ;
183  else if (s==-1) {
184  for (i=0;i<n;i++) {
185  *ts=(T) 1./(*ts);
186  ts+=inc;
187  }
188  } else if (s==-2) {
189  for (i=0;i<n;i++) {
190  *ts=(T) 1./((*ts)*(*ts));
191  ts+=inc;
192  }
193  } else if (s==2) {
194  for (i=0;i<n;i++) {
195  *ts=(T) ((*ts)*(*ts));
196  ts+=inc;
197  }
198  } else {
199  for (i=0;i<n;i++) {
200  *ts=(T) pow((tReal)*ts,(tReal)s);
201  ts+=inc;
202  }
203  }
204  };
205 
208  inline void multiplyBy(const LAP_Vector<T>& s) {
209  tLVectorIndex i,n=getSize();
210  if (s.getSize()!=n) throw LAP_Exception("math/linalg/core",
211  "*= operator",
212  "incompatible vector dimension");
213  const T *vs=&s(0);
214  T *ts=&(*this)(0);
215  const tLVectorIncrement& incs=s.getIncrement();
216  const tLVectorIncrement& inc=getIncrement();
217  for (i=0;i<n;i++) {
218  *ts*=*vs;
219  vs+=incs;
220  ts+=inc;
221  }
222  };
226  tLVectorIndex i,n=getSize();
227  if (s.getSize()!=n) throw LAP_Exception("math/linalg/core",
228  "/= operator",
229  "incompatible vector dimension");
230 
231  const T *vs=&s(0);
232  T *ts=&(*this)(0);
233  const tLVectorIncrement& incs=s.getIncrement();
234  const tLVectorIncrement& inc=getIncrement();
235  for (i=0;i<n;i++) {
236  *ts/=*vs;
237  vs+=incs;
238  ts+=inc;
239  }
240  return (*this);
241  };
242 
243 
244  //SET methods
245  //===========
246  // copy method
247 
250  virtual void copy(const vector<T>& s) {
251  tLVectorIndex i,n=s.size();
252  setSize(n);
253  T *ts=mValues;
254  for (i=0;i<n;i++) {
255  *ts=s[i];
256  ts++;
257  }
258  }
259 
263  inline void setIsValuesReferenced(const tBoolean& isReferenced) {
264  mIsVectorReference=isReferenced;
265  }
266  /* \brief set the values to values of size n
267  * if isReferenced is true the vector is not destroyed with the class
268  * otherwise it is destroyed
269  */
270  inline void setValues(const tLVectorIndex& n,
271  T*& values,
272  const tBoolean& isReferenced) {
273  //delete the old values
274  deleteVector();
275  //make the pointer to the new values
276  mValues=values;
277  //set the capacity of the vector to n
278  mCapacity=n;
279  //set the the values pointer is referenced by another object
280  mIsVectorReference=isReferenced;
281 
282  //update the view
283  mView.setView(0,n,1);
284  };
289  inline void setValues(const tLVectorIndex& n,const T* values) {
290  setValues(n,1,values);
291  }
299  inline void setValues(const tLVectorIndex& n,const tLVectorIncrement& incv,const T* values) {
300 
301  if (n>0) {
302  if (n!=mCapacity) {
303  deleteVector();
304  try {
305  mValues=new T[n+1];
306  } catch (std::exception e) {
307  throw LAP_Exception("math/linalg/core","LAP_Vector<T>::setValues()",
308  "memory allocation over flow for size "+CORE_Integer::toString(n));
309  }
310  if (mValues==null) throw LAP_Exception("math/linalg/core","LAP_Vector<T>::setValues()",
311  "memory allocation over flowfor size "+CORE_Integer::toString(n));
312  }
313 
314  T *pValues=&mValues[0];
315  const T *vs=values;
316  for (tLVectorIndex i=0;i<n;i++) {
317  *pValues=*vs;
318  pValues++;
319  vs+=incv;
320  }
321  *pValues=0;
322 
323  mCapacity=n+1;
324  mIsVectorReference=false;
325  mView.setView(0,n,1);
326  } else {
327  deleteVector();
328  mValues=null;
329  mCapacity=0;
330  mView.reset();
331  }
332  }
335  inline tBoolean setView(SP::LAP_View v) {
336  return setView(v->getStart(),v->getEnd(),v->getIncrement());
337  }
338 
342  inline tBoolean setView(const tLVectorIndex& start,const tLVectorIndex& end,const tLVectorIndex& increment) {
343  if (increment==0) return false;
344  if (end<start) return false;
345  if (end-increment>=mCapacity) return false;
346  mView.setView(start,end,increment);
347  return true;
348  }
349 
352  inline void resetView() {
353  setView(0,mCapacity-1,1);
354  }
355 
360  inline void setSize(const tLVectorIndex& n) {
361  if (getSize()!=n) {
362  if (!setView(0,n,1)) {
363  //resize the vector
364  resize(n);
365  //re-set the view
366  setView(0,n,1);
367  }
368  }
369  }
370 
371 
372 private:
375  inline void resize(const tLVectorIndex& n) {
376  if (n!=mCapacity) {
377  T *newVector=null;
378  //reallocate the new vector of size n
379  try {
380  newVector=new T[n+1];
381  if (newVector==null) throw LAP_Exception("math/linalg/core","LAP_Vector<T>::resize()",
382  "memory allocation over flow for size "+CORE_Integer::toString(n));
383  } catch(std::exception e) {
384  throw LAP_Exception("math/linalg/core","LAP_Vector<T>::resize()",
385  "memory allocation over flow for size "+CORE_Integer::toString(n));
386  }
387 
388  //copy the old values taking into account the view
389  tLVectorIndex oldSize=mView.getSize();
390  tLVectorIndex k=0,i,dim=(oldSize<n)?oldSize:n;
391 
392  const tLVectorIncrement& inc=getIncrement();
393  //copy the old values
394  i=mView.getStart();
395  for (k=0;k<dim;k++) {
396  newVector[k]=mValues[i];
397  i+=inc;
398  }
399  for (i=dim;i<=n;i++) {
400  newVector[i]=(T) 0;
401  }
402 
403  //delete the old vector
404  deleteVector();
405  //reallocate the new vector
406  mValues=newVector;
407  mIsVectorReference=false;
408  //update the capacity
409  mCapacity=n+1;
410  //update the view
411  mView.setView(0,n,1);
412  }
413  };
414 public:
417  inline void fit() {
418  resize(mView.getSize());
419  };
420 
423  inline void init(const T& v) {
424  tLVectorIndex i;
425  const tLVectorIndex& n=mView.getEnd();
426  const tLVectorIncrement& inc=mView.getIncrement();
427  for (i=mView.getStart();i<n;i+=inc) (*this)[i]=v;
428  };
429 
435  inline void removeValues(const tLVectorIndex& index,const tLVectorIndex& n) {
436  // get the number of elements
437  tLVectorIndex s=mView.getSize();
438 
439  //get the max element to erase
440  int nElementsToErase=n;
441  int maxElementsToErase=s-index;
442  if ( ((int)n)>maxElementsToErase) nElementsToErase=maxElementsToErase;
443  if (nElementsToErase==0) return;
444 
445  //get the increment of the view
446  const tLVectorIncrement& inc=mView.getIncrement();
447 
448  //first index to remove
449  tLVectorIndex i0=mView.getStart()+index*inc;
450  //after last index to remove
451  tLVectorIndex i1=i0+n*inc;
452 
453  //pointer to first element to erase
454  T* v1=&mValues[i0];
455  //pointer to element to copy to v1
456  T *v2=&mValues[i1];
457  int i=0;
458  while (i1<mCapacity) {
459  *v1=*v2;
460  v1+=inc;
461  v2+=inc;
462  i++;
463  i1+=inc;
464  }
465 
466  mView.setView(mView.getStart(),mView.getStart()+(s-nElementsToErase)*inc,inc);
467  };
468 
469 
472  inline void set(const tLVectorIndex& i,const T& v) {
473  (*this)(i)=v;
474  };
475 
476 
479  virtual void add(const tLVectorIndex& i,const T& v) {
480  (*this)(i)+=v;
481  };
482 
483 
487  void merge(const T &alpha,const LAP_Vector<T>& x,
488  const T &beta,const LAP_Vector<T>& y);
489 
490  // GET methods
491  // ===========
492 
493 public:
494 
497  virtual const T* getValues() const {
498  return mValues;
499  };
500 
503  virtual T* getValues() {
504  return mValues;
505  };
506 
509  virtual tLVectorIndex getNullValuesNumber(const tReal& eps) const=0;
510 
513  virtual const T& get(const tLVectorIndex & i) const {
514  return (*this)(i);
515  };
516 
519  inline tLVectorIndex getSize() const {
520  return mView.getSize();
521  }
524  inline tLVectorIndex getCapacity() const {
525  return mCapacity;
526  }
529  inline const tLVectorIncrement& getIncrement() const {
530  return mView.getIncrement();
531  }
534  inline const tLVectorIndex& getStart() const {
535  return mView.getStart();
536  }
539  T sum() const;
540 
543  inline static void max(const LAP_Vector<T>& x,LAP_Vector<T>& y) {
544 
545  // take the min dimension
546  tLVectorIndex dimx=x.getSize();
547  tLVectorIndex dimy=y.getSize();
548  const tLVectorIncrement& incx=x.getIncrement();
549  const tLVectorIncrement& incy=y.getIncrement();
550 
551  // dim=min(dimx,dimy)
552  tLVectorIndex i,dim=(dimx>dimy)?dimy:dimx;
553 
554  //get the first element form start
555  const T *ptr_x=&x(0);
556  T *ptr_y=&y(0);
557  for (i=0;i<dim;i++) {
558  *ptr_y=(*ptr_x>*ptr_y)?*ptr_x:*ptr_y;
559  ptr_x+=incx;
560  ptr_y+=incy;
561  }
562  }
563 
566  inline void max(LAP_Vector<T>& y) {
567  max(*this,y);
568  }
569 
572  T maxValue(tLVectorIndex& i) const ;
575  T minValue(tLVectorIndex& i) const;
576 
579  tLVectorIndex indexMin() const;
580 
583  inline tReal distance2(const LAP_Vector<T>& a) const {
584  return LAP_Vector<T>::distance2(*this,a);
585  }
588  static tReal distance2(const LAP_Vector<T>& a, const LAP_Vector<T>& b);
589 
590 
591 
592  //----------
593  // sorting
594  // ---------
595 
598  void quickSort();
599 
600  private:
601  void quickSort(T* v,
602  const tLVectorIncrement& incv,
603  const tLVectorIndex& from,
604  const tLVectorIndex& left);
605 
606  public:
609  void sort(const tFlag& order);
610 
611 
614  inline void sort() {
615  sort(ASCENT);
616  };
617 
620  inline tBoolean compare(const T& a,const T& b,const tFlag& order) {
621  if (order==DESCENT) {
622  return (a>=b);
623  } else {
624  return (a<=b);
625  }
626  }
629  void reverse();
630 
631 };
632 
633 #include "LAP_Vector.hpp"
634 
635 #endif
tLVectorIndex getCapacity() const
get the capacity
Definition: LAP_Vector.h:524
this class describes the exceptions raised for LAP package
Definition: LAP_Exception.h:14
tBoolean setView(SP::LAP_View v)
set the view
Definition: LAP_Vector.h:335
void setIsValuesReferenced(const tBoolean &isReferenced)
deferenced the values the values is unreferenced : so it is not destroyed with this class is desctroy...
Definition: LAP_Vector.h:263
This class is a view of a pointer.
Definition: LAP_View.h:14
static const tFlag DESCENT
Definition: LAP_Vector.h:22
const T & operator()(const tLVectorIndex &i) const
get the value for reading only
Definition: LAP_Vector.h:144
void sub(const T &s)
sub operator
Definition: LAP_Vector.h:166
const tLVectorIndex & getStart() const
get the start of the vector
Definition: LAP_Vector.h:534
static void max(const LAP_Vector< T > &x, LAP_Vector< T > &y)
y=max(x,y);
Definition: LAP_Vector.h:543
const T & operator[](const tLVectorIndex &i) const
get the value for reading only
Definition: LAP_Vector.h:118
void set(const tLVectorIndex &i, const T &v)
set element at index i taking into account the view
Definition: LAP_Vector.h:472
T sum() const
compute the sum of the elements
Definition: LAP_Vector.hpp:170
T maxValue(tLVectorIndex &i) const
return the max value of the vector and the corresponding index
Definition: LAP_Vector.hpp:15
#define tBoolean
Definition: types.h:48
T & operator[](const tLVectorIndex &i)
get the value for reading & writing
Definition: LAP_Vector.h:127
void fit()
fit the vector
Definition: LAP_Vector.h:417
const tLVectorIndex & getStart() const
get the start index of the first element in [start,end[
Definition: LAP_View.h:130
void setView(const tLVectorIndex &start, const tLVectorIndex &end, const tLVectorIndex &inc)
set the the view the element considered are in [start,end[ with increment inc
Definition: LAP_View.h:88
#define null
Definition: types.h:13
const tLVectorIncrement & getIncrement() const
get the increment of the vector
Definition: LAP_Vector.h:529
virtual void add(const tLVectorIndex &i, const T &v)
add v to element at index i taking into account the view
Definition: LAP_Vector.h:479
void max(LAP_Vector< T > &y)
y=max(this,y);
Definition: LAP_Vector.h:566
virtual T * getValues()
get the values for writing
Definition: LAP_Vector.h:503
const tLVectorIncrement & getIncrement() const
get increment
Definition: LAP_View.h:125
tLVectorIndex getSize() const
get the size of the vector
Definition: LAP_Vector.h:519
tReal distance2(const LAP_Vector< T > &a) const
compute the distance between this and a
Definition: LAP_Vector.h:583
void getSharedPointer(boost::shared_ptr< const LAP_Vector< T > > &p) const
return the shared pointer corresponding to the class whith casting
Definition: LAP_Vector.h:90
void merge(const T &alpha, const LAP_Vector< T > &x, const T &beta, const LAP_Vector< T > &y)
this is the merging of 2 vectors This = [alpha.x,beta.y]
Definition: LAP_Vector.hpp:96
This class is the base class of all lapack objects.
Definition: LAP_Object.h:17
virtual const T * getValues() const
get the values for reading
Definition: LAP_Vector.h:497
void quickSort()
quit sort the vector in an increasing order
Definition: LAP_Vector.hpp:233
this class describes a vector of double
Definition: LAP_Vector.h:16
void power(const T &s)
power operator
Definition: LAP_Vector.h:178
void reset()
reset
Definition: LAP_View.h:114
T & operator()(const tLVectorIndex &i)
get the value for reading & writing
Definition: LAP_Vector.h:138
void getSharedPointer(SP::CORE_Object &p)
get the shared pointer of this class into p
Definition: CORE_Object.h:65
virtual tLVectorIndex getNullValuesNumber(const tReal &eps) const =0
tString toString() const
return the string associated to the integer
Definition: CORE_Integer.h:142
virtual void copy(const vector< T > &s)
copy the vector
Definition: LAP_Vector.h:250
LAP_Vector()
build a vector
Definition: LAP_Vector.h:48
LAP_Vector< T > & divideBy(const LAP_Vector< T > &s)
division elementt by elementt vector operator
Definition: LAP_Vector.h:225
#define tLVectorIndex
Definition: lapack_types.h:13
void sort()
sort in ascent order view untaken into account
Definition: LAP_Vector.h:614
void setValues(const tLVectorIndex &n, const tLVectorIncrement &incv, const T *values)
copy the values
Definition: LAP_Vector.h:299
tLVectorIndex indexMin() const
retun the index min of th evector
Definition: LAP_Vector.hpp:56
void multiplyBy(const LAP_Vector< T > &s)
multiplication element by element vector operator
Definition: LAP_Vector.h:208
void setValues(const tLVectorIndex &n, const T *values)
copy the values
Definition: LAP_Vector.h:289
void add(const T &s)
add operator
Definition: LAP_Vector.h:154
tBoolean compare(const T &a, const T &b, const tFlag &order)
compare
Definition: LAP_Vector.h:620
tBoolean setView(const tLVectorIndex &start, const tLVectorIndex &end, const tLVectorIndex &increment)
set the view of the vector
Definition: LAP_Vector.h:342
virtual ~LAP_Vector()
destroy a vector
Definition: LAP_Vector.h:60
void init(const T &v)
init the value to v from the view
Definition: LAP_Vector.h:423
static const tFlag ASCENT
Definition: LAP_Vector.h:21
void setValues(const tLVectorIndex &n, T *&values, const tBoolean &isReferenced)
Definition: LAP_Vector.h:270
#define tLVectorIncrement
Definition: lapack_types.h:16
T minValue(tLVectorIndex &i) const
return the min value of the vector and the corresponding index
Definition: LAP_Vector.hpp:35
void getSharedPointer(boost::shared_ptr< LAP_Vector< T > > &p)
return the shared pointer corresponding to the class with casting
Definition: LAP_Vector.h:83
const tLVectorIndex & getEnd() const
get the end index of the last element in [start,end[
Definition: LAP_View.h:135
#define tReal
Definition: types.h:18
tLVectorIndex getSize() const
Definition: LAP_View.h:141
void removeValues(const tLVectorIndex &index, const tLVectorIndex &n)
remove n values of the array from index if values=[0,1,...,index,...index+n,index+n+,....,size[ the values {index,...index+n-1} are removed values becomes values=[0,...index-1,index+n,...,size-n[
Definition: LAP_Vector.h:435
void resetView()
set the view of the vector to all the values
Definition: LAP_Vector.h:352
void setSize(const tLVectorIndex &n)
set the view to [0,n[ by 1 increment if values is too small, re-alocate it
Definition: LAP_Vector.h:360
void reverse()
reverse the vector
Definition: LAP_Vector.hpp:270
#define tFlag
Definition: types.h:14