C++ main module for gpm Package  1.0
CORE_Vector.h
Go to the documentation of this file.
1 #ifndef CORE_Vector_H
2 #define CORE_Vector_H
3 #include "CORE_List.h"
4 #include "types.h"
5 #include <vector>
7 
8 #include "CORE_String.h"
9 using namespace std;
10 
11 
12 
17 template <class T>
18 class CORE_Vector : public CORE_List {
19 
20 
21 private:
22  vector<T> mVector;
23 
24  int mIterator;
25 
26 
27 
28  // CONSTRUCTORS
29 public:
32  CORE_Vector();
33 
36  CORE_Vector(const int& n);
37 
40  CORE_Vector(const CORE_Vector<T>& v);
41 
42 
43 
44 
45 
46  // DESTRUCTORS
47 public:
50  virtual ~CORE_Vector();
51 
52 
53 public:
54  // NEW operators
58  static inline boost::shared_ptr<CORE_Vector<T> > New(const boost::shared_ptr<CORE_Vector<T> >& v ) {
59  boost::shared_ptr<CORE_Vector<T> > p(new CORE_Vector<T>(),CORE_Object::Delete());
60  p->setThis(p);
61  p->copy(v);
62  return p;
63  };
67  static inline boost::shared_ptr<CORE_Vector<T> > New(const CORE_Vector<T>& v ) {
68  boost::shared_ptr<CORE_Vector<T> > p(new CORE_Vector<T>(),CORE_Object::Delete());
69  p->setThis(p);
70  p->copy(v);
71  return p;
72  };
76  static inline boost::shared_ptr<CORE_Vector<T> > New(const CORE_Vector<T>* v ) {
77  boost::shared_ptr<CORE_Vector<T> > p(new CORE_Vector<T>(),CORE_Object::Delete());
78  p->setThis(p);
79  if (v!=null) p->copy(*v);
80  return p;
81  };
84  static inline boost::shared_ptr<CORE_Vector<T> > New() {
85  boost::shared_ptr<CORE_Vector<T> > p(new CORE_Vector<T>(),CORE_Object::Delete());
86  p->setThis(p);
87  return p;
88  };
92  static inline boost::shared_ptr<CORE_Vector<T> > New(const int& dim) {
93  boost::shared_ptr<CORE_Vector<T> > p(new CORE_Vector<T>(),CORE_Object::Delete());
94  p->setThis(p);
95  p->setSize(dim);
96  return p;
97  };
98  // OPERATORS
99 public:
102  void getSharedPointer(boost::shared_ptr<CORE_Vector<T> >& p){
103  SP::CORE_Object r;
105  p=boost::dynamic_pointer_cast<CORE_Vector<T> >(r);
106  };
109  void getSharedPointer( boost::shared_ptr<const CORE_Vector<T> >& p) const{
110  SPC::CORE_Object r;
112  p=boost::dynamic_pointer_cast<const CORE_Vector<T> >(r);
113  };
114 private:
117  inline boost::shared_ptr<CORE_Vector<T> > getThis() {
118  boost::shared_ptr<CORE_Vector<T> > p;
119  getSharedPointer(p);
120  return p;
121  };
124  inline boost::shared_ptr<const CORE_Vector<T> > getThis() const {
125  boost::shared_ptr<const CORE_Vector<T> > p;
126  getSharedPointer(p);
127  return p;
128  };
129 public:
130 
135  inline const T& operator[](int i) const {
136  ASSERT_IN(i>-1);
137  ASSERT_IN(i<((int)mVector.size()));
138  return mVector[(int)i];
139  };
140 
145  inline T& operator[](int i) {
146  ASSERT_IN(i>-1);
147  ASSERT_IN(i<((int)mVector.size()));
148  return mVector[(int)i];
149  };
150 
151 
152  // SET
153 
156  inline void setValue(const int& i,const T& v) {
157  mVector[i]=v;
158  };
159 
162  inline void setSize(const int& n){
163  mVector.resize(n);
164  };
167  inline void setCapacity(const int& n){
168  mVector.reserve(n);
169  };
170 
173  inline void initValues(const T& v) {
174  int n=mVector.size();
175  for(int i=0;i<n;i++) mVector[i]=v;
176  };
179  inline void divideBy(const tReal& v) {
180  int dim=getSize();
181  for(int i=0;i<dim;i++) mVector[i]/=v;
182  }
185  inline void multiplyBy(const tReal& v) {
186  int dim=getSize();
187  for(int i=0;i<dim;i++) mVector[i]*=v;
188  }
194  void addAfterIndex(const int& index,const T& v);
195 
201  void addAfterIndices(boost::shared_ptr<CORE_Vector<int> >& p_indices,
202  const CORE_SharedPointersList<CORE_Vector<T> >& values);
203 
206  inline void add(const CORE_Vector<T>& v) {
207  add(1,v);
208  };
211  inline void normalize() {
212  tReal n=norm();
213  if (n>0) divideBy(n);
214  };
217  inline void add(const tReal& f,const CORE_Vector<T>& v) {
218  int n=mVector.size();
219  int p=v.getSize();
220  int dim=(n<p)?n:p;
221  for(int i=0;i<dim;i++) mVector[i]+=f*v[i];
222  };
223 
226  inline void sub(const CORE_Vector<T>& v) {
227  sub(1,v);
228  };
231  inline void sub(const CORE_Vector<T>& u,const CORE_Vector<T>& v) {
232  int n=mVector.size();
233  int p=v.getSize();
234  int dim=(n<p)?n:p;
235  for(int i=0;i<dim;i++) mVector[i]=u[i]-v[i];
236  };
239  inline void sub(const tReal& f,const CORE_Vector<T>& v) {
240  int n=mVector.size();
241  int p=v.getSize();
242  int dim=(n<p)?n:p;
243  for(int i=0;i<dim;i++) mVector[i]-=f*v[i];
244  };
247  template<class Y> void copy(const CORE_Vector<Y>& array) {
248  int n=array.getSize();
249  mVector.resize(n);
250  for (int i=0;i<n;i++) mVector[i]=(T) array[i];
251  }
254  template<class Y> void copy(const CORE_Vector<Y>* array) {
255  if (array!=null) copy(*array);
256  else setSize(0);
257  }
260  template<class Y> void copy(const boost::shared_ptr<CORE_Vector<T> >& array) {
261  copy(array.get());
262  }
265  template<class Y> void setValues(const vector<Y>& array) {
266  int n=array.size();
267  mVector.resize(n);
268  for (int i=0;i<n;i++) mVector[i]=(T) array[i];
269  }
272  template<class Y> void setValues(const Y* array,const int& n) {
273  mVector.resize(n);
274  for (int i=0;i<n;i++) mVector[i]=(T) array[i];
275  }
276 
279  tBoolean set(int i,const T& obj);
280 
284  tBoolean insert(int i,const T& obj);
285 
292  int insertInOrder(const T& obj,const tBoolean& evenIfEqual);
298  inline int insert(const T& obj){
299  return insertInOrder(obj,false);
300  };
301 
302 
305  void add(const T& obj) {
306  mVector.push_back(obj);
307  };
308 
311  virtual void addInList(const T& obj) {
312  add(obj);
313  };
314 
317  void merge(const CORE_Vector<T>& array);
318 
323  inline void sort(const tString& order) {
324  sort(mVector,order);
325  };
326 
329  static void sort(vector<T>& items,
330  const tString& order);
336  tBoolean remove(const T& obj);
337 
340  tBoolean removeAtIndex(const int& i);
341 
342 
345  inline tBoolean remove() {
346  return remove(size()-1);
347  };
350  inline void clear(){
351  setSize(0);
352  mVector.clear();
353  };
354 
355  // GET
358  inline T& getLastElement() {
359  ASSERT_IN(mVector.size()>0);
360  return mVector[mVector.size()-1];
361  };
362 
367  inline const T& get(int i) const {
368  ASSERT_IN(i>-1);
369  ASSERT_IN(i<((int)mVector.size()));
370  return mVector[i];
371  };
372 
377  inline T& get(int i) {
378  ASSERT_IN(i>-1);
379  ASSERT_IN(i<((int)mVector.size()));
380  return mVector[i];
381  };
382 
384  inline int size() const {return mVector.size();};
385 
387  inline int getSize() const {return mVector.size();};
388 
390  inline int getDimension() const {return mVector.size();};
391 
392 
395  inline const T& getValue(const int& i) const {return mVector[i];};
396 
399  inline const vector<T>& getValues() const {return mVector;};
402  inline vector<T>& getValues() {return mVector;};
403 
404 
407  tBoolean exists(const T& obj) const;
408 
411  inline vector<T>& getVector() {
412  return mVector;
413  };
416  inline const vector<T>& getVector() const{
417  return mVector;
418  };
419 
422  static int search(const vector<T>& values,
423  const T& value,const tString& order);
424 
427  int search(const T& value,const tString& order) {
428  return search(mVector,value,order);
429  }
430 
431  // OTHERS
432 
433 
436  void permute(const int& i,const int&j);
437 
440  void reverse();
441 
442 
445  inline void begin() {
446  mIterator=0;
447  };
448 
451  inline tBoolean hasNext() const {
452  return (mIterator<mVector.size());
453  };
456  inline T& next() {
457  T& ret=mVector[mIterator];
458  mIterator++;
459  return ret;
460  };
461 
462 
463  // OPERATIONS
466  template<class Q>
467  inline tReal distance2(const CORE_Vector<Q>& a) const {
468  int n=a.getSize();
469  int m=getSize();
470  int p=(n<m)?n:m;
471  tReal d=0;
472  tReal t=0;
473  for (int i=0;i<p;i++) {
474  t=((T)a[i])-mVector[i];
475  d+=t*t;
476  }
477  return d;
478  };
481  template<class Q>
482  tReal distance(const CORE_Vector<Q>& a) const {
483  return sqrt(distance2(a));
484  };
487  template<class Q>
489  int n=a.getSize();
490  int m=getSize();
491  int p=(n<m)?n:m;
492  tReal s=0;
493  for (int i=0;i<p;i++)
494  s+=((T)a[i])*mVector[i];
495  return s;
496  };
499  template<class Q>
501  if (a!=null) return scalarProduct(*a);
502  else return 0;
503  };
506  template<class Q>
507  inline static void crossProduct(const CORE_Vector<Q>& a,
508  const CORE_Vector<Q>& b,
509  CORE_Vector<Q>& res) {
510  int dim_a=a.getSize();
511  int dim_b=b.getSize();
512  int dim=(dim_a<dim_b)?dim_a:dim_b;
513  if (dim==3) {
514  res.setSize(3);
515  res[0]=a[1]*b[2]-a[2]*b[1];
516  res[1]=a[2]*b[0]-a[0]*b[2];
517  res[2]=a[0]*b[1]-a[1]*b[0];
518  } else{
519  res.setSize(dim);
520  for (int i=0;i<dim;i++) res[i]=0;
521  }
522  };
525  template<class Q>
526  inline void crossProduct(const CORE_Vector<Q>& a,
527  CORE_Vector<Q>& res) const {
528  crossProduct(*this,a,res);
529  };
530 
533  inline void rotX(const tReal& alpha) {
534  if (getSize()<3) setSize(3);
535  tReal v1= mVector[1]*cos(alpha) + mVector[2]*sin(alpha);
536  mVector[2] = - mVector[1]*sin(alpha) + mVector[2]*cos(alpha);
537  mVector[1] =v1;
538  };
539 
542  inline void rotZ(const tReal& gamma) {
543  if (getSize()<2) setSize(2);
544  tReal v0 = mVector[0]*cos(gamma) + mVector[1]*sin(gamma) ;
545  mVector[1] =-mVector[0]*sin(gamma) + mVector[1]*cos(gamma) ;
546  mVector[0]=v0;
547  }
548 
551  inline tReal norm2() const {
552  int p=getSize();
553  tReal d=0;
554  for (int i=0;i<p;i++) {
555  d+=mVector[i]*mVector[i];
556  }
557  return d;
558  };
559 
562  tReal norm() const {
563  return sqrt(norm2());
564  };
565 
566  virtual tString toString() const {
567  int n=getSize();
568  tString ret=getIdentityString()+"(";
569  for (int i=0;i<n;i++)
570  ret+=CORE_String::toString(mVector[i])+",";
571  ret+=")";
572  return ret;
573  }
574 
575 
576 
577 };
578 
579 #include "CORE_Vector.hpp"
580 
591 
592 //share pointer of primitive type vector
603 
604 
605 #endif
void begin()
put the iterator on begin
Definition: CORE_Vector.h:445
CORE_Vector< tBoolean > CORE_BooleanVector
Definition: CORE_Vector.h:581
void crossProduct(const CORE_Vector< Q > &a, CORE_Vector< Q > &res) const
return the cross product of the vector with the a vector
Definition: CORE_Vector.h:526
void sort(const tString &order)
sort the vector decreasing order < increasing order
Definition: CORE_Vector.h:323
const vector< T > & getVector() const
get vector
Definition: CORE_Vector.h:416
tString toString() const
return the string associated to the string
Definition: CORE_String.h:150
virtual tString toString() const
return the string representation of the object node
Definition: CORE_Vector.h:566
const T & operator[](int i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_Vector.h:135
static boost::shared_ptr< CORE_Vector< T > > New(const boost::shared_ptr< CORE_Vector< T > > &v)
create a shared pointer of vector which is a copy of vector v
Definition: CORE_Vector.h:58
this class describes an array
Definition: CORE_Vector.h:18
class CORE_SharedPointersList is a list of shared pointers
Definition: CORE_SharedPointersList.h:11
void add(const T &obj)
add an element at the end
Definition: CORE_Vector.h:305
CORE_Vector< tComplex > CORE_ComplexVector
Definition: CORE_Vector.h:585
void multiplyBy(const tReal &v)
multiplied by value v
Definition: CORE_Vector.h:185
this class describes a list
Definition: CORE_List.h:12
T & next()
return the next element
Definition: CORE_Vector.h:456
void add(const tReal &f, const CORE_Vector< T > &v)
init the value to v
Definition: CORE_Vector.h:217
tReal norm() const
return the norm of the vector
Definition: CORE_Vector.h:562
void copy(const CORE_Vector< Y > *array)
copy
Definition: CORE_Vector.h:254
tBoolean hasNext() const
return true if the array has next element
Definition: CORE_Vector.h:451
tReal scalarProduct(const CORE_Vector< Q > &a) const
return the scalar product of the vector with the a vector
Definition: CORE_Vector.h:488
#define tBoolean
Definition: types.h:35
const vector< T > & getValues() const
get values
Definition: CORE_Vector.h:399
vector< T > & getValues()
get values
Definition: CORE_Vector.h:402
CORE_Vector< tFlag > CORE_FlagVector
Definition: CORE_Vector.h:586
void add(const CORE_Vector< T > &v)
init the value to v
Definition: CORE_Vector.h:206
CORE_Vector< tInteger > CORE_IntegerVector
Definition: CORE_Vector.h:589
tReal norm2() const
return the squared norm of the vector
Definition: CORE_Vector.h:551
T & operator[](int i)
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_Vector.h:145
int insert(const T &obj)
insert T
Definition: CORE_Vector.h:298
#define null
Definition: types.h:13
void getSharedPointer(boost::shared_ptr< CORE_Vector< T > > &p)
get the shared pointer into P
Definition: CORE_Vector.h:102
CORE_Vector< tCharacter > CORE_CharacterVector
Definition: CORE_Vector.h:582
CORE_Vector< int > CORE_IntVector
Definition: CORE_Vector.h:583
int getDimension() const
return the dimension of the vector
Definition: CORE_Vector.h:390
CORE_Vector< tRelativeInteger > CORE_RelativeIntegerVector
Definition: CORE_Vector.h:590
void rotZ(const tReal &gamma)
rotate the vector among z-axes with angle gamma
Definition: CORE_Vector.h:542
const T & getValue(const int &i) const
get value
Definition: CORE_Vector.h:395
void rotX(const tReal &alpha)
rotate the vector among x-axes with angle alpha
Definition: CORE_Vector.h:533
void sub(const CORE_Vector< T > &v)
init the value to v
Definition: CORE_Vector.h:226
T & getLastElement()
get last element pointer
Definition: CORE_Vector.h:358
CORE_Vector< tReal > CORE_RealVector
Definition: CORE_Vector.h:584
void sub(const tReal &f, const CORE_Vector< T > &v)
init the value to v
Definition: CORE_Vector.h:239
void divideBy(const tReal &v)
divide by value v
Definition: CORE_Vector.h:179
void initValues(const T &v)
init the value to v
Definition: CORE_Vector.h:173
void copy(const boost::shared_ptr< CORE_Vector< T > > &array)
copy
Definition: CORE_Vector.h:260
void getSharedPointer(SP::CORE_Object &p)
get the shared pointer of this class into p
Definition: CORE_Object.h:65
int size() const
return the size of the vector
Definition: CORE_Vector.h:384
void getSharedPointer(boost::shared_ptr< const CORE_Vector< T > > &p) const
get the shared pointer into P
Definition: CORE_Vector.h:109
void setValue(const int &i, const T &v)
set value
Definition: CORE_Vector.h:156
int getSize() const
return the size of the vector
Definition: CORE_Vector.h:387
void setCapacity(const int &n)
set the size of the array
Definition: CORE_Vector.h:167
static void crossProduct(const CORE_Vector< Q > &a, const CORE_Vector< Q > &b, CORE_Vector< Q > &res)
return the cross product of the vector with the a vector
Definition: CORE_Vector.h:507
#define tString
Definition: types.h:36
CORE_Vector< tString > CORE_StringVector
Definition: CORE_Vector.h:588
static boost::shared_ptr< CORE_Vector< T > > New()
create a shared pointer of vector
Definition: CORE_Vector.h:84
static boost::shared_ptr< CORE_Vector< T > > New(const CORE_Vector< T > &v)
create a shared pointer of vector which is a copy of vector v
Definition: CORE_Vector.h:67
CORE_Vector< tShort > CORE_ShortVector
Definition: CORE_Vector.h:587
static boost::shared_ptr< CORE_Vector< T > > New(const int &dim)
create a shared pointer of vector of size dim
Definition: CORE_Vector.h:92
tReal scalarProduct(const CORE_Vector< Q > *a) const
return the scalar product of the vector with the a vector
Definition: CORE_Vector.h:500
tReal distance(const CORE_Vector< Q > &a) const
return the distance of the vector with the a vector
Definition: CORE_Vector.h:482
void setSize(const int &n)
set the size of the array
Definition: CORE_Vector.h:162
virtual void addInList(const T &obj)
add a core object
Definition: CORE_Vector.h:311
vector< T > & getVector()
get vector
Definition: CORE_Vector.h:411
void sub(const CORE_Vector< T > &u, const CORE_Vector< T > &v)
init the value to u-v
Definition: CORE_Vector.h:231
void setValues(const Y *array, const int &n)
copy
Definition: CORE_Vector.h:272
static boost::shared_ptr< CORE_Vector< T > > New(const CORE_Vector< T > *v)
create a shared pointer of vector which is a copy of vector v
Definition: CORE_Vector.h:76
int search(const T &value, const tString &order)
search the value in values vector ordered in order
Definition: CORE_Vector.h:427
TYPEDEF_SPTR(CORE_BooleanVector)
void clear()
clear the array
Definition: CORE_Vector.h:350
#define tReal
Definition: types.h:18
tReal distance2(const CORE_Vector< Q > &a) const
return the squared distance of the vector with the a vector
Definition: CORE_Vector.h:467
#define ASSERT_IN(a)
Definition: types.h:82
void copy(const CORE_Vector< Y > &array)
copy
Definition: CORE_Vector.h:247
void normalize()
normalize
Definition: CORE_Vector.h:211
void setValues(const vector< Y > &array)
copy
Definition: CORE_Vector.h:265
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106