4 #include "MEDMEM_Exception.hxx"
5 #include "MEDMEM_define.hxx"
6 #include "MEDMEM_PointerOf.hxx"
9 using namespace MED_EN;
12 A template class to generate an array of any particular type (int, long,
13 float, double) for our purpose in the MED++ library.
16 template <class T> class MEDARRAY
17 // Class Template MEDARRAY
18 // template to generate an array of any
19 // particular type (int, long, float, double)
20 // for our purpose in the MED++ library
23 med_int _ldValues; // leading dimension of value (example : space dimension with coordinates)
24 med_int _lengthValues; // length of values (example : number of nodes with coordinates)
25 medModeSwitch _mode; // data access mode:
26 // MED_FULL_INTERLACE (default mode)
27 // or MED_NO_INTERLACE
28 PointerOf <T> _valuesDefault; // in _mode representation
29 PointerOf <T> _valuesOther; // in other _mode representation
32 MEDARRAY(): _ldValues(0), _lengthValues(0), _mode(MED_FULL_INTERLACE),
33 _valuesDefault(), _valuesOther()
42 // the following constructor are for the generation
43 // with the default or not _mode
45 MEDARRAY(const med_int ld_values, const med_int length_values,
46 const medModeSwitch mode=MED_FULL_INTERLACE) ;
48 MEDARRAY( T*values, const med_int ld_values,
49 const med_int length_values, const medModeSwitch mode=MED_FULL_INTERLACE) ;
51 MEDARRAY(MEDARRAY const &m );
53 inline med_int getLeadingValue() const; // as Space Dimension with coordinates
54 inline med_int getLengthValue() const; // as Number of Nodes with coordinates
56 T * const get (const medModeSwitch mode) ;
57 T * const getI (const medModeSwitch mode, const med_int i) ;
58 T const getIJ (const med_int i, const med_int j) const;
60 // need by write, to get default mode !
61 inline medModeSwitch getMode() const ;
63 void set (medModeSwitch mode, T* value);
64 void setI (medModeSwitch mode, med_int i, T* value);
65 void setIJ (med_int i, med_int j, T value);
68 void calculateOther() ;
76 template <class T> MEDARRAY<T>::MEDARRAY(const med_int ld_values,
77 const med_int length_values,
78 const medModeSwitch mode):
80 _lengthValues(length_values),
83 _valuesDefault(length_values*ld_values)
85 BEGIN_OF("constructor MEDARRAY<T>::MEDARRAY(const med_int, const med_int, const medModeSwitch(= MED_FULL_INTERLACE))");
87 // if could not allocate
89 if ((ld_values<1)|(length_values<1))
91 throw MEDEXCEPTION(LOCALIZED("MEDARRAY<T>::MEDARRAY(const med_int, const med_int, const medModeSwitch) : dimension < 1 !"));
94 SCRUTE((T*)_valuesDefault);
95 SCRUTE((T*)_valuesOther);
97 SCRUTE(_lengthValues);
100 END_OF("constructor MEDARRAY<T>::MEDARRAY(const med_int, const med_int, const medModeSwitch (= MED_FULL_INTERLACE))");
103 template <class T> MEDARRAY<T>::MEDARRAY( T*values,
104 const med_int ld_values,
105 const med_int length_values,
106 const medModeSwitch mode):
107 _ldValues(ld_values),
108 _lengthValues(length_values),
112 _valuesDefault.set(values);
115 template <class T> MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m ):
116 _ldValues(m._ldValues),
117 _lengthValues(m._lengthValues),
119 _valuesDefault((int)m._ldValues*m._lengthValues)
121 if ( m._ldValues*m._lengthValues >= 0 )
123 memcpy((T*)_valuesDefault,(const T* const)m._valuesDefault,m._ldValues*m._lengthValues*sizeof(T));
125 if ((const T* const)(m._valuesOther) != NULL)
127 _valuesOther.set(m._ldValues*m._lengthValues);
128 memcpy((T*)_valuesOther,(const T* const)m._valuesOther,m._ldValues*m._lengthValues*sizeof(T));
136 template <class T> inline med_int MEDARRAY<T>::getLeadingValue() const
141 template <class T> inline med_int MEDARRAY<T>::getLengthValue() const
143 return _lengthValues ;
146 template <class T> T* const MEDARRAY<T>::get(const medModeSwitch mode)
148 if ((T*)_valuesDefault != NULL)
151 return _valuesDefault;
156 return _valuesOther ;
158 throw MEDEXCEPTION("MEDARRAY::get(mode) : No values defined !");
161 template <class T> T* const MEDARRAY<T>::getI(const medModeSwitch mode,const med_int i)
163 // 1<=i<=_lengthValues and return an array of _ldValues length
164 // if MED_FULL_INTERLACE
165 // 1<=i<=_ldValues and return an array of _lengthValues length
166 // if MED_NO_INTERLACE
169 throw MEDEXCEPTION("MEDARRAY::getI(mode,i) : argument i must be > 0");
171 if (( T* )_valuesDefault != NULL)
173 // if mode = MED_FULL_INTERLACE :
174 int first = _ldValues ;
175 int second = _lengthValues;
178 if (mode == MED_NO_INTERLACE)
180 first = _lengthValues ;
185 throw MEDEXCEPTION("MEDARRAY::getI(mode,i) : argument i must be <= second");
188 return _valuesDefault + (i-1)*first ;
192 return _valuesOther + (i-1)*first ;
195 throw MEDEXCEPTION("MEDARRAY::getI(mode,i) : No values defined !");
199 template <class T> T const MEDARRAY<T>::getIJ(const med_int i,const med_int j) const
201 // 1<=i<=_lengthValues and 1<j<_ldValues
204 throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be >= 1");
206 throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be <= _lengthValues");
209 throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be >= 1");
211 throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be <= _ldValues");
213 if ( (const T* const)_valuesDefault != NULL)
217 case MED_FULL_INTERLACE :
219 return _valuesDefault[(i-1)*_ldValues+j-1];
221 case MED_NO_INTERLACE :
223 return _valuesDefault[(j-1)*_lengthValues+i-1];
228 throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : No value in array !");
231 template <class T> inline medModeSwitch MEDARRAY<T>::getMode() const
236 template <class T> void MEDARRAY<T>::set(medModeSwitch mode, T* value)
239 BEGIN_OF("MEDARRAY<T>::set(mode,i,value)");
242 SCRUTE(_lengthValues);
243 SCRUTE((T*)_valuesDefault);
244 SCRUTE((T*)_valuesOther);
249 if ((T*)_valuesDefault != NULL)
253 _valuesDefault = value ;
254 if ((T*)_valuesOther != NULL) calculateOther() ;
259 _valuesOther = value ;
265 throw MEDEXCEPTION("MEDARRAY::set(mode,value) : No values defined !");
268 END_OF("MEDARRAY<T>::set(mode,i,value)");
271 template <class T> void MEDARRAY<T>::setI(medModeSwitch mode, med_int i, T* value)
273 BEGIN_OF("MEDARRAY<T>::setI(mode,i,value)");
277 SCRUTE(_lengthValues);
278 SCRUTE((T*)_valuesDefault);
279 SCRUTE((T*)_valuesOther);
283 // 1<=i<=_lengthValues and return an array of _ldValues length
284 // if MED_FULL_INTERLACE
285 // 1<=i<=_ldValues and return an array of _lengthValues length
286 // if MED_NO_INTERLACE
290 throw MEDEXCEPTION("MEDARRAY::setI(mode,i,value) : argument i must be > 0");
292 if ((T*)_valuesDefault != NULL)
294 int first = _ldValues;
295 int second = _lengthValues;
297 if (mode == MED_NO_INTERLACE)
299 first = _lengthValues;
308 throw MEDEXCEPTION("MEDARRAY::setI(mode,i,value) : argument i must be <= second");
312 for (int k =0;k<first;k++)
314 (_valuesDefault + (i-1)*first)[k] = value[k];
316 if ((T*)_valuesOther != NULL)
317 for (int k =0;k<first;k++)
319 _valuesOther[k*second+i-1] = value[k];
325 // return _valuesOther + (i-1)*first = &value ;
326 for (int k =0;k<second;k++)
328 _valuesOther[(i-1)*first+k-1] = value[k];
329 _valuesDefault[k*second+i-1] = value[k];
335 throw MEDEXCEPTION("MEDARRAY::setI(mode,i,value) : No values defined !");
338 SCRUTE((T*) _valuesDefault);
339 SCRUTE((T*) _valuesOther);
341 END_OF("MEDARRAY::setI(mode,i,value)");
344 template <class T> void MEDARRAY<T>::setIJ(med_int i, med_int j, T value)
346 // 1<=i<=_lengthValues and 1<=j<=_ldValues
349 throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument i must be >= 1");
351 throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument i must be <= _lengthValues");
354 throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument j must be >= 1");
356 throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument j must be <= _ldValues");
358 if ((T*)_valuesDefault != NULL)
362 case MED_FULL_INTERLACE :
364 _valuesDefault[(i-1)*_ldValues+j-1] = value;
367 case MED_NO_INTERLACE :
369 _valuesDefault[(j-1)*_lengthValues+i-1] = value;
376 throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : No value in array !");
380 template <class T> void MEDARRAY<T>::calculateOther()
382 BEGIN_OF("MEDARRAY<T>::calculateOther()") ;
383 SCRUTE((T*)_valuesDefault);
384 SCRUTE((T*)_valuesOther);
385 if ((T*)_valuesDefault != NULL)
387 //we allocate _valuesOther if needed
388 if ((T*)_valuesOther == NULL)
390 _valuesOther.set(_ldValues*_lengthValues);
393 // we set _valuesOther
394 int first = _ldValues ;
395 int second = _lengthValues;
396 if (_mode == MED_NO_INTERLACE)
398 first = _lengthValues ;
401 for (int i=0; i<first;i++)
402 for (int j=0; j<second; j++)
403 _valuesOther[i*second+j] = _valuesDefault[j*first+i];
407 throw MEDEXCEPTION("MEDARRAY::calculateOther() : No values defined !");
409 END_OF("MEDARRAY<T>::calculateOther()") ;
412 template <class T> void MEDARRAY<T>::allocateOther()
414 if ((T*)_valuesDefault != NULL)
416 if ((T*)_valuesOther == NULL)
418 // we set _valuesOther
419 _valuesOther.set(_ldValues*_lengthValues);
423 throw MEDEXCEPTION("MEDARRAY::allocateOther() : No values defined !");
426 template <class T> void MEDARRAY<T>::updateDefault()
428 BEGIN_OF("MEDARRAY<T>::updateDefault()");
430 if ((T*)_valuesDefault != NULL)
432 if ((T*)_valuesOther != NULL)
434 // we update _valuesDefault with _valuesOther
435 // if mode = MED_FULL_INTERLACE :
436 int first = _ldValues ;
437 int second = _lengthValues;
439 if (_mode == MED_NO_INTERLACE)
441 first = _lengthValues ;
444 for (int i=0; i<first;i++)
445 for (int j=0; j<second; j++)
446 _valuesDefault[j*first+i] = _valuesOther[i*second+j];
450 throw MEDEXCEPTION("MEDARRAY<T>::updateDefault() : No valuesOther defined !");
455 throw MEDEXCEPTION("MEDARRAY<T>::updateDefault() : No values defined !");
458 END_OF("MEDARRAY<T>::updateDefault()");
461 # endif /* # ifndef __MEDARRAY_H__ */