C++ main module for gpm Package  1.0
CORE_Array2D.hpp
Go to the documentation of this file.
1 #ifndef CORE_Array2D_HPP
2 #define CORE_Array2D_HPP
3 
4 #include "CORE_Array2D.h"
5 #include "CORE_Integer.h"
6 #include "CORE_String.h"
7 
8 template<class T>
10  mRowsNumber=0;
11  mRowsCapacity=2;
12  mCapacity=1;
13  mCapacityFactor=1;
14 
15  mVector=new T[mCapacity];
16  mRowIndices=new int[mRowsCapacity];
17  mRowIndices[0]=0;
18  mRowIndices[1]=0;
19  mVector[0]=0;
20 
21  mHasToBeDeleted=true;
22 
23 
24 
25 }
26 
27 template<class T>
29  if (mHasToBeDeleted) {
30  if (mVector!=null) delete[] mVector;
31  if (mRowIndices!=null) delete[] mRowIndices;
32  }
33 }
34 
35 template<class T>
36 void CORE_Array2D<T>::setCapacity(const int& n,const int& p) {
37 
38 
39  if (mCapacity<n*p) {
40  // resize the array
41  int size=n*p*mCapacityFactor;
42  T *newVector=new T[size];
43  // copy the values
44  for (int i=0;i<mCapacity;i++) {
45  newVector[i]=mVector[i];
46  }
47  for (int i=mCapacity;i<size;i++) {
48  newVector[i]=0;
49  }
50  if (mHasToBeDeleted && (mVector!=null) ) delete[] mVector;
51  mVector=newVector;
52  mCapacity=size;
53 
54  }
55 
56  //copy the row
57  if (n>mRowsCapacity) {
58  int size=getValuesNumber();
59  mRowsCapacity=n*mCapacityFactor+1;
60  int *rows=new int[mRowsCapacity];
61  for (int i=0;i<=mRowsNumber;i++) {
62  rows[i]=mRowIndices[i];
63  }
64  for (int i=mRowsNumber+1;i<mRowsCapacity;i++) {
65  rows[i]=size;
66  }
67 
68 
69  if (mHasToBeDeleted && (mRowIndices!=null)) delete[] mRowIndices;
70  mRowIndices=rows;
71 
72  }
73  mHasToBeDeleted=true;
74 }
75 
76 template<class T>
77 void CORE_Array2D<T>::setSize(const int& n,const int& p) {
78  setCapacity(n,p);
79  // set the row
80  int r=0;
81  int *rowIndices=mRowIndices;
82  for (int i=0;i<n-1;i++) {
83  *rowIndices=r;
84  r+=p;
85  rowIndices++;
86  }
87  // last size
88  *rowIndices=r;
89  mRowsNumber=n;
90 }
91 
92 template<class T>
94  if (mRowIndices[mRowsNumber]<mCapacity) {
95  // set vector capacity
96  mCapacity=mRowIndices[mRowsNumber];
97  T *newVector=new T[mCapacity];
98  for (int i=0;i<mCapacity;i++) {
99  newVector[i]=mVector[i];
100  }
101  if (mHasToBeDeleted && (mVector!=null) ) delete[] mVector;
102  mVector=newVector;
103  }
104  if (mRowsCapacity>mRowsNumber+1) {
105  mRowsCapacity=mRowsNumber+1;
106  int *newIndices=new int[mRowsCapacity];
107  for (int i=0;i<=mRowsNumber;i++) {
108  newIndices[i]=mRowIndices[i];
109  }
110  if (mHasToBeDeleted) delete [] mRowIndices;
111  mRowIndices=newIndices;
112  }
113  mHasToBeDeleted=true;
114 
115 }
116 template<class T>
118  if (this==&src) return;
119  mCapacityFactor=src.getCapacityFactor();
120 
121  int nValues=src.getValuesNumber();
122 
123  mCapacity=nValues;
124  if (mCapacity==0) mCapacity=1;
125 
126  if (mHasToBeDeleted) {
127  if (mVector!=null) delete[] mVector;
128  if (mRowIndices!=null) delete[] mRowIndices;
129  }
130 
131  mRowsNumber=src.getRowsNumber();
132  mRowsCapacity=mRowsNumber+1;
133  mRowIndices=new int[mRowsCapacity];
134  mVector=new T[mCapacity];
135  T *curV=mVector;
136  if (nValues>0) {
137  const T *srcV=src[0];
138  for (int i=0;i<mCapacity-1;i++) {
139  *curV=*srcV;
140  curV++;
141  srcV++;
142  }
143  *curV=*srcV;
144  } else {
145  mVector[0]=0;
146  }
147  int *curR=mRowIndices;
148  const int *inds=src.getRowIndices();
149  for (int i=0;i<mRowsCapacity-1;i++) {
150  *curR=*inds;
151  inds++;
152  curR++;
153  }
154  *curR=*inds;
155  mHasToBeDeleted=true;
156 }
157 
158 
159 template<class T>
161  tString str("");
162  int n=getValuesNumber();
163  int nRows=getRowsNumber();
164  str+="size:"+CORE_Integer::toString(n)+"\n";
165  str+="capacityFactor:"+CORE_Integer::toString(mCapacityFactor)+"\n";
166  str+="capacity:"+CORE_Integer::toString(mCapacity)+"\n";
167  const int *inds=&mRowIndices[1];
168  int p,i0=0;
169  for (int i=0;i<nRows;i++) {
170  const T * vs=(*this)[i];
171  p=*inds-i0;
172  i0=*inds;
173  inds++;
174  for (int j=0;j<p;j++) {
175  str+=CORE_String::toString(vs[j]);
176  str+="\t";
177  }
178  str+="\n";
179 
180 
181  }
182  return str;
183 }
184 
185 #endif
tString toString() const
return the string associated to the string
Definition: CORE_String.h:150
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
CORE_Array2D()
build an array of T*
Definition: CORE_Array2D.hpp:9
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 int * getRowIndices() const
get the row indices
Definition: CORE_Array2D.h:388
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
tString toString() const
return the string associated to the integer
Definition: CORE_Integer.h:142
#define tString
Definition: types.h:36
int getValuesNumber() const
return the size of the array
Definition: CORE_Array2D.h:355
void resize()
fit to the size
Definition: CORE_Array2D.hpp:93