Salome HOME
86dc8a297a987ea75725bbaabd2b98af5efdfbb7
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArray.txx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #ifndef __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
22 #define __PARAMEDMEM_MEDCOUPLINGMEMARRAY_TXX__
23
24 #include "MEDCouplingMemArray.hxx"
25 #include "NormalizedUnstructuredMesh.hxx"
26 #include "InterpKernelException.hxx"
27 #include "InterpolationUtils.hxx"
28 #include "MEDCouplingPartDefinition.hxx"
29 #include "InterpKernelAutoPtr.hxx"
30 #include "MCAuto.hxx"
31
32 #include <sstream>
33 #include <cstdlib>
34 #include <numeric>
35 #include <algorithm>
36
37 namespace MEDCoupling
38 {
39   template<class T>
40   void MEDCouplingPointer<T>::setInternal(T *pointer)
41   {
42     _internal=pointer;
43     _external=0;
44   }
45
46   template<class T>
47   void MEDCouplingPointer<T>::setExternal(const T *pointer)
48   {
49     _external=pointer;
50     _internal=0;
51   }
52
53   template<class T>
54   MemArray<T>::MemArray(const MemArray<T>& other):_nb_of_elem(0),_nb_of_elem_alloc(0),_ownership(false),_dealloc(0),_param_for_deallocator(0)
55   {
56     if(!other._pointer.isNull())
57       {
58         _nb_of_elem_alloc=other._nb_of_elem;
59         T *pointer=(T*)malloc(_nb_of_elem_alloc*sizeof(T));
60         std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+other._nb_of_elem,pointer);
61         useArray(pointer,true,C_DEALLOC,other._nb_of_elem);
62       }
63   }
64
65   template<class T>
66   void MemArray<T>::useArray(const T *array, bool ownership, DeallocType type, std::size_t nbOfElem)
67   {
68     destroy();
69     _nb_of_elem=nbOfElem;
70     _nb_of_elem_alloc=nbOfElem;
71     if(ownership)
72       _pointer.setInternal(const_cast<T *>(array));
73     else
74       _pointer.setExternal(array);
75     _ownership=ownership;
76     _dealloc=BuildFromType(type);
77   }
78
79   template<class T>
80   void MemArray<T>::useExternalArrayWithRWAccess(const T *array, std::size_t nbOfElem)
81   {
82     destroy();
83     _nb_of_elem=nbOfElem;
84     _nb_of_elem_alloc=nbOfElem;
85     _pointer.setInternal(const_cast<T *>(array));
86     _ownership=false;
87     _dealloc=CPPDeallocator;
88   }
89
90   template<class T>
91   void MemArray<T>::writeOnPlace(std::size_t id, T element0, const T *others, std::size_t sizeOfOthers)
92   {
93     if(id+sizeOfOthers>=_nb_of_elem_alloc)
94       reserve(2*_nb_of_elem+sizeOfOthers+1);
95     T *pointer=_pointer.getPointer();
96     pointer[id]=element0;
97     std::copy(others,others+sizeOfOthers,pointer+id+1);
98     _nb_of_elem=std::max<std::size_t>(_nb_of_elem,id+sizeOfOthers+1);
99   }
100
101   template<class T>
102   void MemArray<T>::pushBack(T elem)
103   {
104     if(_nb_of_elem>=_nb_of_elem_alloc)
105       reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1);
106     T *pt=getPointer();
107     pt[_nb_of_elem++]=elem;
108   }
109
110   template<class T>
111   T MemArray<T>::popBack()
112   {
113     if(_nb_of_elem>0)
114       {
115         const T *pt=getConstPointer();
116         return pt[--_nb_of_elem];
117       }
118     throw INTERP_KERNEL::Exception("MemArray::popBack : nothing to pop in array !");
119   }
120
121   template<class T>
122   void MemArray<T>::pack() const
123   {
124     (const_cast<MemArray<T> * >(this))->reserve(_nb_of_elem);
125   }
126
127   template<class T>
128   bool MemArray<T>::isEqual(const MemArray<T>& other, T prec, std::string& reason) const
129   {
130     std::ostringstream oss; oss.precision(15);
131     if(_nb_of_elem!=other._nb_of_elem)
132       {
133         oss << "Number of elements in coarse data of DataArray mismatch : this=" << _nb_of_elem << " other=" << other._nb_of_elem;
134         reason=oss.str();
135         return false;
136       }
137     const T *pt1=_pointer.getConstPointer();
138     const T *pt2=other._pointer.getConstPointer();
139     if(pt1==0 && pt2==0)
140       return true;
141     if(pt1==0 || pt2==0)
142       {
143         oss << "coarse data pointer is defined for only one DataArray instance !";
144         reason=oss.str();
145         return false;
146       }
147     if(pt1==pt2)
148       return true;
149     for(std::size_t i=0;i<_nb_of_elem;i++)
150       if(pt1[i]-pt2[i]<-prec || (pt1[i]-pt2[i])>prec)
151         {
152           oss << "The content of data differs at pos #" << i << " of coarse data ! this[i]=" << pt1[i] << " other[i]=" << pt2[i];
153           reason=oss.str();
154           return false;
155         }
156     return true;
157   }
158
159   /*!
160    * \param [in] sl is typically the number of components
161    * \return True if a not null pointer is present, False if not.
162    */
163   template<class T>
164   bool MemArray<T>::reprHeader(int sl, std::ostream& stream) const
165   {
166     stream << "Number of tuples : ";
167     if(!_pointer.isNull())
168       {
169         if(sl!=0)
170           stream << _nb_of_elem/sl << std::endl << "Internal memory facts : " << _nb_of_elem << "/" << _nb_of_elem_alloc;
171         else
172           stream << "Empty Data";
173       }
174     else
175       stream << "No data";
176     stream << "\n";
177     stream << "Data content :\n";
178     bool ret=!_pointer.isNull();
179     if(!ret)
180       stream << "No data !\n";
181     return ret;
182   }
183
184   /*!
185    * \param [in] sl is typically the number of components
186    */
187   template<class T>
188   void MemArray<T>::repr(int sl, std::ostream& stream) const
189   {
190     if(reprHeader(sl,stream))
191       {
192         const T *data=getConstPointer();
193         if(_nb_of_elem!=0 && sl!=0)
194           {
195             std::size_t nbOfTuples=_nb_of_elem/std::abs(sl);
196             for(std::size_t i=0;i<nbOfTuples;i++)
197               {
198                 stream << "Tuple #" << i << " : ";
199                 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
200                 stream << "\n";
201                 data+=sl;
202               }
203           }
204         else
205           stream << "Empty Data\n";
206       }
207   }
208
209   /*!
210    * \param [in] sl is typically the number of components
211    */
212   template<class T>
213   void MemArray<T>::reprZip(int sl, std::ostream& stream) const
214   {
215     stream << "Number of tuples : ";
216     if(!_pointer.isNull())
217       {
218         if(sl!=0)
219           stream << _nb_of_elem/sl;
220         else
221           stream << "Empty Data";
222       }
223     else
224       stream << "No data";
225     stream << "\n";
226     stream << "Data content : ";
227     const T *data=getConstPointer();
228     if(!_pointer.isNull())
229       {
230         if(_nb_of_elem!=0 && sl!=0)
231           {
232             std::size_t nbOfTuples=_nb_of_elem/std::abs(sl);
233             for(std::size_t i=0;i<nbOfTuples;i++)
234               {
235                 stream << "|";
236                 std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
237                 stream << "| ";
238                 data+=sl;
239               }
240             stream << "\n";
241           }
242         else
243           stream << "Empty Data\n";
244       }
245     else
246       stream << "No data !\n";
247   }
248
249   /*!
250    * \param [in] sl is typically the number of components
251    */
252   template<class T>
253   void MemArray<T>::reprNotTooLong(int sl, std::ostream& stream) const
254   {
255     if(reprHeader(sl,stream))
256       {
257         const T *data=getConstPointer();
258         if(_nb_of_elem!=0 && sl!=0)
259           {
260             std::size_t nbOfTuples=_nb_of_elem/std::abs(sl);
261             if(nbOfTuples<=1000)
262               {
263                 for(std::size_t i=0;i<nbOfTuples;i++)
264                   {
265                     stream << "Tuple #" << i << " : ";
266                     std::copy(data,data+sl,std::ostream_iterator<T>(stream," "));
267                     stream << "\n";
268                     data+=sl;
269                   }
270               }
271             else
272               {// too much tuples -> print the 3 first tuples and 3 last.
273                 stream << "Tuple #0 : ";
274                 std::copy(data,data+sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
275                 stream << "Tuple #1 : ";
276                 std::copy(data+sl,data+2*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
277                 stream << "Tuple #2 : ";
278                 std::copy(data+2*sl,data+3*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
279                 stream << "...\n";
280                 stream << "Tuple #" << nbOfTuples-3 << " : ";
281                 std::copy(data+(nbOfTuples-3)*sl,data+(nbOfTuples-2)*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
282                 stream << "Tuple #" << nbOfTuples-2 << " : ";
283                 std::copy(data+(nbOfTuples-2)*sl,data+(nbOfTuples-1)*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
284                 stream << "Tuple #" << nbOfTuples-1 << " : ";
285                 std::copy(data+(nbOfTuples-1)*sl,data+nbOfTuples*sl,std::ostream_iterator<T>(stream," ")); stream << "\n";
286               }
287           }
288         else
289           stream << "Empty Data\n";
290       }
291   }
292
293   template<class T>
294   void MemArray<T>::fillWithValue(const T& val)
295   {
296     T *pt=_pointer.getPointer();
297     std::fill(pt,pt+_nb_of_elem,val);
298   }
299
300   template<class T>
301   T *MemArray<T>::fromNoInterlace(int nbOfComp) const
302   {
303     if(nbOfComp<1)
304       throw INTERP_KERNEL::Exception("MemArray<T>::fromNoInterlace : number of components must be > 0 !");
305     const T *pt=_pointer.getConstPointer();
306     std::size_t nbOfTuples=_nb_of_elem/nbOfComp;
307     T *ret=(T*)malloc(_nb_of_elem*sizeof(T));
308     T *w=ret;
309     for(std::size_t i=0;i<nbOfTuples;i++)
310       for(int j=0;j<nbOfComp;j++,w++)
311         *w=pt[j*nbOfTuples+i];
312     return ret;
313   }
314
315   template<class T>
316   T *MemArray<T>::toNoInterlace(int nbOfComp) const
317   {
318     if(nbOfComp<1)
319       throw INTERP_KERNEL::Exception("MemArray<T>::toNoInterlace : number of components must be > 0 !");
320     const T *pt=_pointer.getConstPointer();
321     std::size_t nbOfTuples=_nb_of_elem/nbOfComp;
322     T *ret=(T*)malloc(_nb_of_elem*sizeof(T));
323     T *w=ret;
324     for(int i=0;i<nbOfComp;i++)
325       for(std::size_t j=0;j<nbOfTuples;j++,w++)
326         *w=pt[j*nbOfComp+i];
327     return ret;
328   }
329
330   template<class T>
331   void MemArray<T>::sort(bool asc)
332   {
333     T *pt=_pointer.getPointer();
334     if(asc)
335       std::sort(pt,pt+_nb_of_elem);
336     else
337       {
338         typename std::reverse_iterator<T *> it1(pt+_nb_of_elem);
339         typename std::reverse_iterator<T *> it2(pt);
340         std::sort(it1,it2);
341       }
342   }
343
344   template<class T>
345   void MemArray<T>::reverse(int nbOfComp)
346   {
347     if(nbOfComp<1)
348       throw INTERP_KERNEL::Exception("MemArray<T>::reverse : only supported with 'this' array with ONE or more than ONE component !");
349     T *pt=_pointer.getPointer();
350     if(nbOfComp==1)
351       {
352         std::reverse(pt,pt+_nb_of_elem);
353         return ;
354       }
355     else
356       {
357         T *pt2=pt+_nb_of_elem-nbOfComp;
358         std::size_t nbOfTuples=_nb_of_elem/nbOfComp;
359         for(std::size_t i=0;i<nbOfTuples/2;i++,pt+=nbOfComp,pt2-=nbOfComp)
360           {
361             for(int j=0;j<nbOfComp;j++)
362               std::swap(pt[j],pt2[j]);
363           }
364       }
365   }
366
367   template<class T>
368   void MemArray<T>::alloc(std::size_t nbOfElements)
369   {
370     destroy();
371     _nb_of_elem=nbOfElements;
372     _nb_of_elem_alloc=nbOfElements;
373     _pointer.setInternal((T*)malloc(_nb_of_elem_alloc*sizeof(T)));
374     _ownership=true;
375     _dealloc=CDeallocator;
376   }
377
378   /*!
379    * This method performs systematically an allocation of \a newNbOfElements elements in \a this.
380    * \a _nb_of_elem and \a _nb_of_elem_alloc will \b NOT be systematically equal (contrary to MemArray<T>::reAlloc method.
381    * So after the call of this method \a _nb_of_elem will be equal tostd::min<std::size_t>(_nb_of_elem,newNbOfElements) and \a _nb_of_elem_alloc equal to 
382    * \a newNbOfElements. This method is typically used to perform a pushBack to avoid systematic allocations-copy-deallocation.
383    * So after the call of this method the accessible content is perfectly set.
384    * 
385    * So this method should not be confused with MemArray<T>::reserve that is close to MemArray<T>::reAlloc but not same.
386    */
387   template<class T>
388   void MemArray<T>::reserve(std::size_t newNbOfElements)
389   {
390     if(_nb_of_elem_alloc==newNbOfElements)
391       return ;
392     T *pointer=(T*)malloc(newNbOfElements*sizeof(T));
393     std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<std::size_t>(_nb_of_elem,newNbOfElements),pointer);
394     if(_ownership)
395       DestroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external
396     _pointer.setInternal(pointer);
397     _nb_of_elem=std::min<std::size_t>(_nb_of_elem,newNbOfElements);
398     _nb_of_elem_alloc=newNbOfElements;
399     _ownership=true;
400     _dealloc=CDeallocator;
401     _param_for_deallocator=0;
402   }
403
404   /*!
405    * This method performs systematically an allocation of \a newNbOfElements elements in \a this.
406    * \a _nb_of_elem and \a _nb_of_elem_alloc will be equal even if only std::min<std::size_t>(_nb_of_elem,newNbOfElements) come from the .
407    * The remaing part of the new allocated chunk are available but not set previouly !
408    * 
409    * So this method should not be confused with MemArray<T>::reserve that is close to MemArray<T>::reAlloc but not same.
410    */
411   template<class T>
412   void MemArray<T>::reAlloc(std::size_t newNbOfElements)
413   {
414     if(_nb_of_elem==newNbOfElements)
415       return ;
416     T *pointer=(T*)malloc(newNbOfElements*sizeof(T));
417     std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min<std::size_t>(_nb_of_elem,newNbOfElements),pointer);
418     if(_ownership)
419       DestroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external
420     _pointer.setInternal(pointer);
421     _nb_of_elem=newNbOfElements;
422     _nb_of_elem_alloc=newNbOfElements;
423     _ownership=true;
424     _dealloc=CDeallocator;
425     _param_for_deallocator=0;
426   }
427
428   template<class T>
429   void MemArray<T>::CPPDeallocator(void *pt, void *param)
430   {
431     delete [] reinterpret_cast<T*>(pt);
432   }
433
434   template<class T>
435   void MemArray<T>::CDeallocator(void *pt, void *param)
436   {
437     free(pt);
438   }
439
440   template<class T>
441   typename MemArray<T>::Deallocator MemArray<T>::BuildFromType(DeallocType type)
442   {
443     switch(type)
444     {
445       case CPP_DEALLOC:
446         return CPPDeallocator;
447       case C_DEALLOC:
448         return CDeallocator;
449       default:
450         throw INTERP_KERNEL::Exception("Invalid deallocation requested ! Unrecognized enum DeallocType !");
451     }
452   }
453
454   template<class T>
455   void MemArray<T>::DestroyPointer(T *pt, typename MemArray<T>::Deallocator dealloc, void *param)
456   {
457     if(dealloc)
458       dealloc(pt,param);
459   }
460
461   template<class T>
462   void MemArray<T>::destroy()
463   {
464     if(_ownership)
465       DestroyPointer(const_cast<T *>(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external
466     _pointer.null();
467     _ownership=false;
468     _dealloc=NULL;
469     _param_for_deallocator=NULL;
470     _nb_of_elem=0;
471     _nb_of_elem_alloc=0;
472   }
473
474   template<class T>
475   MemArray<T> &MemArray<T>::operator=(const MemArray<T>& other)
476   {
477     alloc(other._nb_of_elem);
478     std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+_nb_of_elem,_pointer.getPointer());
479     return *this;
480   }
481
482   //////////////////////////////////
483
484   template<class T>
485   DataArrayIterator<T>::DataArrayIterator(typename Traits<T>::ArrayType *da):_da(da),_tuple_id(0),_nb_comp(0),_nb_tuple(0)
486   {
487     if(_da)
488       {
489         _da->incrRef();
490         if(_da->isAllocated())
491           {
492             _nb_comp=da->getNumberOfComponents();
493             _nb_tuple=da->getNumberOfTuples();
494             _pt=da->getPointer();
495           }
496       }
497   }
498   
499   template<class T>
500   DataArrayIterator<T>::~DataArrayIterator()
501   {
502     if(_da)
503       _da->decrRef();
504   }
505
506   template<class T>
507   typename Traits<T>::ArrayTuple *DataArrayIterator<T>::nextt()
508   {
509     if(_tuple_id<_nb_tuple)
510       {
511         _tuple_id++;
512         typename Traits<T>::ArrayTuple *ret=new typename Traits<T>::ArrayTuple(_pt,_nb_comp);
513         _pt+=_nb_comp;
514         return ret;
515       }
516     else
517       return 0;
518   }
519
520   //////////////////////////////////
521
522   template<class T>
523   DataArrayTuple<T>::DataArrayTuple(T *pt, int nbOfComp):_pt(pt),_nb_of_compo(nbOfComp)
524   {
525   }
526   
527   template<class T>
528   T DataArrayTuple<T>::zeValue() const
529   {
530     if(_nb_of_compo==1)
531       return *_pt;
532     throw INTERP_KERNEL::Exception("DataArrayTuple<T>::zeValue : DataArrayTuple instance has not exactly 1 component -> Not possible to convert it into a single value !");
533   }
534   
535   template<class T>
536   typename Traits<T>::ArrayType *DataArrayTuple<T>::buildDA(int nbOfTuples, int nbOfCompo) const
537   {
538     if((_nb_of_compo==nbOfCompo && nbOfTuples==1) || (_nb_of_compo==nbOfTuples && nbOfCompo==1))
539     {
540       typename Traits<T>::ArrayType *ret=Traits<T>::ArrayType::New();
541       ret->useExternalArrayWithRWAccess(_pt,nbOfTuples,nbOfCompo);
542       return ret;
543     }
544   else
545     {
546       std::ostringstream oss; oss << "DataArrayTuple<T>::buildDA : unable to build a requested DataArrayDouble instance with nbofTuple=" << nbOfTuples << " and nbOfCompo=" << nbOfCompo;
547       oss << ".\nBecause the number of elements in this is " << _nb_of_compo << " !";
548       throw INTERP_KERNEL::Exception(oss.str().c_str());
549     }
550   }
551   
552   //////////////////////////////////
553
554   template<class T>
555   MCAuto< typename Traits<T>::ArrayTypeCh > DataArrayTemplate<T>::NewFromStdVector(const typename std::vector<T>& v)
556   {
557     std::size_t sz(v.size());
558     MCAuto< typename Traits<T>::ArrayTypeCh > ret(Traits<T>::ArrayTypeCh::New());
559     ret->alloc(sz,1);
560     T *pt(ret->getPointer());
561     std::copy(v.begin(),v.end(),pt);
562     return ret;
563   }
564   
565   template<class T>
566   std::vector< MCAuto< typename Traits<T>::ArrayTypeCh > > DataArrayTemplate<T>::explodeComponents() const
567   {
568     checkAllocated();
569     std::size_t sz(getNumberOfComponents());
570     int nbTuples(getNumberOfTuples());
571     std::string name(getName());
572     std::vector<std::string> compNames(getInfoOnComponents());
573     std::vector< MCAuto< typename Traits<T>::ArrayTypeCh > > ret(sz);
574     const T *thisPt(begin());
575     for(std::size_t i=0;i<sz;i++)
576       {
577         MCAuto< typename Traits<T>::ArrayTypeCh > part(Traits<T>::ArrayTypeCh::New());
578         part->alloc(nbTuples,1);
579         part->setName(name);
580         part->setInfoOnComponent(0,compNames[i]);
581         T *otherPt(part->getPointer());
582         for(int j=0;j<nbTuples;j++)
583           otherPt[j]=thisPt[sz*j+i];
584         ret[i]=part;
585       }
586     return ret;
587   }
588   
589   template<class T>
590   std::size_t DataArrayTemplate<T>::getHeapMemorySizeWithoutChildren() const
591   {
592     std::size_t sz(_mem.getNbOfElemAllocated());
593     sz*=sizeof(T);
594     return DataArray::getHeapMemorySizeWithoutChildren()+sz;
595   }
596   
597   /*!
598    * Allocates the raw data in memory. If the memory was already allocated, then it is
599    * freed and re-allocated. See an example of this method use
600    * \ref MEDCouplingArraySteps1WC "here".
601    *  \param [in] nbOfTuple - number of tuples of data to allocate.
602    *  \param [in] nbOfCompo - number of components of data to allocate.
603    *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
604    */
605   template<class T>
606   void DataArrayTemplate<T>::alloc(std::size_t nbOfTuple, std::size_t nbOfCompo)
607   {
608     _info_on_compo.resize(nbOfCompo);
609     _mem.alloc(nbOfCompo*nbOfTuple);
610     declareAsNew();
611   }
612
613   /*!
614    * Sets a C array to be used as raw data of \a this. The previously set info
615    *  of components is retained and re-sized. 
616    * For more info see \ref MEDCouplingArraySteps1.
617    *  \param [in] array - the C array to be used as raw data of \a this.
618    *  \param [in] ownership - if \a true, \a array will be deallocated at destruction of \a this.
619    *  \param [in] type - specifies how to deallocate \a array. If \a type == MEDCoupling::CPP_DEALLOC,
620    *                     \c delete [] \c array; will be called. If \a type == MEDCoupling::C_DEALLOC,
621    *                     \c free(\c array ) will be called.
622    *  \param [in] nbOfTuple - new number of tuples in \a this.
623    *  \param [in] nbOfCompo - new number of components in \a this.
624    */
625   template<class T>
626   void DataArrayTemplate<T>::useArray(const T *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo)
627   {
628     _info_on_compo.resize(nbOfCompo);
629     _mem.useArray(array,ownership,type,(std::size_t)nbOfTuple*nbOfCompo);
630     declareAsNew();
631   }
632   
633   template<class T>
634   void DataArrayTemplate<T>::useExternalArrayWithRWAccess(const T *array, int nbOfTuple, int nbOfCompo)
635   {
636     _info_on_compo.resize(nbOfCompo);
637     _mem.useExternalArrayWithRWAccess(array,(std::size_t)nbOfTuple*nbOfCompo);
638     declareAsNew();
639   }
640
641   /*!
642    * Returns a value located at specified tuple and component.
643    * This method is equivalent to DataArrayTemplate<T>::getIJ() except that validity of
644    * parameters is checked. So this method is safe but expensive if used to go through
645    * all values of \a this.
646    *  \param [in] tupleId - index of tuple of interest.
647    *  \param [in] compoId - index of component of interest.
648    *  \return double - value located by \a tupleId and \a compoId.
649    *  \throw If \a this is not allocated.
650    *  \throw If condition <em>( 0 <= tupleId < this->getNumberOfTuples() )</em> is violated.
651    *  \throw If condition <em>( 0 <= compoId < this->getNumberOfComponents() )</em> is violated.
652    */
653   template<class T>
654   T DataArrayTemplate<T>::getIJSafe(int tupleId, int compoId) const
655   {
656     checkAllocated();
657     if(tupleId<0 || tupleId>=getNumberOfTuples())
658       {
659         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::getIJSafe : request for tupleId " << tupleId << " should be in [0," << getNumberOfTuples() << ") !";
660         throw INTERP_KERNEL::Exception(oss.str().c_str());
661       }
662     if(compoId<0 || compoId>=(int)getNumberOfComponents())
663       {
664         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::getIJSafe : request for compoId " << compoId << " should be in [0," << getNumberOfComponents() << ") !";
665         throw INTERP_KERNEL::Exception(oss.str().c_str());
666       }
667     return _mem[tupleId*_info_on_compo.size()+compoId];
668   }
669
670   /*!
671    * This method \b do \b not modify content of \a this. It only modify its memory footprint if the allocated memory is to high regarding real data to store.
672    *
673    * \sa DataArray::getHeapMemorySizeWithoutChildren, DataArrayTemplate<T>::reserve
674    */
675   template<class T>
676   void DataArrayTemplate<T>::pack() const
677   {
678     _mem.pack();
679   }
680
681   /*!
682    * Checks if raw data is allocated. Read more on the raw data
683    * in \ref MEDCouplingArrayBasicsTuplesAndCompo "DataArrays infos" for more information.
684    *  \return bool - \a true if the raw data is allocated, \a false else.
685    */
686   template<class T>
687   bool DataArrayTemplate<T>::isAllocated() const
688   {
689     return getConstPointer()!=0;
690   }
691   
692   /*!
693    * Checks if raw data is allocated and throws an exception if it is not the case.
694    *  \throw If the raw data is not allocated.
695    */
696   template<class T>
697   void DataArrayTemplate<T>::checkAllocated() const
698   {
699     if(!isAllocated())
700       {
701         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !";
702         throw INTERP_KERNEL::Exception(oss.str().c_str());
703       }
704   }
705   
706   /*!
707    * This method desallocated \a this without modification of informations relative to the components.
708    * After call of this method, DataArrayDouble::isAllocated will return false.
709    * If \a this is already not allocated, \a this is let unchanged.
710    */
711   template<class T>
712   void DataArrayTemplate<T>::desallocate()
713   {
714     _mem.destroy();
715   }
716
717   /*!
718    * This method reserve nbOfElems elements in memory ( nbOfElems*8 bytes ) \b without impacting the number of tuples in \a this.
719    * If \a this has already been allocated, this method checks that \a this has only one component. If not an INTERP_KERNEL::Exception will be thrown.
720    * If \a this has not already been allocated, number of components is set to one.
721    * This method allows to reduce number of reallocations on invokation of DataArrayDouble::pushBackSilent and DataArrayDouble::pushBackValsSilent on \a this.
722    * 
723    * \sa DataArrayDouble::pack, DataArrayDouble::pushBackSilent, DataArrayDouble::pushBackValsSilent
724    */
725   template<class T>
726   void DataArrayTemplate<T>::reserve(std::size_t nbOfElems)
727   {
728     int nbCompo(getNumberOfComponents());
729     if(nbCompo==1)
730       {
731         _mem.reserve(nbOfElems);
732       }
733     else if(nbCompo==0)
734       {
735         _mem.reserve(nbOfElems);
736         _info_on_compo.resize(1);
737       }
738     else
739       {
740         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::reserve : not available for DataArrayDouble with number of components different than 1 !";
741         throw INTERP_KERNEL::Exception(oss.str().c_str());
742       }
743   }
744   
745   /*!
746    * This method adds at the end of \a this the single value \a val. This method do \b not update its time label to avoid useless incrementation
747    * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
748    *
749    * \param [in] val the value to be added in \a this
750    * \throw If \a this has already been allocated with number of components different from one.
751    * \sa DataArrayDouble::pushBackValsSilent
752    */
753   template<class T>
754   void DataArrayTemplate<T>::pushBackSilent(T val)
755   {
756     int nbCompo(getNumberOfComponents());
757     if(nbCompo==1)
758       _mem.pushBack(val);
759     else if(nbCompo==0)
760       {
761         _info_on_compo.resize(1);
762         _mem.pushBack(val);
763       }
764     else
765       {
766         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::pushBackSilent : not available for DataArrayDouble with number of components different than 1 !";
767         throw INTERP_KERNEL::Exception(oss.str().c_str());
768       }
769   }
770   
771   /*!
772    * This method adds at the end of \a this a serie of values [\c valsBg,\c valsEnd). This method do \b not update its time label to avoid useless incrementation
773    * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
774    *
775    *  \param [in] valsBg - an array of values to push at the end of \c this.
776    *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
777    *              the last value of \a valsBg is \a valsEnd[ -1 ].
778    * \throw If \a this has already been allocated with number of components different from one.
779    * \sa DataArrayDouble::pushBackSilent
780    */
781   template<class T>
782   void DataArrayTemplate<T>::pushBackValsSilent(const T *valsBg, const T *valsEnd)
783   {
784     int nbCompo(getNumberOfComponents());
785     if(nbCompo==1)
786       _mem.insertAtTheEnd(valsBg,valsEnd);
787     else if(nbCompo==0)
788       {
789         _info_on_compo.resize(1);
790         _mem.insertAtTheEnd(valsBg,valsEnd);
791       }
792     else
793       {
794         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::pushBackValsSilent : not available for DataArrayDouble with number of components different than 1 !";
795         throw INTERP_KERNEL::Exception(oss.str().c_str());
796       }
797   }
798   
799   /*!
800    * This method returns silently ( without updating time label in \a this ) the last value, if any and suppress it.
801    * \throw If \a this is already empty.
802    * \throw If \a this has number of components different from one.
803    */
804   template<class T>
805   T DataArrayTemplate<T>::popBackSilent()
806   {
807     if(getNumberOfComponents()==1)
808       return _mem.popBack();
809     else
810       {
811         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::popBackSilent : not available for DataArrayDouble with number of components different than 1 !";
812         throw INTERP_KERNEL::Exception(oss.str().c_str());
813       }
814   }
815   
816   /*!
817    * Allocates the raw data in memory. If exactly same memory as needed already
818    * allocated, it is not re-allocated.
819    *  \param [in] nbOfTuple - number of tuples of data to allocate.
820    *  \param [in] nbOfCompo - number of components of data to allocate.
821    *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
822    */
823   template<class T>
824   void DataArrayTemplate<T>::allocIfNecessary(int nbOfTuple, int nbOfCompo)
825   {
826     if(isAllocated())
827       {
828         if(nbOfTuple!=getNumberOfTuples() || nbOfCompo!=(int)getNumberOfComponents())
829           alloc(nbOfTuple,nbOfCompo);
830       }
831     else
832       alloc(nbOfTuple,nbOfCompo);
833   }
834
835   /*!
836    * Checks the number of tuples.
837    *  \return bool - \a true if getNumberOfTuples() == 0, \a false else.
838    *  \throw If \a this is not allocated.
839    */
840   template<class T>
841   bool DataArrayTemplate<T>::empty() const
842   {
843     checkAllocated();
844     return getNumberOfTuples()==0;
845   }
846
847   /*!
848    * Copies all the data from another DataArrayDouble. For more info see
849    * \ref MEDCouplingArrayBasicsCopyDeepAssign.
850    *  \param [in] other - another instance of DataArrayDouble to copy data from.
851    *  \throw If the \a other is not allocated.
852    */
853   template<class T>
854   void DataArrayTemplate<T>::deepCopyFrom(const DataArrayTemplate<T>& other)
855   {
856     other.checkAllocated();
857     int nbOfTuples(other.getNumberOfTuples()),nbOfComp(other.getNumberOfComponents());
858     allocIfNecessary(nbOfTuples,nbOfComp);
859     std::size_t nbOfElems((std::size_t)nbOfTuples*nbOfComp);
860     T *pt(getPointer());
861     const T *ptI(other.begin());
862     for(std::size_t i=0;i<nbOfElems;i++)
863       pt[i]=ptI[i];
864     copyStringInfoFrom(other);
865   }
866
867   /*!
868    * Reverse the array values.
869    *  \throw If \a this->getNumberOfComponents() < 1.
870    *  \throw If \a this is not allocated.
871    */
872   template<class T>
873   void DataArrayTemplate<T>::reverse()
874   {
875     checkAllocated();
876     _mem.reverse(getNumberOfComponents());
877     declareAsNew();
878   }
879
880   /*!
881    * Assign \a val to all values in \a this array. To know more on filling arrays see
882    * \ref MEDCouplingArrayFill.
883    *  \param [in] val - the value to fill with.
884    *  \throw If \a this is not allocated.
885    */
886   template<class T>
887   void DataArrayTemplate<T>::fillWithValue(T val)
888   {
889     checkAllocated();
890     _mem.fillWithValue(val);
891     declareAsNew();
892   }
893
894   /*!
895    * Changes number of tuples in the array. If the new number of tuples is smaller
896    * than the current number the array is truncated, otherwise the array is extended.
897    *  \param [in] nbOfTuples - new number of tuples. 
898    *  \throw If \a this is not allocated.
899    *  \throw If \a nbOfTuples is negative.
900    */
901   template<class T>
902   void DataArrayTemplate<T>::reAlloc(std::size_t nbOfTuples)
903   {
904     checkAllocated();
905     _mem.reAlloc(getNumberOfComponents()*nbOfTuples);
906     declareAsNew();
907   }
908
909   /*!
910    * Permutes values of \a this array as required by \a old2New array. The values are
911    * permuted so that \c new[ \a old2New[ i ]] = \c old[ i ]. Number of tuples remains
912    * the same as in \c this one.
913    * If a permutation reduction is needed, subArray() or selectByTupleId() should be used.
914    * For more info on renumbering see \ref numbering.
915    *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
916    *     giving a new position for i-th old value.
917    */
918   template<class T>
919   void DataArrayTemplate<T>::renumberInPlace(const int *old2New)
920   {
921     checkAllocated();
922     int nbTuples(getNumberOfTuples()),nbOfCompo(getNumberOfComponents());
923     T *tmp(new T[nbTuples*nbOfCompo]);
924     const T *iptr(begin());
925     for(int i=0;i<nbTuples;i++)
926       {
927         int v=old2New[i];
928         if(v>=0 && v<nbTuples)
929           std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),tmp+nbOfCompo*v);
930         else
931           {
932             std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::renumberInPlace : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
933             throw INTERP_KERNEL::Exception(oss.str().c_str());
934           }
935       }
936     std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
937     delete [] tmp;
938     declareAsNew();
939   }
940
941
942   /*!
943    * Permutes values of \a this array as required by \a new2Old array. The values are
944    * permuted so that \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of tuples remains
945    * the same as in \c this one.
946    * For more info on renumbering see \ref numbering.
947    *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
948    *     giving a previous position of i-th new value.
949    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
950    *          is to delete using decrRef() as it is no more needed.
951    */
952   template<class T>
953   void DataArrayTemplate<T>::renumberInPlaceR(const int *new2Old)
954   {
955     checkAllocated();
956     int nbTuples(getNumberOfTuples()),nbOfCompo(getNumberOfComponents());
957     T *tmp(new T[nbTuples*nbOfCompo]);
958     const T *iptr(begin());
959     for(int i=0;i<nbTuples;i++)
960       {
961         int v=new2Old[i];
962         if(v>=0 && v<nbTuples)
963           std::copy(iptr+nbOfCompo*v,iptr+nbOfCompo*(v+1),tmp+nbOfCompo*i);
964         else
965           {
966             std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::renumberInPlaceR : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
967             throw INTERP_KERNEL::Exception(oss.str().c_str());
968           }
969       }
970     std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
971     delete [] tmp;
972     declareAsNew();
973   }
974
975   /*!
976    * Sorts values of the array.
977    *  \param [in] asc - \a true means ascending order, \a false, descending.
978    *  \throw If \a this is not allocated.
979    *  \throw If \a this->getNumberOfComponents() != 1.
980    */
981   template<class T>
982   void DataArrayTemplate<T>::sort(bool asc)
983   {
984     checkAllocated();
985     if(getNumberOfComponents()!=1)
986       {
987         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::sort : only supported with 'this' array with ONE component !";
988         throw INTERP_KERNEL::Exception(oss.str().c_str());
989       }
990     _mem.sort(asc);
991     declareAsNew();
992   }
993
994   /*!
995    * Returns a copy of \a this array with values permuted as required by \a old2New array.
996    * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ].
997    * Number of tuples in the result array remains the same as in \c this one.
998    * If a permutation reduction is needed, renumberAndReduce() should be used.
999    * For more info on renumbering see \ref numbering.
1000    *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1001    *          giving a new position for i-th old value.
1002    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1003    *          is to delete using decrRef() as it is no more needed.
1004    *  \throw If \a this is not allocated.
1005    */
1006   template<class T>
1007   typename Traits<T>::ArrayType *DataArrayTemplate<T>::renumber(const int *old2New) const
1008   {
1009     checkAllocated();
1010     int nbTuples(getNumberOfTuples()),nbOfCompo(getNumberOfComponents());
1011     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1012     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1013     ret->alloc(nbTuples,nbOfCompo);
1014     ret->copyStringInfoFrom(*this);
1015     const T *iptr(begin());
1016     T *optr(ret->getPointer());
1017     for(int i=0;i<nbTuples;i++)
1018       std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),optr+nbOfCompo*old2New[i]);
1019     ret->copyStringInfoFrom(*this);
1020     return ret.retn();
1021   }
1022
1023   /*!
1024    * Returns a copy of \a this array with values permuted as required by \a new2Old array.
1025    * The values are permuted so that  \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of
1026    * tuples in the result array remains the same as in \c this one.
1027    * If a permutation reduction is needed, subArray() or selectByTupleId() should be used.
1028    * For more info on renumbering see \ref numbering.
1029    *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
1030    *     giving a previous position of i-th new value.
1031    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1032    *          is to delete using decrRef() as it is no more needed.
1033    */
1034   template<class T>
1035   typename Traits<T>::ArrayType *DataArrayTemplate<T>::renumberR(const int *new2Old) const
1036   {
1037     checkAllocated();
1038     int nbTuples(getNumberOfTuples()),nbOfCompo(getNumberOfComponents());
1039     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1040     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1041     ret->alloc(nbTuples,nbOfCompo);
1042     ret->copyStringInfoFrom(*this);
1043     const T *iptr(getConstPointer());
1044     T *optr(ret->getPointer());
1045     for(int i=0;i<nbTuples;i++)
1046       std::copy(iptr+nbOfCompo*new2Old[i],iptr+nbOfCompo*(new2Old[i]+1),optr+i*nbOfCompo);
1047     ret->copyStringInfoFrom(*this);
1048     return ret.retn();
1049   }
1050
1051   /*!
1052    * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1053    * of size \a newNbOfTuple and it's values are permuted as required by \a old2New array.
1054    * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ] for all
1055    * \a old2New[ i ] >= 0. In other words every i-th tuple in \a this array, for which 
1056    * \a old2New[ i ] is negative, is missing from the result array.
1057    * For more info on renumbering see \ref numbering.
1058    *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1059    *     giving a new position for i-th old tuple and giving negative position for
1060    *     for i-th old tuple that should be omitted.
1061    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1062    *          is to delete using decrRef() as it is no more needed.
1063    */
1064   template<class T>
1065   typename Traits<T>::ArrayType *DataArrayTemplate<T>::renumberAndReduce(const int *old2New, int newNbOfTuple) const
1066   {
1067     checkAllocated();
1068     int nbTuples(getNumberOfTuples()),nbOfCompo(getNumberOfComponents());
1069     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1070     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1071     ret->alloc(newNbOfTuple,nbOfCompo);
1072     const T *iptr=getConstPointer();
1073     T *optr=ret->getPointer();
1074     for(int i=0;i<nbTuples;i++)
1075       {
1076         int w=old2New[i];
1077         if(w>=0)
1078           std::copy(iptr+i*nbOfCompo,iptr+(i+1)*nbOfCompo,optr+w*nbOfCompo);
1079       }
1080     ret->copyStringInfoFrom(*this);
1081     return ret.retn();
1082   }
1083
1084   /*!
1085    * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1086    * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
1087    * \a new2OldBg array.
1088    * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
1089    * This method is equivalent to renumberAndReduce() except that convention in input is
1090    * \c new2old and \b not \c old2new.
1091    * For more info on renumbering see \ref numbering.
1092    *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
1093    *              tuple index in \a this array to fill the i-th tuple in the new array.
1094    *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
1095    *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
1096    *              \a new2OldBg <= \a pi < \a new2OldEnd.
1097    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1098    *          is to delete using decrRef() as it is no more needed.
1099    */
1100   template<class T>
1101   typename Traits<T>::ArrayType *DataArrayTemplate<T>::mySelectByTupleId(const int *new2OldBg, const int *new2OldEnd) const
1102   {
1103     checkAllocated();
1104     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1105     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1106     int nbComp(getNumberOfComponents());
1107     ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
1108     ret->copyStringInfoFrom(*this);
1109     T *pt(ret->getPointer());
1110     const T *srcPt(getConstPointer());
1111     int i(0);
1112     for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
1113       std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
1114     ret->copyStringInfoFrom(*this);
1115     return ret.retn();
1116   }
1117
1118   template<class T>
1119   typename Traits<T>::ArrayType *DataArrayTemplate<T>::mySelectByTupleId(const DataArrayInt& di) const
1120   {
1121     return DataArrayTemplate<T>::mySelectByTupleId(di.begin(),di.end());
1122   }
1123
1124   template<class T>
1125   MCAuto<typename Traits<T>::ArrayTypeCh> DataArrayTemplate<T>::selectPartDef(const PartDefinition *pd) const
1126   {
1127     if(!pd)
1128       throw INTERP_KERNEL::Exception("DataArrayTemplate<T>::selectPartDef : null input pointer !");
1129     MCAuto<typename Traits<T>::ArrayTypeCh> ret(Traits<T>::ArrayTypeCh::New());
1130     const SlicePartDefinition *spd(dynamic_cast<const SlicePartDefinition *>(pd));
1131     if(spd)
1132       {
1133         int a,b,c;
1134         spd->getSlice(a,b,c);
1135         if(a==0 && b==(int)getNumberOfTuples() && c==1)
1136           {
1137             DataArrayTemplate<T> *directRet(const_cast<DataArrayTemplate<T> *>(this));
1138             directRet->incrRef();
1139             MCAuto<DataArrayTemplate<T> > ret2(directRet);
1140             return DynamicCastSafe<DataArrayTemplate<T>,typename Traits<T>::ArrayTypeCh>(ret2);
1141           }
1142         else
1143           {
1144             MCAuto<DataArray> ret2(selectByTupleIdSafeSlice(a,b,c));
1145             return DynamicCastSafe<DataArray,typename Traits<T>::ArrayTypeCh>(ret2);
1146           }
1147       }
1148     const DataArrayPartDefinition *dpd(dynamic_cast<const DataArrayPartDefinition *>(pd));
1149     if(dpd)
1150       {
1151         MCAuto<DataArrayInt> arr(dpd->toDAI());
1152         MCAuto<DataArray> ret2(selectByTupleIdSafe(arr->begin(),arr->end()));
1153         return DynamicCastSafe<DataArray,typename Traits<T>::ArrayTypeCh>(ret2);
1154         
1155       }
1156     throw INTERP_KERNEL::Exception("DataArrayTemplate<T>::selectPartDef : unrecognized part def !");
1157   }
1158   
1159   /*!
1160    * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1161    * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
1162    * \a new2OldBg array.
1163    * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
1164    * This method is equivalent to renumberAndReduce() except that convention in input is
1165    * \c new2old and \b not \c old2new.
1166    * This method is equivalent to selectByTupleId() except that it prevents coping data
1167    * from behind the end of \a this array.
1168    * For more info on renumbering see \ref numbering.
1169    *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
1170    *              tuple index in \a this array to fill the i-th tuple in the new array.
1171    *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
1172    *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
1173    *              \a new2OldBg <= \a pi < \a new2OldEnd.
1174    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1175    *          is to delete using decrRef() as it is no more needed.
1176    *  \throw If \a new2OldEnd - \a new2OldBg > \a this->getNumberOfTuples().
1177    */
1178   template<class T>
1179   typename Traits<T>::ArrayType *DataArrayTemplate<T>::mySelectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const
1180   {
1181     checkAllocated();
1182     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1183     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1184     int nbComp(getNumberOfComponents()),oldNbOfTuples(getNumberOfTuples());
1185     ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
1186     ret->copyStringInfoFrom(*this);
1187     T *pt(ret->getPointer());
1188     const T *srcPt(getConstPointer());
1189     int i(0);
1190     for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
1191       if(*w>=0 && *w<oldNbOfTuples)
1192         std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
1193       else
1194         {
1195           std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::selectByTupleIdSafe : some ids has been detected to be out of [0,this->getNumberOfTuples) !";
1196           throw INTERP_KERNEL::Exception(oss.str().c_str());
1197         }
1198     ret->copyStringInfoFrom(*this);
1199     return ret.retn();
1200   }
1201
1202   /*!
1203    * Changes the number of components within \a this array so that its raw data **does
1204    * not** change, instead splitting this data into tuples changes.
1205    *  \warning This method erases all (name and unit) component info set before!
1206    *  \param [in] newNbOfComp - number of components for \a this array to have.
1207    *  \throw If \a this is not allocated
1208    *  \throw If getNbOfElems() % \a newNbOfCompo != 0.
1209    *  \throw If \a newNbOfCompo is lower than 1.
1210    *  \throw If the rearrange method would lead to a number of tuples higher than 2147483647 (maximal capacity of int32 !).
1211    *  \warning This method erases all (name and unit) component info set before!
1212    */
1213   template<class T>
1214   void DataArrayTemplate<T>::rearrange(int newNbOfCompo)
1215   {
1216     checkAllocated();
1217     if(newNbOfCompo<1)
1218       {
1219         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::rearrange : input newNbOfCompo must be > 0 !";
1220         throw INTERP_KERNEL::Exception(oss.str().c_str());
1221       }
1222     std::size_t nbOfElems=getNbOfElems();
1223     if(nbOfElems%newNbOfCompo!=0)
1224       {
1225         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::rearrange : nbOfElems%newNbOfCompo!=0 !";
1226         throw INTERP_KERNEL::Exception(oss.str().c_str());
1227       }
1228     if(nbOfElems/newNbOfCompo>(std::size_t)std::numeric_limits<int>::max())
1229       {
1230         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::rearrange : the rearrangement leads to too high number of tuples (> 2147483647) !";
1231         throw INTERP_KERNEL::Exception(oss.str().c_str());
1232       }
1233     _info_on_compo.clear();
1234     _info_on_compo.resize(newNbOfCompo);
1235     declareAsNew();
1236   }
1237
1238   /*!
1239    * Changes the number of components within \a this array to be equal to its number
1240    * of tuples, and inversely its number of tuples to become equal to its number of 
1241    * components. So that its raw data **does not** change, instead splitting this
1242    * data into tuples changes.
1243    *  \warning This method erases all (name and unit) component info set before!
1244    *  \warning Do not confuse this method with fromNoInterlace() and toNoInterlace()!
1245    *  \throw If \a this is not allocated.
1246    *  \sa rearrange()
1247    */
1248   template<class T>
1249   void DataArrayTemplate<T>::transpose()
1250   {
1251     checkAllocated();
1252     int nbOfTuples(getNumberOfTuples());
1253     rearrange(nbOfTuples);
1254   }
1255
1256   /*!
1257    * Returns a shorten or extended copy of \a this array. If \a newNbOfComp is less
1258    * than \a this->getNumberOfComponents() then the result array is shorten as each tuple
1259    * is truncated to have \a newNbOfComp components, keeping first components. If \a
1260    * newNbOfComp is more than \a this->getNumberOfComponents() then the result array is
1261    * expanded as each tuple is populated with \a dftValue to have \a newNbOfComp
1262    * components.  
1263    *  \param [in] newNbOfComp - number of components for the new array to have.
1264    *  \param [in] dftValue - value assigned to new values added to the new array.
1265    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1266    *          is to delete using decrRef() as it is no more needed.
1267    *  \throw If \a this is not allocated.
1268    */
1269   template<class T>
1270   typename Traits<T>::ArrayType *DataArrayTemplate<T>::changeNbOfComponents(int newNbOfComp, T dftValue) const
1271   {
1272     checkAllocated();
1273     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1274     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1275     ret->alloc(getNumberOfTuples(),newNbOfComp);
1276     const T *oldc(getConstPointer());
1277     T *nc(ret->getPointer());
1278     int nbOfTuples(getNumberOfTuples()),oldNbOfComp(getNumberOfComponents());
1279     int dim(std::min(oldNbOfComp,newNbOfComp));
1280     for(int i=0;i<nbOfTuples;i++)
1281       {
1282         int j=0;
1283         for(;j<dim;j++)
1284           nc[newNbOfComp*i+j]=oldc[i*oldNbOfComp+j];
1285         for(;j<newNbOfComp;j++)
1286           nc[newNbOfComp*i+j]=dftValue;
1287       }
1288     ret->setName(getName());
1289     for(int i=0;i<dim;i++)
1290       ret->setInfoOnComponent(i,getInfoOnComponent(i));
1291     ret->setName(getName());
1292     return ret.retn();
1293   }
1294
1295   /*!
1296    * Returns a copy of \a this array composed of selected components.
1297    * The new DataArrayDouble has the same number of tuples but includes components
1298    * specified by \a compoIds parameter. So that getNbOfElems() of the result array
1299    * can be either less, same or more than \a this->getNbOfElems().
1300    *  \param [in] compoIds - sequence of zero based indices of components to include
1301    *              into the new array.
1302    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1303    *          is to delete using decrRef() as it is no more needed.
1304    *  \throw If \a this is not allocated.
1305    *  \throw If a component index (\a i) is not valid: 
1306    *         \a i < 0 || \a i >= \a this->getNumberOfComponents().
1307    *
1308    *  \if ENABLE_EXAMPLES
1309    *  \ref py_mcdataarraydouble_KeepSelectedComponents "Here is a Python example".
1310    *  \endif
1311    */
1312   template<class T>
1313   typename Traits<T>::ArrayType *DataArrayTemplate<T>::myKeepSelectedComponents(const std::vector<int>& compoIds) const
1314   {
1315     checkAllocated();
1316     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1317     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1318     std::size_t newNbOfCompo(compoIds.size());
1319     int oldNbOfCompo(getNumberOfComponents());
1320     for(std::vector<int>::const_iterator it=compoIds.begin();it!=compoIds.end();it++)
1321       if((*it)<0 || (*it)>=oldNbOfCompo)
1322         {
1323           std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::keepSelectedComponents : invalid requested component : " << *it << " whereas it should be in [0," << oldNbOfCompo << ") !";
1324           throw INTERP_KERNEL::Exception(oss.str().c_str());
1325         }
1326     int nbOfTuples(getNumberOfTuples());
1327     ret->alloc(nbOfTuples,(int)newNbOfCompo);
1328     ret->copyPartOfStringInfoFrom(*this,compoIds);
1329     const T *oldc(getConstPointer());
1330     T *nc(ret->getPointer());
1331     for(int i=0;i<nbOfTuples;i++)
1332       for(std::size_t j=0;j<newNbOfCompo;j++,nc++)
1333         *nc=oldc[i*oldNbOfCompo+compoIds[j]];
1334     return ret.retn();
1335   }
1336
1337   /*!
1338    * Returns a shorten copy of \a this array. The new DataArrayDouble contains all
1339    * tuples starting from the \a tupleIdBg-th tuple and including all tuples located before
1340    * the \a tupleIdEnd-th one. This methods has a similar behavior as std::string::substr().
1341    * This method is a specialization of selectByTupleIdSafeSlice().
1342    *  \param [in] tupleIdBg - index of the first tuple to copy from \a this array.
1343    *  \param [in] tupleIdEnd - index of the tuple before which the tuples to copy are located.
1344    *          If \a tupleIdEnd == -1, all the tuples till the end of \a this array are copied.
1345    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1346    *          is to delete using decrRef() as it is no more needed.
1347    *  \throw If \a tupleIdBg < 0.
1348    *  \throw If \a tupleIdBg > \a this->getNumberOfTuples().
1349    *  \throw If \a tupleIdEnd != -1 && \a tupleIdEnd < \a this->getNumberOfTuples().
1350    *  \sa DataArrayDouble::selectByTupleIdSafeSlice
1351    */
1352   template<class T>
1353   typename Traits<T>::ArrayType *DataArrayTemplate<T>::subArray(int tupleIdBg, int tupleIdEnd) const
1354   {
1355     checkAllocated();
1356     int nbt(getNumberOfTuples());
1357     if(tupleIdBg<0)
1358       {
1359         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::subArray : The tupleIdBg parameter must be greater than 0 !";
1360         throw INTERP_KERNEL::Exception(oss.str().c_str());
1361       }
1362     if(tupleIdBg>nbt)
1363       {
1364         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << ":subArray : The tupleIdBg parameter is greater than number of tuples !";
1365         throw INTERP_KERNEL::Exception(oss.str().c_str());
1366       }
1367     int trueEnd=tupleIdEnd;
1368     if(tupleIdEnd!=-1)
1369       {
1370         if(tupleIdEnd>nbt)
1371           {
1372             std::ostringstream oss; oss << Traits<T>::ArrayTypeName << ":subArray : The tupleIdBg parameter is greater than number of tuples !";
1373             throw INTERP_KERNEL::Exception(oss.str().c_str());
1374           }
1375       }
1376     else
1377       trueEnd=nbt;
1378     int nbComp(getNumberOfComponents());
1379     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1380     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1381     ret->alloc(trueEnd-tupleIdBg,nbComp);
1382     ret->copyStringInfoFrom(*this);
1383     std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer());
1384     return ret.retn();
1385   }
1386
1387   /*!
1388    * Returns a shorten copy of \a this array. The new DataArrayDouble contains every
1389    * (\a bg + \c i * \a step)-th tuple of \a this array located before the \a end2-th
1390    * tuple. Indices of the selected tuples are the same as ones returned by the Python
1391    * command \c range( \a bg, \a end2, \a step ).
1392    * This method is equivalent to selectByTupleIdSafe() except that the input array is
1393    * not constructed explicitly.
1394    * For more info on renumbering see \ref numbering.
1395    *  \param [in] bg - index of the first tuple to copy from \a this array.
1396    *  \param [in] end2 - index of the tuple before which the tuples to copy are located.
1397    *  \param [in] step - index increment to get index of the next tuple to copy.
1398    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1399    *          is to delete using decrRef() as it is no more needed.
1400    *  \sa DataArrayDouble::subArray.
1401    */
1402   template<class T>
1403   typename Traits<T>::ArrayType *DataArrayTemplate<T>::mySelectByTupleIdSafeSlice(int bg, int end2, int step) const
1404   {
1405     checkAllocated();
1406     MCAuto<DataArray> ret0(buildNewEmptyInstance());
1407     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
1408     int nbComp(getNumberOfComponents());
1409     std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::selectByTupleIdSafeSlice : ";
1410     int newNbOfTuples(GetNumberOfItemGivenBESRelative(bg,end2,step,oss.str()));
1411     ret->alloc(newNbOfTuples,nbComp);
1412     T *pt(ret->getPointer());
1413     const T *srcPt(getConstPointer()+bg*nbComp);
1414     for(int i=0;i<newNbOfTuples;i++,srcPt+=step*nbComp)
1415       std::copy(srcPt,srcPt+nbComp,pt+i*nbComp);
1416     ret->copyStringInfoFrom(*this);
1417     return ret.retn();
1418   }
1419   
1420   /*!
1421    * Copy all values from another DataArrayDouble into specified tuples and components
1422    * of \a this array. Textual data is not copied.
1423    * The tree parameters defining set of indices of tuples and components are similar to
1424    * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
1425    *  \param [in] a - the array to copy values from.
1426    *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
1427    *  \param [in] endTuples - index of the tuple before which the tuples to assign to
1428    *              are located.
1429    *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
1430    *  \param [in] bgComp - index of the first component of \a this array to assign values to.
1431    *  \param [in] endComp - index of the component before which the components to assign
1432    *              to are located.
1433    *  \param [in] stepComp - index increment to get index of the next component to assign to.
1434    *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
1435    *              must be equal to the number of columns to assign to, else an
1436    *              exception is thrown; if \a false, then it is only required that \a
1437    *              a->getNbOfElems() equals to number of values to assign to (this condition
1438    *              must be respected even if \a strictCompoCompare is \a true). The number of 
1439    *              values to assign to is given by following Python expression:
1440    *              \a nbTargetValues = 
1441    *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
1442    *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
1443    *  \throw If \a a is NULL.
1444    *  \throw If \a a is not allocated.
1445    *  \throw If \a this is not allocated.
1446    *  \throw If parameters specifying tuples and components to assign to do not give a
1447    *            non-empty range of increasing indices.
1448    *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
1449    *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
1450    *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
1451    *
1452    *  \if ENABLE_EXAMPLES
1453    *  \ref py_mcdataarraydouble_setpartofvalues1 "Here is a Python example".
1454    *  \endif
1455    */
1456   template<class T>
1457   void DataArrayTemplate<T>::setPartOfValues1(const typename Traits<T>::ArrayType *a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
1458   {
1459     if(!a)
1460       {
1461         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::setPartOfValues1 : input DataArrayDouble is NULL !";
1462         throw INTERP_KERNEL::Exception(oss.str().c_str());
1463       }
1464     const char msg[]="DataArrayTemplate::setPartOfValues1";
1465     checkAllocated();
1466     a->checkAllocated();
1467     int newNbOfTuples(DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg));
1468     int newNbOfComp(DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg));
1469     int nbComp(getNumberOfComponents()),nbOfTuples(getNumberOfTuples());
1470     DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
1471     DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
1472     bool assignTech(true);
1473     if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
1474       {
1475         if(strictCompoCompare)
1476           a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
1477       }
1478     else
1479       {
1480         a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
1481         assignTech=false;
1482       }
1483     const T *srcPt(a->getConstPointer());
1484     T *pt(getPointer()+bgTuples*nbComp+bgComp);
1485     if(assignTech)
1486       {
1487         for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
1488           for(int j=0;j<newNbOfComp;j++,srcPt++)
1489             pt[j*stepComp]=*srcPt;
1490       }
1491     else
1492       {
1493         for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
1494           {
1495             const T*srcPt2=srcPt;
1496             for(int j=0;j<newNbOfComp;j++,srcPt2++)
1497               pt[j*stepComp]=*srcPt2;
1498           }
1499       }
1500   }
1501   
1502   /*!
1503  * Assign a given value to values at specified tuples and components of \a this array.
1504  * The tree parameters defining set of indices of tuples and components are similar to
1505  * the tree parameters of the Python function \c range(\c start,\c stop,\c step)..
1506  *  \param [in] a - the value to assign.
1507  *  \param [in] bgTuples - index of the first tuple of \a this array to assign to.
1508  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
1509  *              are located.
1510  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
1511  *  \param [in] bgComp - index of the first component of \a this array to assign to.
1512  *  \param [in] endComp - index of the component before which the components to assign
1513  *              to are located.
1514  *  \param [in] stepComp - index increment to get index of the next component to assign to.
1515  *  \throw If \a this is not allocated.
1516  *  \throw If parameters specifying tuples and components to assign to, do not give a
1517  *            non-empty range of increasing indices or indices are out of a valid range
1518  *            for \c this array.
1519  *
1520  *  \if ENABLE_EXAMPLES
1521  *  \ref py_mcdataarraydouble_setpartofvaluessimple1 "Here is a Python example".
1522  *  \endif
1523  */
1524   template<class T>
1525   void DataArrayTemplate<T>::setPartOfValuesSimple1(T a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp)
1526   {
1527     const char msg[]="DataArrayTemplate::setPartOfValuesSimple1";
1528     checkAllocated();
1529     int newNbOfTuples(DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg));
1530     int newNbOfComp(DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg));
1531     int nbComp(getNumberOfComponents()),nbOfTuples(getNumberOfTuples());
1532     DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
1533     DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
1534     T *pt=getPointer()+bgTuples*nbComp+bgComp;
1535     for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
1536       for(int j=0;j<newNbOfComp;j++)
1537         pt[j*stepComp]=a;
1538   }
1539   
1540   /*!
1541    * Copy all values from another DataArrayDouble (\a a) into specified tuples and 
1542    * components of \a this array. Textual data is not copied.
1543    * The tuples and components to assign to are defined by C arrays of indices.
1544    * There are two *modes of usage*:
1545    * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
1546    *   of \a a is assigned to its own location within \a this array. 
1547    * - If \a a includes one tuple, then all values of \a a are assigned to the specified
1548    *   components of every specified tuple of \a this array. In this mode it is required
1549    *   that \a a->getNumberOfComponents() equals to the number of specified components.
1550    *
1551    *  \param [in] a - the array to copy values from.
1552    *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
1553    *              assign values of \a a to.
1554    *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
1555    *              pointer to a tuple index <em>(pi)</em> varies as this: 
1556    *              \a bgTuples <= \a pi < \a endTuples.
1557    *  \param [in] bgComp - pointer to an array of component indices of \a this array to
1558    *              assign values of \a a to.
1559    *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
1560    *              pointer to a component index <em>(pi)</em> varies as this: 
1561    *              \a bgComp <= \a pi < \a endComp.
1562    *  \param [in] strictCompoCompare - this parameter is checked only if the
1563    *               *mode of usage* is the first; if it is \a true (default), 
1564    *               then \a a->getNumberOfComponents() must be equal 
1565    *               to the number of specified columns, else this is not required.
1566    *  \throw If \a a is NULL.
1567    *  \throw If \a a is not allocated.
1568    *  \throw If \a this is not allocated.
1569    *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
1570    *         out of a valid range for \a this array.
1571    *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
1572    *         if <em> a->getNumberOfComponents() != (endComp - bgComp) </em>.
1573    *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
1574    *         <em> a->getNumberOfComponents() != (endComp - bgComp)</em>.
1575    *
1576    *  \if ENABLE_EXAMPLES
1577    *  \ref py_mcdataarraydouble_setpartofvalues2 "Here is a Python example".
1578    *  \endif
1579    */
1580   template<class T>
1581   void DataArrayTemplate<T>::setPartOfValues2(const typename Traits<T>::ArrayType *a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
1582   {
1583     if(!a)
1584       throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues2 : input DataArrayDouble is NULL !");
1585     const char msg[]="DataArrayTemplate::setPartOfValues2";
1586     checkAllocated();
1587     a->checkAllocated();
1588     int nbComp(getNumberOfComponents()),nbOfTuples(getNumberOfTuples());
1589     for(const int *z=bgComp;z!=endComp;z++)
1590       DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
1591     int newNbOfTuples((int)std::distance(bgTuples,endTuples));
1592     int newNbOfComp((int)std::distance(bgComp,endComp));
1593     bool assignTech(true);
1594     if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
1595       {
1596         if(strictCompoCompare)
1597           a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
1598       }
1599     else
1600       {
1601         a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
1602         assignTech=false;
1603       }
1604     T *pt(getPointer());
1605     const T *srcPt(a->getConstPointer());
1606     if(assignTech)
1607       {    
1608         for(const int *w=bgTuples;w!=endTuples;w++)
1609           {
1610             DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
1611             for(const int *z=bgComp;z!=endComp;z++,srcPt++)
1612               {    
1613                 pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt;
1614               }
1615           }
1616       }
1617     else
1618       {
1619         for(const int *w=bgTuples;w!=endTuples;w++)
1620           {
1621             const T *srcPt2=srcPt;
1622             DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
1623             for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
1624               {    
1625                 pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt2;
1626               }
1627           }
1628       }
1629   }
1630   
1631   /*!
1632    * Assign a given value to values at specified tuples and components of \a this array.
1633    * The tuples and components to assign to are defined by C arrays of indices.
1634    *  \param [in] a - the value to assign.
1635    *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
1636    *              assign \a a to.
1637    *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
1638    *              pointer to a tuple index (\a pi) varies as this: 
1639    *              \a bgTuples <= \a pi < \a endTuples.
1640    *  \param [in] bgComp - pointer to an array of component indices of \a this array to
1641    *              assign \a a to.
1642    *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
1643    *              pointer to a component index (\a pi) varies as this: 
1644    *              \a bgComp <= \a pi < \a endComp.
1645    *  \throw If \a this is not allocated.
1646    *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
1647    *         out of a valid range for \a this array.
1648    *
1649    *  \if ENABLE_EXAMPLES
1650    *  \ref py_mcdataarraydouble_setpartofvaluessimple2 "Here is a Python example".
1651    *  \endif
1652    */
1653   template<class T>
1654   void DataArrayTemplate<T>::setPartOfValuesSimple2(T a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp)
1655   {
1656     checkAllocated();
1657     int nbComp(getNumberOfComponents()),nbOfTuples(getNumberOfTuples());
1658     for(const int *z=bgComp;z!=endComp;z++)
1659       DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
1660     T *pt(getPointer());
1661     for(const int *w=bgTuples;w!=endTuples;w++)
1662       for(const int *z=bgComp;z!=endComp;z++)
1663         {
1664           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
1665           pt[(std::size_t)(*w)*nbComp+(*z)]=a;
1666         }
1667   }
1668   
1669   /*!
1670    * Copy all values from another DataArrayDouble (\a a) into specified tuples and 
1671    * components of \a this array. Textual data is not copied.
1672    * The tuples to assign to are defined by a C array of indices.
1673    * The components to assign to are defined by three values similar to parameters of
1674    * the Python function \c range(\c start,\c stop,\c step).
1675    * There are two *modes of usage*:
1676    * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
1677    *   of \a a is assigned to its own location within \a this array. 
1678    * - If \a a includes one tuple, then all values of \a a are assigned to the specified
1679    *   components of every specified tuple of \a this array. In this mode it is required
1680    *   that \a a->getNumberOfComponents() equals to the number of specified components.
1681    *
1682    *  \param [in] a - the array to copy values from.
1683    *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
1684    *              assign values of \a a to.
1685    *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
1686    *              pointer to a tuple index <em>(pi)</em> varies as this: 
1687    *              \a bgTuples <= \a pi < \a endTuples.
1688    *  \param [in] bgComp - index of the first component of \a this array to assign to.
1689    *  \param [in] endComp - index of the component before which the components to assign
1690    *              to are located.
1691    *  \param [in] stepComp - index increment to get index of the next component to assign to.
1692    *  \param [in] strictCompoCompare - this parameter is checked only in the first
1693    *               *mode of usage*; if \a strictCompoCompare is \a true (default), 
1694    *               then \a a->getNumberOfComponents() must be equal 
1695    *               to the number of specified columns, else this is not required.
1696    *  \throw If \a a is NULL.
1697    *  \throw If \a a is not allocated.
1698    *  \throw If \a this is not allocated.
1699    *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
1700    *         \a this array.
1701    *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
1702    *         if <em> a->getNumberOfComponents()</em> is unequal to the number of components
1703    *         defined by <em>(bgComp,endComp,stepComp)</em>.
1704    *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
1705    *         <em> a->getNumberOfComponents()</em> is unequal to the number of components
1706    *         defined by <em>(bgComp,endComp,stepComp)</em>.
1707    *  \throw If parameters specifying components to assign to, do not give a
1708    *            non-empty range of increasing indices or indices are out of a valid range
1709    *            for \c this array.
1710    *
1711    *  \if ENABLE_EXAMPLES
1712    *  \ref py_mcdataarraydouble_setpartofvalues3 "Here is a Python example".
1713    *  \endif
1714    */
1715   template<class T>
1716   void DataArrayTemplate<T>::setPartOfValues3(const typename Traits<T>::ArrayType *a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
1717   {
1718     if(!a)
1719       throw INTERP_KERNEL::Exception("DataArrayTemplate::setPartOfValues3 : input DataArrayDouble is NULL !");
1720     const char msg[]="DataArrayTemplate::setPartOfValues3";
1721     checkAllocated();
1722     a->checkAllocated();
1723     int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
1724     int nbComp=getNumberOfComponents();
1725     int nbOfTuples=getNumberOfTuples();
1726     DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
1727     int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
1728     bool assignTech=true;
1729     if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
1730       {
1731         if(strictCompoCompare)
1732           a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
1733       }
1734     else
1735       {
1736         a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
1737         assignTech=false;
1738       }
1739     T *pt(getPointer()+bgComp);
1740     const T *srcPt(a->getConstPointer());
1741     if(assignTech)
1742       {
1743         for(const int *w=bgTuples;w!=endTuples;w++)
1744           for(int j=0;j<newNbOfComp;j++,srcPt++)
1745             {
1746               DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
1747               pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt;
1748             }
1749       }
1750     else
1751       {
1752         for(const int *w=bgTuples;w!=endTuples;w++)
1753           {
1754             const T *srcPt2=srcPt;
1755             for(int j=0;j<newNbOfComp;j++,srcPt2++)
1756               {
1757                 DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
1758                 pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt2;
1759               }
1760           }
1761       }
1762   }
1763   
1764   /*!
1765    * Assign a given value to values at specified tuples and components of \a this array.
1766    * The tuples to assign to are defined by a C array of indices.
1767    * The components to assign to are defined by three values similar to parameters of
1768    * the Python function \c range(\c start,\c stop,\c step).
1769    *  \param [in] a - the value to assign.
1770    *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
1771    *              assign \a a to.
1772    *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
1773    *              pointer to a tuple index <em>(pi)</em> varies as this: 
1774    *              \a bgTuples <= \a pi < \a endTuples.
1775    *  \param [in] bgComp - index of the first component of \a this array to assign to.
1776    *  \param [in] endComp - index of the component before which the components to assign
1777    *              to are located.
1778    *  \param [in] stepComp - index increment to get index of the next component to assign to.
1779    *  \throw If \a this is not allocated.
1780    *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
1781    *         \a this array.
1782    *  \throw If parameters specifying components to assign to, do not give a
1783    *            non-empty range of increasing indices or indices are out of a valid range
1784    *            for \c this array.
1785    *
1786    *  \if ENABLE_EXAMPLES
1787    *  \ref py_mcdataarraydouble_setpartofvaluessimple3 "Here is a Python example".
1788    *  \endif
1789    */
1790   template<class T>
1791   void DataArrayTemplate<T>::setPartOfValuesSimple3(T a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp)
1792   {
1793     const char msg[]="DataArrayTemplate::setPartOfValuesSimple3";
1794     checkAllocated();
1795     int newNbOfComp(DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg));
1796     int nbComp(getNumberOfComponents()),nbOfTuples(getNumberOfTuples());
1797     DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
1798     T *pt(getPointer()+bgComp);
1799     for(const int *w=bgTuples;w!=endTuples;w++)
1800       for(int j=0;j<newNbOfComp;j++)
1801         {
1802           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
1803           pt[(std::size_t)(*w)*nbComp+j*stepComp]=a;
1804         }
1805   }
1806
1807   /*!
1808    * Copy all values from another DataArrayDouble into specified tuples and components
1809    * of \a this array. Textual data is not copied.
1810    * The tree parameters defining set of indices of tuples and components are similar to
1811    * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
1812    *  \param [in] a - the array to copy values from.
1813    *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
1814    *  \param [in] endTuples - index of the tuple before which the tuples to assign to
1815    *              are located.
1816    *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
1817    *  \param [in] bgComp - pointer to an array of component indices of \a this array to
1818    *              assign \a a to.
1819    *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
1820    *              pointer to a component index (\a pi) varies as this: 
1821    *              \a bgComp <= \a pi < \a endComp.
1822    *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
1823    *              must be equal to the number of columns to assign to, else an
1824    *              exception is thrown; if \a false, then it is only required that \a
1825    *              a->getNbOfElems() equals to number of values to assign to (this condition
1826    *              must be respected even if \a strictCompoCompare is \a true). The number of 
1827    *              values to assign to is given by following Python expression:
1828    *              \a nbTargetValues = 
1829    *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
1830    *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
1831    *  \throw If \a a is NULL.
1832    *  \throw If \a a is not allocated.
1833    *  \throw If \a this is not allocated.
1834    *  \throw If parameters specifying tuples and components to assign to do not give a
1835    *            non-empty range of increasing indices.
1836    *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
1837    *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
1838    *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
1839    *
1840    */
1841   template<class T>
1842   void DataArrayTemplate<T>::setPartOfValues4(const typename Traits<T>::ArrayType *a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
1843   {if(!a)
1844       throw INTERP_KERNEL::Exception("DataArrayTemplate::setPartOfValues4 : input DataArrayTemplate is NULL !");
1845     const char msg[]="DataArrayTemplate::setPartOfValues4";
1846     checkAllocated();
1847     a->checkAllocated();
1848     int newNbOfTuples(DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg));
1849     int newNbOfComp((int)std::distance(bgComp,endComp));
1850     int nbComp(getNumberOfComponents());
1851     for(const int *z=bgComp;z!=endComp;z++)
1852       DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
1853     int nbOfTuples(getNumberOfTuples());
1854     DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
1855     bool assignTech(true);
1856     if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
1857       {
1858         if(strictCompoCompare)
1859           a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
1860       }
1861     else
1862       {
1863         a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
1864         assignTech=false;
1865       }
1866     const T *srcPt(a->getConstPointer());
1867     T *pt(getPointer()+bgTuples*nbComp);
1868     if(assignTech)
1869       {
1870         for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
1871           for(const int *z=bgComp;z!=endComp;z++,srcPt++)
1872             pt[*z]=*srcPt;
1873       }
1874     else
1875       {
1876       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
1877         {
1878           const T *srcPt2(srcPt);
1879           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
1880             pt[*z]=*srcPt2;
1881         }
1882       }
1883   }
1884
1885   template<class T>
1886   void DataArrayTemplate<T>::setPartOfValuesSimple4(T a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp)
1887   {
1888     const char msg[]="DataArrayTemplate::setPartOfValuesSimple4";
1889     checkAllocated();
1890     int newNbOfTuples(DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg));
1891     int nbComp(getNumberOfComponents());
1892     for(const int *z=bgComp;z!=endComp;z++)
1893       DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
1894     int nbOfTuples(getNumberOfTuples());
1895     DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
1896     T *pt=getPointer()+bgTuples*nbComp;
1897     for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
1898       for(const int *z=bgComp;z!=endComp;z++)
1899         pt[*z]=a;
1900   }
1901   
1902   /*!
1903    * Copy some tuples from another DataArrayDouble into specified tuples
1904    * of \a this array. Textual data is not copied. Both arrays must have equal number of
1905    * components.
1906    * Both the tuples to assign and the tuples to assign to are defined by a DataArrayInt.
1907    * All components of selected tuples are copied.
1908    *  \param [in] a - the array to copy values from.
1909    *  \param [in] tuplesSelec - the array specifying both source tuples of \a a and
1910    *              target tuples of \a this. \a tuplesSelec has two components, and the
1911    *              first component specifies index of the source tuple and the second
1912    *              one specifies index of the target tuple.
1913    *  \throw If \a this is not allocated.
1914    *  \throw If \a a is NULL.
1915    *  \throw If \a a is not allocated.
1916    *  \throw If \a tuplesSelec is NULL.
1917    *  \throw If \a tuplesSelec is not allocated.
1918    *  \throw If <em>this->getNumberOfComponents() != a->getNumberOfComponents()</em>.
1919    *  \throw If \a tuplesSelec->getNumberOfComponents() != 2.
1920    *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
1921    *         the corresponding (\a this or \a a) array.
1922    */
1923   template<class T>
1924   void DataArrayTemplate<T>::setPartOfValuesAdv(const typename Traits<T>::ArrayType *a, const DataArrayInt *tuplesSelec)
1925   {
1926     if(!a || !tuplesSelec)
1927       throw INTERP_KERNEL::Exception("DataArrayTemplate::setPartOfValuesAdv : input DataArrayTemplate is NULL !");
1928     checkAllocated();
1929     a->checkAllocated();
1930     tuplesSelec->checkAllocated();
1931     std::size_t nbOfComp(getNumberOfComponents());
1932     if(nbOfComp!=a->getNumberOfComponents())
1933       throw INTERP_KERNEL::Exception("DataArrayTemplate::setPartOfValuesAdv : This and a do not have the same number of components !");
1934     if(tuplesSelec->getNumberOfComponents()!=2)
1935       throw INTERP_KERNEL::Exception("DataArrayTemplate::setPartOfValuesAdv : Expecting to have a tuple selector DataArrayInt instance with exactly 2 components !");
1936     int thisNt(getNumberOfTuples());
1937     int aNt(a->getNumberOfTuples());
1938     T *valsToSet(getPointer());
1939     const T *valsSrc(a->getConstPointer());
1940     for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple+=2)
1941     {
1942       if(tuple[1]>=0 && tuple[1]<aNt)
1943         {
1944           if(tuple[0]>=0 && tuple[0]<thisNt)
1945             std::copy(valsSrc+nbOfComp*tuple[1],valsSrc+nbOfComp*(tuple[1]+1),valsToSet+nbOfComp*tuple[0]);
1946           else
1947             {
1948               std::ostringstream oss; oss << "DataArrayTemplate::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
1949               oss << " of 'tuplesSelec' request of tuple id #" << tuple[0] << " in 'this' ! It should be in [0," << thisNt << ") !";
1950               throw INTERP_KERNEL::Exception(oss.str().c_str());
1951             }
1952         }
1953       else
1954         {
1955           std::ostringstream oss; oss << "DataArrayTemplate::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
1956           oss << " of 'tuplesSelec' request of tuple id #" << tuple[1] << " in 'a' ! It should be in [0," << aNt << ") !";
1957           throw INTERP_KERNEL::Exception(oss.str().c_str());
1958         }
1959     }
1960   }
1961   
1962   /*!
1963    * Copy some tuples from another DataArrayDouble (\a aBase) into contiguous tuples
1964    * of \a this array. Textual data is not copied. Both arrays must have equal number of
1965    * components.
1966    * The tuples to assign to are defined by index of the first tuple, and
1967    * their number is defined by \a tuplesSelec->getNumberOfTuples().
1968    * The tuples to copy are defined by values of a DataArrayInt.
1969    * All components of selected tuples are copied.
1970    *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
1971    *              values to.
1972    *  \param [in] aBase - the array to copy values from.
1973    *  \param [in] tuplesSelec - the array specifying tuples of \a a to copy.
1974    *  \throw If \a this is not allocated.
1975    *  \throw If \a aBase is NULL.
1976    *  \throw If \a aBase is not allocated.
1977    *  \throw If \a tuplesSelec is NULL.
1978    *  \throw If \a tuplesSelec is not allocated.
1979    *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
1980    *  \throw If \a tuplesSelec->getNumberOfComponents() != 1.
1981    *  \throw If <em>tupleIdStart + tuplesSelec->getNumberOfTuples() > this->getNumberOfTuples().</em>
1982    *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
1983    *         \a aBase array.
1984  */
1985   template<class T>
1986   void DataArrayTemplate<T>::setContigPartOfSelectedValues(int tupleIdStart, const DataArray *aBase, const DataArrayInt *tuplesSelec)
1987   {
1988     if(!aBase || !tuplesSelec)
1989       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValues : input DataArray is NULL !");
1990     const typename Traits<T>::ArrayType *a(dynamic_cast<const typename Traits<T>::ArrayType *>(aBase));
1991     if(!a)
1992       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValues : input DataArray aBase is not a DataArrayDouble !");
1993     checkAllocated();
1994     a->checkAllocated();
1995     tuplesSelec->checkAllocated();
1996     std::size_t nbOfComp(getNumberOfComponents());
1997     if(nbOfComp!=a->getNumberOfComponents())
1998       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValues : This and a do not have the same number of components !");
1999     if(tuplesSelec->getNumberOfComponents()!=1)
2000       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValues : Expecting to have a tuple selector DataArrayInt instance with exactly 1 component !");
2001     int thisNt(getNumberOfTuples());
2002     int aNt(a->getNumberOfTuples());
2003     int nbOfTupleToWrite(tuplesSelec->getNumberOfTuples());
2004     T *valsToSet(getPointer()+tupleIdStart*nbOfComp);
2005     if(tupleIdStart+nbOfTupleToWrite>thisNt)
2006       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValues : invalid number range of values to write !");
2007     const T *valsSrc=a->getConstPointer();
2008     for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple++,valsToSet+=nbOfComp)
2009       {
2010         if(*tuple>=0 && *tuple<aNt)
2011           {
2012             std::copy(valsSrc+nbOfComp*(*tuple),valsSrc+nbOfComp*(*tuple+1),valsToSet);
2013           }
2014         else
2015           {
2016             std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::setContigPartOfSelectedValues : Tuple #" << std::distance(tuplesSelec->begin(),tuple);
2017             oss << " of 'tuplesSelec' request of tuple id #" << *tuple << " in 'a' ! It should be in [0," << aNt << ") !";
2018             throw INTERP_KERNEL::Exception(oss.str().c_str());
2019           }
2020       }
2021   }
2022   
2023   /*!
2024    * Copy some tuples from another DataArrayDouble (\a aBase) into contiguous tuples
2025    * of \a this array. Textual data is not copied. Both arrays must have equal number of
2026    * components.
2027    * The tuples to copy are defined by three values similar to parameters of
2028    * the Python function \c range(\c start,\c stop,\c step).
2029    * The tuples to assign to are defined by index of the first tuple, and
2030    * their number is defined by number of tuples to copy.
2031    * All components of selected tuples are copied.
2032    *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
2033    *              values to.
2034    *  \param [in] aBase - the array to copy values from.
2035    *  \param [in] bg - index of the first tuple to copy of the array \a aBase.
2036    *  \param [in] end2 - index of the tuple of \a aBase before which the tuples to copy
2037    *              are located.
2038    *  \param [in] step - index increment to get index of the next tuple to copy.
2039    *  \throw If \a this is not allocated.
2040    *  \throw If \a aBase is NULL.
2041    *  \throw If \a aBase is not allocated.
2042    *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
2043    *  \throw If <em>tupleIdStart + len(range(bg,end2,step)) > this->getNumberOfTuples().</em>
2044    *  \throw If parameters specifying tuples to copy, do not give a
2045    *            non-empty range of increasing indices or indices are out of a valid range
2046    *            for the array \a aBase.
2047    */
2048   template<class T>
2049   void DataArrayTemplate<T>::setContigPartOfSelectedValuesSlice(int tupleIdStart, const DataArray *aBase, int bg, int end2, int step)
2050   {
2051     if(!aBase)
2052       {
2053         std::ostringstream oss; oss << Traits<T>::ArrayTypeName << "::setContigPartOfSelectedValuesSlice : input DataArray is NULL !";
2054         throw INTERP_KERNEL::Exception(oss.str().c_str());
2055       }
2056     const typename Traits<T>::ArrayType *a(dynamic_cast<const typename Traits<T>::ArrayType *>(aBase));
2057     if(!a)
2058       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValuesSlice : input DataArray aBase is not a DataArrayDouble !");
2059     checkAllocated();
2060     a->checkAllocated();
2061     std::size_t nbOfComp(getNumberOfComponents());
2062     const char msg[]="DataArrayDouble::setContigPartOfSelectedValuesSlice";
2063     int nbOfTupleToWrite(DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg));
2064     if(nbOfComp!=a->getNumberOfComponents())
2065       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValuesSlice : This and a do not have the same number of components !");
2066     int thisNt(getNumberOfTuples()),aNt(a->getNumberOfTuples());
2067     T *valsToSet(getPointer()+tupleIdStart*nbOfComp);
2068     if(tupleIdStart+nbOfTupleToWrite>thisNt)
2069       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValuesSlice : invalid number range of values to write !");
2070     if(end2>aNt)
2071       throw INTERP_KERNEL::Exception("DataArrayTemplate::setContigPartOfSelectedValuesSlice : invalid range of values to read !");
2072     const T *valsSrc(a->getConstPointer()+bg*nbOfComp);
2073     for(int i=0;i<nbOfTupleToWrite;i++,valsToSet+=nbOfComp,valsSrc+=step*nbOfComp)
2074       {
2075         std::copy(valsSrc,valsSrc+nbOfComp,valsToSet);
2076       }
2077   }
2078
2079   /*!
2080    * Returns a shorten copy of \a this array. The new DataArrayDouble contains ranges
2081    * of tuples specified by \a ranges parameter.
2082    * For more info on renumbering see \ref numbering.
2083    *  \param [in] ranges - std::vector of std::pair's each of which defines a range
2084    *              of tuples in [\c begin,\c end) format.
2085    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
2086    *          is to delete using decrRef() as it is no more needed.
2087    *  \throw If \a end < \a begin.
2088    *  \throw If \a end > \a this->getNumberOfTuples().
2089    *  \throw If \a this is not allocated.
2090    */
2091   template<class T>
2092   typename Traits<T>::ArrayType *DataArrayTemplate<T>::mySelectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const
2093   {
2094     checkAllocated();
2095     int nbOfComp(getNumberOfComponents()),nbOfTuplesThis(getNumberOfTuples());
2096     if(ranges.empty())
2097       {
2098         MCAuto<DataArray> ret0(buildNewEmptyInstance());
2099         MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
2100         ret->alloc(0,nbOfComp);
2101         ret->copyStringInfoFrom(*this);
2102         return ret.retn();
2103       }
2104     int ref(ranges.front().first),nbOfTuples(0);
2105     bool isIncreasing(true);
2106     for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
2107       {
2108         if((*it).first<=(*it).second)
2109           {
2110             if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
2111               {
2112                 nbOfTuples+=(*it).second-(*it).first;
2113                 if(isIncreasing)
2114                   isIncreasing=ref<=(*it).first;
2115                 ref=(*it).second;
2116               }
2117             else
2118               {
2119                 std::ostringstream oss; oss << "DataArrayTemplate::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
2120                 oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !";
2121                 throw INTERP_KERNEL::Exception(oss.str().c_str());
2122               }
2123           }
2124         else
2125           {
2126             std::ostringstream oss; oss << "DataArrayTemplate::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
2127             oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !";
2128             throw INTERP_KERNEL::Exception(oss.str().c_str());
2129           }
2130       }
2131     if(isIncreasing && nbOfTuplesThis==nbOfTuples)
2132       return static_cast<typename Traits<T>::ArrayType *>(deepCopy());
2133     MCAuto<DataArray> ret0(buildNewEmptyInstance());
2134     MCAuto< typename Traits<T>::ArrayType > ret(DynamicCastSafe<DataArray,typename Traits<T>::ArrayType>(ret0));
2135     ret->alloc(nbOfTuples,nbOfComp);
2136     ret->copyStringInfoFrom(*this);
2137     const T *src(getConstPointer());
2138     T *work(ret->getPointer());
2139     for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
2140       work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work);
2141     return ret.retn();
2142   }
2143
2144   /*!
2145    * Returns the first value of \a this. 
2146    *  \return double - the last value of \a this array.
2147    *  \throw If \a this is not allocated.
2148    *  \throw If \a this->getNumberOfComponents() != 1.
2149    *  \throw If \a this->getNumberOfTuples() < 1.
2150    */
2151   template<class T>
2152   T DataArrayTemplate<T>::front() const
2153   {
2154     checkAllocated();
2155     if(getNumberOfComponents()!=1)
2156       throw INTERP_KERNEL::Exception("DataArrayTemplate::front : number of components not equal to one !");
2157     int nbOfTuples(getNumberOfTuples());
2158     if(nbOfTuples<1)
2159       throw INTERP_KERNEL::Exception("DataArrayTemplate::front : number of tuples must be >= 1 !");
2160     return *(getConstPointer());
2161   }
2162   
2163   /*!
2164    * Returns the last value of \a this. 
2165    *  \return double - the last value of \a this array.
2166    *  \throw If \a this is not allocated.
2167    *  \throw If \a this->getNumberOfComponents() != 1.
2168    *  \throw If \a this->getNumberOfTuples() < 1.
2169    */
2170   template<class T>
2171   T DataArrayTemplate<T>::back() const
2172   {
2173     checkAllocated();
2174     if(getNumberOfComponents()!=1)
2175       throw INTERP_KERNEL::Exception("DataArrayTemplate::back : number of components not equal to one !");
2176     int nbOfTuples(getNumberOfTuples());
2177     if(nbOfTuples<1)
2178       throw INTERP_KERNEL::Exception("DataArrayTemplate::back : number of tuples must be >= 1 !");
2179     return *(getConstPointer()+nbOfTuples-1);
2180   }
2181   
2182   /*!
2183    * Returns the maximal value and its location within \a this one-dimensional array.
2184    *  \param [out] tupleId - index of the tuple holding the maximal value.
2185    *  \return double - the maximal value among all values of \a this array.
2186    *  \throw If \a this->getNumberOfComponents() != 1
2187    *  \throw If \a this->getNumberOfTuples() < 1
2188    */
2189   template<class T>
2190   T DataArrayTemplate<T>::getMaxValue(int& tupleId) const
2191   {
2192     checkAllocated();
2193     if(getNumberOfComponents()!=1)
2194       throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before or call 'getMaxValueInArray' method !");
2195     int nbOfTuples(getNumberOfTuples());
2196     if(nbOfTuples<=0)
2197       throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : array exists but number of tuples must be > 0 !");
2198     const T *vals(getConstPointer());
2199     const T *loc(std::max_element(vals,vals+nbOfTuples));
2200     tupleId=(int)std::distance(vals,loc);
2201     return *loc;
2202   }
2203   
2204   /*!
2205    * Returns the maximal value within \a this array that is allowed to have more than
2206    *  one component.
2207    *  \return double - the maximal value among all values of \a this array.
2208    *  \throw If \a this is not allocated.
2209    */
2210   template<class T>
2211   T DataArrayTemplate<T>::getMaxValueInArray() const
2212   {
2213     checkAllocated();
2214     const T *loc(std::max_element(begin(),end()));
2215     return *loc;
2216   }
2217   
2218   /*!
2219    * Returns the minimal value and its location within \a this one-dimensional array.
2220    *  \param [out] tupleId - index of the tuple holding the minimal value.
2221    *  \return double - the minimal value among all values of \a this array.
2222    *  \throw If \a this->getNumberOfComponents() != 1
2223    *  \throw If \a this->getNumberOfTuples() < 1
2224    */
2225   template<class T>
2226   T DataArrayTemplate<T>::getMinValue(int& tupleId) const
2227   {
2228     checkAllocated();
2229     if(getNumberOfComponents()!=1)
2230       throw INTERP_KERNEL::Exception("DataArrayDouble::getMinValue : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before call 'getMinValueInArray' method !");
2231     int nbOfTuples(getNumberOfTuples());
2232     if(nbOfTuples<=0)
2233       throw INTERP_KERNEL::Exception("DataArrayDouble::getMinValue : array exists but number of tuples must be > 0 !");
2234     const T *vals(getConstPointer());
2235     const T *loc(std::min_element(vals,vals+nbOfTuples));
2236     tupleId=(int)std::distance(vals,loc);
2237     return *loc;
2238   }
2239   
2240   /*!
2241    * Returns the minimal value within \a this array that is allowed to have more than
2242    *  one component.
2243    *  \return double - the minimal value among all values of \a this array.
2244    *  \throw If \a this is not allocated.
2245    */
2246   template<class T>
2247   T DataArrayTemplate<T>::getMinValueInArray() const
2248   {
2249     checkAllocated();
2250     const T *loc=std::min_element(begin(),end());
2251     return *loc;
2252   }
2253   
2254   template<class T>
2255   void DataArrayTemplate<T>::circularPermutation(int nbOfShift)
2256   {
2257     checkAllocated();
2258     int nbOfCompo(getNumberOfComponents()),nbTuples(getNumberOfTuples());
2259     int effNbSh(EffectiveCircPerm(nbOfShift,nbTuples));
2260     if(effNbSh==0)
2261       return ;
2262     T *work(getPointer());
2263     if(effNbSh<nbTuples-effNbSh)
2264       {
2265         typename INTERP_KERNEL::AutoPtr<T> buf(new T[effNbSh*nbOfCompo]);
2266         std::copy(work,work+effNbSh*nbOfCompo,(T *)buf);
2267         std::copy(work+effNbSh*nbOfCompo,work+nbTuples*nbOfCompo,work);// ze big shift
2268         std::copy((T *)buf,(T *)buf+effNbSh*nbOfCompo,work+(nbTuples-effNbSh)*nbOfCompo);
2269       }
2270     else
2271       {
2272         typename INTERP_KERNEL::AutoPtr<T> buf(new T[(nbTuples-effNbSh)*nbOfCompo]);
2273         std::copy(work+effNbSh*nbOfCompo,work+nbTuples*nbOfCompo,(T *)buf);
2274         std::copy(work,work+effNbSh*nbOfCompo,work+(nbTuples-effNbSh)*nbOfCompo);// ze big shift
2275         std::copy((T*)buf,(T *)buf+(nbTuples-effNbSh)*nbOfCompo,work);
2276       }
2277   }
2278   
2279   template<class T>
2280   void DataArrayTemplate<T>::circularPermutationPerTuple(int nbOfShift)
2281   {
2282     checkAllocated();
2283     int nbOfCompo(getNumberOfComponents()),nbTuples(getNumberOfTuples());
2284     int effNbSh(EffectiveCircPerm(nbOfShift,nbOfCompo));
2285     if(effNbSh==0)
2286       return ;
2287     T *work(getPointer());
2288     if(effNbSh<nbOfCompo-effNbSh)
2289       {
2290         typename INTERP_KERNEL::AutoPtr<T> buf(new T[effNbSh]);
2291         for(int i=0;i<nbTuples;i++,work+=nbOfCompo)
2292           {
2293             std::copy(work,work+effNbSh,(T *)buf);
2294             std::copy(work+effNbSh,work+nbOfCompo,work);// ze big shift
2295             std::copy((T *)buf,(T *)buf+effNbSh,work+(nbOfCompo-effNbSh));
2296           }
2297       }
2298     else
2299       {
2300         typename INTERP_KERNEL::AutoPtr<T> buf(new T[nbOfCompo-effNbSh]);
2301         for(int i=0;i<nbTuples;i++,work+=nbOfCompo)
2302           {
2303             std::copy(work+effNbSh,work+nbOfCompo,(T *)buf);
2304             std::copy(work,work+effNbSh,work+(nbOfCompo-effNbSh));// ze big shift
2305             std::copy((T*)buf,(T *)buf+(nbOfCompo-effNbSh),work);
2306           }
2307       }
2308     std::vector<std::string> sts(nbOfCompo);
2309     for(int i=0;i<nbOfCompo;i++)
2310       sts[i]=_info_on_compo[(i+effNbSh)%nbOfCompo];
2311     setInfoOnComponents(sts);
2312   }
2313   
2314   template<class T>
2315   void DataArrayTemplate<T>::reversePerTuple()
2316   {
2317     checkAllocated();
2318     int nbOfCompo(getNumberOfComponents()),nbTuples(getNumberOfTuples());
2319     if(nbOfCompo<=1)
2320       return ;
2321     T *work(getPointer());
2322     for(int i=0;i<nbTuples;i++,work+=nbOfCompo)
2323       std::reverse(work,work+nbOfCompo);
2324     std::reverse(_info_on_compo.begin(),_info_on_compo.end());
2325   }
2326
2327   /*!
2328    * Assign pointer to one array to a pointer to another appay. Reference counter of
2329    * \a arrayToSet is incremented / decremented.
2330    *  \param [in] newArray - the pointer to array to assign to \a arrayToSet.
2331    *  \param [in,out] arrayToSet - the pointer to array to assign to.
2332    */
2333   template<class T>
2334   void DataArrayTemplate<T>::SetArrayIn(typename Traits<T>::ArrayType *newArray, typename Traits<T>::ArrayType* &arrayToSet)
2335   {
2336     if(newArray!=arrayToSet)
2337       {
2338         if(arrayToSet)
2339           arrayToSet->decrRef();
2340         arrayToSet=newArray;
2341         if(arrayToSet)
2342           arrayToSet->incrRef();
2343       }
2344   }
2345
2346     /*!
2347    * Assign zero to all values in \a this array. To know more on filling arrays see
2348    * \ref MEDCouplingArrayFill.
2349    * \throw If \a this is not allocated.
2350    */
2351   template<class T>
2352   void DataArrayTemplate<T>::fillWithZero()
2353   {
2354     fillWithValue((T)0);
2355   }
2356
2357   //////////////////////////////
2358
2359   template<class T>
2360   template<class U>
2361   MCAuto< typename Traits<U>::ArrayType > DataArrayTemplateClassic<T>::convertToOtherTypeOfArr() const
2362   {
2363     this->checkAllocated();
2364     MCAuto<typename Traits<U>::ArrayType> ret(Traits<U>::ArrayType::New());
2365     ret->alloc(this->getNumberOfTuples(),this->getNumberOfComponents());
2366     std::size_t nbOfVals(this->getNbOfElems());
2367     const T *src(this->begin());
2368     U *dest(ret->getPointer());
2369     // to make Visual C++ happy : instead of std::size_t nbOfVals=getNbOfElems(); std::copy(src,src+nbOfVals,dest);
2370     //for(const T *src=this->begin();src!=this->end();src++,dest++)
2371     //  *dest=(int)*src;
2372     std::copy(src,src+nbOfVals,dest);
2373     ret->copyStringInfoFrom(*this);
2374     return ret;
2375   }
2376   
2377   /*!
2378    * Creates a new DataArrayDouble and assigns all (textual and numerical) data of \a this
2379    * array to the new one.
2380    *  \return DataArrayDouble * - the new instance of DataArrayInt.
2381    */
2382   template<class T>
2383   MCAuto<DataArrayDouble> DataArrayTemplateClassic<T>::convertToDblArr() const
2384   {
2385     return convertToOtherTypeOfArr<double>();
2386   }
2387
2388   /*!
2389    * Creates a new DataArrayInt and assigns all (textual and numerical) data of \a this
2390    * array to the new one.
2391    *  \return DataArrayInt * - the new instance of DataArrayInt.
2392    */
2393   template<class T>
2394   MCAuto<DataArrayInt> DataArrayTemplateClassic<T>::convertToIntArr() const
2395   {
2396     return convertToOtherTypeOfArr<int>();
2397   }
2398
2399   /*!
2400    * Creates a new DataArrayFloat and assigns all (textual and numerical) data of \a this
2401    * array to the new one.
2402    *  \return DataArrayFloat * - the new instance of DataArrayInt.
2403    */
2404   template<class T>
2405   MCAuto<DataArrayFloat> DataArrayTemplateClassic<T>::convertToFloatArr() const
2406   {
2407     return convertToOtherTypeOfArr<float>();
2408   }
2409
2410   /*!
2411    * Apply a linear function to a given component of \a this array, so that
2412    * an array element <em>(x)</em> becomes \f$ a * x + b \f$.
2413    *  \param [in] a - the first coefficient of the function.
2414    *  \param [in] b - the second coefficient of the function.
2415    *  \param [in] compoId - the index of component to modify.
2416    *  \throw If \a this is not allocated, or \a compoId is not in [0,\c this->getNumberOfComponents() ).
2417    */
2418   template<class T>
2419   void DataArrayTemplateClassic<T>::applyLin(T a, T b, int compoId)
2420   {
2421     this->checkAllocated();
2422     T *ptr(this->getPointer()+compoId);
2423     int nbOfComp(this->getNumberOfComponents()),nbOfTuple(this->getNumberOfTuples());
2424     if(compoId<0 || compoId>=nbOfComp)
2425       {
2426         std::ostringstream oss; oss << "DataArrayDouble::applyLin : The compoId requested (" << compoId << ") is not valid ! Must be in [0," << nbOfComp << ") !";
2427         throw INTERP_KERNEL::Exception(oss.str().c_str());
2428       }
2429     for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
2430       *ptr=a*(*ptr)+b;
2431     this->declareAsNew();
2432   }
2433
2434   /*!
2435    * Apply a linear function to all elements of \a this array, so that
2436    * an element _x_ becomes \f$ a * x + b \f$.
2437    *  \param [in] a - the first coefficient of the function.
2438    *  \param [in] b - the second coefficient of the function.
2439    *  \throw If \a this is not allocated.
2440    */
2441   template<class T>
2442   void DataArrayTemplateClassic<T>::applyLin(T a, T b)
2443   {
2444     this->checkAllocated();
2445     T *ptr(this->getPointer());
2446     std::size_t nbOfElems(this->getNbOfElems());
2447     for(std::size_t i=0;i<nbOfElems;i++,ptr++)
2448       *ptr=a*(*ptr)+b;
2449     this->declareAsNew();
2450   }
2451   
2452   /*!
2453    * Returns a full copy of \a this array except that sign of all elements is reversed.
2454    *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
2455    *          same number of tuples and component as \a this array.
2456    *          The caller is to delete this result array using decrRef() as it is no more
2457    *          needed.
2458    *  \throw If \a this is not allocated.
2459    */
2460   template<class T>
2461   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::negate() const
2462   {
2463     this->checkAllocated();
2464     MCAuto<typename Traits<T>::ArrayType> newArr(Traits<T>::ArrayType::New());
2465     int nbOfTuples(this->getNumberOfTuples()),nbOfComp(this->getNumberOfComponents());
2466     newArr->alloc(nbOfTuples,nbOfComp);
2467     const T *cptr(this->begin());
2468     std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate<T>());
2469     newArr->copyStringInfoFrom(*this);
2470     return newArr.retn();
2471   }
2472
2473   template<class T>
2474   template<class FCT>
2475   void DataArrayTemplateClassic<T>::somethingEqual(const typename Traits<T>::ArrayType *other)
2476   {
2477     if(!other)
2478       throw INTERP_KERNEL::Exception("DataArray<T>::SomethingEqual : input DataArray<T> instance is NULL !");
2479     const char *msg="Nb of tuples mismatch for DataArrayDouble::multiplyEqual !";
2480     this->checkAllocated();
2481     other->checkAllocated();
2482     int nbOfTuple(this->getNumberOfTuples()),nbOfTuple2(other->getNumberOfTuples());
2483     int nbOfComp(this->getNumberOfComponents()),nbOfComp2(other->getNumberOfComponents());
2484     if(nbOfTuple==nbOfTuple2)
2485       {
2486         if(nbOfComp==nbOfComp2)
2487           {
2488             std::transform(this->begin(),this->end(),other->begin(),this->getPointer(),FCT());
2489           }
2490         else if(nbOfComp2==1)
2491           {
2492             T *ptr(this->getPointer());
2493             const T *ptrc(other->begin());
2494             for(int i=0;i<nbOfTuple;i++)
2495               std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(FCT(),*ptrc++));
2496           }
2497         else
2498           throw INTERP_KERNEL::Exception(msg);
2499       }
2500     else if(nbOfTuple2==1)
2501       {
2502         if(nbOfComp2==nbOfComp)
2503           {
2504             T *ptr(this->getPointer());
2505             const T *ptrc(other->begin());
2506             for(int i=0;i<nbOfTuple;i++)
2507               std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,FCT());
2508           }
2509         else
2510           throw INTERP_KERNEL::Exception(msg);
2511       }
2512     else
2513       throw INTERP_KERNEL::Exception(msg);
2514     this->declareAsNew();
2515   }
2516   
2517   /*!
2518    * Adds values of another DataArrayDouble to values of \a this one. There are 3
2519    * valid cases.
2520    * 1.  The arrays have same number of tuples and components. Then each value of
2521    *   \a other array is added to the corresponding value of \a this array, i.e.:
2522    *   _a_ [ i, j ] += _other_ [ i, j ].
2523    * 2.  The arrays have same number of tuples and \a other array has one component. Then
2524    *   _a_ [ i, j ] += _other_ [ i, 0 ].
2525    * 3.  The arrays have same number of components and \a other array has one tuple. Then
2526    *   _a_ [ i, j ] += _a2_ [ 0, j ].
2527    *
2528    *  \param [in] other - an array to add to \a this one.
2529    *  \throw If \a other is NULL.
2530    *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
2531    *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
2532    *         \a other has number of both tuples and components not equal to 1.
2533    */
2534   template<class T>
2535   void DataArrayTemplateClassic<T>::addEqual(const typename Traits<T>::ArrayType *other)
2536   {
2537     this->somethingEqual< std::plus<T> >(other);
2538   }
2539
2540   /*!
2541    * Subtract values of another DataArrayDouble from values of \a this one. There are 3
2542    * valid cases.
2543    * 1.  The arrays have same number of tuples and components. Then each value of
2544    *   \a other array is subtracted from the corresponding value of \a this array, i.e.:
2545    *   _a_ [ i, j ] -= _other_ [ i, j ].
2546    * 2.  The arrays have same number of tuples and \a other array has one component. Then
2547    *   _a_ [ i, j ] -= _other_ [ i, 0 ].
2548    * 3.  The arrays have same number of components and \a other array has one tuple. Then
2549    *   _a_ [ i, j ] -= _a2_ [ 0, j ].
2550    *
2551    *  \param [in] other - an array to subtract from \a this one.
2552    *  \throw If \a other is NULL.
2553    *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
2554    *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
2555    *         \a other has number of both tuples and components not equal to 1.
2556    */
2557   template<class T>
2558   void DataArrayTemplateClassic<T>::substractEqual(const typename Traits<T>::ArrayType *other)
2559   {
2560     this->somethingEqual< std::minus<T> >(other);
2561   }
2562   
2563   /*!
2564    * Multiply values of another DataArrayDouble to values of \a this one. There are 3
2565    * valid cases.
2566    * 1.  The arrays have same number of tuples and components. Then each value of
2567    *   \a other array is multiplied to the corresponding value of \a this array, i.e.
2568    *   _this_ [ i, j ] *= _other_ [ i, j ].
2569    * 2.  The arrays have same number of tuples and \a other array has one component. Then
2570    *   _this_ [ i, j ] *= _other_ [ i, 0 ].
2571    * 3.  The arrays have same number of components and \a other array has one tuple. Then
2572    *   _this_ [ i, j ] *= _a2_ [ 0, j ].
2573    *
2574    *  \param [in] other - an array to multiply to \a this one.
2575    *  \throw If \a other is NULL.
2576    *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
2577    *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
2578    *         \a other has number of both tuples and components not equal to 1.
2579    */
2580   template<class T>
2581   void DataArrayTemplateClassic<T>::multiplyEqual(const typename Traits<T>::ArrayType *other)
2582   {
2583     this->somethingEqual< std::multiplies<T> >(other);
2584   }
2585
2586   /*!
2587    * Divide values of \a this array by values of another DataArrayDouble. There are 3
2588    * valid cases.
2589    * 1.  The arrays have same number of tuples and components. Then each value of
2590    *    \a this array is divided by the corresponding value of \a other one, i.e.:
2591    *   _a_ [ i, j ] /= _other_ [ i, j ].
2592    * 2.  The arrays have same number of tuples and \a other array has one component. Then
2593    *   _a_ [ i, j ] /= _other_ [ i, 0 ].
2594    * 3.  The arrays have same number of components and \a other array has one tuple. Then
2595    *   _a_ [ i, j ] /= _a2_ [ 0, j ].
2596    *
2597    *  \warning No check of division by zero is performed!
2598    *  \param [in] other - an array to divide \a this one by.
2599    *  \throw If \a other is NULL.
2600    *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
2601    *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
2602    *         \a other has number of both tuples and components not equal to 1.
2603    */
2604   template<class T>
2605   void DataArrayTemplateClassic<T>::divideEqual(const typename Traits<T>::ArrayType *other)
2606   {
2607     this->somethingEqual< std::divides<T> >(other);
2608   }
2609   
2610   template<class T, class FCT>
2611   typename Traits<T>::ArrayType *DivSub(const typename Traits<T>::ArrayType *a1, const typename Traits<T>::ArrayType *a2)
2612   {
2613     if(!a1 || !a2)
2614       throw INTERP_KERNEL::Exception("DivSub : input DataArrayDouble instance is NULL !");
2615     int nbOfTuple1(a1->getNumberOfTuples()),nbOfTuple2(a2->getNumberOfTuples());
2616     int nbOfComp1(a1->getNumberOfComponents()),nbOfComp2(a2->getNumberOfComponents());
2617     if(nbOfTuple2==nbOfTuple1)
2618       {
2619         if(nbOfComp1==nbOfComp2)
2620           {
2621             MCAuto<typename Traits<T>::ArrayType> ret(Traits<T>::ArrayType::New());
2622             ret->alloc(nbOfTuple2,nbOfComp1);
2623             std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),FCT());
2624             ret->copyStringInfoFrom(*a1);
2625             return ret.retn();
2626           }
2627         else if(nbOfComp2==1)
2628           {
2629             MCAuto<typename Traits<T>::ArrayType> ret(Traits<T>::ArrayType::New());
2630             ret->alloc(nbOfTuple1,nbOfComp1);
2631             const T *a2Ptr(a2->begin()),*a1Ptr(a1->begin());
2632             T *res(ret->getPointer());
2633             for(int i=0;i<nbOfTuple1;i++)
2634               res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(FCT(),a2Ptr[i]));
2635             ret->copyStringInfoFrom(*a1);
2636             return ret.retn();
2637           }
2638         else
2639           {
2640             a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
2641             return 0;
2642           }
2643       }
2644     else if(nbOfTuple2==1)
2645       {
2646         a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
2647         MCAuto<typename Traits<T>::ArrayType> ret(Traits<T>::ArrayType::New());
2648         ret->alloc(nbOfTuple1,nbOfComp1);
2649         const T *a1ptr=a1->begin(),*a2ptr(a2->begin());
2650         T *pt(ret->getPointer());
2651         for(int i=0;i<nbOfTuple1;i++)
2652           pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,FCT());
2653         ret->copyStringInfoFrom(*a1);
2654         return ret.retn();
2655       }
2656     else
2657       {
2658         a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception
2659         return 0;
2660       }
2661   }
2662   
2663   /*!
2664    * Returns a new DataArrayDouble that is a subtraction of two given arrays. There are 3
2665    * valid cases.
2666    * 1.  The arrays have same number of tuples and components. Then each value of
2667    *   the result array (_a_) is a subtraction of the corresponding values of \a a1 and
2668    *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, j ].
2669    * 2.  The arrays have same number of tuples and one array, say _a2_, has one
2670    *   component. Then
2671    *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, 0 ].
2672    * 3.  The arrays have same number of components and one array, say _a2_, has one
2673    *   tuple. Then
2674    *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ 0, j ].
2675    *
2676    * Info on components is copied either from the first array (in the first case) or from
2677    * the array with maximal number of elements (getNbOfElems()).
2678    *  \param [in] a1 - an array to subtract from.
2679    *  \param [in] a2 - an array to subtract.
2680    *  \return DataArrayDouble * - the new instance of DataArrayDouble.
2681    *          The caller is to delete this result array using decrRef() as it is no more
2682    *          needed.
2683    *  \throw If either \a a1 or \a a2 is NULL.
2684    *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
2685    *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
2686    *         none of them has number of tuples or components equal to 1.
2687    */
2688   template<class T>
2689   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::Substract(const typename Traits<T>::ArrayType *a1, const typename Traits<T>::ArrayType *a2)
2690   {
2691     return DivSub< T,std::minus<T> >(a1,a2);
2692   }
2693   
2694   /*!
2695    * Returns a new DataArrayDouble that is a division of two given arrays. There are 3
2696    * valid cases.
2697    * 1.  The arrays have same number of tuples and components. Then each value of
2698    *   the result array (_a_) is a division of the corresponding values of \a a1 and
2699    *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, j ].
2700    * 2.  The arrays have same number of tuples and one array, say _a2_, has one
2701    *   component. Then
2702    *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, 0 ].
2703    * 3.  The arrays have same number of components and one array, say _a2_, has one
2704    *   tuple. Then
2705    *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ 0, j ].
2706    *
2707    * Info on components is copied either from the first array (in the first case) or from
2708    * the array with maximal number of elements (getNbOfElems()).
2709    *  \warning No check of division by zero is performed!
2710    *  \param [in] a1 - a numerator array.
2711    *  \param [in] a2 - a denominator array.
2712    *  \return DataArrayDouble * - the new instance of DataArrayDouble.
2713    *          The caller is to delete this result array using decrRef() as it is no more
2714    *          needed.
2715    *  \throw If either \a a1 or \a a2 is NULL.
2716    *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
2717    *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
2718    *         none of them has number of tuples or components equal to 1.
2719    */
2720   template<class T>
2721   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::Divide(const typename Traits<T>::ArrayType *a1, const typename Traits<T>::ArrayType *a2)
2722   {
2723     return DivSub< T,std::divides<T> >(a1,a2);
2724   }
2725
2726   template<class T, class FCT>
2727   typename Traits<T>::ArrayType *MulAdd(const typename Traits<T>::ArrayType *a1, const typename Traits<T>::ArrayType *a2)
2728   {
2729     if(!a1 || !a2)
2730       throw INTERP_KERNEL::Exception("DataArrayDouble::MulAdd : input DataArrayDouble instance is NULL !");
2731     int nbOfTuple(a1->getNumberOfTuples()),nbOfTuple2(a2->getNumberOfTuples());
2732     int nbOfComp(a1->getNumberOfComponents()),nbOfComp2(a2->getNumberOfComponents());
2733     MCAuto<typename Traits<T>::ArrayType> ret=0;
2734     if(nbOfTuple==nbOfTuple2)
2735       {
2736         if(nbOfComp==nbOfComp2)
2737           {
2738             ret=Traits<T>::ArrayType::New();
2739             ret->alloc(nbOfTuple,nbOfComp);
2740             std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),FCT());
2741             ret->copyStringInfoFrom(*a1);
2742           }
2743         else
2744           {
2745             int nbOfCompMin,nbOfCompMax;
2746             const typename Traits<T>::ArrayType *aMin, *aMax;
2747             if(nbOfComp>nbOfComp2)
2748               {
2749                 nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
2750                 aMin=a2; aMax=a1;
2751               }
2752             else
2753               {
2754                 nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
2755                 aMin=a1; aMax=a2;
2756               }
2757             if(nbOfCompMin==1)
2758               {
2759                 ret=Traits<T>::ArrayType::New();
2760                 ret->alloc(nbOfTuple,nbOfCompMax);
2761                 const T *aMinPtr(aMin->begin());
2762                 const T *aMaxPtr(aMax->begin());
2763                 T *res=ret->getPointer();
2764                 for(int i=0;i<nbOfTuple;i++)
2765                   res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(FCT(),aMinPtr[i]));
2766                 ret->copyStringInfoFrom(*aMax);
2767               }
2768             else
2769               throw INTERP_KERNEL::Exception("Nb of components mismatch for array MulAdd !");
2770           }
2771       }
2772     else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
2773       {
2774         if(nbOfComp==nbOfComp2)
2775           {
2776             int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
2777             const typename Traits<T>::ArrayType *aMin(nbOfTuple>nbOfTuple2?a2:a1);
2778             const typename Traits<T>::ArrayType *aMax(nbOfTuple>nbOfTuple2?a1:a2);
2779             const T *aMinPtr(aMin->begin()),*aMaxPtr(aMax->begin());
2780             ret=Traits<T>::ArrayType::New();
2781             ret->alloc(nbOfTupleMax,nbOfComp);
2782             T *res(ret->getPointer());
2783             for(int i=0;i<nbOfTupleMax;i++)
2784               res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,FCT());
2785             ret->copyStringInfoFrom(*aMax);
2786           }
2787         else
2788           throw INTERP_KERNEL::Exception("Nb of components mismatch for array MulAdd !");
2789       }
2790     else
2791       throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array MulAdd !");
2792     return ret.retn();
2793   }
2794   
2795   /*!
2796    * Returns a new DataArrayDouble that is a product of two given arrays. There are 3
2797    * valid cases.
2798    * 1.  The arrays have same number of tuples and components. Then each value of
2799    *   the result array (_a_) is a product of the corresponding values of \a a1 and
2800    *   \a a2, i.e. _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, j ].
2801    * 2.  The arrays have same number of tuples and one array, say _a2_, has one
2802    *   component. Then
2803    *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, 0 ].
2804    * 3.  The arrays have same number of components and one array, say _a2_, has one
2805    *   tuple. Then
2806    *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ 0, j ].
2807    *
2808    * Info on components is copied either from the first array (in the first case) or from
2809    * the array with maximal number of elements (getNbOfElems()).
2810    *  \param [in] a1 - a factor array.
2811    *  \param [in] a2 - another factor array.
2812    *  \return DataArrayDouble * - the new instance of DataArrayDouble.
2813    *          The caller is to delete this result array using decrRef() as it is no more
2814    *          needed.
2815    *  \throw If either \a a1 or \a a2 is NULL.
2816    *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
2817    *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
2818    *         none of them has number of tuples or components equal to 1.
2819    */
2820   template<class T>
2821   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::Multiply(const typename Traits<T>::ArrayType *a1, const typename Traits<T>::ArrayType *a2)
2822   {
2823     return MulAdd< T , std::multiplies<T> >(a1,a2);
2824   }
2825   
2826   /*!
2827    * Returns a new DataArrayDouble that is a sum of two given arrays. There are 3
2828    * valid cases.
2829    * 1.  The arrays have same number of tuples and components. Then each value of
2830    *   the result array (_a_) is a sum of the corresponding values of \a a1 and \a a2,
2831    *   i.e.: _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, j ].
2832    * 2.  The arrays have same number of tuples and one array, say _a2_, has one
2833    *   component. Then
2834    *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, 0 ].
2835    * 3.  The arrays have same number of components and one array, say _a2_, has one
2836    *   tuple. Then
2837    *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ 0, j ].
2838    *
2839    * Info on components is copied either from the first array (in the first case) or from
2840    * the array with maximal number of elements (getNbOfElems()).
2841    *  \param [in] a1 - an array to sum up.
2842    *  \param [in] a2 - another array to sum up.
2843    *  \return DataArrayDouble * - the new instance of DataArrayDouble.
2844    *          The caller is to delete this result array using decrRef() as it is no more
2845    *          needed.
2846    *  \throw If either \a a1 or \a a2 is NULL.
2847    *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
2848    *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
2849    *         none of them has number of tuples or components equal to 1.
2850    */
2851   template<class T>
2852   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::Add(const typename Traits<T>::ArrayType *a1, const typename Traits<T>::ArrayType *a2)
2853   {
2854     return MulAdd< T , std::plus<T> >(a1,a2);
2855   }
2856   
2857   /*!
2858    * Returns either a \a deep or \a shallow copy of this array. For more info see
2859    * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
2860    *  \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
2861    *  \return DataArrayDouble * - either a new instance of DataArrayDouble (if \a dCpy
2862    *          == \a true) or \a this instance (if \a dCpy == \a false).
2863    */
2864   template<class T>
2865   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::PerformCopyOrIncrRef(bool dCpy, const typename Traits<T>::ArrayType& self)
2866   {
2867     if(dCpy)
2868       return self.deepCopy();
2869     else
2870       {
2871         self.incrRef();
2872         return const_cast<typename Traits<T>::ArrayType *>(&self);
2873       }
2874   }
2875
2876   template<class T>
2877   struct GreatEqual
2878   {
2879     GreatEqual(T v):_v(v) { }
2880     bool operator()(T v) const { return v>=_v; }
2881     T _v;
2882   };
2883   
2884   template<class T>
2885   struct GreaterThan
2886   {
2887     GreaterThan(T v):_v(v) { }
2888     bool operator()(T v) const { return v>_v; }
2889     T _v;
2890   };
2891   
2892   template<class T>
2893   struct LowerEqual
2894   {
2895     LowerEqual(T v):_v(v) { }
2896     bool operator()(T v) const { return v<=_v; }
2897     T _v;
2898   };
2899   
2900   template<class T>
2901   struct LowerThan
2902   {
2903     LowerThan(T v):_v(v) { }
2904     bool operator()(T v) const { return v<_v; }
2905     T _v;
2906   };
2907   
2908   template<class T>
2909   struct InRange
2910   {
2911     InRange(T a, T b):_a(a),_b(b) { }
2912     bool operator()(T v) const { return v>=_a && v<_b; }
2913     T _a,_b;
2914   };
2915
2916 template<class T>
2917 struct NotInRange
2918 {
2919   NotInRange(T a, T b):_a(a),_b(b) { }
2920   bool operator()(T v) const { return v<_a || v>=_b; }
2921   T _a,_b;
2922 };
2923
2924   /*!
2925    * This method works only on data array with one component. This method returns a newly allocated array storing stored ascendantly of tuple ids in \a this so that this[id]<0.
2926    *
2927    * \return a newly allocated data array that the caller should deal with.
2928    * \sa DataArrayInt::findIdsInRange
2929    */
2930   template<class T>
2931   DataArrayInt *DataArrayTemplateClassic<T>::findIdsStrictlyNegative() const
2932   {
2933     LowerThan<T> lt((T)0);
2934     MCAuto<DataArrayInt> ret(findIdsAdv(lt));
2935     return ret.retn();
2936   }
2937   
2938   template<class T>
2939   MCAuto<DataArrayInt> DataArrayTemplateClassic<T>::findIdsGreaterOrEqualTo(T val) const
2940   {
2941     GreatEqual<T> ge(val);
2942     return findIdsAdv(ge);
2943   }
2944   
2945   template<class T>
2946   MCAuto<DataArrayInt> DataArrayTemplateClassic<T>::findIdsGreaterThan(T val) const
2947   {
2948     GreaterThan<T> gt(val);
2949     return findIdsAdv(gt);
2950   }
2951   
2952   template<class T>
2953   MCAuto<DataArrayInt> DataArrayTemplateClassic<T>::findIdsLowerOrEqualTo(T val) const
2954   {
2955     LowerEqual<T> le(val);
2956     return findIdsAdv(le);
2957   }
2958   
2959   template<class T>
2960   MCAuto<DataArrayInt> DataArrayTemplateClassic<T>::findIdsLowerThan(T val) const
2961   {
2962     LowerThan<T> lt(val);
2963     return findIdsAdv(lt);
2964   }
2965
2966   /*!
2967    * Returns a new DataArrayDouble by aggregating two given arrays, so that (1) the number
2968    * of components in the result array is a sum of the number of components of given arrays
2969    * and (2) the number of tuples in the result array is same as that of each of given
2970    * arrays. In other words the i-th tuple of result array includes all components of
2971    * i-th tuples of all given arrays.
2972    * Number of tuples in the given arrays must be  the same.
2973    *  \param [in] a1 - an array to include in the result array.
2974    *  \param [in] a2 - another array to include in the result array.
2975    *  \return DataArrayDouble * - the new instance of DataArrayDouble.
2976    *          The caller is to delete this result array using decrRef() as it is no more
2977    *          needed.
2978    *  \throw If both \a a1 and \a a2 are NULL.
2979    *  \throw If any given array is not allocated.
2980    *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
2981    */
2982   template<class T>
2983   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::Meld(const typename Traits<T>::ArrayType *a1, const typename Traits<T>::ArrayType *a2)
2984   {
2985     std::vector<const typename Traits<T>::ArrayType *> arr(2);
2986     arr[0]=a1; arr[1]=a2;
2987     return Meld(arr);
2988   }
2989
2990   /*!
2991    * Returns a new DataArrayDouble by aggregating all given arrays, so that (1) the number
2992    * of components in the result array is a sum of the number of components of given arrays
2993    * and (2) the number of tuples in the result array is same as that of each of given
2994    * arrays. In other words the i-th tuple of result array includes all components of
2995    * i-th tuples of all given arrays.
2996    * Number of tuples in the given arrays must be  the same.
2997    *  \param [in] arr - a sequence of arrays to include in the result array.
2998    *  \return DataArrayDouble * - the new instance of DataArrayDouble.
2999    *          The caller is to delete this result array using decrRef() as it is no more
3000    *          needed.
3001    *  \throw If all arrays within \a arr are NULL.
3002    *  \throw If any given array is not allocated.
3003    *  \throw If getNumberOfTuples() of arrays within \a arr is different.
3004    */
3005   template<class T>
3006   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::Meld(const std::vector<const typename Traits<T>::ArrayType *>& arr)
3007   {
3008     std::vector<const typename Traits<T>::ArrayType *> a;
3009     for(typename std::vector<const typename Traits<T>::ArrayType *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
3010       if(*it4)
3011         a.push_back(*it4);
3012     if(a.empty())
3013       throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : input list must contain at least one NON EMPTY DataArrayDouble !");
3014     typename std::vector<const typename Traits<T>::ArrayType *>::const_iterator it;
3015     for(it=a.begin();it!=a.end();it++)
3016       (*it)->checkAllocated();
3017     it=a.begin();
3018     std::size_t nbOfTuples((*it)->getNumberOfTuples());
3019     std::vector<int> nbc(a.size());
3020     std::vector<const T *> pts(a.size());
3021     nbc[0]=(*it)->getNumberOfComponents();
3022     pts[0]=(*it++)->getConstPointer();
3023     for(int i=1;it!=a.end();it++,i++)
3024       {
3025         if(nbOfTuples!=(*it)->getNumberOfTuples())
3026           throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : mismatch of number of tuples !");
3027         nbc[i]=(*it)->getNumberOfComponents();
3028         pts[i]=(*it)->getConstPointer();
3029       }
3030     int totalNbOfComp=std::accumulate(nbc.begin(),nbc.end(),0);
3031     typename Traits<T>::ArrayType *ret(Traits<T>::ArrayType::New());
3032     ret->alloc(nbOfTuples,totalNbOfComp);
3033     T *retPtr(ret->getPointer());
3034     for(std::size_t i=0;i<nbOfTuples;i++)
3035       for(std::size_t j=0;j<a.size();j++)
3036         {
3037           retPtr=std::copy(pts[j],pts[j]+nbc[j],retPtr);
3038           pts[j]+=nbc[j];
3039         }
3040     int k=0;
3041     for(int i=0;i<(int)a.size();i++)
3042       for(int j=0;j<nbc[i];j++,k++)
3043         ret->setInfoOnComponent(k,a[i]->getInfoOnComponent(j));
3044     return ret;
3045   }
3046
3047   /*!
3048    * Returns a new DataArrayDouble holding the same values as \a this array but differently
3049    * arranged in memory. If \a this array holds 2 components of 3 values:
3050    * \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$, then the result array holds these values arranged
3051    * as follows: \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$.
3052    *  \warning Do not confuse this method with transpose()!
3053    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
3054    *          is to delete using decrRef() as it is no more needed.
3055    *  \throw If \a this is not allocated.
3056    */
3057   template<class T>
3058   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::fromNoInterlace() const
3059   {
3060     if(this->_mem.isNull())
3061       throw INTERP_KERNEL::Exception("DataArrayDouble::fromNoInterlace : Not defined array !");
3062     T *tab(this->_mem.fromNoInterlace(this->getNumberOfComponents()));
3063     MCAuto<typename Traits<T>::ArrayType> ret(Traits<T>::ArrayType::New());
3064     ret->useArray(tab,true,C_DEALLOC,this->getNumberOfTuples(),this->getNumberOfComponents());
3065     return ret.retn();
3066   }
3067
3068   /*!
3069    * Returns a new DataArrayDouble holding the same values as \a this array but differently
3070    * arranged in memory. If \a this array holds 2 components of 3 values:
3071    * \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$, then the result array holds these values arranged
3072    * as follows: \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$.
3073    *  \warning Do not confuse this method with transpose()!
3074    *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
3075    *          is to delete using decrRef() as it is no more needed.
3076    *  \throw If \a this is not allocated.
3077    */
3078   template<class T>
3079   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::toNoInterlace() const
3080   {
3081     if(this->_mem.isNull())
3082       throw INTERP_KERNEL::Exception("DataArrayDouble::toNoInterlace : Not defined array !");
3083     T *tab(this->_mem.toNoInterlace(this->getNumberOfComponents()));
3084     MCAuto<typename Traits<T>::ArrayType> ret(Traits<T>::ArrayType::New());
3085     ret->useArray(tab,true,C_DEALLOC,this->getNumberOfTuples(),this->getNumberOfComponents());
3086     return ret.retn();
3087   }
3088   
3089   /*!
3090    * Appends components of another array to components of \a this one, tuple by tuple.
3091    * So that the number of tuples of \a this array remains the same and the number of 
3092    * components increases.
3093    *  \param [in] other - the DataArrayDouble to append to \a this one.
3094    *  \throw If \a this is not allocated.
3095    *  \throw If \a this and \a other arrays have different number of tuples.
3096    *
3097    *  \if ENABLE_EXAMPLES
3098    *  \ref cpp_mcdataarraydouble_meldwith "Here is a C++ example".
3099    *
3100    *  \ref py_mcdataarraydouble_meldwith "Here is a Python example".
3101    *  \endif
3102    */
3103   template<class T>
3104   void DataArrayTemplateClassic<T>::meldWith(const typename Traits<T>::ArrayType *other)
3105   {
3106     this->checkAllocated();
3107     other->checkAllocated();
3108     std::size_t nbOfTuples(this->getNumberOfTuples());
3109     if(nbOfTuples!=other->getNumberOfTuples())
3110       throw INTERP_KERNEL::Exception("DataArrayDouble::meldWith : mismatch of number of tuples !");
3111     int nbOfComp1(this->getNumberOfComponents()),nbOfComp2(other->getNumberOfComponents());
3112     T *newArr=(T *)malloc((nbOfTuples*(nbOfComp1+nbOfComp2))*sizeof(T));
3113     T *w=newArr;
3114     const T *inp1(this->begin()),*inp2(other->begin());
3115     for(std::size_t i=0;i<nbOfTuples;i++,inp1+=nbOfComp1,inp2+=nbOfComp2)
3116       {
3117         w=std::copy(inp1,inp1+nbOfComp1,w);
3118         w=std::copy(inp2,inp2+nbOfComp2,w);
3119       }
3120     this->useArray(newArr,true,C_DEALLOC,nbOfTuples,nbOfComp1+nbOfComp2);
3121     std::vector<int> compIds(nbOfComp2);
3122     for(int i=0;i<nbOfComp2;i++)
3123       compIds[i]=nbOfComp1+i;
3124     this->copyPartOfStringInfoFrom2(compIds,*other);
3125   }
3126
3127   /*!
3128    * 
3129    * \param [in] nbTimes specifies the nb of times each tuples in \a this will be duplicated contiguouly in returned DataArrayDouble instance.
3130    *             \a nbTimes  should be at least equal to 1.
3131    * \return a newly allocated DataArrayDouble having one component and number of tuples equal to \a nbTimes * \c this->getNumberOfTuples.
3132    * \throw if \a this is not allocated or if \a this has not number of components set to one or if \a nbTimes is lower than 1.
3133    */
3134   template<class T>
3135   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::duplicateEachTupleNTimes(int nbTimes) const
3136   {
3137     this->checkAllocated();
3138     if(this->getNumberOfComponents()!=1)
3139       throw INTERP_KERNEL::Exception("DataArrayDouble::duplicateEachTupleNTimes : this should have only one component !");
3140     if(nbTimes<1)
3141       throw INTERP_KERNEL::Exception("DataArrayDouble::duplicateEachTupleNTimes : nb times should be >= 1 !");
3142     int nbTuples(this->getNumberOfTuples());
3143     const T *inPtr(this->begin());
3144     MCAuto<typename Traits<T>::ArrayType> ret(Traits<T>::ArrayType::New()); ret->alloc(nbTimes*nbTuples,1);
3145     T *retPtr(ret->getPointer());
3146     for(int i=0;i<nbTuples;i++,inPtr++)
3147       {
3148         T val(*inPtr);
3149         for(int j=0;j<nbTimes;j++,retPtr++)
3150           *retPtr=val;
3151       }
3152     ret->copyStringInfoFrom(*this);
3153     return ret.retn();
3154   }
3155   
3156   template<class T>
3157   void DataArrayTemplateClassic<T>::aggregate(const typename Traits<T>::ArrayType *other)
3158   {
3159     if(!other)
3160       throw INTERP_KERNEL::Exception("DataArrayDouble::aggregate : null pointer !");
3161     if(this->getNumberOfComponents()!=other->getNumberOfComponents())
3162       throw INTERP_KERNEL::Exception("DataArrayDouble::aggregate : mismatch number of components !");
3163     this->_mem.insertAtTheEnd(other->begin(),other->end());
3164   }
3165
3166   /*!
3167    * Converts every value of \a this array to its absolute value.
3168    * \b WARNING this method is non const. If a new DataArrayDouble instance should be built containing the result of abs DataArrayDouble::computeAbs
3169    * should be called instead.
3170    *
3171    * \throw If \a this is not allocated.
3172    * \sa DataArrayDouble::computeAbs
3173    */
3174   template<class T>
3175   void DataArrayTemplateClassic<T>::abs()
3176   {
3177     this->checkAllocated();
3178     T *ptr(this->getPointer());
3179     std::size_t nbOfElems(this->getNbOfElems());
3180     std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun<T,T>(std::abs));
3181     this->declareAsNew();
3182   }
3183
3184   /*!
3185    * This method builds a new instance of \a this object containing the result of std::abs applied of all elements in \a this.
3186    * This method is a const method (that do not change any values in \a this) contrary to  DataArrayDouble::abs method.
3187    *
3188    * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3189    *         same number of tuples and component as \a this array.
3190    *         The caller is to delete this result array using decrRef() as it is no more
3191    *         needed.
3192    * \throw If \a this is not allocated.
3193    * \sa DataArrayDouble::abs
3194    */
3195   template<class T>
3196   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::computeAbs() const
3197   {
3198     this->checkAllocated();
3199     MCAuto<typename Traits<T>::ArrayType> newArr(Traits<T>::ArrayType::New());
3200     int nbOfTuples(this->getNumberOfTuples());
3201     int nbOfComp(this->getNumberOfComponents());
3202     newArr->alloc(nbOfTuples,nbOfComp);
3203     std::transform(this->begin(),this->end(),newArr->getPointer(),std::ptr_fun<T,T>(std::abs));
3204     newArr->copyStringInfoFrom(*this);
3205     return newArr.retn();
3206   }
3207
3208   /*!
3209    * Returns either a \a deep or \a shallow copy of this array. For more info see
3210    * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
3211    *  \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
3212    *  \return DataArrayDouble * - either a new instance of DataArrayDouble (if \a dCpy
3213    *          == \a true) or \a this instance (if \a dCpy == \a false).
3214    */
3215   template<class T>
3216   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::performCopyOrIncrRef(bool dCpy) const
3217   {
3218     const typename Traits<T>::ArrayType *thisC(static_cast<const typename Traits<T>::ArrayType *>(this));
3219     return DataArrayTemplateClassic<T>::PerformCopyOrIncrRef(dCpy,*thisC);
3220   }
3221
3222   /*!
3223    * Computes for each tuple the sum of number of components values in the tuple and return it.
3224    * 
3225    * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3226    *          same number of tuples as \a this array and one component.
3227    *          The caller is to delete this result array using decrRef() as it is no more
3228    *          needed.
3229    *  \throw If \a this is not allocated.
3230    */
3231   template<class T>
3232   typename Traits<T>::ArrayType *DataArrayTemplateClassic<T>::sumPerTuple() const
3233   {
3234     this->checkAllocated();
3235     std::size_t nbOfComp(this->getNumberOfComponents()),nbOfTuple(this->getNumberOfTuples());
3236     MCAuto<typename Traits<T>::ArrayType> ret(Traits<T>::ArrayType::New());
3237     ret->alloc(nbOfTuple,1);
3238     const T *src(this->begin());
3239     T *dest(ret->getPointer());
3240     for(std::size_t i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
3241       *dest=std::accumulate(src,src+nbOfComp,(T)0);
3242     return ret.retn();
3243   }
3244
3245   /*!
3246    * Set all values in \a this array so that the i-th element equals to \a init + i
3247    * (i starts from zero). To know more on filling arrays see \ref MEDCouplingArrayFill.
3248    *  \param [in] init - value to assign to the first element of array.
3249    *  \throw If \a this->getNumberOfComponents() != 1
3250    *  \throw If \a this is not allocated.
3251    */
3252   template<class T>
3253   void DataArrayTemplateClassic<T>::iota(T init)
3254   {
3255     this->checkAllocated();
3256     if(this->getNumberOfComponents()!=1)
3257       throw INTERP_KERNEL::Exception("DataArrayDouble::iota : works only for arrays with only one component, you can call 'rearrange' method before !");
3258     T *ptr(this->getPointer());
3259     std::size_t ntuples(this->getNumberOfTuples());
3260     for(std::size_t i=0;i<ntuples;i++)
3261       ptr[i]=init+(T)i;
3262     this->declareAsNew();
3263   }
3264
3265   template<class T>
3266   struct ImplReprTraits { static void SetPrecision(std::ostream& oss) { } };
3267
3268   template<>
3269   struct ImplReprTraits<double> {  static void SetPrecision(std::ostream& oss) { oss.precision(17); } };
3270   
3271   template<>
3272   struct ImplReprTraits<float> {  static void SetPrecision(std::ostream& oss) { oss.precision(7); } };
3273   
3274   template<class T>
3275   void DataArrayTemplateClassic<T>::reprStream(std::ostream& stream) const
3276   {
3277     stream << "Name of " << Traits<T>::ReprStr << " array : \"" << this->_name << "\"\n";
3278     reprWithoutNameStream(stream);
3279   }
3280
3281   template<class T>
3282   void DataArrayTemplateClassic<T>::reprZipStream(std::ostream& stream) const
3283   {
3284     stream << "Name of " << Traits<T>::ReprStr << " array : \"" << this->_name << "\"\n";
3285     reprZipWithoutNameStream(stream);
3286   }
3287
3288   template<class T>
3289   void DataArrayTemplateClassic<T>::reprNotTooLongStream(std::ostream& stream) const
3290   {
3291     stream << "Name of "<< Traits<T>::ReprStr << " array : \"" << this->_name << "\"\n";
3292     reprNotTooLongWithoutNameStream(stream);
3293   }
3294
3295   template<class T>
3296   void DataArrayTemplateClassic<T>::reprWithoutNameStream(std::ostream& stream) const
3297   {
3298     DataArray::reprWithoutNameStream(stream);
3299     ImplReprTraits<T>::SetPrecision(stream);
3300     this->_mem.repr(this->getNumberOfComponents(),stream);
3301   }
3302
3303   template<class T>
3304   void DataArrayTemplateClassic<T>::reprZipWithoutNameStream(std::ostream& stream) const
3305   {
3306     DataArray::reprWithoutNameStream(stream);
3307     ImplReprTraits<T>::SetPrecision(stream);
3308     this->_mem.reprZip(this->getNumberOfComponents(),stream);
3309   }
3310
3311   template<class T>
3312   void DataArrayTemplateClassic<T>::reprNotTooLongWithoutNameStream(std::ostream& stream) const
3313   {
3314     DataArray::reprWithoutNameStream(stream);
3315     ImplReprTraits<T>::SetPrecision(stream);
3316     this->_mem.reprNotTooLong(this->getNumberOfComponents(),stream);
3317   }
3318
3319   /*!
3320    * This method is close to repr method except that when \a this has more than 1000 tuples, all tuples are not
3321    * printed out to avoid to consume too much space in interpretor.
3322    * \sa repr
3323    */
3324   template<class T>
3325   std::string DataArrayTemplateClassic<T>::reprNotTooLong() const
3326   {
3327     std::ostringstream ret;
3328     reprNotTooLongStream(ret);
3329     return ret.str();
3330   }
3331   
3332   /////////////////////////////////
3333   
3334   /*!
3335    * Checks if all values in \a this array are equal to \a val at precision \a eps.
3336    *  \param [in] val - value to check equality of array values to.
3337    *  \param [in] eps - precision to check the equality.
3338    *  \return bool - \a true if all values are in range (_val_ - _eps_; _val_ + _eps_),
3339    *                 \a false else.
3340    *  \throw If \a this->getNumberOfComponents() != 1
3341    *  \throw If \a this is not allocated.
3342    */
3343   template<class T>
3344   bool DataArrayTemplateFP<T>::isUniform(T val, T eps) const
3345   {
3346     this->checkAllocated();
3347     if(this->getNumberOfComponents()!=1)
3348       throw INTERP_KERNEL::Exception("DataArrayDouble::isUniform : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before !");
3349     const T *w(this->begin()),*end2(this->end());
3350     const T vmin(val-eps),vmax(val+eps);
3351     for(;w!=end2;w++)
3352       if(*w<vmin || *w>vmax)
3353         return false;
3354     return true;
3355   }
3356
3357   /*!
3358    * Equivalent to DataArrayInt::isEqual except that if false the reason of
3359    * mismatch is given.
3360    * 
3361    * \param [in] other the instance to be compared with \a this
3362    * \param [out] reason In case of inequality returns the reason.
3363    * \sa DataArrayInt::isEqual
3364    */
3365   template<class T>
3366   bool DataArrayDiscrete<T>::isEqualIfNotWhy(const DataArrayDiscrete<T>& other, std::string& reason) const
3367   {
3368     if(!this->areInfoEqualsIfNotWhy(other,reason))
3369       return false;
3370     return this->_mem.isEqual(other._mem,0,reason);
3371   }
3372
3373   /*!
3374    * Checks if \a this and another DataArrayInt are fully equal. For more info see
3375    * \ref MEDCouplingArrayBasicsCompare.
3376    *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
3377    *  \return bool - \a true if the two arrays are equal, \a false else.
3378    */
3379   template<class T>
3380   bool DataArrayDiscrete<T>::isEqual(const DataArrayDiscrete<T>& other) const
3381   {
3382     std::string tmp;
3383     return isEqualIfNotWhy(other,tmp);
3384   }
3385
3386   /*!
3387    * Checks if values of \a this and another DataArrayInt are equal. For more info see
3388    * \ref MEDCouplingArrayBasicsCompare.
3389    *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
3390    *  \return bool - \a true if the values of two arrays are equal, \a false else.
3391    */
3392   template<class T>
3393   bool DataArrayDiscrete<T>::isEqualWithoutConsideringStr(const DataArrayDiscrete<T>& other) const
3394   {
3395     std::string tmp;
3396     return this->_mem.isEqual(other._mem,0,tmp);
3397   }
3398
3399   /*!
3400    * Checks if values of \a this and another DataArrayInt are equal. Comparison is
3401    * performed on sorted value sequences.
3402    * For more info see\ref MEDCouplingArrayBasicsCompare.
3403    *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
3404    *  \return bool - \a true if the sorted values of two arrays are equal, \a false else.
3405    */
3406   template<class T>
3407   bool DataArrayDiscrete<T>::isEqualWithoutConsideringStrAndOrder(const typename Traits<T>::ArrayType& other) const
3408   {
3409     MCAuto<DataArrayInt> a(static_cast<const typename Traits<T>::ArrayType *>(this)->deepCopy()),b(other.deepCopy());
3410     a->sort();
3411     b->sort();
3412     return a->isEqualWithoutConsideringStr(*b);
3413   }
3414   
3415   template<class T>
3416   template<class ALG>
3417   void DataArrayDiscrete<T>::switchOnTupleAlg(T val, std::vector<bool>& vec, ALG algo) const
3418   {
3419     this->checkAllocated();
3420     if(this->getNumberOfComponents()!=1)
3421       throw INTERP_KERNEL::Exception("DataArrayInt::switchOnTupleEqualTo : number of components of this should be equal to one !");
3422     int nbOfTuples(this->getNumberOfTuples());
3423     if(nbOfTuples!=(int)vec.size())
3424       throw INTERP_KERNEL::Exception("DataArrayInt::switchOnTupleEqualTo : number of tuples of this should be equal to size of input vector of bool !");
3425     const T *pt(this->begin());
3426     for(int i=0;i<nbOfTuples;i++)
3427       if(algo(pt[i],val))
3428         vec[i]=true;
3429   }
3430   
3431   /*!
3432    * This method assumes that \a this has one component and is allocated. This method scans all tuples in \a this and for all tuple equal to \a val
3433    * put True to the corresponding entry in \a vec.
3434    * \a vec is expected to be with the same size than the number of tuples of \a this.
3435    *
3436    *  \sa DataArrayInt::switchOnTupleNotEqualTo.
3437    */
3438   template<class T>
3439   void DataArrayDiscrete<T>::switchOnTupleEqualTo(T val, std::vector<bool>& vec) const
3440   {
3441     switchOnTupleAlg(val,vec,std::equal_to<T>());
3442   }
3443
3444   /*!
3445    * This method assumes that \a this has one component and is allocated. This method scans all tuples in \a this and for all tuple different from \a val
3446    * put True to the corresponding entry in \a vec.
3447    * \a vec is expected to be with the same size than the number of tuples of \a this.
3448    * 
3449    *  \sa DataArrayInt::switchOnTupleEqualTo.
3450    */
3451   template<class T>
3452   void DataArrayDiscrete<T>::switchOnTupleNotEqualTo(T val, std::vector<bool>& vec) const
3453   {
3454     switchOnTupleAlg(val,vec,std::not_equal_to<T>());
3455   }
3456
3457   /*!
3458    * Creates a new one-dimensional DataArrayInt of the same size as \a this and a given
3459    * one-dimensional arrays that must be of the same length. The result array describes
3460    * correspondence between \a this and \a other arrays, so that 
3461    * <em> other.getIJ(i,0) == this->getIJ(ret->getIJ(i),0)</em>. If such a permutation is
3462    * not possible because some element in \a other is not in \a this, an exception is thrown.
3463    *  \param [in] other - an array to compute permutation to.
3464    *  \return DataArrayInt * - a new instance of DataArrayInt, which is a permutation array
3465    * from \a this to \a other. The caller is to delete this array using decrRef() as it is
3466    * no more needed.
3467    *  \throw If \a this->getNumberOfComponents() != 1.
3468    *  \throw If \a other->getNumberOfComponents() != 1.
3469    *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples().
3470    *  \throw If \a other includes a value which is not in \a this array.
3471    * 
3472    *  \if ENABLE_EXAMPLES
3473    *  \ref cpp_mcdataarrayint_buildpermutationarr "Here is a C++ example".
3474    *
3475    *  \ref py_mcdataarrayint_buildpermutationarr "Here is a Python example".
3476    *  \endif
3477    */
3478   template<class T>
3479   DataArrayIdType *DataArrayDiscrete<T>::buildPermutationArr(const DataArrayDiscrete<T>& other) const
3480   {
3481     this->checkAllocated();
3482     if(this->getNumberOfComponents()!=1 || other.getNumberOfComponents()!=1)
3483       throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' have to have exactly ONE component !");
3484     std::size_t nbTuple(this->getNumberOfTuples());
3485     other.checkAllocated();
3486     if(nbTuple!=other.getNumberOfTuples())
3487       throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' must have the same number of tuple !");
3488     MCAuto<DataArrayIdType> ret(DataArrayIdType::New());
3489     ret->alloc(nbTuple,1);
3490     ret->fillWithValue(-1);
3491     const T *pt(this->begin());
3492     std::map<int,mcIdType> mm;
3493     for(std::size_t i=0;i<nbTuple;i++)
3494       mm[pt[i]]=(mcIdType)i;
3495     pt=other.begin();
3496     mcIdType *retToFill(ret->getPointer());
3497     for(std::size_t i=0;i<nbTuple;i++)
3498       {
3499         std::map<int,int>::const_iterator it=mm.find(pt[i]);
3500         if(it==mm.end())
3501           {
3502             std::ostringstream oss; oss << "DataArrayInt::buildPermutationArr : Arrays mismatch : element (" << pt[i] << ") in 'other' not findable in 'this' !";
3503             throw INTERP_KERNEL::Exception(oss.str().c_str());
3504           }
3505         retToFill[i]=(*it).second;
3506       }
3507     return ret.retn();
3508   }
3509
3510   /*!
3511    * Elements of \a partOfThis are expected to be included in \a this.
3512    * The returned array \a ret is so that this[ret]==partOfThis
3513    *
3514    * For example, if \a this array contents are [9,10,0,6,4,11,3,8] and if \a partOfThis contains [6,0,11,8]
3515    * the return array will contain [3,2,5,7].
3516    *
3517    * \a this is expected to be a 1 compo allocated array.
3518    * \param [in] partOfThis - A 1 compo allocated array
3519    * \return - A newly allocated array to be dealed by caller having the same number of tuples than \a partOfThis.
3520    * \throw if two same element is present twice in \a this
3521    * \throw if an element in \a partOfThis is \b NOT in \a this.
3522    */
3523   template<class T>
3524   DataArrayIdType *DataArrayDiscrete<T>::indicesOfSubPart(const DataArrayDiscrete<T>& partOfThis) const
3525   {
3526     if(this->getNumberOfComponents()!=1 || partOfThis.getNumberOfComponents()!=1)
3527       throw INTERP_KERNEL::Exception("DataArrayInt::indicesOfSubPart : this and input array must be one component array !");
3528     this->checkAllocated(); partOfThis.checkAllocated();
3529     std::size_t thisNbTuples(this->getNumberOfTuples()),nbTuples(partOfThis.getNumberOfTuples());
3530     const T *thisPt(this->begin()),*pt(partOfThis.begin());
3531     MCAuto<DataArrayIdType> ret(DataArrayIdType::New());
3532     ret->alloc(nbTuples,1);
3533     T *retPt(ret->getPointer());
3534     std::map<int,mcIdType> m;
3535     for(std::size_t i=0;i<thisNbTuples;i++,thisPt++)
3536       m[*thisPt]=(mcIdType)i;
3537     if(m.size()!=thisNbTuples)
3538       throw INTERP_KERNEL::Exception("DataArrayInt::indicesOfSubPart : some elements appears more than once !");
3539     for(std::size_t i=0;i<nbTuples;i++,retPt++,pt++)
3540       {
3541         std::map<int,mcIdType>::const_iterator it(m.find(*pt));
3542         if(it!=m.end())
3543           *retPt=(*it).second;
3544         else
3545           {
3546             std::ostringstream oss; oss << "DataArrayInt::indicesOfSubPart : At pos #" << i << " of input array value is " << *pt << " not in this !";
3547             throw INTERP_KERNEL::Exception(oss.str());
3548           }
3549       }
3550     return ret.retn();
3551   }
3552
3553   /*!
3554    * Checks that \a this array is consistently **increasing** or **decreasing** in value.
3555    * If not an exception is thrown.
3556    *  \param [in] increasing - if \a true, the array values should be increasing.
3557    *  \throw If sequence of values is not strictly monotonic in agreement with \a
3558    *         increasing arg.
3559    *  \throw If \a this->getNumberOfComponents() != 1.
3560    *  \throw If \a this is not allocated.
3561    */
3562   template<class T>
3563   void DataArrayDiscrete<T>::checkMonotonic(bool increasing) const
3564   {
3565     if(!isMonotonic(increasing))
3566       {
3567         if (increasing)
3568           throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not INCREASING monotonic !");
3569         else
3570           throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not DECREASING monotonic !");
3571       }
3572   }
3573
3574   /*!
3575    * Checks that \a this array is consistently **increasing** or **decreasing** in value.
3576    *  \param [in] increasing - if \a true, array values should be increasing.
3577    *  \return bool - \a true if values change in accordance with \a increasing arg.
3578    *  \throw If \a this->getNumberOfComponents() != 1.
3579    *  \throw If \a this is not allocated.
3580    */
3581   template<class T>
3582   bool DataArrayDiscrete<T>::isMonotonic(bool increasing) const
3583   {
3584     this->checkAllocated();
3585     if(this->getNumberOfComponents()!=1)
3586       throw INTERP_KERNEL::Exception("DataArrayInt::isMonotonic : only supported with 'this' array with ONE component !");
3587     std::size_t nbOfElements(this->getNumberOfTuples());
3588     const T *ptr(this->begin());
3589     if(nbOfElements==0)
3590       return true;
3591     T ref(ptr[0]);
3592     if(increasing)
3593       {
3594         for(std::size_t i=1;i<nbOfElements;i++)
3595           {
3596             if(ptr[i]>=ref)
3597               ref=ptr[i];
3598             else
3599               return false;
3600           }
3601       }
3602     else
3603       {
3604         for(std::size_t i=1;i<nbOfElements;i++)
3605           {
3606             if(ptr[i]<=ref)
3607               ref=ptr[i];
3608             else
3609               return false;
3610           }
3611       }
3612     return true;
3613   }
3614
3615   /*!
3616    * This method check that array consistently INCREASING or DECREASING in value.
3617    */
3618   template<class T>
3619   bool DataArrayDiscrete<T>::isStrictlyMonotonic(bool increasing) const
3620   {
3621     this->checkAllocated();
3622     if(this->getNumberOfComponents()!=1)
3623       throw INTERP_KERNEL::Exception("DataArrayInt::isStrictlyMonotonic : only supported with 'this' array with ONE component !");
3624     std::size_t nbOfElements(this->getNumberOfTuples());
3625     const T *ptr(this->begin());
3626     if(nbOfElements==0)
3627       return true;
3628     T ref(ptr[0]);
3629     if(increasing)
3630       {
3631         for(std::size_t i=1;i<nbOfElements;i++)
3632           {
3633             if(ptr[i]>ref)
3634               ref=ptr[i];
3635             else
3636               return false;
3637           }
3638       }
3639     else
3640       {
3641         for(std::size_t i=1;i<nbOfElements;i++)
3642           {
3643             if(ptr[i]<ref)
3644               ref=ptr[i];
3645             else
3646               return false;
3647           }
3648       }
3649     return true;
3650   }
3651
3652   /*!
3653    * This method check that array consistently INCREASING or DECREASING in value.
3654    */
3655   template<class T>
3656   void DataArrayDiscrete<T>::checkStrictlyMonotonic(bool increasing) const
3657   {
3658     if(!isStrictlyMonotonic(increasing))
3659       {
3660         if (increasing)
3661           throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly INCREASING monotonic !");
3662         else
3663           throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly DECREASING monotonic !");
3664       }
3665   }
3666
3667   ////////////////////////////////////
3668
3669   /*!
3670    * This method compares content of input vector \a v and \a this.
3671    * If for each id in \a this v[id]==True and for all other ids id2 not in \a this v[id2]==False, true is returned.
3672    * For performance reasons \a this is expected to be sorted ascendingly. If not an exception will be thrown.
3673    *
3674    * \param [in] v - the vector of 'flags' to be compared with \a this.
3675    *
3676    * \throw If \a this is not sorted ascendingly.
3677    * \throw If \a this has not exactly one component.
3678    * \throw If \a this is not allocated.
3679    */
3680   template<class T>
3681   bool DataArrayDiscreteSigned<T>::isFittingWith(const std::vector<bool>& v) const
3682   {
3683     this->checkAllocated();
3684     if(this->getNumberOfComponents()!=1)
3685       throw INTERP_KERNEL::Exception("DataArrayInt::isFittingWith : number of components of this should be equal to one !");
3686     const T *w(this->begin()),*end2(this->end());
3687     T refVal=-std::numeric_limits<T>::max();
3688     int i=0;
3689     std::vector<bool>::const_iterator it(v.begin());
3690     for(;it!=v.end();it++,i++)
3691       {
3692         if(*it)
3693           {
3694             if(w!=end2)
3695               {
3696                 if(*w++==i)
3697                   {
3698                     if(i>refVal)
3699                       refVal=i;
3700                     else
3701                       {
3702                         std::ostringstream oss; oss << "DataArrayInt::isFittingWith : At pos #" << std::distance(this->begin(),w-1) << " this is not sorted ascendingly !";
3703                         throw INTERP_KERNEL::Exception(oss.str().c_str());
3704                       }
3705                   }
3706                 else
3707                   return false;
3708               }
3709             else
3710               return false;
3711           }
3712       }
3713     return w==end2;
3714   }
3715 }
3716
3717 #endif