]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_Array.hxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_Array.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #ifndef __MEDARRAY_H__
23 #define __MEDARRAY_H__
24
25 #include "MEDMEM_Exception.hxx"
26 #include "MEDMEM_define.hxx"
27 #include "MEDMEM_PointerOf.hxx"
28 #include "MEDMEM_Utilities.hxx"
29
30 /*!
31   A template class to generate an array of any particular type (int, long,
32   float, double) for our purpose in the MED++ library.\n\n
33
34   Arrays can be stored in MED_FULL_INTERLACE mode (ie : x1,y1,z1,x2,y2,z2...) or
35   in MED_NO_INTERLACE mode ( x1,x2,..xn, y1, y2 ..,yn,z1,...,zn).\n The alternate
36   representation mode is calculate ONLY when it is usefull. We assure coherency
37   for minor data modifications (element, line or column) if you use set methods.
38   But, if you get a pointer and modify the array, no automatical coherency is possible.
39   You can use calculateOther to force a recalculation and insure the coherency.\n
40   No recalculation is done, when the entire array is modified.\n
41   Theses arrays are "Med like" arrays; lower bound equals 1. (first element is element 1,
42   first coordinate is coordinate 1). \n
43
44   Available constructors are :\n
45
46   - default constructor (not very usefull)\n
47   - constructor asking for array dimensions and mode (does memory allocation for you)\n
48   - constructor asking for array dimensions, mode and values (doesn't do memory allocation
49     but copies pointers only.)\n
50   - a copy constructor which copies only pointers.\n
51     (be aware of coherency)
52   - a copy constructor which copies data (deepcopy).\n
53   - assignement operator is also available and only copies pointers (and not data).\n\n
54
55   Attribute "pointers" aren't standard pointers but class PointerOf objects in order to simplify
56   memory management.\n
57
58   A simple test program (testUArray) allows to test this class.
59 */
60
61 namespace MEDMEM {
62 template <class T> class MEDARRAY
63 {
64 private :
65
66                                 /*! leading dimension of value (example : space dimension for coordinates) */
67   int   _ldValues;
68                                 /*! length of values (example : number of nodes for coordinates) */
69   int   _lengthValues;
70                                 /*! data access mode. possible values are :\n
71                                   -  MED_FULL_INTERLACE (default mode) \n
72                                   -  MED_NO_INTERLACE */
73   MED_EN::medModeSwitch _mode;
74                                 /*! Pointer to representation in mode MED_FULL_INTERLACE */
75   PointerOf <T> _valuesFull;
76                                 /*! Pointer to representation in mode MED_NO_INTERLACE */
77   PointerOf <T> _valuesNo;
78                                 /*! Pointer to representation in mode _mode */
79   PointerOf <T> _valuesDefault;
80                                 /*! Pointer to representation in the other mode (!=_mode) */
81   PointerOf <T> _valuesOther;
82
83 public :
84
85   inline  MEDARRAY();
86   inline ~MEDARRAY();
87
88   MEDARRAY  (const int ld_values, const int length_values,
89              const MED_EN::medModeSwitch mode=MED_EN::MED_FULL_INTERLACE);
90   MEDARRAY  (T* values, const int ld_values,
91              const int length_values, const MED_EN::medModeSwitch mode=MED_EN::MED_FULL_INTERLACE,bool shallowCopy=false, bool ownershipOfValues=false);
92   MEDARRAY  (MEDARRAY const &m);
93   MEDARRAY  (MEDARRAY const &m, bool copyOther);
94   MEDARRAY & operator = (const MEDARRAY & m);
95
96   MEDARRAY & shallowCopy(const MEDARRAY & m);
97
98   inline int getLeadingValue() const;
99   inline int getLengthValue()  const;
100
101   const T * get        (const MED_EN::medModeSwitch mode) ;
102   const T * getRow     (const int i) ;
103   const T * getColumn  (const int j) ;
104   const T   getIJ (const int i, const int j) const;
105 //    T * const get        (const MED_EN::medModeSwitch mode) const;
106 //    T * const getRow     (const int i) const;
107 //    T * const getColumn  (const int j) const;
108 //    T   const getIJ (const int i, const int j) const;
109
110   inline MED_EN::medModeSwitch getMode() const;
111
112   void set   (const MED_EN::medModeSwitch mode,const T* value);
113   void setI  (const int i,             const T* value);
114   void setJ  (const int j,             const T* value);
115   void setIJ (const int i, const int j, const T  value);
116
117   void calculateOther();
118   bool isOtherCalculated() const {return (const T*)_valuesOther != NULL;}
119   void clearOtherMode();
120 };
121
122 //-------------------------------------------------//
123 //                                                 //
124 //              IMPLEMENTED CODE                   //
125 //                                                 //
126 //-------------------------------------------------//
127
128
129 template <class T> inline MEDARRAY<T>::MEDARRAY():
130   _ldValues(0), _lengthValues(0), _mode(MED_EN::MED_FULL_INTERLACE),
131   _valuesFull(), _valuesNo(),
132   _valuesDefault(), _valuesOther()
133 {
134 }
135
136 //                              ------------------
137
138 template <class T> inline MEDARRAY<T>::~MEDARRAY()
139 {
140 }
141
142 //                              ------------------
143
144                                 /*! This constructor does allocation and does not set values : \n.
145                                     It allocates a "T" array of length_values*ld_values length.\n
146                                     You don't have to know the T values when calling this construtor
147                                     but you have to call "set" method to initialize them later.
148                                     You also can  get the pointer to the memory zone (with "get" method),
149                                     and work with it.\n
150                                     The desallocation of T array is not your responsability. \n\n
151                                     Throws MEDEXCEPTION if  T array length is < 1*/
152
153 template <class T> MEDARRAY<T>::MEDARRAY(const int ld_values,
154                                          const int length_values,
155                                          const MED_EN::medModeSwitch mode):
156
157                                                 _ldValues(ld_values),
158                                                 _lengthValues(length_values),
159                                                 _mode(mode),
160                                                 _valuesFull(), _valuesNo(),
161                                                 _valuesDefault(),_valuesOther()
162 {
163   //  BEGIN_OF_MED("constructor MEDARRAY<T>::MEDARRAY(const int, const int, const medModeSwitch)");
164
165   // if ld_values < 1 or length_values < 1
166   // throws an exception
167   // Pointers are setted to NULL
168
169   if ((ld_values<1)|(length_values<1))
170   {
171         throw MEDEXCEPTION(LOCALIZED("MEDARRAY<T>::MEDARRAY(const int, const int, const medModeSwitch) : dimension < 1 !"));
172   }
173
174   if ( _mode == MED_EN::MED_FULL_INTERLACE)
175   {
176         _valuesFull.set(length_values*ld_values);
177         _valuesDefault.set((T*) _valuesFull);
178   }
179   else
180   {
181         ASSERT_MED (_mode == MED_EN::MED_NO_INTERLACE);
182         _valuesNo.set(length_values*ld_values);
183         _valuesDefault.set((T*)_valuesNo);
184   }
185
186   ASSERT_MED( (T*)_valuesDefault != NULL);
187 //   SCRUTE_MED((T*)_valuesDefault);
188 //   SCRUTE_MED((T*)_valuesOther);
189 //   SCRUTE_MED((T*)_valuesNo);
190 //   SCRUTE_MED((T*)_valuesFull);
191
192   //  END_OF_MED("constructor MEDARRAY<T>::MEDARRAY(const int, const int, const medModeSwitch ()");
193 }
194
195 //                              ------------------
196
197                                 /*! This constructor duplicate T*values.\n
198
199                                     Throws MEDEXCEPTION if  the lenght of T is < 1*/
200 template <class T> MEDARRAY<T>::MEDARRAY( T*values,
201                                           const int ld_values,
202                                           const int length_values,
203                                           const MED_EN::medModeSwitch mode,
204                                           bool shallowCopy,
205                                           bool ownershipOfValues):
206                                                 _ldValues(ld_values),
207                                                 _lengthValues(length_values),
208                                                 _mode(mode),
209                                                 _valuesFull(),_valuesNo(),
210                                                 _valuesDefault(),_valuesOther()
211 {
212   //  BEGIN_OF_MED("constructor MEDARRAY<T>::MEDARRAY(T* values, const int, const int, const medModeSwitch)");
213
214   // if ld_values < 1 or length_values < 1, we could not allocate
215   // throws an exception
216
217   if ( (ld_values<1) | (length_values<1) )
218   {
219            throw MEDEXCEPTION(LOCALIZED("MEDARRAY<T>::MEDARRAY(T* values, const int, const medModeSwitch) : dimension < 1 !"));
220   }
221   if ( _mode == MED_EN::MED_FULL_INTERLACE)
222   {
223         if(shallowCopy)
224           {
225             if(ownershipOfValues)
226               {
227                 _valuesFull.setShallowAndOwnership((const T*)values);
228             }
229             else
230               {
231                 _valuesFull.set((const T*)values);
232               }
233           }
234         else
235           {
236             _valuesFull.set(_ldValues*length_values,values);
237           }
238         _valuesDefault.set((T*)_valuesFull);
239   }
240   else
241   {
242         ASSERT_MED (_mode == MED_EN::MED_NO_INTERLACE);
243         if(shallowCopy)
244         {
245           if(ownershipOfValues)
246             {
247               _valuesNo.setShallowAndOwnership((const T*)values);
248             }
249           else
250             {
251               _valuesNo.set((const T*)values);
252             }
253         }
254         else
255           _valuesNo.set(_ldValues*length_values,values);
256         _valuesDefault.set((T*)_valuesNo);
257   }
258   ASSERT_MED( (T*)_valuesDefault != NULL);
259 //   SCRUTE_MED((T*)_valuesDefault);
260 //   SCRUTE_MED((T*)_valuesOther);
261 //   SCRUTE_MED((T*)_valuesNo);
262 //   SCRUTE_MED((T*)_valuesFull);
263
264   //  END_OF_MED("constructor MEDARRAY<T>::MEDARRAY(T* values, const int, const int, const medModeSwitch)");
265 }
266
267 //                              ------------------
268
269                                 /*! This constructor allocates a new medarray and does a copy of pointers\n
270                                     It DOES NOT copy the memory . The two objects will share the same data.\n
271                                     (for copying data, use constructor MEDARRAY(MEDARRAY<T> const & m,bool copyOther). */
272 template <class T> MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m ):
273                                         _ldValues(m._ldValues),
274                                         _lengthValues(m._lengthValues),
275                                         _mode(m._mode),
276                                         _valuesFull((const T*)m._valuesFull),
277                                         _valuesNo((const T*)m._valuesNo),
278                                         _valuesDefault((const T*)m._valuesDefault),
279                                         _valuesOther((const T*)m._valuesOther)
280 {
281   //  BEGIN_OF_MED("constructor MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m)");
282   ASSERT_MED( (T*)_valuesDefault != NULL);
283 //   SCRUTE_MED((T*)_valuesDefault);
284 //   SCRUTE_MED((T*)_valuesOther);
285 //   SCRUTE_MED((T*)_valuesNo);
286 //   SCRUTE_MED((T*)_valuesFull);
287   //  END_OF_MED("constructor MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m)");
288 }
289
290                                 /*! This constructor allocates a new array and does a copy of values
291                                     included in the m arrays.\n
292                                     If the boolean is setted to true, both representations (in full mode
293                                     and no interlace mode)  will be copied.\n
294                                     Otherwise, only _valuesDefault will be copied.\n
295                                     Desallocation of the arrays is not your reponsability.\n\n
296                                     Throws MEDEXCEPTION if _valuesOther is null and copyOther equals true*/
297 template <class T> MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & p,bool copyOther ):
298                                         _ldValues(p._ldValues),
299                                         _lengthValues(p._lengthValues),
300                                         _mode(p._mode),
301                                         _valuesFull(),
302                                         _valuesNo(),
303                                         _valuesDefault(),
304                                         _valuesOther()
305 {
306   //  BEGIN_OF_MED("Constructeur deepCopy MEDARRAY<T>::MEDARRAY(MEDARRAY<T> const & m,bool copyOther");
307
308   // PG : Non, s'il n'y a rien, on test et on ne copie rien, c'est tout !
309
310 //    if ( copyOther==true && ((const T*)p._valuesOther==NULL))
311 //      {
312 //        throw MEDEXCEPTION("MEDARRAY MEDARRAY const &m,bool copyOther : No Other values defined and bool = true !");
313 //      }
314
315   if ( _mode == MED_EN::MED_FULL_INTERLACE)
316     {
317       _valuesFull.set(p._ldValues*p._lengthValues,(const T*)p._valuesFull);
318       _valuesDefault.set((T*)_valuesFull);
319       if (copyOther)
320         if ((const T*)p._valuesNo != NULL)
321           {
322             _valuesNo.set(p._ldValues*p._lengthValues,(const T*)p._valuesNo);
323             _valuesOther.set((T*)_valuesNo);
324           }
325     }
326   else
327     {
328       ASSERT_MED (_mode == MED_EN::MED_NO_INTERLACE);
329       _valuesNo.set(p._ldValues*p._lengthValues,(const T*)p._valuesNo);
330       _valuesDefault.set((T*)_valuesNo);
331       if (copyOther)
332         if ((const T*)p._valuesFull != NULL)
333           {
334             _valuesFull.set(p._ldValues*p._lengthValues,(const T*)p._valuesFull);
335             _valuesOther.set((T*)_valuesFull);
336           }
337     }
338 }
339
340 //                              ------------------
341
342 /*! This operator makes a deep copy of the arrays.\n */
343
344 template <class T> MEDARRAY<T> & MEDARRAY<T>::operator = (const MEDARRAY & m)
345 {
346
347   //  BEGIN_OF_MED("Operator = MEDARRAY<T>");
348
349   _ldValues=m._ldValues;
350   _lengthValues=m._lengthValues;
351   _mode=m._mode;
352
353   //  SCRUTE_MED(_mode);
354
355   if ((const T*) m._valuesFull !=NULL)
356     _valuesFull.set(_ldValues*_lengthValues,(const T*) m._valuesFull);
357
358   if ((const T*) m._valuesNo !=NULL)
359     _valuesNo.set(_ldValues*_lengthValues,(const T*) m._valuesNo);
360
361   if (_mode == MED_EN::MED_FULL_INTERLACE) {
362     //PN : pour enlever les warning compilateur
363     //_valuesDefault.set((const T*) _valuesFull);
364     //_valuesOther.set((const T*) _valuesNo);
365     _valuesDefault.set((T*) _valuesFull);
366     _valuesOther.set((T*) _valuesNo);
367   } else {
368     ASSERT_MED (_mode == MED_EN::MED_NO_INTERLACE);
369     //PN : pour enlever les warning compilateur
370     //_valuesDefault.set((const T*) _valuesNo);
371     //_valuesOther.set((const T*) _valuesFull);
372     _valuesDefault.set((T*) _valuesNo);
373     _valuesOther.set((T*) _valuesFull);
374   }
375
376 //   SCRUTE_MED((T*)_valuesDefault);
377 //   SCRUTE_MED((T*)_valuesOther);
378 //   SCRUTE_MED((T*)_valuesNo);
379 //   SCRUTE_MED((T*)_valuesFull);
380
381   //  END_OF_MED("Operator = MEDARRAY<T>");
382   return *this;
383 }
384
385 /*! Idem operator= but performs only shallow copy (just copy of pointers) of arrays contains in _valuesFull and _valuesNo \n
386  WARNING the MEDARRAY returned HAS THE OWNERSHIP OF THE ARRAY !!!! */
387
388 template <class T> MEDARRAY<T> & MEDARRAY<T>::shallowCopy(const MEDARRAY & m)
389 {
390   _ldValues=m._ldValues;
391   _lengthValues=m._lengthValues;
392   _mode=m._mode;
393   if ((const T*) m._valuesFull !=NULL)
394     _valuesFull.setShallowAndOwnership((const T*) m._valuesFull);
395   if ((const T*) m._valuesNo !=NULL)
396     _valuesNo.setShallowAndOwnership((const T*) m._valuesNo);
397   if (_mode == MED_EN::MED_FULL_INTERLACE) {
398     _valuesDefault.set((T*) _valuesFull);
399     _valuesOther.set((T*) _valuesNo);
400   } else {
401     _valuesDefault.set((T*) _valuesNo);
402     _valuesOther.set((T*) _valuesFull);
403   }
404   return *this;
405 }
406
407 //                              ------------------
408
409                                 /*! returns _ldValues. (for example, space dimension for coordinates array)*/
410 template <class T> inline int MEDARRAY<T>::getLeadingValue() const
411 {
412   return _ldValues;
413 }
414
415 //                              ------------------
416
417                                 /*! returns _ldValues. (for example, number of nodes for coordinates array)*/
418 template <class T> inline int MEDARRAY<T>::getLengthValue() const
419 {
420   return _lengthValues;
421 }
422
423 //                              ------------------
424
425                                 /*! returns a pointer to _valuesDefault or _valuesOther, depending on
426                                     mode value : if mode is the same as _mode, _valuesDefault is returned.
427                                     else, if _valuesOther is calculated (if necessary) and then returned.
428                                     The pointer can be used to set values */
429 template <class T> const T* MEDARRAY<T>::get(const MED_EN::medModeSwitch mode)
430 {
431   //  BEGIN_OF_MED("MEDARRAY<T>::get(const medModeSwitch mode)");
432   if ((T*)_valuesDefault == NULL)
433   {
434         throw MEDEXCEPTION("MEDARRAY::get(mode) : No values defined !");
435   }
436   if (mode == _mode)
437   {
438         //PN : pour enlever les warning compilateurs
439         //return (const T*)_valuesDefault;
440         return  (T*) _valuesDefault;
441   }
442   else
443   {
444         if ((T*)_valuesOther == NULL)
445         {
446                 calculateOther();
447         }
448         //PN : pour enlever les warning compilateurs
449         //return (const T*)_valuesDefault;
450         return  (T*) _valuesOther;
451   }
452 }
453
454 //                              ------------------
455
456                                 /*! returns a pointer to ith element of the array.
457                                     (ith line in a MED_FULL_INTERLACE representation )\n
458                                     Be aware : if _mode is MED_NO_INTERLACE, the entire
459                                     array will be recalculate in MED_FULL_INTERLACE representation.\n*/
460
461 template <class T> const T* MEDARRAY<T>::getRow(const int i)
462 {
463   if ((T*)_valuesDefault == NULL)
464   {
465         throw MEDEXCEPTION("MEDARRAY::getRow(i) : No values defined !");
466   }
467   if (i<1)
468   {
469         throw MEDEXCEPTION("MEDARRAY::getRow(i) : argument i must be >= 1");
470   }
471   if (i>_lengthValues)
472   {
473         throw MEDEXCEPTION("MEDARRAY::getRow(i) : argument i must be <= _lengthValues");
474   }
475
476   if ((T*)_valuesFull == NULL)
477   {
478         calculateOther();
479   }
480   ASSERT_MED((T*)_valuesFull != NULL);
481
482   // PN pour enlever les warning compilateurs
483   //const T* ptr = (const T*)_valuesFull + (i-1)*_ldValues;
484   const T* ptr =  (T*) _valuesFull + (i-1)*_ldValues;
485
486   return ptr;
487 }
488 //                              ------------------
489
490                                 /*! this method is similar to getRow method.\n
491                                     It returns a pointer to jth line of the array in a MED_NO-INTERLACE representation
492                                     (for example, 2nd coordinates).\n
493                                     Be aware : if _mode is MED_FULL_INTERLACE, the entire
494                                     array will be recalculate in MED_NO_INTERLACE representation.\n*/
495
496 template <class T> const T* MEDARRAY<T>::getColumn(const int j)
497 {
498   if ((T*)_valuesDefault == NULL)
499   {
500         throw MEDEXCEPTION("MEDARRAY::getColumn(j) : No values defined !");
501   }
502   if (j<1)
503   {
504         throw MEDEXCEPTION("MEDARRAY::getColumn(j) : argument j must be >= 1");
505   }
506   if (j>_ldValues)
507   {
508         throw MEDEXCEPTION("MEDARRAY::getColumn(j) : argument j must be <= _ldValues");
509   }
510
511   if ((T*)_valuesNo == NULL)
512   {
513         ASSERT_MED(((T*) _valuesDefault)==((T*) _valuesFull));
514         calculateOther();
515   }
516   //PN pour enlever les warning compilateur
517   //const T* ptr = (const T*)_valuesNo + (j-1)*_lengthValues;
518   const T* ptr = ( T*)_valuesNo + (j-1)*_lengthValues;
519
520   return ptr;
521 }
522
523 //                              ------------------
524
525                                 /*! returns Jth value of Ith element .\n
526                                     don't forget first element is element 1 (and not element 0). */
527 template <class T> const T MEDARRAY<T>::getIJ(const int i,const  int j) const
528 {
529
530   if (i<1)
531   {
532         throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be >= 1");
533   }
534   if (i>_lengthValues)
535   {
536         throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument i must be <= _lengthValues");
537   }
538   if (j<1)
539   {
540         throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be >= 1");
541   }
542   if (j>_ldValues)
543   {
544         throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : argument j must be <= _ldValues");
545   }
546
547   if ( (const T*)_valuesDefault ==  NULL)
548   {
549         throw MEDEXCEPTION("MEDARRAY::getIJ(i,j) : No value in array !");
550   }
551
552   if (_mode == MED_EN::MED_FULL_INTERLACE)
553   {
554         return _valuesDefault[(i-1)*_ldValues+j-1];
555   }
556   else
557   {
558         return _valuesDefault[(j-1)*_lengthValues+i-1];
559   }
560
561 }
562
563 //                              ------------------
564
565                                 /*! returns the default mode (_mode)\n
566                                     (internal use : needed by write method) */
567 template <class T> inline MED_EN::medModeSwitch MEDARRAY<T>::getMode() const
568 {
569   return _mode;
570 }
571
572 //                              ------------------
573
574                                 /*! sets T pointer of _valuesDefault (cf class PointerOf) on value.\n
575                                     no copy of value is done. \n
576                                     the other representation mode is not recalculate.
577                                     the corresponding pointers are setted to null */
578 //  template <class T> void MEDARRAY<T>::set(const medModeSwitch mode, const T* value)
579 //  {
580
581 //    BEGIN_OF_MED("MEDARRAY<T>::set(mode,value)");
582
583 //    _mode = mode;
584 //    if ( _mode == MED_FULL_INTERLACE)
585 //    {
586 //      _valuesFull.set(value);
587 //      _valuesDefault.set((T*)_valuesFull);
588 //      _valuesNo.set(0);
589 //    }
590 //    else
591 //    {
592 //      ASSERT_MED (_mode == MED_NO_INTERLACE);
593 //      _valuesNo.set(value);
594 //      _valuesDefault.set((T*)_valuesNo);
595 //      _valuesFull.set(0);
596 //    }
597 //    _valuesOther.set(0);
598 //    END_OF_MED("MEDARRAY<T>::set(mode,i,value)");
599 //  }
600
601 // set with duplication because we don't know were value come and
602 // MEDARRAY must have properties on it !!!!
603 template <class T> void MEDARRAY<T>::set(const MED_EN::medModeSwitch mode, const T* value)
604 {
605   //  BEGIN_OF_MED("MEDARRAY<T>::set(mode,value)");
606
607   _mode = mode;
608   if ( _mode == MED_EN::MED_FULL_INTERLACE)
609     {
610       _valuesFull.set(_ldValues*_lengthValues,value);
611       _valuesDefault.set((T*)_valuesFull);
612       _valuesNo.set(0);
613     }
614   else
615     {
616       ASSERT_MED (_mode == MED_EN::MED_NO_INTERLACE);
617       _valuesNo.set(_ldValues*_lengthValues,value);
618       _valuesDefault.set((T*)_valuesNo);
619       _valuesFull.set(0);
620     }
621   _valuesOther.set(0);
622
623   //  END_OF_MED("MEDARRAY<T>::set(mode,i,value)");
624 }
625
626 /*! This function clears the other mode of representation if it exists
627  *  It is usefull for people who needs for optimisation reasons to work directly
628  *  on the inside array without using set-functions
629  */
630 template <class T> void MEDARRAY<T>::clearOtherMode()
631 {
632     if(isOtherCalculated())
633     {
634         if ( _mode == MED_EN::MED_FULL_INTERLACE)
635             _valuesNo.set(0);
636         else
637             _valuesFull.set(0);
638         _valuesOther.set(0);
639     }
640 }
641
642
643 //                              ------------------
644
645                                         /*! Sets ith element to T* values\n
646                                             if they both exist, both _valuesFull and _valuesNo arrays will be updated.\n
647                                             Throws exception if i < 1 or i > _lengthValues */
648 template <class T> void MEDARRAY<T>::setI(const int i, const T* value)
649 {
650   //  BEGIN_OF_MED("MEDARRAY<T>::setI(i,value)");
651
652   if ((T*)_valuesDefault == NULL)
653   {
654         throw MEDEXCEPTION("MEDARRAY::setI(i,value) : No values defined !");
655   }
656   if (i<=0)
657   {
658       throw MEDEXCEPTION("MEDARRAY::setI(i,value) : argument i must be > 0");
659   }
660   if ( i > _lengthValues)
661   {
662      throw MEDEXCEPTION("MEDARRAY::setI(i,value) : argument i must be <= _lenghtValues");
663   }
664
665   if ((T*)_valuesFull != NULL)
666   {
667    for (int k = 0;k<_ldValues;k++)
668    {
669                 _valuesFull[k+_ldValues*(i-1)] = value[k];
670    }
671   }
672
673   if ((T*)_valuesNo != NULL)
674   {
675    for (int k = 0;k<_ldValues;k++)
676    {
677                 _valuesNo[k*_lengthValues +(i-1)] = value[k];
678    }
679   }
680
681   //  END_OF_MED("MEDARRAY::setI(i,value)");
682 }
683 //                              ------------------
684
685                                         /*! Sets ith element to T* values\n
686                                             if they both exist, both _valuesFull and _valuesNo arrays will be updated.\n
687                                             Throws exception if i < 1 or i > _lengthValues */
688 template <class T> void MEDARRAY<T>::setJ(const int j, const T* value)
689 {
690   //  BEGIN_OF_MED("MEDARRAY::setJ(j,value)");
691   if (( T*)_valuesDefault == NULL)
692   {
693         throw MEDEXCEPTION("MEDARRAY::setJ(j) : No values defined !");
694   }
695   if (j<1)
696   {
697         throw MEDEXCEPTION("MEDARRAY::setJ(j) : argument j must be >= 1");
698   }
699   if (j>_ldValues)
700   {
701         throw MEDEXCEPTION("MEDARRAY::setJ(j) : argument j must be <= _ldValues");
702   }
703   if ((T*)_valuesFull != NULL)
704   {
705    for (int k = 0;k<_lengthValues;k++)
706    {
707                 _valuesFull[k*_ldValues+(j-1)] = value[k];
708    }
709   }
710
711   if ((T*)_valuesNo != NULL)
712   {
713    for (int k = 0;k<_lengthValues;k++)
714    {
715                 _valuesNo[k+_lengthValues*(j-1)] = value[k];
716    }
717   }
718   //  END_OF_MED("MEDARRAY::setJ(j,value)");
719 }
720
721 //                              ------------------
722
723                                         /*! Sets value of Jth coordinate of Ith element to T value.\n
724                                             Maintains coherency.\n
725                                             Throws exception if we don't have
726                                             1<=i<=_lengthValues and 1<=j<=_ldValues */
727 template <class T> void MEDARRAY<T>::setIJ(const int i, const int j, const T value)
728 {
729   // 1<=i<=_lengthValues and 1<=j<=_ldValues
730
731   if (i<1)
732     throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument i must be >= 1");
733   if (i>_lengthValues)
734     throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument i must be <= _lengthValues");
735
736   if (j<1)
737     throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument j must be >= 1");
738   if (j>_ldValues)
739     throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : argument j must be <= _ldValues");
740
741   if ((T*)_valuesDefault == NULL)
742   {
743     throw MEDEXCEPTION("MEDARRAY::setIJ(i,j,value) : No value in array !");
744   }
745
746   if ((T*)_valuesFull != NULL)
747   {
748         _valuesFull[j-1+_ldValues*(i-1)] = value;
749   }
750   if ((T*)_valuesNo != NULL)
751   {
752         _valuesNo[(j-1)*_lengthValues+i-1] = value;
753   }
754 }
755
756                                         /*! Calculates the other mode of representation : MED_FULL_INTERLACE
757                                             if __mode = MED_NO_INTERLACE and vice versa.\n
758                                             Throws exception if no value are setted */
759 template <class T> void MEDARRAY<T>::calculateOther()
760 {
761   //  BEGIN_OF_MED("MEDARRAY<T>::calculateOther()");
762   if ((T*)_valuesDefault == NULL)
763   {
764         throw MEDEXCEPTION("MEDARRAY::calculateOther() : No values defined !");
765   }
766
767   if ((T*)_valuesOther == NULL)
768   {
769         _valuesOther.set(_ldValues*_lengthValues);
770   }
771   if (_mode == MED_EN::MED_NO_INTERLACE)
772   {
773         _valuesFull.set((T*)_valuesOther);
774   }
775   else
776   {
777         ASSERT_MED( _mode==MED_EN::MED_FULL_INTERLACE);
778         _valuesNo.set((T*)_valuesOther);
779   }
780
781   for (int i=0; i<_lengthValues;i++)
782   {
783         for (int j=0; j<_ldValues; j++)
784         {
785                 if (_mode == MED_EN::MED_NO_INTERLACE)
786                 {
787                         _valuesFull[i*_ldValues+j] = _valuesNo[j*_lengthValues+i];
788                 }
789                 else
790                 {
791                         _valuesNo[j*_lengthValues+i]=_valuesFull[i*_ldValues+j];
792                 }
793         }
794   }
795   //  END_OF_MED("MEDARRAY<T>::calculateOther()");
796 }
797
798 } //End of namespace MEDMEM
799
800 # endif         /* # ifndef __MEDARRAY_H__ */