C++ main module for mmsd 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  tArrayIndex mSize;
26  // memory size of the array
27  tArrayIndex mCapacity;
28  // capacity factor: the memory size of the array is the (mStartIndex + mSize) * mCapacityFactor by default
29  tFlag mCapacityFactor;
30 
31  // start index of the array (0 by default)
32  tArrayIndex 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 tArrayIndex& 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 tArrayIndex& 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 tArrayIndex& i) const {
122  ASSERT_IN(i<mSize);
123  return mCVector[i];
124  };
125 
130  inline T& operator[](const tArrayIndex& i) {
131  ASSERT_IN(i<mSize);
132  return mCVector[i];
133  };
134  inline T& operator()(const tArrayIndex& i) {
135  ASSERT_IN(i<mSize);
136  return mCVector[i];
137  };
138  inline const T& operator()(const tArrayIndex& i) const {
139  ASSERT_IN(i<mSize);
140  return mCVector[i];
141  };
142 
145  inline T& operator=(const CORE_Array<T>& f) {
146  for (tArrayIndex i=0;i<mSize;i++) mCVector[i]=f[i];
147  return mCVector[0];
148  };
149 
152  inline T& operator/=(const T& f) {
153  for (tArrayIndex i=0;i<mSize;i++) mCVector[i]/=f;
154  return mCVector[0];
155  };
158  inline T& operator*=(const T& f) {
159  for (tArrayIndex i=0;i<mSize;i++) mCVector[i]*=f;
160  return mCVector[0];
161  };
164  inline T& operator+=(const T& f) {
165  for (int i=0;i<mSize;i++) mCVector[i]+=f;
166  return mCVector[0];
167  };
170  inline T& operator-=(const T& f) {
171  for (tArrayIndex i=0;i<mSize;i++) mCVector[i]-=f;
172  return mCVector[0];
173  };
174 
179  inline const T& get(const tArrayIndex& i) const {
180 
181  ASSERT_IN(i<mSize);
182  return mCVector[i];
183  };
188  inline T& get(const tArrayIndex& i) {
189 
190  ASSERT_IN(i<mSize);
191  return mCVector[i];
192  };
197  inline const T& getValue(const tArrayIndex& i) const {
198  ASSERT_IN(i<mSize);
199  return mCVector[i];
200  };
201 
205  inline const T& getLastElement() {
206  if (mSize==0) throw CORE_Exception("common/core","CORE_Array::getLastElement()","no element in array");
207  tArrayIndex n=mSize;
208  return mCVector[n-1];
209  };
210 
213  inline const tFlag& getCapacityFactor() const {return mCapacityFactor;};
214 
217  inline const tArrayIndex& getStartIndex() const {return mStartIndex;};
218 
220  inline const tArrayIndex& size() const {return mSize;};
221 
223  inline const tArrayIndex& getSize() const {return mSize;};
224 
226  inline const tArrayIndex& getCapacity() const {return mCapacity;};
227 
228  // -------------
229  // copy methods
230  // -------------
231 
234  void copy(const CORE_Array<T>& src);
237  inline void copy(const CORE_Array<T>*src){
238  if (src!=null) copy(*src);
239  };
240 
241  // -----------
242  // SET Methods
243  // ------------
244 public:
252  void setStartIndex(const tArrayIndex& n) {
253  if (n>mCapacity) throw CORE_Exception("common/core",
254  "CORE_Array::setStartIndex()",
255  "start index out of bounds");
256 
257  long long int d=n-mStartIndex;
258  mStartIndex=n;
259  mSize-=d;
260  mCVector=&mVector[mStartIndex];
261  };
264  void setSize(const tArrayIndex& n);
267  inline void setCapacityFactor(const tFlag& n) {
268  mCapacityFactor=n;
269  };
270 
273  void setCapacity(const tArrayIndex& c);
274 
278  void setValues(const CORE_Array<T>& array,const tArrayIndex& fromIndex);
281  inline void setValues(const CORE_Array<T>& array) {
282  setValues(array,0);
283  };
284 
287  inline void setValues(const CORE_Array<T>* array) {
288  if (array!=null) setValues(*array);
289  else setSize(0);
290  }
294  void setValues(const tArrayIndex& n,const T* array,const tArrayIndex& fromIndex);
295 
298  inline void setValues(const tArrayIndex& n,const T* array) {
299  setValues(n,array,0);
300  }
301 
305  inline void setValuesByReference(const tArrayIndex& n,T*& array,const tBoolean& hasToBeDeleted) {
306  if ((mVector!=null) && (mHasToBeDeleted)) delete[] mVector;
307  mVector=array;
308  mCVector=mVector;
309  mCapacity=n;
310  mSize=n;
311  mStartIndex=0;
312  mHasToBeDeleted=hasToBeDeleted;
313  }
314 
315 
319  void setValues(const vector<T>& array,const tArrayIndex& fromIndex);
320 
323  inline void setValues(const vector<T>& array) {
324  setValues(array,0);
325  }
330  void setValues(const tString& str,const tArrayIndex& fromIndex);
334  void setValues(const tString& str) {
335  setValues(str,0);
336  }
337 
340  inline void set(const tArrayIndex& i,const T& obj) {
341  ASSERT_IN(i<mSize);
342  mCVector[i]=obj;
343  };
346  inline void setValue(const tArrayIndex& i,const T& obj){
347  set(i,obj);
348  };
351  inline void initValues(const T& v) {
352  for (tArrayIndex i=0;i<mSize;i++)
353  mCVector[i]=v;
354  };
355 
359  void resize();
362  void resize(const tArrayIndex & n);
363 
366  void contractToLastElements(const tArrayIndex& n);
367 
368 public:
371  inline const T* getValues(tArrayIndex& s) const {
372  s=mSize;
373  return mCVector;
374  };
377  inline const T* getValues() const {
378  tArrayIndex s=0;
379  return getValues(s);
380  };
383  inline T* getValues() {
384  tArrayIndex s=0;
385  return getValues(s);
386  };
389  inline T* getValues(tArrayIndex& s) {
390  s=mSize;
391  return mCVector;
392  };
393 
396  inline const T* getCompleteValues(tArrayIndex& s) const {
397  s=mCapacity;
398  return mVector;
399  };
400 
404  s=mCapacity;
405  return mVector;
406  };
407 
408 
409 
410 public:
411  // -----------------
412  // ADDING Methods
413  // -----------------
414 
422  void addAfterIndex(const tArrayIndex& index,const T& v);
423 
431  void addAfterIndices(const CORE_Array<tArrayIndex>& indices,
433 
434 
438  tArrayIndex insert(const tArrayIndex& i,const T& obj);
439 
444  tArrayIndex insertInOrder(const T& obj,const tBoolean& evenIfEqual);
449  inline tArrayIndex 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 
483 
486  inline tBoolean remove() {
487  tArrayIndex n=size();
488  if (n==0) return false;
489  return removeAtIndex(n-1);
490  }
493  virtual void clear(){
494  setSize(0);
495  };
496 
497  // -----------------
498  // ITERATOR methods
499  // ----------------
500 
504  tBoolean getIndex(const T& v,tArrayIndex& index) const;
508  tBoolean getInfIndex(const T& v,tArrayIndex& index) const;
512  tBoolean getSupIndex(const T& v,tArrayIndex& index) const;
513 
514 
517  inline tBoolean exists(const T& obj) const {
518  for (tArrayIndex i=0;i<mSize;i++) {
519  if (mCVector[i]==obj) {
520  return true;
521  }
522  }
523  return false;
524  }
525 
526  // other METHODS
527 
530  void reverse();
531 
532 
535  static tBoolean search(const T* values, const tArrayIndex& n,
536  const T& value,const tString& order,tArrayIndex& index);
537 
540  int search(const T& value,const tString& order) {
541  return search(mCVector,mSize,value,order);
542  }
543 
546  inline void sort() {
547  sort(mCVector,mSize,">");
548  };
551  inline void sort(const tString& order) {
552  sort(mCVector,mSize,order);
553  };
556  static void sort(T*& items,const tArrayIndex& N,
557  const tString& order);
558  // -------------
559  // PRINT Methods
560  // -------------
561 
564  tString toString() const;
565 
566 
567  // ------------------------
568  // Mathematiocal operation
569  // ------------------------
570 
573  inline T norm2() const {
574  T s=(T) 0;
575  T aux;
576  for (tArrayIndex i=0;i<mSize;i++) {
577  aux=mCVector[i];
578  s+=aux*aux;
579  }
580  return s;
581  }
584  inline T norm() const{
585  return sqrt(norm2());
586  };
589  inline T distance2(const CORE_Array<T>& y) const {
590  T s=(T) 0;
591  T d;
592  tArrayIndex n=mSize;
593  for (tArrayIndex i=0;i<n;i++) {
594  d=mCVector[i]-y[i];
595  s+=d*d;
596  }
597  return s;
598  };
601  inline T distance(const CORE_Array<T>& y) const{
602  return sqrt(distance2(y));
603  };
604 
605 
606 };
607 
608 #include "CORE_Array.hpp"
609 
623 
624 
637 
649 #endif
TYPEDEF_SPTR(CORE_DoubleArray)
const T & getLastElement()
get last element Assert in (mSize>0)
Definition: CORE_Array.h:205
const T * getValues() const
get the values of the util vector
Definition: CORE_Array.h:377
const T * getCompleteValues(tArrayIndex &s) const
get the values of the complete vector
Definition: CORE_Array.h:396
void setCapacity(const tArrayIndex &c)
set the capacity of the vector
Definition: CORE_Array.hpp:84
T * getValues(tArrayIndex &s)
get the values of the util vector
Definition: CORE_Array.h:389
virtual void clear()
clear the array
Definition: CORE_Array.h:493
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 set(const tArrayIndex &i, const T &obj)
set the object at the index i
Definition: CORE_Array.h:340
static boost::shared_ptr< CORE_Array< T > > New()
return a CORE_Array shared pointer
Definition: CORE_Array.h:98
T & operator()(const tArrayIndex &i)
Definition: CORE_Array.h:134
#define tArrayIndex
Definition: types.h:39
tBoolean getSupIndex(const T &v, tArrayIndex &index) const
get the sup index of value return false if no value less than v
Definition: CORE_Array.hpp:513
void setCapacityFactor(const tFlag &n)
set the capacity factor
Definition: CORE_Array.h:267
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:540
T distance2(const CORE_Array< T > &y) const
return distance squared
Definition: CORE_Array.h:589
void addAfterIndex(const tArrayIndex &index, const T &v)
add an element after index
Definition: CORE_Array.hpp:665
T * getValues()
get the values of the util vector
Definition: CORE_Array.h:383
void setValue(const tArrayIndex &i, const T &obj)
set the object at the index i
Definition: CORE_Array.h:346
void setValues(const CORE_Array< T > *array)
copy a CORE_Array and convert it
Definition: CORE_Array.h:287
static boost::shared_ptr< CORE_Array< T > > New(const tArrayIndex &n)
return a CORE_Array shared pointer
Definition: CORE_Array.h:106
this class describes a list
Definition: CORE_List.h:12
void setValues(const CORE_Array< T > &array)
Definition: CORE_Array.h:281
T distance(const CORE_Array< T > &y) const
get the distance
Definition: CORE_Array.h:601
#define tBoolean
Definition: types.h:48
void setValuesByReference(const tArrayIndex &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:305
CORE_Array< tString > CORE_StringArray
Definition: CORE_Array.h:619
tBoolean exists(const T &obj) const
exists
Definition: CORE_Array.h:517
CORE_Array< double > CORE_DoubleArray
Definition: CORE_Array.h:610
T & operator+=(const T &f)
operator +=
Definition: CORE_Array.h:164
#define null
Definition: types.h:13
CORE_Array< tCharacter > CORE_CharacterArray
Definition: CORE_Array.h:611
T & operator-=(const T &f)
operator -=
Definition: CORE_Array.h:170
CORE_Array< tDoubleComplex > CORE_DoubleComplexArray
Definition: CORE_Array.h:616
void reverse()
reverse the vector
Definition: CORE_Array.hpp:589
void contractToLastElements(const tArrayIndex &n)
keep only the last n elements of the array and set its capacity also to n
Definition: CORE_Array.hpp:304
void append(const CORE_Array< T > &array)
merge the array in this
Definition: CORE_Array.h:465
tArrayIndex insertInOrder(const T &obj, const tBoolean &evenIfEqual)
insert the object in increasing order
Definition: CORE_Array.hpp:353
tBoolean removeAtIndex(const tArrayIndex &i)
Definition: CORE_Array.hpp:291
void add(const T &obj)
add an element at the end re-allocate the array if capacity too small
Definition: CORE_Array.hpp:165
CORE_Array< tFlag > CORE_FlagArray
Definition: CORE_Array.h:617
T & operator=(const CORE_Array< T > &f)
operator =
Definition: CORE_Array.h:145
T norm2() const
get the norm
Definition: CORE_Array.h:573
T & operator[](const tArrayIndex &i)
get the i-th element Assert in (i>-1) Assert in (i
Definition: CORE_Array.h:130
void copy(const CORE_Array< T > *src)
void copy
Definition: CORE_Array.h:237
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:15
static tBoolean search(const T *values, const tArrayIndex &n, const T &value, const tString &order, tArrayIndex &index)
search the value in values array ordered in order
Definition: CORE_Array.hpp:780
T norm() const
get the norm squared
Definition: CORE_Array.h:584
CORE_Array< tReal > CORE_RealArray
Definition: CORE_Array.h:614
CORE_Array< tBoolean > CORE_BooleanArray
Definition: CORE_Array.h:613
T * getCompleteValues(tArrayIndex &s)
get the values of the complete vector
Definition: CORE_Array.h:403
virtual ~CORE_Array()
destroy an array of T*
Definition: CORE_Array.hpp:65
this class describes an array
Definition: CORE_Array.h:18
tArrayIndex insert(const tArrayIndex &i, const T &obj)
insert the object at index i re-allocate the array if capacity too small
Definition: CORE_Array.hpp:324
CORE_Array< tRelativeInteger > CORE_RelativeIntegerArray
Definition: CORE_Array.h:621
const T & operator()(const tArrayIndex &i) const
Definition: CORE_Array.h:138
tBoolean getInfIndex(const T &v, tArrayIndex &index) const
get the inf index of value return false if no value less than v
Definition: CORE_Array.hpp:441
void setSize(const tArrayIndex &n)
set the size of the array
Definition: CORE_Array.hpp:149
void getSharedPointer(SP::CORE_Object &p)
get the shared pointer of this class into p
Definition: CORE_Object.h:65
void sort()
sort the array in an increasing order
Definition: CORE_Array.h:546
tArrayIndex insert(const T &obj)
insert the object in increasing order
Definition: CORE_Array.h:449
T & operator*=(const T &f)
operator *=
Definition: CORE_Array.h:158
void copy(const CORE_Array< T > &src)
void copy
Definition: CORE_Array.hpp:72
CORE_Array()
build an array of T*
Definition: CORE_Array.hpp:32
CORE_Array< tInteger > CORE_IntegerArray
Definition: CORE_Array.h:620
tString toString() const
turn the array into string
Definition: CORE_Array.hpp:651
void setValues(const CORE_Array< T > &array, const tArrayIndex &fromIndex)
Definition: CORE_Array.hpp:214
CORE_Array< int > CORE_IntArray
Definition: CORE_Array.h:612
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
const T * getValues(tArrayIndex &s) const
get the values of the util vector
Definition: CORE_Array.h:371
const T & operator[](const tArrayIndex &i) const
get the i-th element Assert in (i>-1) Assert in (i
Definition: CORE_Array.h:121
#define tString
Definition: types.h:49
T & operator/=(const T &f)
operator /=
Definition: CORE_Array.h:152
void sort(const tString &order)
sort the array
Definition: CORE_Array.h:551
void merge(const CORE_Array< T > &array)
merge the array in this
Definition: CORE_Array.hpp:604
void setValues(const tArrayIndex &n, const T *array)
copy an array of T
Definition: CORE_Array.h:298
CORE_Array< tShort > CORE_ShortArray
Definition: CORE_Array.h:618
const tArrayIndex & getStartIndex() const
get start index
Definition: CORE_Array.h:217
const tFlag & getCapacityFactor() const
get the capacity factor
Definition: CORE_Array.h:213
void resize()
resize the array to the util size
Definition: CORE_Array.hpp:115
const tArrayIndex & getSize() const
return the size of the array
Definition: CORE_Array.h:223
CORE_Array< tComplex > CORE_ComplexArray
Definition: CORE_Array.h:615
void setValues(const tString &str)
Definition: CORE_Array.h:334
const tArrayIndex & getCapacity() const
return the memory allocation size of the array
Definition: CORE_Array.h:226
void addAfterIndices(const CORE_Array< tArrayIndex > &indices, CORE_SharedPointersList< CORE_Array< T > > &values)
add alements after indices
Definition: CORE_Array.hpp:675
TYPEDEF_SVPTR(CORE_BooleanArray)
void setStartIndex(const tArrayIndex &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:252
tBoolean getIndex(const T &v, tArrayIndex &index) const
get the index of value return false if no value in index
Definition: CORE_Array.hpp:404
CORE_Array< tLong > CORE_LongArray
Definition: CORE_Array.h:622
const tArrayIndex & size() const
return the size of the array
Definition: CORE_Array.h:220
void setValues(const vector< T > &array)
Definition: CORE_Array.h:323
#define ASSERT_IN(a)
Definition: types.h:96
const T & getValue(const tArrayIndex &i) const
get the value at index i Assert in (i>-1) Assert in (i
Definition: CORE_Array.h:197
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14