C++ main module for emicrom Package  1.0
CORE_Array.hpp
Go to the documentation of this file.
1 #ifndef CORE_Array_HPP
2 #define CORE_Array_HPP
3 
4 #include <CORE_Array.h>
5 #include <vector>
6 #include "CORE_Integer.h"
7 #include "CORE_String.h"
8 #include "CORE_Vector.h"
9 #include "CORE_Exception.h"
10 
11 using namespace std;
12 
13 
14 template<class T>
15 template<class Q>
17  tUIndex k,n=f.getSize();
18 
19  if (n==0) {
20  desallocate();
21  return;
22  }
23 
24  //allocate the vector
25  if (n>mCapacity) {
26  allocate(n);
27  }
28 
29  //set the size
30  mSize=n;
31 
32  //copy the vector
33  const Q* pf=&f[0];
34  T* vs=mValues;
35 
36  if (sizeof(T)==sizeof(Q)) {
37  //copy a block of memory
38  memcpy(vs,pf,sizeof(T)*n);
39  } else {
40  //copy each value
41  for (k=0;k<n;k++) {
42  *vs=*pf;
43  vs++;
44  pf++;
45 
46  }
47  }
48 }
49 
50 
51 template<class T>
53  if ( mIsPrivateAllocated && (mValues!=null)) {
54  delete[] mValues;
55  mValues=null;
56  mIsPrivateAllocated=false;
57  mCapacity=0;
58  mSize=0;
59 
60  }
61 }
62 
63 template<class T>
65  //nothink to do
66  if (s==mCapacity) return;
67 
68  //save the old size
69  tUIndex oldSize=mSize;
70 
71  //allocate the new values
72  T* newValues=null;
73  try {
74  //allocate with an element more for iteration with pointer
75  newValues=new T[s+1];
76  } catch(std::exception& e) {
77  throw CORE_Exception("common/core",
78  "CORE_Array::allocate("+CORE_Integer::toString(s)+")",
79  "not enought memory for allocation");
80  }
81  if (newValues==null) {
82  throw CORE_Exception("common/core",
83  "CORE_Array::allocate("+CORE_Integer::toString(s)+")",
84  "not enought memory for allocation");
85  }
86 
87  //copy the old values
88  T* p=newValues;
89  const T* oldP=mValues;
90  //m = min(mCapacity,s);
91  tUIndex m=(mCapacity<s)?mCapacity:s;
92  if (m>0) {
93  //copy a block of memory
94  memcpy(p,oldP,m*sizeof(T));
95  }
96 
97  // for (tUIndex i=0;i<m;i++) {
98  // *p=*oldP;
99  // p++;
100  // oldP++;
101  // }
102 
103  //reset to 0 the new values by block memory
104  p+=m;
105  memset(p,0,sizeof(T)*(s-m+1));
106 
107  // for (i=m;i<=s;i++) {
108  // *p=(T) 0;
109  // p++;
110  // }
111 
112 
113  //desallocate the values array: sze is reduced to 0
114  desallocate();
115 
116  //set the new values
117  mValues=newValues;
118  mIsPrivateAllocated=true;
119  mCapacity=s;
120  //update the size
121  mSize=(oldSize>=mCapacity)?mCapacity:oldSize;
122 }
123 
124 
125 
126 
127 template<class T>
129 
130  tUIndex n=getSize();
131  if (mCapacity==n) return;
132  allocate(n);
133 }
134 
135 
136 template<class T>
137 template<class Q>
138 void CORE_Array<T>::initArray(const tUIndex& p,const Q B[]) {
139 
140  //get the size of the field
141  tUIndex n=getSize()/p;
142  if (n==0) return;
143 
144 
145  //set the value
146  tUSInt d=0;
147  tUIndex i;
148 
149  //get the first element of this
150  T *vs=&(*this)[0];
151 
152  const Q* b=null;
153 
154  if (sizeof(T)==sizeof(Q)) {
155  //copy by block
156  i=0;
157  while (i<n) {
158  //copy the block B
159  memcpy(vs,B,sizeof(T)*p);
160 
161  //next block
162  vs+=p;
163  i++;
164 
165  }
166  } else {
167  //make i loops on size p
168  i=0;
169  while (i<n) {
170  b=&B[0];
171  //loop on size p
172  for (d=0;d<p;d++) {
173  *vs=(T) *b;
174  b++;
175  vs++;
176  }
177  i++;
178  }
179  }
180 }
181 
182 
183 
184 
185 
186 
187 template<class T>
188 template<class Q>
189 void CORE_Array<T>::initArray(const tUIndex& from,const tUIndex& to,const tUIndex& p,const tUSInt& inc,const Q B[]) {
190 
191  //get the size of the field
192  tUIndex n=getSize();
193  if (n==0) return;
194 
195  //for dimenson loop
196  tUSInt d=0;
197 
198  //set the bounds
199  if (to<n) n=to;
200  if (from>=n) return;
201 
202  tUIndex i=from;
203 
204  //get the value of This at from index
205  T *vs=&(*this)[i];
206 
207  //ge the intial value of B
208  const Q* b=&B[0];
209 
210  //index of element of b
211  d=0;
212  while (i<n) {//loop on i
213 
214  //the the This value to b
215  *vs=(T)(*b);
216 
217  //next element i
218  i++;
219 
220  //next value of this
221  vs++;
222 
223  //index of next element of b
224  d+=inc;
225  //test if the end values of B is reached
226  if (d>=p) {
227  //end values of B is reached, go back to the first element
228  d=0;
229  b=&B[0];
230  } else {
231  //increment b
232  b+=inc;
233  }
234  }
235 
236 }
237 
238 
239 template<class T>
240 template<class Q>
241 void CORE_Array<T>::initArray(const tUIndex& from,const tUIndex& to,const Q& Bx,const Q& By,const Q& Bz) {
242 
243  //get the size of the field
244  tUIndex n=getSize();
245  if (n==0) return;
246 
247  //set the bounds
248  if (to<n) n=to;
249  if (from>=n) return;
250 
251  tUIndex i=from;
252  T *vs=&(*this)[i];
253 
254  n=(n-from)/3;
255  while (i<n) {
256  *vs=(T)Bx;
257  vs++;
258  *vs=(T)By;
259  vs++;
260  *vs=(T)Bz;
261  vs++;
262  i++;
263  }
264 
265 
266 }
267 
268 
269 template<class T>
270 void CORE_Array<T>::initArray(const tUIndex& from,const tString& v) {
271  tUIndex l=v.length();
272  if (l==0) {
273  clear();
274  }
275  if ((v[0]!='[') && (v[0]!='(')) return;
276  SP::CORE_String str=CORE_String::New(v.substr(1,v.length()-2));
277  str->tokenize(",");
278  str->begin();
279  tUIndex n=str->getTokensCount();
280  tUIndex newDim=n+from;
281  // verify the capacity is sufficent
282  if (newDim>mCapacity) allocate(newDim);
283 
284  // set the size
285  setSize(n+from);
286 
287  T value;
288  T* values=&mValues[from];
289  while (str->hasNextToken()) {
290  //parse the value
291  CORE_String::parse(str->nextToken(),value);
292  //set it
293  *values=value;
294  //next iter
295  values++;
296  }
297 }
298 
299 
300 template<class T>
302  tString ret="[";
303  if (mSize>0) {
304  const T *vs=&(*this)[0];
305  for (tUIndex i=0;i<mSize-1;i++) {
306  ret+=CORE_String::toString(*vs);
307  ret+=",";
308  vs++;
309  }
310  ret+=CORE_String::toString(*vs);
311 
312  }
313  ret+="]";
314  return ret;
315 }
316 
317 
318 #endif
const tUIndex & getSize() const
return the size of the array for reading
Definition: CORE_Array.h:1018
void desallocate()
desallocate the memory
Definition: CORE_Array.hpp:52
static void parse(const tString &str, tUChar &c)
parse unsigned char c in str
Definition: CORE_String.h:504
virtual tString toString() const
turn the array into string
Definition: CORE_Array.hpp:301
#define tUSInt
Definition: types.h:28
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
void allocate(const tUIndex &cap)
allocate the memory if the array
Definition: CORE_Array.hpp:64
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:15
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
#define tUIndex
Definition: types.h:126
#define tString
Definition: types.h:135
static SP::CORE_String New()
create a class String
Definition: CORE_String.h:96
void initArray(const Q &f)
init the array to uniform value
Definition: CORE_Array.h:316
void copy(const CORE_Array< Q > &f)
copy the array
Definition: CORE_Array.hpp:16