C++ main module for gpm Package  1.0
CORE_Array2D.h
Go to the documentation of this file.
1 #ifndef CORE_Array2D_H
2 #define CORE_Array2D_H
3 
4 #include "CORE_List.h"
5 #include "CORE_Array.h"
6 #include "CORE_Vector.h"
7 
12 template < class T>
13 class CORE_Array2D : public CORE_List {
14 
15 private:
16 
17  tBoolean mHasToBeDeleted;
18 
19  // values
20  T *mVector;
21 
22  // mCapacity factor
23  int mCapacityFactor;
24 
25  // allocated size of mVector
26  int mCapacity;
27 
28  // number of lines
29  int mRowsNumber;
30 
31  // allocated size of mRowIndices
32  int mRowsCapacity;
33 
34  // the index of row i. The util size of the values is mRowIndices[mRowsNumber];
35  int *mRowIndices;
36 
37 
38  // CONSTRUCTORS
39 public:
42  CORE_Array2D();
43 
44 
45  // DESTRUCTORS
48  virtual ~CORE_Array2D();
49 
50 
51 
52 public:
53  // OPERATOR
54 
59  const T* operator[](int i) const {
60  ASSERT_IN(i>-1);
61  ASSERT_IN(i<mRowsNumber);
62  return &mVector[mRowIndices[i]];
63  };
64 
69  T* operator[](int i) {
70  ASSERT_IN(i>-1);
71  ASSERT_IN(i<mRowsNumber);
72  return &mVector[mRowIndices[i]];
73  };
74 
77  static inline boost::shared_ptr<CORE_Array2D<T> > New() {
78  boost::shared_ptr<CORE_Array2D<T> > p(new CORE_Array2D<T>(),CORE_Object::Delete());
79  return p;
80  };
81 
82  // set METHODS
83 
86  inline void setCapacityFactor(const int& i) {
87  mCapacityFactor=i;
88  };
89 
90 
93  void setCapacity(const int& n,const int& p);
94 
95 
98  void setSize(const int& n,const int& p);
99 
102  void resize();
103 
106  void copy(const CORE_Array2D<T>& src);
109  inline void copy(const CORE_Array2D<T>* src){
110  if (src!=null) copy(*src);
111  else clear();
112  };
113 
114 
117  inline void setValues(const int& nvs,const T* vs,const int& nrs,const int* rs) {
118  if (mHasToBeDeleted && (mVector!=null) ) delete[] mVector;
119 
120  // cpacity factor
121  mCapacityFactor=1;
122 
123  // capacity for values
124  mCapacity=nvs;
125  if (mCapacity==0) mCapacity=1;
126  mVector=new T[mCapacity];
127 
128  T * vectc=mVector;
129  const T* vsc=vs;
130  for (int i=0;i<nvs-1;i++) {
131  *vectc=*vsc;
132  vectc++;
133  vsc++;
134  }
135  *vectc=*vsc;
136 
137  // capacity of row indices
138  if (mHasToBeDeleted && (mRowIndices!=null)) delete[] mRowIndices;
139  mRowsCapacity=nrs;
140  if (nrs==0) mRowsCapacity=1;
141  mRowIndices=new int[mRowsCapacity];
142  int *inds=mRowIndices;
143  const int * rsc=rs;
144  for (int i=0;i<nrs-1;i++) {
145  *inds=*rsc;
146  inds++;
147  rsc++;
148  }
149  *inds=*rsc;
150 
151 
152  mRowsNumber=nrs-1;
153 
154  mHasToBeDeleted=true;
155 
156  }
157 
160  inline void setValues(const int& nvs,T* vs,const int& nrs,int* rs,const tBoolean& toBeDeleted) {
161 
162 
163  if (mHasToBeDeleted && (mVector!=null) ) delete[] mVector;
164  mVector=vs;
165 
166  if (mHasToBeDeleted && (mRowIndices!=null) ) delete[] mRowIndices;
167  mRowIndices=rs;
168 
169 
170  mCapacityFactor=1;
171  mCapacity=nvs;
172  mRowsNumber=nrs-1;
173  mRowsCapacity=nrs;
174 
175  mHasToBeDeleted=toBeDeleted;
176 
177  }
178 
181  inline void set(const int& i,const int& j,const T& obj) {
182  (*this)[i][j]=obj;
183  };
186  inline void addRow(const int& rowSize){
187  // get the actual size
188  int size=getValuesNumber();
189  // new size
190  int newSize=size+rowSize;
191  if (newSize>mCapacity) {
192  int n=newSize*mCapacityFactor;
193  // new allocation
194  T *newVector=new T[n];
195  // copy the values
196  for (int i=0;i<mCapacity;i++) {
197  newVector[i]=mVector[i];
198  }
199  for (int i=mCapacity;i<n;i++) {
200  newVector[i]=0;
201  }
202  if (mHasToBeDeleted && (mVector!=null) ) {
203  delete[] mVector;
204  }
205  mVector=newVector;
206  mCapacity=n;
207  mHasToBeDeleted=true;
208 
209  }
210  mRowsNumber++;
211  if (mRowsNumber+1>mRowsCapacity) {
212  mRowsCapacity=(mRowsNumber+1)*mCapacityFactor+1;
213  // new allocation
214  int *rows=new int[mRowsCapacity];
215  for (int i=0;i<mRowsNumber;i++) {
216  rows[i]=mRowIndices[i];
217  }
218  for (int i=mRowsNumber;i<mRowsCapacity;i++) {
219  rows[i]=newSize;
220  }
221  if (mHasToBeDeleted && (mRowIndices!=null) ) delete[] mRowIndices;
222  mRowIndices=rows;
223  mHasToBeDeleted=true;
224  }
225 
226 
227  mRowIndices[mRowsNumber]=newSize;
228 
229  };
230 
233  void setRowSize(const int& r,const int& s) {
234  ASSERT_IN(r<mRowsNumber);
235  int n=getValuesNumber();
236  int p=s-getRowSize(r);
237  int newValuesNumber=n+p;
238 
239 
240  // set the new row indices
241  for (int i=r+1;i<mRowsNumber;i++)
242  mRowIndices[i]+=p;
243 
244  if (newValuesNumber>mCapacity) {
245  // reallocate the values
246  int n=newValuesNumber*mCapacityFactor;
247  // new allocation
248  T *newVector=new T[n];
249  // copy the values
250  for (int i=0;i<mCapacity;i++) {
251  newVector[i]=mVector[i];
252  }
253  for (int i=mCapacity;i<n;i++) {
254  newVector[i]=0;
255  }
256  if (mHasToBeDeleted && (mVector!=null) ) delete[] mVector;
257  mVector=newVector;
258  mCapacity=n;
259  mHasToBeDeleted=true;
260  }
261 
262  // move the values if needed
263  if (mRowIndices[r+1]!=newValuesNumber) {
264  if (p>0) {
265  T *curN=&mVector[getValuesNumber()-1];
266  const T *curP=curN-p;
267  const T *curR=&mVector[mRowIndices[r+1]];
268  while (curN>=curR) {
269  *curN=*curP;
270  curN--;
271  curP--;
272  }
273  } else {
274  const T *curR=&mVector[getValuesNumber()-1];
275  T *curN=&mVector[mRowIndices[r+1]];
276  const T *curP=curN-p;//curN < curP
277 
278  while (curN<=curR) {
279  *curN=*curP;
280  curN++;
281  curP++;
282  }
283  }
284  }
285  };
288  inline void setRowValue(const int& i,const int& p,const T* vs) {
289  setRowSize(i,p);
290  T *v=mVector[mRowIndices[i]];
291  const T * vc=vs;
292  for (int j=0;j<p-1;j++) {
293  *v=*vc;
294  vc++;
295  v++;
296  }
297  *v=*vc;
298  };
301  inline void setRowValue(const int& i,const vector<T>& vs) {
302  int p=vs.size();
303  setRowSize(i,p);
304  T *v=mVector[mRowIndices[i]];
305  if (p>0) for (int j=0;j<p-1;j++) {*v=vs[j];v++;};*v=vs[p-1];
306 
307  };
310  template<class Q>
311  void setRowValue(const int& i,const CORE_Array<Q>& vs) {
312  int p=vs.getSize();
313  setRowSize(i,p);
314  T *v=mVector[mRowIndices[i]];
315  if (p>0) for (int j=0;j<p-1;j++) {*v=(T) vs[j];v++;};*v=vs[p-1];
316  };
319  template<class Q>
320  void setRowValue(const int& i,const CORE_Vector<Q>& vs) {
321  int p=vs.getSize();
322  setRowSize(i,p);
323  T *v=mVector[mRowIndices[i]];
324  if (p>0) {
325  for (int j=0;j<p-1;j++) {
326  *v=(T) vs[j];
327  v++;
328  }
329  *v=vs[p-1];
330  }
331  };
332 
333 
336  inline void add(const int& i,
337  const T& obj){
338 
339  int p=getRowSize(i);
340  setRowSize(i,p+1);
341  mVector[mRowIndices[i]+p]=obj;
342  };
343 
344 
345 
348  virtual void clear(){
349  setSize(0,0);
350  };
351 
352  // get METHODS
353 
355  inline int getValuesNumber() const {
356  return mRowIndices[mRowsNumber];
357  };
358 
360  inline int getRowSize(const int& i) const {
361  int *rowp=&mRowIndices[i+1];
362  int *row=rowp-1;
363  return (*rowp)-(*row);
364  };
367  inline const T* getRow(const int& i) const{
368  return (*this)[i];
369  };
372  inline T* getRow(const int& i) {
373  return (*this)[i];
374  };
377  inline const T* getValues() const {
378  return mVector;
379  };
382  inline T* getValues() {
383  return mVector;
384  };
385 
388  inline const int* getRowIndices() const {
389  return mRowIndices;
390  };
393  inline int getRowsNumber() const {
394  return mRowsNumber;
395  };
396 
399  inline int getCapacityFactor() const {
400  return mCapacityFactor;
401  };
404  inline int getCapacity() const {
405  return mCapacity;
406  };
407 
408 
409  // other METHODS
410 
411 
414  tString toString() const;
415 
416 
417 };
418 
419 #include "CORE_Array2D.hpp"
420 
432 
433 
445 
446 #endif
void addRow(const int &rowSize)
add a row
Definition: CORE_Array2D.h:186
static boost::shared_ptr< CORE_Array2D< T > > New()
New constructor. return a shared pointer of CORE_Array2D.
Definition: CORE_Array2D.h:77
int getCapacity() const
get capacity
Definition: CORE_Array2D.h:404
CORE_Array2D< tBoolean > CORE_BooleanArray2D
Definition: CORE_Array2D.h:424
void setRowValue(const int &i, const CORE_Vector< Q > &vs)
set row value
Definition: CORE_Array2D.h:320
T * getValues()
get the values
Definition: CORE_Array2D.h:382
this class describes an array
Definition: CORE_Vector.h:18
virtual ~CORE_Array2D()
destroy an array of T*
Definition: CORE_Array2D.hpp:28
void copy(const CORE_Array2D< T > &src)
void copy
Definition: CORE_Array2D.hpp:117
int getCapacityFactor() const
get capacity factor
Definition: CORE_Array2D.h:399
this class describes a list
Definition: CORE_List.h:12
void set(const int &i, const int &j, const T &obj)
set the object at the index i
Definition: CORE_Array2D.h:181
CORE_Array2D()
build an array of T*
Definition: CORE_Array2D.hpp:9
void setValues(const int &nvs, const T *vs, const int &nrs, const int *rs)
set the values
Definition: CORE_Array2D.h:117
T * getRow(const int &i)
get row
Definition: CORE_Array2D.h:372
void setValues(const int &nvs, T *vs, const int &nrs, int *rs, const tBoolean &toBeDeleted)
set the values by reference
Definition: CORE_Array2D.h:160
CORE_Array2D< double > CORE_DoubleArray2D
Definition: CORE_Array2D.h:421
#define tBoolean
Definition: types.h:35
CORE_Array2D< int > CORE_IntArray2D
Definition: CORE_Array2D.h:423
void setRowValue(const int &i, const int &p, const T *vs)
set row value
Definition: CORE_Array2D.h:288
virtual void clear()
clear the array
Definition: CORE_Array2D.h:348
void setSize(const int &n, const int &p)
set the size of the 2D array n rows of n columns
Definition: CORE_Array2D.hpp:77
#define null
Definition: types.h:13
int getRowsNumber() const
get the rows number
Definition: CORE_Array2D.h:393
void setCapacity(const int &n, const int &p)
set total capacity of nRows & nColumns by row
Definition: CORE_Array2D.hpp:36
const T * operator[](int i) const
get the i-th row Assert in (i>-1) Assert in (i
Definition: CORE_Array2D.h:59
TYPEDEF_SPTR(CORE_DoubleArray2D)
const int * getRowIndices() const
get the row indices
Definition: CORE_Array2D.h:388
CORE_Array2D< tInteger > CORE_IntegerArray2D
Definition: CORE_Array2D.h:430
void setRowValue(const int &i, const vector< T > &vs)
set row value
Definition: CORE_Array2D.h:301
CORE_Array2D< tComplex > CORE_ComplexArray2D
Definition: CORE_Array2D.h:426
void copy(const CORE_Array2D< T > *src)
void copy
Definition: CORE_Array2D.h:109
void setRowSize(const int &r, const int &s)
set the row size
Definition: CORE_Array2D.h:233
T * operator[](int i)
get the i-th element Assert in (i>-1) Assert in (i
Definition: CORE_Array2D.h:69
void add(const int &i, const T &obj)
add an element at the end of row at index i
Definition: CORE_Array2D.h:336
int getRowSize(const int &i) const
return the size of the array at index i
Definition: CORE_Array2D.h:360
this class describes an array
Definition: CORE_Array.h:18
CORE_Array2D< tRelativeInteger > CORE_RelativeIntegerArray2D
Definition: CORE_Array2D.h:431
int getSize() const
return the size of the array
Definition: CORE_Array.h:226
this class describes an array of arrays
Definition: CORE_Array2D.h:13
tString toString() const
turn the array into string
Definition: CORE_Array2D.hpp:160
void setRowValue(const int &i, const CORE_Array< Q > &vs)
set row value
Definition: CORE_Array2D.h:311
CORE_Array2D< tCharacter > CORE_CharacterArray2D
Definition: CORE_Array2D.h:422
void setCapacityFactor(const int &i)
set capacity factor
Definition: CORE_Array2D.h:86
int getSize() const
return the size of the vector
Definition: CORE_Vector.h:387
#define tString
Definition: types.h:36
const T * getValues() const
get the values
Definition: CORE_Array2D.h:377
int getValuesNumber() const
return the size of the array
Definition: CORE_Array2D.h:355
CORE_Array2D< tShort > CORE_ShortArray2D
Definition: CORE_Array2D.h:428
void resize()
fit to the size
Definition: CORE_Array2D.hpp:93
CORE_Array2D< tFlag > CORE_FlagArray2D
Definition: CORE_Array2D.h:427
CORE_Array2D< tReal > CORE_RealArray2D
Definition: CORE_Array2D.h:425
const T * getRow(const int &i) const
get row
Definition: CORE_Array2D.h:367
CORE_Array2D< tString > CORE_StringArray2D
Definition: CORE_Array2D.h:429
#define ASSERT_IN(a)
Definition: types.h:82
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106