C++ main module for emicrom 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 
11  mRowsNumber=0;
12  mRowCapacity=0;
14 
16 
17 
18 
19 }
20 
21 template<class T>
24 }
25 
26 
27 template<class T>
30  delete[] mRowIndices;
33  mRowCapacity=0;
34  mRowsNumber=0;
35 
36  }
37 }
38 
39 template<class T>
41 
42  //nothink to do
43  if (s==mRowCapacity) return;
44 
45  //save the old size
46  tUIndex oldSize=mRowsNumber;
47 
48  //allocate the new values
49  tUIndex* newValues=null;
50  try {
51  //allocate with an element more for iteration with pointer
52  newValues=new tUIndex[s+1];
53  } catch(std::exception& e) {
54  throw CORE_Exception("common/core",
55  "CORE_Array::allocate("+CORE_Integer::toString(s)+")",
56  "not enought memory for allocation");
57  }
58  if (newValues==null) {
59  throw CORE_Exception("common/core",
60  "CORE_Array::allocate("+CORE_Integer::toString(s)+")",
61  "not enought memory for allocation");
62  }
63 
64  //copy the old values
65  tUIndex* p=newValues;
66  const tUIndex* oldP=mRowIndices;
67  //m = min(mCapacity,s);
69  for (i=0;i<m;i++) {
70  *p=*oldP;
71  p++;
72  oldP++;
73  }
74  //reset to 0 the new values
75  if (s>m) memset(p,0,(s-m)*sizeof(*p));
76 
77 
78  //desallocate the values array: sze is reduced to 0
80 
81  //set the new values
82  mRowIndices=newValues;
84  mRowCapacity=s;
85  //update the size
86  mRowsNumber=(oldSize>=mRowCapacity)?mRowCapacity:oldSize;
87 }
88 
89 template<class T>
90 void CORE_Array2D<T>::setSize(const tUIndex& nRows,const tUIndex& nCols) {
91 
92  //set the size of values
93  CORE_Array<T>::setSize(nRows*nCols);
94 
95  //set the rows nuuber
96  setRowsNumber(nRows);
97 
98 }
99 template<class T>
100 void CORE_Array2D<T>::reserve(const tUIndex& nRows,const tUIndex& nCols) {
101 
102  //set the size of values
103  CORE_Array<T>::reserve(nRows*nCols);
104 
105  //set the rows number
106  if (mRowCapacity<nRows) allocateRows(nRows);
107 
108 }
109 
110 template<class T>
112  //allocate the row
113  if (nRows+1>mRowCapacity) {
114 
115  //allocate the new rows indices
116  allocateRows(nRows+1);
117 
118  }
119 
120  //set the rows number
121  mRowsNumber=nRows;
122 }
123 
124 template<class T>
127 
128  tUIndex n=getRowsNumber()+1;
129  if (mRowCapacity==n) return;
130  allocateRows(n);
131 
132 }
133 
134 template<class T>
136  //copy the values
138 
139  //copy the rows
140  tUIndex k,n=src.getRowsNumber();
141 
142  //allocate the vector
143  if (n+1>=mRowCapacity) {
144  mRowsNumber=n;
145  allocateRows(n+1);
146  }
147 
148  //copy the rows indices
149  const tUIndex* pf=src.getRowIndices();
150  tUIndex * vs=mRowIndices;
151  for (k=0;k<=n;k++) {
152  *vs=*pf;
153  vs++;
154  pf++;
155 
156  }
157 }
158 
159 
160 template<class T>
161 void CORE_Array2D<T>::setValues(const tUIndex& nvs,const T* vs,const tUIndex& nrs,const tUIndex* rs) {
162  //set the values
164 
165  //set the indices
166  setRowsNumber(nrs-1);
167 
168  //copy it
169  const tUIndex* pf=rs;
170  tUIndex * rows=mRowIndices;
171  for (tUIndex k=0;k<nrs;k++) {
172  *rows=*pf;
173  rows++;
174  pf++;
175 
176  }
177 }
178 template<class T>
179 void CORE_Array2D<T>::setValuesByReference(const tUIndex& nvs,T* vs,const tUIndex& nrs,tUIndex* rs) {
180  //set the values
182 
183  //set the indices
184  setRowsNumber(nrs-1);
185 
186  //copy it
187  desallocateRows();
188 
189  mRowIndices=rs;
190  mRowsNumber=nrs-1;
191  mRowCapacity=nrs;
193 }
194 
195 template<class T>
196 void CORE_Array2D<T>::setRowSize(const tUIndex& iRow,const tUIndex& n) {
197 
198  //get the old size of the row
199  tUIndex nOld=getRowSize(iRow);
200  if (nOld==n) return ;
201  tUIndex oldSize=getValuesNumber();
202 
203  //pointer to the values'index of the row iRow
204  tUIndex *rIndex=&mRowIndices[iRow];
205 
206  //values
207  T *values=getValues();
208 
209  //number of element to add
210  tIndex p=n-nOld;
211 
212  //if nOld > n
213  if (p<0) {
214 
215  //number of element to translate
216  tUIndex nElementsToTranslate=oldSize-(*rIndex)-nOld;
217  //first element of the element to translate
218  const T *ovs=&values[(*rIndex)+nOld];
219  //new position of the translated element
220  T* vs=ovs+p;
221  //move the indices of rows
222  for (tUIndex i=0;i<nElementsToTranslate;i++) {
223  *vs=*ovs;
224  vs++;
225  ovs++;
226  }
227 
228  tUIndex newSize=getValuesNumber()+p;
230 
231  return;
232  }
233 
234  //set the new size
235  tUIndex newSize=getValuesNumber()+p;
237 
238  //move the element from index = mRowIndices[i]+nOld-1 to the end
239  //last element of the old values
240  const T *ovs=&values[oldSize-1];
241 
242  //last element of rows
243  const T *lastRowElement=&values[(*rIndex)+nOld];
244 
245  //last element of the new values
246  T* vs=&values[newSize-1];
247 
248  //move the last element to end of new arrays
249  while (ovs!=lastRowElement) {
250  *vs=*ovs;
251  ovs--;
252  vs--;
253  }
254 
255  //set the new indices of element
256  rIndex++;
257  for (tUIndex i=iRow+1;i<=mRowsNumber;i++) {
258  (*rIndex)+=p;
259  rIndex++;
260  }
261 
262 
263 
264 }
265 template<class T>
266 template<class Q>
268  //set the szie of the row
269  setRowSize(i,vs.getSize());
270  //set the value
271  T* values=&getValues()[mRowIndices[i]];
272  typename vector<Q>::const_iterator iter=vs.begin();
273  while (iter!=vs.end()) {
274  *values=(T) *iter;
275  iter++;
276  values++;
277  }
278 }
279 template<class T>
280 template<class Q>
281 void CORE_Array2D<T>::setRowValue(const tUIndex& i,const vector<Q>& vs) {
282  //set the szie of the row
283  setRowSize(i,vs.getSize());
284  //set the value
285  T* values=&getValues()[mRowIndices[i]];
286  typename vector<Q>::const_iterator iter=vs.begin();
287  while (iter!=vs.end()) {
288  *values=(T) *iter;
289  iter++;
290  values++;
291  }
292 }
293 template<class T>
294 template<class Q>
295 void CORE_Array2D<T>::setRowValue(const tUIndex& index,const CORE_Array<Q>& vs) {
296  tUIndex i,n=vs.getSize();
297  //set the size of the row
298  setRowSize(index,n);
299  //set the value
300  T* values=&getValues()[mRowIndices[i]];
301  const Q* iter=&vs[0];
302  while (i<n) {
303  *values=(T) *iter;
304  iter++;
305  values++;
306  i++;
307  }
308 }
309 template<class T>
310 template<class Q>
311 void CORE_Array2D<T>::setRowValue(const tUIndex& index,const tUIndex& n,const Q* vs) {
312  tUIndex i;
313  //set the size of the row
314  setRowSize(index,n);
315 
316  //set the value
317  T* values=&getValues()[mRowIndices[i]];
318  const Q* iter=vs;
319  while (i<n) {
320  *values=(T) *iter;
321  iter++;
322  values++;
323  i++;
324  }
325 }
326 
327 template<class T>
330  //set the new size of the values
331  CORE_ArrayList<T>::setSize(oldSize+n);
332  //set the new size of rows
334  //resgister the last inde of the new row
335  mRowIndices[getRowsNumber()]=oldSize+n;
336 }
337 
338 template<class T>
340  tString str="";
342  tUIndex i,nRows=getRowsNumber();
343  str+="size:"+CORE_Integer::toString(n)+"\n";
344 
345  //row indices
346  const tUIndex *inds=mRowIndices;
347  tUIndex i0=*inds;
348  inds++;
349 
350  //row values
351  const T * vs=getValues();
352 
353  tUIndex j,nCols;
354 
355  for (i=0;i<nRows;i++) { //loop on row
356 
357  //number of element of the column
358  nCols=(*inds)-i0;
359  for (j=0;j<nCols;j++) {
360  str+=CORE_String::toString(*vs);
361  str+="\t";
362  //next column
363  vs++;
364  }
365  str+="\n";
366 
367  //next row
368  i0=(*inds);
369  inds++;
370 
371  }
372 
373  return str;
374 }
375 
376 #endif
const tUIndex & getRowsNumber() const
get the rows number
Definition: CORE_Array2D.h:306
void allocateRows(const tUIndex &n)
allocate the rows array indices
Definition: CORE_Array2D.hpp:40
const tUIndex & getSize() const
return the size of the array for reading
Definition: CORE_Array.h:1018
void reserve(const tUIndex &cap)
reserve the capacity
Definition: CORE_Array.h:147
void copy(const CORE_Array2D< T > &v)
copy the array 2D
Definition: CORE_Array2D.hpp:135
this class describes an array
Definition: CORE_Vector.h:19
virtual void fitToSize()
fit to size
Definition: CORE_Array2D.hpp:125
virtual ~CORE_Array2D()
destroy an array of T*
Definition: CORE_Array2D.hpp:22
tUIndex getRowSize(const tUIndex &i) const
return the size of the row at index i
Definition: CORE_Array2D.h:261
CORE_Array2D()
build an array of T*
Definition: CORE_Array2D.hpp:9
tUIndex mRowCapacity
Definition: CORE_Array2D.h:36
void setSize(const tUIndex &n)
set the size
Definition: CORE_Array.h:292
virtual tString toString() const
return the string associated to the string
Definition: CORE_String.h:223
tString toString() const
return the string associated to the integer
Definition: CORE_Integer.h:106
#define null
Definition: types.h:144
tBoolean setValuesByReference(const tUIndex &n, Q *array)
set the the values by reference
Definition: CORE_Array.h:405
void reserve(const tUIndex &nRows, const tUIndex &nCols)
reserve of the 2D array of size nRows x nCols
Definition: CORE_Array2D.hpp:100
#define tIndex
Definition: types.h:129
void desallocateRows()
desallocate the rows
Definition: CORE_Array2D.hpp:28
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:15
const T * getValues() const
get the values
Definition: CORE_Array2D.h:288
tUIndex getSize() const
return the size of the vector
Definition: CORE_Vector.h:391
void setRowsNumber(const tUIndex &nRows)
set the rows number
Definition: CORE_Array2D.hpp:111
this class describes an array
Definition: CORE_Array.h:19
virtual void fitToSize()
fit the array alocation exactly to size fit the allocation of the array to its size ...
Definition: CORE_Array.hpp:128
this class describes an array of arrays ordered by rows The array like this 0 1 2 3 4 ...
Definition: CORE_Array2D.h:24
void setValues(const tUIndex &n, const Q *v)
set the values of the array by copying the n first values of pointer v
Definition: CORE_Array.h:445
#define tUIndex
Definition: types.h:126
void addRow(const tUIndex &rowSize)
add a row of size row size at end
Definition: CORE_Array2D.hpp:328
const tUIndex * getRowIndices() const
get the row indices
Definition: CORE_Array2D.h:300
#define tString
Definition: types.h:135
tUIndex mRowsNumber
Definition: CORE_Array2D.h:30
void setValues(const tUIndex &nvs, const T *vs, const tUIndex &nrs, const tUIndex *rs)
set the values
Definition: CORE_Array2D.hpp:161
void setRowSize(const tUIndex &i, const tUIndex &n)
the the size of the row at index i
Definition: CORE_Array2D.hpp:196
void setRowValue(const tUIndex &i, const tUIndex &p, const Q *vs)
set row value to vs array
Definition: CORE_Array2D.hpp:311
Definition: CORE_ArrayList.h:12
vector< T >::const_iterator begin() const
get iterator
Definition: CORE_Vector.h:331
void setValuesByReference(const tUIndex &nvs, T *vs, const tUIndex &nrs, tUIndex *rs)
set the values by reference
Definition: CORE_Array2D.hpp:179
tBoolean mIsPrivateRowAllocated
Definition: CORE_Array2D.h:35
void setSize(const tUIndex &nRows, const tUIndex &nCols)
set the size of the 2D array n rows of n columns
Definition: CORE_Array2D.hpp:90
vector< T >::const_iterator end() const
get iterator
Definition: CORE_Vector.h:341
tString toString() const
turn the array 2D into string
Definition: CORE_Array2D.hpp:339
const tUIndex & getValuesNumber() const
return the number of values
Definition: CORE_Array2D.h:253
tUIndex * mRowIndices
Definition: CORE_Array2D.h:37