C++ main module for gpm Package  1.0
CORE_Array.h
Go to the documentation of this file.
1 #ifndef CORE_Array_H
2 #define CORE_Array_H
3 
4 
5 #include "CORE_Object.h"
6 #include "CORE_List.h"
7 #include "CORE_ListPointers.h"
8 
9 #include "types.h"
10 #include <vector>
11 
12 
17 template <class T>
18 class CORE_Array : public CORE_List {
19 private:
20  // values of the array
21  T* mVector;
22  // current vector
23  T* mCVector;
24  // size of the array
25  int mSize;
26  // memory size of the array
27  int mCapacity;
28  // capacity factor: the memory size of the array is the (mStartIndex + mSize) * mCapacityFactor by default
29  int mCapacityFactor;
30 
31  // start index of the array (0 by default)
32  int mStartIndex;
33 
34  tBoolean mHasToBeDeleted;
35 
36  // CONSTRUCTORS
37 public:
40  CORE_Array();
43  template<class Q>
44  CORE_Array(const std::vector<Q>& values);
45 
48  CORE_Array(const int& n);
49 
50 
51  // DESTRUCTORS
54  virtual ~CORE_Array();
55 
56 
57 
58  // -----------------------
59  // shared pointer operator
60  // ------------------------
61 public:
64  void getSharedPointer(boost::shared_ptr<CORE_Array<T> >& p){
65  SP::CORE_Object r;
67  p=boost::dynamic_pointer_cast<CORE_Array<T> >(r);
68  };
71  void getSharedPointer( boost::shared_ptr<const CORE_Array<T> >& p) const{
72  SPC::CORE_Object r;
74  p=boost::dynamic_pointer_cast<const CORE_Array<T> >(r);
75  };
76 private:
79  inline boost::shared_ptr<CORE_Array<T> > getThis() {
80  boost::shared_ptr<CORE_Array<T> > p;
82  return p;
83  };
86  inline boost::shared_ptr<const CORE_Array<T> > getThis() const {
87  boost::shared_ptr<const CORE_Array<T> > p;
89  return p;
90  };
91 
92  // ----------------
93  // New constructors
94  // ----------------
95 public:
98  static inline boost::shared_ptr<CORE_Array<T> > New() {
99  boost::shared_ptr<CORE_Array<T> > p(new CORE_Array<T>(),
101  p->setThis(p);
102  return p;
103  };
106  static inline boost::shared_ptr<CORE_Array<T> > New(const int& n) {
107  boost::shared_ptr<CORE_Array<T> > p(new CORE_Array<T>(n),
109  p->setThis(p);
110  return p;
111  };
112 
113  // ---------------------------
114  // Accessor & basic operators
115  // ---------------------------
116 public:
121  inline const T& operator[](const int& i) const {
122  ASSERT_IN(i>-1);
123  ASSERT_IN(i<mSize);
124  return mCVector[i];
125  };
126 
131  inline T& operator[](const int& i) {
132  ASSERT_IN(i>-1);
133  ASSERT_IN(i<mSize);
134  return mCVector[i];
135  };
136  inline T& operator()(const size_t& i) {
137  ASSERT_IN(i<(size_t)mSize);
138  return mCVector[i];
139  };
140  inline const T& operator()(const size_t& i) const {
141  ASSERT_IN(i<(size_t)mSize);
142  return mCVector[i];
143  };
144 
147  inline T& operator=(const CORE_Array<T>& f) {
148  for (int i=0;i<mSize;i++) mCVector[i]=f[i];
149  return mCVector[0];
150  };
151 
154  inline T& operator/=(const T& f) {
155  for (int i=0;i<mSize;i++) mCVector[i]/=f;
156  return mCVector[0];
157  };
160  inline T& operator*=(const T& f) {
161  for (int i=0;i<mSize;i++) mCVector[i]*=f;
162  return mCVector[0];
163  };
166  inline T& operator+=(const T& f) {
167  for (int i=0;i<mSize;i++) mCVector[i]+=f;
168  return mCVector[0];
169  };
172  inline T& operator-=(const T& f) {
173  for (int i=0;i<mSize;i++) mCVector[i]-=f;
174  return mCVector[0];
175  };
176 
181  inline const T& get(const int& i) const {
182  ASSERT_IN(i>-1);
183  ASSERT_IN(i<mSize);
184  return mCVector[i];
185  };
190  inline T& get(const int& i) {
191  ASSERT_IN(i>-1);
192  ASSERT_IN(i<mSize);
193  return mCVector[i];
194  };
199  inline const T& getValue(const int& i) const {
200  ASSERT_IN(i>-1);
201  ASSERT_IN(i<mSize);
202  return mCVector[i];
203  };
204 
208  inline const T& getLastElement() {
209  ASSERT_IN(mSize>0);
210  int n=mSize;
211  return mCVector[n-1];
212  };
213 
216  inline int getCapacityFactor() const {return mCapacityFactor;};
217 
220  inline int getStartIndex() const {return mStartIndex;};
221 
223  inline int size() const {return mSize;};
224 
226  inline int getSize() const {return mSize;};
227 
229  inline int getCapacity() const {return mCapacity;};
230 
231  // -------------
232  // copy methods
233  // -------------
234 
237  void copy(const CORE_Array<T>& src);
240  inline void copy(const CORE_Array<T>*src){
241  if (src!=null) copy(*src);
242  };
243 
244  // -----------
245  // SET Methods
246  // ------------
247 public:
255  void setStartIndex(const int& n) {
256  int d=n-mStartIndex;
257  mStartIndex=n;
258  mSize-=d;
259  mCVector=&mVector[mStartIndex];
260  };
263  void setSize(const int& n);
266  inline void setCapacityFactor(const int& n) {
267  mCapacityFactor=n;
268  };
269 
272  void setCapacity(const int& c);
273 
277  void setValues(const CORE_Array<T>& array,const int& fromIndex);
280  inline void setValues(const CORE_Array<T>& array) {
281  setValues(array,0);
282  };
283 
286  inline void setValues(const CORE_Array<T>* array) {
287  if (array!=null) setValues(*array);
288  else setSize(0);
289  }
293  void setValues(const int& n,const T* array,const int& fromIndex);
294 
297  inline void setValues(const int& n,const T* array) {
298  setValues(n,array,0);
299  }
300 
304  inline void setValuesByReference(const int& n,T*& array,const tBoolean& hasToBeDeleted) {
305  if ((mVector!=null) && (mHasToBeDeleted)) delete[] mVector;
306  mVector=array;
307  mCVector=mVector;
308  mCapacity=n;
309  mSize=n;
310  mStartIndex=0;
311  mHasToBeDeleted=hasToBeDeleted;
312  }
313 
314 
318  void setValues(const vector<T>& array,const int& fromIndex);
319 
322  inline void setValues(const vector<T>& array) {
323  setValues(array,0);
324  }
329  void setValues(const tString& str,const int& fromIndex);
333  void setValues(const tString& str) {
334  setValues(str,0);
335  }
336 
339  inline void set(const int& i,const T& obj) {
340  ASSERT_IN(i>-1);
341  ASSERT_IN(i<mSize);
342  mCVector[i]=obj;
343  };
346  inline void setValue(const int& i,const T& obj){
347  set(i,obj);
348  };
351  inline void initValues(const T& v) {
352  for (int i=0;i<mSize;i++)
353  mCVector[i]=v;
354  };
355 
359  void resize();
362  void resize(const int& n);
363 
366  void contractToLastElements(const int& n);
367 
368 public:
371  inline const T* getValues(int& s) const {
372  s=mSize;
373  return mCVector;
374  };
377  inline const T* getValues() const {
378  int s=0;
379  return getValues(s);
380  };
383  inline T* getValues() {
384  int s=0;
385  return getValues(s);
386  };
389  inline T* getValues(int& s) {
390  s=mSize;
391  return mCVector;
392  };
393 
396  inline const T* getCompleteValues(int& s) const {
397  s=mCapacity;
398  return mVector;
399  };
400 
403  inline T* getCompleteValues(int& s) {
404  s=mCapacity;
405  return mVector;
406  };
407 
408 
409 
410 public:
411  // -----------------
412  // ADDING Methods
413  // -----------------
414 
422  void addAfterIndex(const int& index,const T& v);
423 
431  void addAfterIndices(const CORE_Array<int>& indices,
433 
434 
438  int insert(const int& i,const T& obj);
439 
444  int insertInOrder(const T& obj,const tBoolean& evenIfEqual);
449  inline int insert(const T& obj){
450  return insertInOrder(obj,false);
451  };
452 
456  void add(const T& obj);
457 
458 
461  void merge(const CORE_Array<T>& array);
462 
465  inline void append(const CORE_Array<T>& array) {merge(array);};
466 
467  // ----------------
468  // REMOVING methods
469  // ----------------
470 
476  tBoolean remove(const T& obj);
477 
482  tBoolean removeAtIndex(const int& i);
483 
486  inline tBoolean remove() {return removeAtIndex(size()-1);}
489  virtual void clear(){setSize(0);};
490 
491  // -----------------
492  // ITERATOR methods
493  // ----------------
494 
498  int getIndex(const T& v) const;
502  int getInfIndex(const T& v) const;
506  int getSupIndex(const T& v) const;
507 
508 
511  inline tBoolean exists(const T& obj) const {
512  for (int i=0;i<mSize;i++) {
513  if (mCVector[i]==obj) {
514  return true;
515  }
516  }
517  return false;
518  }
519 
520  // other METHODS
521 
524  void reverse();
525 
526 
529  static int search(const T* values, const int& n,
530  const T& value,const tString& order);
531 
534  int search(const T& value,const tString& order) {
535  return search(mCVector,mSize,value,order);
536  }
537 
540  inline void sort() {
541  sort(mCVector,mSize,">");
542  };
545  inline void sort(const tString& order) {
546  sort(mCVector,mSize,order);
547  };
550  static void sort(T*& items,const int& N,
551  const tString& order);
552  // -------------
553  // PRINT Methods
554  // -------------
555 
558  tString toString() const;
559 
560 
561  // ------------------------
562  // Mathematiocal operation
563  // ------------------------
564 
567  inline T norm2() const {
568  T s=(T) 0;
569  T aux;
570  for (int i=0;i<mSize;i++) {
571  aux=mCVector[i];
572  s+=aux*aux;
573  }
574  return s;
575  }
578  inline T norm() const{
579  return sqrt(norm2());
580  };
583  inline T distance2(const CORE_Array<T>& y) const {
584  T s=(T) 0;
585  T d;
586  int n=mSize;
587  for (int i=0;i<n;i++) {
588  d=mCVector[i]-y[i];
589  s+=d*d;
590  }
591  return s;
592  };
595  inline T distance(const CORE_Array<T>& y) const{
596  return sqrt(distance2(y));
597  };
598 
599 
600 };
601 
602 #include "CORE_Array.hpp"
603 
617 
618 
631 
643 #endif
TYPEDEF_SPTR(CORE_DoubleArray)
const T & getLastElement()
get last element Assert in (mSize>0)
Definition: CORE_Array.h:208
const T * getValues() const
get the values of the util vector
Definition: CORE_Array.h:377
virtual void clear()
clear the array
Definition: CORE_Array.h:489
void getSharedPointer(boost::shared_ptr< CORE_Array< T > > &p)
return the shared pointer corresponding to the class with casting
Definition: CORE_Array.h:64
void addAfterIndex(const int &index, const T &v)
add an element after index
Definition: CORE_Array.hpp:558
static boost::shared_ptr< CORE_Array< T > > New()
return a CORE_Array shared pointer
Definition: CORE_Array.h:98
class CORE_SharedPointersList is a list of shared pointers
Definition: CORE_SharedPointersList.h:11
int search(const T &value, const tString &order)
search the value in values vector ordered in order
Definition: CORE_Array.h:534
T distance2(const CORE_Array< T > &y) const
return distance squared
Definition: CORE_Array.h:583
void setStartIndex(const int &n)
set the start index of the array adjust the size of the array such that the last element of the array...
Definition: CORE_Array.h:255
int getCapacity() const
return the memory allocation size of the array
Definition: CORE_Array.h:229
T & operator[](const int &i)
get the i-th element Assert in (i>-1) Assert in (i
Definition: CORE_Array.h:131
T * getValues()
get the values of the util vector
Definition: CORE_Array.h:383
void set(const int &i, const T &obj)
set the object at the index i
Definition: CORE_Array.h:339
void setValues(const CORE_Array< T > *array)
copy a CORE_Array and convert it
Definition: CORE_Array.h:286
this class describes a list
Definition: CORE_List.h:12
int getSupIndex(const T &v) const
get the sup index of value return -1 if no value less than v
Definition: CORE_Array.hpp:447
void setValues(const CORE_Array< T > &array)
Definition: CORE_Array.h:280
T distance(const CORE_Array< T > &y) const
get the distance
Definition: CORE_Array.h:595
const T & operator[](const int &i) const
get the i-th element Assert in (i>-1) Assert in (i
Definition: CORE_Array.h:121
T * getCompleteValues(int &s)
get the values of the complete vector
Definition: CORE_Array.h:403
int getCapacityFactor() const
get the capacity factor
Definition: CORE_Array.h:216
#define tBoolean
Definition: types.h:35
CORE_Array< tString > CORE_StringArray
Definition: CORE_Array.h:613
tBoolean exists(const T &obj) const
exists
Definition: CORE_Array.h:511
void setValues(const int &n, const T *array)
copy an array of T
Definition: CORE_Array.h:297
T * getValues(int &s)
get the values of the util vector
Definition: CORE_Array.h:389
int insert(const T &obj)
insert the object in increasing order
Definition: CORE_Array.h:449
CORE_Array< double > CORE_DoubleArray
Definition: CORE_Array.h:604
T & operator+=(const T &f)
operator +=
Definition: CORE_Array.h:166
#define null
Definition: types.h:13
const T * getCompleteValues(int &s) const
get the values of the complete vector
Definition: CORE_Array.h:396
CORE_Array< tCharacter > CORE_CharacterArray
Definition: CORE_Array.h:605
T & operator-=(const T &f)
operator -=
Definition: CORE_Array.h:172
CORE_Array< tDoubleComplex > CORE_DoubleComplexArray
Definition: CORE_Array.h:610
void reverse()
reverse the vector
Definition: CORE_Array.hpp:494
void append(const CORE_Array< T > &array)
merge the array in this
Definition: CORE_Array.h:465
void add(const T &obj)
add an element at the end re-allocate the array if capacity too small
Definition: CORE_Array.hpp:157
CORE_Array< tFlag > CORE_FlagArray
Definition: CORE_Array.h:611
static int search(const T *values, const int &n, const T &value, const tString &order)
search the value in values array ordered in order
Definition: CORE_Array.hpp:666
void setSize(const int &n)
set the size of the array
Definition: CORE_Array.hpp:141
T & operator=(const CORE_Array< T > &f)
operator =
Definition: CORE_Array.h:147
T norm2() const
get the norm
Definition: CORE_Array.h:567
void copy(const CORE_Array< T > *src)
void copy
Definition: CORE_Array.h:240
T norm() const
get the norm squared
Definition: CORE_Array.h:578
CORE_Array< tReal > CORE_RealArray
Definition: CORE_Array.h:608
CORE_Array< tBoolean > CORE_BooleanArray
Definition: CORE_Array.h:607
int size() const
return the size of the array
Definition: CORE_Array.h:223
virtual ~CORE_Array()
destroy an array of T*
Definition: CORE_Array.hpp:57
this class describes an array
Definition: CORE_Array.h:18
int insertInOrder(const T &obj, const tBoolean &evenIfEqual)
insert the object in increasing order
Definition: CORE_Array.hpp:337
CORE_Array< tRelativeInteger > CORE_RelativeIntegerArray
Definition: CORE_Array.h:615
int getStartIndex() const
get start index
Definition: CORE_Array.h:220
int getSize() const
return the size of the array
Definition: CORE_Array.h:226
void setValue(const int &i, const T &obj)
set the object at the index i
Definition: CORE_Array.h:346
const T * getValues(int &s) const
get the values of the util vector
Definition: CORE_Array.h:371
void getSharedPointer(SP::CORE_Object &p)
get the shared pointer of this class into p
Definition: CORE_Object.h:65
void setValuesByReference(const int &n, T *&array, const tBoolean &hasToBeDeleted)
set the the values by reference the values is destroyed at the end if hasToBeDeleted is true ...
Definition: CORE_Array.h:304
void sort()
sort the array in an increasing order
Definition: CORE_Array.h:540
T & operator*=(const T &f)
operator *=
Definition: CORE_Array.h:160
void copy(const CORE_Array< T > &src)
void copy
Definition: CORE_Array.hpp:64
CORE_Array()
build an array of T*
Definition: CORE_Array.hpp:29
const T & getValue(const int &i) const
get the value at index i Assert in (i>-1) Assert in (i
Definition: CORE_Array.h:199
CORE_Array< tInteger > CORE_IntegerArray
Definition: CORE_Array.h:614
int getInfIndex(const T &v) const
get the inf index of value return -1 if no value less than v
Definition: CORE_Array.hpp:404
tString toString() const
turn the array into string
Definition: CORE_Array.hpp:544
int insert(const int &i, const T &obj)
insert the object at index i re-allocate the array if capacity too small
Definition: CORE_Array.hpp:308
CORE_Array< int > CORE_IntArray
Definition: CORE_Array.h:606
void getSharedPointer(boost::shared_ptr< const CORE_Array< T > > &p) const
return the shared pointer corresponding to the class whith casting
Definition: CORE_Array.h:71
void initValues(const T &v)
init all values to v
Definition: CORE_Array.h:351
#define tString
Definition: types.h:36
T & operator/=(const T &f)
operator /=
Definition: CORE_Array.h:154
void sort(const tString &order)
sort the array
Definition: CORE_Array.h:545
void merge(const CORE_Array< T > &array)
merge the array in this
Definition: CORE_Array.hpp:507
void setCapacityFactor(const int &n)
set the capacity factor
Definition: CORE_Array.h:266
CORE_Array< tShort > CORE_ShortArray
Definition: CORE_Array.h:612
void resize()
resize the array to the util size
Definition: CORE_Array.hpp:107
T & operator()(const size_t &i)
Definition: CORE_Array.h:136
int getIndex(const T &v) const
get the index of value return -1 if no value in index
Definition: CORE_Array.hpp:380
CORE_Array< tComplex > CORE_ComplexArray
Definition: CORE_Array.h:609
void setValues(const tString &str)
Definition: CORE_Array.h:333
void setCapacity(const int &c)
set the capacity of the vector
Definition: CORE_Array.hpp:76
TYPEDEF_SVPTR(CORE_BooleanArray)
void setValues(const CORE_Array< T > &array, const int &fromIndex)
Definition: CORE_Array.hpp:201
CORE_Array< tLong > CORE_LongArray
Definition: CORE_Array.h:616
void setValues(const vector< T > &array)
Definition: CORE_Array.h:322
const T & operator()(const size_t &i) const
Definition: CORE_Array.h:140
#define ASSERT_IN(a)
Definition: types.h:82
void contractToLastElements(const int &n)
keep only the last n elements of the array and set its capacity also to n
Definition: CORE_Array.hpp:289
tBoolean removeAtIndex(const int &i)
Definition: CORE_Array.hpp:278
static boost::shared_ptr< CORE_Array< T > > New(const int &n)
return a CORE_Array shared pointer
Definition: CORE_Array.h:106
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
void addAfterIndices(const CORE_Array< int > &indices, CORE_SharedPointersList< CORE_Array< T > > &values)
add alements after indices
Definition: CORE_Array.hpp:568