#include "MEDMEM_Exception.hxx"
#include "MEDMEM_define.hxx"
+#include "MEDMEM_PointerOf.hxx"
#include "utilities.h"
using namespace MED_EN;
/*!
A template class to generate an array of any particular type (int, long,
- float, double) for our purpose in the MED++ library.
+ float, double) for our purpose in the MED++ library./n/n
+
+ Arrays can be stored in MED_FULL_INTERLACE mode (ie : x1,y1,z1,x2,y2,z2...) or
+ in MED_NO_INTERLACE mode ( x1,x2,..xn, y1, y2 ..,yn,z1,...,zn)./n The alternate
+ representation mode is calculate ONLY when it is usefull. We assure coherency
+ for minor data modifications (element, line or column) if you use set methods.
+ But, if you get a pointer and modify the array, no automatical coherency is possible.
+ You can use calculateOther to force a recalculation and insure the coherency./n
+ No recalculation is done, when the entire array is modified./n
+ Theses arrays are "Med like" arrays; lower bound equals 1. (first element is element 1,
+ first coordinate is coordinate 1). /n
+
+ Available constructors are :/n
+
+ - default constructor (not very usefull)/n
+ - constructor asking for array dimensions and mode (does memory allocation for you)/n
+ - constructor asking for array dimensions, mode and values (doesn't do memory allocation
+ but copies pointers only.)/n
+ - a copy constructor which copies only pointers./n
+ (be aware of coherency)
+ - a copy constructor which copies data (deepcopy)./n
+ - assignement operator is also available and only copies pointers (and not data)./n/n
+
+ Attribute "pointers" aren't standard pointers but class PointerOf objects in order to simplify
+ memory management./n
+
+ A simple test program (testUArray) allows to test this class.
*/
-template <class T> class MEDARRAY
-// Class Template MEDARRAY
-// template to generate an array of any
-// particular type (int, long, float, double)
-// for our purpose in the MED++ library
+namespace MEDMEM {
+template <class T> class MEDARRAY
{
private :
- med_int _ld_values ; // leading dimension of value (example : space dimension with coordinates)
- med_int _length_values ; // length of values (example : number of nodes with coordinates)
- medModeSwitch _mode; // data access mode:
- // MED_FULL_INTERLACE (default mode)
- // or MED_NO_INTERLACE
- T * _valuesDefault ; // in _mode representation
- T * _valuesOther ; // in other _mode
+
+ /*! leading dimension of value (example : space dimension for coordinates) */
+ med_int _ldValues;
+ /*! length of values (example : number of nodes for coordinates) */
+ med_int _lengthValues;
+ /*! data access mode. possible values are :\n
+ - MED_FULL_INTERLACE (default mode) \n
+ - MED_NO_INTERLACE */
+ medModeSwitch _mode;
+ /*! Pointer to representation in mode MED_FULL_INTERLACE */
+ PointerOf <T> _valuesFull;
+ /*! Pointer to representation in mode MED_NO_INTERLACE */
+ PointerOf <T> _valuesNo;
+ /*! Pointer to representation in mode _mode */
+ PointerOf <T> _valuesDefault;
+ /*! Pointer to representation in the other mode (!=_mode) */
+ PointerOf <T> _valuesOther;
public :
- MEDARRAY():_ld_values(0),_length_values(0),_mode(MED_FULL_INTERLACE),_valuesDefault((T*)NULL),_valuesOther((T*)NULL)
- {
- };
- ~MEDARRAY()
- {
- if (_valuesDefault)
- delete [] _valuesDefault ;
- if (_valuesOther)
- delete [] _valuesOther ;
- };
-
+ inline MEDARRAY();
+ inline ~MEDARRAY();
+
+ MEDARRAY (const med_int ld_values, const med_int length_values,
+ const medModeSwitch mode=MED_FULL_INTERLACE);
+ MEDARRAY (T* values, const med_int ld_values,
+ const med_int length_values, const medModeSwitch mode=MED_FULL_INTERLACE);
+ MEDARRAY (MEDARRAY const &m);
+ MEDARRAY (MEDARRAY const &m, bool copyOther);
+ MEDARRAY & operator = (const MEDARRAY & m);
+
+ inline med_int getLeadingValue() const;
+ inline med_int getLengthValue() const;
+
+ const T * get (const medModeSwitch mode) ;
+ const T * getRow (const med_int i) ;
+ const T * getColumn (const med_int j) ;
+ const T getIJ (const med_int i, const med_int j) const;
+// T * const get (const medModeSwitch mode) const;
+// T * const getRow (const med_int i) const;
+// T * const getColumn (const med_int j) const;
+// T const getIJ (const med_int i, const med_int j) const;
+
+ inline medModeSwitch getMode() const;
+
+ void set (const medModeSwitch mode,const T* value);
+ void setI (const med_int i, const T* value);
+ void setJ (const med_int j, const T* value);
+ void setIJ (const med_int i, const med_int j, const T value);
+
+ void calculateOther();
+ bool isOtherCalculated() const {return (const T*)_valuesOther != NULL;}
+ void clearOtherMode();
+};
+}
- // the following constructor are for the generation
- // with the default or not _mode
+//-------------------------------------------------//
+// //
+// IMPLEMENTED CODE //
+// //
+//-------------------------------------------------//
- MEDARRAY(const med_int ld_values, const med_int length_values,
- const medModeSwitch mode=MED_FULL_INTERLACE) ;
- MEDARRAY( T*values, const med_int ld_values,
- const med_int length_values, const medModeSwitch mode=MED_FULL_INTERLACE) ;
+template <class T> inline MEDARRAY<T>::MEDARRAY():
+ _ldValues(0), _lengthValues(0), _mode(MED_FULL_INTERLACE),
+ _valuesFull(), _valuesNo(),
+ _valuesDefault(), _valuesOther()
+{
+};
- inline med_int getLeadingValue() ; // as Space Dimension with coordinates
- inline med_int getLengthValue() ; // as Number of Nodes with coordinates
-
- T * const get(medModeSwitch mode) ;
- T* const getI(medModeSwitch mode,med_int i) ;
- T getIJ(med_int i, med_int j) const ;
+// ------------------
- // need by write, to get default mode !
- inline medModeSwitch getMode() const ;
-
- void set(medModeSwitch mode, T* value);
- void setI(medModeSwitch mode, med_int i, T* value);
- void setIJ(med_int i, med_int j, T value);
-
-private:
- void calculateOther() ;
- void allocateOther();
- void updateDefault();
+template <class T> inline MEDARRAY<T>::~MEDARRAY()
+{
};
+// ------------------
-// implemented code
+ /*! This constructor does allocation and does not set values : \n.
+ It allocates a "T" array of length_values*ld_values length.\n
+ You don't have to know the T values when calling this construtor
+ but you have to call "set" method to initialize them later.
+ You also can get the pointer to the memory zone (with "get" method),
+ and work with it./n
+ The desallocation of T array is not your responsability. \n\n
+ Throws MEDEXCEPTION if T array length is < 1*/
template <class T> MEDARRAY<T>::MEDARRAY(const med_int ld_values,
const med_int length_values,
- const medModeSwitch mode = MED_FULL_INTERLACE): _ld_values(ld_values),_length_values(length_values),_mode(mode),_valuesOther((T*)NULL)
-{
- BEGIN_OF("constructor MEDARRAY<T>::MEDARRAY(const med_int , const med_int, const medModeSwitch (= MED_FULL_INTERLACE))");
-
- // if could not allocate ?
- if ((ld_values<1)|(length_values<1))
- throw MEDEXCEPTION( LOCALIZED("MEDARRAY<T>::MEDARRAY(const med_int, const med_int, const medModeSwitch) : dimension < 1 !"));
- _valuesDefault = new T[ld_values*length_values] ;
+ const medModeSwitch mode):
- SCRUTE(_valuesDefault);
+ _ldValues(ld_values),
+ _lengthValues(length_values),
+ _mode(mode),
+ _valuesFull(), _valuesNo(),
+ _valuesDefault(),_valuesOther()
+{
+ BEGIN_OF("constructor MEDARRAY<T>::MEDARRAY(const med_int, const med_int, const medModeSwitch)");
- SCRUTE(_valuesOther);
+ // if ld_values < 1 or length_values < 1
+ // throws an exception
+ // Pointers are setted to NULL
- SCRUTE(_ld_values);
+ if ((ld_values<1)|(length_values<1))
+ {
+ throw MEDEXCEPTION(LOCALIZED("MEDARRAY<T>::MEDARRAY(const med_int, const med_int, const medModeSwitch) : dimension < 1 !"));
+ }
- SCRUTE(_length_values);
+ if ( _mode == MED_FULL_INTERLACE)
+ {
+ _valuesFull.set(length_values*ld_values);
+ _valuesDefault.set((T*) _valuesFull);
+ }
+ else
+ {
+ ASSERT (_mode == MED_NO_INTERLACE);
+ _valuesNo.set(length_values*ld_values);
+ _valuesDefault.set((T*)_valuesNo);
+ }
- SCRUTE(_mode);
+ ASSERT( (T*)_valuesDefault != NULL);
+ SCRUTE((T*)_valuesDefault);
+ SCRUTE((T*)_valuesOther);
+ SCRUTE((T*)_valuesNo);
+ SCRUTE((T*)_valuesFull);
- END_OF("constructor MEDARRAY<T>::MEDARRAY(const med_int , const med_int, const medModeSwitch (= MED_FULL_INTERLACE))");
+ END_OF("constructor MEDARRAY<T>::MEDARRAY(const med_int, const med_int, const medModeSwitch ()");
}
+// ------------------
+
+ /*! This constructor duplicate T*values.\n
+
+ Throws MEDEXCEPTION if the lenght of T is < 1*/
template <class T> MEDARRAY<T>::MEDARRAY( T*values,
const med_int ld_values,
const med_int length_values,
- const medModeSwitch mode=MED_FULL_INTERLACE): _ld_values(ld_values),_length_values(length_values),_mode(mode),_valuesOther((T*)NULL)
+ const medModeSwitch mode):
+ _ldValues(ld_values),
+ _lengthValues(length_values),
+ _mode(mode),
+ _valuesFull(),_valuesNo(),
+ _valuesDefault(),_valuesOther()
{
- _valuesDefault = values;
+ BEGIN_OF("constructor MEDARRAY<T>::MEDARRAY(T* values, const med_int, const med_int, const medModeSwitch)");
+
+ // if ld_values < 1 or length_values < 1, we could not allocate
+ // throws an exception
+
+ if ( (ld_values<1) | (length_values<1) )
+ {
+ throw MEDEXCEPTION(LOCALIZED("MEDARRAY<T>::MEDARRAY(T* values, const med_int, const medModeSwitch) : dimension < 1 !"));
+ }
+ if ( _mode == MED_FULL_INTERLACE)
+ {
+ _valuesFull.set(_ldValues*length_values,values);
+ _valuesDefault.set((T*)_valuesFull);
+ }
+ else
+ {
+ ASSERT (_mode == MED_NO_INTERLACE);
+ _valuesNo.set(_ldValues*length_values,values);
+ _valuesDefault.set((T*)_valuesNo);
+ }
+
+ ASSERT( (T*)_valuesDefault != NULL);
+ SCRUTE((T*)_valuesDefault);
+ SCRUTE((T*)_valuesOther);
+ SCRUTE((T*)_valuesNo);
+ SCRUTE((T*)_valuesFull);
+
+ END_OF("constructor MEDARRAY<T>::MEDARRAY(T* values, const med_int, const med_int, const medModeSwitch)");
}
-template <class T> inline med_int MEDARRAY<T>::getLeadingValue() {
- return _ld_values ;
-};
-template <class T> inline med_int MEDARRAY<T>::getLengthValue() {
- return _length_values ;
-};
+// ------------------
+
+ /*! This constructor allocates a new medarray and does a copy of pointers/n
+ It DOES NOT copy the memory . The two objects will share the same data./n
+ (for copying data, use constructor MEDARRAY(MEDARRAY<T> const & m,bool copyOther). */
+template <class T> MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m ):
+ _ldValues(m._ldValues),
+ _lengthValues(m._lengthValues),
+ _mode(m._mode),
+ _valuesFull((const T*)m._valuesFull),
+ _valuesNo((const T*)m._valuesNo),
+ _valuesDefault((const T*)m._valuesDefault),
+ _valuesOther((const T*)m._valuesOther)
+{
+ BEGIN_OF("constructor MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m)");
+ ASSERT( (T*)_valuesDefault != NULL);
+ SCRUTE((T*)_valuesDefault);
+ SCRUTE((T*)_valuesOther);
+ SCRUTE((T*)_valuesNo);
+ SCRUTE((T*)_valuesFull);
+ END_OF("constructor MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m)");
+}
-template <class T> T* const MEDARRAY<T>::get(medModeSwitch mode)
+ /*! This constructor allocates a new array and does a copy of values
+ included in the m arrays./n
+ If the boolean is setted to true, both representations (in full mode
+ and no interlace mode) will be copied./n
+ Otherwise, only _valuesDefault will be copied./n
+ Desallocation of the arrays is not your reponsability./n/n
+ Throws MEDEXCEPTION if _valuesOther is null and copyOther equals true*/
+template <class T> MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & p,bool copyOther ):
+ _ldValues(p._ldValues),
+ _lengthValues(p._lengthValues),
+ _mode(p._mode),
+ _valuesFull(),
+ _valuesNo(),
+ _valuesDefault(),
+ _valuesOther()
{
- if (_valuesDefault != NULL)
- if (mode == _mode) {
- return _valuesDefault;
+ BEGIN_OF("Constructeur deepCopy MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m,bool copyOther");
+
+ // PG : Non, s'il n'y a rien, on test et on ne copie rien, c'est tout !
+
+// if ( copyOther==true && ((const T*)p._valuesOther==NULL))
+// {
+// throw MEDEXCEPTION("MEDARRAY MEDARRAY const &m,bool copyOther : No Other values defined and bool = true !");
+// }
+
+ if ( _mode == MED_FULL_INTERLACE)
+ {
+ _valuesFull.set(p._ldValues*p._lengthValues,(const T*)p._valuesFull);
+ _valuesDefault.set((T*)_valuesFull);
+ if (copyOther)
+ if ((const T*)p._valuesNo != NULL)
+ {
+ _valuesNo.set(p._ldValues*p._lengthValues,(const T*)p._valuesNo);
+ _valuesOther.set((T*)_valuesNo);
+ }
}
- else {
- calculateOther() ;
- return _valuesOther ;
+ else
+ {
+ ASSERT (_mode == MED_NO_INTERLACE);
+ _valuesNo.set(p._ldValues*p._lengthValues,(const T*)p._valuesNo);
+ _valuesDefault.set((T*)_valuesNo);
+ if (copyOther)
+ if ((const T*)p._valuesFull != NULL)
+ {
+ _valuesFull.set(p._ldValues*p._lengthValues,(const T*)p._valuesFull);
+ _valuesOther.set((T*)_valuesFull);
+ }
}
- throw MEDEXCEPTION("MEDARRAY::get(mode) : No values defined !");
}
-template <class T> T* const MEDARRAY<T>::getI(medModeSwitch mode,med_int i)
+// ------------------
+
+// /*! This operator does a copy of pointers/n
+// It DOES NOT copy of the memory.
+// The two objects will share data./n */
+
+template <class T> MEDARRAY<T> & MEDARRAY<T>::operator = (const MEDARRAY & m)
{
- // 1<=i<=_length_values and return an array of _ld_values length
- // if MED_FULL_INTERLACE
- // 1<=i<=_ld_values and return an array of _length_values length
- // if MED_NO_INTERLACE
-
- if (_valuesDefault != NULL) {
- // if mode = MED_FULL_INTERLACE :
- int first = _ld_values ;
- int second = _length_values;
- // else :
- if (mode == MED_NO_INTERLACE) {
- first = _length_values ;
- second = _ld_values ;
- }
- if (i<=0)
- throw MEDEXCEPTION("MEDARRAY::getI(mode,i) : argument i must be > 0");
- if (i>second)
- throw MEDEXCEPTION("MEDARRAY::getI(mode,i) : argument i must be <= second");
- if (mode == _mode)
- return _valuesDefault + (i-1)*first ;
- else {
- calculateOther() ;
- return _valuesOther + (i-1)*first ;
- // return _valuesOther + (i-1)*second ;
- }
+
+ BEGIN_OF("Operator = MEDARRAY<T>");
+
+ _ldValues=m._ldValues;
+ _lengthValues=m._lengthValues;
+ _mode=m._mode;
+
+ SCRUTE(_mode);
+
+ if ((const T*) m._valuesFull !=NULL)
+ _valuesFull.set(_ldValues*_lengthValues,(const T*) m._valuesFull);
+
+ if ((const T*) m._valuesNo !=NULL)
+ _valuesNo.set(_ldValues*_lengthValues,(const T*) m._valuesNo);
+
+ if (_mode == MED_FULL_INTERLACE) {
+ //PN : pour enlever les warning compilateur
+ //_valuesDefault.set((const T*) _valuesFull);
+ //_valuesOther.set((const T*) _valuesNo);
+ _valuesDefault.set((T*) _valuesFull);
+ _valuesOther.set((T*) _valuesNo);
+ } else {
+ ASSERT (_mode == MED_NO_INTERLACE);
+ //PN : pour enlever les warning compilateur
+ //_valuesDefault.set((const T*) _valuesNo);
+ //_valuesOther.set((const T*) _valuesFull);
+ _valuesDefault.set((T*) _valuesNo);
+ _valuesOther.set((T*) _valuesFull);
}
- throw MEDEXCEPTION("MEDARRAY::getI(mode,i) : No values defined !");
+
+ SCRUTE((T*)_valuesDefault);
+ SCRUTE((T*)_valuesOther);
+ SCRUTE((T*)_valuesNo);
+ SCRUTE((T*)_valuesFull);
+
+ END_OF("Operator = MEDARRAY<T>");
+ return *this;
}
+// ------------------
-template <class T> T MEDARRAY<T>::getIJ(med_int i, med_int j) const
+ /*! returns _ldValues. (for example, space dimension for coordinates array)*/
+template <class T> inline med_int MEDARRAY<T>::getLeadingValue() const
{
- // 1<=i<=_length_values and 1<j<_ld_values
-
- if (i<1)
- throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be >= 1");
- if (i>_length_values)
- throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be <= _length_values");
-
- if (j<1)
- throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be >= 1");
- if (j>_ld_values)
- throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be <= _ld_values");
-
- if (_valuesDefault != NULL) {
- switch (_mode)
- {
- case MED_FULL_INTERLACE :
- {
- return _valuesDefault[(i-1)*_ld_values+j-1];
- }
- case MED_NO_INTERLACE :
+ return _ldValues;
+};
+
+// ------------------
+
+ /*! returns _ldValues. (for example, number of nodes for coordinates array)*/
+template <class T> inline med_int MEDARRAY<T>::getLengthValue() const
+{
+ return _lengthValues;
+};
+
+// ------------------
+
+ /*! returns a pointer to _valuesDefault or _valuesOther, depending on
+ mode value : if mode is the same as _mode, _valuesDefault is returned.
+ else, if _valuesOther is calculated (if necessary) and then returned.
+ The pointer can be used to set values */
+template <class T> const T* MEDARRAY<T>::get(const medModeSwitch mode)
+{
+ BEGIN_OF("MEDARRAY<T>::get(const medModeSwitch mode)");
+ if ((T*)_valuesDefault == NULL)
+ {
+ throw MEDEXCEPTION("MEDARRAY::get(mode) : No values defined !");
+ }
+ if (mode == _mode)
+ {
+ //PN : pour enlever les warning compilateurs
+ //return (const T*)_valuesDefault;
+ return (T*) _valuesDefault;
+ }
+ else
+ {
+ if ((T*)_valuesOther == NULL)
{
- return _valuesDefault[(j-1)*_length_values+i-1];
+ calculateOther();
}
- }
- } else
- throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : No value in array !");
+ //PN : pour enlever les warning compilateurs
+ //return (const T*)_valuesDefault;
+ return (T*) _valuesOther;
+ }
+ END_OF("MEDARRAY<T>::get(const medModeSwitch mode)");
+}
+
+// ------------------
+
+ /*! returns a pointer to ith element of the array.
+ (ith line in a MED_FULL_INTERLACE representation )/n
+ Be aware : if _mode is MED_NO_INTERLACE, the entire
+ array will be recalculate in MED_FULL_INTERLACE representation./n*/
+
+template <class T> const T* MEDARRAY<T>::getRow(const med_int i)
+{
+
+ BEGIN_OF("MEDARRAY<T>::getRow(const med_int i)");
+
+ if ((T*)_valuesDefault == NULL)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getRow(i) : No values defined !");
+ }
+ if (i<1)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getRow(i) : argument i must be >= 1");
+ }
+ if (i>_lengthValues)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getRow(i) : argument i must be <= _lengthValues");
+ }
+
+ if ((T*)_valuesFull == NULL)
+ {
+ ASSERT(((T*) _valuesDefault)==((T*) _valuesNo));
+ calculateOther();
+ }
+ ASSERT((T*)_valuesFull != NULL);
+
+ // PN pour enlever les warning compilateurs
+ //const T* ptr = (const T*)_valuesFull + (i-1)*_ldValues;
+ const T* ptr = (T*) _valuesFull + (i-1)*_ldValues;
+
+ END_OF("MEDARRAY<T>::getRow(const med_int i )");
+ return ptr;
}
+// ------------------
+
+ /*! this method is similar to getRow method./n
+ It returns a pointer to jth line of the array in a MED_NO-INTERLACE representation
+ (for example, 2nd coordinates)./n
+ Be aware : if _mode is MED_FULL_INTERLACE, the entire
+ array will be recalculate in MED_NO_INTERLACE representation./n*/
-template <class T> inline medModeSwitch MEDARRAY<T>::getMode() const
+template <class T> const T* MEDARRAY<T>::getColumn(const med_int j)
{
- return _mode ;
+ BEGIN_OF("MEDARRAY<T>::getColumn(const med_int j)");
+ if ((T*)_valuesDefault == NULL)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getColumn(j) : No values defined !");
+ }
+ if (j<1)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getColumn(j) : argument j must be >= 1");
+ }
+ if (j>_ldValues)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getColumn(j) : argument j must be <= _ldValues");
+ }
+
+ if ((T*)_valuesNo == NULL)
+ {
+ ASSERT(((T*) _valuesDefault)==((T*) _valuesFull));
+ calculateOther();
+ }
+ //PN pour enlever les warning compilateur
+ //const T* ptr = (const T*)_valuesNo + (j-1)*_lengthValues;
+ const T* ptr = ( T*)_valuesNo + (j-1)*_lengthValues;
+
+ return ptr;
}
-template <class T> void MEDARRAY<T>::set(medModeSwitch mode, T* value)
+// ------------------
+
+ /*! returns Jth value of Ith element .\n
+ don't forget first element is element 1 (and not element 0). */
+template <class T> const T MEDARRAY<T>::getIJ(const med_int i,const med_int j) const
{
- BEGIN_OF("MEDARRAY<T>::set(mode,i,value)");
+ if (i<1)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be >= 1");
+ }
+ if (i>_lengthValues)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be <= _lengthValues");
+ }
+ if (j<1)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be >= 1");
+ }
+ if (j>_ldValues)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be <= _ldValues");
+ }
- _mode = mode ;
+ if ( (const T*)_valuesDefault == NULL)
+ {
+ throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : No value in array !");
+ }
- SCRUTE(_ld_values);
- SCRUTE(_length_values);
- SCRUTE(_valuesDefault);
- SCRUTE(_valuesOther);
- SCRUTE(_mode);
- SCRUTE(mode);
- SCRUTE(value);
-
- if (_valuesDefault != NULL)
- if (mode == _mode) {
- _valuesDefault = value ;
- if (_valuesOther != NULL) calculateOther() ;
- // if (_valuesOther != NULL) _valuesOther = value;
+ if (_mode == MED_FULL_INTERLACE)
+ {
+ return _valuesDefault[(i-1)*_ldValues+j-1];
+ }
+ else
+ {
+ return _valuesDefault[(j-1)*_lengthValues+i-1];
+ }
+
+}
+
+// ------------------
+
+ /*! returns the default mode (_mode)/n
+ (internal use : needed by write method) */
+template <class T> inline medModeSwitch MEDARRAY<T>::getMode() const
+{
+ BEGIN_OF("MEDARRAY<T>::getMode()");
+ END_OF("MEDARRAY<T>::getMode()");
+ return _mode;
+}
+
+// ------------------
+
+ /*! sets T pointer of _valuesDefault (cf class PointerOf) on value.\n
+ no copy of value is done. \n
+ the other representation mode is not recalculate.
+ the corresponding pointers are setted to null */
+// template <class T> void MEDARRAY<T>::set(const medModeSwitch mode, const T* value)
+// {
+
+// BEGIN_OF("MEDARRAY<T>::set(mode,value)");
+
+// _mode = mode;
+// if ( _mode == MED_FULL_INTERLACE)
+// {
+// _valuesFull.set(value);
+// _valuesDefault.set((T*)_valuesFull);
+// _valuesNo.set(0);
+// }
+// else
+// {
+// ASSERT (_mode == MED_NO_INTERLACE);
+// _valuesNo.set(value);
+// _valuesDefault.set((T*)_valuesNo);
+// _valuesFull.set(0);
+// }
+// _valuesOther.set(0);
+// END_OF("MEDARRAY<T>::set(mode,i,value)");
+// }
+
+// set with duplication because we don't know were value come and
+// MEDARRAY must have properties on it !!!!
+template <class T> void MEDARRAY<T>::set(const medModeSwitch mode, const T* value)
+{
+ BEGIN_OF("MEDARRAY<T>::set(mode,value)");
+
+ _mode = mode;
+ if ( _mode == MED_FULL_INTERLACE)
+ {
+ _valuesFull.set(_ldValues*_lengthValues,value);
+ _valuesDefault.set((T*)_valuesFull);
+ _valuesNo.set(0);
}
- else {
- allocateOther() ;
- _valuesOther = value ;
- updateDefault() ;
+ else
+ {
+ ASSERT (_mode == MED_NO_INTERLACE);
+ _valuesNo.set(_ldValues*_lengthValues,value);
+ _valuesDefault.set((T*)_valuesNo);
+ _valuesFull.set(0);
}
- else throw MEDEXCEPTION("MEDARRAY::set(mode,value) : No values defined !");
+ _valuesOther.set(0);
END_OF("MEDARRAY<T>::set(mode,i,value)");
}
-template <class T> void MEDARRAY<T>::setI(medModeSwitch mode, med_int i, T* value)
+/*! This function clears the other mode of representation if it exists
+ * It is usefull for people who needs for optimisation reasons to work directly
+ * on the inside array without using set-functions
+ */
+template <class T> void MEDARRAY<T>::clearOtherMode()
{
- BEGIN_OF("MEDARRAY<T>::setI(mode,i,value)");
+ if(isOtherCalculated())
+ {
+ if ( _mode == MED_FULL_INTERLACE)
+ _valuesNo.set(0);
+ else
+ _valuesFull.set(0);
+ _valuesOther.set(0);
+ }
+}
- _mode = mode ;
- SCRUTE(i);
- SCRUTE(_ld_values);
- SCRUTE(_length_values);
- SCRUTE(_valuesDefault);
- SCRUTE(_valuesOther);
- SCRUTE(_mode);
- SCRUTE(mode);
-
- // 1<=i<=_length_values and return an array of _ld_values length
- // if MED_FULL_INTERLACE
- // 1<=i<=_ld_values and return an array of _length_values length
- // if MED_NO_INTERLACE
-
- if (_valuesDefault != NULL) {
- // if mode = MED_FULL_INTERLACE :
- int first = _ld_values ;
- int second = _length_values;
- // else :
- if (mode == MED_NO_INTERLACE) {
- first = _length_values ;
- second = _ld_values ;
- }
- SCRUTE(second);
- if (i<=0)
- throw MEDEXCEPTION("MEDARRAY::setI(mode,i,value) : argument i must be > 0");
- if (i>second) {
- SCRUTE(i);
- SCRUTE(second);
- throw MEDEXCEPTION("MEDARRAY::setI(mode,i,value) : argument i must be <= second");}
- if (mode == _mode)
- {
- // _valuesDefault + (i-1)*first = value ;
- for (int k =0;k<first;k++)
- // _valuesDefault[(i-1)*second+k] = value[k];
- (_valuesDefault + (i-1)*first)[k] = value[k];
- if (_valuesOther != NULL)
- for (int k =0;k<first;k++) {
- _valuesOther[k*second+i-1] = value[k];
- }
- }
- else
- {
- allocateOther() ;
- // return _valuesOther + (i-1)*first = &value ;
- for (int k =0;k<second;k++) {
- _valuesOther[(i-1)*first+k-1] = value[k];
- _valuesDefault[k*second+i-1] = value[k];
- }
- }
+// ------------------
+
+ /*! Sets ith element to T* values\n
+ if they both exist, both _valuesFull and _valuesNo arrays will be updated./n
+ Throws exception if i < 1 or i > _lengthValues */
+template <class T> void MEDARRAY<T>::setI(const med_int i, const T* value)
+{
+ BEGIN_OF("MEDARRAY<T>::setI(i,value)");
+
+ if ((T*)_valuesDefault == NULL)
+ {
+ throw MEDEXCEPTION("MEDARRAY::setI(i,value) : No values defined !");
+ }
+ if (i<=0)
+ {
+ throw MEDEXCEPTION("MEDARRAY::setI(i,value) : argument i must be > 0");
+ }
+ if ( i > _lengthValues)
+ {
+ throw MEDEXCEPTION("MEDARRAY::setI(i,value) : argument i must be <= _lenghtValues");
}
- else throw MEDEXCEPTION("MEDARRAY::setI(mode,i,value) : No values defined !");
- SCRUTE(_valuesDefault);
- SCRUTE( _valuesOther);
+ if ((T*)_valuesFull != NULL)
+ {
+ for (int k = 0;k<_ldValues;k++)
+ {
+ _valuesFull[k+_ldValues*(i-1)] = value[k];
+ }
+ }
- END_OF("MEDARRAY::setI(mode,i,value)");
+ if ((T*)_valuesNo != NULL)
+ {
+ for (int k = 0;k<_ldValues;k++)
+ {
+ _valuesNo[k*_lengthValues +(i-1)] = value[k];
+ }
+ }
+
+ END_OF("MEDARRAY::setI(i,value)");
+}
+// ------------------
+
+ /*! Sets ith element to T* values\n
+ if they both exist, both _valuesFull and _valuesNo arrays will be updated./n
+ Throws exception if i < 1 or i > _lengthValues */
+template <class T> void MEDARRAY<T>::setJ(const med_int j, const T* value)
+{
+ BEGIN_OF("MEDARRAY::setJ(j,value)");
+ if (( T*)_valuesDefault == NULL)
+ {
+ throw MEDEXCEPTION("MEDARRAY::setJ(j) : No values defined !");
+ }
+ if (j<1)
+ {
+ throw MEDEXCEPTION("MEDARRAY::setJ(j) : argument j must be >= 1");
+ }
+ if (j>_ldValues)
+ {
+ throw MEDEXCEPTION("MEDARRAY::setJ(j) : argument j must be <= _ldValues");
+ }
+ if ((T*)_valuesFull != NULL)
+ {
+ for (int k = 0;k<_lengthValues;k++)
+ {
+ _valuesFull[k*_ldValues+(j-1)] = value[k];
+ }
+ }
+
+ if ((T*)_valuesNo != NULL)
+ {
+ for (int k = 0;k<_lengthValues;k++)
+ {
+ _valuesNo[k+_lengthValues*(j-1)] = value[k];
+ }
+ }
+ END_OF("MEDARRAY::setJ(j,value)");
}
-template <class T> void MEDARRAY<T>::setIJ(med_int i, med_int j, T value)
+// ------------------
+
+ /*! Sets value of Jth coordinate of Ith element to T value./n
+ Maintains coherency./n
+ Throws exception if we don't have
+ 1<=i<=_lengthValues and 1<=j<=_ldValues */
+template <class T> void MEDARRAY<T>::setIJ(const med_int i, const med_int j, const T value)
{
- // 1<=i<=_length_values and 1<j<_ld_values
+ // 1<=i<=_lengthValues and 1<=j<=_ldValues
- if (i<1)
+ if (i<1)
throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument i must be >= 1");
- if (i>_length_values)
- throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument i must be <= _length_values");
+ if (i>_lengthValues)
+ throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument i must be <= _lengthValues");
- if (j<1)
+ if (j<1)
throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument j must be >= 1");
- if (j>_ld_values)
- throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument j must be <= _ld_values");
+ if (j>_ldValues)
+ throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument j must be <= _ldValues");
- if (_valuesDefault != NULL) {
- switch (_mode)
- {
- case MED_FULL_INTERLACE :
- {
- _valuesDefault[(i-1)*_ld_values+j-1] = value;
- break;
- }
- case MED_NO_INTERLACE :
- {
- _valuesDefault[(j-1)*_length_values+i-1] = value;
- break;
- }
- }
- } else
+ if ((T*)_valuesDefault == NULL)
+ {
throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : No value in array !");
-}
-
-template <class T> void MEDARRAY<T>::calculateOther()
-{
- if (_valuesDefault != NULL) {
- //we allocate _valuesOther if needed
- if (_valuesOther == NULL) _valuesOther = new T[_ld_values*_length_values] ;
- // we set _valuesOther
- // if mode = MED_FULL_INTERLACE :
- int first = _ld_values ;
- int second = _length_values;
- // else :
- if (_mode == MED_NO_INTERLACE) {
- first = _length_values ;
- second = _ld_values;
- }
+ }
- for (int i=0; i<first;i++)
- for (int j=0; j<second; j++)
- _valuesOther[i*second+j] = _valuesDefault[j*first+i];
+ if ((T*)_valuesFull != NULL)
+ {
+ _valuesFull[j-1+_ldValues*(i-1)] = value;
+ }
+ if ((T*)_valuesNo != NULL)
+ {
+ _valuesNo[(j-1)*_lengthValues+i-1] = value;
}
- else
- throw MEDEXCEPTION("MEDARRAY::calculateOther() : No values defined !");
}
-template <class T> void MEDARRAY<T>::allocateOther()
+ /*! Calculates the other mode of representation : MED_FULL_INTERLACE
+ if __mode = MED_NO_INTERLACE and vice versa./n
+ Throws exception if no value are setted */
+template <class T> void MEDARRAY<T>::calculateOther()
{
- if (_valuesDefault != NULL) {
- if (_valuesOther == NULL) {
- // we set _valuesOther
- _valuesOther = new T[_ld_values*_length_values] ;
- }
+ BEGIN_OF("MEDARRAY<T>::calculateOther()");
+ if ((T*)_valuesDefault == NULL)
+ {
+ throw MEDEXCEPTION("MEDARRAY::calculateOther() : No values defined !");
}
- else
- throw MEDEXCEPTION("MEDARRAY::allocateOther() : No values defined !");
-}
-template <class T> void MEDARRAY<T>::updateDefault()
-{
- BEGIN_OF("MEDARRAY<T>::updateDefault()");
-
- if (_valuesDefault != NULL) {
- if (_valuesOther != NULL) {
- // we update _valuesDefault with _valuesOther
- // if mode = MED_FULL_INTERLACE :
- int first = _ld_values ;
- int second = _length_values;
- // else :
- if (_mode == MED_NO_INTERLACE) {
- first = _length_values ;
- second = _ld_values;
- }
- for (int i=0; i<first;i++)
- for (int j=0; j<second; j++)
- _valuesDefault[j*first+i] = _valuesOther[i*second+j];
- }
- else
- throw MEDEXCEPTION("MEDARRAY<T>::updateDefault() : No valuesOther defined !");
+ if ((T*)_valuesOther == NULL)
+ {
+ _valuesOther.set(_ldValues*_lengthValues);
+ }
+ if (_mode == MED_NO_INTERLACE)
+ {
+ _valuesFull.set((T*)_valuesOther);
}
else
- throw MEDEXCEPTION("MEDARRAY<T>::updateDefault() : No values defined !");
+ {
+ ASSERT( _mode==MED_FULL_INTERLACE);
+ _valuesNo.set((T*)_valuesOther);
+ }
- END_OF("MEDARRAY<T>::updateDefault()");
+ for (int i=0; i<_lengthValues;i++)
+ {
+ for (int j=0; j<_ldValues; j++)
+ {
+ if (_mode == MED_NO_INTERLACE)
+ {
+ _valuesFull[i*_ldValues+j] = _valuesNo[j*_lengthValues+i];
+ }
+ else
+ {
+ _valuesNo[j*_lengthValues+i]=_valuesFull[i*_ldValues+j];
+ }
+ }
+ }
+ END_OF("MEDARRAY<T>::calculateOther()");
}
# endif /* # ifndef __MEDARRAY_H__ */