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