Salome HOME
Debug of CMake build procedure
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArray.cxx
1 // Copyright (C) 2007-2014  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 (CEA/DEN)
20
21 #include "MEDCouplingMemArray.txx"
22 #include "MEDCouplingAutoRefCountObjectPtr.hxx"
23
24 #include "BBTree.txx"
25 #include "GenMathFormulae.hxx"
26 #include "InterpKernelAutoPtr.hxx"
27 #include "InterpKernelExprParser.hxx"
28
29 #include <set>
30 #include <cmath>
31 #include <limits>
32 #include <numeric>
33 #include <algorithm>
34 #include <functional>
35
36 typedef double (*MYFUNCPTR)(double);
37
38 using namespace ParaMEDMEM;
39
40 template<int SPACEDIM>
41 void DataArrayDouble::findCommonTuplesAlg(const double *bbox, int nbNodes, int limitNodeId, double prec, DataArrayInt *c, DataArrayInt *cI) const
42 {
43   const double *coordsPtr=getConstPointer();
44   BBTreePts<SPACEDIM,int> myTree(bbox,0,0,nbNodes,prec);
45   std::vector<bool> isDone(nbNodes);
46   for(int i=0;i<nbNodes;i++)
47     {
48       if(!isDone[i])
49         {
50           std::vector<int> intersectingElems;
51           myTree.getElementsAroundPoint(coordsPtr+i*SPACEDIM,intersectingElems);
52           if(intersectingElems.size()>1)
53             {
54               std::vector<int> commonNodes;
55               for(std::vector<int>::const_iterator it=intersectingElems.begin();it!=intersectingElems.end();it++)
56                 if(*it!=i)
57                   if(*it>=limitNodeId)
58                     {
59                       commonNodes.push_back(*it);
60                       isDone[*it]=true;
61                     }
62               if(!commonNodes.empty())
63                 {
64                   cI->pushBackSilent(cI->back()+(int)commonNodes.size()+1);
65                   c->pushBackSilent(i);
66                   c->insertAtTheEnd(commonNodes.begin(),commonNodes.end());
67                 }
68             }
69         }
70     }
71 }
72
73 template<int SPACEDIM>
74 void DataArrayDouble::FindTupleIdsNearTuplesAlg(const BBTreePts<SPACEDIM,int>& myTree, const double *pos, int nbOfTuples, double eps,
75                                                 DataArrayInt *c, DataArrayInt *cI)
76 {
77   for(int i=0;i<nbOfTuples;i++)
78     {
79       std::vector<int> intersectingElems;
80       myTree.getElementsAroundPoint(pos+i*SPACEDIM,intersectingElems);
81       std::vector<int> commonNodes;
82       for(std::vector<int>::const_iterator it=intersectingElems.begin();it!=intersectingElems.end();it++)
83         commonNodes.push_back(*it);
84       cI->pushBackSilent(cI->back()+(int)commonNodes.size());
85       c->insertAtTheEnd(commonNodes.begin(),commonNodes.end());
86     }
87 }
88
89 template<int SPACEDIM>
90 void DataArrayDouble::FindClosestTupleIdAlg(const BBTreePts<SPACEDIM,int>& myTree, double dist, const double *pos, int nbOfTuples, const double *thisPt, int thisNbOfTuples, int *res)
91 {
92   double distOpt(dist);
93   const double *p(pos);
94   int *r(res);
95   for(int i=0;i<nbOfTuples;i++,p+=SPACEDIM,r++)
96     {
97       while(true)
98         {
99           int elem=-1;
100           double ret=myTree.getElementsAroundPoint2(p,distOpt,elem);
101           if(ret!=std::numeric_limits<double>::max())
102             {
103               distOpt=std::max(ret,1e-4);
104               *r=elem;
105               break;
106             }
107           else
108             { distOpt=2*distOpt; continue; }
109         }
110     }
111 }
112
113 std::size_t DataArray::getHeapMemorySizeWithoutChildren() const
114 {
115   std::size_t sz1=_name.capacity();
116   std::size_t sz2=_info_on_compo.capacity();
117   std::size_t sz3=0;
118   for(std::vector<std::string>::const_iterator it=_info_on_compo.begin();it!=_info_on_compo.end();it++)
119     sz3+=(*it).capacity();
120   return sz1+sz2+sz3;
121 }
122
123 std::vector<const BigMemoryObject *> DataArray::getDirectChildren() const
124 {
125   return std::vector<const BigMemoryObject *>();
126 }
127
128 /*!
129  * Sets the attribute \a _name of \a this array.
130  * See \ref MEDCouplingArrayBasicsName "DataArrays infos" for more information.
131  *  \param [in] name - new array name
132  */
133 void DataArray::setName(const std::string& name)
134 {
135   _name=name;
136 }
137
138 /*!
139  * Copies textual data from an \a other DataArray. The copied data are
140  * - the name attribute,
141  * - the information of components.
142  *
143  * For more information on these data see \ref MEDCouplingArrayBasicsName "DataArrays infos".
144  *
145  *  \param [in] other - another instance of DataArray to copy the textual data from.
146  *  \throw If number of components of \a this array differs from that of the \a other.
147  */
148 void DataArray::copyStringInfoFrom(const DataArray& other)
149 {
150   if(_info_on_compo.size()!=other._info_on_compo.size())
151     throw INTERP_KERNEL::Exception("Size of arrays mismatches on copyStringInfoFrom !");
152   _name=other._name;
153   _info_on_compo=other._info_on_compo;
154 }
155
156 void DataArray::copyPartOfStringInfoFrom(const DataArray& other, const std::vector<int>& compoIds)
157 {
158   int nbOfCompoOth=other.getNumberOfComponents();
159   std::size_t newNbOfCompo=compoIds.size();
160   for(std::size_t i=0;i<newNbOfCompo;i++)
161     if(compoIds[i]>=nbOfCompoOth || compoIds[i]<0)
162       {
163         std::ostringstream oss; oss << "Specified component id is out of range (" << compoIds[i] << ") compared with nb of actual components (" << nbOfCompoOth << ")";
164         throw INTERP_KERNEL::Exception(oss.str().c_str());
165       }
166   for(std::size_t i=0;i<newNbOfCompo;i++)
167     setInfoOnComponent((int)i,other.getInfoOnComponent(compoIds[i]));
168 }
169
170 void DataArray::copyPartOfStringInfoFrom2(const std::vector<int>& compoIds, const DataArray& other)
171 {
172   int nbOfCompo=getNumberOfComponents();
173   std::size_t partOfCompoToSet=compoIds.size();
174   if((int)partOfCompoToSet!=other.getNumberOfComponents())
175     throw INTERP_KERNEL::Exception("Given compoIds has not the same size as number of components of given array !");
176   for(std::size_t i=0;i<partOfCompoToSet;i++)
177     if(compoIds[i]>=nbOfCompo || compoIds[i]<0)
178       {
179         std::ostringstream oss; oss << "Specified component id is out of range (" << compoIds[i] << ") compared with nb of actual components (" << nbOfCompo << ")";
180         throw INTERP_KERNEL::Exception(oss.str().c_str());
181       }
182   for(std::size_t i=0;i<partOfCompoToSet;i++)
183     setInfoOnComponent(compoIds[i],other.getInfoOnComponent((int)i));
184 }
185
186 bool DataArray::areInfoEqualsIfNotWhy(const DataArray& other, std::string& reason) const
187 {
188   std::ostringstream oss;
189   if(_name!=other._name)
190     {
191       oss << "Names DataArray mismatch : this name=\"" << _name << " other name=\"" << other._name << "\" !";
192       reason=oss.str();
193       return false;
194     }
195   if(_info_on_compo!=other._info_on_compo)
196     {
197       oss << "Components DataArray mismatch : \nThis components=";
198       for(std::vector<std::string>::const_iterator it=_info_on_compo.begin();it!=_info_on_compo.end();it++)
199         oss << "\"" << *it << "\",";
200       oss << "\nOther components=";
201       for(std::vector<std::string>::const_iterator it=other._info_on_compo.begin();it!=other._info_on_compo.end();it++)
202         oss << "\"" << *it << "\",";
203       reason=oss.str();
204       return false;
205     }
206   return true;
207 }
208
209 /*!
210  * Compares textual information of \a this DataArray with that of an \a other one.
211  * The compared data are
212  * - the name attribute,
213  * - the information of components.
214  *
215  * For more information on these data see \ref MEDCouplingArrayBasicsName "DataArrays infos".
216  *  \param [in] other - another instance of DataArray to compare the textual data of.
217  *  \return bool - \a true if the textual information is same, \a false else.
218  */
219 bool DataArray::areInfoEquals(const DataArray& other) const
220 {
221   std::string tmp;
222   return areInfoEqualsIfNotWhy(other,tmp);
223 }
224
225 void DataArray::reprWithoutNameStream(std::ostream& stream) const
226 {
227   stream << "Number of components : "<< getNumberOfComponents() << "\n";
228   stream << "Info of these components : ";
229   for(std::vector<std::string>::const_iterator iter=_info_on_compo.begin();iter!=_info_on_compo.end();iter++)
230     stream << "\"" << *iter << "\"   ";
231   stream << "\n";
232 }
233
234 std::string DataArray::cppRepr(const std::string& varName) const
235 {
236   std::ostringstream ret;
237   reprCppStream(varName,ret);
238   return ret.str();
239 }
240
241 /*!
242  * Sets information on all components. To know more on format of this information
243  * see \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
244  *  \param [in] info - a vector of strings.
245  *  \throw If size of \a info differs from the number of components of \a this.
246  */
247 void DataArray::setInfoOnComponents(const std::vector<std::string>& info)
248 {
249   if(getNumberOfComponents()!=(int)info.size())
250     {
251       std::ostringstream oss; oss << "DataArray::setInfoOnComponents : input is of size " << info.size() << " whereas number of components is equal to " << getNumberOfComponents() << " !";
252       throw INTERP_KERNEL::Exception(oss.str().c_str());
253     }
254   _info_on_compo=info;
255 }
256
257 /*!
258  * This method is only a dispatcher towards DataArrayDouble::setPartOfValues3, DataArrayInt::setPartOfValues3, DataArrayChar::setPartOfValues3 depending on the true
259  * type of \a this and \a aBase.
260  *
261  * \throw If \a aBase and \a this do not have the same type.
262  *
263  * \sa DataArrayDouble::setPartOfValues3, DataArrayInt::setPartOfValues3, DataArrayChar::setPartOfValues3.
264  */
265 void DataArray::setPartOfValuesBase3(const DataArray *aBase, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
266 {
267   if(!aBase)
268     throw INTERP_KERNEL::Exception("DataArray::setPartOfValuesBase3 : input aBase object is NULL !");
269   DataArrayDouble *this1(dynamic_cast<DataArrayDouble *>(this));
270   DataArrayInt *this2(dynamic_cast<DataArrayInt *>(this));
271   DataArrayChar *this3(dynamic_cast<DataArrayChar *>(this));
272   const DataArrayDouble *a1(dynamic_cast<const DataArrayDouble *>(aBase));
273   const DataArrayInt *a2(dynamic_cast<const DataArrayInt *>(aBase));
274   const DataArrayChar *a3(dynamic_cast<const DataArrayChar *>(aBase));
275   if(this1 && a1)
276     {
277       this1->setPartOfValues3(a1,bgTuples,endTuples,bgComp,endComp,stepComp,strictCompoCompare);
278       return ;
279     }
280   if(this2 && a2)
281     {
282       this2->setPartOfValues3(a2,bgTuples,endTuples,bgComp,endComp,stepComp,strictCompoCompare);
283       return ;
284     }
285   if(this3 && a3)
286     {
287       this3->setPartOfValues3(a3,bgTuples,endTuples,bgComp,endComp,stepComp,strictCompoCompare);
288       return ;
289     }
290   throw INTERP_KERNEL::Exception("DataArray::setPartOfValuesBase3 : input aBase object and this do not have the same type !");
291 }
292
293 std::vector<std::string> DataArray::getVarsOnComponent() const
294 {
295   int nbOfCompo=(int)_info_on_compo.size();
296   std::vector<std::string> ret(nbOfCompo);
297   for(int i=0;i<nbOfCompo;i++)
298     ret[i]=getVarOnComponent(i);
299   return ret;
300 }
301
302 std::vector<std::string> DataArray::getUnitsOnComponent() const
303 {
304   int nbOfCompo=(int)_info_on_compo.size();
305   std::vector<std::string> ret(nbOfCompo);
306   for(int i=0;i<nbOfCompo;i++)
307     ret[i]=getUnitOnComponent(i);
308   return ret;
309 }
310
311 /*!
312  * Returns information on a component specified by an index.
313  * To know more on format of this information
314  * see \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
315  *  \param [in] i - the index (zero based) of the component of interest.
316  *  \return std::string - a string containing the information on \a i-th component.
317  *  \throw If \a i is not a valid component index.
318  */
319 std::string DataArray::getInfoOnComponent(int i) const
320 {
321   if(i<(int)_info_on_compo.size() && i>=0)
322     return _info_on_compo[i];
323   else
324     {
325       std::ostringstream oss; oss << "DataArray::getInfoOnComponent : Specified component id is out of range (" << i << ") compared with nb of actual components (" << (int) _info_on_compo.size();
326       throw INTERP_KERNEL::Exception(oss.str().c_str());
327     }
328 }
329
330 /*!
331  * Returns the var part of the full information of the \a i-th component.
332  * For example, if \c getInfoOnComponent(0) returns "SIGXY [N/m^2]", then
333  * \c getVarOnComponent(0) returns "SIGXY".
334  * If a unit part of information is not detected by presence of
335  * two square brackets, then the full information is returned.
336  * To read more about the component information format, see
337  * \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
338  *  \param [in] i - the index (zero based) of the component of interest.
339  *  \return std::string - a string containing the var information, or the full info.
340  *  \throw If \a i is not a valid component index.
341  */
342 std::string DataArray::getVarOnComponent(int i) const
343 {
344   if(i<(int)_info_on_compo.size() && i>=0)
345     {
346       return GetVarNameFromInfo(_info_on_compo[i]);
347     }
348   else
349     {
350       std::ostringstream oss; oss << "DataArray::getVarOnComponent : Specified component id is out of range  (" << i << ") compared with nb of actual components (" << (int) _info_on_compo.size();
351       throw INTERP_KERNEL::Exception(oss.str().c_str());
352     }
353 }
354
355 /*!
356  * Returns the unit part of the full information of the \a i-th component.
357  * For example, if \c getInfoOnComponent(0) returns "SIGXY [ N/m^2]", then
358  * \c getUnitOnComponent(0) returns " N/m^2".
359  * If a unit part of information is not detected by presence of
360  * two square brackets, then an empty string is returned.
361  * To read more about the component information format, see
362  * \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
363  *  \param [in] i - the index (zero based) of the component of interest.
364  *  \return std::string - a string containing the unit information, if any, or "".
365  *  \throw If \a i is not a valid component index.
366  */
367 std::string DataArray::getUnitOnComponent(int i) const
368 {
369   if(i<(int)_info_on_compo.size() && i>=0)
370     {
371       return GetUnitFromInfo(_info_on_compo[i]);
372     }
373   else
374     {
375       std::ostringstream oss; oss << "DataArray::getUnitOnComponent : Specified component id is out of range  (" << i << ") compared with nb of actual components (" << (int) _info_on_compo.size();
376       throw INTERP_KERNEL::Exception(oss.str().c_str());
377     }
378 }
379
380 /*!
381  * Returns the var part of the full component information.
382  * For example, if \a info == "SIGXY [N/m^2]", then this method returns "SIGXY".
383  * If a unit part of information is not detected by presence of
384  * two square brackets, then the whole \a info is returned.
385  * To read more about the component information format, see
386  * \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
387  *  \param [in] info - the full component information.
388  *  \return std::string - a string containing only var information, or the \a info.
389  */
390 std::string DataArray::GetVarNameFromInfo(const std::string& info)
391 {
392   std::size_t p1=info.find_last_of('[');
393   std::size_t p2=info.find_last_of(']');
394   if(p1==std::string::npos || p2==std::string::npos)
395     return info;
396   if(p1>p2)
397     return info;
398   if(p1==0)
399     return std::string();
400   std::size_t p3=info.find_last_not_of(' ',p1-1);
401   return info.substr(0,p3+1);
402 }
403
404 /*!
405  * Returns the unit part of the full component information.
406  * For example, if \a info == "SIGXY [ N/m^2]", then this method returns " N/m^2".
407  * If a unit part of information is not detected by presence of
408  * two square brackets, then an empty string is returned.
409  * To read more about the component information format, see
410  * \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
411  *  \param [in] info - the full component information.
412  *  \return std::string - a string containing only unit information, if any, or "".
413  */
414 std::string DataArray::GetUnitFromInfo(const std::string& info)
415 {
416   std::size_t p1=info.find_last_of('[');
417   std::size_t p2=info.find_last_of(']');
418   if(p1==std::string::npos || p2==std::string::npos)
419     return std::string();
420   if(p1>p2)
421     return std::string();
422   return info.substr(p1+1,p2-p1-1);
423 }
424
425 /*!
426  * Returns a new DataArray by concatenating all given arrays, so that (1) the number
427  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
428  * the number of component in the result array is same as that of each of given arrays.
429  * Info on components is copied from the first of the given arrays. Number of components
430  * in the given arrays must be  the same.
431  *  \param [in] arrs - a sequence of arrays to include in the result array. All arrays must have the same type.
432  *  \return DataArray * - the new instance of DataArray (that can be either DataArrayInt, DataArrayDouble, DataArrayChar).
433  *          The caller is to delete this result array using decrRef() as it is no more
434  *          needed.
435  *  \throw If all arrays within \a arrs are NULL.
436  *  \throw If all not null arrays in \a arrs have not the same type.
437  *  \throw If getNumberOfComponents() of arrays within \a arrs.
438  */
439 DataArray *DataArray::Aggregate(const std::vector<const DataArray *>& arrs)
440 {
441   std::vector<const DataArray *> arr2;
442   for(std::vector<const DataArray *>::const_iterator it=arrs.begin();it!=arrs.end();it++)
443     if(*it)
444       arr2.push_back(*it);
445   if(arr2.empty())
446     throw INTERP_KERNEL::Exception("DataArray::Aggregate : only null instance in input vector !");
447   std::vector<const DataArrayDouble *> arrd;
448   std::vector<const DataArrayInt *> arri;
449   std::vector<const DataArrayChar *> arrc;
450   for(std::vector<const DataArray *>::const_iterator it=arr2.begin();it!=arr2.end();it++)
451     {
452       const DataArrayDouble *a=dynamic_cast<const DataArrayDouble *>(*it);
453       if(a)
454         { arrd.push_back(a); continue; }
455       const DataArrayInt *b=dynamic_cast<const DataArrayInt *>(*it);
456       if(b)
457         { arri.push_back(b); continue; }
458       const DataArrayChar *c=dynamic_cast<const DataArrayChar *>(*it);
459       if(c)
460         { arrc.push_back(c); continue; }
461       throw INTERP_KERNEL::Exception("DataArray::Aggregate : presence of not null instance in inuput that is not in [DataArrayDouble, DataArrayInt, DataArrayChar] !");
462     }
463   if(arr2.size()==arrd.size())
464     return DataArrayDouble::Aggregate(arrd);
465   if(arr2.size()==arri.size())
466     return DataArrayInt::Aggregate(arri);
467   if(arr2.size()==arrc.size())
468     return DataArrayChar::Aggregate(arrc);
469   throw INTERP_KERNEL::Exception("DataArray::Aggregate : all input arrays must have the same type !");
470 }
471
472 /*!
473  * Sets information on a component specified by an index.
474  * To know more on format of this information
475  * see \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
476  *  \warning Don't pass NULL as \a info!
477  *  \param [in] i - the index (zero based) of the component of interest.
478  *  \param [in] info - the string containing the information.
479  *  \throw If \a i is not a valid component index.
480  */
481 void DataArray::setInfoOnComponent(int i, const std::string& info)
482 {
483   if(i<(int)_info_on_compo.size() && i>=0)
484     _info_on_compo[i]=info;
485   else
486     {
487       std::ostringstream oss; oss << "DataArray::setInfoOnComponent : Specified component id is out of range  (" << i << ") compared with nb of actual components (" << (int) _info_on_compo.size();
488       throw INTERP_KERNEL::Exception(oss.str().c_str());
489     }
490 }
491
492 /*!
493  * Sets information on all components. This method can change number of components
494  * at certain conditions; if the conditions are not respected, an exception is thrown.
495  * The number of components can be changed in \a this only if \a this is not allocated.
496  * The condition of number of components must not be changed.
497  *
498  * To know more on format of the component information see
499  * \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
500  *  \param [in] info - a vector of component infos.
501  *  \throw If \a this->getNumberOfComponents() != \a info.size() && \a this->isAllocated()
502  */
503 void DataArray::setInfoAndChangeNbOfCompo(const std::vector<std::string>& info)
504 {
505   if(getNumberOfComponents()!=(int)info.size())
506     {
507       if(!isAllocated())
508         _info_on_compo=info;
509       else
510         {
511           std::ostringstream oss; oss << "DataArray::setInfoAndChangeNbOfCompo : input is of size " << info.size() << " whereas number of components is equal to " << getNumberOfComponents() << "  and this is already allocated !";
512           throw INTERP_KERNEL::Exception(oss.str().c_str());
513         }
514     }
515   else
516     _info_on_compo=info;
517 }
518
519 void DataArray::checkNbOfTuples(int nbOfTuples, const std::string& msg) const
520 {
521   if(getNumberOfTuples()!=nbOfTuples)
522     {
523       std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " <<  nbOfTuples << " having " << getNumberOfTuples() << " !";
524       throw INTERP_KERNEL::Exception(oss.str().c_str());
525     }
526 }
527
528 void DataArray::checkNbOfComps(int nbOfCompo, const std::string& msg) const
529 {
530   if(getNumberOfComponents()!=nbOfCompo)
531     {
532       std::ostringstream oss; oss << msg << " : mismatch number of components : expected " << nbOfCompo << " having " << getNumberOfComponents() << " !";
533       throw INTERP_KERNEL::Exception(oss.str().c_str());
534     }
535 }
536
537 void DataArray::checkNbOfElems(std::size_t nbOfElems, const std::string& msg) const
538 {
539   if(getNbOfElems()!=nbOfElems)
540     {
541       std::ostringstream oss; oss << msg << " : mismatch number of elems : Expected " << nbOfElems << " having " << getNbOfElems() << " !";
542       throw INTERP_KERNEL::Exception(oss.str().c_str());
543     }
544 }
545
546 void DataArray::checkNbOfTuplesAndComp(const DataArray& other, const std::string& msg) const
547 {
548    if(getNumberOfTuples()!=other.getNumberOfTuples())
549     {
550       std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " <<  other.getNumberOfTuples() << " having " << getNumberOfTuples() << " !";
551       throw INTERP_KERNEL::Exception(oss.str().c_str());
552     }
553   if(getNumberOfComponents()!=other.getNumberOfComponents())
554     {
555       std::ostringstream oss; oss << msg << " : mismatch number of components : expected " << other.getNumberOfComponents() << " having " << getNumberOfComponents() << " !";
556       throw INTERP_KERNEL::Exception(oss.str().c_str());
557     }
558 }
559
560 void DataArray::checkNbOfTuplesAndComp(int nbOfTuples, int nbOfCompo, const std::string& msg) const
561 {
562   checkNbOfTuples(nbOfTuples,msg);
563   checkNbOfComps(nbOfCompo,msg);
564 }
565
566 /*!
567  * Simply this method checks that \b value is in [0,\b ref).
568  */
569 void DataArray::CheckValueInRange(int ref, int value, const std::string& msg)
570 {
571   if(value<0 || value>=ref)
572     {
573       std::ostringstream oss; oss << "DataArray::CheckValueInRange : " << msg  << " ! Expected in range [0," << ref << "[ having " << value << " !";
574       throw INTERP_KERNEL::Exception(oss.str().c_str());
575     }
576 }
577
578 /*!
579  * This method checks that [\b start, \b end) is compliant with ref length \b value.
580  * typicaly start in [0,\b value) and end in [0,\b value). If value==start and start==end, it is supported.
581  */
582 void DataArray::CheckValueInRangeEx(int value, int start, int end, const std::string& msg)
583 {
584   if(start<0 || start>=value)
585     {
586       if(value!=start || end!=start)
587         {
588           std::ostringstream oss; oss << "DataArray::CheckValueInRangeEx : " << msg  << " ! Expected start " << start << " of input range, in [0," << value << "[ !";
589           throw INTERP_KERNEL::Exception(oss.str().c_str());
590         }
591     }
592   if(end<0 || end>value)
593     {
594       std::ostringstream oss; oss << "DataArray::CheckValueInRangeEx : " << msg  << " ! Expected end " << end << " of input range, in [0," << value << "] !";
595       throw INTERP_KERNEL::Exception(oss.str().c_str());
596     }
597 }
598
599 void DataArray::CheckClosingParInRange(int ref, int value, const std::string& msg)
600 {
601   if(value<0 || value>ref)
602     {
603       std::ostringstream oss; oss << "DataArray::CheckClosingParInRange : " << msg  << " ! Expected input range in [0," << ref << "] having closing open parenthesis " << value << " !";
604       throw INTERP_KERNEL::Exception(oss.str().c_str());
605     }
606 }
607
608 /*!
609  * This method is useful to slice work among a pool of threads or processes. \a begin, \a end \a step is the input whole slice of work to perform, 
610  * typically it is a whole slice of tuples of DataArray or cells, nodes of a mesh...
611  *
612  * The input \a sliceId should be an id in [0, \a nbOfSlices) that specifies the slice of work.
613  *
614  * \param [in] start - the start of the input slice of the whole work to perform splitted into slices.
615  * \param [in] stop - the stop of the input slice of the whole work to perform splitted into slices.
616  * \param [in] step - the step (that can be <0) of the input slice of the whole work to perform splitted into slices.
617  * \param [in] sliceId - the slice id considered
618  * \param [in] nbOfSlices - the number of slices (typically the number of cores on which the work is expected to be sliced)
619  * \param [out] startSlice - the start of the slice considered
620  * \param [out] stopSlice - the stop of the slice consided
621  * 
622  * \throw If \a step == 0
623  * \throw If \a nbOfSlices not > 0
624  * \throw If \a sliceId not in [0,nbOfSlices)
625  */
626 void DataArray::GetSlice(int start, int stop, int step, int sliceId, int nbOfSlices, int& startSlice, int& stopSlice)
627 {
628   if(nbOfSlices<=0)
629     {
630       std::ostringstream oss; oss << "DataArray::GetSlice : nbOfSlices (" << nbOfSlices << ") must be > 0 !";
631       throw INTERP_KERNEL::Exception(oss.str().c_str());
632     }
633   if(sliceId<0 || sliceId>=nbOfSlices)
634     {
635       std::ostringstream oss; oss << "DataArray::GetSlice : sliceId (" << nbOfSlices << ") must be in [0 , nbOfSlices (" << nbOfSlices << ") ) !";
636       throw INTERP_KERNEL::Exception(oss.str().c_str());
637     }
638   int nbElems=GetNumberOfItemGivenBESRelative(start,stop,step,"DataArray::GetSlice");
639   int minNbOfElemsPerSlice=nbElems/nbOfSlices;
640   startSlice=start+minNbOfElemsPerSlice*step*sliceId;
641   if(sliceId<nbOfSlices-1)
642     stopSlice=start+minNbOfElemsPerSlice*step*(sliceId+1);
643   else
644     stopSlice=stop;
645 }
646
647 int DataArray::GetNumberOfItemGivenBES(int begin, int end, int step, const std::string& msg)
648 {
649   if(end<begin)
650     {
651       std::ostringstream oss; oss << msg << " : end before begin !";
652       throw INTERP_KERNEL::Exception(oss.str().c_str());
653     }
654   if(end==begin)
655     return 0;
656   if(step<=0)
657     {
658       std::ostringstream oss; oss << msg << " : invalid step should be > 0 !";
659       throw INTERP_KERNEL::Exception(oss.str().c_str());
660     }
661   return (end-1-begin)/step+1;
662 }
663
664 int DataArray::GetNumberOfItemGivenBESRelative(int begin, int end, int step, const std::string& msg)
665 {
666   if(step==0)
667     throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES : step=0 is not allowed !");
668   if(end<begin && step>0)
669     {
670       std::ostringstream oss; oss << msg << " : end before begin whereas step is positive !";
671       throw INTERP_KERNEL::Exception(oss.str().c_str());
672     }
673   if(begin<end && step<0)
674     {
675       std::ostringstream oss; oss << msg << " : invalid step should be > 0 !";
676       throw INTERP_KERNEL::Exception(oss.str().c_str());
677     }
678   if(begin!=end)
679     return (std::max(begin,end)-1-std::min(begin,end))/std::abs(step)+1;
680   else
681     return 0;
682 }
683
684 int DataArray::GetPosOfItemGivenBESRelativeNoThrow(int value, int begin, int end, int step)
685 {
686   if(step!=0)
687     {
688       if(step>0)
689         {
690           if(begin<=value && value<end)
691             {
692               if((value-begin)%step==0)
693                 return (value-begin)/step;
694               else
695                 return -1;
696             }
697           else
698             return -1;
699         }
700       else
701         {
702           if(begin>=value && value>end)
703             {
704               if((begin-value)%(-step)==0)
705                 return (begin-value)/(-step);
706               else
707                 return -1;
708             }
709           else
710             return -1;
711         }
712     }
713   else
714     return -1;
715 }
716
717 /*!
718  * Returns a new instance of DataArrayDouble. The caller is to delete this array
719  * using decrRef() as it is no more needed. 
720  */
721 DataArrayDouble *DataArrayDouble::New()
722 {
723   return new DataArrayDouble;
724 }
725
726 /*!
727  * Checks if raw data is allocated. Read more on the raw data
728  * in \ref MEDCouplingArrayBasicsTuplesAndCompo "DataArrays infos" for more information.
729  *  \return bool - \a true if the raw data is allocated, \a false else.
730  */
731 bool DataArrayDouble::isAllocated() const
732 {
733   return getConstPointer()!=0;
734 }
735
736 /*!
737  * Checks if raw data is allocated and throws an exception if it is not the case.
738  *  \throw If the raw data is not allocated.
739  */
740 void DataArrayDouble::checkAllocated() const
741 {
742   if(!isAllocated())
743     throw INTERP_KERNEL::Exception("DataArrayDouble::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !");
744 }
745
746 /*!
747  * This method desallocated \a this without modification of informations relative to the components.
748  * After call of this method, DataArrayDouble::isAllocated will return false.
749  * If \a this is already not allocated, \a this is let unchanged.
750  */
751 void DataArrayDouble::desallocate()
752 {
753   _mem.destroy();
754 }
755
756 std::size_t DataArrayDouble::getHeapMemorySizeWithoutChildren() const
757 {
758   std::size_t sz(_mem.getNbOfElemAllocated());
759   sz*=sizeof(double);
760   return DataArray::getHeapMemorySizeWithoutChildren()+sz;
761 }
762
763 /*!
764  * Returns the only one value in \a this, if and only if number of elements
765  * (nb of tuples * nb of components) is equal to 1, and that \a this is allocated.
766  *  \return double - the sole value stored in \a this array.
767  *  \throw If at least one of conditions stated above is not fulfilled.
768  */
769 double DataArrayDouble::doubleValue() const
770 {
771   if(isAllocated())
772     {
773       if(getNbOfElems()==1)
774         {
775           return *getConstPointer();
776         }
777       else
778         throw INTERP_KERNEL::Exception("DataArrayDouble::doubleValue : DataArrayDouble instance is allocated but number of elements is not equal to 1 !");
779     }
780   else
781     throw INTERP_KERNEL::Exception("DataArrayDouble::doubleValue : DataArrayDouble instance is not allocated !");
782 }
783
784 /*!
785  * Checks the number of tuples.
786  *  \return bool - \a true if getNumberOfTuples() == 0, \a false else.
787  *  \throw If \a this is not allocated.
788  */
789 bool DataArrayDouble::empty() const
790 {
791   checkAllocated();
792   return getNumberOfTuples()==0;
793 }
794
795 /*!
796  * Returns a full copy of \a this. For more info on copying data arrays see
797  * \ref MEDCouplingArrayBasicsCopyDeep.
798  *  \return DataArrayDouble * - a new instance of DataArrayDouble. The caller is to
799  *          delete this array using decrRef() as it is no more needed. 
800  */
801 DataArrayDouble *DataArrayDouble::deepCpy() const
802 {
803   return new DataArrayDouble(*this);
804 }
805
806 /*!
807  * Returns either a \a deep or \a shallow copy of this array. For more info see
808  * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
809  *  \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
810  *  \return DataArrayDouble * - either a new instance of DataArrayDouble (if \a dCpy
811  *          == \a true) or \a this instance (if \a dCpy == \a false).
812  */
813 DataArrayDouble *DataArrayDouble::performCpy(bool dCpy) const
814 {
815   if(dCpy)
816     return deepCpy();
817   else
818     {
819       incrRef();
820       return const_cast<DataArrayDouble *>(this);
821     }
822 }
823
824 /*!
825  * Copies all the data from another DataArrayDouble. For more info see
826  * \ref MEDCouplingArrayBasicsCopyDeepAssign.
827  *  \param [in] other - another instance of DataArrayDouble to copy data from.
828  *  \throw If the \a other is not allocated.
829  */
830 void DataArrayDouble::cpyFrom(const DataArrayDouble& other)
831 {
832   other.checkAllocated();
833   int nbOfTuples=other.getNumberOfTuples();
834   int nbOfComp=other.getNumberOfComponents();
835   allocIfNecessary(nbOfTuples,nbOfComp);
836   std::size_t nbOfElems=(std::size_t)nbOfTuples*nbOfComp;
837   double *pt=getPointer();
838   const double *ptI=other.getConstPointer();
839   for(std::size_t i=0;i<nbOfElems;i++)
840     pt[i]=ptI[i];
841   copyStringInfoFrom(other);
842 }
843
844 /*!
845  * This method reserve nbOfElems elements in memory ( nbOfElems*8 bytes ) \b without impacting the number of tuples in \a this.
846  * 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.
847  * If \a this has not already been allocated, number of components is set to one.
848  * This method allows to reduce number of reallocations on invokation of DataArrayDouble::pushBackSilent and DataArrayDouble::pushBackValsSilent on \a this.
849  * 
850  * \sa DataArrayDouble::pack, DataArrayDouble::pushBackSilent, DataArrayDouble::pushBackValsSilent
851  */
852 void DataArrayDouble::reserve(std::size_t nbOfElems)
853 {
854   int nbCompo=getNumberOfComponents();
855   if(nbCompo==1)
856     {
857       _mem.reserve(nbOfElems);
858     }
859   else if(nbCompo==0)
860     {
861       _mem.reserve(nbOfElems);
862       _info_on_compo.resize(1);
863     }
864   else
865     throw INTERP_KERNEL::Exception("DataArrayDouble::reserve : not available for DataArrayDouble with number of components different than 1 !");
866 }
867
868 /*!
869  * 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
870  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
871  *
872  * \param [in] val the value to be added in \a this
873  * \throw If \a this has already been allocated with number of components different from one.
874  * \sa DataArrayDouble::pushBackValsSilent
875  */
876 void DataArrayDouble::pushBackSilent(double val)
877 {
878   int nbCompo=getNumberOfComponents();
879   if(nbCompo==1)
880     _mem.pushBack(val);
881   else if(nbCompo==0)
882     {
883       _info_on_compo.resize(1);
884       _mem.pushBack(val);
885     }
886   else
887     throw INTERP_KERNEL::Exception("DataArrayDouble::pushBackSilent : not available for DataArrayDouble with number of components different than 1 !");
888 }
889
890 /*!
891  * 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
892  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
893  *
894  *  \param [in] valsBg - an array of values to push at the end of \this.
895  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
896  *              the last value of \a valsBg is \a valsEnd[ -1 ].
897  * \throw If \a this has already been allocated with number of components different from one.
898  * \sa DataArrayDouble::pushBackSilent
899  */
900 void DataArrayDouble::pushBackValsSilent(const double *valsBg, const double *valsEnd)
901 {
902   int nbCompo=getNumberOfComponents();
903   if(nbCompo==1)
904     _mem.insertAtTheEnd(valsBg,valsEnd);
905   else if(nbCompo==0)
906     {
907       _info_on_compo.resize(1);
908       _mem.insertAtTheEnd(valsBg,valsEnd);
909     }
910   else
911     throw INTERP_KERNEL::Exception("DataArrayDouble::pushBackValsSilent : not available for DataArrayDouble with number of components different than 1 !");
912 }
913
914 /*!
915  * This method returns silently ( without updating time label in \a this ) the last value, if any and suppress it.
916  * \throw If \a this is already empty.
917  * \throw If \a this has number of components different from one.
918  */
919 double DataArrayDouble::popBackSilent()
920 {
921   if(getNumberOfComponents()==1)
922     return _mem.popBack();
923   else
924     throw INTERP_KERNEL::Exception("DataArrayDouble::popBackSilent : not available for DataArrayDouble with number of components different than 1 !");
925 }
926
927 /*!
928  * 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.
929  *
930  * \sa DataArrayDouble::getHeapMemorySizeWithoutChildren, DataArrayDouble::reserve
931  */
932 void DataArrayDouble::pack() const
933 {
934   _mem.pack();
935 }
936
937 /*!
938  * Allocates the raw data in memory. If exactly same memory as needed already
939  * allocated, it is not re-allocated.
940  *  \param [in] nbOfTuple - number of tuples of data to allocate.
941  *  \param [in] nbOfCompo - number of components of data to allocate.
942  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
943  */
944 void DataArrayDouble::allocIfNecessary(int nbOfTuple, int nbOfCompo)
945 {
946   if(isAllocated())
947     {
948       if(nbOfTuple!=getNumberOfTuples() || nbOfCompo!=getNumberOfComponents())
949         alloc(nbOfTuple,nbOfCompo);
950     }
951   else
952     alloc(nbOfTuple,nbOfCompo);
953 }
954
955 /*!
956  * Allocates the raw data in memory. If the memory was already allocated, then it is
957  * freed and re-allocated. See an example of this method use
958  * \ref MEDCouplingArraySteps1WC "here".
959  *  \param [in] nbOfTuple - number of tuples of data to allocate.
960  *  \param [in] nbOfCompo - number of components of data to allocate.
961  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
962  */
963 void DataArrayDouble::alloc(int nbOfTuple, int nbOfCompo)
964 {
965   if(nbOfTuple<0 || nbOfCompo<0)
966     throw INTERP_KERNEL::Exception("DataArrayDouble::alloc : request for negative length of data !");
967   _info_on_compo.resize(nbOfCompo);
968   _mem.alloc(nbOfCompo*(std::size_t)nbOfTuple);
969   declareAsNew();
970 }
971
972 /*!
973  * Assign zero to all values in \a this array. To know more on filling arrays see
974  * \ref MEDCouplingArrayFill.
975  * \throw If \a this is not allocated.
976  */
977 void DataArrayDouble::fillWithZero()
978 {
979   checkAllocated();
980   _mem.fillWithValue(0.);
981   declareAsNew();
982 }
983
984 /*!
985  * Assign \a val to all values in \a this array. To know more on filling arrays see
986  * \ref MEDCouplingArrayFill.
987  *  \param [in] val - the value to fill with.
988  *  \throw If \a this is not allocated.
989  */
990 void DataArrayDouble::fillWithValue(double val)
991 {
992   checkAllocated();
993   _mem.fillWithValue(val);
994   declareAsNew();
995 }
996
997 /*!
998  * Set all values in \a this array so that the i-th element equals to \a init + i
999  * (i starts from zero). To know more on filling arrays see \ref MEDCouplingArrayFill.
1000  *  \param [in] init - value to assign to the first element of array.
1001  *  \throw If \a this->getNumberOfComponents() != 1
1002  *  \throw If \a this is not allocated.
1003  */
1004 void DataArrayDouble::iota(double init)
1005 {
1006   checkAllocated();
1007   if(getNumberOfComponents()!=1)
1008     throw INTERP_KERNEL::Exception("DataArrayDouble::iota : works only for arrays with only one component, you can call 'rearrange' method before !");
1009   double *ptr=getPointer();
1010   int ntuples=getNumberOfTuples();
1011   for(int i=0;i<ntuples;i++)
1012     ptr[i]=init+double(i);
1013   declareAsNew();
1014 }
1015
1016 /*!
1017  * Checks if all values in \a this array are equal to \a val at precision \a eps.
1018  *  \param [in] val - value to check equality of array values to.
1019  *  \param [in] eps - precision to check the equality.
1020  *  \return bool - \a true if all values are in range (_val_ - _eps_; _val_ + _eps_),
1021  *                 \a false else.
1022  *  \throw If \a this->getNumberOfComponents() != 1
1023  *  \throw If \a this is not allocated.
1024  */
1025 bool DataArrayDouble::isUniform(double val, double eps) const
1026 {
1027   checkAllocated();
1028   if(getNumberOfComponents()!=1)
1029     throw INTERP_KERNEL::Exception("DataArrayDouble::isUniform : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before !");
1030   int nbOfTuples=getNumberOfTuples();
1031   const double *w=getConstPointer();
1032   const double *end2=w+nbOfTuples;
1033   const double vmin=val-eps;
1034   const double vmax=val+eps;
1035   for(;w!=end2;w++)
1036     if(*w<vmin || *w>vmax)
1037       return false;
1038   return true;
1039 }
1040
1041 /*!
1042  * Sorts values of the array.
1043  *  \param [in] asc - \a true means ascending order, \a false, descending.
1044  *  \throw If \a this is not allocated.
1045  *  \throw If \a this->getNumberOfComponents() != 1.
1046  */
1047 void DataArrayDouble::sort(bool asc)
1048 {
1049   checkAllocated();
1050   if(getNumberOfComponents()!=1)
1051     throw INTERP_KERNEL::Exception("DataArrayDouble::sort : only supported with 'this' array with ONE component !");
1052   _mem.sort(asc);
1053   declareAsNew();
1054 }
1055
1056 /*!
1057  * Reverse the array values.
1058  *  \throw If \a this->getNumberOfComponents() < 1.
1059  *  \throw If \a this is not allocated.
1060  */
1061 void DataArrayDouble::reverse()
1062 {
1063   checkAllocated();
1064   _mem.reverse(getNumberOfComponents());
1065   declareAsNew();
1066 }
1067
1068 /*!
1069  * Checks that \a this array is consistently **increasing** or **decreasing** in value,
1070  * with at least absolute difference value of |\a eps| at each step.
1071  * If not an exception is thrown.
1072  *  \param [in] increasing - if \a true, the array values should be increasing.
1073  *  \param [in] eps - minimal absolute difference between the neighbor values at which 
1074  *                    the values are considered different.
1075  *  \throw If sequence of values is not strictly monotonic in agreement with \a
1076  *         increasing arg.
1077  *  \throw If \a this->getNumberOfComponents() != 1.
1078  *  \throw If \a this is not allocated.
1079  */
1080 void DataArrayDouble::checkMonotonic(bool increasing, double eps) const
1081 {
1082   if(!isMonotonic(increasing,eps))
1083     {
1084       if (increasing)
1085         throw INTERP_KERNEL::Exception("DataArrayDouble::checkMonotonic : 'this' is not INCREASING monotonic !");
1086       else
1087         throw INTERP_KERNEL::Exception("DataArrayDouble::checkMonotonic : 'this' is not DECREASING monotonic !");
1088     }
1089 }
1090
1091 /*!
1092  * Checks that \a this array is consistently **increasing** or **decreasing** in value,
1093  * with at least absolute difference value of |\a eps| at each step.
1094  *  \param [in] increasing - if \a true, array values should be increasing.
1095  *  \param [in] eps - minimal absolute difference between the neighbor values at which 
1096  *                    the values are considered different.
1097  *  \return bool - \a true if values change in accordance with \a increasing arg.
1098  *  \throw If \a this->getNumberOfComponents() != 1.
1099  *  \throw If \a this is not allocated.
1100  */
1101 bool DataArrayDouble::isMonotonic(bool increasing, double eps) const
1102 {
1103   checkAllocated();
1104   if(getNumberOfComponents()!=1)
1105     throw INTERP_KERNEL::Exception("DataArrayDouble::isMonotonic : only supported with 'this' array with ONE component !");
1106   int nbOfElements=getNumberOfTuples();
1107   const double *ptr=getConstPointer();
1108   if(nbOfElements==0)
1109     return true;
1110   double ref=ptr[0];
1111   double absEps=fabs(eps);
1112   if(increasing)
1113     {
1114       for(int i=1;i<nbOfElements;i++)
1115         {
1116           if(ptr[i]<(ref+absEps))
1117             return false;
1118           ref=ptr[i];
1119         }
1120       return true;
1121     }
1122   else
1123     {
1124       for(int i=1;i<nbOfElements;i++)
1125         {
1126           if(ptr[i]>(ref-absEps))
1127             return false;
1128           ref=ptr[i];
1129         }
1130       return true;
1131     }
1132 }
1133
1134 /*!
1135  * Returns a textual and human readable representation of \a this instance of
1136  * DataArrayDouble. This text is shown when a DataArrayDouble is printed in Python.
1137  *  \return std::string - text describing \a this DataArrayDouble.
1138  */
1139 std::string DataArrayDouble::repr() const
1140 {
1141   std::ostringstream ret;
1142   reprStream(ret);
1143   return ret.str();
1144 }
1145
1146 std::string DataArrayDouble::reprZip() const
1147 {
1148   std::ostringstream ret;
1149   reprZipStream(ret);
1150   return ret.str();
1151 }
1152
1153 void DataArrayDouble::writeVTK(std::ostream& ofs, int indent, const std::string& nameInFile, DataArrayByte *byteArr) const
1154 {
1155   static const char SPACE[4]={' ',' ',' ',' '};
1156   checkAllocated();
1157   std::string idt(indent,' ');
1158   ofs.precision(17);
1159   ofs << idt << "<DataArray type=\"Float32\" Name=\"" << nameInFile << "\" NumberOfComponents=\"" << getNumberOfComponents() << "\"";
1160   if(byteArr)
1161     {
1162       ofs << " format=\"appended\" offset=\"" << byteArr->getNumberOfTuples() << "\">";
1163       INTERP_KERNEL::AutoPtr<float> tmp(new float[getNbOfElems()]);
1164       float *pt(tmp);
1165       // to make Visual C++ happy : instead of std::copy(begin(),end(),(float *)tmp);
1166       for(const double *src=begin();src!=end();src++,pt++)
1167         *pt=float(*src);
1168       const char *data(reinterpret_cast<const char *>((float *)tmp));
1169       std::size_t sz(getNbOfElems()*sizeof(float));
1170       byteArr->insertAtTheEnd(data,data+sz);
1171       byteArr->insertAtTheEnd(SPACE,SPACE+4);
1172     }
1173   else
1174     {
1175       ofs << " RangeMin=\"" << getMinValueInArray() << "\" RangeMax=\"" << getMaxValueInArray() << "\" format=\"ascii\">\n" << idt;
1176       std::copy(begin(),end(),std::ostream_iterator<double>(ofs," "));
1177     }
1178   ofs << std::endl << idt << "</DataArray>\n";
1179 }
1180
1181 void DataArrayDouble::reprStream(std::ostream& stream) const
1182 {
1183   stream << "Name of double array : \"" << _name << "\"\n";
1184   reprWithoutNameStream(stream);
1185 }
1186
1187 void DataArrayDouble::reprZipStream(std::ostream& stream) const
1188 {
1189   stream << "Name of double array : \"" << _name << "\"\n";
1190   reprZipWithoutNameStream(stream);
1191 }
1192
1193 void DataArrayDouble::reprWithoutNameStream(std::ostream& stream) const
1194 {
1195   DataArray::reprWithoutNameStream(stream);
1196   stream.precision(17);
1197   _mem.repr(getNumberOfComponents(),stream);
1198 }
1199
1200 void DataArrayDouble::reprZipWithoutNameStream(std::ostream& stream) const
1201 {
1202   DataArray::reprWithoutNameStream(stream);
1203   stream.precision(17);
1204   _mem.reprZip(getNumberOfComponents(),stream);
1205 }
1206
1207 void DataArrayDouble::reprCppStream(const std::string& varName, std::ostream& stream) const
1208 {
1209   int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents();
1210   const double *data=getConstPointer();
1211   stream.precision(17);
1212   stream << "DataArrayDouble *" << varName << "=DataArrayDouble::New();" << std::endl;
1213   if(nbTuples*nbComp>=1)
1214     {
1215       stream << "const double " << varName << "Data[" << nbTuples*nbComp << "]={";
1216       std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<double>(stream,","));
1217       stream << data[nbTuples*nbComp-1] << "};" << std::endl;
1218       stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
1219     }
1220   else
1221     stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
1222   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
1223 }
1224
1225 /*!
1226  * Method that gives a quick overvien of \a this for python.
1227  */
1228 void DataArrayDouble::reprQuickOverview(std::ostream& stream) const
1229 {
1230   static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
1231   stream << "DataArrayDouble C++ instance at " << this << ". ";
1232   if(isAllocated())
1233     {
1234       int nbOfCompo=(int)_info_on_compo.size();
1235       if(nbOfCompo>=1)
1236         {
1237           int nbOfTuples=getNumberOfTuples();
1238           stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
1239           reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
1240         }
1241       else
1242         stream << "Number of components : 0.";
1243     }
1244   else
1245     stream << "*** No data allocated ****";
1246 }
1247
1248 void DataArrayDouble::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
1249 {
1250   const double *data=begin();
1251   int nbOfTuples=getNumberOfTuples();
1252   int nbOfCompo=(int)_info_on_compo.size();
1253   std::ostringstream oss2; oss2 << "[";
1254   oss2.precision(17);
1255   std::string oss2Str(oss2.str());
1256   bool isFinished=true;
1257   for(int i=0;i<nbOfTuples && isFinished;i++)
1258     {
1259       if(nbOfCompo>1)
1260         {
1261           oss2 << "(";
1262           for(int j=0;j<nbOfCompo;j++,data++)
1263             {
1264               oss2 << *data;
1265               if(j!=nbOfCompo-1) oss2 << ", ";
1266             }
1267           oss2 << ")";
1268         }
1269       else
1270         oss2 << *data++;
1271       if(i!=nbOfTuples-1) oss2 << ", ";
1272       std::string oss3Str(oss2.str());
1273       if(oss3Str.length()<maxNbOfByteInRepr)
1274         oss2Str=oss3Str;
1275       else
1276         isFinished=false;
1277     }
1278   stream << oss2Str;
1279   if(!isFinished)
1280     stream << "... ";
1281   stream << "]";
1282 }
1283
1284 /*!
1285  * Equivalent to DataArrayDouble::isEqual except that if false the reason of
1286  * mismatch is given.
1287  * 
1288  * \param [in] other the instance to be compared with \a this
1289  * \param [in] prec the precision to compare numeric data of the arrays.
1290  * \param [out] reason In case of inequality returns the reason.
1291  * \sa DataArrayDouble::isEqual
1292  */
1293 bool DataArrayDouble::isEqualIfNotWhy(const DataArrayDouble& other, double prec, std::string& reason) const
1294 {
1295   if(!areInfoEqualsIfNotWhy(other,reason))
1296     return false;
1297   return _mem.isEqual(other._mem,prec,reason);
1298 }
1299
1300 /*!
1301  * Checks if \a this and another DataArrayDouble are fully equal. For more info see
1302  * \ref MEDCouplingArrayBasicsCompare.
1303  *  \param [in] other - an instance of DataArrayDouble to compare with \a this one.
1304  *  \param [in] prec - precision value to compare numeric data of the arrays.
1305  *  \return bool - \a true if the two arrays are equal, \a false else.
1306  */
1307 bool DataArrayDouble::isEqual(const DataArrayDouble& other, double prec) const
1308 {
1309   std::string tmp;
1310   return isEqualIfNotWhy(other,prec,tmp);
1311 }
1312
1313 /*!
1314  * Checks if values of \a this and another DataArrayDouble are equal. For more info see
1315  * \ref MEDCouplingArrayBasicsCompare.
1316  *  \param [in] other - an instance of DataArrayDouble to compare with \a this one.
1317  *  \param [in] prec - precision value to compare numeric data of the arrays.
1318  *  \return bool - \a true if the values of two arrays are equal, \a false else.
1319  */
1320 bool DataArrayDouble::isEqualWithoutConsideringStr(const DataArrayDouble& other, double prec) const
1321 {
1322   std::string tmp;
1323   return _mem.isEqual(other._mem,prec,tmp);
1324 }
1325
1326 /*!
1327  * Changes number of tuples in the array. If the new number of tuples is smaller
1328  * than the current number the array is truncated, otherwise the array is extended.
1329  *  \param [in] nbOfTuples - new number of tuples. 
1330  *  \throw If \a this is not allocated.
1331  *  \throw If \a nbOfTuples is negative.
1332  */
1333 void DataArrayDouble::reAlloc(int nbOfTuples)
1334 {
1335   if(nbOfTuples<0)
1336     throw INTERP_KERNEL::Exception("DataArrayDouble::reAlloc : input new number of tuples should be >=0 !");
1337   checkAllocated();
1338   _mem.reAlloc(getNumberOfComponents()*(std::size_t)nbOfTuples);
1339   declareAsNew();
1340 }
1341
1342 /*!
1343  * Creates a new DataArrayInt and assigns all (textual and numerical) data of \a this
1344  * array to the new one.
1345  *  \return DataArrayInt * - the new instance of DataArrayInt.
1346  */
1347 DataArrayInt *DataArrayDouble::convertToIntArr() const
1348 {
1349   DataArrayInt *ret=DataArrayInt::New();
1350   ret->alloc(getNumberOfTuples(),getNumberOfComponents());
1351   int *dest=ret->getPointer();
1352   // to make Visual C++ happy : instead of std::size_t nbOfVals=getNbOfElems(); std::copy(src,src+nbOfVals,dest);
1353   for(const double *src=begin();src!=end();src++,dest++)
1354     *dest=(int)*src;
1355   ret->copyStringInfoFrom(*this);
1356   return ret;
1357 }
1358
1359 /*!
1360  * Returns a new DataArrayDouble holding the same values as \a this array but differently
1361  * arranged in memory. If \a this array holds 2 components of 3 values:
1362  * \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$, then the result array holds these values arranged
1363  * as follows: \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$.
1364  *  \warning Do not confuse this method with transpose()!
1365  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1366  *          is to delete using decrRef() as it is no more needed.
1367  *  \throw If \a this is not allocated.
1368  */
1369 DataArrayDouble *DataArrayDouble::fromNoInterlace() const
1370 {
1371   if(_mem.isNull())
1372     throw INTERP_KERNEL::Exception("DataArrayDouble::fromNoInterlace : Not defined array !");
1373   double *tab=_mem.fromNoInterlace(getNumberOfComponents());
1374   DataArrayDouble *ret=DataArrayDouble::New();
1375   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
1376   return ret;
1377 }
1378
1379 /*!
1380  * Returns a new DataArrayDouble holding the same values as \a this array but differently
1381  * arranged in memory. If \a this array holds 2 components of 3 values:
1382  * \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$, then the result array holds these values arranged
1383  * as follows: \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$.
1384  *  \warning Do not confuse this method with transpose()!
1385  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1386  *          is to delete using decrRef() as it is no more needed.
1387  *  \throw If \a this is not allocated.
1388  */
1389 DataArrayDouble *DataArrayDouble::toNoInterlace() const
1390 {
1391   if(_mem.isNull())
1392     throw INTERP_KERNEL::Exception("DataArrayDouble::toNoInterlace : Not defined array !");
1393   double *tab=_mem.toNoInterlace(getNumberOfComponents());
1394   DataArrayDouble *ret=DataArrayDouble::New();
1395   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
1396   return ret;
1397 }
1398
1399 /*!
1400  * Permutes values of \a this array as required by \a old2New array. The values are
1401  * permuted so that \c new[ \a old2New[ i ]] = \c old[ i ]. Number of tuples remains
1402  * the same as in \this one.
1403  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
1404  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1405  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1406  *     giving a new position for i-th old value.
1407  */
1408 void DataArrayDouble::renumberInPlace(const int *old2New)
1409 {
1410   checkAllocated();
1411   int nbTuples=getNumberOfTuples();
1412   int nbOfCompo=getNumberOfComponents();
1413   double *tmp=new double[nbTuples*nbOfCompo];
1414   const double *iptr=getConstPointer();
1415   for(int i=0;i<nbTuples;i++)
1416     {
1417       int v=old2New[i];
1418       if(v>=0 && v<nbTuples)
1419         std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),tmp+nbOfCompo*v);
1420       else
1421         {
1422           std::ostringstream oss; oss << "DataArrayDouble::renumberInPlace : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
1423           throw INTERP_KERNEL::Exception(oss.str().c_str());
1424         }
1425     }
1426   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
1427   delete [] tmp;
1428   declareAsNew();
1429 }
1430
1431 /*!
1432  * Permutes values of \a this array as required by \a new2Old array. The values are
1433  * permuted so that \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of tuples remains
1434  * the same as in \this one.
1435  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1436  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
1437  *     giving a previous position of i-th new value.
1438  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1439  *          is to delete using decrRef() as it is no more needed.
1440  */
1441 void DataArrayDouble::renumberInPlaceR(const int *new2Old)
1442 {
1443   checkAllocated();
1444   int nbTuples=getNumberOfTuples();
1445   int nbOfCompo=getNumberOfComponents();
1446   double *tmp=new double[nbTuples*nbOfCompo];
1447   const double *iptr=getConstPointer();
1448   for(int i=0;i<nbTuples;i++)
1449     {
1450       int v=new2Old[i];
1451       if(v>=0 && v<nbTuples)
1452         std::copy(iptr+nbOfCompo*v,iptr+nbOfCompo*(v+1),tmp+nbOfCompo*i);
1453       else
1454         {
1455           std::ostringstream oss; oss << "DataArrayDouble::renumberInPlaceR : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
1456           throw INTERP_KERNEL::Exception(oss.str().c_str());
1457         }
1458     }
1459   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
1460   delete [] tmp;
1461   declareAsNew();
1462 }
1463
1464 /*!
1465  * Returns a copy of \a this array with values permuted as required by \a old2New array.
1466  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ].
1467  * Number of tuples in the result array remains the same as in \this one.
1468  * If a permutation reduction is needed, renumberAndReduce() should be used.
1469  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1470  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1471  *          giving a new position for i-th old value.
1472  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1473  *          is to delete using decrRef() as it is no more needed.
1474  *  \throw If \a this is not allocated.
1475  */
1476 DataArrayDouble *DataArrayDouble::renumber(const int *old2New) const
1477 {
1478   checkAllocated();
1479   int nbTuples=getNumberOfTuples();
1480   int nbOfCompo=getNumberOfComponents();
1481   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1482   ret->alloc(nbTuples,nbOfCompo);
1483   ret->copyStringInfoFrom(*this);
1484   const double *iptr=getConstPointer();
1485   double *optr=ret->getPointer();
1486   for(int i=0;i<nbTuples;i++)
1487     std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),optr+nbOfCompo*old2New[i]);
1488   ret->copyStringInfoFrom(*this);
1489   return ret.retn();
1490 }
1491
1492 /*!
1493  * Returns a copy of \a this array with values permuted as required by \a new2Old array.
1494  * The values are permuted so that  \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of
1495  * tuples in the result array remains the same as in \this one.
1496  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
1497  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1498  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
1499  *     giving a previous position of i-th new value.
1500  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1501  *          is to delete using decrRef() as it is no more needed.
1502  */
1503 DataArrayDouble *DataArrayDouble::renumberR(const int *new2Old) const
1504 {
1505   checkAllocated();
1506   int nbTuples=getNumberOfTuples();
1507   int nbOfCompo=getNumberOfComponents();
1508   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1509   ret->alloc(nbTuples,nbOfCompo);
1510   ret->copyStringInfoFrom(*this);
1511   const double *iptr=getConstPointer();
1512   double *optr=ret->getPointer();
1513   for(int i=0;i<nbTuples;i++)
1514     std::copy(iptr+nbOfCompo*new2Old[i],iptr+nbOfCompo*(new2Old[i]+1),optr+i*nbOfCompo);
1515   ret->copyStringInfoFrom(*this);
1516   return ret.retn();
1517 }
1518
1519 /*!
1520  * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1521  * of size \a newNbOfTuple and it's values are permuted as required by \a old2New array.
1522  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ] for all
1523  * \a old2New[ i ] >= 0. In other words every i-th tuple in \a this array, for which 
1524  * \a old2New[ i ] is negative, is missing from the result array.
1525  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1526  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1527  *     giving a new position for i-th old tuple and giving negative position for
1528  *     for i-th old tuple that should be omitted.
1529  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1530  *          is to delete using decrRef() as it is no more needed.
1531  */
1532 DataArrayDouble *DataArrayDouble::renumberAndReduce(const int *old2New, int newNbOfTuple) const
1533 {
1534   checkAllocated();
1535   int nbTuples=getNumberOfTuples();
1536   int nbOfCompo=getNumberOfComponents();
1537   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1538   ret->alloc(newNbOfTuple,nbOfCompo);
1539   const double *iptr=getConstPointer();
1540   double *optr=ret->getPointer();
1541   for(int i=0;i<nbTuples;i++)
1542     {
1543       int w=old2New[i];
1544       if(w>=0)
1545         std::copy(iptr+i*nbOfCompo,iptr+(i+1)*nbOfCompo,optr+w*nbOfCompo);
1546     }
1547   ret->copyStringInfoFrom(*this);
1548   return ret.retn();
1549 }
1550
1551 /*!
1552  * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1553  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
1554  * \a new2OldBg array.
1555  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
1556  * This method is equivalent to renumberAndReduce() except that convention in input is
1557  * \c new2old and \b not \c old2new.
1558  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1559  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
1560  *              tuple index in \a this array to fill the i-th tuple in the new array.
1561  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
1562  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
1563  *              \a new2OldBg <= \a pi < \a new2OldEnd.
1564  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1565  *          is to delete using decrRef() as it is no more needed.
1566  */
1567 DataArrayDouble *DataArrayDouble::selectByTupleId(const int *new2OldBg, const int *new2OldEnd) const
1568 {
1569   checkAllocated();
1570   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1571   int nbComp=getNumberOfComponents();
1572   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
1573   ret->copyStringInfoFrom(*this);
1574   double *pt=ret->getPointer();
1575   const double *srcPt=getConstPointer();
1576   int i=0;
1577   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
1578     std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
1579   ret->copyStringInfoFrom(*this);
1580   return ret.retn();
1581 }
1582
1583 /*!
1584  * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1585  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
1586  * \a new2OldBg array.
1587  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
1588  * This method is equivalent to renumberAndReduce() except that convention in input is
1589  * \c new2old and \b not \c old2new.
1590  * This method is equivalent to selectByTupleId() except that it prevents coping data
1591  * from behind the end of \a this array.
1592  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1593  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
1594  *              tuple index in \a this array to fill the i-th tuple in the new array.
1595  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
1596  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
1597  *              \a new2OldBg <= \a pi < \a new2OldEnd.
1598  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1599  *          is to delete using decrRef() as it is no more needed.
1600  *  \throw If \a new2OldEnd - \a new2OldBg > \a this->getNumberOfTuples().
1601  */
1602 DataArrayDouble *DataArrayDouble::selectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const
1603 {
1604   checkAllocated();
1605   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1606   int nbComp=getNumberOfComponents();
1607   int oldNbOfTuples=getNumberOfTuples();
1608   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
1609   ret->copyStringInfoFrom(*this);
1610   double *pt=ret->getPointer();
1611   const double *srcPt=getConstPointer();
1612   int i=0;
1613   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
1614     if(*w>=0 && *w<oldNbOfTuples)
1615       std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
1616     else
1617       throw INTERP_KERNEL::Exception("DataArrayDouble::selectByTupleIdSafe : some ids has been detected to be out of [0,this->getNumberOfTuples) !");
1618   ret->copyStringInfoFrom(*this);
1619   return ret.retn();
1620 }
1621
1622 /*!
1623  * Returns a shorten copy of \a this array. The new DataArrayDouble contains every
1624  * (\a bg + \c i * \a step)-th tuple of \a this array located before the \a end2-th
1625  * tuple. Indices of the selected tuples are the same as ones returned by the Python
1626  * command \c range( \a bg, \a end2, \a step ).
1627  * This method is equivalent to selectByTupleIdSafe() except that the input array is
1628  * not constructed explicitly.
1629  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1630  *  \param [in] bg - index of the first tuple to copy from \a this array.
1631  *  \param [in] end2 - index of the tuple before which the tuples to copy are located.
1632  *  \param [in] step - index increment to get index of the next tuple to copy.
1633  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1634  *          is to delete using decrRef() as it is no more needed.
1635  *  \sa DataArrayDouble::substr.
1636  */
1637 DataArrayDouble *DataArrayDouble::selectByTupleId2(int bg, int end2, int step) const
1638 {
1639   checkAllocated();
1640   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1641   int nbComp=getNumberOfComponents();
1642   int newNbOfTuples=GetNumberOfItemGivenBESRelative(bg,end2,step,"DataArrayDouble::selectByTupleId2 : ");
1643   ret->alloc(newNbOfTuples,nbComp);
1644   double *pt=ret->getPointer();
1645   const double *srcPt=getConstPointer()+bg*nbComp;
1646   for(int i=0;i<newNbOfTuples;i++,srcPt+=step*nbComp)
1647     std::copy(srcPt,srcPt+nbComp,pt+i*nbComp);
1648   ret->copyStringInfoFrom(*this);
1649   return ret.retn();
1650 }
1651
1652 /*!
1653  * Returns a shorten copy of \a this array. The new DataArrayDouble contains ranges
1654  * of tuples specified by \a ranges parameter.
1655  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
1656  *  \param [in] ranges - std::vector of std::pair's each of which defines a range
1657  *              of tuples in [\c begin,\c end) format.
1658  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1659  *          is to delete using decrRef() as it is no more needed.
1660  *  \throw If \a end < \a begin.
1661  *  \throw If \a end > \a this->getNumberOfTuples().
1662  *  \throw If \a this is not allocated.
1663  */
1664 DataArray *DataArrayDouble::selectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const
1665 {
1666   checkAllocated();
1667   int nbOfComp=getNumberOfComponents();
1668   int nbOfTuplesThis=getNumberOfTuples();
1669   if(ranges.empty())
1670     {
1671       DataArrayDouble *ret=DataArrayDouble::New();
1672       ret->alloc(0,nbOfComp);
1673       ret->copyStringInfoFrom(*this);
1674       return ret;
1675     }
1676   int ref=ranges.front().first;
1677   int nbOfTuples=0;
1678   bool isIncreasing=true;
1679   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
1680     {
1681       if((*it).first<=(*it).second)
1682         {
1683           if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
1684             {
1685               nbOfTuples+=(*it).second-(*it).first;
1686               if(isIncreasing)
1687                 isIncreasing=ref<=(*it).first;
1688               ref=(*it).second;
1689             }
1690           else
1691             {
1692               std::ostringstream oss; oss << "DataArrayDouble::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
1693               oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !";
1694               throw INTERP_KERNEL::Exception(oss.str().c_str());
1695             }
1696         }
1697       else
1698         {
1699           std::ostringstream oss; oss << "DataArrayDouble::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
1700           oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !";
1701           throw INTERP_KERNEL::Exception(oss.str().c_str());
1702         }
1703     }
1704   if(isIncreasing && nbOfTuplesThis==nbOfTuples)
1705     return deepCpy();
1706   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1707   ret->alloc(nbOfTuples,nbOfComp);
1708   ret->copyStringInfoFrom(*this);
1709   const double *src=getConstPointer();
1710   double *work=ret->getPointer();
1711   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
1712     work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work);
1713   return ret.retn();
1714 }
1715
1716 /*!
1717  * Returns a shorten copy of \a this array. The new DataArrayDouble contains all
1718  * tuples starting from the \a tupleIdBg-th tuple and including all tuples located before
1719  * the \a tupleIdEnd-th one. This methods has a similar behavior as std::string::substr().
1720  * This method is a specialization of selectByTupleId2().
1721  *  \param [in] tupleIdBg - index of the first tuple to copy from \a this array.
1722  *  \param [in] tupleIdEnd - index of the tuple before which the tuples to copy are located.
1723  *          If \a tupleIdEnd == -1, all the tuples till the end of \a this array are copied.
1724  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1725  *          is to delete using decrRef() as it is no more needed.
1726  *  \throw If \a tupleIdBg < 0.
1727  *  \throw If \a tupleIdBg > \a this->getNumberOfTuples().
1728     \throw If \a tupleIdEnd != -1 && \a tupleIdEnd < \a this->getNumberOfTuples().
1729  *  \sa DataArrayDouble::selectByTupleId2
1730  */
1731 DataArrayDouble *DataArrayDouble::substr(int tupleIdBg, int tupleIdEnd) const
1732 {
1733   checkAllocated();
1734   int nbt=getNumberOfTuples();
1735   if(tupleIdBg<0)
1736     throw INTERP_KERNEL::Exception("DataArrayDouble::substr : The tupleIdBg parameter must be greater than 0 !");
1737   if(tupleIdBg>nbt)
1738     throw INTERP_KERNEL::Exception("DataArrayDouble::substr : The tupleIdBg parameter is greater than number of tuples !");
1739   int trueEnd=tupleIdEnd;
1740   if(tupleIdEnd!=-1)
1741     {
1742       if(tupleIdEnd>nbt)
1743         throw INTERP_KERNEL::Exception("DataArrayDouble::substr : The tupleIdBg parameter is greater or equal than number of tuples !");
1744     }
1745   else
1746     trueEnd=nbt;
1747   int nbComp=getNumberOfComponents();
1748   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1749   ret->alloc(trueEnd-tupleIdBg,nbComp);
1750   ret->copyStringInfoFrom(*this);
1751   std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer());
1752   return ret.retn();
1753 }
1754
1755 /*!
1756  * Returns a shorten or extended copy of \a this array. If \a newNbOfComp is less
1757  * than \a this->getNumberOfComponents() then the result array is shorten as each tuple
1758  * is truncated to have \a newNbOfComp components, keeping first components. If \a
1759  * newNbOfComp is more than \a this->getNumberOfComponents() then the result array is
1760  * expanded as each tuple is populated with \a dftValue to have \a newNbOfComp
1761  * components.  
1762  *  \param [in] newNbOfComp - number of components for the new array to have.
1763  *  \param [in] dftValue - value assigned to new values added to the new array.
1764  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1765  *          is to delete using decrRef() as it is no more needed.
1766  *  \throw If \a this is not allocated.
1767  */
1768 DataArrayDouble *DataArrayDouble::changeNbOfComponents(int newNbOfComp, double dftValue) const
1769 {
1770   checkAllocated();
1771   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1772   ret->alloc(getNumberOfTuples(),newNbOfComp);
1773   const double *oldc=getConstPointer();
1774   double *nc=ret->getPointer();
1775   int nbOfTuples=getNumberOfTuples();
1776   int oldNbOfComp=getNumberOfComponents();
1777   int dim=std::min(oldNbOfComp,newNbOfComp);
1778   for(int i=0;i<nbOfTuples;i++)
1779     {
1780       int j=0;
1781       for(;j<dim;j++)
1782         nc[newNbOfComp*i+j]=oldc[i*oldNbOfComp+j];
1783       for(;j<newNbOfComp;j++)
1784         nc[newNbOfComp*i+j]=dftValue;
1785     }
1786   ret->setName(getName());
1787   for(int i=0;i<dim;i++)
1788     ret->setInfoOnComponent(i,getInfoOnComponent(i));
1789   ret->setName(getName());
1790   return ret.retn();
1791 }
1792
1793 /*!
1794  * Changes the number of components within \a this array so that its raw data **does
1795  * not** change, instead splitting this data into tuples changes.
1796  *  \warning This method erases all (name and unit) component info set before!
1797  *  \param [in] newNbOfComp - number of components for \a this array to have.
1798  *  \throw If \a this is not allocated
1799  *  \throw If getNbOfElems() % \a newNbOfCompo != 0.
1800  *  \throw If \a newNbOfCompo is lower than 1.
1801  *  \throw If the rearrange method would lead to a number of tuples higher than 2147483647 (maximal capacity of int32 !).
1802  *  \warning This method erases all (name and unit) component info set before!
1803  */
1804 void DataArrayDouble::rearrange(int newNbOfCompo)
1805 {
1806   checkAllocated();
1807   if(newNbOfCompo<1)
1808     throw INTERP_KERNEL::Exception("DataArrayDouble::rearrange : input newNbOfCompo must be > 0 !");
1809   std::size_t nbOfElems=getNbOfElems();
1810   if(nbOfElems%newNbOfCompo!=0)
1811     throw INTERP_KERNEL::Exception("DataArrayDouble::rearrange : nbOfElems%newNbOfCompo!=0 !");
1812   if(nbOfElems/newNbOfCompo>(std::size_t)std::numeric_limits<int>::max())
1813     throw INTERP_KERNEL::Exception("DataArrayDouble::rearrange : the rearrangement leads to too high number of tuples (> 2147483647) !");
1814   _info_on_compo.clear();
1815   _info_on_compo.resize(newNbOfCompo);
1816   declareAsNew();
1817 }
1818
1819 /*!
1820  * Changes the number of components within \a this array to be equal to its number
1821  * of tuples, and inversely its number of tuples to become equal to its number of 
1822  * components. So that its raw data **does not** change, instead splitting this
1823  * data into tuples changes.
1824  *  \warning This method erases all (name and unit) component info set before!
1825  *  \warning Do not confuse this method with fromNoInterlace() and toNoInterlace()!
1826  *  \throw If \a this is not allocated.
1827  *  \sa rearrange()
1828  */
1829 void DataArrayDouble::transpose()
1830 {
1831   checkAllocated();
1832   int nbOfTuples=getNumberOfTuples();
1833   rearrange(nbOfTuples);
1834 }
1835
1836 /*!
1837  * Returns a copy of \a this array composed of selected components.
1838  * The new DataArrayDouble has the same number of tuples but includes components
1839  * specified by \a compoIds parameter. So that getNbOfElems() of the result array
1840  * can be either less, same or more than \a this->getNbOfElems().
1841  *  \param [in] compoIds - sequence of zero based indices of components to include
1842  *              into the new array.
1843  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1844  *          is to delete using decrRef() as it is no more needed.
1845  *  \throw If \a this is not allocated.
1846  *  \throw If a component index (\a i) is not valid: 
1847  *         \a i < 0 || \a i >= \a this->getNumberOfComponents().
1848  *
1849  *  \if ENABLE_EXAMPLES
1850  *  \ref py_mcdataarraydouble_KeepSelectedComponents "Here is a Python example".
1851  *  \endif
1852  */
1853 DataArray *DataArrayDouble::keepSelectedComponents(const std::vector<int>& compoIds) const
1854 {
1855   checkAllocated();
1856   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
1857   std::size_t newNbOfCompo=compoIds.size();
1858   int oldNbOfCompo=getNumberOfComponents();
1859   for(std::vector<int>::const_iterator it=compoIds.begin();it!=compoIds.end();it++)
1860     if((*it)<0 || (*it)>=oldNbOfCompo)
1861       {
1862         std::ostringstream oss; oss << "DataArrayDouble::keepSelectedComponents : invalid requested component : " << *it << " whereas it should be in [0," << oldNbOfCompo << ") !";
1863         throw INTERP_KERNEL::Exception(oss.str().c_str());
1864       }
1865   int nbOfTuples=getNumberOfTuples();
1866   ret->alloc(nbOfTuples,(int)newNbOfCompo);
1867   ret->copyPartOfStringInfoFrom(*this,compoIds);
1868   const double *oldc=getConstPointer();
1869   double *nc=ret->getPointer();
1870   for(int i=0;i<nbOfTuples;i++)
1871     for(std::size_t j=0;j<newNbOfCompo;j++,nc++)
1872       *nc=oldc[i*oldNbOfCompo+compoIds[j]];
1873   return ret.retn();
1874 }
1875
1876 /*!
1877  * Appends components of another array to components of \a this one, tuple by tuple.
1878  * So that the number of tuples of \a this array remains the same and the number of 
1879  * components increases.
1880  *  \param [in] other - the DataArrayDouble to append to \a this one.
1881  *  \throw If \a this is not allocated.
1882  *  \throw If \a this and \a other arrays have different number of tuples.
1883  *
1884  *  \if ENABLE_EXAMPLES
1885  *  \ref cpp_mcdataarraydouble_meldwith "Here is a C++ example".
1886  *
1887  *  \ref py_mcdataarraydouble_meldwith "Here is a Python example".
1888  *  \endif
1889  */
1890 void DataArrayDouble::meldWith(const DataArrayDouble *other)
1891 {
1892   checkAllocated();
1893   other->checkAllocated();
1894   int nbOfTuples=getNumberOfTuples();
1895   if(nbOfTuples!=other->getNumberOfTuples())
1896     throw INTERP_KERNEL::Exception("DataArrayDouble::meldWith : mismatch of number of tuples !");
1897   int nbOfComp1=getNumberOfComponents();
1898   int nbOfComp2=other->getNumberOfComponents();
1899   double *newArr=(double *)malloc((nbOfTuples*(nbOfComp1+nbOfComp2))*sizeof(double));
1900   double *w=newArr;
1901   const double *inp1=getConstPointer();
1902   const double *inp2=other->getConstPointer();
1903   for(int i=0;i<nbOfTuples;i++,inp1+=nbOfComp1,inp2+=nbOfComp2)
1904     {
1905       w=std::copy(inp1,inp1+nbOfComp1,w);
1906       w=std::copy(inp2,inp2+nbOfComp2,w);
1907     }
1908   useArray(newArr,true,C_DEALLOC,nbOfTuples,nbOfComp1+nbOfComp2);
1909   std::vector<int> compIds(nbOfComp2);
1910   for(int i=0;i<nbOfComp2;i++)
1911     compIds[i]=nbOfComp1+i;
1912   copyPartOfStringInfoFrom2(compIds,*other);
1913 }
1914
1915 /*!
1916  * This method checks that all tuples in \a other are in \a this.
1917  * If true, the output param \a tupleIds contains the tuples ids of \a this that correspond to tupes in \a this.
1918  * For each i in [ 0 , other->getNumberOfTuples() ) tuple #i in \a other is equal ( regarding input precision \a prec ) to tuple tupleIds[i] in \a this.
1919  *
1920  * \param [in] other - the array having the same number of components than \a this.
1921  * \param [out] tupleIds - the tuple ids containing the same number of tuples than \a other has.
1922  * \sa DataArrayDouble::findCommonTuples
1923  */
1924 bool DataArrayDouble::areIncludedInMe(const DataArrayDouble *other, double prec, DataArrayInt *&tupleIds) const
1925 {
1926   if(!other)
1927     throw INTERP_KERNEL::Exception("DataArrayDouble::areIncludedInMe : input array is NULL !");
1928   checkAllocated(); other->checkAllocated();
1929   if(getNumberOfComponents()!=other->getNumberOfComponents())
1930     throw INTERP_KERNEL::Exception("DataArrayDouble::areIncludedInMe : the number of components does not match !");
1931   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> a=DataArrayDouble::Aggregate(this,other);
1932   DataArrayInt *c=0,*ci=0;
1933   a->findCommonTuples(prec,getNumberOfTuples(),c,ci);
1934   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cSafe(c),ciSafe(ci);
1935   int newNbOfTuples=-1;
1936   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ids=DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(a->getNumberOfTuples(),c->begin(),ci->begin(),ci->end(),newNbOfTuples);
1937   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=ids->selectByTupleId2(getNumberOfTuples(),a->getNumberOfTuples(),1);
1938   tupleIds=ret1.retn();
1939   return newNbOfTuples==getNumberOfTuples();
1940 }
1941
1942 /*!
1943  * Searches for tuples coincident within \a prec tolerance. Each tuple is considered
1944  * as coordinates of a point in getNumberOfComponents()-dimensional space. The
1945  * distance separating two points is computed with the infinite norm.
1946  *
1947  * Indices of coincident tuples are stored in output arrays.
1948  * A pair of arrays (\a comm, \a commIndex) is called "Surjective Format 2".
1949  *
1950  * This method is typically used by MEDCouplingPointSet::findCommonNodes() and
1951  * MEDCouplingUMesh::mergeNodes().
1952  *  \param [in] prec - minimal absolute distance between two tuples (infinite norm) at which they are
1953  *              considered not coincident.
1954  *  \param [in] limitTupleId - limit tuple id. If all tuples within a group of coincident
1955  *              tuples have id strictly lower than \a limitTupleId then they are not returned.
1956  *  \param [out] comm - the array holding ids (== indices) of coincident tuples. 
1957  *               \a comm->getNumberOfComponents() == 1. 
1958  *               \a comm->getNumberOfTuples() == \a commIndex->back().
1959  *  \param [out] commIndex - the array dividing all indices stored in \a comm into
1960  *               groups of (indices of) coincident tuples. Its every value is a tuple
1961  *               index where a next group of tuples begins. For example the second
1962  *               group of tuples in \a comm is described by following range of indices:
1963  *               [ \a commIndex[1], \a commIndex[2] ). \a commIndex->getNumberOfTuples()-1
1964  *               gives the number of groups of coincident tuples.
1965  *  \throw If \a this is not allocated.
1966  *  \throw If the number of components is not in [1,2,3,4].
1967  *
1968  *  \if ENABLE_EXAMPLES
1969  *  \ref cpp_mcdataarraydouble_findcommontuples "Here is a C++ example".
1970  *
1971  *  \ref py_mcdataarraydouble_findcommontuples  "Here is a Python example".
1972  *  \endif
1973  *  \sa DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(), DataArrayDouble::areIncludedInMe
1974  */
1975 void DataArrayDouble::findCommonTuples(double prec, int limitTupleId, DataArrayInt *&comm, DataArrayInt *&commIndex) const
1976 {
1977   checkAllocated();
1978   int nbOfCompo=getNumberOfComponents();
1979   if ((nbOfCompo<1) || (nbOfCompo>4)) //test before work
1980     throw INTERP_KERNEL::Exception("DataArrayDouble::findCommonTuples : Unexpected spacedim of coords. Must be 1, 2, 3 or 4.");
1981   
1982   int nbOfTuples=getNumberOfTuples();
1983   //
1984   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> c(DataArrayInt::New()),cI(DataArrayInt::New()); c->alloc(0,1); cI->pushBackSilent(0);
1985   switch(nbOfCompo)
1986     {
1987     case 4:
1988       findCommonTuplesAlg<4>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
1989       break;
1990     case 3:
1991       findCommonTuplesAlg<3>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
1992       break;
1993     case 2:
1994       findCommonTuplesAlg<2>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
1995       break;
1996     case 1:
1997       findCommonTuplesAlg<1>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
1998       break;
1999     default:
2000       throw INTERP_KERNEL::Exception("DataArrayDouble::findCommonTuples : nb of components managed are 1,2,3 and 4 ! not implemented for other number of components !");
2001     }
2002   comm=c.retn();
2003   commIndex=cI.retn();
2004 }
2005
2006 /*!
2007  * 
2008  * \param [in] nbTimes specifies the nb of times each tuples in \a this will be duplicated contiguouly in returned DataArrayDouble instance.
2009  *             \a nbTimes  should be at least equal to 1.
2010  * \return a newly allocated DataArrayDouble having one component and number of tuples equal to \a nbTimes * \c this->getNumberOfTuples.
2011  * \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.
2012  */
2013 DataArrayDouble *DataArrayDouble::duplicateEachTupleNTimes(int nbTimes) const
2014 {
2015   checkAllocated();
2016   if(getNumberOfComponents()!=1)
2017     throw INTERP_KERNEL::Exception("DataArrayDouble::duplicateEachTupleNTimes : this should have only one component !");
2018   if(nbTimes<1)
2019     throw INTERP_KERNEL::Exception("DataArrayDouble::duplicateEachTupleNTimes : nb times should be >= 1 !");
2020   int nbTuples=getNumberOfTuples();
2021   const double *inPtr=getConstPointer();
2022   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(nbTimes*nbTuples,1);
2023   double *retPtr=ret->getPointer();
2024   for(int i=0;i<nbTuples;i++,inPtr++)
2025     {
2026       double val=*inPtr;
2027       for(int j=0;j<nbTimes;j++,retPtr++)
2028         *retPtr=val;
2029     }
2030   ret->copyStringInfoFrom(*this);
2031   return ret.retn();
2032 }
2033
2034 /*!
2035  * This methods returns the minimal distance between the two set of points \a this and \a other.
2036  * So \a this and \a other have to have the same number of components. If not an INTERP_KERNEL::Exception will be thrown.
2037  * This method works only if number of components of \a this (equal to those of \a other) is in 1, 2 or 3.
2038  *
2039  * \param [out] thisTupleId the tuple id in \a this corresponding to the returned minimal distance
2040  * \param [out] otherTupleId the tuple id in \a other corresponding to the returned minimal distance
2041  * \return the minimal distance between the two set of points \a this and \a other.
2042  * \sa DataArrayDouble::findClosestTupleId
2043  */
2044 double DataArrayDouble::minimalDistanceTo(const DataArrayDouble *other, int& thisTupleId, int& otherTupleId) const
2045 {
2046   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> part1=findClosestTupleId(other);
2047   int nbOfCompo(getNumberOfComponents());
2048   int otherNbTuples(other->getNumberOfTuples());
2049   const double *thisPt(begin()),*otherPt(other->begin());
2050   const int *part1Pt(part1->begin());
2051   double ret=std::numeric_limits<double>::max();
2052   for(int i=0;i<otherNbTuples;i++,part1Pt++,otherPt+=nbOfCompo)
2053     {
2054       double tmp(0.);
2055       for(int j=0;j<nbOfCompo;j++)
2056         tmp+=(otherPt[j]-thisPt[nbOfCompo*(*part1Pt)+j])*(otherPt[j]-thisPt[nbOfCompo*(*part1Pt)+j]);
2057       if(tmp<ret)
2058         { ret=tmp; thisTupleId=*part1Pt; otherTupleId=i; }
2059     }
2060   return sqrt(ret);
2061 }
2062
2063 /*!
2064  * This methods returns for each tuple in \a other which tuple in \a this is the closest.
2065  * So \a this and \a other have to have the same number of components. If not an INTERP_KERNEL::Exception will be thrown.
2066  * This method works only if number of components of \a this (equal to those of \a other) is in 1, 2 or 3.
2067  *
2068  * \return a newly allocated (new object to be dealt by the caller) DataArrayInt having \c other->getNumberOfTuples() tuples and one components.
2069  * \sa DataArrayDouble::minimalDistanceTo
2070  */
2071 DataArrayInt *DataArrayDouble::findClosestTupleId(const DataArrayDouble *other) const
2072 {
2073   if(!other)
2074     throw INTERP_KERNEL::Exception("DataArrayDouble::findClosestTupleId : other instance is NULL !");
2075   checkAllocated(); other->checkAllocated();
2076   int nbOfCompo=getNumberOfComponents();
2077   if(nbOfCompo!=other->getNumberOfComponents())
2078     {
2079       std::ostringstream oss; oss << "DataArrayDouble::findClosestTupleId : number of components in this is " << nbOfCompo;
2080       oss << ", whereas number of components in other is " << other->getNumberOfComponents() << "! Should be equal !";
2081       throw INTERP_KERNEL::Exception(oss.str().c_str());
2082     }
2083   int nbOfTuples=other->getNumberOfTuples();
2084   int thisNbOfTuples=getNumberOfTuples();
2085   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbOfTuples,1);
2086   double bounds[6];
2087   getMinMaxPerComponent(bounds);
2088   switch(nbOfCompo)
2089     {
2090     case 3:
2091       {
2092         double xDelta(fabs(bounds[1]-bounds[0])),yDelta(fabs(bounds[3]-bounds[2])),zDelta(fabs(bounds[5]-bounds[4]));
2093         double delta=std::max(xDelta,yDelta); delta=std::max(delta,zDelta);
2094         double characSize=pow((delta*delta*delta)/((double)thisNbOfTuples),1./3.);
2095         BBTreePts<3,int> myTree(begin(),0,0,getNumberOfTuples(),characSize*1e-12);
2096         FindClosestTupleIdAlg<3>(myTree,3.*characSize*characSize,other->begin(),nbOfTuples,begin(),thisNbOfTuples,ret->getPointer());
2097         break;
2098       }
2099     case 2:
2100       {
2101         double xDelta(fabs(bounds[1]-bounds[0])),yDelta(fabs(bounds[3]-bounds[2]));
2102         double delta=std::max(xDelta,yDelta);
2103         double characSize=sqrt(delta/(double)thisNbOfTuples);
2104         BBTreePts<2,int> myTree(begin(),0,0,getNumberOfTuples(),characSize*1e-12);
2105         FindClosestTupleIdAlg<2>(myTree,2.*characSize*characSize,other->begin(),nbOfTuples,begin(),thisNbOfTuples,ret->getPointer());
2106         break;
2107       }
2108     case 1:
2109       {
2110         double characSize=fabs(bounds[1]-bounds[0])/thisNbOfTuples;
2111         BBTreePts<1,int> myTree(begin(),0,0,getNumberOfTuples(),characSize*1e-12);
2112         FindClosestTupleIdAlg<1>(myTree,1.*characSize*characSize,other->begin(),nbOfTuples,begin(),thisNbOfTuples,ret->getPointer());
2113         break;
2114       }
2115     default:
2116       throw INTERP_KERNEL::Exception("Unexpected spacedim of coords for findClosestTupleId. Must be 1, 2 or 3.");
2117     }
2118   return ret.retn();
2119 }
2120
2121 /*!
2122  * This method expects that \a this and \a otherBBoxFrmt arrays are bounding box arrays ( as the output of MEDCouplingPointSet::getBoundingBoxForBBTree method ).
2123  * This method will return a DataArrayInt array having the same number of tuples than \a this. This returned array tells for each cell in \a this
2124  * how many bounding boxes in \a otherBBoxFrmt.
2125  * So, this method expects that \a this and \a otherBBoxFrmt have the same number of components.
2126  *
2127  * \param [in] otherBBoxFrmt - It is an array .
2128  * \param [in] eps - the absolute precision of the detection. when eps < 0 the bboxes are enlarged so more interactions are detected. Inversely when > 0 the bboxes are stretched.
2129  * \sa MEDCouplingPointSet::getBoundingBoxForBBTree
2130  * \throw If \a this and \a otherBBoxFrmt have not the same number of components.
2131  * \throw If \a this and \a otherBBoxFrmt number of components is not even (BBox format).
2132  */
2133 DataArrayInt *DataArrayDouble::computeNbOfInteractionsWith(const DataArrayDouble *otherBBoxFrmt, double eps) const
2134 {
2135   if(!otherBBoxFrmt)
2136     throw INTERP_KERNEL::Exception("DataArrayDouble::computeNbOfInteractionsWith : input array is NULL !");
2137   if(!isAllocated() || !otherBBoxFrmt->isAllocated())
2138     throw INTERP_KERNEL::Exception("DataArrayDouble::computeNbOfInteractionsWith : this and input array must be allocated !");
2139   int nbOfComp(getNumberOfComponents()),nbOfTuples(getNumberOfTuples());
2140   if(nbOfComp!=otherBBoxFrmt->getNumberOfComponents())
2141     {
2142       std::ostringstream oss; oss << "DataArrayDouble::computeNbOfInteractionsWith : this number of components (" << nbOfComp << ") must be equal to the number of components of input array (" << otherBBoxFrmt->getNumberOfComponents() << ") !";
2143       throw INTERP_KERNEL::Exception(oss.str().c_str());
2144     }
2145   if(nbOfComp%2!=0)
2146     {
2147       std::ostringstream oss; oss << "DataArrayDouble::computeNbOfInteractionsWith : Number of components (" << nbOfComp << ") is not even ! It should be to be compatible with bbox format !";
2148       throw INTERP_KERNEL::Exception(oss.str().c_str());
2149     }
2150   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(nbOfTuples,1);
2151   const double *thisBBPtr(begin());
2152   int *retPtr(ret->getPointer());
2153   switch(nbOfComp/2)
2154     {
2155     case 3:
2156       {
2157         BBTree<3,int> bbt(otherBBoxFrmt->begin(),0,0,otherBBoxFrmt->getNumberOfTuples(),eps);
2158         for(int i=0;i<nbOfTuples;i++,retPtr++,thisBBPtr+=nbOfComp)
2159           *retPtr=bbt.getNbOfIntersectingElems(thisBBPtr);
2160         break;
2161       }
2162     case 2:
2163       {
2164         BBTree<2,int> bbt(otherBBoxFrmt->begin(),0,0,otherBBoxFrmt->getNumberOfTuples(),eps);
2165         for(int i=0;i<nbOfTuples;i++,retPtr++,thisBBPtr+=nbOfComp)
2166           *retPtr=bbt.getNbOfIntersectingElems(thisBBPtr);
2167         break;
2168       }
2169     case 1:
2170       {
2171         BBTree<1,int> bbt(otherBBoxFrmt->begin(),0,0,otherBBoxFrmt->getNumberOfTuples(),eps);
2172         for(int i=0;i<nbOfTuples;i++,retPtr++,thisBBPtr+=nbOfComp)
2173           *retPtr=bbt.getNbOfIntersectingElems(thisBBPtr);
2174         break;
2175       }
2176     default:
2177       throw INTERP_KERNEL::Exception("DataArrayDouble::computeNbOfInteractionsWith : space dimension supported are [1,2,3] !");
2178     }
2179   
2180   return ret.retn();
2181 }
2182
2183 /*!
2184  * Returns a copy of \a this array by excluding coincident tuples. Each tuple is
2185  * considered as coordinates of a point in getNumberOfComponents()-dimensional
2186  * space. The distance between tuples is computed using norm2. If several tuples are
2187  * not far each from other than \a prec, only one of them remains in the result
2188  * array. The order of tuples in the result array is same as in \a this one except
2189  * that coincident tuples are excluded.
2190  *  \param [in] prec - minimal absolute distance between two tuples at which they are
2191  *              considered not coincident.
2192  *  \param [in] limitTupleId - limit tuple id. If all tuples within a group of coincident
2193  *              tuples have id strictly lower than \a limitTupleId then they are not excluded.
2194  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
2195  *          is to delete using decrRef() as it is no more needed.
2196  *  \throw If \a this is not allocated.
2197  *  \throw If the number of components is not in [1,2,3,4].
2198  *
2199  *  \if ENABLE_EXAMPLES
2200  *  \ref py_mcdataarraydouble_getdifferentvalues "Here is a Python example".
2201  *  \endif
2202  */
2203 DataArrayDouble *DataArrayDouble::getDifferentValues(double prec, int limitTupleId) const
2204 {
2205   checkAllocated();
2206   DataArrayInt *c0=0,*cI0=0;
2207   findCommonTuples(prec,limitTupleId,c0,cI0);
2208   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> c(c0),cI(cI0);
2209   int newNbOfTuples=-1;
2210   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(getNumberOfTuples(),c0->begin(),cI0->begin(),cI0->end(),newNbOfTuples);
2211   return renumberAndReduce(o2n->getConstPointer(),newNbOfTuples);
2212 }
2213
2214 /*!
2215  * Copy all components in a specified order from another DataArrayDouble.
2216  * Both numerical and textual data is copied. The number of tuples in \a this and
2217  * the other array can be different.
2218  *  \param [in] a - the array to copy data from.
2219  *  \param [in] compoIds - sequence of zero based indices of components, data of which is
2220  *              to be copied.
2221  *  \throw If \a a is NULL.
2222  *  \throw If \a compoIds.size() != \a a->getNumberOfComponents().
2223  *  \throw If \a compoIds[i] < 0 or \a compoIds[i] > \a this->getNumberOfComponents().
2224  *
2225  *  \if ENABLE_EXAMPLES
2226  *  \ref py_mcdataarraydouble_setselectedcomponents "Here is a Python example".
2227  *  \endif
2228  */
2229 void DataArrayDouble::setSelectedComponents(const DataArrayDouble *a, const std::vector<int>& compoIds)
2230 {
2231   if(!a)
2232     throw INTERP_KERNEL::Exception("DataArrayDouble::setSelectedComponents : input DataArrayDouble is NULL !");
2233   checkAllocated();
2234   copyPartOfStringInfoFrom2(compoIds,*a);
2235   std::size_t partOfCompoSz=compoIds.size();
2236   int nbOfCompo=getNumberOfComponents();
2237   int nbOfTuples=std::min(getNumberOfTuples(),a->getNumberOfTuples());
2238   const double *ac=a->getConstPointer();
2239   double *nc=getPointer();
2240   for(int i=0;i<nbOfTuples;i++)
2241     for(std::size_t j=0;j<partOfCompoSz;j++,ac++)
2242       nc[nbOfCompo*i+compoIds[j]]=*ac;
2243 }
2244
2245 /*!
2246  * Copy all values from another DataArrayDouble into specified tuples and components
2247  * of \a this array. Textual data is not copied.
2248  * The tree parameters defining set of indices of tuples and components are similar to
2249  * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
2250  *  \param [in] a - the array to copy values from.
2251  *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
2252  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
2253  *              are located.
2254  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
2255  *  \param [in] bgComp - index of the first component of \a this array to assign values to.
2256  *  \param [in] endComp - index of the component before which the components to assign
2257  *              to are located.
2258  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2259  *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
2260  *              must be equal to the number of columns to assign to, else an
2261  *              exception is thrown; if \a false, then it is only required that \a
2262  *              a->getNbOfElems() equals to number of values to assign to (this condition
2263  *              must be respected even if \a strictCompoCompare is \a true). The number of 
2264  *              values to assign to is given by following Python expression:
2265  *              \a nbTargetValues = 
2266  *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
2267  *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2268  *  \throw If \a a is NULL.
2269  *  \throw If \a a is not allocated.
2270  *  \throw If \a this is not allocated.
2271  *  \throw If parameters specifying tuples and components to assign to do not give a
2272  *            non-empty range of increasing indices.
2273  *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
2274  *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
2275  *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2276  *
2277  *  \if ENABLE_EXAMPLES
2278  *  \ref py_mcdataarraydouble_setpartofvalues1 "Here is a Python example".
2279  *  \endif
2280  */
2281 void DataArrayDouble::setPartOfValues1(const DataArrayDouble *a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
2282 {
2283   if(!a)
2284     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues1 : input DataArrayDouble is NULL !");
2285   const char msg[]="DataArrayDouble::setPartOfValues1";
2286   checkAllocated();
2287   a->checkAllocated();
2288   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2289   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2290   int nbComp=getNumberOfComponents();
2291   int nbOfTuples=getNumberOfTuples();
2292   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2293   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2294   bool assignTech=true;
2295   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2296     {
2297       if(strictCompoCompare)
2298         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2299     }
2300   else
2301     {
2302       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2303       assignTech=false;
2304     }
2305   const double *srcPt=a->getConstPointer();
2306   double *pt=getPointer()+bgTuples*nbComp+bgComp;
2307   if(assignTech)
2308     {
2309       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2310         for(int j=0;j<newNbOfComp;j++,srcPt++)
2311           pt[j*stepComp]=*srcPt;
2312     }
2313   else
2314     {
2315       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2316         {
2317           const double *srcPt2=srcPt;
2318           for(int j=0;j<newNbOfComp;j++,srcPt2++)
2319             pt[j*stepComp]=*srcPt2;
2320         }
2321     }
2322 }
2323
2324 /*!
2325  * Assign a given value to values at specified tuples and components of \a this array.
2326  * The tree parameters defining set of indices of tuples and components are similar to
2327  * the tree parameters of the Python function \c range(\c start,\c stop,\c step)..
2328  *  \param [in] a - the value to assign.
2329  *  \param [in] bgTuples - index of the first tuple of \a this array to assign to.
2330  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
2331  *              are located.
2332  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
2333  *  \param [in] bgComp - index of the first component of \a this array to assign to.
2334  *  \param [in] endComp - index of the component before which the components to assign
2335  *              to are located.
2336  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2337  *  \throw If \a this is not allocated.
2338  *  \throw If parameters specifying tuples and components to assign to, do not give a
2339  *            non-empty range of increasing indices or indices are out of a valid range
2340  *            for \this array.
2341  *
2342  *  \if ENABLE_EXAMPLES
2343  *  \ref py_mcdataarraydouble_setpartofvaluessimple1 "Here is a Python example".
2344  *  \endif
2345  */
2346 void DataArrayDouble::setPartOfValuesSimple1(double a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp)
2347 {
2348   const char msg[]="DataArrayDouble::setPartOfValuesSimple1";
2349   checkAllocated();
2350   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2351   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2352   int nbComp=getNumberOfComponents();
2353   int nbOfTuples=getNumberOfTuples();
2354   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2355   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2356   double *pt=getPointer()+bgTuples*nbComp+bgComp;
2357   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2358     for(int j=0;j<newNbOfComp;j++)
2359       pt[j*stepComp]=a;
2360 }
2361
2362 /*!
2363  * Copy all values from another DataArrayDouble (\a a) into specified tuples and 
2364  * components of \a this array. Textual data is not copied.
2365  * The tuples and components to assign to are defined by C arrays of indices.
2366  * There are two *modes of usage*:
2367  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
2368  *   of \a a is assigned to its own location within \a this array. 
2369  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
2370  *   components of every specified tuple of \a this array. In this mode it is required
2371  *   that \a a->getNumberOfComponents() equals to the number of specified components.
2372  *
2373  *  \param [in] a - the array to copy values from.
2374  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2375  *              assign values of \a a to.
2376  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2377  *              pointer to a tuple index <em>(pi)</em> varies as this: 
2378  *              \a bgTuples <= \a pi < \a endTuples.
2379  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
2380  *              assign values of \a a to.
2381  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
2382  *              pointer to a component index <em>(pi)</em> varies as this: 
2383  *              \a bgComp <= \a pi < \a endComp.
2384  *  \param [in] strictCompoCompare - this parameter is checked only if the
2385  *               *mode of usage* is the first; if it is \a true (default), 
2386  *               then \a a->getNumberOfComponents() must be equal 
2387  *               to the number of specified columns, else this is not required.
2388  *  \throw If \a a is NULL.
2389  *  \throw If \a a is not allocated.
2390  *  \throw If \a this is not allocated.
2391  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
2392  *         out of a valid range for \a this array.
2393  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
2394  *         if <em> a->getNumberOfComponents() != (endComp - bgComp) </em>.
2395  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
2396  *         <em> a->getNumberOfComponents() != (endComp - bgComp)</em>.
2397  *
2398  *  \if ENABLE_EXAMPLES
2399  *  \ref py_mcdataarraydouble_setpartofvalues2 "Here is a Python example".
2400  *  \endif
2401  */
2402 void DataArrayDouble::setPartOfValues2(const DataArrayDouble *a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
2403 {
2404   if(!a)
2405     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues2 : input DataArrayDouble is NULL !");
2406   const char msg[]="DataArrayDouble::setPartOfValues2";
2407   checkAllocated();
2408   a->checkAllocated();
2409   int nbComp=getNumberOfComponents();
2410   int nbOfTuples=getNumberOfTuples();
2411   for(const int *z=bgComp;z!=endComp;z++)
2412     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2413   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
2414   int newNbOfComp=(int)std::distance(bgComp,endComp);
2415   bool assignTech=true;
2416   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2417     {
2418       if(strictCompoCompare)
2419         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2420     }
2421   else
2422     {
2423       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2424       assignTech=false;
2425     }
2426   double *pt=getPointer();
2427   const double *srcPt=a->getConstPointer();
2428   if(assignTech)
2429     {    
2430       for(const int *w=bgTuples;w!=endTuples;w++)
2431         {
2432           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2433           for(const int *z=bgComp;z!=endComp;z++,srcPt++)
2434             {    
2435               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt;
2436             }
2437         }
2438     }
2439   else
2440     {
2441       for(const int *w=bgTuples;w!=endTuples;w++)
2442         {
2443           const double *srcPt2=srcPt;
2444           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2445           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
2446             {    
2447               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt2;
2448             }
2449         }
2450     }
2451 }
2452
2453 /*!
2454  * Assign a given value to values at specified tuples and components of \a this array.
2455  * The tuples and components to assign to are defined by C arrays of indices.
2456  *  \param [in] a - the value to assign.
2457  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2458  *              assign \a a to.
2459  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2460  *              pointer to a tuple index (\a pi) varies as this: 
2461  *              \a bgTuples <= \a pi < \a endTuples.
2462  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
2463  *              assign \a a to.
2464  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
2465  *              pointer to a component index (\a pi) varies as this: 
2466  *              \a bgComp <= \a pi < \a endComp.
2467  *  \throw If \a this is not allocated.
2468  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
2469  *         out of a valid range for \a this array.
2470  *
2471  *  \if ENABLE_EXAMPLES
2472  *  \ref py_mcdataarraydouble_setpartofvaluessimple2 "Here is a Python example".
2473  *  \endif
2474  */
2475 void DataArrayDouble::setPartOfValuesSimple2(double a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp)
2476 {
2477   checkAllocated();
2478   int nbComp=getNumberOfComponents();
2479   int nbOfTuples=getNumberOfTuples();
2480   for(const int *z=bgComp;z!=endComp;z++)
2481     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2482   double *pt=getPointer();
2483   for(const int *w=bgTuples;w!=endTuples;w++)
2484     for(const int *z=bgComp;z!=endComp;z++)
2485       {
2486         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2487         pt[(std::size_t)(*w)*nbComp+(*z)]=a;
2488       }
2489 }
2490
2491 /*!
2492  * Copy all values from another DataArrayDouble (\a a) into specified tuples and 
2493  * components of \a this array. Textual data is not copied.
2494  * The tuples to assign to are defined by a C array of indices.
2495  * The components to assign to are defined by three values similar to parameters of
2496  * the Python function \c range(\c start,\c stop,\c step).
2497  * There are two *modes of usage*:
2498  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
2499  *   of \a a is assigned to its own location within \a this array. 
2500  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
2501  *   components of every specified tuple of \a this array. In this mode it is required
2502  *   that \a a->getNumberOfComponents() equals to the number of specified components.
2503  *
2504  *  \param [in] a - the array to copy values from.
2505  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2506  *              assign values of \a a to.
2507  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2508  *              pointer to a tuple index <em>(pi)</em> varies as this: 
2509  *              \a bgTuples <= \a pi < \a endTuples.
2510  *  \param [in] bgComp - index of the first component of \a this array to assign to.
2511  *  \param [in] endComp - index of the component before which the components to assign
2512  *              to are located.
2513  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2514  *  \param [in] strictCompoCompare - this parameter is checked only in the first
2515  *               *mode of usage*; if \a strictCompoCompare is \a true (default), 
2516  *               then \a a->getNumberOfComponents() must be equal 
2517  *               to the number of specified columns, else this is not required.
2518  *  \throw If \a a is NULL.
2519  *  \throw If \a a is not allocated.
2520  *  \throw If \a this is not allocated.
2521  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
2522  *         \a this array.
2523  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
2524  *         if <em> a->getNumberOfComponents()</em> is unequal to the number of components
2525  *         defined by <em>(bgComp,endComp,stepComp)</em>.
2526  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
2527  *         <em> a->getNumberOfComponents()</em> is unequal to the number of components
2528  *         defined by <em>(bgComp,endComp,stepComp)</em>.
2529  *  \throw If parameters specifying components to assign to, do not give a
2530  *            non-empty range of increasing indices or indices are out of a valid range
2531  *            for \this array.
2532  *
2533  *  \if ENABLE_EXAMPLES
2534  *  \ref py_mcdataarraydouble_setpartofvalues3 "Here is a Python example".
2535  *  \endif
2536  */
2537 void DataArrayDouble::setPartOfValues3(const DataArrayDouble *a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
2538 {
2539   if(!a)
2540     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues3 : input DataArrayDouble is NULL !");
2541   const char msg[]="DataArrayDouble::setPartOfValues3";
2542   checkAllocated();
2543   a->checkAllocated();
2544   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2545   int nbComp=getNumberOfComponents();
2546   int nbOfTuples=getNumberOfTuples();
2547   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2548   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
2549   bool assignTech=true;
2550   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2551     {
2552       if(strictCompoCompare)
2553         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2554     }
2555   else
2556     {
2557       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2558       assignTech=false;
2559     }
2560   double *pt=getPointer()+bgComp;
2561   const double *srcPt=a->getConstPointer();
2562   if(assignTech)
2563     {
2564       for(const int *w=bgTuples;w!=endTuples;w++)
2565         for(int j=0;j<newNbOfComp;j++,srcPt++)
2566           {
2567             DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2568             pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt;
2569           }
2570     }
2571   else
2572     {
2573       for(const int *w=bgTuples;w!=endTuples;w++)
2574         {
2575           const double *srcPt2=srcPt;
2576           for(int j=0;j<newNbOfComp;j++,srcPt2++)
2577             {
2578               DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2579               pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt2;
2580             }
2581         }
2582     }
2583 }
2584
2585 /*!
2586  * Assign a given value to values at specified tuples and components of \a this array.
2587  * The tuples to assign to are defined by a C array of indices.
2588  * The components to assign to are defined by three values similar to parameters of
2589  * the Python function \c range(\c start,\c stop,\c step).
2590  *  \param [in] a - the value to assign.
2591  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2592  *              assign \a a to.
2593  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2594  *              pointer to a tuple index <em>(pi)</em> varies as this: 
2595  *              \a bgTuples <= \a pi < \a endTuples.
2596  *  \param [in] bgComp - index of the first component of \a this array to assign to.
2597  *  \param [in] endComp - index of the component before which the components to assign
2598  *              to are located.
2599  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2600  *  \throw If \a this is not allocated.
2601  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
2602  *         \a this array.
2603  *  \throw If parameters specifying components to assign to, do not give a
2604  *            non-empty range of increasing indices or indices are out of a valid range
2605  *            for \this array.
2606  *
2607  *  \if ENABLE_EXAMPLES
2608  *  \ref py_mcdataarraydouble_setpartofvaluessimple3 "Here is a Python example".
2609  *  \endif
2610  */
2611 void DataArrayDouble::setPartOfValuesSimple3(double a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp)
2612 {
2613   const char msg[]="DataArrayDouble::setPartOfValuesSimple3";
2614   checkAllocated();
2615   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2616   int nbComp=getNumberOfComponents();
2617   int nbOfTuples=getNumberOfTuples();
2618   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2619   double *pt=getPointer()+bgComp;
2620   for(const int *w=bgTuples;w!=endTuples;w++)
2621     for(int j=0;j<newNbOfComp;j++)
2622       {
2623         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2624         pt[(std::size_t)(*w)*nbComp+j*stepComp]=a;
2625       }
2626 }
2627
2628 /*!
2629  * Copy all values from another DataArrayDouble into specified tuples and components
2630  * of \a this array. Textual data is not copied.
2631  * The tree parameters defining set of indices of tuples and components are similar to
2632  * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
2633  *  \param [in] a - the array to copy values from.
2634  *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
2635  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
2636  *              are located.
2637  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
2638  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
2639  *              assign \a a to.
2640  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
2641  *              pointer to a component index (\a pi) varies as this: 
2642  *              \a bgComp <= \a pi < \a endComp.
2643  *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
2644  *              must be equal to the number of columns to assign to, else an
2645  *              exception is thrown; if \a false, then it is only required that \a
2646  *              a->getNbOfElems() equals to number of values to assign to (this condition
2647  *              must be respected even if \a strictCompoCompare is \a true). The number of 
2648  *              values to assign to is given by following Python expression:
2649  *              \a nbTargetValues = 
2650  *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
2651  *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2652  *  \throw If \a a is NULL.
2653  *  \throw If \a a is not allocated.
2654  *  \throw If \a this is not allocated.
2655  *  \throw If parameters specifying tuples and components to assign to do not give a
2656  *            non-empty range of increasing indices.
2657  *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
2658  *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
2659  *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2660  *
2661  */
2662 void DataArrayDouble::setPartOfValues4(const DataArrayDouble *a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
2663 {
2664   if(!a)
2665     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues4 : input DataArrayDouble is NULL !");
2666   const char msg[]="DataArrayDouble::setPartOfValues4";
2667   checkAllocated();
2668   a->checkAllocated();
2669   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2670   int newNbOfComp=(int)std::distance(bgComp,endComp);
2671   int nbComp=getNumberOfComponents();
2672   for(const int *z=bgComp;z!=endComp;z++)
2673     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2674   int nbOfTuples=getNumberOfTuples();
2675   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2676   bool assignTech=true;
2677   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2678     {
2679       if(strictCompoCompare)
2680         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2681     }
2682   else
2683     {
2684       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2685       assignTech=false;
2686     }
2687   const double *srcPt=a->getConstPointer();
2688   double *pt=getPointer()+bgTuples*nbComp;
2689   if(assignTech)
2690     {
2691       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2692         for(const int *z=bgComp;z!=endComp;z++,srcPt++)
2693           pt[*z]=*srcPt;
2694     }
2695   else
2696     {
2697       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2698         {
2699           const double *srcPt2=srcPt;
2700           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
2701             pt[*z]=*srcPt2;
2702         }
2703     }
2704 }
2705
2706 void DataArrayDouble::setPartOfValuesSimple4(double a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp)
2707 {
2708   const char msg[]="DataArrayDouble::setPartOfValuesSimple4";
2709   checkAllocated();
2710   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2711   int nbComp=getNumberOfComponents();
2712   for(const int *z=bgComp;z!=endComp;z++)
2713     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2714   int nbOfTuples=getNumberOfTuples();
2715   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2716   double *pt=getPointer()+bgTuples*nbComp;
2717   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2718     for(const int *z=bgComp;z!=endComp;z++)
2719       pt[*z]=a;
2720 }
2721
2722 /*!
2723  * Copy some tuples from another DataArrayDouble into specified tuples
2724  * of \a this array. Textual data is not copied. Both arrays must have equal number of
2725  * components.
2726  * Both the tuples to assign and the tuples to assign to are defined by a DataArrayInt.
2727  * All components of selected tuples are copied.
2728  *  \param [in] a - the array to copy values from.
2729  *  \param [in] tuplesSelec - the array specifying both source tuples of \a a and
2730  *              target tuples of \a this. \a tuplesSelec has two components, and the
2731  *              first component specifies index of the source tuple and the second
2732  *              one specifies index of the target tuple.
2733  *  \throw If \a this is not allocated.
2734  *  \throw If \a a is NULL.
2735  *  \throw If \a a is not allocated.
2736  *  \throw If \a tuplesSelec is NULL.
2737  *  \throw If \a tuplesSelec is not allocated.
2738  *  \throw If <em>this->getNumberOfComponents() != a->getNumberOfComponents()</em>.
2739  *  \throw If \a tuplesSelec->getNumberOfComponents() != 2.
2740  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
2741  *         the corresponding (\a this or \a a) array.
2742  */
2743 void DataArrayDouble::setPartOfValuesAdv(const DataArrayDouble *a, const DataArrayInt *tuplesSelec)
2744 {
2745   if(!a || !tuplesSelec)
2746     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValuesAdv : input DataArrayDouble is NULL !");
2747   checkAllocated();
2748   a->checkAllocated();
2749   tuplesSelec->checkAllocated();
2750   int nbOfComp=getNumberOfComponents();
2751   if(nbOfComp!=a->getNumberOfComponents())
2752     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValuesAdv : This and a do not have the same number of components !");
2753   if(tuplesSelec->getNumberOfComponents()!=2)
2754     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValuesAdv : Expecting to have a tuple selector DataArrayInt instance with exactly 2 components !");
2755   int thisNt=getNumberOfTuples();
2756   int aNt=a->getNumberOfTuples();
2757   double *valsToSet=getPointer();
2758   const double *valsSrc=a->getConstPointer();
2759   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple+=2)
2760     {
2761       if(tuple[1]>=0 && tuple[1]<aNt)
2762         {
2763           if(tuple[0]>=0 && tuple[0]<thisNt)
2764             std::copy(valsSrc+nbOfComp*tuple[1],valsSrc+nbOfComp*(tuple[1]+1),valsToSet+nbOfComp*tuple[0]);
2765           else
2766             {
2767               std::ostringstream oss; oss << "DataArrayDouble::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
2768               oss << " of 'tuplesSelec' request of tuple id #" << tuple[0] << " in 'this' ! It should be in [0," << thisNt << ") !";
2769               throw INTERP_KERNEL::Exception(oss.str().c_str());
2770             }
2771         }
2772       else
2773         {
2774           std::ostringstream oss; oss << "DataArrayDouble::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
2775           oss << " of 'tuplesSelec' request of tuple id #" << tuple[1] << " in 'a' ! It should be in [0," << aNt << ") !";
2776           throw INTERP_KERNEL::Exception(oss.str().c_str());
2777         }
2778     }
2779 }
2780
2781 /*!
2782  * Copy some tuples from another DataArrayDouble (\a aBase) into contiguous tuples
2783  * of \a this array. Textual data is not copied. Both arrays must have equal number of
2784  * components.
2785  * The tuples to assign to are defined by index of the first tuple, and
2786  * their number is defined by \a tuplesSelec->getNumberOfTuples().
2787  * The tuples to copy are defined by values of a DataArrayInt.
2788  * All components of selected tuples are copied.
2789  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
2790  *              values to.
2791  *  \param [in] aBase - the array to copy values from.
2792  *  \param [in] tuplesSelec - the array specifying tuples of \a a to copy.
2793  *  \throw If \a this is not allocated.
2794  *  \throw If \a aBase is NULL.
2795  *  \throw If \a aBase is not allocated.
2796  *  \throw If \a tuplesSelec is NULL.
2797  *  \throw If \a tuplesSelec is not allocated.
2798  *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
2799  *  \throw If \a tuplesSelec->getNumberOfComponents() != 1.
2800  *  \throw If <em>tupleIdStart + tuplesSelec->getNumberOfTuples() > this->getNumberOfTuples().</em>
2801  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
2802  *         \a aBase array.
2803  */
2804 void DataArrayDouble::setContigPartOfSelectedValues(int tupleIdStart, const DataArray *aBase, const DataArrayInt *tuplesSelec)
2805 {
2806   if(!aBase || !tuplesSelec)
2807     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : input DataArray is NULL !");
2808   const DataArrayDouble *a=dynamic_cast<const DataArrayDouble *>(aBase);
2809   if(!a)
2810     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : input DataArray aBase is not a DataArrayDouble !");
2811   checkAllocated();
2812   a->checkAllocated();
2813   tuplesSelec->checkAllocated();
2814   int nbOfComp=getNumberOfComponents();
2815   if(nbOfComp!=a->getNumberOfComponents())
2816     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : This and a do not have the same number of components !");
2817   if(tuplesSelec->getNumberOfComponents()!=1)
2818     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : Expecting to have a tuple selector DataArrayInt instance with exactly 1 component !");
2819   int thisNt=getNumberOfTuples();
2820   int aNt=a->getNumberOfTuples();
2821   int nbOfTupleToWrite=tuplesSelec->getNumberOfTuples();
2822   double *valsToSet=getPointer()+tupleIdStart*nbOfComp;
2823   if(tupleIdStart+nbOfTupleToWrite>thisNt)
2824     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : invalid number range of values to write !");
2825   const double *valsSrc=a->getConstPointer();
2826   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple++,valsToSet+=nbOfComp)
2827     {
2828       if(*tuple>=0 && *tuple<aNt)
2829         {
2830           std::copy(valsSrc+nbOfComp*(*tuple),valsSrc+nbOfComp*(*tuple+1),valsToSet);
2831         }
2832       else
2833         {
2834           std::ostringstream oss; oss << "DataArrayDouble::setContigPartOfSelectedValues : Tuple #" << std::distance(tuplesSelec->begin(),tuple);
2835           oss << " of 'tuplesSelec' request of tuple id #" << *tuple << " in 'a' ! It should be in [0," << aNt << ") !";
2836           throw INTERP_KERNEL::Exception(oss.str().c_str());
2837         }
2838     }
2839 }
2840
2841 /*!
2842  * Copy some tuples from another DataArrayDouble (\a aBase) into contiguous tuples
2843  * of \a this array. Textual data is not copied. Both arrays must have equal number of
2844  * components.
2845  * The tuples to copy are defined by three values similar to parameters of
2846  * the Python function \c range(\c start,\c stop,\c step).
2847  * The tuples to assign to are defined by index of the first tuple, and
2848  * their number is defined by number of tuples to copy.
2849  * All components of selected tuples are copied.
2850  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
2851  *              values to.
2852  *  \param [in] aBase - the array to copy values from.
2853  *  \param [in] bg - index of the first tuple to copy of the array \a aBase.
2854  *  \param [in] end2 - index of the tuple of \a aBase before which the tuples to copy
2855  *              are located.
2856  *  \param [in] step - index increment to get index of the next tuple to copy.
2857  *  \throw If \a this is not allocated.
2858  *  \throw If \a aBase is NULL.
2859  *  \throw If \a aBase is not allocated.
2860  *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
2861  *  \throw If <em>tupleIdStart + len(range(bg,end2,step)) > this->getNumberOfTuples().</em>
2862  *  \throw If parameters specifying tuples to copy, do not give a
2863  *            non-empty range of increasing indices or indices are out of a valid range
2864  *            for the array \a aBase.
2865  */
2866 void DataArrayDouble::setContigPartOfSelectedValues2(int tupleIdStart, const DataArray *aBase, int bg, int end2, int step)
2867 {
2868   if(!aBase)
2869     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : input DataArray is NULL !");
2870   const DataArrayDouble *a=dynamic_cast<const DataArrayDouble *>(aBase);
2871   if(!a)
2872     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : input DataArray aBase is not a DataArrayDouble !");
2873   checkAllocated();
2874   a->checkAllocated();
2875   int nbOfComp=getNumberOfComponents();
2876   const char msg[]="DataArrayDouble::setContigPartOfSelectedValues2";
2877   int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg);
2878   if(nbOfComp!=a->getNumberOfComponents())
2879     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : This and a do not have the same number of components !");
2880   int thisNt=getNumberOfTuples();
2881   int aNt=a->getNumberOfTuples();
2882   double *valsToSet=getPointer()+tupleIdStart*nbOfComp;
2883   if(tupleIdStart+nbOfTupleToWrite>thisNt)
2884     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid number range of values to write !");
2885   if(end2>aNt)
2886     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid range of values to read !");
2887   const double *valsSrc=a->getConstPointer()+bg*nbOfComp;
2888   for(int i=0;i<nbOfTupleToWrite;i++,valsToSet+=nbOfComp,valsSrc+=step*nbOfComp)
2889     {
2890       std::copy(valsSrc,valsSrc+nbOfComp,valsToSet);
2891     }
2892 }
2893
2894 /*!
2895  * Returns a value located at specified tuple and component.
2896  * This method is equivalent to DataArrayDouble::getIJ() except that validity of
2897  * parameters is checked. So this method is safe but expensive if used to go through
2898  * all values of \a this.
2899  *  \param [in] tupleId - index of tuple of interest.
2900  *  \param [in] compoId - index of component of interest.
2901  *  \return double - value located by \a tupleId and \a compoId.
2902  *  \throw If \a this is not allocated.
2903  *  \throw If condition <em>( 0 <= tupleId < this->getNumberOfTuples() )</em> is violated.
2904  *  \throw If condition <em>( 0 <= compoId < this->getNumberOfComponents() )</em> is violated.
2905  */
2906 double DataArrayDouble::getIJSafe(int tupleId, int compoId) const
2907 {
2908   checkAllocated();
2909   if(tupleId<0 || tupleId>=getNumberOfTuples())
2910     {
2911       std::ostringstream oss; oss << "DataArrayDouble::getIJSafe : request for tupleId " << tupleId << " should be in [0," << getNumberOfTuples() << ") !";
2912       throw INTERP_KERNEL::Exception(oss.str().c_str());
2913     }
2914   if(compoId<0 || compoId>=getNumberOfComponents())
2915     {
2916       std::ostringstream oss; oss << "DataArrayDouble::getIJSafe : request for compoId " << compoId << " should be in [0," << getNumberOfComponents() << ") !";
2917       throw INTERP_KERNEL::Exception(oss.str().c_str());
2918     }
2919   return _mem[tupleId*_info_on_compo.size()+compoId];
2920 }
2921
2922 /*!
2923  * Returns the first value of \a this. 
2924  *  \return double - the last value of \a this array.
2925  *  \throw If \a this is not allocated.
2926  *  \throw If \a this->getNumberOfComponents() != 1.
2927  *  \throw If \a this->getNumberOfTuples() < 1.
2928  */
2929 double DataArrayDouble::front() const
2930 {
2931   checkAllocated();
2932   if(getNumberOfComponents()!=1)
2933     throw INTERP_KERNEL::Exception("DataArrayDouble::front : number of components not equal to one !");
2934   int nbOfTuples=getNumberOfTuples();
2935   if(nbOfTuples<1)
2936     throw INTERP_KERNEL::Exception("DataArrayDouble::front : number of tuples must be >= 1 !");
2937   return *(getConstPointer());
2938 }
2939
2940 /*!
2941  * Returns the last value of \a this. 
2942  *  \return double - the last value of \a this array.
2943  *  \throw If \a this is not allocated.
2944  *  \throw If \a this->getNumberOfComponents() != 1.
2945  *  \throw If \a this->getNumberOfTuples() < 1.
2946  */
2947 double DataArrayDouble::back() const
2948 {
2949   checkAllocated();
2950   if(getNumberOfComponents()!=1)
2951     throw INTERP_KERNEL::Exception("DataArrayDouble::back : number of components not equal to one !");
2952   int nbOfTuples=getNumberOfTuples();
2953   if(nbOfTuples<1)
2954     throw INTERP_KERNEL::Exception("DataArrayDouble::back : number of tuples must be >= 1 !");
2955   return *(getConstPointer()+nbOfTuples-1);
2956 }
2957
2958 void DataArrayDouble::SetArrayIn(DataArrayDouble *newArray, DataArrayDouble* &arrayToSet)
2959 {
2960   if(newArray!=arrayToSet)
2961     {
2962       if(arrayToSet)
2963         arrayToSet->decrRef();
2964       arrayToSet=newArray;
2965       if(arrayToSet)
2966         arrayToSet->incrRef();
2967     }
2968 }
2969
2970 /*!
2971  * Sets a C array to be used as raw data of \a this. The previously set info
2972  *  of components is retained and re-sized. 
2973  * For more info see \ref MEDCouplingArraySteps1.
2974  *  \param [in] array - the C array to be used as raw data of \a this.
2975  *  \param [in] ownership - if \a true, \a array will be deallocated at destruction of \a this.
2976  *  \param [in] type - specifies how to deallocate \a array. If \a type == ParaMEDMEM::CPP_DEALLOC,
2977  *                     \c delete [] \c array; will be called. If \a type == ParaMEDMEM::C_DEALLOC,
2978  *                     \c free(\c array ) will be called.
2979  *  \param [in] nbOfTuple - new number of tuples in \a this.
2980  *  \param [in] nbOfCompo - new number of components in \a this.
2981  */
2982 void DataArrayDouble::useArray(const double *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo)
2983 {
2984   _info_on_compo.resize(nbOfCompo);
2985   _mem.useArray(array,ownership,type,(std::size_t)nbOfTuple*nbOfCompo);
2986   declareAsNew();
2987 }
2988
2989 void DataArrayDouble::useExternalArrayWithRWAccess(const double *array, int nbOfTuple, int nbOfCompo)
2990 {
2991   _info_on_compo.resize(nbOfCompo);
2992   _mem.useExternalArrayWithRWAccess(array,(std::size_t)nbOfTuple*nbOfCompo);
2993   declareAsNew();
2994 }
2995
2996 /*!
2997  * Checks if 0.0 value is present in \a this array. If it is the case, an exception
2998  * is thrown.
2999  * \throw If zero is found in \a this array.
3000  */
3001 void DataArrayDouble::checkNoNullValues() const
3002 {
3003   const double *tmp=getConstPointer();
3004   std::size_t nbOfElems=getNbOfElems();
3005   const double *where=std::find(tmp,tmp+nbOfElems,0.);
3006   if(where!=tmp+nbOfElems)
3007     throw INTERP_KERNEL::Exception("A value 0.0 have been detected !");
3008 }
3009
3010 /*!
3011  * Computes minimal and maximal value in each component. An output array is filled
3012  * with \c 2 * \a this->getNumberOfComponents() values, so the caller is to allocate
3013  * enough memory before calling this method.
3014  *  \param [out] bounds - array of size at least 2 *\a this->getNumberOfComponents().
3015  *               It is filled as follows:<br>
3016  *               \a bounds[0] = \c min_of_component_0 <br>
3017  *               \a bounds[1] = \c max_of_component_0 <br>
3018  *               \a bounds[2] = \c min_of_component_1 <br>
3019  *               \a bounds[3] = \c max_of_component_1 <br>
3020  *               ...
3021  */
3022 void DataArrayDouble::getMinMaxPerComponent(double *bounds) const
3023 {
3024   checkAllocated();
3025   int dim=getNumberOfComponents();
3026   for (int idim=0; idim<dim; idim++)
3027     {
3028       bounds[idim*2]=std::numeric_limits<double>::max();
3029       bounds[idim*2+1]=-std::numeric_limits<double>::max();
3030     } 
3031   const double *ptr=getConstPointer();
3032   int nbOfTuples=getNumberOfTuples();
3033   for(int i=0;i<nbOfTuples;i++)
3034     {
3035       for(int idim=0;idim<dim;idim++)
3036         {
3037           if(bounds[idim*2]>ptr[i*dim+idim])
3038             {
3039               bounds[idim*2]=ptr[i*dim+idim];
3040             }
3041           if(bounds[idim*2+1]<ptr[i*dim+idim])
3042             {
3043               bounds[idim*2+1]=ptr[i*dim+idim];
3044             }
3045         }
3046     }
3047 }
3048
3049 /*!
3050  * This method retrieves a newly allocated DataArrayDouble instance having same number of tuples than \a this and twice number of components than \a this
3051  * to store both the min and max per component of each tuples. 
3052  * \param [in] epsilon the width of the bbox (identical in each direction) - 0.0 by default
3053  *
3054  * \return a newly created DataArrayDouble instance having \c this->getNumberOfTuples() tuples and 2 * \c this->getNumberOfComponent() components
3055  *
3056  * \throw If \a this is not allocated yet.
3057  */
3058 DataArrayDouble *DataArrayDouble::computeBBoxPerTuple(double epsilon) const
3059 {
3060   checkAllocated();
3061   const double *dataPtr=getConstPointer();
3062   int nbOfCompo=getNumberOfComponents();
3063   int nbTuples=getNumberOfTuples();
3064   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> bbox=DataArrayDouble::New();
3065   bbox->alloc(nbTuples,2*nbOfCompo);
3066   double *bboxPtr=bbox->getPointer();
3067   for(int i=0;i<nbTuples;i++)
3068     {
3069       for(int j=0;j<nbOfCompo;j++)
3070         {
3071           bboxPtr[2*nbOfCompo*i+2*j]=dataPtr[nbOfCompo*i+j]-epsilon;
3072           bboxPtr[2*nbOfCompo*i+2*j+1]=dataPtr[nbOfCompo*i+j]+epsilon;
3073         }
3074     }
3075   return bbox.retn();
3076 }
3077
3078 /*!
3079  * For each tuples **t** in \a other, this method retrieves tuples in \a this that are equal to **t**.
3080  * Two tuples are considered equal if the euclidian distance between the two tuples is lower than \a eps.
3081  * 
3082  * \param [in] other a DataArrayDouble having same number of components than \a this.
3083  * \param [in] eps absolute precision representing distance (using infinite norm) between 2 tuples behind which 2 tuples are considered equal.
3084  * \param [out] c will contain the set of tuple ids in \a this that are equal to to the tuple ids in \a other contiguously.
3085  *             \a cI allows to extract information in \a c.
3086  * \param [out] cI is an indirection array that allows to extract the data contained in \a c.
3087  *
3088  * \throw In case of:
3089  *  - \a this is not allocated
3090  *  - \a other is not allocated or null
3091  *  - \a this and \a other do not have the same number of components
3092  *  - if number of components of \a this is not in [1,2,3]
3093  *
3094  * \sa MEDCouplingPointSet::getNodeIdsNearPoints, DataArrayDouble::getDifferentValues
3095  */
3096 void DataArrayDouble::computeTupleIdsNearTuples(const DataArrayDouble *other, double eps, DataArrayInt *& c, DataArrayInt *& cI) const
3097 {
3098   if(!other)
3099     throw INTERP_KERNEL::Exception("DataArrayDouble::computeTupleIdsNearTuples : input pointer other is null !");
3100   checkAllocated();
3101   other->checkAllocated();
3102   int nbOfCompo=getNumberOfComponents();
3103   int otherNbOfCompo=other->getNumberOfComponents();
3104   if(nbOfCompo!=otherNbOfCompo)
3105     throw INTERP_KERNEL::Exception("DataArrayDouble::computeTupleIdsNearTuples : number of components should be equal between this and other !");
3106   int nbOfTuplesOther=other->getNumberOfTuples();
3107   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cArr(DataArrayInt::New()),cIArr(DataArrayInt::New()); cArr->alloc(0,1); cIArr->pushBackSilent(0);
3108   switch(nbOfCompo)
3109     {
3110     case 3:
3111       {
3112         BBTreePts<3,int> myTree(begin(),0,0,getNumberOfTuples(),eps);
3113         FindTupleIdsNearTuplesAlg<3>(myTree,other->getConstPointer(),nbOfTuplesOther,eps,cArr,cIArr);
3114         break;
3115       }
3116     case 2:
3117       {
3118         BBTreePts<2,int> myTree(begin(),0,0,getNumberOfTuples(),eps);
3119         FindTupleIdsNearTuplesAlg<2>(myTree,other->getConstPointer(),nbOfTuplesOther,eps,cArr,cIArr);
3120         break;
3121       }
3122     case 1:
3123       {
3124         BBTreePts<1,int> myTree(begin(),0,0,getNumberOfTuples(),eps);
3125         FindTupleIdsNearTuplesAlg<1>(myTree,other->getConstPointer(),nbOfTuplesOther,eps,cArr,cIArr);
3126         break;
3127       }
3128     default:
3129       throw INTERP_KERNEL::Exception("Unexpected spacedim of coords for computeTupleIdsNearTuples. Must be 1, 2 or 3.");
3130     }
3131   c=cArr.retn(); cI=cIArr.retn();
3132 }
3133
3134 /*!
3135  * This method recenter tuples in \b this in order to be centered at the origin to benefit about the advantages of maximal precision to be around the box
3136  * around origin of 'radius' 1.
3137  * 
3138  * \param [in] eps absolute epsilon. under that value of delta between max and min no scale is performed.
3139  */
3140 void DataArrayDouble::recenterForMaxPrecision(double eps)
3141 {
3142   checkAllocated();
3143   int dim=getNumberOfComponents();
3144   std::vector<double> bounds(2*dim);
3145   getMinMaxPerComponent(&bounds[0]);
3146   for(int i=0;i<dim;i++)
3147     {
3148       double delta=bounds[2*i+1]-bounds[2*i];
3149       double offset=(bounds[2*i]+bounds[2*i+1])/2.;
3150       if(delta>eps)
3151         applyLin(1./delta,-offset/delta,i);
3152       else
3153         applyLin(1.,-offset,i);
3154     }
3155 }
3156
3157 /*!
3158  * Returns the maximal value and its location within \a this one-dimensional array.
3159  *  \param [out] tupleId - index of the tuple holding the maximal value.
3160  *  \return double - the maximal value among all values of \a this array.
3161  *  \throw If \a this->getNumberOfComponents() != 1
3162  *  \throw If \a this->getNumberOfTuples() < 1
3163  */
3164 double DataArrayDouble::getMaxValue(int& tupleId) const
3165 {
3166   checkAllocated();
3167   if(getNumberOfComponents()!=1)
3168     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 !");
3169   int nbOfTuples=getNumberOfTuples();
3170   if(nbOfTuples<=0)
3171     throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : array exists but number of tuples must be > 0 !");
3172   const double *vals=getConstPointer();
3173   const double *loc=std::max_element(vals,vals+nbOfTuples);
3174   tupleId=(int)std::distance(vals,loc);
3175   return *loc;
3176 }
3177
3178 /*!
3179  * Returns the maximal value within \a this array that is allowed to have more than
3180  *  one component.
3181  *  \return double - the maximal value among all values of \a this array.
3182  *  \throw If \a this is not allocated.
3183  */
3184 double DataArrayDouble::getMaxValueInArray() const
3185 {
3186   checkAllocated();
3187   const double *loc=std::max_element(begin(),end());
3188   return *loc;
3189 }
3190
3191 /*!
3192  * Returns the maximal value and all its locations within \a this one-dimensional array.
3193  *  \param [out] tupleIds - a new instance of DataArrayInt containg indices of
3194  *               tuples holding the maximal value. The caller is to delete it using
3195  *               decrRef() as it is no more needed.
3196  *  \return double - the maximal value among all values of \a this array.
3197  *  \throw If \a this->getNumberOfComponents() != 1
3198  *  \throw If \a this->getNumberOfTuples() < 1
3199  */
3200 double DataArrayDouble::getMaxValue2(DataArrayInt*& tupleIds) const
3201 {
3202   int tmp;
3203   tupleIds=0;
3204   double ret=getMaxValue(tmp);
3205   tupleIds=getIdsInRange(ret,ret);
3206   return ret;
3207 }
3208
3209 /*!
3210  * Returns the minimal value and its location within \a this one-dimensional array.
3211  *  \param [out] tupleId - index of the tuple holding the minimal value.
3212  *  \return double - the minimal value among all values of \a this array.
3213  *  \throw If \a this->getNumberOfComponents() != 1
3214  *  \throw If \a this->getNumberOfTuples() < 1
3215  */
3216 double DataArrayDouble::getMinValue(int& tupleId) const
3217 {
3218   checkAllocated();
3219   if(getNumberOfComponents()!=1)
3220     throw INTERP_KERNEL::Exception("DataArrayDouble::getMinValue : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before call 'getMinValueInArray' method !");
3221   int nbOfTuples=getNumberOfTuples();
3222   if(nbOfTuples<=0)
3223     throw INTERP_KERNEL::Exception("DataArrayDouble::getMinValue : array exists but number of tuples must be > 0 !");
3224   const double *vals=getConstPointer();
3225   const double *loc=std::min_element(vals,vals+nbOfTuples);
3226   tupleId=(int)std::distance(vals,loc);
3227   return *loc;
3228 }
3229
3230 /*!
3231  * Returns the minimal value within \a this array that is allowed to have more than
3232  *  one component.
3233  *  \return double - the minimal value among all values of \a this array.
3234  *  \throw If \a this is not allocated.
3235  */
3236 double DataArrayDouble::getMinValueInArray() const
3237 {
3238   checkAllocated();
3239   const double *loc=std::min_element(begin(),end());
3240   return *loc;
3241 }
3242
3243 /*!
3244  * Returns the minimal value and all its locations within \a this one-dimensional array.
3245  *  \param [out] tupleIds - a new instance of DataArrayInt containg indices of
3246  *               tuples holding the minimal value. The caller is to delete it using
3247  *               decrRef() as it is no more needed.
3248  *  \return double - the minimal value among all values of \a this array.
3249  *  \throw If \a this->getNumberOfComponents() != 1
3250  *  \throw If \a this->getNumberOfTuples() < 1
3251  */
3252 double DataArrayDouble::getMinValue2(DataArrayInt*& tupleIds) const
3253 {
3254   int tmp;
3255   tupleIds=0;
3256   double ret=getMinValue(tmp);
3257   tupleIds=getIdsInRange(ret,ret);
3258   return ret;
3259 }
3260
3261 /*!
3262  * This method returns the number of values in \a this that are equals ( within an absolute precision of \a eps ) to input parameter \a value.
3263  * This method only works for single component array.
3264  *
3265  * \return a value in [ 0, \c this->getNumberOfTuples() )
3266  *
3267  * \throw If \a this is not allocated
3268  *
3269  */
3270 int DataArrayDouble::count(double value, double eps) const
3271 {
3272   int ret=0;
3273   checkAllocated();
3274   if(getNumberOfComponents()!=1)
3275     throw INTERP_KERNEL::Exception("DataArrayDouble::count : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before !");
3276   const double *vals=begin();
3277   int nbOfTuples=getNumberOfTuples();
3278   for(int i=0;i<nbOfTuples;i++,vals++)
3279     if(fabs(*vals-value)<=eps)
3280       ret++;
3281   return ret;
3282 }
3283
3284 /*!
3285  * Returns the average value of \a this one-dimensional array.
3286  *  \return double - the average value over all values of \a this array.
3287  *  \throw If \a this->getNumberOfComponents() != 1
3288  *  \throw If \a this->getNumberOfTuples() < 1
3289  */
3290 double DataArrayDouble::getAverageValue() const
3291 {
3292   if(getNumberOfComponents()!=1)
3293     throw INTERP_KERNEL::Exception("DataArrayDouble::getAverageValue : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before !");
3294   int nbOfTuples=getNumberOfTuples();
3295   if(nbOfTuples<=0)
3296     throw INTERP_KERNEL::Exception("DataArrayDouble::getAverageValue : array exists but number of tuples must be > 0 !");
3297   const double *vals=getConstPointer();
3298   double ret=std::accumulate(vals,vals+nbOfTuples,0.);
3299   return ret/nbOfTuples;
3300 }
3301
3302 /*!
3303  * Returns the Euclidean norm of the vector defined by \a this array.
3304  *  \return double - the value of the Euclidean norm, i.e.
3305  *          the square root of the inner product of vector.
3306  *  \throw If \a this is not allocated.
3307  */
3308 double DataArrayDouble::norm2() const
3309 {
3310   checkAllocated();
3311   double ret=0.;
3312   std::size_t nbOfElems=getNbOfElems();
3313   const double *pt=getConstPointer();
3314   for(std::size_t i=0;i<nbOfElems;i++,pt++)
3315     ret+=(*pt)*(*pt);
3316   return sqrt(ret);
3317 }
3318
3319 /*!
3320  * Returns the maximum norm of the vector defined by \a this array.
3321  * This method works even if the number of components is diferent from one.
3322  * If the number of elements in \a this is 0, -1. is returned.
3323  *  \return double - the value of the maximum norm, i.e.
3324  *          the maximal absolute value among values of \a this array (whatever its number of components).
3325  *  \throw If \a this is not allocated.
3326  */
3327 double DataArrayDouble::normMax() const
3328 {
3329   checkAllocated();
3330   double ret(-1.);
3331   std::size_t nbOfElems(getNbOfElems());
3332   const double *pt(getConstPointer());
3333   for(std::size_t i=0;i<nbOfElems;i++,pt++)
3334     {
3335       double val(std::abs(*pt));
3336       if(val>ret)
3337         ret=val;
3338     }
3339   return ret;
3340 }
3341
3342 /*!
3343  * Returns the minimum norm (absolute value) of the vector defined by \a this array.
3344  * This method works even if the number of components is diferent from one.
3345  * If the number of elements in \a this is 0, std::numeric_limits<double>::max() is returned.
3346  *  \return double - the value of the minimum norm, i.e.
3347  *          the minimal absolute value among values of \a this array (whatever its number of components).
3348  *  \throw If \a this is not allocated.
3349  */
3350 double DataArrayDouble::normMin() const
3351 {
3352   checkAllocated();
3353   double ret(std::numeric_limits<double>::max());
3354   std::size_t nbOfElems(getNbOfElems());
3355   const double *pt(getConstPointer());
3356   for(std::size_t i=0;i<nbOfElems;i++,pt++)
3357     {
3358       double val(std::abs(*pt));
3359       if(val<ret)
3360         ret=val;
3361     }
3362   return ret;
3363 }
3364
3365 /*!
3366  * Accumulates values of each component of \a this array.
3367  *  \param [out] res - an array of length \a this->getNumberOfComponents(), allocated 
3368  *         by the caller, that is filled by this method with sum value for each
3369  *         component.
3370  *  \throw If \a this is not allocated.
3371  */
3372 void DataArrayDouble::accumulate(double *res) const
3373 {
3374   checkAllocated();
3375   const double *ptr=getConstPointer();
3376   int nbTuple=getNumberOfTuples();
3377   int nbComps=getNumberOfComponents();
3378   std::fill(res,res+nbComps,0.);
3379   for(int i=0;i<nbTuple;i++)
3380     std::transform(ptr+i*nbComps,ptr+(i+1)*nbComps,res,res,std::plus<double>());
3381 }
3382
3383 /*!
3384  * This method returns the min distance from an external tuple defined by [ \a tupleBg , \a tupleEnd ) to \a this and
3385  * the first tuple in \a this that matches the returned distance. If there is no tuples in \a this an exception will be thrown.
3386  *
3387  *
3388  * \a this is expected to be allocated and expected to have a number of components equal to the distance from \a tupleBg to
3389  * \a tupleEnd. If not an exception will be thrown.
3390  *
3391  * \param [in] tupleBg start pointer (included) of input external tuple
3392  * \param [in] tupleEnd end pointer (not included) of input external tuple
3393  * \param [out] tupleId the tuple id in \a this that matches the min of distance between \a this and input external tuple
3394  * \return the min distance.
3395  * \sa MEDCouplingUMesh::distanceToPoint
3396  */
3397 double DataArrayDouble::distanceToTuple(const double *tupleBg, const double *tupleEnd, int& tupleId) const
3398 {
3399   checkAllocated();
3400   int nbTuple=getNumberOfTuples();
3401   int nbComps=getNumberOfComponents();
3402   if(nbComps!=(int)std::distance(tupleBg,tupleEnd))
3403     { std::ostringstream oss; oss << "DataArrayDouble::distanceToTuple : size of input tuple is " << std::distance(tupleBg,tupleEnd) << " should be equal to the number of components in this : " << nbComps << " !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); }
3404   if(nbTuple==0)
3405     throw INTERP_KERNEL::Exception("DataArrayDouble::distanceToTuple : no tuple in this ! No distance to compute !");
3406   double ret0=std::numeric_limits<double>::max();
3407   tupleId=-1;
3408   const double *work=getConstPointer();
3409   for(int i=0;i<nbTuple;i++)
3410     {
3411       double val=0.;
3412       for(int j=0;j<nbComps;j++,work++) 
3413         val+=(*work-tupleBg[j])*((*work-tupleBg[j]));
3414       if(val>=ret0)
3415         continue;
3416       else
3417         { ret0=val; tupleId=i; }
3418     }
3419   return sqrt(ret0);
3420 }
3421
3422 /*!
3423  * Accumulate values of the given component of \a this array.
3424  *  \param [in] compId - the index of the component of interest.
3425  *  \return double - a sum value of \a compId-th component.
3426  *  \throw If \a this is not allocated.
3427  *  \throw If \a the condition ( 0 <= \a compId < \a this->getNumberOfComponents() ) is
3428  *         not respected.
3429  */
3430 double DataArrayDouble::accumulate(int compId) const
3431 {
3432   checkAllocated();
3433   const double *ptr=getConstPointer();
3434   int nbTuple=getNumberOfTuples();
3435   int nbComps=getNumberOfComponents();
3436   if(compId<0 || compId>=nbComps)
3437     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulate : Invalid compId specified : No such nb of components !");
3438   double ret=0.;
3439   for(int i=0;i<nbTuple;i++)
3440     ret+=ptr[i*nbComps+compId];
3441   return ret;
3442 }
3443
3444 /*!
3445  * This method accumulate using addition tuples in \a this using input index array [ \a bgOfIndex, \a endOfIndex ).
3446  * The returned array will have same number of components than \a this and number of tuples equal to
3447  * \c std::distance(bgOfIndex,endOfIndex) \b minus \b one.
3448  *
3449  * The input index array is expected to be ascendingly sorted in which the all referenced ids should be in [0, \c this->getNumberOfTuples).
3450  * This method is quite useful for users that need to put a field on cells to field on nodes on the same mesh without a need of conservation.
3451  *
3452  * \param [in] bgOfIndex - begin (included) of the input index array.
3453  * \param [in] endOfIndex - end (excluded) of the input index array.
3454  * \return DataArrayDouble * - the new instance having the same number of components than \a this.
3455  * 
3456  * \throw If bgOfIndex or end is NULL.
3457  * \throw If input index array is not ascendingly sorted.
3458  * \throw If there is an id in [ \a bgOfIndex, \a endOfIndex ) not in [0, \c this->getNumberOfTuples).
3459  * \throw If std::distance(bgOfIndex,endOfIndex)==0.
3460  */
3461 DataArrayDouble *DataArrayDouble::accumulatePerChunck(const int *bgOfIndex, const int *endOfIndex) const
3462 {
3463   if(!bgOfIndex || !endOfIndex)
3464     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : input pointer NULL !");
3465   checkAllocated();
3466   int nbCompo=getNumberOfComponents();
3467   int nbOfTuples=getNumberOfTuples();
3468   int sz=(int)std::distance(bgOfIndex,endOfIndex);
3469   if(sz<1)
3470     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : invalid size of input index array !");
3471   sz--;
3472   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(sz,nbCompo);
3473   const int *w=bgOfIndex;
3474   if(*w<0 || *w>=nbOfTuples)
3475     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : The first element of the input index not in [0,nbOfTuples) !");
3476   const double *srcPt=begin()+(*w)*nbCompo;
3477   double *tmp=ret->getPointer();
3478   for(int i=0;i<sz;i++,tmp+=nbCompo,w++)
3479     {
3480       std::fill(tmp,tmp+nbCompo,0.);
3481       if(w[1]>=w[0])
3482         {
3483           for(int j=w[0];j<w[1];j++,srcPt+=nbCompo)
3484             {
3485               if(j>=0 && j<nbOfTuples)
3486                 std::transform(srcPt,srcPt+nbCompo,tmp,tmp,std::plus<double>());
3487               else
3488                 {
3489                   std::ostringstream oss; oss << "DataArrayDouble::accumulatePerChunck : At rank #" << i << " the input index array points to id " << j << " should be in [0," << nbOfTuples << ") !";
3490                   throw INTERP_KERNEL::Exception(oss.str().c_str());
3491                 }
3492             }
3493         }
3494       else
3495         {
3496           std::ostringstream oss; oss << "DataArrayDouble::accumulatePerChunck : At rank #" << i << " the input index array is not in ascendingly sorted.";
3497           throw INTERP_KERNEL::Exception(oss.str().c_str());
3498         }
3499     }
3500   ret->copyStringInfoFrom(*this);
3501   return ret.retn();
3502 }
3503
3504 /*!
3505  * Converts each 2D point defined by the tuple of \a this array from the Polar to the
3506  * Cartesian coordinate system. The two components of the tuple of \a this array are 
3507  * considered to contain (1) radius and (2) angle of the point in the Polar CS.
3508  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3509  *          contains X and Y coordinates of the point in the Cartesian CS. The caller
3510  *          is to delete this array using decrRef() as it is no more needed. The array
3511  *          does not contain any textual info on components.
3512  *  \throw If \a this->getNumberOfComponents() != 2.
3513  */
3514 DataArrayDouble *DataArrayDouble::fromPolarToCart() const
3515 {
3516   checkAllocated();
3517   int nbOfComp=getNumberOfComponents();
3518   if(nbOfComp!=2)
3519     throw INTERP_KERNEL::Exception("DataArrayDouble::fromPolarToCart : must be an array with exactly 2 components !");
3520   int nbOfTuple=getNumberOfTuples();
3521   DataArrayDouble *ret=DataArrayDouble::New();
3522   ret->alloc(nbOfTuple,2);
3523   double *w=ret->getPointer();
3524   const double *wIn=getConstPointer();
3525   for(int i=0;i<nbOfTuple;i++,w+=2,wIn+=2)
3526     {
3527       w[0]=wIn[0]*cos(wIn[1]);
3528       w[1]=wIn[0]*sin(wIn[1]);
3529     }
3530   return ret;
3531 }
3532
3533 /*!
3534  * Converts each 3D point defined by the tuple of \a this array from the Cylindrical to
3535  * the Cartesian coordinate system. The three components of the tuple of \a this array 
3536  * are considered to contain (1) radius, (2) azimuth and (3) altitude of the point in
3537  * the Cylindrical CS.
3538  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3539  *          contains X, Y and Z coordinates of the point in the Cartesian CS. The info
3540  *          on the third component is copied from \a this array. The caller
3541  *          is to delete this array using decrRef() as it is no more needed. 
3542  *  \throw If \a this->getNumberOfComponents() != 3.
3543  */
3544 DataArrayDouble *DataArrayDouble::fromCylToCart() const
3545 {
3546   checkAllocated();
3547   int nbOfComp=getNumberOfComponents();
3548   if(nbOfComp!=3)
3549     throw INTERP_KERNEL::Exception("DataArrayDouble::fromCylToCart : must be an array with exactly 3 components !");
3550   int nbOfTuple=getNumberOfTuples();
3551   DataArrayDouble *ret=DataArrayDouble::New();
3552   ret->alloc(getNumberOfTuples(),3);
3553   double *w=ret->getPointer();
3554   const double *wIn=getConstPointer();
3555   for(int i=0;i<nbOfTuple;i++,w+=3,wIn+=3)
3556     {
3557       w[0]=wIn[0]*cos(wIn[1]);
3558       w[1]=wIn[0]*sin(wIn[1]);
3559       w[2]=wIn[2];
3560     }
3561   ret->setInfoOnComponent(2,getInfoOnComponent(2));
3562   return ret;
3563 }
3564
3565 /*!
3566  * Converts each 3D point defined by the tuple of \a this array from the Spherical to
3567  * the Cartesian coordinate system. The three components of the tuple of \a this array 
3568  * are considered to contain (1) radius, (2) polar angle and (3) azimuthal angle of the
3569  * point in the Cylindrical CS.
3570  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3571  *          contains X, Y and Z coordinates of the point in the Cartesian CS. The info
3572  *          on the third component is copied from \a this array. The caller
3573  *          is to delete this array using decrRef() as it is no more needed.
3574  *  \throw If \a this->getNumberOfComponents() != 3.
3575  */
3576 DataArrayDouble *DataArrayDouble::fromSpherToCart() const
3577 {
3578   checkAllocated();
3579   int nbOfComp=getNumberOfComponents();
3580   if(nbOfComp!=3)
3581     throw INTERP_KERNEL::Exception("DataArrayDouble::fromSpherToCart : must be an array with exactly 3 components !");
3582   int nbOfTuple=getNumberOfTuples();
3583   DataArrayDouble *ret=DataArrayDouble::New();
3584   ret->alloc(getNumberOfTuples(),3);
3585   double *w=ret->getPointer();
3586   const double *wIn=getConstPointer();
3587   for(int i=0;i<nbOfTuple;i++,w+=3,wIn+=3)
3588     {
3589       w[0]=wIn[0]*cos(wIn[2])*sin(wIn[1]);
3590       w[1]=wIn[0]*sin(wIn[2])*sin(wIn[1]);
3591       w[2]=wIn[0]*cos(wIn[1]);
3592     }
3593   return ret;
3594 }
3595
3596 /*!
3597  * Computes the doubly contracted product of every tensor defined by the tuple of \a this
3598  * array contating 6 components.
3599  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3600  *          is calculated from the tuple <em>(t)</em> of \a this array as follows:
3601  *          \f$ t[0]^2+t[1]^2+t[2]^2+2*t[3]^2+2*t[4]^2+2*t[5]^2\f$.
3602  *         The caller is to delete this result array using decrRef() as it is no more needed. 
3603  *  \throw If \a this->getNumberOfComponents() != 6.
3604  */
3605 DataArrayDouble *DataArrayDouble::doublyContractedProduct() const
3606 {
3607   checkAllocated();
3608   int nbOfComp=getNumberOfComponents();
3609   if(nbOfComp!=6)
3610     throw INTERP_KERNEL::Exception("DataArrayDouble::doublyContractedProduct : must be an array with exactly 6 components !");
3611   DataArrayDouble *ret=DataArrayDouble::New();
3612   int nbOfTuple=getNumberOfTuples();
3613   ret->alloc(nbOfTuple,1);
3614   const double *src=getConstPointer();
3615   double *dest=ret->getPointer();
3616   for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
3617     *dest=src[0]*src[0]+src[1]*src[1]+src[2]*src[2]+2.*src[3]*src[3]+2.*src[4]*src[4]+2.*src[5]*src[5];
3618   return ret;
3619 }
3620
3621 /*!
3622  * Computes the determinant of every square matrix defined by the tuple of \a this
3623  * array, which contains either 4, 6 or 9 components. The case of 6 components
3624  * corresponds to that of the upper triangular matrix.
3625  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3626  *          is the determinant of matrix of the corresponding tuple of \a this array.
3627  *          The caller is to delete this result array using decrRef() as it is no more
3628  *          needed. 
3629  *  \throw If \a this->getNumberOfComponents() is not in [4,6,9].
3630  */
3631 DataArrayDouble *DataArrayDouble::determinant() const
3632 {
3633   checkAllocated();
3634   DataArrayDouble *ret=DataArrayDouble::New();
3635   int nbOfTuple=getNumberOfTuples();
3636   ret->alloc(nbOfTuple,1);
3637   const double *src=getConstPointer();
3638   double *dest=ret->getPointer();
3639   switch(getNumberOfComponents())
3640     {
3641     case 6:
3642       for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
3643         *dest=src[0]*src[1]*src[2]+2.*src[4]*src[5]*src[3]-src[0]*src[4]*src[4]-src[2]*src[3]*src[3]-src[1]*src[5]*src[5];
3644       return ret;
3645     case 4:
3646       for(int i=0;i<nbOfTuple;i++,dest++,src+=4)
3647         *dest=src[0]*src[3]-src[1]*src[2];
3648       return ret;
3649     case 9:
3650       for(int i=0;i<nbOfTuple;i++,dest++,src+=9)
3651         *dest=src[0]*src[4]*src[8]+src[1]*src[5]*src[6]+src[2]*src[3]*src[7]-src[0]*src[5]*src[7]-src[1]*src[3]*src[8]-src[2]*src[4]*src[6];
3652       return ret;
3653     default:
3654       ret->decrRef();
3655       throw INTERP_KERNEL::Exception("DataArrayDouble::determinant : Invalid number of components ! must be in 4,6,9 !");
3656     }
3657 }
3658
3659 /*!
3660  * Computes 3 eigenvalues of every upper triangular matrix defined by the tuple of
3661  * \a this array, which contains 6 components.
3662  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing 3
3663  *          components, whose each tuple contains the eigenvalues of the matrix of
3664  *          corresponding tuple of \a this array. 
3665  *          The caller is to delete this result array using decrRef() as it is no more
3666  *          needed. 
3667  *  \throw If \a this->getNumberOfComponents() != 6.
3668  */
3669 DataArrayDouble *DataArrayDouble::eigenValues() const
3670 {
3671   checkAllocated();
3672   int nbOfComp=getNumberOfComponents();
3673   if(nbOfComp!=6)
3674     throw INTERP_KERNEL::Exception("DataArrayDouble::eigenValues : must be an array with exactly 6 components !");
3675   DataArrayDouble *ret=DataArrayDouble::New();
3676   int nbOfTuple=getNumberOfTuples();
3677   ret->alloc(nbOfTuple,3);
3678   const double *src=getConstPointer();
3679   double *dest=ret->getPointer();
3680   for(int i=0;i<nbOfTuple;i++,dest+=3,src+=6)
3681     INTERP_KERNEL::computeEigenValues6(src,dest);
3682   return ret;
3683 }
3684
3685 /*!
3686  * Computes 3 eigenvectors of every upper triangular matrix defined by the tuple of
3687  * \a this array, which contains 6 components.
3688  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing 9
3689  *          components, whose each tuple contains 3 eigenvectors of the matrix of
3690  *          corresponding tuple of \a this array.
3691  *          The caller is to delete this result array using decrRef() as it is no more
3692  *          needed.
3693  *  \throw If \a this->getNumberOfComponents() != 6.
3694  */
3695 DataArrayDouble *DataArrayDouble::eigenVectors() const
3696 {
3697   checkAllocated();
3698   int nbOfComp=getNumberOfComponents();
3699   if(nbOfComp!=6)
3700     throw INTERP_KERNEL::Exception("DataArrayDouble::eigenVectors : must be an array with exactly 6 components !");
3701   DataArrayDouble *ret=DataArrayDouble::New();
3702   int nbOfTuple=getNumberOfTuples();
3703   ret->alloc(nbOfTuple,9);
3704   const double *src=getConstPointer();
3705   double *dest=ret->getPointer();
3706   for(int i=0;i<nbOfTuple;i++,src+=6)
3707     {
3708       double tmp[3];
3709       INTERP_KERNEL::computeEigenValues6(src,tmp);
3710       for(int j=0;j<3;j++,dest+=3)
3711         INTERP_KERNEL::computeEigenVectorForEigenValue6(src,tmp[j],1e-12,dest);
3712     }
3713   return ret;
3714 }
3715
3716 /*!
3717  * Computes the inverse matrix of every matrix defined by the tuple of \a this
3718  * array, which contains either 4, 6 or 9 components. The case of 6 components
3719  * corresponds to that of the upper triangular matrix.
3720  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3721  *          same number of components as \a this one, whose each tuple is the inverse
3722  *          matrix of the matrix of corresponding tuple of \a this array. 
3723  *          The caller is to delete this result array using decrRef() as it is no more
3724  *          needed. 
3725  *  \throw If \a this->getNumberOfComponents() is not in [4,6,9].
3726  */
3727 DataArrayDouble *DataArrayDouble::inverse() const
3728 {
3729   checkAllocated();
3730   int nbOfComp=getNumberOfComponents();
3731   if(nbOfComp!=6 && nbOfComp!=9 && nbOfComp!=4)
3732     throw INTERP_KERNEL::Exception("DataArrayDouble::inversion : must be an array with 4,6 or 9 components !");
3733   DataArrayDouble *ret=DataArrayDouble::New();
3734   int nbOfTuple=getNumberOfTuples();
3735   ret->alloc(nbOfTuple,nbOfComp);
3736   const double *src=getConstPointer();
3737   double *dest=ret->getPointer();
3738 if(nbOfComp==6)
3739     for(int i=0;i<nbOfTuple;i++,dest+=6,src+=6)
3740       {
3741         double det=src[0]*src[1]*src[2]+2.*src[4]*src[5]*src[3]-src[0]*src[4]*src[4]-src[2]*src[3]*src[3]-src[1]*src[5]*src[5];
3742         dest[0]=(src[1]*src[2]-src[4]*src[4])/det;
3743         dest[1]=(src[0]*src[2]-src[5]*src[5])/det;
3744         dest[2]=(src[0]*src[1]-src[3]*src[3])/det;
3745         dest[3]=(src[5]*src[4]-src[3]*src[2])/det;
3746         dest[4]=(src[5]*src[3]-src[0]*src[4])/det;
3747         dest[5]=(src[3]*src[4]-src[1]*src[5])/det;
3748       }
3749   else if(nbOfComp==4)
3750     for(int i=0;i<nbOfTuple;i++,dest+=4,src+=4)
3751       {
3752         double det=src[0]*src[3]-src[1]*src[2];
3753         dest[0]=src[3]/det;
3754         dest[1]=-src[1]/det;
3755         dest[2]=-src[2]/det;
3756         dest[3]=src[0]/det;
3757       }
3758   else
3759     for(int i=0;i<nbOfTuple;i++,dest+=9,src+=9)
3760       {
3761         double det=src[0]*src[4]*src[8]+src[1]*src[5]*src[6]+src[2]*src[3]*src[7]-src[0]*src[5]*src[7]-src[1]*src[3]*src[8]-src[2]*src[4]*src[6];
3762         dest[0]=(src[4]*src[8]-src[7]*src[5])/det;
3763         dest[1]=(src[7]*src[2]-src[1]*src[8])/det;
3764         dest[2]=(src[1]*src[5]-src[4]*src[2])/det;
3765         dest[3]=(src[6]*src[5]-src[3]*src[8])/det;
3766         dest[4]=(src[0]*src[8]-src[6]*src[2])/det;
3767         dest[5]=(src[2]*src[3]-src[0]*src[5])/det;
3768         dest[6]=(src[3]*src[7]-src[6]*src[4])/det;
3769         dest[7]=(src[6]*src[1]-src[0]*src[7])/det;
3770         dest[8]=(src[0]*src[4]-src[1]*src[3])/det;
3771       }
3772   return ret;
3773 }
3774
3775 /*!
3776  * Computes the trace of every matrix defined by the tuple of \a this
3777  * array, which contains either 4, 6 or 9 components. The case of 6 components
3778  * corresponds to that of the upper triangular matrix.
3779  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing 
3780  *          1 component, whose each tuple is the trace of
3781  *          the matrix of corresponding tuple of \a this array. 
3782  *          The caller is to delete this result array using decrRef() as it is no more
3783  *          needed. 
3784  *  \throw If \a this->getNumberOfComponents() is not in [4,6,9].
3785  */
3786 DataArrayDouble *DataArrayDouble::trace() const
3787 {
3788   checkAllocated();
3789   int nbOfComp=getNumberOfComponents();
3790   if(nbOfComp!=6 && nbOfComp!=9 && nbOfComp!=4)
3791     throw INTERP_KERNEL::Exception("DataArrayDouble::trace : must be an array with 4,6 or 9 components !");
3792   DataArrayDouble *ret=DataArrayDouble::New();
3793   int nbOfTuple=getNumberOfTuples();
3794   ret->alloc(nbOfTuple,1);
3795   const double *src=getConstPointer();
3796   double *dest=ret->getPointer();
3797   if(nbOfComp==6)
3798     for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
3799       *dest=src[0]+src[1]+src[2];
3800   else if(nbOfComp==4)
3801     for(int i=0;i<nbOfTuple;i++,dest++,src+=4)
3802       *dest=src[0]+src[3];
3803   else
3804     for(int i=0;i<nbOfTuple;i++,dest++,src+=9)
3805       *dest=src[0]+src[4]+src[8];
3806   return ret;
3807 }
3808
3809 /*!
3810  * Computes the stress deviator tensor of every stress tensor defined by the tuple of
3811  * \a this array, which contains 6 components.
3812  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3813  *          same number of components and tuples as \a this array.
3814  *          The caller is to delete this result array using decrRef() as it is no more
3815  *          needed.
3816  *  \throw If \a this->getNumberOfComponents() != 6.
3817  */
3818 DataArrayDouble *DataArrayDouble::deviator() const
3819 {
3820   checkAllocated();
3821   int nbOfComp=getNumberOfComponents();
3822   if(nbOfComp!=6)
3823     throw INTERP_KERNEL::Exception("DataArrayDouble::deviator : must be an array with exactly 6 components !");
3824   DataArrayDouble *ret=DataArrayDouble::New();
3825   int nbOfTuple=getNumberOfTuples();
3826   ret->alloc(nbOfTuple,6);
3827   const double *src=getConstPointer();
3828   double *dest=ret->getPointer();
3829   for(int i=0;i<nbOfTuple;i++,dest+=6,src+=6)
3830     {
3831       double tr=(src[0]+src[1]+src[2])/3.;
3832       dest[0]=src[0]-tr;
3833       dest[1]=src[1]-tr;
3834       dest[2]=src[2]-tr;
3835       dest[3]=src[3];
3836       dest[4]=src[4];
3837       dest[5]=src[5];
3838     }
3839   return ret;
3840 }
3841
3842 /*!
3843  * Computes the magnitude of every vector defined by the tuple of
3844  * \a this array.
3845  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3846  *          same number of tuples as \a this array and one component.
3847  *          The caller is to delete this result array using decrRef() as it is no more
3848  *          needed.
3849  *  \throw If \a this is not allocated.
3850  */
3851 DataArrayDouble *DataArrayDouble::magnitude() const
3852 {
3853   checkAllocated();
3854   int nbOfComp=getNumberOfComponents();
3855   DataArrayDouble *ret=DataArrayDouble::New();
3856   int nbOfTuple=getNumberOfTuples();
3857   ret->alloc(nbOfTuple,1);
3858   const double *src=getConstPointer();
3859   double *dest=ret->getPointer();
3860   for(int i=0;i<nbOfTuple;i++,dest++)
3861     {
3862       double sum=0.;
3863       for(int j=0;j<nbOfComp;j++,src++)
3864         sum+=(*src)*(*src);
3865       *dest=sqrt(sum);
3866     }
3867   return ret;
3868 }
3869
3870 /*!
3871  * Computes for each tuple the sum of number of components values in the tuple and return it.
3872  * 
3873  * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3874  *          same number of tuples as \a this array and one component.
3875  *          The caller is to delete this result array using decrRef() as it is no more
3876  *          needed.
3877  *  \throw If \a this is not allocated.
3878  */
3879 DataArrayDouble *DataArrayDouble::sumPerTuple() const
3880 {
3881   checkAllocated();
3882   int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
3883   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
3884   ret->alloc(nbOfTuple,1);
3885   const double *src(getConstPointer());
3886   double *dest(ret->getPointer());
3887   for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
3888     *dest=std::accumulate(src,src+nbOfComp,0.);
3889   return ret.retn();
3890 }
3891
3892 /*!
3893  * Computes the maximal value within every tuple of \a this array.
3894  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3895  *          same number of tuples as \a this array and one component.
3896  *          The caller is to delete this result array using decrRef() as it is no more
3897  *          needed.
3898  *  \throw If \a this is not allocated.
3899  *  \sa DataArrayDouble::maxPerTupleWithCompoId
3900  */
3901 DataArrayDouble *DataArrayDouble::maxPerTuple() const
3902 {
3903   checkAllocated();
3904   int nbOfComp=getNumberOfComponents();
3905   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
3906   int nbOfTuple=getNumberOfTuples();
3907   ret->alloc(nbOfTuple,1);
3908   const double *src=getConstPointer();
3909   double *dest=ret->getPointer();
3910   for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
3911     *dest=*std::max_element(src,src+nbOfComp);
3912   return ret.retn();
3913 }
3914
3915 /*!
3916  * Computes the maximal value within every tuple of \a this array and it returns the first component
3917  * id for each tuple that corresponds to the maximal value within the tuple.
3918  * 
3919  *  \param [out] compoIdOfMaxPerTuple - the new new instance of DataArrayInt containing the
3920  *          same number of tuples and only one component.
3921  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3922  *          same number of tuples as \a this array and one component.
3923  *          The caller is to delete this result array using decrRef() as it is no more
3924  *          needed.
3925  *  \throw If \a this is not allocated.
3926  *  \sa DataArrayDouble::maxPerTuple
3927  */
3928 DataArrayDouble *DataArrayDouble::maxPerTupleWithCompoId(DataArrayInt* &compoIdOfMaxPerTuple) const
3929 {
3930   checkAllocated();
3931   int nbOfComp=getNumberOfComponents();
3932   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret0=DataArrayDouble::New();
3933   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=DataArrayInt::New();
3934   int nbOfTuple=getNumberOfTuples();
3935   ret0->alloc(nbOfTuple,1); ret1->alloc(nbOfTuple,1);
3936   const double *src=getConstPointer();
3937   double *dest=ret0->getPointer(); int *dest1=ret1->getPointer();
3938   for(int i=0;i<nbOfTuple;i++,dest++,dest1++,src+=nbOfComp)
3939     {
3940       const double *loc=std::max_element(src,src+nbOfComp);
3941       *dest=*loc;
3942       *dest1=(int)std::distance(src,loc);
3943     }
3944   compoIdOfMaxPerTuple=ret1.retn();
3945   return ret0.retn();
3946 }
3947
3948 /*!
3949  * This method returns a newly allocated DataArrayDouble instance having one component and \c this->getNumberOfTuples() * \c this->getNumberOfTuples() tuples.
3950  * \n This returned array contains the euclidian distance for each tuple in \a this. 
3951  * \n So the returned array can be seen as a dense symmetrical matrix whose diagonal elements are equal to 0.
3952  * \n The returned array has only one component (and **not** \c this->getNumberOfTuples() components to avoid the useless memory consumption due to components info in returned DataArrayDouble)
3953  *
3954  * \warning use this method with care because it can leads to big amount of consumed memory !
3955  * 
3956  * \return A newly allocated (huge) ParaMEDMEM::DataArrayDouble instance that the caller should deal with.
3957  *
3958  * \throw If \a this is not allocated.
3959  *
3960  * \sa DataArrayDouble::buildEuclidianDistanceDenseMatrixWith
3961  */
3962 DataArrayDouble *DataArrayDouble::buildEuclidianDistanceDenseMatrix() const
3963 {
3964   checkAllocated();
3965   int nbOfComp=getNumberOfComponents();
3966   int nbOfTuples=getNumberOfTuples();
3967   const double *inData=getConstPointer();
3968   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
3969   ret->alloc(nbOfTuples*nbOfTuples,1);
3970   double *outData=ret->getPointer();
3971   for(int i=0;i<nbOfTuples;i++)
3972     {
3973       outData[i*nbOfTuples+i]=0.;
3974       for(int j=i+1;j<nbOfTuples;j++)
3975         {
3976           double dist=0.;
3977           for(int k=0;k<nbOfComp;k++)
3978             { double delta=inData[i*nbOfComp+k]-inData[j*nbOfComp+k]; dist+=delta*delta; }
3979           dist=sqrt(dist);
3980           outData[i*nbOfTuples+j]=dist;
3981           outData[j*nbOfTuples+i]=dist;
3982         }
3983     }
3984   return ret.retn();
3985 }
3986
3987 /*!
3988  * This method returns a newly allocated DataArrayDouble instance having one component and \c this->getNumberOfTuples() * \c other->getNumberOfTuples() tuples.
3989  * \n This returned array contains the euclidian distance for each tuple in \a other with each tuple in \a this. 
3990  * \n So the returned array can be seen as a dense rectangular matrix with \c other->getNumberOfTuples() rows and \c this->getNumberOfTuples() columns.
3991  * \n Output rectangular matrix is sorted along rows.
3992  * \n The returned array has only one component (and **not** \c this->getNumberOfTuples() components to avoid the useless memory consumption due to components info in returned DataArrayDouble)
3993  *
3994  * \warning use this method with care because it can leads to big amount of consumed memory !
3995  * 
3996  * \param [in] other DataArrayDouble instance having same number of components than \a this.
3997  * \return A newly allocated (huge) ParaMEDMEM::DataArrayDouble instance that the caller should deal with.
3998  *
3999  * \throw If \a this is not allocated, or if \a other is null or if \a other is not allocated, or if number of components of \a other and \a this differs.
4000  *
4001  * \sa DataArrayDouble::buildEuclidianDistanceDenseMatrix
4002  */
4003 DataArrayDouble *DataArrayDouble::buildEuclidianDistanceDenseMatrixWith(const DataArrayDouble *other) const
4004 {
4005   if(!other)
4006     throw INTERP_KERNEL::Exception("DataArrayDouble::buildEuclidianDistanceDenseMatrixWith : input parameter is null !");
4007   checkAllocated();
4008   other->checkAllocated();
4009   int nbOfComp=getNumberOfComponents();
4010   int otherNbOfComp=other->getNumberOfComponents();
4011   if(nbOfComp!=otherNbOfComp)
4012     {
4013       std::ostringstream oss; oss << "DataArrayDouble::buildEuclidianDistanceDenseMatrixWith : this nb of compo=" << nbOfComp << " and other nb of compo=" << otherNbOfComp << ". It should match !";
4014       throw INTERP_KERNEL::Exception(oss.str().c_str());
4015     }
4016   int nbOfTuples=getNumberOfTuples();
4017   int otherNbOfTuples=other->getNumberOfTuples();
4018   const double *inData=getConstPointer();
4019   const double *inDataOther=other->getConstPointer();
4020   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
4021   ret->alloc(otherNbOfTuples*nbOfTuples,1);
4022   double *outData=ret->getPointer();
4023   for(int i=0;i<otherNbOfTuples;i++,inDataOther+=nbOfComp)
4024     {
4025       for(int j=0;j<nbOfTuples;j++)
4026         {
4027           double dist=0.;
4028           for(int k=0;k<nbOfComp;k++)
4029             { double delta=inDataOther[k]-inData[j*nbOfComp+k]; dist+=delta*delta; }
4030           dist=sqrt(dist);
4031           outData[i*nbOfTuples+j]=dist;
4032         }
4033     }
4034   return ret.retn();
4035 }
4036
4037 /*!
4038  * Sorts value within every tuple of \a this array.
4039  *  \param [in] asc - if \a true, the values are sorted in ascending order, else,
4040  *              in descending order.
4041  *  \throw If \a this is not allocated.
4042  */
4043 void DataArrayDouble::sortPerTuple(bool asc)
4044 {
4045   checkAllocated();
4046   double *pt=getPointer();
4047   int nbOfTuple=getNumberOfTuples();
4048   int nbOfComp=getNumberOfComponents();
4049   if(asc)
4050     for(int i=0;i<nbOfTuple;i++,pt+=nbOfComp)
4051       std::sort(pt,pt+nbOfComp);
4052   else
4053     for(int i=0;i<nbOfTuple;i++,pt+=nbOfComp)
4054       std::sort(pt,pt+nbOfComp,std::greater<double>());
4055   declareAsNew();
4056 }
4057
4058 /*!
4059  * Converts every value of \a this array to its absolute value.
4060  * \b WARNING this method is non const. If a new DataArrayDouble instance should be built containing the result of abs DataArrayDouble::computeAbs
4061  * should be called instead.
4062  *
4063  * \throw If \a this is not allocated.
4064  * \sa DataArrayDouble::computeAbs
4065  */
4066 void DataArrayDouble::abs()
4067 {
4068   checkAllocated();
4069   double *ptr(getPointer());
4070   std::size_t nbOfElems(getNbOfElems());
4071   std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun<double,double>(fabs));
4072   declareAsNew();
4073 }
4074
4075 /*!
4076  * This method builds a new instance of \a this object containing the result of std::abs applied of all elements in \a this.
4077  * This method is a const method (that do not change any values in \a this) contrary to  DataArrayDouble::abs method.
4078  *
4079  * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4080  *         same number of tuples and component as \a this array.
4081  *         The caller is to delete this result array using decrRef() as it is no more
4082  *         needed.
4083  * \throw If \a this is not allocated.
4084  * \sa DataArrayDouble::abs
4085  */
4086 DataArrayDouble *DataArrayDouble::computeAbs() const
4087 {
4088   checkAllocated();
4089   DataArrayDouble *newArr(DataArrayDouble::New());
4090   int nbOfTuples(getNumberOfTuples());
4091   int nbOfComp(getNumberOfComponents());
4092   newArr->alloc(nbOfTuples,nbOfComp);
4093   std::transform(begin(),end(),newArr->getPointer(),std::ptr_fun<double,double>(fabs));
4094   newArr->copyStringInfoFrom(*this);
4095   return newArr;
4096 }
4097
4098 /*!
4099  * Apply a liner function to a given component of \a this array, so that
4100  * an array element <em>(x)</em> becomes \f$ a * x + b \f$.
4101  *  \param [in] a - the first coefficient of the function.
4102  *  \param [in] b - the second coefficient of the function.
4103  *  \param [in] compoId - the index of component to modify.
4104  *  \throw If \a this is not allocated.
4105  */
4106 void DataArrayDouble::applyLin(double a, double b, int compoId)
4107 {
4108   checkAllocated();
4109   double *ptr=getPointer()+compoId;
4110   int nbOfComp=getNumberOfComponents();
4111   int nbOfTuple=getNumberOfTuples();
4112   for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
4113     *ptr=a*(*ptr)+b;
4114   declareAsNew();
4115 }
4116
4117 /*!
4118  * Apply a liner function to all elements of \a this array, so that
4119  * an element _x_ becomes \f$ a * x + b \f$.
4120  *  \param [in] a - the first coefficient of the function.
4121  *  \param [in] b - the second coefficient of the function.
4122  *  \throw If \a this is not allocated.
4123  */
4124 void DataArrayDouble::applyLin(double a, double b)
4125 {
4126   checkAllocated();
4127   double *ptr=getPointer();
4128   std::size_t nbOfElems=getNbOfElems();
4129   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4130     *ptr=a*(*ptr)+b;
4131   declareAsNew();
4132 }
4133
4134 /*!
4135  * Modify all elements of \a this array, so that
4136  * an element _x_ becomes \f$ numerator / x \f$.
4137  *  \warning If an exception is thrown because of presence of 0.0 element in \a this 
4138  *           array, all elements processed before detection of the zero element remain
4139  *           modified.
4140  *  \param [in] numerator - the numerator used to modify array elements.
4141  *  \throw If \a this is not allocated.
4142  *  \throw If there is an element equal to 0.0 in \a this array.
4143  */
4144 void DataArrayDouble::applyInv(double numerator)
4145 {
4146   checkAllocated();
4147   double *ptr=getPointer();
4148   std::size_t nbOfElems=getNbOfElems();
4149   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4150     {
4151       if(std::abs(*ptr)>std::numeric_limits<double>::min())
4152         {
4153           *ptr=numerator/(*ptr);
4154         }
4155       else
4156         {
4157           std::ostringstream oss; oss << "DataArrayDouble::applyInv : presence of null value in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
4158           oss << " !";
4159           throw INTERP_KERNEL::Exception(oss.str().c_str());
4160         }
4161     }
4162   declareAsNew();
4163 }
4164
4165 /*!
4166  * Returns a full copy of \a this array except that sign of all elements is reversed.
4167  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4168  *          same number of tuples and component as \a this array.
4169  *          The caller is to delete this result array using decrRef() as it is no more
4170  *          needed.
4171  *  \throw If \a this is not allocated.
4172  */
4173 DataArrayDouble *DataArrayDouble::negate() const
4174 {
4175   checkAllocated();
4176   DataArrayDouble *newArr=DataArrayDouble::New();
4177   int nbOfTuples=getNumberOfTuples();
4178   int nbOfComp=getNumberOfComponents();
4179   newArr->alloc(nbOfTuples,nbOfComp);
4180   const double *cptr=getConstPointer();
4181   std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate<double>());
4182   newArr->copyStringInfoFrom(*this);
4183   return newArr;
4184 }
4185
4186 /*!
4187  * Modify all elements of \a this array, so that
4188  * an element _x_ becomes <em> val ^ x </em>. Contrary to DataArrayInt::applyPow
4189  * all values in \a this have to be >= 0 if val is \b not integer.
4190  *  \param [in] val - the value used to apply pow on all array elements.
4191  *  \throw If \a this is not allocated.
4192  *  \warning If an exception is thrown because of presence of 0 element in \a this 
4193  *           array and \a val is \b not integer, all elements processed before detection of the zero element remain
4194  *           modified.
4195  */
4196 void DataArrayDouble::applyPow(double val)
4197 {
4198   checkAllocated();
4199   double *ptr=getPointer();
4200   std::size_t nbOfElems=getNbOfElems();
4201   int val2=(int)val;
4202   bool isInt=((double)val2)==val;
4203   if(!isInt)
4204     {
4205       for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4206         {
4207           if(*ptr>=0)
4208             *ptr=pow(*ptr,val);
4209           else
4210             {
4211               std::ostringstream oss; oss << "DataArrayDouble::applyPow (double) : At elem # " << i << " value is " << *ptr << " ! must be >=0. !";
4212               throw INTERP_KERNEL::Exception(oss.str().c_str());
4213             }
4214         }
4215     }
4216   else
4217     {
4218       for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4219         *ptr=pow(*ptr,val2);
4220     }
4221   declareAsNew();
4222 }
4223
4224 /*!
4225  * Modify all elements of \a this array, so that
4226  * an element _x_ becomes \f$ val ^ x \f$.
4227  *  \param [in] val - the value used to apply pow on all array elements.
4228  *  \throw If \a this is not allocated.
4229  *  \throw If \a val < 0.
4230  *  \warning If an exception is thrown because of presence of 0 element in \a this 
4231  *           array, all elements processed before detection of the zero element remain
4232  *           modified.
4233  */
4234 void DataArrayDouble::applyRPow(double val)
4235 {
4236   checkAllocated();
4237   if(val<0.)
4238     throw INTERP_KERNEL::Exception("DataArrayDouble::applyRPow : the input value has to be >= 0 !");
4239   double *ptr=getPointer();
4240   std::size_t nbOfElems=getNbOfElems();
4241   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4242     *ptr=pow(val,*ptr);
4243   declareAsNew();
4244 }
4245
4246 /*!
4247  * Returns a new DataArrayDouble created from \a this one by applying \a
4248  * FunctionToEvaluate to every tuple of \a this array. Textual data is not copied.
4249  * For more info see \ref MEDCouplingArrayApplyFunc
4250  *  \param [in] nbOfComp - number of components in the result array.
4251  *  \param [in] func - the \a FunctionToEvaluate declared as 
4252  *              \c bool (*\a func)(\c const \c double *\a pos, \c double *\a res), 
4253  *              where \a pos points to the first component of a tuple of \a this array
4254  *              and \a res points to the first component of a tuple of the result array.
4255  *              Note that length (number of components) of \a pos can differ from
4256  *              that of \a res.
4257  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4258  *          same number of tuples as \a this array.
4259  *          The caller is to delete this result array using decrRef() as it is no more
4260  *          needed.
4261  *  \throw If \a this is not allocated.
4262  *  \throw If \a func returns \a false.
4263  */
4264 DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, FunctionToEvaluate func) const
4265 {
4266   checkAllocated();
4267   DataArrayDouble *newArr=DataArrayDouble::New();
4268   int nbOfTuples=getNumberOfTuples();
4269   int oldNbOfComp=getNumberOfComponents();
4270   newArr->alloc(nbOfTuples,nbOfComp);
4271   const double *ptr=getConstPointer();
4272   double *ptrToFill=newArr->getPointer();
4273   for(int i=0;i<nbOfTuples;i++)
4274     {
4275       if(!func(ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp))
4276         {
4277           std::ostringstream oss; oss << "For tuple # " << i << " with value (";
4278           std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
4279           oss << ") : Evaluation of function failed !";
4280           newArr->decrRef();
4281           throw INTERP_KERNEL::Exception(oss.str().c_str());
4282         }
4283     }
4284   return newArr;
4285 }
4286
4287 /*!
4288  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4289  * tuple of \a this array. Textual data is not copied.
4290  * For more info see \ref MEDCouplingArrayApplyFunc1.
4291  *  \param [in] nbOfComp - number of components in the result array.
4292  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4293  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4294  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4295  *          same number of tuples as \a this array and \a nbOfComp components.
4296  *          The caller is to delete this result array using decrRef() as it is no more
4297  *          needed.
4298  *  \throw If \a this is not allocated.
4299  *  \throw If computing \a func fails.
4300  */
4301 DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, const std::string& func) const
4302 {
4303   checkAllocated();
4304   INTERP_KERNEL::ExprParser expr(func);
4305   expr.parse();
4306   std::set<std::string> vars;
4307   expr.getTrueSetOfVars(vars);
4308   int oldNbOfComp=getNumberOfComponents();
4309   if((int)vars.size()>oldNbOfComp)
4310     {
4311       std::ostringstream oss; oss << "The field has " << oldNbOfComp << " components and there are ";
4312       oss << vars.size() << " variables : ";
4313       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
4314       throw INTERP_KERNEL::Exception(oss.str().c_str());
4315     }
4316   std::vector<std::string> varsV(vars.begin(),vars.end());
4317   expr.prepareExprEvaluation(varsV,oldNbOfComp,nbOfComp);
4318   //
4319   DataArrayDouble *newArr=DataArrayDouble::New();
4320   int nbOfTuples=getNumberOfTuples();
4321   newArr->alloc(nbOfTuples,nbOfComp);
4322   const double *ptr=getConstPointer();
4323   double *ptrToFill=newArr->getPointer();
4324   for(int i=0;i<nbOfTuples;i++)
4325     {
4326       try
4327         {
4328           expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
4329         }
4330       catch(INTERP_KERNEL::Exception& e)
4331         {
4332           std::ostringstream oss; oss << "For tuple # " << i << " with value (";
4333           std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
4334           oss << ") : Evaluation of function failed !" << e.what();
4335           newArr->decrRef();
4336           throw INTERP_KERNEL::Exception(oss.str().c_str());
4337         }
4338     }
4339   return newArr;
4340 }
4341
4342 /*!
4343  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4344  * tuple of \a this array. Textual data is not copied.
4345  * For more info see \ref MEDCouplingArrayApplyFunc0.
4346  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4347  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4348  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4349  *          same number of tuples and components as \a this array.
4350  *          The caller is to delete this result array using decrRef() as it is no more
4351  *          needed.
4352  *  \throw If \a this is not allocated.
4353  *  \throw If computing \a func fails.
4354  */
4355 DataArrayDouble *DataArrayDouble::applyFunc(const std::string& func) const
4356 {
4357   checkAllocated();
4358   INTERP_KERNEL::ExprParser expr(func);
4359   expr.parse();
4360   expr.prepareExprEvaluationVec();
4361   //
4362   DataArrayDouble *newArr=DataArrayDouble::New();
4363   int nbOfTuples=getNumberOfTuples();
4364   int nbOfComp=getNumberOfComponents();
4365   newArr->alloc(nbOfTuples,nbOfComp);
4366   const double *ptr=getConstPointer();
4367   double *ptrToFill=newArr->getPointer();
4368   for(int i=0;i<nbOfTuples;i++)
4369     {
4370       try
4371         {
4372           expr.evaluateExpr(nbOfComp,ptr+i*nbOfComp,ptrToFill+i*nbOfComp);
4373         }
4374       catch(INTERP_KERNEL::Exception& e)
4375         {
4376           std::ostringstream oss; oss << "For tuple # " << i << " with value (";
4377           std::copy(ptr+nbOfComp*i,ptr+nbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
4378           oss << ") : Evaluation of function failed ! " << e.what();
4379           newArr->decrRef();
4380           throw INTERP_KERNEL::Exception(oss.str().c_str());
4381         }
4382     }
4383   return newArr;
4384 }
4385
4386 /*!
4387  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4388  * tuple of \a this array. Textual data is not copied.
4389  * For more info see \ref MEDCouplingArrayApplyFunc2.
4390  *  \param [in] nbOfComp - number of components in the result array.
4391  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4392  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4393  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4394  *          same number of tuples as \a this array.
4395  *          The caller is to delete this result array using decrRef() as it is no more
4396  *          needed.
4397  *  \throw If \a this is not allocated.
4398  *  \throw If \a func contains vars that are not in \a this->getInfoOnComponent().
4399  *  \throw If computing \a func fails.
4400  */
4401 DataArrayDouble *DataArrayDouble::applyFunc2(int nbOfComp, const std::string& func) const
4402 {
4403   checkAllocated();
4404   INTERP_KERNEL::ExprParser expr(func);
4405   expr.parse();
4406   std::set<std::string> vars;
4407   expr.getTrueSetOfVars(vars);
4408   int oldNbOfComp=getNumberOfComponents();
4409   if((int)vars.size()>oldNbOfComp)
4410     {
4411       std::ostringstream oss; oss << "The field has " << oldNbOfComp << " components and there are ";
4412       oss << vars.size() << " variables : ";
4413       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
4414       throw INTERP_KERNEL::Exception(oss.str().c_str());
4415     }
4416   expr.prepareExprEvaluation(getVarsOnComponent(),oldNbOfComp,nbOfComp);
4417   //
4418   DataArrayDouble *newArr=DataArrayDouble::New();
4419   int nbOfTuples=getNumberOfTuples();
4420   newArr->alloc(nbOfTuples,nbOfComp);
4421   const double *ptr=getConstPointer();
4422   double *ptrToFill=newArr->getPointer();
4423   for(int i=0;i<nbOfTuples;i++)
4424     {
4425       try
4426         {
4427           expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
4428         }
4429       catch(INTERP_KERNEL::Exception& e)
4430         {
4431           std::ostringstream oss; oss << "For tuple # " << i << " with value (";
4432           std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
4433           oss << ") : Evaluation of function failed !" << e.what();
4434           newArr->decrRef();
4435           throw INTERP_KERNEL::Exception(oss.str().c_str());
4436         }
4437     }
4438   return newArr;
4439 }
4440
4441 /*!
4442  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4443  * tuple of \a this array. Textual data is not copied.
4444  * For more info see \ref MEDCouplingArrayApplyFunc3.
4445  *  \param [in] nbOfComp - number of components in the result array.
4446  *  \param [in] varsOrder - sequence of vars defining their order.
4447  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4448  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4449  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4450  *          same number of tuples as \a this array.
4451  *          The caller is to delete this result array using decrRef() as it is no more
4452  *          needed.
4453  *  \throw If \a this is not allocated.
4454  *  \throw If \a func contains vars not in \a varsOrder.
4455  *  \throw If computing \a func fails.
4456  */
4457 DataArrayDouble *DataArrayDouble::applyFunc3(int nbOfComp, const std::vector<std::string>& varsOrder, const std::string& func) const
4458 {
4459   checkAllocated();
4460   INTERP_KERNEL::ExprParser expr(func);
4461   expr.parse();
4462   std::set<std::string> vars;
4463   expr.getTrueSetOfVars(vars);
4464   int oldNbOfComp=getNumberOfComponents();
4465   if((int)vars.size()>oldNbOfComp)
4466     {
4467       std::ostringstream oss; oss << "The field has " << oldNbOfComp << " components and there are ";
4468       oss << vars.size() << " variables : ";
4469       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
4470       throw INTERP_KERNEL::Exception(oss.str().c_str());
4471     }
4472   expr.prepareExprEvaluation(varsOrder,oldNbOfComp,nbOfComp);
4473   //
4474   DataArrayDouble *newArr=DataArrayDouble::New();
4475   int nbOfTuples=getNumberOfTuples();
4476   newArr->alloc(nbOfTuples,nbOfComp);
4477   const double *ptr=getConstPointer();
4478   double *ptrToFill=newArr->getPointer();
4479   for(int i=0;i<nbOfTuples;i++)
4480     {
4481       try
4482         {
4483           expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
4484         }
4485       catch(INTERP_KERNEL::Exception& e)
4486         {
4487           std::ostringstream oss; oss << "For tuple # " << i << " with value (";
4488           std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
4489           oss << ") : Evaluation of function failed !" << e.what();
4490           newArr->decrRef();
4491           throw INTERP_KERNEL::Exception(oss.str().c_str());
4492         }
4493     }
4494   return newArr;
4495 }
4496
4497 void DataArrayDouble::applyFuncFast32(const std::string& func)
4498 {
4499   checkAllocated();
4500   INTERP_KERNEL::ExprParser expr(func);
4501   expr.parse();
4502   char *funcStr=expr.compileX86();
4503   MYFUNCPTR funcPtr;
4504   *((void **)&funcPtr)=funcStr;//he he...
4505   //
4506   double *ptr=getPointer();
4507   int nbOfComp=getNumberOfComponents();
4508   int nbOfTuples=getNumberOfTuples();
4509   int nbOfElems=nbOfTuples*nbOfComp;
4510   for(int i=0;i<nbOfElems;i++,ptr++)
4511     *ptr=funcPtr(*ptr);
4512   declareAsNew();
4513 }
4514
4515 void DataArrayDouble::applyFuncFast64(const std::string& func)
4516 {
4517   checkAllocated();
4518   INTERP_KERNEL::ExprParser expr(func);
4519   expr.parse();
4520   char *funcStr=expr.compileX86_64();
4521   MYFUNCPTR funcPtr;
4522   *((void **)&funcPtr)=funcStr;//he he...
4523   //
4524   double *ptr=getPointer();
4525   int nbOfComp=getNumberOfComponents();
4526   int nbOfTuples=getNumberOfTuples();
4527   int nbOfElems=nbOfTuples*nbOfComp;
4528   for(int i=0;i<nbOfElems;i++,ptr++)
4529     *ptr=funcPtr(*ptr);
4530   declareAsNew();
4531 }
4532
4533 DataArrayDoubleIterator *DataArrayDouble::iterator()
4534 {
4535   return new DataArrayDoubleIterator(this);
4536 }
4537
4538 /*!
4539  * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
4540  * array whose values are within a given range. Textual data is not copied.
4541  *  \param [in] vmin - a lowest acceptable value (included).
4542  *  \param [in] vmax - a greatest acceptable value (included).
4543  *  \return DataArrayInt * - the new instance of DataArrayInt.
4544  *          The caller is to delete this result array using decrRef() as it is no more
4545  *          needed.
4546  *  \throw If \a this->getNumberOfComponents() != 1.
4547  *
4548  *  \sa DataArrayDouble::getIdsNotInRange
4549  *
4550  *  \if ENABLE_EXAMPLES
4551  *  \ref cpp_mcdataarraydouble_getidsinrange "Here is a C++ example".<br>
4552  *  \ref py_mcdataarraydouble_getidsinrange "Here is a Python example".
4553  *  \endif
4554  */
4555 DataArrayInt *DataArrayDouble::getIdsInRange(double vmin, double vmax) const
4556 {
4557   checkAllocated();
4558   if(getNumberOfComponents()!=1)
4559     throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsInRange : this must have exactly one component !");
4560   const double *cptr(begin());
4561   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
4562   int nbOfTuples(getNumberOfTuples());
4563   for(int i=0;i<nbOfTuples;i++,cptr++)
4564     if(*cptr>=vmin && *cptr<=vmax)
4565       ret->pushBackSilent(i);
4566   return ret.retn();
4567 }
4568
4569 /*!
4570  * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
4571  * array whose values are not within a given range. Textual data is not copied.
4572  *  \param [in] vmin - a lowest not acceptable value (excluded).
4573  *  \param [in] vmax - a greatest not acceptable value (excluded).
4574  *  \return DataArrayInt * - the new instance of DataArrayInt.
4575  *          The caller is to delete this result array using decrRef() as it is no more
4576  *          needed.
4577  *  \throw If \a this->getNumberOfComponents() != 1.
4578  *
4579  *  \sa DataArrayDouble::getIdsInRange
4580  */
4581 DataArrayInt *DataArrayDouble::getIdsNotInRange(double vmin, double vmax) const
4582 {
4583   checkAllocated();
4584   if(getNumberOfComponents()!=1)
4585     throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsNotInRange : this must have exactly one component !");
4586   const double *cptr(begin());
4587   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
4588   int nbOfTuples(getNumberOfTuples());
4589   for(int i=0;i<nbOfTuples;i++,cptr++)
4590     if(*cptr<vmin || *cptr>vmax)
4591       ret->pushBackSilent(i);
4592   return ret.retn();
4593 }
4594
4595 /*!
4596  * Returns a new DataArrayDouble by concatenating two given arrays, so that (1) the number
4597  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
4598  * the number of component in the result array is same as that of each of given arrays.
4599  * Info on components is copied from the first of the given arrays. Number of components
4600  * in the given arrays must be  the same.
4601  *  \param [in] a1 - an array to include in the result array.
4602  *  \param [in] a2 - another array to include in the result array.
4603  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4604  *          The caller is to delete this result array using decrRef() as it is no more
4605  *          needed.
4606  *  \throw If both \a a1 and \a a2 are NULL.
4607  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents().
4608  */
4609 DataArrayDouble *DataArrayDouble::Aggregate(const DataArrayDouble *a1, const DataArrayDouble *a2)
4610 {
4611   std::vector<const DataArrayDouble *> tmp(2);
4612   tmp[0]=a1; tmp[1]=a2;
4613   return Aggregate(tmp);
4614 }
4615
4616 /*!
4617  * Returns a new DataArrayDouble by concatenating all given arrays, so that (1) the number
4618  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
4619  * the number of component in the result array is same as that of each of given arrays.
4620  * Info on components is copied from the first of the given arrays. Number of components
4621  * in the given arrays must be  the same.
4622  * If the number of non null of elements in \a arr is equal to one the returned object is a copy of it
4623  * not the object itself.
4624  *  \param [in] arr - a sequence of arrays to include in the result array.
4625  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4626  *          The caller is to delete this result array using decrRef() as it is no more
4627  *          needed.
4628  *  \throw If all arrays within \a arr are NULL.
4629  *  \throw If getNumberOfComponents() of arrays within \a arr.
4630  */
4631 DataArrayDouble *DataArrayDouble::Aggregate(const std::vector<const DataArrayDouble *>& arr)
4632 {
4633   std::vector<const DataArrayDouble *> a;
4634   for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
4635     if(*it4)
4636       a.push_back(*it4);
4637   if(a.empty())
4638     throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : input list must contain at least one NON EMPTY DataArrayDouble !");
4639   std::vector<const DataArrayDouble *>::const_iterator it=a.begin();
4640   int nbOfComp=(*it)->getNumberOfComponents();
4641   int nbt=(*it++)->getNumberOfTuples();
4642   for(int i=1;it!=a.end();it++,i++)
4643     {
4644       if((*it)->getNumberOfComponents()!=nbOfComp)
4645         throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : Nb of components mismatch for array aggregation !");
4646       nbt+=(*it)->getNumberOfTuples();
4647     }
4648   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
4649   ret->alloc(nbt,nbOfComp);
4650   double *pt=ret->getPointer();
4651   for(it=a.begin();it!=a.end();it++)
4652     pt=std::copy((*it)->getConstPointer(),(*it)->getConstPointer()+(*it)->getNbOfElems(),pt);
4653   ret->copyStringInfoFrom(*(a[0]));
4654   return ret.retn();
4655 }
4656
4657 /*!
4658  * Returns a new DataArrayDouble by aggregating two given arrays, so that (1) the number
4659  * of components in the result array is a sum of the number of components of given arrays
4660  * and (2) the number of tuples in the result array is same as that of each of given
4661  * arrays. In other words the i-th tuple of result array includes all components of
4662  * i-th tuples of all given arrays.
4663  * Number of tuples in the given arrays must be  the same.
4664  *  \param [in] a1 - an array to include in the result array.
4665  *  \param [in] a2 - another array to include in the result array.
4666  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4667  *          The caller is to delete this result array using decrRef() as it is no more
4668  *          needed.
4669  *  \throw If both \a a1 and \a a2 are NULL.
4670  *  \throw If any given array is not allocated.
4671  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4672  */
4673 DataArrayDouble *DataArrayDouble::Meld(const DataArrayDouble *a1, const DataArrayDouble *a2)
4674 {
4675   std::vector<const DataArrayDouble *> arr(2);
4676   arr[0]=a1; arr[1]=a2;
4677   return Meld(arr);
4678 }
4679
4680 /*!
4681  * Returns a new DataArrayDouble by aggregating all given arrays, so that (1) the number
4682  * of components in the result array is a sum of the number of components of given arrays
4683  * and (2) the number of tuples in the result array is same as that of each of given
4684  * arrays. In other words the i-th tuple of result array includes all components of
4685  * i-th tuples of all given arrays.
4686  * Number of tuples in the given arrays must be  the same.
4687  *  \param [in] arr - a sequence of arrays to include in the result array.
4688  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4689  *          The caller is to delete this result array using decrRef() as it is no more
4690  *          needed.
4691  *  \throw If all arrays within \a arr are NULL.
4692  *  \throw If any given array is not allocated.
4693  *  \throw If getNumberOfTuples() of arrays within \a arr is different.
4694  */
4695 DataArrayDouble *DataArrayDouble::Meld(const std::vector<const DataArrayDouble *>& arr)
4696 {
4697   std::vector<const DataArrayDouble *> a;
4698   for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
4699     if(*it4)
4700       a.push_back(*it4);
4701   if(a.empty())
4702     throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : input list must contain at least one NON EMPTY DataArrayDouble !");
4703   std::vector<const DataArrayDouble *>::const_iterator it;
4704   for(it=a.begin();it!=a.end();it++)
4705     (*it)->checkAllocated();
4706   it=a.begin();
4707   int nbOfTuples=(*it)->getNumberOfTuples();
4708   std::vector<int> nbc(a.size());
4709   std::vector<const double *> pts(a.size());
4710   nbc[0]=(*it)->getNumberOfComponents();
4711   pts[0]=(*it++)->getConstPointer();
4712   for(int i=1;it!=a.end();it++,i++)
4713     {
4714       if(nbOfTuples!=(*it)->getNumberOfTuples())
4715         throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : mismatch of number of tuples !");
4716       nbc[i]=(*it)->getNumberOfComponents();
4717       pts[i]=(*it)->getConstPointer();
4718     }
4719   int totalNbOfComp=std::accumulate(nbc.begin(),nbc.end(),0);
4720   DataArrayDouble *ret=DataArrayDouble::New();
4721   ret->alloc(nbOfTuples,totalNbOfComp);
4722   double *retPtr=ret->getPointer();
4723   for(int i=0;i<nbOfTuples;i++)
4724     for(int j=0;j<(int)a.size();j++)
4725       {
4726         retPtr=std::copy(pts[j],pts[j]+nbc[j],retPtr);
4727         pts[j]+=nbc[j];
4728       }
4729   int k=0;
4730   for(int i=0;i<(int)a.size();i++)
4731     for(int j=0;j<nbc[i];j++,k++)
4732       ret->setInfoOnComponent(k,a[i]->getInfoOnComponent(j));
4733   return ret;
4734 }
4735
4736 /*!
4737  * Returns a new DataArrayDouble containing a dot product of two given arrays, so that
4738  * the i-th tuple of the result array is a sum of products of j-th components of i-th
4739  * tuples of given arrays (\f$ a_i = \sum_{j=1}^n a1_j * a2_j \f$).
4740  * Info on components and name is copied from the first of the given arrays.
4741  * Number of tuples and components in the given arrays must be the same.
4742  *  \param [in] a1 - a given array.
4743  *  \param [in] a2 - another given array.
4744  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4745  *          The caller is to delete this result array using decrRef() as it is no more
4746  *          needed.
4747  *  \throw If either \a a1 or \a a2 is NULL.
4748  *  \throw If any given array is not allocated.
4749  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4750  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
4751  */
4752 DataArrayDouble *DataArrayDouble::Dot(const DataArrayDouble *a1, const DataArrayDouble *a2)
4753 {
4754   if(!a1 || !a2)
4755     throw INTERP_KERNEL::Exception("DataArrayDouble::Dot : input DataArrayDouble instance is NULL !");
4756   a1->checkAllocated();
4757   a2->checkAllocated();
4758   int nbOfComp=a1->getNumberOfComponents();
4759   if(nbOfComp!=a2->getNumberOfComponents())
4760     throw INTERP_KERNEL::Exception("Nb of components mismatch for array Dot !");
4761   int nbOfTuple=a1->getNumberOfTuples();
4762   if(nbOfTuple!=a2->getNumberOfTuples())
4763     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Dot !");
4764   DataArrayDouble *ret=DataArrayDouble::New();
4765   ret->alloc(nbOfTuple,1);
4766   double *retPtr=ret->getPointer();
4767   const double *a1Ptr=a1->getConstPointer();
4768   const double *a2Ptr=a2->getConstPointer();
4769   for(int i=0;i<nbOfTuple;i++)
4770     {
4771       double sum=0.;
4772       for(int j=0;j<nbOfComp;j++)
4773         sum+=a1Ptr[i*nbOfComp+j]*a2Ptr[i*nbOfComp+j];
4774       retPtr[i]=sum;
4775     }
4776   ret->setInfoOnComponent(0,a1->getInfoOnComponent(0));
4777   ret->setName(a1->getName());
4778   return ret;
4779 }
4780
4781 /*!
4782  * Returns a new DataArrayDouble containing a cross product of two given arrays, so that
4783  * the i-th tuple of the result array contains 3 components of a vector which is a cross
4784  * product of two vectors defined by the i-th tuples of given arrays.
4785  * Info on components is copied from the first of the given arrays.
4786  * Number of tuples in the given arrays must be the same.
4787  * Number of components in the given arrays must be 3.
4788  *  \param [in] a1 - a given array.
4789  *  \param [in] a2 - another given array.
4790  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4791  *          The caller is to delete this result array using decrRef() as it is no more
4792  *          needed.
4793  *  \throw If either \a a1 or \a a2 is NULL.
4794  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4795  *  \throw If \a a1->getNumberOfComponents() != 3
4796  *  \throw If \a a2->getNumberOfComponents() != 3
4797  */
4798 DataArrayDouble *DataArrayDouble::CrossProduct(const DataArrayDouble *a1, const DataArrayDouble *a2)
4799 {
4800   if(!a1 || !a2)
4801     throw INTERP_KERNEL::Exception("DataArrayDouble::CrossProduct : input DataArrayDouble instance is NULL !");
4802   int nbOfComp=a1->getNumberOfComponents();
4803   if(nbOfComp!=a2->getNumberOfComponents())
4804     throw INTERP_KERNEL::Exception("Nb of components mismatch for array crossProduct !");
4805   if(nbOfComp!=3)
4806     throw INTERP_KERNEL::Exception("Nb of components must be equal to 3 for array crossProduct !");
4807   int nbOfTuple=a1->getNumberOfTuples();
4808   if(nbOfTuple!=a2->getNumberOfTuples())
4809     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array crossProduct !");
4810   DataArrayDouble *ret=DataArrayDouble::New();
4811   ret->alloc(nbOfTuple,3);
4812   double *retPtr=ret->getPointer();
4813   const double *a1Ptr=a1->getConstPointer();
4814   const double *a2Ptr=a2->getConstPointer();
4815   for(int i=0;i<nbOfTuple;i++)
4816     {
4817       retPtr[3*i]=a1Ptr[3*i+1]*a2Ptr[3*i+2]-a1Ptr[3*i+2]*a2Ptr[3*i+1];
4818       retPtr[3*i+1]=a1Ptr[3*i+2]*a2Ptr[3*i]-a1Ptr[3*i]*a2Ptr[3*i+2];
4819       retPtr[3*i+2]=a1Ptr[3*i]*a2Ptr[3*i+1]-a1Ptr[3*i+1]*a2Ptr[3*i];
4820     }
4821   ret->copyStringInfoFrom(*a1);
4822   return ret;
4823 }
4824
4825 /*!
4826  * Returns a new DataArrayDouble containing maximal values of two given arrays.
4827  * Info on components is copied from the first of the given arrays.
4828  * Number of tuples and components in the given arrays must be the same.
4829  *  \param [in] a1 - an array to compare values with another one.
4830  *  \param [in] a2 - another array to compare values with the first one.
4831  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4832  *          The caller is to delete this result array using decrRef() as it is no more
4833  *          needed.
4834  *  \throw If either \a a1 or \a a2 is NULL.
4835  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4836  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
4837  */
4838 DataArrayDouble *DataArrayDouble::Max(const DataArrayDouble *a1, const DataArrayDouble *a2)
4839 {
4840   if(!a1 || !a2)
4841     throw INTERP_KERNEL::Exception("DataArrayDouble::Max : input DataArrayDouble instance is NULL !");
4842   int nbOfComp=a1->getNumberOfComponents();
4843   if(nbOfComp!=a2->getNumberOfComponents())
4844     throw INTERP_KERNEL::Exception("Nb of components mismatch for array Max !");
4845   int nbOfTuple=a1->getNumberOfTuples();
4846   if(nbOfTuple!=a2->getNumberOfTuples())
4847     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Max !");
4848   DataArrayDouble *ret=DataArrayDouble::New();
4849   ret->alloc(nbOfTuple,nbOfComp);
4850   double *retPtr=ret->getPointer();
4851   const double *a1Ptr=a1->getConstPointer();
4852   const double *a2Ptr=a2->getConstPointer();
4853   int nbElem=nbOfTuple*nbOfComp;
4854   for(int i=0;i<nbElem;i++)
4855     retPtr[i]=std::max(a1Ptr[i],a2Ptr[i]);
4856   ret->copyStringInfoFrom(*a1);
4857   return ret;
4858 }
4859
4860 /*!
4861  * Returns a new DataArrayDouble containing minimal values of two given arrays.
4862  * Info on components is copied from the first of the given arrays.
4863  * Number of tuples and components in the given arrays must be the same.
4864  *  \param [in] a1 - an array to compare values with another one.
4865  *  \param [in] a2 - another array to compare values with the first one.
4866  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4867  *          The caller is to delete this result array using decrRef() as it is no more
4868  *          needed.
4869  *  \throw If either \a a1 or \a a2 is NULL.
4870  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4871  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
4872  */
4873 DataArrayDouble *DataArrayDouble::Min(const DataArrayDouble *a1, const DataArrayDouble *a2)
4874 {
4875   if(!a1 || !a2)
4876     throw INTERP_KERNEL::Exception("DataArrayDouble::Min : input DataArrayDouble instance is NULL !");
4877   int nbOfComp=a1->getNumberOfComponents();
4878   if(nbOfComp!=a2->getNumberOfComponents())
4879     throw INTERP_KERNEL::Exception("Nb of components mismatch for array min !");
4880   int nbOfTuple=a1->getNumberOfTuples();
4881   if(nbOfTuple!=a2->getNumberOfTuples())
4882     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array min !");
4883   DataArrayDouble *ret=DataArrayDouble::New();
4884   ret->alloc(nbOfTuple,nbOfComp);
4885   double *retPtr=ret->getPointer();
4886   const double *a1Ptr=a1->getConstPointer();
4887   const double *a2Ptr=a2->getConstPointer();
4888   int nbElem=nbOfTuple*nbOfComp;
4889   for(int i=0;i<nbElem;i++)
4890     retPtr[i]=std::min(a1Ptr[i],a2Ptr[i]);
4891   ret->copyStringInfoFrom(*a1);
4892   return ret;
4893 }
4894
4895 /*!
4896  * Returns a new DataArrayDouble that is a sum of two given arrays. There are 3
4897  * valid cases.
4898  * 1.  The arrays have same number of tuples and components. Then each value of
4899  *   the result array (_a_) is a sum of the corresponding values of \a a1 and \a a2,
4900  *   i.e.: _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, j ].
4901  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
4902  *   component. Then
4903  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, 0 ].
4904  * 3.  The arrays have same number of components and one array, say _a2_, has one
4905  *   tuple. Then
4906  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ 0, j ].
4907  *
4908  * Info on components is copied either from the first array (in the first case) or from
4909  * the array with maximal number of elements (getNbOfElems()).
4910  *  \param [in] a1 - an array to sum up.
4911  *  \param [in] a2 - another array to sum up.
4912  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4913  *          The caller is to delete this result array using decrRef() as it is no more
4914  *          needed.
4915  *  \throw If either \a a1 or \a a2 is NULL.
4916  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
4917  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
4918  *         none of them has number of tuples or components equal to 1.
4919  */
4920 DataArrayDouble *DataArrayDouble::Add(const DataArrayDouble *a1, const DataArrayDouble *a2)
4921 {
4922   if(!a1 || !a2)
4923     throw INTERP_KERNEL::Exception("DataArrayDouble::Add : input DataArrayDouble instance is NULL !");
4924   int nbOfTuple=a1->getNumberOfTuples();
4925   int nbOfTuple2=a2->getNumberOfTuples();
4926   int nbOfComp=a1->getNumberOfComponents();
4927   int nbOfComp2=a2->getNumberOfComponents();
4928   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=0;
4929   if(nbOfTuple==nbOfTuple2)
4930     {
4931       if(nbOfComp==nbOfComp2)
4932         {
4933           ret=DataArrayDouble::New();
4934           ret->alloc(nbOfTuple,nbOfComp);
4935           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<double>());
4936           ret->copyStringInfoFrom(*a1);
4937         }
4938       else
4939         {
4940           int nbOfCompMin,nbOfCompMax;
4941           const DataArrayDouble *aMin, *aMax;
4942           if(nbOfComp>nbOfComp2)
4943             {
4944               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
4945               aMin=a2; aMax=a1;
4946             }
4947           else
4948             {
4949               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
4950               aMin=a1; aMax=a2;
4951             }
4952           if(nbOfCompMin==1)
4953             {
4954               ret=DataArrayDouble::New();
4955               ret->alloc(nbOfTuple,nbOfCompMax);
4956               const double *aMinPtr=aMin->getConstPointer();
4957               const double *aMaxPtr=aMax->getConstPointer();
4958               double *res=ret->getPointer();
4959               for(int i=0;i<nbOfTuple;i++)
4960                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::plus<double>(),aMinPtr[i]));
4961               ret->copyStringInfoFrom(*aMax);
4962             }
4963           else
4964             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
4965         }
4966     }
4967   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
4968     {
4969       if(nbOfComp==nbOfComp2)
4970         {
4971           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
4972           const DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1;
4973           const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2;
4974           const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
4975           ret=DataArrayDouble::New();
4976           ret->alloc(nbOfTupleMax,nbOfComp);
4977           double *res=ret->getPointer();
4978           for(int i=0;i<nbOfTupleMax;i++)
4979             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::plus<double>());
4980           ret->copyStringInfoFrom(*aMax);
4981         }
4982       else
4983         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
4984     }
4985   else
4986     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !");
4987   return ret.retn();
4988 }
4989
4990 /*!
4991  * Adds values of another DataArrayDouble to values of \a this one. There are 3
4992  * valid cases.
4993  * 1.  The arrays have same number of tuples and components. Then each value of
4994  *   \a other array is added to the corresponding value of \a this array, i.e.:
4995  *   _a_ [ i, j ] += _other_ [ i, j ].
4996  * 2.  The arrays have same number of tuples and \a other array has one component. Then
4997  *   _a_ [ i, j ] += _other_ [ i, 0 ].
4998  * 3.  The arrays have same number of components and \a other array has one tuple. Then
4999  *   _a_ [ i, j ] += _a2_ [ 0, j ].
5000  *
5001  *  \param [in] other - an array to add to \a this one.
5002  *  \throw If \a other is NULL.
5003  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5004  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5005  *         \a other has number of both tuples and components not equal to 1.
5006  */
5007 void DataArrayDouble::addEqual(const DataArrayDouble *other)
5008 {
5009   if(!other)
5010     throw INTERP_KERNEL::Exception("DataArrayDouble::addEqual : input DataArrayDouble instance is NULL !");
5011   const char *msg="Nb of tuples mismatch for DataArrayDouble::addEqual  !";
5012   checkAllocated();
5013   other->checkAllocated();
5014   int nbOfTuple=getNumberOfTuples();
5015   int nbOfTuple2=other->getNumberOfTuples();
5016   int nbOfComp=getNumberOfComponents();
5017   int nbOfComp2=other->getNumberOfComponents();
5018   if(nbOfTuple==nbOfTuple2)
5019     {
5020       if(nbOfComp==nbOfComp2)
5021         {
5022           std::transform(begin(),end(),other->begin(),getPointer(),std::plus<double>());
5023         }
5024       else if(nbOfComp2==1)
5025         {
5026           double *ptr=getPointer();
5027           const double *ptrc=other->getConstPointer();
5028           for(int i=0;i<nbOfTuple;i++)
5029             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::plus<double>(),*ptrc++));
5030         }
5031       else
5032         throw INTERP_KERNEL::Exception(msg);
5033     }
5034   else if(nbOfTuple2==1)
5035     {
5036       if(nbOfComp2==nbOfComp)
5037         {
5038           double *ptr=getPointer();
5039           const double *ptrc=other->getConstPointer();
5040           for(int i=0;i<nbOfTuple;i++)
5041             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::plus<double>());
5042         }
5043       else
5044         throw INTERP_KERNEL::Exception(msg);
5045     }
5046   else
5047     throw INTERP_KERNEL::Exception(msg);
5048   declareAsNew();
5049 }
5050
5051 /*!
5052  * Returns a new DataArrayDouble that is a subtraction of two given arrays. There are 3
5053  * valid cases.
5054  * 1.  The arrays have same number of tuples and components. Then each value of
5055  *   the result array (_a_) is a subtraction of the corresponding values of \a a1 and
5056  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, j ].
5057  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
5058  *   component. Then
5059  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, 0 ].
5060  * 3.  The arrays have same number of components and one array, say _a2_, has one
5061  *   tuple. Then
5062  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ 0, j ].
5063  *
5064  * Info on components is copied either from the first array (in the first case) or from
5065  * the array with maximal number of elements (getNbOfElems()).
5066  *  \param [in] a1 - an array to subtract from.
5067  *  \param [in] a2 - an array to subtract.
5068  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5069  *          The caller is to delete this result array using decrRef() as it is no more
5070  *          needed.
5071  *  \throw If either \a a1 or \a a2 is NULL.
5072  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
5073  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
5074  *         none of them has number of tuples or components equal to 1.
5075  */
5076 DataArrayDouble *DataArrayDouble::Substract(const DataArrayDouble *a1, const DataArrayDouble *a2)
5077 {
5078   if(!a1 || !a2)
5079     throw INTERP_KERNEL::Exception("DataArrayDouble::Substract : input DataArrayDouble instance is NULL !");
5080   int nbOfTuple1=a1->getNumberOfTuples();
5081   int nbOfTuple2=a2->getNumberOfTuples();
5082   int nbOfComp1=a1->getNumberOfComponents();
5083   int nbOfComp2=a2->getNumberOfComponents();
5084   if(nbOfTuple2==nbOfTuple1)
5085     {
5086       if(nbOfComp1==nbOfComp2)
5087         {
5088           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5089           ret->alloc(nbOfTuple2,nbOfComp1);
5090           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus<double>());
5091           ret->copyStringInfoFrom(*a1);
5092           return ret.retn();
5093         }
5094       else if(nbOfComp2==1)
5095         {
5096           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5097           ret->alloc(nbOfTuple1,nbOfComp1);
5098           const double *a2Ptr=a2->getConstPointer();
5099           const double *a1Ptr=a1->getConstPointer();
5100           double *res=ret->getPointer();
5101           for(int i=0;i<nbOfTuple1;i++)
5102             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::minus<double>(),a2Ptr[i]));
5103           ret->copyStringInfoFrom(*a1);
5104           return ret.retn();
5105         }
5106       else
5107         {
5108           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
5109           return 0;
5110         }
5111     }
5112   else if(nbOfTuple2==1)
5113     {
5114       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
5115       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5116       ret->alloc(nbOfTuple1,nbOfComp1);
5117       const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
5118       double *pt=ret->getPointer();
5119       for(int i=0;i<nbOfTuple1;i++)
5120         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::minus<double>());
5121       ret->copyStringInfoFrom(*a1);
5122       return ret.retn();
5123     }
5124   else
5125     {
5126       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception
5127       return 0;
5128     }
5129 }
5130
5131 /*!
5132  * Subtract values of another DataArrayDouble from values of \a this one. There are 3
5133  * valid cases.
5134  * 1.  The arrays have same number of tuples and components. Then each value of
5135  *   \a other array is subtracted from the corresponding value of \a this array, i.e.:
5136  *   _a_ [ i, j ] -= _other_ [ i, j ].
5137  * 2.  The arrays have same number of tuples and \a other array has one component. Then
5138  *   _a_ [ i, j ] -= _other_ [ i, 0 ].
5139  * 3.  The arrays have same number of components and \a other array has one tuple. Then
5140  *   _a_ [ i, j ] -= _a2_ [ 0, j ].
5141  *
5142  *  \param [in] other - an array to subtract from \a this one.
5143  *  \throw If \a other is NULL.
5144  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5145  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5146  *         \a other has number of both tuples and components not equal to 1.
5147  */
5148 void DataArrayDouble::substractEqual(const DataArrayDouble *other)
5149 {
5150   if(!other)
5151     throw INTERP_KERNEL::Exception("DataArrayDouble::substractEqual : input DataArrayDouble instance is NULL !");
5152   const char *msg="Nb of tuples mismatch for DataArrayDouble::substractEqual  !";
5153   checkAllocated();
5154   other->checkAllocated();
5155   int nbOfTuple=getNumberOfTuples();
5156   int nbOfTuple2=other->getNumberOfTuples();
5157   int nbOfComp=getNumberOfComponents();
5158   int nbOfComp2=other->getNumberOfComponents();
5159   if(nbOfTuple==nbOfTuple2)
5160     {
5161       if(nbOfComp==nbOfComp2)
5162         {
5163           std::transform(begin(),end(),other->begin(),getPointer(),std::minus<double>());
5164         }
5165       else if(nbOfComp2==1)
5166         {
5167           double *ptr=getPointer();
5168           const double *ptrc=other->getConstPointer();
5169           for(int i=0;i<nbOfTuple;i++)
5170             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::minus<double>(),*ptrc++)); 
5171         }
5172       else
5173         throw INTERP_KERNEL::Exception(msg);
5174     }
5175   else if(nbOfTuple2==1)
5176     {
5177       if(nbOfComp2==nbOfComp)
5178         {
5179           double *ptr=getPointer();
5180           const double *ptrc=other->getConstPointer();
5181           for(int i=0;i<nbOfTuple;i++)
5182             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::minus<double>());
5183         }
5184       else
5185         throw INTERP_KERNEL::Exception(msg);
5186     }
5187   else
5188     throw INTERP_KERNEL::Exception(msg);
5189   declareAsNew();
5190 }
5191
5192 /*!
5193  * Returns a new DataArrayDouble that is a product of two given arrays. There are 3
5194  * valid cases.
5195  * 1.  The arrays have same number of tuples and components. Then each value of
5196  *   the result array (_a_) is a product of the corresponding values of \a a1 and
5197  *   \a a2, i.e. _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, j ].
5198  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
5199  *   component. Then
5200  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, 0 ].
5201  * 3.  The arrays have same number of components and one array, say _a2_, has one
5202  *   tuple. Then
5203  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ 0, j ].
5204  *
5205  * Info on components is copied either from the first array (in the first case) or from
5206  * the array with maximal number of elements (getNbOfElems()).
5207  *  \param [in] a1 - a factor array.
5208  *  \param [in] a2 - another factor array.
5209  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5210  *          The caller is to delete this result array using decrRef() as it is no more
5211  *          needed.
5212  *  \throw If either \a a1 or \a a2 is NULL.
5213  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
5214  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
5215  *         none of them has number of tuples or components equal to 1.
5216  */
5217 DataArrayDouble *DataArrayDouble::Multiply(const DataArrayDouble *a1, const DataArrayDouble *a2)
5218 {
5219   if(!a1 || !a2)
5220     throw INTERP_KERNEL::Exception("DataArrayDouble::Multiply : input DataArrayDouble instance is NULL !");
5221   int nbOfTuple=a1->getNumberOfTuples();
5222   int nbOfTuple2=a2->getNumberOfTuples();
5223   int nbOfComp=a1->getNumberOfComponents();
5224   int nbOfComp2=a2->getNumberOfComponents();
5225   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=0;
5226   if(nbOfTuple==nbOfTuple2)
5227     {
5228       if(nbOfComp==nbOfComp2)
5229         {
5230           ret=DataArrayDouble::New();
5231           ret->alloc(nbOfTuple,nbOfComp);
5232           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies<double>());
5233           ret->copyStringInfoFrom(*a1);
5234         }
5235       else
5236         {
5237           int nbOfCompMin,nbOfCompMax;
5238           const DataArrayDouble *aMin, *aMax;
5239           if(nbOfComp>nbOfComp2)
5240             {
5241               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
5242               aMin=a2; aMax=a1;
5243             }
5244           else
5245             {
5246               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
5247               aMin=a1; aMax=a2;
5248             }
5249           if(nbOfCompMin==1)
5250             {
5251               ret=DataArrayDouble::New();
5252               ret->alloc(nbOfTuple,nbOfCompMax);
5253               const double *aMinPtr=aMin->getConstPointer();
5254               const double *aMaxPtr=aMax->getConstPointer();
5255               double *res=ret->getPointer();
5256               for(int i=0;i<nbOfTuple;i++)
5257                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::multiplies<double>(),aMinPtr[i]));
5258               ret->copyStringInfoFrom(*aMax);
5259             }
5260           else
5261             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
5262         }
5263     }
5264   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
5265     {
5266       if(nbOfComp==nbOfComp2)
5267         {
5268           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
5269           const DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1;
5270           const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2;
5271           const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
5272           ret=DataArrayDouble::New();
5273           ret->alloc(nbOfTupleMax,nbOfComp);
5274           double *res=ret->getPointer();
5275           for(int i=0;i<nbOfTupleMax;i++)
5276             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::multiplies<double>());
5277           ret->copyStringInfoFrom(*aMax);
5278         }
5279       else
5280         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
5281     }
5282   else
5283     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !");
5284   return ret.retn();
5285 }
5286
5287 /*!
5288  * Multiply values of another DataArrayDouble to values of \a this one. There are 3
5289  * valid cases.
5290  * 1.  The arrays have same number of tuples and components. Then each value of
5291  *   \a other array is multiplied to the corresponding value of \a this array, i.e.
5292  *   _this_ [ i, j ] *= _other_ [ i, j ].
5293  * 2.  The arrays have same number of tuples and \a other array has one component. Then
5294  *   _this_ [ i, j ] *= _other_ [ i, 0 ].
5295  * 3.  The arrays have same number of components and \a other array has one tuple. Then
5296  *   _this_ [ i, j ] *= _a2_ [ 0, j ].
5297  *
5298  *  \param [in] other - an array to multiply to \a this one.
5299  *  \throw If \a other is NULL.
5300  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5301  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5302  *         \a other has number of both tuples and components not equal to 1.
5303  */
5304 void DataArrayDouble::multiplyEqual(const DataArrayDouble *other)
5305 {
5306   if(!other)
5307     throw INTERP_KERNEL::Exception("DataArrayDouble::multiplyEqual : input DataArrayDouble instance is NULL !");
5308   const char *msg="Nb of tuples mismatch for DataArrayDouble::multiplyEqual !";
5309   checkAllocated();
5310   other->checkAllocated();
5311   int nbOfTuple=getNumberOfTuples();
5312   int nbOfTuple2=other->getNumberOfTuples();
5313   int nbOfComp=getNumberOfComponents();
5314   int nbOfComp2=other->getNumberOfComponents();
5315   if(nbOfTuple==nbOfTuple2)
5316     {
5317       if(nbOfComp==nbOfComp2)
5318         {
5319           std::transform(begin(),end(),other->begin(),getPointer(),std::multiplies<double>());
5320         }
5321       else if(nbOfComp2==1)
5322         {
5323           double *ptr=getPointer();
5324           const double *ptrc=other->getConstPointer();
5325           for(int i=0;i<nbOfTuple;i++)
5326             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::multiplies<double>(),*ptrc++));
5327         }
5328       else
5329         throw INTERP_KERNEL::Exception(msg);
5330     }
5331   else if(nbOfTuple2==1)
5332     {
5333       if(nbOfComp2==nbOfComp)
5334         {
5335           double *ptr=getPointer();
5336           const double *ptrc=other->getConstPointer();
5337           for(int i=0;i<nbOfTuple;i++)
5338             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::multiplies<double>());
5339         }
5340       else
5341         throw INTERP_KERNEL::Exception(msg);
5342     }
5343   else
5344     throw INTERP_KERNEL::Exception(msg);
5345   declareAsNew();
5346 }
5347
5348 /*!
5349  * Returns a new DataArrayDouble that is a division of two given arrays. There are 3
5350  * valid cases.
5351  * 1.  The arrays have same number of tuples and components. Then each value of
5352  *   the result array (_a_) is a division of the corresponding values of \a a1 and
5353  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, j ].
5354  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
5355  *   component. Then
5356  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, 0 ].
5357  * 3.  The arrays have same number of components and one array, say _a2_, has one
5358  *   tuple. Then
5359  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ 0, j ].
5360  *
5361  * Info on components is copied either from the first array (in the first case) or from
5362  * the array with maximal number of elements (getNbOfElems()).
5363  *  \warning No check of division by zero is performed!
5364  *  \param [in] a1 - a numerator array.
5365  *  \param [in] a2 - a denominator array.
5366  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5367  *          The caller is to delete this result array using decrRef() as it is no more
5368  *          needed.
5369  *  \throw If either \a a1 or \a a2 is NULL.
5370  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
5371  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
5372  *         none of them has number of tuples or components equal to 1.
5373  */
5374 DataArrayDouble *DataArrayDouble::Divide(const DataArrayDouble *a1, const DataArrayDouble *a2)
5375 {
5376   if(!a1 || !a2)
5377     throw INTERP_KERNEL::Exception("DataArrayDouble::Divide : input DataArrayDouble instance is NULL !");
5378   int nbOfTuple1=a1->getNumberOfTuples();
5379   int nbOfTuple2=a2->getNumberOfTuples();
5380   int nbOfComp1=a1->getNumberOfComponents();
5381   int nbOfComp2=a2->getNumberOfComponents();
5382   if(nbOfTuple2==nbOfTuple1)
5383     {
5384       if(nbOfComp1==nbOfComp2)
5385         {
5386           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5387           ret->alloc(nbOfTuple2,nbOfComp1);
5388           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides<double>());
5389           ret->copyStringInfoFrom(*a1);
5390           return ret.retn();
5391         }
5392       else if(nbOfComp2==1)
5393         {
5394           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5395           ret->alloc(nbOfTuple1,nbOfComp1);
5396           const double *a2Ptr=a2->getConstPointer();
5397           const double *a1Ptr=a1->getConstPointer();
5398           double *res=ret->getPointer();
5399           for(int i=0;i<nbOfTuple1;i++)
5400             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::divides<double>(),a2Ptr[i]));
5401           ret->copyStringInfoFrom(*a1);
5402           return ret.retn();
5403         }
5404       else
5405         {
5406           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
5407           return 0;
5408         }
5409     }
5410   else if(nbOfTuple2==1)
5411     {
5412       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
5413       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5414       ret->alloc(nbOfTuple1,nbOfComp1);
5415       const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
5416       double *pt=ret->getPointer();
5417       for(int i=0;i<nbOfTuple1;i++)
5418         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::divides<double>());
5419       ret->copyStringInfoFrom(*a1);
5420       return ret.retn();
5421     }
5422   else
5423     {
5424       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception
5425       return 0;
5426     }
5427 }
5428
5429 /*!
5430  * Divide values of \a this array by values of another DataArrayDouble. There are 3
5431  * valid cases.
5432  * 1.  The arrays have same number of tuples and components. Then each value of
5433  *    \a this array is divided by the corresponding value of \a other one, i.e.:
5434  *   _a_ [ i, j ] /= _other_ [ i, j ].
5435  * 2.  The arrays have same number of tuples and \a other array has one component. Then
5436  *   _a_ [ i, j ] /= _other_ [ i, 0 ].
5437  * 3.  The arrays have same number of components and \a other array has one tuple. Then
5438  *   _a_ [ i, j ] /= _a2_ [ 0, j ].
5439  *
5440  *  \warning No check of division by zero is performed!
5441  *  \param [in] other - an array to divide \a this one by.
5442  *  \throw If \a other is NULL.
5443  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5444  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5445  *         \a other has number of both tuples and components not equal to 1.
5446  */
5447 void DataArrayDouble::divideEqual(const DataArrayDouble *other)
5448 {
5449   if(!other)
5450     throw INTERP_KERNEL::Exception("DataArrayDouble::divideEqual : input DataArrayDouble instance is NULL !");
5451   const char *msg="Nb of tuples mismatch for DataArrayDouble::divideEqual !";
5452   checkAllocated();
5453   other->checkAllocated();
5454   int nbOfTuple=getNumberOfTuples();
5455   int nbOfTuple2=other->getNumberOfTuples();
5456   int nbOfComp=getNumberOfComponents();
5457   int nbOfComp2=other->getNumberOfComponents();
5458   if(nbOfTuple==nbOfTuple2)
5459     {
5460       if(nbOfComp==nbOfComp2)
5461         {
5462           std::transform(begin(),end(),other->begin(),getPointer(),std::divides<double>());
5463         }
5464       else if(nbOfComp2==1)
5465         {
5466           double *ptr=getPointer();
5467           const double *ptrc=other->getConstPointer();
5468           for(int i=0;i<nbOfTuple;i++)
5469             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::divides<double>(),*ptrc++));
5470         }
5471       else
5472         throw INTERP_KERNEL::Exception(msg);
5473     }
5474   else if(nbOfTuple2==1)
5475     {
5476       if(nbOfComp2==nbOfComp)
5477         {
5478           double *ptr=getPointer();
5479           const double *ptrc=other->getConstPointer();
5480           for(int i=0;i<nbOfTuple;i++)
5481             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::divides<double>());
5482         }
5483       else
5484         throw INTERP_KERNEL::Exception(msg);
5485     }
5486   else
5487     throw INTERP_KERNEL::Exception(msg);
5488   declareAsNew();
5489 }
5490
5491 /*!
5492  * Returns a new DataArrayDouble that is the result of pow of two given arrays. There are 3
5493  * valid cases.
5494  *
5495  *  \param [in] a1 - an array to pow up.
5496  *  \param [in] a2 - another array to sum up.
5497  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5498  *          The caller is to delete this result array using decrRef() as it is no more
5499  *          needed.
5500  *  \throw If either \a a1 or \a a2 is NULL.
5501  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
5502  *  \throw If \a a1->getNumberOfComponents() != 1 or \a a2->getNumberOfComponents() != 1.
5503  *  \throw If there is a negative value in \a a1.
5504  */
5505 DataArrayDouble *DataArrayDouble::Pow(const DataArrayDouble *a1, const DataArrayDouble *a2)
5506 {
5507   if(!a1 || !a2)
5508     throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : at least one of input instances is null !");
5509   int nbOfTuple=a1->getNumberOfTuples();
5510   int nbOfTuple2=a2->getNumberOfTuples();
5511   int nbOfComp=a1->getNumberOfComponents();
5512   int nbOfComp2=a2->getNumberOfComponents();
5513   if(nbOfTuple!=nbOfTuple2)
5514     throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of tuples mismatches !");
5515   if(nbOfComp!=1 || nbOfComp2!=1)
5516     throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of components of both arrays must be equal to 1 !");
5517   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(nbOfTuple,1);
5518   const double *ptr1(a1->begin()),*ptr2(a2->begin());
5519   double *ptr=ret->getPointer();
5520   for(int i=0;i<nbOfTuple;i++,ptr1++,ptr2++,ptr++)
5521     {
5522       if(*ptr1>=0)
5523         {
5524           *ptr=pow(*ptr1,*ptr2);
5525         }
5526       else
5527         {
5528           std::ostringstream oss; oss << "DataArrayDouble::Pow : on tuple #" << i << " of a1 value is < 0 (" << *ptr1 << ") !";
5529           throw INTERP_KERNEL::Exception(oss.str().c_str());
5530         }
5531     }
5532   return ret.retn();
5533 }
5534
5535 /*!
5536  * Apply pow on values of another DataArrayDouble to values of \a this one.
5537  *
5538  *  \param [in] other - an array to pow to \a this one.
5539  *  \throw If \a other is NULL.
5540  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples()
5541  *  \throw If \a this->getNumberOfComponents() != 1 or \a other->getNumberOfComponents() != 1
5542  *  \throw If there is a negative value in \a this.
5543  */
5544 void DataArrayDouble::powEqual(const DataArrayDouble *other)
5545 {
5546   if(!other)
5547     throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : input instance is null !");
5548   int nbOfTuple=getNumberOfTuples();
5549   int nbOfTuple2=other->getNumberOfTuples();
5550   int nbOfComp=getNumberOfComponents();
5551   int nbOfComp2=other->getNumberOfComponents();
5552   if(nbOfTuple!=nbOfTuple2)
5553     throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of tuples mismatches !");
5554   if(nbOfComp!=1 || nbOfComp2!=1)
5555     throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of components of both arrays must be equal to 1 !");
5556   double *ptr=getPointer();
5557   const double *ptrc=other->begin();
5558   for(int i=0;i<nbOfTuple;i++,ptrc++,ptr++)
5559     {
5560       if(*ptr>=0)
5561         *ptr=pow(*ptr,*ptrc);
5562       else
5563         {
5564           std::ostringstream oss; oss << "DataArrayDouble::powEqual : on tuple #" << i << " of this value is < 0 (" << *ptr << ") !";
5565           throw INTERP_KERNEL::Exception(oss.str().c_str());
5566         }
5567     }
5568   declareAsNew();
5569 }
5570
5571 /*!
5572  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5573  * Server side.
5574  */
5575 void DataArrayDouble::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
5576 {
5577   tinyInfo.resize(2);
5578   if(isAllocated())
5579     {
5580       tinyInfo[0]=getNumberOfTuples();
5581       tinyInfo[1]=getNumberOfComponents();
5582     }
5583   else
5584     {
5585       tinyInfo[0]=-1;
5586       tinyInfo[1]=-1;
5587     }
5588 }
5589
5590 /*!
5591  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5592  * Server side.
5593  */
5594 void DataArrayDouble::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
5595 {
5596   if(isAllocated())
5597     {
5598       int nbOfCompo=getNumberOfComponents();
5599       tinyInfo.resize(nbOfCompo+1);
5600       tinyInfo[0]=getName();
5601       for(int i=0;i<nbOfCompo;i++)
5602         tinyInfo[i+1]=getInfoOnComponent(i);
5603     }
5604   else
5605     {
5606       tinyInfo.resize(1);
5607       tinyInfo[0]=getName();
5608     }
5609 }
5610
5611 /*!
5612  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5613  * This method returns if a feeding is needed.
5614  */
5615 bool DataArrayDouble::resizeForUnserialization(const std::vector<int>& tinyInfoI)
5616 {
5617   int nbOfTuple=tinyInfoI[0];
5618   int nbOfComp=tinyInfoI[1];
5619   if(nbOfTuple!=-1 || nbOfComp!=-1)
5620     {
5621       alloc(nbOfTuple,nbOfComp);
5622       return true;
5623     }
5624   return false;
5625 }
5626
5627 /*!
5628  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5629  */
5630 void DataArrayDouble::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<std::string>& tinyInfoS)
5631 {
5632   setName(tinyInfoS[0]);
5633   if(isAllocated())
5634     {
5635       int nbOfCompo=getNumberOfComponents();
5636       for(int i=0;i<nbOfCompo;i++)
5637         setInfoOnComponent(i,tinyInfoS[i+1]);
5638     }
5639 }
5640
5641 DataArrayDoubleIterator::DataArrayDoubleIterator(DataArrayDouble *da):_da(da),_tuple_id(0),_nb_comp(0),_nb_tuple(0)
5642 {
5643   if(_da)
5644     {
5645       _da->incrRef();
5646       if(_da->isAllocated())
5647         {
5648           _nb_comp=da->getNumberOfComponents();
5649           _nb_tuple=da->getNumberOfTuples();
5650           _pt=da->getPointer();
5651         }
5652     }
5653 }
5654
5655 DataArrayDoubleIterator::~DataArrayDoubleIterator()
5656 {
5657   if(_da)
5658     _da->decrRef();
5659 }
5660
5661 DataArrayDoubleTuple *DataArrayDoubleIterator::nextt()
5662 {
5663   if(_tuple_id<_nb_tuple)
5664     {
5665       _tuple_id++;
5666       DataArrayDoubleTuple *ret=new DataArrayDoubleTuple(_pt,_nb_comp);
5667       _pt+=_nb_comp;
5668       return ret;
5669     }
5670   else
5671     return 0;
5672 }
5673
5674 DataArrayDoubleTuple::DataArrayDoubleTuple(double *pt, int nbOfComp):_pt(pt),_nb_of_compo(nbOfComp)
5675 {
5676 }
5677
5678
5679 std::string DataArrayDoubleTuple::repr() const
5680 {
5681   std::ostringstream oss; oss.precision(17); oss << "(";
5682   for(int i=0;i<_nb_of_compo-1;i++)
5683     oss << _pt[i] << ", ";
5684   oss << _pt[_nb_of_compo-1] << ")";
5685   return oss.str();
5686 }
5687
5688 double DataArrayDoubleTuple::doubleValue() const
5689 {
5690   if(_nb_of_compo==1)
5691     return *_pt;
5692   throw INTERP_KERNEL::Exception("DataArrayDoubleTuple::doubleValue : DataArrayDoubleTuple instance has not exactly 1 component -> Not possible to convert it into a double precision float !");
5693 }
5694
5695 /*!
5696  * This method returns a newly allocated instance the caller should dealed with by a ParaMEDMEM::DataArrayDouble::decrRef.
5697  * This method performs \b no copy of data. The content is only referenced using ParaMEDMEM::DataArrayDouble::useArray with ownership set to \b false.
5698  * This method throws an INTERP_KERNEL::Exception is it is impossible to match sizes of \b this that is too say \b nbOfCompo=this->_nb_of_elem and \bnbOfTuples==1 or
5699  * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
5700  */
5701 DataArrayDouble *DataArrayDoubleTuple::buildDADouble(int nbOfTuples, int nbOfCompo) const
5702 {
5703   if((_nb_of_compo==nbOfCompo && nbOfTuples==1) || (_nb_of_compo==nbOfTuples && nbOfCompo==1))
5704     {
5705       DataArrayDouble *ret=DataArrayDouble::New();
5706       ret->useExternalArrayWithRWAccess(_pt,nbOfTuples,nbOfCompo);
5707       return ret;
5708     }
5709   else
5710     {
5711       std::ostringstream oss; oss << "DataArrayDoubleTuple::buildDADouble : unable to build a requested DataArrayDouble instance with nbofTuple=" << nbOfTuples << " and nbOfCompo=" << nbOfCompo;
5712       oss << ".\nBecause the number of elements in this is " << _nb_of_compo << " !";
5713       throw INTERP_KERNEL::Exception(oss.str().c_str());
5714     }
5715 }
5716
5717 /*!
5718  * Returns a new instance of DataArrayInt. The caller is to delete this array
5719  * using decrRef() as it is no more needed. 
5720  */
5721 DataArrayInt *DataArrayInt::New()
5722 {
5723   return new DataArrayInt;
5724 }
5725
5726 /*!
5727  * Checks if raw data is allocated. Read more on the raw data
5728  * in \ref MEDCouplingArrayBasicsTuplesAndCompo "DataArrays infos" for more information.
5729  *  \return bool - \a true if the raw data is allocated, \a false else.
5730  */
5731 bool DataArrayInt::isAllocated() const
5732 {
5733   return getConstPointer()!=0;
5734 }
5735
5736 /*!
5737  * Checks if raw data is allocated and throws an exception if it is not the case.
5738  *  \throw If the raw data is not allocated.
5739  */
5740 void DataArrayInt::checkAllocated() const
5741 {
5742   if(!isAllocated())
5743     throw INTERP_KERNEL::Exception("DataArrayInt::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !");
5744 }
5745
5746 /*!
5747  * This method desallocated \a this without modification of informations relative to the components.
5748  * After call of this method, DataArrayInt::isAllocated will return false.
5749  * If \a this is already not allocated, \a this is let unchanged.
5750  */
5751 void DataArrayInt::desallocate()
5752 {
5753   _mem.destroy();
5754 }
5755
5756 std::size_t DataArrayInt::getHeapMemorySizeWithoutChildren() const
5757 {
5758   std::size_t sz(_mem.getNbOfElemAllocated());
5759   sz*=sizeof(int);
5760   return DataArray::getHeapMemorySizeWithoutChildren()+sz;
5761 }
5762
5763 /*!
5764  * Returns the only one value in \a this, if and only if number of elements
5765  * (nb of tuples * nb of components) is equal to 1, and that \a this is allocated.
5766  *  \return double - the sole value stored in \a this array.
5767  *  \throw If at least one of conditions stated above is not fulfilled.
5768  */
5769 int DataArrayInt::intValue() const
5770 {
5771   if(isAllocated())
5772     {
5773       if(getNbOfElems()==1)
5774         {
5775           return *getConstPointer();
5776         }
5777       else
5778         throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is allocated but number of elements is not equal to 1 !");
5779     }
5780   else
5781     throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is not allocated !");
5782 }
5783
5784 /*!
5785  * Returns an integer value characterizing \a this array, which is useful for a quick
5786  * comparison of many instances of DataArrayInt.
5787  *  \return int - the hash value.
5788  *  \throw If \a this is not allocated.
5789  */
5790 int DataArrayInt::getHashCode() const
5791 {
5792   checkAllocated();
5793   std::size_t nbOfElems=getNbOfElems();
5794   int ret=nbOfElems*65536;
5795   int delta=3;
5796   if(nbOfElems>48)
5797     delta=nbOfElems/8;
5798   int ret0=0;
5799   const int *pt=begin();
5800   for(std::size_t i=0;i<nbOfElems;i+=delta)
5801     ret0+=pt[i] & 0x1FFF;
5802   return ret+ret0;
5803 }
5804
5805 /*!
5806  * Checks the number of tuples.
5807  *  \return bool - \a true if getNumberOfTuples() == 0, \a false else.
5808  *  \throw If \a this is not allocated.
5809  */
5810 bool DataArrayInt::empty() const
5811 {
5812   checkAllocated();
5813   return getNumberOfTuples()==0;
5814 }
5815
5816 /*!
5817  * Returns a full copy of \a this. For more info on copying data arrays see
5818  * \ref MEDCouplingArrayBasicsCopyDeep.
5819  *  \return DataArrayInt * - a new instance of DataArrayInt.
5820  */
5821 DataArrayInt *DataArrayInt::deepCpy() const
5822 {
5823   return new DataArrayInt(*this);
5824 }
5825
5826 /*!
5827  * Returns either a \a deep or \a shallow copy of this array. For more info see
5828  * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
5829  *  \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
5830  *  \return DataArrayInt * - either a new instance of DataArrayInt (if \a dCpy
5831  *          == \a true) or \a this instance (if \a dCpy == \a false).
5832  */
5833 DataArrayInt *DataArrayInt::performCpy(bool dCpy) const
5834 {
5835   if(dCpy)
5836     return deepCpy();
5837   else
5838     {
5839       incrRef();
5840       return const_cast<DataArrayInt *>(this);
5841     }
5842 }
5843
5844 /*!
5845  * Copies all the data from another DataArrayInt. For more info see
5846  * \ref MEDCouplingArrayBasicsCopyDeepAssign.
5847  *  \param [in] other - another instance of DataArrayInt to copy data from.
5848  *  \throw If the \a other is not allocated.
5849  */
5850 void DataArrayInt::cpyFrom(const DataArrayInt& other)
5851 {
5852   other.checkAllocated();
5853   int nbOfTuples=other.getNumberOfTuples();
5854   int nbOfComp=other.getNumberOfComponents();
5855   allocIfNecessary(nbOfTuples,nbOfComp);
5856   std::size_t nbOfElems=(std::size_t)nbOfTuples*nbOfComp;
5857   int *pt=getPointer();
5858   const int *ptI=other.getConstPointer();
5859   for(std::size_t i=0;i<nbOfElems;i++)
5860     pt[i]=ptI[i];
5861   copyStringInfoFrom(other);
5862 }
5863
5864 /*!
5865  * This method reserve nbOfElems elements in memory ( nbOfElems*4 bytes ) \b without impacting the number of tuples in \a this.
5866  * 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.
5867  * If \a this has not already been allocated, number of components is set to one.
5868  * This method allows to reduce number of reallocations on invokation of DataArrayInt::pushBackSilent and DataArrayInt::pushBackValsSilent on \a this.
5869  * 
5870  * \sa DataArrayInt::pack, DataArrayInt::pushBackSilent, DataArrayInt::pushBackValsSilent
5871  */
5872 void DataArrayInt::reserve(std::size_t nbOfElems)
5873 {
5874   int nbCompo=getNumberOfComponents();
5875   if(nbCompo==1)
5876     {
5877       _mem.reserve(nbOfElems);
5878     }
5879   else if(nbCompo==0)
5880     {
5881       _mem.reserve(nbOfElems);
5882       _info_on_compo.resize(1);
5883     }
5884   else
5885     throw INTERP_KERNEL::Exception("DataArrayInt::reserve : not available for DataArrayInt with number of components different than 1 !");
5886 }
5887
5888 /*!
5889  * 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
5890  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
5891  *
5892  * \param [in] val the value to be added in \a this
5893  * \throw If \a this has already been allocated with number of components different from one.
5894  * \sa DataArrayInt::pushBackValsSilent
5895  */
5896 void DataArrayInt::pushBackSilent(int val)
5897 {
5898   int nbCompo=getNumberOfComponents();
5899   if(nbCompo==1)
5900     _mem.pushBack(val);
5901   else if(nbCompo==0)
5902     {
5903       _info_on_compo.resize(1);
5904       _mem.pushBack(val);
5905     }
5906   else
5907     throw INTERP_KERNEL::Exception("DataArrayInt::pushBackSilent : not available for DataArrayInt with number of components different than 1 !");
5908 }
5909
5910 /*!
5911  * 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
5912  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
5913  *
5914  *  \param [in] valsBg - an array of values to push at the end of \this.
5915  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
5916  *              the last value of \a valsBg is \a valsEnd[ -1 ].
5917  * \throw If \a this has already been allocated with number of components different from one.
5918  * \sa DataArrayInt::pushBackSilent
5919  */
5920 void DataArrayInt::pushBackValsSilent(const int *valsBg, const int *valsEnd)
5921 {
5922   int nbCompo=getNumberOfComponents();
5923   if(nbCompo==1)
5924     _mem.insertAtTheEnd(valsBg,valsEnd);
5925   else if(nbCompo==0)
5926     {
5927       _info_on_compo.resize(1);
5928       _mem.insertAtTheEnd(valsBg,valsEnd);
5929     }
5930   else
5931     throw INTERP_KERNEL::Exception("DataArrayInt::pushBackValsSilent : not available for DataArrayInt with number of components different than 1 !");
5932 }
5933
5934 /*!
5935  * This method returns silently ( without updating time label in \a this ) the last value, if any and suppress it.
5936  * \throw If \a this is already empty.
5937  * \throw If \a this has number of components different from one.
5938  */
5939 int DataArrayInt::popBackSilent()
5940 {
5941   if(getNumberOfComponents()==1)
5942     return _mem.popBack();
5943   else
5944     throw INTERP_KERNEL::Exception("DataArrayInt::popBackSilent : not available for DataArrayInt with number of components different than 1 !");
5945 }
5946
5947 /*!
5948  * 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.
5949  *
5950  * \sa DataArrayInt::getHeapMemorySizeWithoutChildren, DataArrayInt::reserve
5951  */
5952 void DataArrayInt::pack() const
5953 {
5954   _mem.pack();
5955 }
5956
5957 /*!
5958  * Allocates the raw data in memory. If exactly as same memory as needed already
5959  * allocated, it is not re-allocated.
5960  *  \param [in] nbOfTuple - number of tuples of data to allocate.
5961  *  \param [in] nbOfCompo - number of components of data to allocate.
5962  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
5963  */
5964 void DataArrayInt::allocIfNecessary(int nbOfTuple, int nbOfCompo)
5965 {
5966   if(isAllocated())
5967     {
5968       if(nbOfTuple!=getNumberOfTuples() || nbOfCompo!=getNumberOfComponents())
5969         alloc(nbOfTuple,nbOfCompo);
5970     }
5971   else
5972     alloc(nbOfTuple,nbOfCompo);
5973 }
5974
5975 /*!
5976  * Allocates the raw data in memory. If the memory was already allocated, then it is
5977  * freed and re-allocated. See an example of this method use
5978  * \ref MEDCouplingArraySteps1WC "here".
5979  *  \param [in] nbOfTuple - number of tuples of data to allocate.
5980  *  \param [in] nbOfCompo - number of components of data to allocate.
5981  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
5982  */
5983 void DataArrayInt::alloc(int nbOfTuple, int nbOfCompo)
5984 {
5985   if(nbOfTuple<0 || nbOfCompo<0)
5986     throw INTERP_KERNEL::Exception("DataArrayInt::alloc : request for negative length of data !");
5987   _info_on_compo.resize(nbOfCompo);
5988   _mem.alloc(nbOfCompo*(std::size_t)nbOfTuple);
5989   declareAsNew();
5990 }
5991
5992 /*!
5993  * Assign zero to all values in \a this array. To know more on filling arrays see
5994  * \ref MEDCouplingArrayFill.
5995  * \throw If \a this is not allocated.
5996  */
5997 void DataArrayInt::fillWithZero()
5998 {
5999   checkAllocated();
6000   _mem.fillWithValue(0);
6001   declareAsNew();
6002 }
6003
6004 /*!
6005  * Assign \a val to all values in \a this array. To know more on filling arrays see
6006  * \ref MEDCouplingArrayFill.
6007  *  \param [in] val - the value to fill with.
6008  *  \throw If \a this is not allocated.
6009  */
6010 void DataArrayInt::fillWithValue(int val)
6011 {
6012   checkAllocated();
6013   _mem.fillWithValue(val);
6014   declareAsNew();
6015 }
6016
6017 /*!
6018  * Set all values in \a this array so that the i-th element equals to \a init + i
6019  * (i starts from zero). To know more on filling arrays see \ref MEDCouplingArrayFill.
6020  *  \param [in] init - value to assign to the first element of array.
6021  *  \throw If \a this->getNumberOfComponents() != 1
6022  *  \throw If \a this is not allocated.
6023  */
6024 void DataArrayInt::iota(int init)
6025 {
6026   checkAllocated();
6027   if(getNumberOfComponents()!=1)
6028     throw INTERP_KERNEL::Exception("DataArrayInt::iota : works only for arrays with only one component, you can call 'rearrange' method before !");
6029   int *ptr=getPointer();
6030   int ntuples=getNumberOfTuples();
6031   for(int i=0;i<ntuples;i++)
6032     ptr[i]=init+i;
6033   declareAsNew();
6034 }
6035
6036 /*!
6037  * Returns a textual and human readable representation of \a this instance of
6038  * DataArrayInt. This text is shown when a DataArrayInt is printed in Python.
6039  *  \return std::string - text describing \a this DataArrayInt.
6040  */
6041 std::string DataArrayInt::repr() const
6042 {
6043   std::ostringstream ret;
6044   reprStream(ret);
6045   return ret.str();
6046 }
6047
6048 std::string DataArrayInt::reprZip() const
6049 {
6050   std::ostringstream ret;
6051   reprZipStream(ret);
6052   return ret.str();
6053 }
6054
6055 void DataArrayInt::writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const
6056 {
6057   static const char SPACE[4]={' ',' ',' ',' '};
6058   checkAllocated();
6059   std::string idt(indent,' ');
6060   ofs << idt << "<DataArray type=\"" << type << "\" Name=\"" << nameInFile << "\" NumberOfComponents=\"" << getNumberOfComponents() << "\"";
6061   if(byteArr)
6062     {
6063       ofs << " format=\"appended\" offset=\"" << byteArr->getNumberOfTuples() << "\">";
6064       if(std::string(type)=="Int32")
6065         {
6066           const char *data(reinterpret_cast<const char *>(begin()));
6067           std::size_t sz(getNbOfElems()*sizeof(int));
6068           byteArr->insertAtTheEnd(data,data+sz);
6069           byteArr->insertAtTheEnd(SPACE,SPACE+4);
6070         }
6071       else if(std::string(type)=="Int8")
6072         {
6073           INTERP_KERNEL::AutoPtr<char> tmp(new char[getNbOfElems()]);
6074           std::copy(begin(),end(),(char *)tmp);
6075           byteArr->insertAtTheEnd((char *)tmp,(char *)tmp+getNbOfElems());
6076           byteArr->insertAtTheEnd(SPACE,SPACE+4);
6077         }
6078       else if(std::string(type)=="UInt8")
6079         {
6080           INTERP_KERNEL::AutoPtr<unsigned char> tmp(new unsigned char[getNbOfElems()]);
6081           std::copy(begin(),end(),(unsigned char *)tmp);
6082           byteArr->insertAtTheEnd((unsigned char *)tmp,(unsigned char *)tmp+getNbOfElems());
6083           byteArr->insertAtTheEnd(SPACE,SPACE+4);
6084         }
6085       else
6086         throw INTERP_KERNEL::Exception("DataArrayInt::writeVTK : Only Int32, Int8 and UInt8 supported !");
6087     }
6088   else
6089     {
6090       ofs << " RangeMin=\"" << getMinValueInArray() << "\" RangeMax=\"" << getMaxValueInArray() << "\" format=\"ascii\">\n" << idt;
6091       std::copy(begin(),end(),std::ostream_iterator<int>(ofs," "));
6092     }
6093   ofs << std::endl << idt << "</DataArray>\n";
6094 }
6095
6096 void DataArrayInt::reprStream(std::ostream& stream) const
6097 {
6098   stream << "Name of int array : \"" << _name << "\"\n";
6099   reprWithoutNameStream(stream);
6100 }
6101
6102 void DataArrayInt::reprZipStream(std::ostream& stream) const
6103 {
6104   stream << "Name of int array : \"" << _name << "\"\n";
6105   reprZipWithoutNameStream(stream);
6106 }
6107
6108 void DataArrayInt::reprWithoutNameStream(std::ostream& stream) const
6109 {
6110   DataArray::reprWithoutNameStream(stream);
6111   _mem.repr(getNumberOfComponents(),stream);
6112 }
6113
6114 void DataArrayInt::reprZipWithoutNameStream(std::ostream& stream) const
6115 {
6116   DataArray::reprWithoutNameStream(stream);
6117   _mem.reprZip(getNumberOfComponents(),stream);
6118 }
6119
6120 void DataArrayInt::reprCppStream(const std::string& varName, std::ostream& stream) const
6121 {
6122   int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents();
6123   const int *data=getConstPointer();
6124   stream << "DataArrayInt *" << varName << "=DataArrayInt::New();" << std::endl;
6125   if(nbTuples*nbComp>=1)
6126     {
6127       stream << "const int " << varName << "Data[" << nbTuples*nbComp << "]={";
6128       std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<int>(stream,","));
6129       stream << data[nbTuples*nbComp-1] << "};" << std::endl;
6130       stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
6131     }
6132   else
6133     stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
6134   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
6135 }
6136
6137 /*!
6138  * Method that gives a quick overvien of \a this for python.
6139  */
6140 void DataArrayInt::reprQuickOverview(std::ostream& stream) const
6141 {
6142   static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
6143   stream << "DataArrayInt C++ instance at " << this << ". ";
6144   if(isAllocated())
6145     {
6146       int nbOfCompo=(int)_info_on_compo.size();
6147       if(nbOfCompo>=1)
6148         {
6149           int nbOfTuples=getNumberOfTuples();
6150           stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
6151           reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
6152         }
6153       else
6154         stream << "Number of components : 0.";
6155     }
6156   else
6157     stream << "*** No data allocated ****";
6158 }
6159
6160 void DataArrayInt::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
6161 {
6162   const int *data=begin();
6163   int nbOfTuples=getNumberOfTuples();
6164   int nbOfCompo=(int)_info_on_compo.size();
6165   std::ostringstream oss2; oss2 << "[";
6166   std::string oss2Str(oss2.str());
6167   bool isFinished=true;
6168   for(int i=0;i<nbOfTuples && isFinished;i++)
6169     {
6170       if(nbOfCompo>1)
6171         {
6172           oss2 << "(";
6173           for(int j=0;j<nbOfCompo;j++,data++)
6174             {
6175               oss2 << *data;
6176               if(j!=nbOfCompo-1) oss2 << ", ";
6177             }
6178           oss2 << ")";
6179         }
6180       else
6181         oss2 << *data++;
6182       if(i!=nbOfTuples-1) oss2 << ", ";
6183       std::string oss3Str(oss2.str());
6184       if(oss3Str.length()<maxNbOfByteInRepr)
6185         oss2Str=oss3Str;
6186       else
6187         isFinished=false;
6188     }
6189   stream << oss2Str;
6190   if(!isFinished)
6191     stream << "... ";
6192   stream << "]";
6193 }
6194
6195 /*!
6196  * Modifies \a this one-dimensional array so that each value \a v = \a indArrBg[ \a v ],
6197  * i.e. a current value is used as in index to get a new value from \a indArrBg.
6198  *  \param [in] indArrBg - pointer to the first element of array of new values to assign
6199  *         to \a this array.
6200  *  \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
6201  *              the last value of \a indArrBg is \a indArrEnd[ -1 ].
6202  *  \throw If \a this->getNumberOfComponents() != 1
6203  *  \throw If any value of \a this can't be used as a valid index for 
6204  *         [\a indArrBg, \a indArrEnd).
6205  */
6206 void DataArrayInt::transformWithIndArr(const int *indArrBg, const int *indArrEnd)
6207 {
6208   checkAllocated();
6209   if(getNumberOfComponents()!=1)
6210     throw INTERP_KERNEL::Exception("Call transformWithIndArr method on DataArrayInt with only one component, you can call 'rearrange' method before !");
6211   int nbElemsIn=(int)std::distance(indArrBg,indArrEnd);
6212   int nbOfTuples=getNumberOfTuples();
6213   int *pt=getPointer();
6214   for(int i=0;i<nbOfTuples;i++,pt++)
6215     {
6216       if(*pt>=0 && *pt<nbElemsIn)
6217         *pt=indArrBg[*pt];
6218       else
6219         {
6220           std::ostringstream oss; oss << "DataArrayInt::transformWithIndArr : error on tuple #" << i << " of this value is " << *pt << ", should be in [0," << nbElemsIn << ") !";
6221           throw INTERP_KERNEL::Exception(oss.str().c_str());
6222         }
6223     }
6224   declareAsNew();
6225 }
6226
6227 /*!
6228  * Computes distribution of values of \a this one-dimensional array between given value
6229  * ranges (casts). This method is typically useful for entity number spliting by types,
6230  * for example. 
6231  *  \warning The values contained in \a arrBg should be sorted ascendently. No
6232  *           check of this is be done. If not, the result is not warranted. 
6233  *  \param [in] arrBg - the array of ascending values defining the value ranges. The i-th
6234  *         value of \a arrBg (\a arrBg[ i ]) gives the lowest value of the i-th range,
6235  *         and the greatest value of the i-th range equals to \a arrBg[ i+1 ] - 1. \a
6236  *         arrBg containing \a n values defines \a n-1 ranges. The last value of \a arrBg
6237  *         should be more than every value in \a this array.
6238  *  \param [in] arrEnd - specifies the end of the array \a arrBg, so that
6239  *              the last value of \a arrBg is \a arrEnd[ -1 ].
6240  *  \param [out] castArr - a new instance of DataArrayInt, of same size as \a this array
6241  *         (same number of tuples and components), the caller is to delete 
6242  *         using decrRef() as it is no more needed.
6243  *         This array contains indices of ranges for every value of \a this array. I.e.
6244  *         the i-th value of \a castArr gives the index of range the i-th value of \a this
6245  *         belongs to. Or, in other words, this parameter contains for each tuple in \a
6246  *         this in which cast it holds.
6247  *  \param [out] rankInsideCast - a new instance of DataArrayInt, of same size as \a this
6248  *         array, the caller is to delete using decrRef() as it is no more needed.
6249  *         This array contains ranks of values of \a this array within ranges
6250  *         they belongs to. I.e. the i-th value of \a rankInsideCast gives the rank of
6251  *         the i-th value of \a this array within the \a castArr[ i ]-th range, to which
6252  *         the i-th value of \a this belongs to. Or, in other words, this param contains 
6253  *         for each tuple its rank inside its cast. The rank is computed as difference
6254  *         between the value and the lowest value of range.
6255  *  \param [out] castsPresent - a new instance of DataArrayInt, containing indices of
6256  *         ranges (casts) to which at least one value of \a this array belongs.
6257  *         Or, in other words, this param contains the casts that \a this contains.
6258  *         The caller is to delete this array using decrRef() as it is no more needed.
6259  *
6260  * \b Example: If \a this contains [6,5,0,3,2,7,8,1,4] and \a arrBg contains [0,4,9] then
6261  *            the output of this method will be : 
6262  * - \a castArr       : [1,1,0,0,0,1,1,0,1]
6263  * - \a rankInsideCast: [2,1,0,3,2,3,4,1,0]
6264  * - \a castsPresent  : [0,1]
6265  *
6266  * I.e. values of \a this array belong to 2 ranges: #0 and #1. Value 6 belongs to the
6267  * range #1 and its rank within this range is 2; etc.
6268  *
6269  *  \throw If \a this->getNumberOfComponents() != 1.
6270  *  \throw If \a arrEnd - arrBg < 2.
6271  *  \throw If any value of \a this is not less than \a arrEnd[-1].
6272  */
6273 void DataArrayInt::splitByValueRange(const int *arrBg, const int *arrEnd,
6274                                      DataArrayInt *& castArr, DataArrayInt *& rankInsideCast, DataArrayInt *& castsPresent) const throw(INTERP_KERNEL::Exception)
6275 {
6276   checkAllocated();
6277   if(getNumberOfComponents()!=1)
6278     throw INTERP_KERNEL::Exception("Call splitByValueRange  method on DataArrayInt with only one component, you can call 'rearrange' method before !");
6279   int nbOfTuples=getNumberOfTuples();
6280   std::size_t nbOfCast=std::distance(arrBg,arrEnd);
6281   if(nbOfCast<2)
6282     throw INTERP_KERNEL::Exception("DataArrayInt::splitByValueRange : The input array giving the cast range values should be of size >=2 !");
6283   nbOfCast--;
6284   const int *work=getConstPointer();
6285   typedef std::reverse_iterator<const int *> rintstart;
6286   rintstart bg(arrEnd);//OK no problem because size of 'arr' is greater or equal 2
6287   rintstart end2(arrBg);
6288   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=DataArrayInt::New();
6289   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret2=DataArrayInt::New();
6290   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret3=DataArrayInt::New();
6291   ret1->alloc(nbOfTuples,1);
6292   ret2->alloc(nbOfTuples,1);
6293   int *ret1Ptr=ret1->getPointer();
6294   int *ret2Ptr=ret2->getPointer();
6295   std::set<std::size_t> castsDetected;
6296   for(int i=0;i<nbOfTuples;i++)
6297     {
6298       rintstart res=std::find_if(bg,end2,std::bind2nd(std::less_equal<int>(), work[i]));
6299       std::size_t pos=std::distance(bg,res);
6300       std::size_t pos2=nbOfCast-pos;
6301       if(pos2<nbOfCast)
6302         {
6303           ret1Ptr[i]=(int)pos2;
6304           ret2Ptr[i]=work[i]-arrBg[pos2];
6305           castsDetected.insert(pos2);
6306         }
6307       else
6308         {
6309           std::ostringstream oss; oss << "DataArrayInt::splitByValueRange : At rank #" << i << " the value is " << work[i] << " should be in [0," << *bg << ") !";
6310           throw INTERP_KERNEL::Exception(oss.str().c_str());
6311         }
6312     }
6313   ret3->alloc((int)castsDetected.size(),1);
6314   std::copy(castsDetected.begin(),castsDetected.end(),ret3->getPointer());
6315   castArr=ret1.retn();
6316   rankInsideCast=ret2.retn();
6317   castsPresent=ret3.retn();
6318 }
6319
6320 /*!
6321  * Creates a one-dimensional DataArrayInt (\a res) whose contents are computed from 
6322  * values of \a this (\a a) and the given (\a indArr) arrays as follows:
6323  * \a res[ \a indArr[ \a a[ i ]]] = i. I.e. for each value in place i \a v = \a a[ i ],
6324  * new value in place \a indArr[ \a v ] is i.
6325  *  \param [in] indArrBg - the array holding indices within the result array to assign
6326  *         indices of values of \a this array pointing to values of \a indArrBg.
6327  *  \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
6328  *              the last value of \a indArrBg is \a indArrEnd[ -1 ].
6329  *  \return DataArrayInt * - the new instance of DataArrayInt.
6330  *          The caller is to delete this result array using decrRef() as it is no more
6331  *          needed.
6332  *  \throw If \a this->getNumberOfComponents() != 1.
6333  *  \throw If any value of \a this array is not a valid index for \a indArrBg array.
6334  *  \throw If any value of \a indArrBg is not a valid index for \a this array.
6335  */
6336 DataArrayInt *DataArrayInt::transformWithIndArrR(const int *indArrBg, const int *indArrEnd) const
6337 {
6338   checkAllocated();
6339   if(getNumberOfComponents()!=1)
6340     throw INTERP_KERNEL::Exception("Call transformWithIndArrR method on DataArrayInt with only one component, you can call 'rearrange' method before !");
6341   int nbElemsIn=(int)std::distance(indArrBg,indArrEnd);
6342   int nbOfTuples=getNumberOfTuples();
6343   const int *pt=getConstPointer();
6344   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6345   ret->alloc(nbOfTuples,1);
6346   ret->fillWithValue(-1);
6347   int *tmp=ret->getPointer();
6348   for(int i=0;i<nbOfTuples;i++,pt++)
6349     {
6350       if(*pt>=0 && *pt<nbElemsIn)
6351         {
6352           int pos=indArrBg[*pt];
6353           if(pos>=0 && pos<nbOfTuples)
6354             tmp[pos]=i;
6355           else
6356             {
6357               std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value of new pos is " << pos << " ( indArrBg[" << *pt << "]) ! Should be in [0," << nbOfTuples << ") !";
6358               throw INTERP_KERNEL::Exception(oss.str().c_str());
6359             }
6360         }
6361       else
6362         {
6363           std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value is " << *pt << " and indirectionnal array as a size equal to " << nbElemsIn << " !";
6364           throw INTERP_KERNEL::Exception(oss.str().c_str());
6365         }
6366     }
6367   return ret.retn();
6368 }
6369
6370 /*!
6371  * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
6372  * from values of \a this array, which is supposed to contain a renumbering map in 
6373  * "Old to New" mode. The result array contains a renumbering map in "New to Old" mode.
6374  * To know how to use the renumbering maps see \ref MEDCouplingArrayRenumbering.
6375  *  \param [in] newNbOfElem - the number of tuples in the result array.
6376  *  \return DataArrayInt * - the new instance of DataArrayInt.
6377  *          The caller is to delete this result array using decrRef() as it is no more
6378  *          needed.
6379  * 
6380  *  \if ENABLE_EXAMPLES
6381  *  \ref cpp_mcdataarrayint_invertarrayo2n2n2o "Here is a C++ example".<br>
6382  *  \ref py_mcdataarrayint_invertarrayo2n2n2o  "Here is a Python example".
6383  *  \endif
6384  */
6385 DataArrayInt *DataArrayInt::invertArrayO2N2N2O(int newNbOfElem) const
6386 {
6387   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6388   ret->alloc(newNbOfElem,1);
6389   int nbOfOldNodes=getNumberOfTuples();
6390   const int *old2New=getConstPointer();
6391   int *pt=ret->getPointer();
6392   for(int i=0;i!=nbOfOldNodes;i++)
6393     {
6394       int newp(old2New[i]);
6395       if(newp!=-1)
6396         {
6397           if(newp>=0 && newp<newNbOfElem)
6398             pt[newp]=i;
6399           else
6400             {
6401               std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2O : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
6402               throw INTERP_KERNEL::Exception(oss.str().c_str());
6403             }
6404         }
6405     }
6406   return ret.retn();
6407 }
6408
6409 /*!
6410  * This method is similar to DataArrayInt::invertArrayO2N2N2O except that 
6411  * Example : If \a this contains [0,1,2,0,3,4,5,4,6,4] this method will return [0,1,2,4,5,6,8] whereas DataArrayInt::invertArrayO2N2N2O returns [3,1,2,4,9,6,8]
6412  */
6413 DataArrayInt *DataArrayInt::invertArrayO2N2N2OBis(int newNbOfElem) const
6414 {
6415   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6416   ret->alloc(newNbOfElem,1);
6417   int nbOfOldNodes=getNumberOfTuples();
6418   const int *old2New=getConstPointer();
6419   int *pt=ret->getPointer();
6420   for(int i=nbOfOldNodes-1;i>=0;i--)
6421     {
6422       int newp(old2New[i]);
6423       if(newp!=-1)
6424         {
6425           if(newp>=0 && newp<newNbOfElem)
6426             pt[newp]=i;
6427           else
6428             {
6429               std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2OBis : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
6430               throw INTERP_KERNEL::Exception(oss.str().c_str());
6431             }
6432         }
6433     }
6434   return ret.retn();
6435 }
6436
6437 /*!
6438  * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
6439  * from values of \a this array, which is supposed to contain a renumbering map in 
6440  * "New to Old" mode. The result array contains a renumbering map in "Old to New" mode.
6441  * To know how to use the renumbering maps see \ref MEDCouplingArrayRenumbering.
6442  *  \param [in] newNbOfElem - the number of tuples in the result array.
6443  *  \return DataArrayInt * - the new instance of DataArrayInt.
6444  *          The caller is to delete this result array using decrRef() as it is no more
6445  *          needed.
6446  * 
6447  *  \if ENABLE_EXAMPLES
6448  *  \ref cpp_mcdataarrayint_invertarrayn2o2o2n "Here is a C++ example".
6449  *
6450  *  \ref py_mcdataarrayint_invertarrayn2o2o2n "Here is a Python example".
6451  *  \endif
6452  */
6453 DataArrayInt *DataArrayInt::invertArrayN2O2O2N(int oldNbOfElem) const
6454 {
6455   checkAllocated();
6456   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6457   ret->alloc(oldNbOfElem,1);
6458   const int *new2Old=getConstPointer();
6459   int *pt=ret->getPointer();
6460   std::fill(pt,pt+oldNbOfElem,-1);
6461   int nbOfNewElems=getNumberOfTuples();
6462   for(int i=0;i<nbOfNewElems;i++)
6463     {
6464       int v(new2Old[i]);
6465       if(v>=0 && v<oldNbOfElem)
6466          pt[v]=i;
6467       else
6468         {
6469           std::ostringstream oss; oss << "DataArrayInt::invertArrayN2O2O2N : in new id #" << i << " old value is " << v << " expected to be in [0," << oldNbOfElem << ") !";
6470           throw INTERP_KERNEL::Exception(oss.str().c_str());
6471         }
6472     }
6473   return ret.retn();
6474 }
6475
6476 /*!
6477  * Equivalent to DataArrayInt::isEqual except that if false the reason of
6478  * mismatch is given.
6479  * 
6480  * \param [in] other the instance to be compared with \a this
6481  * \param [out] reason In case of inequality returns the reason.
6482  * \sa DataArrayInt::isEqual
6483  */
6484 bool DataArrayInt::isEqualIfNotWhy(const DataArrayInt& other, std::string& reason) const
6485 {
6486   if(!areInfoEqualsIfNotWhy(other,reason))
6487     return false;
6488   return _mem.isEqual(other._mem,0,reason);
6489 }
6490
6491 /*!
6492  * Checks if \a this and another DataArrayInt are fully equal. For more info see
6493  * \ref MEDCouplingArrayBasicsCompare.
6494  *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
6495  *  \return bool - \a true if the two arrays are equal, \a false else.
6496  */
6497 bool DataArrayInt::isEqual(const DataArrayInt& other) const
6498 {
6499   std::string tmp;
6500   return isEqualIfNotWhy(other,tmp);
6501 }
6502
6503 /*!
6504  * Checks if values of \a this and another DataArrayInt are equal. For more info see
6505  * \ref MEDCouplingArrayBasicsCompare.
6506  *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
6507  *  \return bool - \a true if the values of two arrays are equal, \a false else.
6508  */
6509 bool DataArrayInt::isEqualWithoutConsideringStr(const DataArrayInt& other) const
6510 {
6511   std::string tmp;
6512   return _mem.isEqual(other._mem,0,tmp);
6513 }
6514
6515 /*!
6516  * Checks if values of \a this and another DataArrayInt are equal. Comparison is
6517  * performed on sorted value sequences.
6518  * For more info see\ref MEDCouplingArrayBasicsCompare.
6519  *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
6520  *  \return bool - \a true if the sorted values of two arrays are equal, \a false else.
6521  */
6522 bool DataArrayInt::isEqualWithoutConsideringStrAndOrder(const DataArrayInt& other) const
6523 {
6524   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a=deepCpy();
6525   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> b=other.deepCpy();
6526   a->sort();
6527   b->sort();
6528   return a->isEqualWithoutConsideringStr(*b);
6529 }
6530
6531 /*!
6532  * This method compares content of input vector \a v and \a this.
6533  * 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.
6534  * For performance reasons \a this is expected to be sorted ascendingly. If not an exception will be thrown.
6535  *
6536  * \param [in] v - the vector of 'flags' to be compared with \a this.
6537  *
6538  * \throw If \a this is not sorted ascendingly.
6539  * \throw If \a this has not exactly one component.
6540  * \throw If \a this is not allocated.
6541  */
6542 bool DataArrayInt::isFittingWith(const std::vector<bool>& v) const
6543 {
6544   checkAllocated();
6545   if(getNumberOfComponents()!=1)
6546     throw INTERP_KERNEL::Exception("DataArrayInt::isFittingWith : number of components of this should be equal to one !");
6547   const int *w(begin()),*end2(end());
6548   int refVal=-std::numeric_limits<int>::max();
6549   int i=0;
6550   std::vector<bool>::const_iterator it(v.begin());
6551   for(;it!=v.end();it++,i++)
6552     {
6553       if(*it)
6554         {
6555           if(w!=end2)
6556             {
6557               if(*w++==i)
6558                 {
6559                   if(i>refVal)
6560                     refVal=i;
6561                   else
6562                     {
6563                       std::ostringstream oss; oss << "DataArrayInt::isFittingWith : At pos #" << std::distance(begin(),w-1) << " this is not sorted ascendingly !";
6564                       throw INTERP_KERNEL::Exception(oss.str().c_str());
6565                     }
6566                 }
6567               else
6568                 return false;
6569             }
6570           else
6571             return false;
6572         }
6573     }
6574   return w==end2;
6575 }
6576
6577 /*!
6578  * Sorts values of the array.
6579  *  \param [in] asc - \a true means ascending order, \a false, descending.
6580  *  \throw If \a this is not allocated.
6581  *  \throw If \a this->getNumberOfComponents() != 1.
6582  */
6583 void DataArrayInt::sort(bool asc)
6584 {
6585   checkAllocated();
6586   if(getNumberOfComponents()!=1)
6587     throw INTERP_KERNEL::Exception("DataArrayInt::sort : only supported with 'this' array with ONE component !");
6588   _mem.sort(asc);
6589   declareAsNew();
6590 }
6591
6592 /*!
6593  * Computes for each tuple the sum of number of components values in the tuple and return it.
6594  * 
6595  * \return DataArrayInt * - the new instance of DataArrayInt containing the
6596  *          same number of tuples as \a this array and one component.
6597  *          The caller is to delete this result array using decrRef() as it is no more
6598  *          needed.
6599  *  \throw If \a this is not allocated.
6600  */
6601 DataArrayInt *DataArrayInt::sumPerTuple() const
6602 {
6603   checkAllocated();
6604   int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
6605   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
6606   ret->alloc(nbOfTuple,1);
6607   const int *src(getConstPointer());
6608   int *dest(ret->getPointer());
6609   for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
6610     *dest=std::accumulate(src,src+nbOfComp,0);
6611   return ret.retn();
6612 }
6613
6614 /*!
6615  * Reverse the array values.
6616  *  \throw If \a this->getNumberOfComponents() < 1.
6617  *  \throw If \a this is not allocated.
6618  */
6619 void DataArrayInt::reverse()
6620 {
6621   checkAllocated();
6622   _mem.reverse(getNumberOfComponents());
6623   declareAsNew();
6624 }
6625
6626 /*!
6627  * Checks that \a this array is consistently **increasing** or **decreasing** in value.
6628  * If not an exception is thrown.
6629  *  \param [in] increasing - if \a true, the array values should be increasing.
6630  *  \throw If sequence of values is not strictly monotonic in agreement with \a
6631  *         increasing arg.
6632  *  \throw If \a this->getNumberOfComponents() != 1.
6633  *  \throw If \a this is not allocated.
6634  */
6635 void DataArrayInt::checkMonotonic(bool increasing) const
6636 {
6637   if(!isMonotonic(increasing))
6638     {
6639       if (increasing)
6640         throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not INCREASING monotonic !");
6641       else
6642         throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not DECREASING monotonic !");
6643     }
6644 }
6645
6646 /*!
6647  * Checks that \a this array is consistently **increasing** or **decreasing** in value.
6648  *  \param [in] increasing - if \a true, array values should be increasing.
6649  *  \return bool - \a true if values change in accordance with \a increasing arg.
6650  *  \throw If \a this->getNumberOfComponents() != 1.
6651  *  \throw If \a this is not allocated.
6652  */
6653 bool DataArrayInt::isMonotonic(bool increasing) const
6654 {
6655   checkAllocated();
6656   if(getNumberOfComponents()!=1)
6657     throw INTERP_KERNEL::Exception("DataArrayInt::isMonotonic : only supported with 'this' array with ONE component !");
6658   int nbOfElements=getNumberOfTuples();
6659   const int *ptr=getConstPointer();
6660   if(nbOfElements==0)
6661     return true;
6662   int ref=ptr[0];
6663   if(increasing)
6664     {
6665       for(int i=1;i<nbOfElements;i++)
6666         {
6667           if(ptr[i]>=ref)
6668             ref=ptr[i];
6669           else
6670             return false;
6671         }
6672     }
6673   else
6674     {
6675       for(int i=1;i<nbOfElements;i++)
6676         {
6677           if(ptr[i]<=ref)
6678             ref=ptr[i];
6679           else
6680             return false;
6681         }
6682     }
6683   return true;
6684 }
6685
6686 /*!
6687  * This method check that array consistently INCREASING or DECREASING in value.
6688  */
6689 bool DataArrayInt::isStrictlyMonotonic(bool increasing) const
6690 {
6691   checkAllocated();
6692   if(getNumberOfComponents()!=1)
6693     throw INTERP_KERNEL::Exception("DataArrayInt::isStrictlyMonotonic : only supported with 'this' array with ONE component !");
6694   int nbOfElements=getNumberOfTuples();
6695   const int *ptr=getConstPointer();
6696   if(nbOfElements==0)
6697     return true;
6698   int ref=ptr[0];
6699   if(increasing)
6700     {
6701       for(int i=1;i<nbOfElements;i++)
6702         {
6703           if(ptr[i]>ref)
6704             ref=ptr[i];
6705           else
6706             return false;
6707         }
6708     }
6709   else
6710     {
6711       for(int i=1;i<nbOfElements;i++)
6712         {
6713           if(ptr[i]<ref)
6714             ref=ptr[i];
6715           else
6716             return false;
6717         }
6718     }
6719   return true;
6720 }
6721
6722 /*!
6723  * This method check that array consistently INCREASING or DECREASING in value.
6724  */
6725 void DataArrayInt::checkStrictlyMonotonic(bool increasing) const
6726 {
6727   if(!isStrictlyMonotonic(increasing))
6728     {
6729       if (increasing)
6730         throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly INCREASING monotonic !");
6731       else
6732         throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly DECREASING monotonic !");
6733     }
6734 }
6735
6736 /*!
6737  * Creates a new one-dimensional DataArrayInt of the same size as \a this and a given
6738  * one-dimensional arrays that must be of the same length. The result array describes
6739  * correspondence between \a this and \a other arrays, so that 
6740  * <em> other.getIJ(i,0) == this->getIJ(ret->getIJ(i),0)</em>. If such a permutation is
6741  * not possible because some element in \a other is not in \a this, an exception is thrown.
6742  *  \param [in] other - an array to compute permutation to.
6743  *  \return DataArrayInt * - a new instance of DataArrayInt, which is a permutation array
6744  * from \a this to \a other. The caller is to delete this array using decrRef() as it is
6745  * no more needed.
6746  *  \throw If \a this->getNumberOfComponents() != 1.
6747  *  \throw If \a other->getNumberOfComponents() != 1.
6748  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples().
6749  *  \throw If \a other includes a value which is not in \a this array.
6750  * 
6751  *  \if ENABLE_EXAMPLES
6752  *  \ref cpp_mcdataarrayint_buildpermutationarr "Here is a C++ example".
6753  *
6754  *  \ref py_mcdataarrayint_buildpermutationarr "Here is a Python example".
6755  *  \endif
6756  */
6757 DataArrayInt *DataArrayInt::buildPermutationArr(const DataArrayInt& other) const
6758 {
6759   checkAllocated();
6760   if(getNumberOfComponents()!=1 || other.getNumberOfComponents()!=1)
6761     throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' have to have exactly ONE component !");
6762   int nbTuple=getNumberOfTuples();
6763   other.checkAllocated();
6764   if(nbTuple!=other.getNumberOfTuples())
6765     throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' must have the same number of tuple !");
6766   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6767   ret->alloc(nbTuple,1);
6768   ret->fillWithValue(-1);
6769   const int *pt=getConstPointer();
6770   std::map<int,int> mm;
6771   for(int i=0;i<nbTuple;i++)
6772     mm[pt[i]]=i;
6773   pt=other.getConstPointer();
6774   int *retToFill=ret->getPointer();
6775   for(int i=0;i<nbTuple;i++)
6776     {
6777       std::map<int,int>::const_iterator it=mm.find(pt[i]);
6778       if(it==mm.end())
6779         {
6780           std::ostringstream oss; oss << "DataArrayInt::buildPermutationArr : Arrays mismatch : element (" << pt[i] << ") in 'other' not findable in 'this' !";
6781           throw INTERP_KERNEL::Exception(oss.str().c_str());
6782         }
6783       retToFill[i]=(*it).second;
6784     }
6785   return ret.retn();
6786 }
6787
6788 /*!
6789  * Sets a C array to be used as raw data of \a this. The previously set info
6790  *  of components is retained and re-sized. 
6791  * For more info see \ref MEDCouplingArraySteps1.
6792  *  \param [in] array - the C array to be used as raw data of \a this.
6793  *  \param [in] ownership - if \a true, \a array will be deallocated at destruction of \a this.
6794  *  \param [in] type - specifies how to deallocate \a array. If \a type == ParaMEDMEM::CPP_DEALLOC,
6795  *                     \c delete [] \c array; will be called. If \a type == ParaMEDMEM::C_DEALLOC,
6796  *                     \c free(\c array ) will be called.
6797  *  \param [in] nbOfTuple - new number of tuples in \a this.
6798  *  \param [in] nbOfCompo - new number of components in \a this.
6799  */
6800 void DataArrayInt::useArray(const int *array, bool ownership,  DeallocType type, int nbOfTuple, int nbOfCompo)
6801 {
6802   _info_on_compo.resize(nbOfCompo);
6803   _mem.useArray(array,ownership,type,nbOfTuple*nbOfCompo);
6804   declareAsNew();
6805 }
6806
6807 void DataArrayInt::useExternalArrayWithRWAccess(const int *array, int nbOfTuple, int nbOfCompo)
6808 {
6809   _info_on_compo.resize(nbOfCompo);
6810   _mem.useExternalArrayWithRWAccess(array,nbOfTuple*nbOfCompo);
6811   declareAsNew();
6812 }
6813
6814 /*!
6815  * Returns a new DataArrayInt holding the same values as \a this array but differently
6816  * arranged in memory. If \a this array holds 2 components of 3 values:
6817  * \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$, then the result array holds these values arranged
6818  * as follows: \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$.
6819  *  \warning Do not confuse this method with transpose()!
6820  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
6821  *          is to delete using decrRef() as it is no more needed.
6822  *  \throw If \a this is not allocated.
6823  */
6824 DataArrayInt *DataArrayInt::fromNoInterlace() const
6825 {
6826   checkAllocated();
6827   if(_mem.isNull())
6828     throw INTERP_KERNEL::Exception("DataArrayInt::fromNoInterlace : Not defined array !");
6829   int *tab=_mem.fromNoInterlace(getNumberOfComponents());
6830   DataArrayInt *ret=DataArrayInt::New();
6831   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
6832   return ret;
6833 }
6834
6835 /*!
6836  * Returns a new DataArrayInt holding the same values as \a this array but differently
6837  * arranged in memory. If \a this array holds 2 components of 3 values:
6838  * \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$, then the result array holds these values arranged
6839  * as follows: \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$.
6840  *  \warning Do not confuse this method with transpose()!
6841  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
6842  *          is to delete using decrRef() as it is no more needed.
6843  *  \throw If \a this is not allocated.
6844  */
6845 DataArrayInt *DataArrayInt::toNoInterlace() const
6846 {
6847   checkAllocated();
6848   if(_mem.isNull())
6849     throw INTERP_KERNEL::Exception("DataArrayInt::toNoInterlace : Not defined array !");
6850   int *tab=_mem.toNoInterlace(getNumberOfComponents());
6851   DataArrayInt *ret=DataArrayInt::New();
6852   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
6853   return ret;
6854 }
6855
6856 /*!
6857  * Permutes values of \a this array as required by \a old2New array. The values are
6858  * permuted so that \c new[ \a old2New[ i ]] = \c old[ i ]. Number of tuples remains
6859  * the same as in \this one.
6860  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
6861  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
6862  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
6863  *     giving a new position for i-th old value.
6864  */
6865 void DataArrayInt::renumberInPlace(const int *old2New)
6866 {
6867   checkAllocated();
6868   int nbTuples=getNumberOfTuples();
6869   int nbOfCompo=getNumberOfComponents();
6870   int *tmp=new int[nbTuples*nbOfCompo];
6871   const int *iptr=getConstPointer();
6872   for(int i=0;i<nbTuples;i++)
6873     {
6874       int v=old2New[i];
6875       if(v>=0 && v<nbTuples)
6876         std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),tmp+nbOfCompo*v);
6877       else
6878         {
6879           std::ostringstream oss; oss << "DataArrayInt::renumberInPlace : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
6880           throw INTERP_KERNEL::Exception(oss.str().c_str());
6881         }
6882     }
6883   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
6884   delete [] tmp;
6885   declareAsNew();
6886 }
6887
6888 /*!
6889  * Permutes values of \a this array as required by \a new2Old array. The values are
6890  * permuted so that \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of tuples remains
6891  * the same as in \this one.
6892  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
6893  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
6894  *     giving a previous position of i-th new value.
6895  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
6896  *          is to delete using decrRef() as it is no more needed.
6897  */
6898 void DataArrayInt::renumberInPlaceR(const int *new2Old)
6899 {
6900   checkAllocated();
6901   int nbTuples=getNumberOfTuples();
6902   int nbOfCompo=getNumberOfComponents();
6903   int *tmp=new int[nbTuples*nbOfCompo];
6904   const int *iptr=getConstPointer();
6905   for(int i=0;i<nbTuples;i++)
6906     {
6907       int v=new2Old[i];
6908       if(v>=0 && v<nbTuples)
6909         std::copy(iptr+nbOfCompo*v,iptr+nbOfCompo*(v+1),tmp+nbOfCompo*i);
6910       else
6911         {
6912           std::ostringstream oss; oss << "DataArrayInt::renumberInPlaceR : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
6913           throw INTERP_KERNEL::Exception(oss.str().c_str());
6914         }
6915     }
6916   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
6917   delete [] tmp;
6918   declareAsNew();
6919 }
6920
6921 /*!
6922  * Returns a copy of \a this array with values permuted as required by \a old2New array.
6923  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ].
6924  * Number of tuples in the result array remains the same as in \this one.
6925  * If a permutation reduction is needed, renumberAndReduce() should be used.
6926  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
6927  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
6928  *          giving a new position for i-th old value.
6929  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
6930  *          is to delete using decrRef() as it is no more needed.
6931  *  \throw If \a this is not allocated.
6932  */
6933 DataArrayInt *DataArrayInt::renumber(const int *old2New) const
6934 {
6935   checkAllocated();
6936   int nbTuples=getNumberOfTuples();
6937   int nbOfCompo=getNumberOfComponents();
6938   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6939   ret->alloc(nbTuples,nbOfCompo);
6940   ret->copyStringInfoFrom(*this);
6941   const int *iptr=getConstPointer();
6942   int *optr=ret->getPointer();
6943   for(int i=0;i<nbTuples;i++)
6944     std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),optr+nbOfCompo*old2New[i]);
6945   ret->copyStringInfoFrom(*this);
6946   return ret.retn();
6947 }
6948
6949 /*!
6950  * Returns a copy of \a this array with values permuted as required by \a new2Old array.
6951  * The values are permuted so that  \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of
6952  * tuples in the result array remains the same as in \this one.
6953  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
6954  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
6955  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
6956  *     giving a previous position of i-th new value.
6957  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
6958  *          is to delete using decrRef() as it is no more needed.
6959  */
6960 DataArrayInt *DataArrayInt::renumberR(const int *new2Old) const
6961 {
6962   checkAllocated();
6963   int nbTuples=getNumberOfTuples();
6964   int nbOfCompo=getNumberOfComponents();
6965   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6966   ret->alloc(nbTuples,nbOfCompo);
6967   ret->copyStringInfoFrom(*this);
6968   const int *iptr=getConstPointer();
6969   int *optr=ret->getPointer();
6970   for(int i=0;i<nbTuples;i++)
6971     std::copy(iptr+nbOfCompo*new2Old[i],iptr+nbOfCompo*(new2Old[i]+1),optr+nbOfCompo*i);
6972   ret->copyStringInfoFrom(*this);
6973   return ret.retn();
6974 }
6975
6976 /*!
6977  * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
6978  * of size \a newNbOfTuple and it's values are permuted as required by \a old2New array.
6979  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ] for all
6980  * \a old2New[ i ] >= 0. In other words every i-th tuple in \a this array, for which 
6981  * \a old2New[ i ] is negative, is missing from the result array.
6982  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
6983  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
6984  *     giving a new position for i-th old tuple and giving negative position for
6985  *     for i-th old tuple that should be omitted.
6986  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
6987  *          is to delete using decrRef() as it is no more needed.
6988  */
6989 DataArrayInt *DataArrayInt::renumberAndReduce(const int *old2New, int newNbOfTuple) const
6990 {
6991   checkAllocated();
6992   int nbTuples=getNumberOfTuples();
6993   int nbOfCompo=getNumberOfComponents();
6994   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6995   ret->alloc(newNbOfTuple,nbOfCompo);
6996   const int *iptr=getConstPointer();
6997   int *optr=ret->getPointer();
6998   for(int i=0;i<nbTuples;i++)
6999     {
7000       int w=old2New[i];
7001       if(w>=0)
7002         std::copy(iptr+i*nbOfCompo,iptr+(i+1)*nbOfCompo,optr+w*nbOfCompo);
7003     }
7004   ret->copyStringInfoFrom(*this);
7005   return ret.retn();
7006 }
7007
7008 /*!
7009  * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
7010  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
7011  * \a new2OldBg array.
7012  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
7013  * This method is equivalent to renumberAndReduce() except that convention in input is
7014  * \c new2old and \b not \c old2new.
7015  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
7016  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
7017  *              tuple index in \a this array to fill the i-th tuple in the new array.
7018  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
7019  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
7020  *              \a new2OldBg <= \a pi < \a new2OldEnd.
7021  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7022  *          is to delete using decrRef() as it is no more needed.
7023  */
7024 DataArrayInt *DataArrayInt::selectByTupleId(const int *new2OldBg, const int *new2OldEnd) const
7025 {
7026   checkAllocated();
7027   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7028   int nbComp=getNumberOfComponents();
7029   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
7030   ret->copyStringInfoFrom(*this);
7031   int *pt=ret->getPointer();
7032   const int *srcPt=getConstPointer();
7033   int i=0;
7034   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
7035     std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
7036   ret->copyStringInfoFrom(*this);
7037   return ret.retn();
7038 }
7039
7040 /*!
7041  * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
7042  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
7043  * \a new2OldBg array.
7044  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
7045  * This method is equivalent to renumberAndReduce() except that convention in input is
7046  * \c new2old and \b not \c old2new.
7047  * This method is equivalent to selectByTupleId() except that it prevents coping data
7048  * from behind the end of \a this array.
7049  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
7050  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
7051  *              tuple index in \a this array to fill the i-th tuple in the new array.
7052  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
7053  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
7054  *              \a new2OldBg <= \a pi < \a new2OldEnd.
7055  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7056  *          is to delete using decrRef() as it is no more needed.
7057  *  \throw If \a new2OldEnd - \a new2OldBg > \a this->getNumberOfTuples().
7058  */
7059 DataArrayInt *DataArrayInt::selectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const
7060 {
7061   checkAllocated();
7062   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7063   int nbComp=getNumberOfComponents();
7064   int oldNbOfTuples=getNumberOfTuples();
7065   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
7066   ret->copyStringInfoFrom(*this);
7067   int *pt=ret->getPointer();
7068   const int *srcPt=getConstPointer();
7069   int i=0;
7070   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
7071     if(*w>=0 && *w<oldNbOfTuples)
7072       std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
7073     else
7074       throw INTERP_KERNEL::Exception("DataArrayInt::selectByTupleIdSafe : some ids has been detected to be out of [0,this->getNumberOfTuples) !");
7075   ret->copyStringInfoFrom(*this);
7076   return ret.retn();
7077 }
7078
7079 /*!
7080  * Returns a shorten copy of \a this array. The new DataArrayInt contains every
7081  * (\a bg + \c i * \a step)-th tuple of \a this array located before the \a end2-th
7082  * tuple. Indices of the selected tuples are the same as ones returned by the Python
7083  * command \c range( \a bg, \a end2, \a step ).
7084  * This method is equivalent to selectByTupleIdSafe() except that the input array is
7085  * not constructed explicitly.
7086  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
7087  *  \param [in] bg - index of the first tuple to copy from \a this array.
7088  *  \param [in] end2 - index of the tuple before which the tuples to copy are located.
7089  *  \param [in] step - index increment to get index of the next tuple to copy.
7090  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7091  *          is to delete using decrRef() as it is no more needed.
7092  *  \sa DataArrayInt::substr.
7093  */
7094 DataArrayInt *DataArrayInt::selectByTupleId2(int bg, int end2, int step) const
7095 {
7096   checkAllocated();
7097   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7098   int nbComp=getNumberOfComponents();
7099   int newNbOfTuples=GetNumberOfItemGivenBESRelative(bg,end2,step,"DataArrayInt::selectByTupleId2 : ");
7100   ret->alloc(newNbOfTuples,nbComp);
7101   int *pt=ret->getPointer();
7102   const int *srcPt=getConstPointer()+bg*nbComp;
7103   for(int i=0;i<newNbOfTuples;i++,srcPt+=step*nbComp)
7104     std::copy(srcPt,srcPt+nbComp,pt+i*nbComp);
7105   ret->copyStringInfoFrom(*this);
7106   return ret.retn();
7107 }
7108
7109 /*!
7110  * Returns a shorten copy of \a this array. The new DataArrayInt contains ranges
7111  * of tuples specified by \a ranges parameter.
7112  * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
7113  *  \param [in] ranges - std::vector of std::pair's each of which defines a range
7114  *              of tuples in [\c begin,\c end) format.
7115  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7116  *          is to delete using decrRef() as it is no more needed.
7117  *  \throw If \a end < \a begin.
7118  *  \throw If \a end > \a this->getNumberOfTuples().
7119  *  \throw If \a this is not allocated.
7120  */
7121 DataArray *DataArrayInt::selectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const
7122 {
7123   checkAllocated();
7124   int nbOfComp=getNumberOfComponents();
7125   int nbOfTuplesThis=getNumberOfTuples();
7126   if(ranges.empty())
7127     {
7128       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7129       ret->alloc(0,nbOfComp);
7130       ret->copyStringInfoFrom(*this);
7131       return ret.retn();
7132     }
7133   int ref=ranges.front().first;
7134   int nbOfTuples=0;
7135   bool isIncreasing=true;
7136   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
7137     {
7138       if((*it).first<=(*it).second)
7139         {
7140           if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
7141             {
7142               nbOfTuples+=(*it).second-(*it).first;
7143               if(isIncreasing)
7144                 isIncreasing=ref<=(*it).first;
7145               ref=(*it).second;
7146             }
7147           else
7148             {
7149               std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
7150               oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !";
7151               throw INTERP_KERNEL::Exception(oss.str().c_str());
7152             }
7153         }
7154       else
7155         {
7156           std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
7157           oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !";
7158           throw INTERP_KERNEL::Exception(oss.str().c_str());
7159         }
7160     }
7161   if(isIncreasing && nbOfTuplesThis==nbOfTuples)
7162     return deepCpy();
7163   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7164   ret->alloc(nbOfTuples,nbOfComp);
7165   ret->copyStringInfoFrom(*this);
7166   const int *src=getConstPointer();
7167   int *work=ret->getPointer();
7168   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
7169     work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work);
7170   return ret.retn();
7171 }
7172
7173 /*!
7174  * Returns a new DataArrayInt containing a renumbering map in "Old to New" mode.
7175  * This map, if applied to \a this array, would make it sorted. For example, if
7176  * \a this array contents are [9,10,0,6,4,11,3,7] then the contents of the result array
7177  * are [5,6,0,3,2,7,1,4]; if this result array (\a res) is used as an argument in call
7178  * \a this->renumber(\a res) then the returned array contains [0,3,4,6,7,9,10,11].
7179  * This method is useful for renumbering (in MED file for example). For more info
7180  * on renumbering see \ref MEDCouplingArrayRenumbering.
7181  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7182  *          array using decrRef() as it is no more needed.
7183  *  \throw If \a this is not allocated.
7184  *  \throw If \a this->getNumberOfComponents() != 1.
7185  *  \throw If there are equal values in \a this array.
7186  */
7187 DataArrayInt *DataArrayInt::checkAndPreparePermutation() const
7188 {
7189   checkAllocated();
7190   if(getNumberOfComponents()!=1)
7191     throw INTERP_KERNEL::Exception("DataArrayInt::checkAndPreparePermutation : number of components must == 1 !");
7192   int nbTuples=getNumberOfTuples();
7193   const int *pt=getConstPointer();
7194   int *pt2=CheckAndPreparePermutation(pt,pt+nbTuples);
7195   DataArrayInt *ret=DataArrayInt::New();
7196   ret->useArray(pt2,true,C_DEALLOC,nbTuples,1);
7197   return ret;
7198 }
7199
7200 /*!
7201  * This method tries to find the permutation to apply to the first input \a ids1 to obtain the same array (without considering strings informations) the second
7202  * input array \a ids2.
7203  * \a ids1 and \a ids2 are expected to be both a list of ids (both with number of components equal to one) not sorted and with values that can be negative.
7204  * This method will throw an exception is no such permutation array can be obtained. It is typically the case if there is some ids in \a ids1 not in \a ids2 or
7205  * inversely.
7206  * In case of success (no throw) : \c ids1->renumber(ret)->isEqual(ids2) where \a ret is the return of this method.
7207  *
7208  * \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7209  *          array using decrRef() as it is no more needed.
7210  * \throw If either ids1 or ids2 is null not allocated or not with one components.
7211  * 
7212  */
7213 DataArrayInt *DataArrayInt::FindPermutationFromFirstToSecond(const DataArrayInt *ids1, const DataArrayInt *ids2)
7214 {
7215   if(!ids1 || !ids2)
7216     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two input arrays must be not null !");
7217   if(!ids1->isAllocated() || !ids2->isAllocated())
7218     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two input arrays must be allocated !");
7219   if(ids1->getNumberOfComponents()!=1 || ids2->getNumberOfComponents()!=1)
7220     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two input arrays have exactly one component !");
7221   if(ids1->getNumberOfTuples()!=ids2->getNumberOfTuples())
7222     {
7223       std::ostringstream oss; oss << "DataArrayInt::FindPermutationFromFirstToSecond : first array has " << ids1->getNumberOfTuples() << " tuples and the second one " << ids2->getNumberOfTuples() << " tuples ! No chance to find a permutation between the 2 arrays !";
7224       throw INTERP_KERNEL::Exception(oss.str().c_str());
7225     }
7226   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(ids1->deepCpy());
7227   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p2(ids2->deepCpy());
7228   p1->sort(true); p2->sort(true);
7229   if(!p1->isEqualWithoutConsideringStr(*p2))
7230     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two arrays are not lying on same ids ! Impossible to find a permutation between the 2 arrays !");
7231   p1=ids1->checkAndPreparePermutation();
7232   p2=ids2->checkAndPreparePermutation();
7233   p2=p2->invertArrayO2N2N2O(p2->getNumberOfTuples());
7234   p2=p2->selectByTupleIdSafe(p1->begin(),p1->end());
7235   return p2.retn();
7236 }
7237
7238 /*!
7239  * Returns two arrays describing a surjective mapping from \a this set of values (\a A)
7240  * onto a set of values of size \a targetNb (\a B). The surjective function is 
7241  * \a B[ \a A[ i ]] = i. That is to say that for each \a id in [0,\a targetNb), where \a
7242  * targetNb < \a this->getNumberOfTuples(), there exists at least one tupleId (\a tid) so
7243  * that <em> this->getIJ( tid, 0 ) == id</em>. <br>
7244  * The first of out arrays returns indices of elements of \a this array, grouped by their
7245  * place in the set \a B. The second out array is the index of the first one; it shows how
7246  * many elements of \a A are mapped into each element of \a B. <br>
7247  * For more info on
7248  * mapping and its usage in renumbering see \ref MEDCouplingArrayRenumbering. <br>
7249  * \b Example:
7250  * - \a this: [0,3,2,3,2,2,1,2]
7251  * - \a targetNb: 4
7252  * - \a arr:  [0,  6,  2,4,5,7,  1,3]
7253  * - \a arrI: [0,1,2,6,8]
7254  *
7255  * This result means: <br>
7256  * the element of \a B 0 encounters within \a A once (\a arrI[ 0+1 ] - \a arrI[ 0 ]) and
7257  * its index within \a A is 0 ( \a arr[ 0:1 ] == \a arr[ \a arrI[ 0 ] : \a arrI[ 0+1 ]]);<br>
7258  * the element of \a B 2 encounters within \a A 4 times (\a arrI[ 2+1 ] - \a arrI[ 2 ]) and
7259  * its indices within \a A are [2,4,5,7] ( \a arr[ 2:6 ] == \a arr[ \a arrI[ 2 ] : 
7260  * \a arrI[ 2+1 ]]); <br> etc.
7261  *  \param [in] targetNb - the size of the set \a B. \a targetNb must be equal or more
7262  *         than the maximal value of \a A.
7263  *  \param [out] arr - a new instance of DataArrayInt returning indices of
7264  *         elements of \a this, grouped by their place in the set \a B. The caller is to delete
7265  *         this array using decrRef() as it is no more needed.
7266  *  \param [out] arrI - a new instance of DataArrayInt returning size of groups of equal
7267  *         elements of \a this. The caller is to delete this array using decrRef() as it
7268  *         is no more needed.
7269  *  \throw If \a this is not allocated.
7270  *  \throw If \a this->getNumberOfComponents() != 1.
7271  *  \throw If any value in \a this is more or equal to \a targetNb.
7272  */
7273 void DataArrayInt::changeSurjectiveFormat(int targetNb, DataArrayInt *&arr, DataArrayInt *&arrI) const
7274 {
7275   checkAllocated();
7276   if(getNumberOfComponents()!=1)
7277     throw INTERP_KERNEL::Exception("DataArrayInt::changeSurjectiveFormat : number of components must == 1 !");
7278   int nbOfTuples=getNumberOfTuples();
7279   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
7280   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> retI(DataArrayInt::New());
7281   retI->alloc(targetNb+1,1);
7282   const int *input=getConstPointer();
7283   std::vector< std::vector<int> > tmp(targetNb);
7284   for(int i=0;i<nbOfTuples;i++)
7285     {
7286       int tmp2=input[i];
7287       if(tmp2>=0 && tmp2<targetNb)
7288         tmp[tmp2].push_back(i);
7289       else
7290         {
7291           std::ostringstream oss; oss << "DataArrayInt::changeSurjectiveFormat : At pos " << i << " presence of element " << tmp2 << " ! should be in [0," << targetNb << ") !";
7292           throw INTERP_KERNEL::Exception(oss.str().c_str());
7293         }
7294     }
7295   int *retIPtr=retI->getPointer();
7296   *retIPtr=0;
7297   for(std::vector< std::vector<int> >::const_iterator it1=tmp.begin();it1!=tmp.end();it1++,retIPtr++)
7298     retIPtr[1]=retIPtr[0]+(int)((*it1).size());
7299   if(nbOfTuples!=retI->getIJ(targetNb,0))
7300     throw INTERP_KERNEL::Exception("DataArrayInt::changeSurjectiveFormat : big problem should never happen !");
7301   ret->alloc(nbOfTuples,1);
7302   int *retPtr=ret->getPointer();
7303   for(std::vector< std::vector<int> >::const_iterator it1=tmp.begin();it1!=tmp.end();it1++)
7304     retPtr=std::copy((*it1).begin(),(*it1).end(),retPtr);
7305   arr=ret.retn();
7306   arrI=retI.retn();
7307 }
7308
7309
7310 /*!
7311  * Returns a new DataArrayInt containing a renumbering map in "Old to New" mode computed
7312  * from a zip representation of a surjective format (returned e.g. by
7313  * \ref ParaMEDMEM::DataArrayDouble::findCommonTuples() "DataArrayDouble::findCommonTuples()"
7314  * for example). The result array minimizes the permutation. <br>
7315  * For more info on renumbering see \ref MEDCouplingArrayRenumbering. <br>
7316  * \b Example: <br>
7317  * - \a nbOfOldTuples: 10 
7318  * - \a arr          : [0,3, 5,7,9]
7319  * - \a arrIBg       : [0,2,5]
7320  * - \a newNbOfTuples: 7
7321  * - result array    : [0,1,2,0,3,4,5,4,6,4]
7322  *
7323  *  \param [in] nbOfOldTuples - number of tuples in the initial array \a arr.
7324  *  \param [in] arr - the array of tuple indices grouped by \a arrIBg array.
7325  *  \param [in] arrIBg - the array dividing all indices stored in \a arr into groups of
7326  *         (indices of) equal values. Its every element (except the last one) points to
7327  *         the first element of a group of equal values.
7328  *  \param [in] arrIEnd - specifies the end of \a arrIBg, so that the last element of \a
7329  *          arrIBg is \a arrIEnd[ -1 ].
7330  *  \param [out] newNbOfTuples - number of tuples after surjection application.
7331  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7332  *          array using decrRef() as it is no more needed.
7333  *  \throw If any value of \a arr breaks condition ( 0 <= \a arr[ i ] < \a nbOfOldTuples ).
7334  */
7335 DataArrayInt *DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTuples, const int *arr, const int *arrIBg, const int *arrIEnd, int &newNbOfTuples)
7336 {
7337   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7338   ret->alloc(nbOfOldTuples,1);
7339   int *pt=ret->getPointer();
7340   std::fill(pt,pt+nbOfOldTuples,-1);
7341   int nbOfGrps=((int)std::distance(arrIBg,arrIEnd))-1;
7342   const int *cIPtr=arrIBg;
7343   for(int i=0;i<nbOfGrps;i++)
7344     pt[arr[cIPtr[i]]]=-(i+2);
7345   int newNb=0;
7346   for(int iNode=0;iNode<nbOfOldTuples;iNode++)
7347     {
7348       if(pt[iNode]<0)
7349         {
7350           if(pt[iNode]==-1)
7351             pt[iNode]=newNb++;
7352           else
7353             {
7354               int grpId=-(pt[iNode]+2);
7355               for(int j=cIPtr[grpId];j<cIPtr[grpId+1];j++)
7356                 {
7357                   if(arr[j]>=0 && arr[j]<nbOfOldTuples)
7358                     pt[arr[j]]=newNb;
7359                   else
7360                     {
7361                       std::ostringstream oss; oss << "DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2 : With element #" << j << " value is " << arr[j] << " should be in [0," << nbOfOldTuples << ") !";
7362                       throw INTERP_KERNEL::Exception(oss.str().c_str());
7363                     }
7364                 }
7365               newNb++;
7366             }
7367         }
7368     }
7369   newNbOfTuples=newNb;
7370   return ret.retn();
7371 }
7372
7373 /*!
7374  * Returns a new DataArrayInt containing a renumbering map in "New to Old" mode,
7375  * which if applied to \a this array would make it sorted ascendingly.
7376  * For more info on renumbering see \ref MEDCouplingArrayRenumbering. <br>
7377  * \b Example: <br>
7378  * - \a this: [2,0,1,1,0,1,2,0,1,1,0,0]
7379  * - result: [10,0,5,6,1,7,11,2,8,9,3,4]
7380  * - after applying result to \a this: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] 
7381  *
7382  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7383  *          array using decrRef() as it is no more needed.
7384  *  \throw If \a this is not allocated.
7385  *  \throw If \a this->getNumberOfComponents() != 1.
7386  */
7387 DataArrayInt *DataArrayInt::buildPermArrPerLevel() const
7388 {
7389   checkAllocated();
7390   if(getNumberOfComponents()!=1)
7391     throw INTERP_KERNEL::Exception("DataArrayInt::buildPermArrPerLevel : number of components must == 1 !");
7392   int nbOfTuples=getNumberOfTuples();
7393   const int *pt=getConstPointer();
7394   std::map<int,int> m;
7395   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7396   ret->alloc(nbOfTuples,1);
7397   int *opt=ret->getPointer();
7398   for(int i=0;i<nbOfTuples;i++,pt++,opt++)
7399     {
7400       int val=*pt;
7401       std::map<int,int>::iterator it=m.find(val);
7402       if(it!=m.end())
7403         {
7404           *opt=(*it).second;
7405           (*it).second++;
7406         }
7407       else
7408         {
7409           *opt=0;
7410           m.insert(std::pair<int,int>(val,1));
7411         }
7412     }
7413   int sum=0;
7414   for(std::map<int,int>::iterator it=m.begin();it!=m.end();it++)
7415     {
7416       int vt=(*it).second;
7417       (*it).second=sum;
7418       sum+=vt;
7419     }
7420   pt=getConstPointer();
7421   opt=ret->getPointer();
7422   for(int i=0;i<nbOfTuples;i++,pt++,opt++)
7423     *opt+=m[*pt];
7424   //
7425   return ret.retn();
7426 }
7427
7428 /*!
7429  * Checks if contents of \a this array are equal to that of an array filled with
7430  * iota(). This method is particularly useful for DataArrayInt instances that represent
7431  * a renumbering array to check the real need in renumbering. 
7432  *  \return bool - \a true if \a this array contents == \a range( \a this->getNumberOfTuples())
7433  *  \throw If \a this is not allocated.
7434  *  \throw If \a this->getNumberOfComponents() != 1.
7435  */
7436 bool DataArrayInt::isIdentity() const
7437 {
7438   checkAllocated();
7439   if(getNumberOfComponents()!=1)
7440     return false;
7441   int nbOfTuples=getNumberOfTuples();
7442   const int *pt=getConstPointer();
7443   for(int i=0;i<nbOfTuples;i++,pt++)
7444     if(*pt!=i)
7445       return false;
7446   return true;
7447 }
7448
7449 /*!
7450  * Checks if all values in \a this array are equal to \a val.
7451  *  \param [in] val - value to check equality of array values to.
7452  *  \return bool - \a true if all values are \a val.
7453  *  \throw If \a this is not allocated.
7454  *  \throw If \a this->getNumberOfComponents() != 1
7455  */
7456 bool DataArrayInt::isUniform(int val) const
7457 {
7458   checkAllocated();
7459   if(getNumberOfComponents()!=1)
7460     throw INTERP_KERNEL::Exception("DataArrayInt::isUniform : must be applied on DataArrayInt with only one component, you can call 'rearrange' method before !");
7461   int nbOfTuples=getNumberOfTuples();
7462   const int *w=getConstPointer();
7463   const int *end2=w+nbOfTuples;
7464   for(;w!=end2;w++)
7465     if(*w!=val)
7466       return false;
7467   return true;
7468 }
7469
7470 /*!
7471  * Creates a new DataArrayDouble and assigns all (textual and numerical) data of \a this
7472  * array to the new one.
7473  *  \return DataArrayDouble * - the new instance of DataArrayInt.
7474  */
7475 DataArrayDouble *DataArrayInt::convertToDblArr() const
7476 {
7477   checkAllocated();
7478   DataArrayDouble *ret=DataArrayDouble::New();
7479   ret->alloc(getNumberOfTuples(),getNumberOfComponents());
7480   std::size_t nbOfVals=getNbOfElems();
7481   const int *src=getConstPointer();
7482   double *dest=ret->getPointer();
7483   std::copy(src,src+nbOfVals,dest);
7484   ret->copyStringInfoFrom(*this);
7485   return ret;
7486 }
7487
7488 /*!
7489  * Returns a shorten copy of \a this array. The new DataArrayInt contains all
7490  * tuples starting from the \a tupleIdBg-th tuple and including all tuples located before
7491  * the \a tupleIdEnd-th one. This methods has a similar behavior as std::string::substr().
7492  * This method is a specialization of selectByTupleId2().
7493  *  \param [in] tupleIdBg - index of the first tuple to copy from \a this array.
7494  *  \param [in] tupleIdEnd - index of the tuple before which the tuples to copy are located.
7495  *          If \a tupleIdEnd == -1, all the tuples till the end of \a this array are copied.
7496  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7497  *          is to delete using decrRef() as it is no more needed.
7498  *  \throw If \a tupleIdBg < 0.
7499  *  \throw If \a tupleIdBg > \a this->getNumberOfTuples().
7500     \throw If \a tupleIdEnd != -1 && \a tupleIdEnd < \a this->getNumberOfTuples().
7501  *  \sa DataArrayInt::selectByTupleId2
7502  */
7503 DataArrayInt *DataArrayInt::substr(int tupleIdBg, int tupleIdEnd) const
7504 {
7505   checkAllocated();
7506   int nbt=getNumberOfTuples();
7507   if(tupleIdBg<0)
7508     throw INTERP_KERNEL::Exception("DataArrayInt::substr : The tupleIdBg parameter must be greater than 0 !");
7509   if(tupleIdBg>nbt)
7510     throw INTERP_KERNEL::Exception("DataArrayInt::substr : The tupleIdBg parameter is greater than number of tuples !");
7511   int trueEnd=tupleIdEnd;
7512   if(tupleIdEnd!=-1)
7513     {
7514       if(tupleIdEnd>nbt)
7515         throw INTERP_KERNEL::Exception("DataArrayInt::substr : The tupleIdBg parameter is greater or equal than number of tuples !");
7516     }
7517   else
7518     trueEnd=nbt;
7519   int nbComp=getNumberOfComponents();
7520   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7521   ret->alloc(trueEnd-tupleIdBg,nbComp);
7522   ret->copyStringInfoFrom(*this);
7523   std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer());
7524   return ret.retn();
7525 }
7526
7527 /*!
7528  * Changes the number of components within \a this array so that its raw data **does
7529  * not** change, instead splitting this data into tuples changes.
7530  *  \warning This method erases all (name and unit) component info set before!
7531  *  \param [in] newNbOfComp - number of components for \a this array to have.
7532  *  \throw If \a this is not allocated
7533  *  \throw If getNbOfElems() % \a newNbOfCompo != 0.
7534  *  \throw If \a newNbOfCompo is lower than 1.
7535  *  \throw If the rearrange method would lead to a number of tuples higher than 2147483647 (maximal capacity of int32 !).
7536  *  \warning This method erases all (name and unit) component info set before!
7537  */
7538 void DataArrayInt::rearrange(int newNbOfCompo)
7539 {
7540   checkAllocated();
7541   if(newNbOfCompo<1)
7542     throw INTERP_KERNEL::Exception("DataArrayInt::rearrange : input newNbOfCompo must be > 0 !");
7543   std::size_t nbOfElems=getNbOfElems();
7544   if(nbOfElems%newNbOfCompo!=0)
7545     throw INTERP_KERNEL::Exception("DataArrayInt::rearrange : nbOfElems%newNbOfCompo!=0 !");
7546   if(nbOfElems/newNbOfCompo>(std::size_t)std::numeric_limits<int>::max())
7547     throw INTERP_KERNEL::Exception("DataArrayInt::rearrange : the rearrangement leads to too high number of tuples (> 2147483647) !");
7548   _info_on_compo.clear();
7549   _info_on_compo.resize(newNbOfCompo);
7550   declareAsNew();
7551 }
7552
7553 /*!
7554  * Changes the number of components within \a this array to be equal to its number
7555  * of tuples, and inversely its number of tuples to become equal to its number of 
7556  * components. So that its raw data **does not** change, instead splitting this
7557  * data into tuples changes.
7558  *  \warning This method erases all (name and unit) component info set before!
7559  *  \warning Do not confuse this method with fromNoInterlace() and toNoInterlace()!
7560  *  \throw If \a this is not allocated.
7561  *  \sa rearrange()
7562  */
7563 void DataArrayInt::transpose()
7564 {
7565   checkAllocated();
7566   int nbOfTuples=getNumberOfTuples();
7567   rearrange(nbOfTuples);
7568 }
7569
7570 /*!
7571  * Returns a shorten or extended copy of \a this array. If \a newNbOfComp is less
7572  * than \a this->getNumberOfComponents() then the result array is shorten as each tuple
7573  * is truncated to have \a newNbOfComp components, keeping first components. If \a
7574  * newNbOfComp is more than \a this->getNumberOfComponents() then the result array is
7575  * expanded as each tuple is populated with \a dftValue to have \a newNbOfComp
7576  * components.  
7577  *  \param [in] newNbOfComp - number of components for the new array to have.
7578  *  \param [in] dftValue - value assigned to new values added to the new array.
7579  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
7580  *          is to delete using decrRef() as it is no more needed.
7581  *  \throw If \a this is not allocated.
7582  */
7583 DataArrayInt *DataArrayInt::changeNbOfComponents(int newNbOfComp, int dftValue) const
7584 {
7585   checkAllocated();
7586   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7587   ret->alloc(getNumberOfTuples(),newNbOfComp);
7588   const int *oldc=getConstPointer();
7589   int *nc=ret->getPointer();
7590   int nbOfTuples=getNumberOfTuples();
7591   int oldNbOfComp=getNumberOfComponents();
7592   int dim=std::min(oldNbOfComp,newNbOfComp);
7593   for(int i=0;i<nbOfTuples;i++)
7594     {
7595       int j=0;
7596       for(;j<dim;j++)
7597         nc[newNbOfComp*i+j]=oldc[i*oldNbOfComp+j];
7598       for(;j<newNbOfComp;j++)
7599         nc[newNbOfComp*i+j]=dftValue;
7600     }
7601   ret->setName(getName());
7602   for(int i=0;i<dim;i++)
7603     ret->setInfoOnComponent(i,getInfoOnComponent(i));
7604   ret->setName(getName());
7605   return ret.retn();
7606 }
7607
7608 /*!
7609  * Changes number of tuples in the array. If the new number of tuples is smaller
7610  * than the current number the array is truncated, otherwise the array is extended.
7611  *  \param [in] nbOfTuples - new number of tuples. 
7612  *  \throw If \a this is not allocated.
7613  *  \throw If \a nbOfTuples is negative.
7614  */
7615 void DataArrayInt::reAlloc(int nbOfTuples)
7616 {
7617   if(nbOfTuples<0)
7618     throw INTERP_KERNEL::Exception("DataArrayInt::reAlloc : input new number of tuples should be >=0 !");
7619   checkAllocated();
7620   _mem.reAlloc(getNumberOfComponents()*(std::size_t)nbOfTuples);
7621   declareAsNew();
7622 }
7623
7624
7625 /*!
7626  * Returns a copy of \a this array composed of selected components.
7627  * The new DataArrayInt has the same number of tuples but includes components
7628  * specified by \a compoIds parameter. So that getNbOfElems() of the result array
7629  * can be either less, same or more than \a this->getNbOfElems().
7630  *  \param [in] compoIds - sequence of zero based indices of components to include
7631  *              into the new array.
7632  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7633  *          is to delete using decrRef() as it is no more needed.
7634  *  \throw If \a this is not allocated.
7635  *  \throw If a component index (\a i) is not valid: 
7636  *         \a i < 0 || \a i >= \a this->getNumberOfComponents().
7637  *
7638  *  \if ENABLE_EXAMPLES
7639  *  \ref py_mcdataarrayint_keepselectedcomponents "Here is a Python example".
7640  *  \endif
7641  */
7642 DataArray *DataArrayInt::keepSelectedComponents(const std::vector<int>& compoIds) const
7643 {
7644   checkAllocated();
7645   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
7646   int newNbOfCompo=(int)compoIds.size();
7647   int oldNbOfCompo=getNumberOfComponents();
7648   for(std::vector<int>::const_iterator it=compoIds.begin();it!=compoIds.end();it++)
7649     DataArray::CheckValueInRange(oldNbOfCompo,(*it),"keepSelectedComponents invalid requested component");
7650   int nbOfTuples=getNumberOfTuples();
7651   ret->alloc(nbOfTuples,newNbOfCompo);
7652   ret->copyPartOfStringInfoFrom(*this,compoIds);
7653   const int *oldc=getConstPointer();
7654   int *nc=ret->getPointer();
7655   for(int i=0;i<nbOfTuples;i++)
7656     for(int j=0;j<newNbOfCompo;j++,nc++)
7657       *nc=oldc[i*oldNbOfCompo+compoIds[j]];
7658   return ret.retn();
7659 }
7660
7661 /*!
7662  * Appends components of another array to components of \a this one, tuple by tuple.
7663  * So that the number of tuples of \a this array remains the same and the number of 
7664  * components increases.
7665  *  \param [in] other - the DataArrayInt to append to \a this one.
7666  *  \throw If \a this is not allocated.
7667  *  \throw If \a this and \a other arrays have different number of tuples.
7668  *
7669  *  \if ENABLE_EXAMPLES
7670  *  \ref cpp_mcdataarrayint_meldwith "Here is a C++ example".
7671  *
7672  *  \ref py_mcdataarrayint_meldwith "Here is a Python example".
7673  *  \endif
7674  */
7675 void DataArrayInt::meldWith(const DataArrayInt *other)
7676 {
7677   if(!other)
7678     throw INTERP_KERNEL::Exception("DataArrayInt::meldWith : DataArrayInt pointer in input is NULL !");
7679   checkAllocated();
7680   other->checkAllocated();
7681   int nbOfTuples=getNumberOfTuples();
7682   if(nbOfTuples!=other->getNumberOfTuples())
7683     throw INTERP_KERNEL::Exception("DataArrayInt::meldWith : mismatch of number of tuples !");
7684   int nbOfComp1=getNumberOfComponents();
7685   int nbOfComp2=other->getNumberOfComponents();
7686   int *newArr=(int *)malloc(nbOfTuples*(nbOfComp1+nbOfComp2)*sizeof(int));
7687   int *w=newArr;
7688   const int *inp1=getConstPointer();
7689   const int *inp2=other->getConstPointer();
7690   for(int i=0;i<nbOfTuples;i++,inp1+=nbOfComp1,inp2+=nbOfComp2)
7691     {
7692       w=std::copy(inp1,inp1+nbOfComp1,w);
7693       w=std::copy(inp2,inp2+nbOfComp2,w);
7694     }
7695   useArray(newArr,true,C_DEALLOC,nbOfTuples,nbOfComp1+nbOfComp2);
7696   std::vector<int> compIds(nbOfComp2);
7697   for(int i=0;i<nbOfComp2;i++)
7698     compIds[i]=nbOfComp1+i;
7699   copyPartOfStringInfoFrom2(compIds,*other);
7700 }
7701
7702 /*!
7703  * Copy all components in a specified order from another DataArrayInt.
7704  * The specified components become the first ones in \a this array.
7705  * Both numerical and textual data is copied. The number of tuples in \a this and
7706  * the other array can be different.
7707  *  \param [in] a - the array to copy data from.
7708  *  \param [in] compoIds - sequence of zero based indices of components, data of which is
7709  *              to be copied.
7710  *  \throw If \a a is NULL.
7711  *  \throw If \a compoIds.size() != \a a->getNumberOfComponents().
7712  *  \throw If \a compoIds[i] < 0 or \a compoIds[i] > \a this->getNumberOfComponents().
7713  *
7714  *  \if ENABLE_EXAMPLES
7715  *  \ref py_mcdataarrayint_setselectedcomponents "Here is a Python example".
7716  *  \endif
7717  */
7718 void DataArrayInt::setSelectedComponents(const DataArrayInt *a, const std::vector<int>& compoIds)
7719 {
7720   if(!a)
7721     throw INTERP_KERNEL::Exception("DataArrayInt::setSelectedComponents : input DataArrayInt is NULL !");
7722   checkAllocated();
7723   a->checkAllocated();
7724   copyPartOfStringInfoFrom2(compoIds,*a);
7725   std::size_t partOfCompoSz=compoIds.size();
7726   int nbOfCompo=getNumberOfComponents();
7727   int nbOfTuples=std::min(getNumberOfTuples(),a->getNumberOfTuples());
7728   const int *ac=a->getConstPointer();
7729   int *nc=getPointer();
7730   for(int i=0;i<nbOfTuples;i++)
7731     for(std::size_t j=0;j<partOfCompoSz;j++,ac++)
7732       nc[nbOfCompo*i+compoIds[j]]=*ac;
7733 }
7734
7735 /*!
7736  * Copy all values from another DataArrayInt into specified tuples and components
7737  * of \a this array. Textual data is not copied.
7738  * The tree parameters defining set of indices of tuples and components are similar to
7739  * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
7740  *  \param [in] a - the array to copy values from.
7741  *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
7742  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
7743  *              are located.
7744  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
7745  *  \param [in] bgComp - index of the first component of \a this array to assign values to.
7746  *  \param [in] endComp - index of the component before which the components to assign
7747  *              to are located.
7748  *  \param [in] stepComp - index increment to get index of the next component to assign to.
7749  *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
7750  *              must be equal to the number of columns to assign to, else an
7751  *              exception is thrown; if \a false, then it is only required that \a
7752  *              a->getNbOfElems() equals to number of values to assign to (this condition
7753  *              must be respected even if \a strictCompoCompare is \a true). The number of 
7754  *              values to assign to is given by following Python expression:
7755  *              \a nbTargetValues = 
7756  *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
7757  *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
7758  *  \throw If \a a is NULL.
7759  *  \throw If \a a is not allocated.
7760  *  \throw If \a this is not allocated.
7761  *  \throw If parameters specifying tuples and components to assign to do not give a
7762  *            non-empty range of increasing indices.
7763  *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
7764  *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
7765  *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
7766  *
7767  *  \if ENABLE_EXAMPLES
7768  *  \ref py_mcdataarrayint_setpartofvalues1 "Here is a Python example".
7769  *  \endif
7770  */
7771 void DataArrayInt::setPartOfValues1(const DataArrayInt *a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
7772 {
7773   if(!a)
7774     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues1 : DataArrayInt pointer in input is NULL !");
7775   const char msg[]="DataArrayInt::setPartOfValues1";
7776   checkAllocated();
7777   a->checkAllocated();
7778   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
7779   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
7780   int nbComp=getNumberOfComponents();
7781   int nbOfTuples=getNumberOfTuples();
7782   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
7783   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
7784   bool assignTech=true;
7785   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
7786     {
7787       if(strictCompoCompare)
7788         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
7789     }
7790   else
7791     {
7792       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
7793       assignTech=false;
7794     }
7795   int *pt=getPointer()+bgTuples*nbComp+bgComp;
7796   const int *srcPt=a->getConstPointer();
7797   if(assignTech)
7798     {
7799       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
7800         for(int j=0;j<newNbOfComp;j++,srcPt++)
7801           pt[j*stepComp]=*srcPt;
7802     }
7803   else
7804     {
7805       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
7806         {
7807           const int *srcPt2=srcPt;
7808           for(int j=0;j<newNbOfComp;j++,srcPt2++)
7809             pt[j*stepComp]=*srcPt2;
7810         }
7811     }
7812 }
7813
7814 /*!
7815  * Assign a given value to values at specified tuples and components of \a this array.
7816  * The tree parameters defining set of indices of tuples and components are similar to
7817  * the tree parameters of the Python function \c range(\c start,\c stop,\c step)..
7818  *  \param [in] a - the value to assign.
7819  *  \param [in] bgTuples - index of the first tuple of \a this array to assign to.
7820  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
7821  *              are located.
7822  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
7823  *  \param [in] bgComp - index of the first component of \a this array to assign to.
7824  *  \param [in] endComp - index of the component before which the components to assign
7825  *              to are located.
7826  *  \param [in] stepComp - index increment to get index of the next component to assign to.
7827  *  \throw If \a this is not allocated.
7828  *  \throw If parameters specifying tuples and components to assign to, do not give a
7829  *            non-empty range of increasing indices or indices are out of a valid range
7830  *            for \this array.
7831  *
7832  *  \if ENABLE_EXAMPLES
7833  *  \ref py_mcdataarrayint_setpartofvaluessimple1 "Here is a Python example".
7834  *  \endif
7835  */
7836 void DataArrayInt::setPartOfValuesSimple1(int a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp)
7837 {
7838   const char msg[]="DataArrayInt::setPartOfValuesSimple1";
7839   checkAllocated();
7840   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
7841   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
7842   int nbComp=getNumberOfComponents();
7843   int nbOfTuples=getNumberOfTuples();
7844   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
7845   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
7846   int *pt=getPointer()+bgTuples*nbComp+bgComp;
7847   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
7848     for(int j=0;j<newNbOfComp;j++)
7849       pt[j*stepComp]=a;
7850 }
7851
7852
7853 /*!
7854  * Copy all values from another DataArrayInt (\a a) into specified tuples and 
7855  * components of \a this array. Textual data is not copied.
7856  * The tuples and components to assign to are defined by C arrays of indices.
7857  * There are two *modes of usage*:
7858  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
7859  *   of \a a is assigned to its own location within \a this array. 
7860  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
7861  *   components of every specified tuple of \a this array. In this mode it is required
7862  *   that \a a->getNumberOfComponents() equals to the number of specified components.
7863  * 
7864  *  \param [in] a - the array to copy values from.
7865  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
7866  *              assign values of \a a to.
7867  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
7868  *              pointer to a tuple index <em>(pi)</em> varies as this: 
7869  *              \a bgTuples <= \a pi < \a endTuples.
7870  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
7871  *              assign values of \a a to.
7872  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
7873  *              pointer to a component index <em>(pi)</em> varies as this: 
7874  *              \a bgComp <= \a pi < \a endComp.
7875  *  \param [in] strictCompoCompare - this parameter is checked only if the
7876  *               *mode of usage* is the first; if it is \a true (default), 
7877  *               then \a a->getNumberOfComponents() must be equal 
7878  *               to the number of specified columns, else this is not required.
7879  *  \throw If \a a is NULL.
7880  *  \throw If \a a is not allocated.
7881  *  \throw If \a this is not allocated.
7882  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
7883  *         out of a valid range for \a this array.
7884  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
7885  *         if <em> a->getNumberOfComponents() != (endComp - bgComp) </em>.
7886  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
7887  *         <em> a->getNumberOfComponents() != (endComp - bgComp)</em>.
7888  *
7889  *  \if ENABLE_EXAMPLES
7890  *  \ref py_mcdataarrayint_setpartofvalues2 "Here is a Python example".
7891  *  \endif
7892  */
7893 void DataArrayInt::setPartOfValues2(const DataArrayInt *a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
7894 {
7895   if(!a)
7896     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues2 : DataArrayInt pointer in input is NULL !");
7897   const char msg[]="DataArrayInt::setPartOfValues2";
7898   checkAllocated();
7899   a->checkAllocated();
7900   int nbComp=getNumberOfComponents();
7901   int nbOfTuples=getNumberOfTuples();
7902   for(const int *z=bgComp;z!=endComp;z++)
7903     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
7904   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
7905   int newNbOfComp=(int)std::distance(bgComp,endComp);
7906   bool assignTech=true;
7907   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
7908     {
7909       if(strictCompoCompare)
7910         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
7911     }
7912   else
7913     {
7914       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
7915       assignTech=false;
7916     }
7917   int *pt=getPointer();
7918   const int *srcPt=a->getConstPointer();
7919   if(assignTech)
7920     {    
7921       for(const int *w=bgTuples;w!=endTuples;w++)
7922         {
7923           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
7924           for(const int *z=bgComp;z!=endComp;z++,srcPt++)
7925             {    
7926               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt;
7927             }
7928         }
7929     }
7930   else
7931     {
7932       for(const int *w=bgTuples;w!=endTuples;w++)
7933         {
7934           const int *srcPt2=srcPt;
7935           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
7936           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
7937             {    
7938               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt2;
7939             }
7940         }
7941     }
7942 }
7943
7944 /*!
7945  * Assign a given value to values at specified tuples and components of \a this array.
7946  * The tuples and components to assign to are defined by C arrays of indices.
7947  *  \param [in] a - the value to assign.
7948  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
7949  *              assign \a a to.
7950  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
7951  *              pointer to a tuple index (\a pi) varies as this: 
7952  *              \a bgTuples <= \a pi < \a endTuples.
7953  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
7954  *              assign \a a to.
7955  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
7956  *              pointer to a component index (\a pi) varies as this: 
7957  *              \a bgComp <= \a pi < \a endComp.
7958  *  \throw If \a this is not allocated.
7959  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
7960  *         out of a valid range for \a this array.
7961  *
7962  *  \if ENABLE_EXAMPLES
7963  *  \ref py_mcdataarrayint_setpartofvaluessimple2 "Here is a Python example".
7964  *  \endif
7965  */
7966 void DataArrayInt::setPartOfValuesSimple2(int a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp)
7967 {
7968   checkAllocated();
7969   int nbComp=getNumberOfComponents();
7970   int nbOfTuples=getNumberOfTuples();
7971   for(const int *z=bgComp;z!=endComp;z++)
7972     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
7973   int *pt=getPointer();
7974   for(const int *w=bgTuples;w!=endTuples;w++)
7975     for(const int *z=bgComp;z!=endComp;z++)
7976       {
7977         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
7978         pt[(std::size_t)(*w)*nbComp+(*z)]=a;
7979       }
7980 }
7981
7982 /*!
7983  * Copy all values from another DataArrayInt (\a a) into specified tuples and 
7984  * components of \a this array. Textual data is not copied.
7985  * The tuples to assign to are defined by a C array of indices.
7986  * The components to assign to are defined by three values similar to parameters of
7987  * the Python function \c range(\c start,\c stop,\c step).
7988  * There are two *modes of usage*:
7989  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
7990  *   of \a a is assigned to its own location within \a this array. 
7991  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
7992  *   components of every specified tuple of \a this array. In this mode it is required
7993  *   that \a a->getNumberOfComponents() equals to the number of specified components.
7994  *
7995  *  \param [in] a - the array to copy values from.
7996  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
7997  *              assign values of \a a to.
7998  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
7999  *              pointer to a tuple index <em>(pi)</em> varies as this: 
8000  *              \a bgTuples <= \a pi < \a endTuples.
8001  *  \param [in] bgComp - index of the first component of \a this array to assign to.
8002  *  \param [in] endComp - index of the component before which the components to assign
8003  *              to are located.
8004  *  \param [in] stepComp - index increment to get index of the next component to assign to.
8005  *  \param [in] strictCompoCompare - this parameter is checked only in the first
8006  *               *mode of usage*; if \a strictCompoCompare is \a true (default), 
8007  *               then \a a->getNumberOfComponents() must be equal 
8008  *               to the number of specified columns, else this is not required.
8009  *  \throw If \a a is NULL.
8010  *  \throw If \a a is not allocated.
8011  *  \throw If \a this is not allocated.
8012  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
8013  *         \a this array.
8014  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
8015  *         if <em> a->getNumberOfComponents()</em> is unequal to the number of components
8016  *         defined by <em>(bgComp,endComp,stepComp)</em>.
8017  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
8018  *         <em> a->getNumberOfComponents()</em> is unequal to the number of components
8019  *         defined by <em>(bgComp,endComp,stepComp)</em>.
8020  *  \throw If parameters specifying components to assign to, do not give a
8021  *            non-empty range of increasing indices or indices are out of a valid range
8022  *            for \this array.
8023  *
8024  *  \if ENABLE_EXAMPLES
8025  *  \ref py_mcdataarrayint_setpartofvalues3 "Here is a Python example".
8026  *  \endif
8027  */
8028 void DataArrayInt::setPartOfValues3(const DataArrayInt *a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
8029 {
8030   if(!a)
8031     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues3 : DataArrayInt pointer in input is NULL !");
8032   const char msg[]="DataArrayInt::setPartOfValues3";
8033   checkAllocated();
8034   a->checkAllocated();
8035   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
8036   int nbComp=getNumberOfComponents();
8037   int nbOfTuples=getNumberOfTuples();
8038   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
8039   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
8040   bool assignTech=true;
8041   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
8042     {
8043       if(strictCompoCompare)
8044         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
8045     }
8046   else
8047     {
8048       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
8049       assignTech=false;
8050     }
8051   int *pt=getPointer()+bgComp;
8052   const int *srcPt=a->getConstPointer();
8053   if(assignTech)
8054     {
8055       for(const int *w=bgTuples;w!=endTuples;w++)
8056         for(int j=0;j<newNbOfComp;j++,srcPt++)
8057           {
8058             DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8059             pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt;
8060           }
8061     }
8062   else
8063     {
8064       for(const int *w=bgTuples;w!=endTuples;w++)
8065         {
8066           const int *srcPt2=srcPt;
8067           for(int j=0;j<newNbOfComp;j++,srcPt2++)
8068             {
8069               DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8070               pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt2;
8071             }
8072         }
8073     }
8074 }
8075
8076 /*!
8077  * Assign a given value to values at specified tuples and components of \a this array.
8078  * The tuples to assign to are defined by a C array of indices.
8079  * The components to assign to are defined by three values similar to parameters of
8080  * the Python function \c range(\c start,\c stop,\c step).
8081  *  \param [in] a - the value to assign.
8082  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
8083  *              assign \a a to.
8084  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
8085  *              pointer to a tuple index <em>(pi)</em> varies as this: 
8086  *              \a bgTuples <= \a pi < \a endTuples.
8087  *  \param [in] bgComp - index of the first component of \a this array to assign to.
8088  *  \param [in] endComp - index of the component before which the components to assign
8089  *              to are located.
8090  *  \param [in] stepComp - index increment to get index of the next component to assign to.
8091  *  \throw If \a this is not allocated.
8092  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
8093  *         \a this array.
8094  *  \throw If parameters specifying components to assign to, do not give a
8095  *            non-empty range of increasing indices or indices are out of a valid range
8096  *            for \this array.
8097  *
8098  *  \if ENABLE_EXAMPLES
8099  *  \ref py_mcdataarrayint_setpartofvaluessimple3 "Here is a Python example".
8100  *  \endif
8101  */
8102 void DataArrayInt::setPartOfValuesSimple3(int a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp)
8103 {
8104   const char msg[]="DataArrayInt::setPartOfValuesSimple3";
8105   checkAllocated();
8106   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
8107   int nbComp=getNumberOfComponents();
8108   int nbOfTuples=getNumberOfTuples();
8109   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
8110   int *pt=getPointer()+bgComp;
8111   for(const int *w=bgTuples;w!=endTuples;w++)
8112     for(int j=0;j<newNbOfComp;j++)
8113       {
8114         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8115         pt[(std::size_t)(*w)*nbComp+j*stepComp]=a;
8116       }
8117 }
8118
8119 void DataArrayInt::setPartOfValues4(const DataArrayInt *a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
8120 {
8121   if(!a)
8122     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues4 : input DataArrayInt is NULL !");
8123   const char msg[]="DataArrayInt::setPartOfValues4";
8124   checkAllocated();
8125   a->checkAllocated();
8126   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
8127   int newNbOfComp=(int)std::distance(bgComp,endComp);
8128   int nbComp=getNumberOfComponents();
8129   for(const int *z=bgComp;z!=endComp;z++)
8130     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
8131   int nbOfTuples=getNumberOfTuples();
8132   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
8133   bool assignTech=true;
8134   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
8135     {
8136       if(strictCompoCompare)
8137         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
8138     }
8139   else
8140     {
8141       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
8142       assignTech=false;
8143     }
8144   const int *srcPt=a->getConstPointer();
8145   int *pt=getPointer()+bgTuples*nbComp;
8146   if(assignTech)
8147     {
8148       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8149         for(const int *z=bgComp;z!=endComp;z++,srcPt++)
8150           pt[*z]=*srcPt;
8151     }
8152   else
8153     {
8154       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8155         {
8156           const int *srcPt2=srcPt;
8157           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
8158             pt[*z]=*srcPt2;
8159         }
8160     }
8161 }
8162
8163 void DataArrayInt::setPartOfValuesSimple4(int a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp)
8164 {
8165   const char msg[]="DataArrayInt::setPartOfValuesSimple4";
8166   checkAllocated();
8167   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
8168   int nbComp=getNumberOfComponents();
8169   for(const int *z=bgComp;z!=endComp;z++)
8170     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
8171   int nbOfTuples=getNumberOfTuples();
8172   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
8173   int *pt=getPointer()+bgTuples*nbComp;
8174   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8175     for(const int *z=bgComp;z!=endComp;z++)
8176       pt[*z]=a;
8177 }
8178
8179 /*!
8180  * Copy some tuples from another DataArrayInt into specified tuples
8181  * of \a this array. Textual data is not copied. Both arrays must have equal number of
8182  * components.
8183  * Both the tuples to assign and the tuples to assign to are defined by a DataArrayInt.
8184  * All components of selected tuples are copied.
8185  *  \param [in] a - the array to copy values from.
8186  *  \param [in] tuplesSelec - the array specifying both source tuples of \a a and
8187  *              target tuples of \a this. \a tuplesSelec has two components, and the
8188  *              first component specifies index of the source tuple and the second
8189  *              one specifies index of the target tuple.
8190  *  \throw If \a this is not allocated.
8191  *  \throw If \a a is NULL.
8192  *  \throw If \a a is not allocated.
8193  *  \throw If \a tuplesSelec is NULL.
8194  *  \throw If \a tuplesSelec is not allocated.
8195  *  \throw If <em>this->getNumberOfComponents() != a->getNumberOfComponents()</em>.
8196  *  \throw If \a tuplesSelec->getNumberOfComponents() != 2.
8197  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
8198  *         the corresponding (\a this or \a a) array.
8199  */
8200 void DataArrayInt::setPartOfValuesAdv(const DataArrayInt *a, const DataArrayInt *tuplesSelec)
8201 {
8202   if(!a || !tuplesSelec)
8203     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValuesAdv : DataArrayInt pointer in input is NULL !");
8204   checkAllocated();
8205   a->checkAllocated();
8206   tuplesSelec->checkAllocated();
8207   int nbOfComp=getNumberOfComponents();
8208   if(nbOfComp!=a->getNumberOfComponents())
8209     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValuesAdv : This and a do not have the same number of components !");
8210   if(tuplesSelec->getNumberOfComponents()!=2)
8211     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValuesAdv : Expecting to have a tuple selector DataArrayInt instance with exactly 2 components !");
8212   int thisNt=getNumberOfTuples();
8213   int aNt=a->getNumberOfTuples();
8214   int *valsToSet=getPointer();
8215   const int *valsSrc=a->getConstPointer();
8216   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple+=2)
8217     {
8218       if(tuple[1]>=0 && tuple[1]<aNt)
8219         {
8220           if(tuple[0]>=0 && tuple[0]<thisNt)
8221             std::copy(valsSrc+nbOfComp*tuple[1],valsSrc+nbOfComp*(tuple[1]+1),valsToSet+nbOfComp*tuple[0]);
8222           else
8223             {
8224               std::ostringstream oss; oss << "DataArrayInt::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
8225               oss << " of 'tuplesSelec' request of tuple id #" << tuple[0] << " in 'this' ! It should be in [0," << thisNt << ") !";
8226               throw INTERP_KERNEL::Exception(oss.str().c_str());
8227             }
8228         }
8229       else
8230         {
8231           std::ostringstream oss; oss << "DataArrayInt::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
8232           oss << " of 'tuplesSelec' request of tuple id #" << tuple[1] << " in 'a' ! It should be in [0," << aNt << ") !";
8233           throw INTERP_KERNEL::Exception(oss.str().c_str());
8234         }
8235     }
8236 }
8237
8238 /*!
8239  * Copy some tuples from another DataArrayInt (\a aBase) into contiguous tuples
8240  * of \a this array. Textual data is not copied. Both arrays must have equal number of
8241  * components.
8242  * The tuples to assign to are defined by index of the first tuple, and
8243  * their number is defined by \a tuplesSelec->getNumberOfTuples().
8244  * The tuples to copy are defined by values of a DataArrayInt.
8245  * All components of selected tuples are copied.
8246  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
8247  *              values to.
8248  *  \param [in] aBase - the array to copy values from.
8249  *  \param [in] tuplesSelec - the array specifying tuples of \a aBase to copy.
8250  *  \throw If \a this is not allocated.
8251  *  \throw If \a aBase is NULL.
8252  *  \throw If \a aBase is not allocated.
8253  *  \throw If \a tuplesSelec is NULL.
8254  *  \throw If \a tuplesSelec is not allocated.
8255  *  \throw If <em>this->getNumberOfComponents() != a->getNumberOfComponents()</em>.
8256  *  \throw If \a tuplesSelec->getNumberOfComponents() != 1.
8257  *  \throw If <em>tupleIdStart + tuplesSelec->getNumberOfTuples() > this->getNumberOfTuples().</em>
8258  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
8259  *         \a aBase array.
8260  */
8261 void DataArrayInt::setContigPartOfSelectedValues(int tupleIdStart, const DataArray *aBase, const DataArrayInt *tuplesSelec)
8262 {
8263   if(!aBase || !tuplesSelec)
8264     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : input DataArray is NULL !");
8265   const DataArrayInt *a=dynamic_cast<const DataArrayInt *>(aBase);
8266   if(!a)
8267     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : input DataArray aBase is not a DataArrayInt !");
8268   checkAllocated();
8269   a->checkAllocated();
8270   tuplesSelec->checkAllocated();
8271   int nbOfComp=getNumberOfComponents();
8272   if(nbOfComp!=a->getNumberOfComponents())
8273     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : This and a do not have the same number of components !");
8274   if(tuplesSelec->getNumberOfComponents()!=1)
8275     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : Expecting to have a tuple selector DataArrayInt instance with exactly 1 component !");
8276   int thisNt=getNumberOfTuples();
8277   int aNt=a->getNumberOfTuples();
8278   int nbOfTupleToWrite=tuplesSelec->getNumberOfTuples();
8279   int *valsToSet=getPointer()+tupleIdStart*nbOfComp;
8280   if(tupleIdStart+nbOfTupleToWrite>thisNt)
8281     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : invalid number range of values to write !");
8282   const int *valsSrc=a->getConstPointer();
8283   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple++,valsToSet+=nbOfComp)
8284     {
8285       if(*tuple>=0 && *tuple<aNt)
8286         {
8287           std::copy(valsSrc+nbOfComp*(*tuple),valsSrc+nbOfComp*(*tuple+1),valsToSet);
8288         }
8289       else
8290         {
8291           std::ostringstream oss; oss << "DataArrayInt::setContigPartOfSelectedValues : Tuple #" << std::distance(tuplesSelec->begin(),tuple);
8292           oss << " of 'tuplesSelec' request of tuple id #" << *tuple << " in 'a' ! It should be in [0," << aNt << ") !";
8293           throw INTERP_KERNEL::Exception(oss.str().c_str());
8294         }
8295     }
8296 }
8297
8298 /*!
8299  * Copy some tuples from another DataArrayInt (\a aBase) into contiguous tuples
8300  * of \a this array. Textual data is not copied. Both arrays must have equal number of
8301  * components.
8302  * The tuples to copy are defined by three values similar to parameters of
8303  * the Python function \c range(\c start,\c stop,\c step).
8304  * The tuples to assign to are defined by index of the first tuple, and
8305  * their number is defined by number of tuples to copy.
8306  * All components of selected tuples are copied.
8307  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
8308  *              values to.
8309  *  \param [in] aBase - the array to copy values from.
8310  *  \param [in] bg - index of the first tuple to copy of the array \a aBase.
8311  *  \param [in] end2 - index of the tuple of \a aBase before which the tuples to copy
8312  *              are located.
8313  *  \param [in] step - index increment to get index of the next tuple to copy.
8314  *  \throw If \a this is not allocated.
8315  *  \throw If \a aBase is NULL.
8316  *  \throw If \a aBase is not allocated.
8317  *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
8318  *  \throw If <em>tupleIdStart + len(range(bg,end2,step)) > this->getNumberOfTuples().</em>
8319  *  \throw If parameters specifying tuples to copy, do not give a
8320  *            non-empty range of increasing indices or indices are out of a valid range
8321  *            for the array \a aBase.
8322  */
8323 void DataArrayInt::setContigPartOfSelectedValues2(int tupleIdStart, const DataArray *aBase, int bg, int end2, int step)
8324 {
8325   if(!aBase)
8326     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : input DataArray is NULL !");
8327   const DataArrayInt *a=dynamic_cast<const DataArrayInt *>(aBase);
8328   if(!a)
8329     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : input DataArray aBase is not a DataArrayInt !");
8330   checkAllocated();
8331   a->checkAllocated();
8332   int nbOfComp=getNumberOfComponents();
8333   const char msg[]="DataArrayInt::setContigPartOfSelectedValues2";
8334   int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg);
8335   if(nbOfComp!=a->getNumberOfComponents())
8336     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : This and a do not have the same number of components !");
8337   int thisNt=getNumberOfTuples();
8338   int aNt=a->getNumberOfTuples();
8339   int *valsToSet=getPointer()+tupleIdStart*nbOfComp;
8340   if(tupleIdStart+nbOfTupleToWrite>thisNt)
8341     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid number range of values to write !");
8342   if(end2>aNt)
8343     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid range of values to read !");
8344   const int *valsSrc=a->getConstPointer()+bg*nbOfComp;
8345   for(int i=0;i<nbOfTupleToWrite;i++,valsToSet+=nbOfComp,valsSrc+=step*nbOfComp)
8346     {
8347       std::copy(valsSrc,valsSrc+nbOfComp,valsToSet);
8348     }
8349 }
8350
8351 /*!
8352  * Returns a value located at specified tuple and component.
8353  * This method is equivalent to DataArrayInt::getIJ() except that validity of
8354  * parameters is checked. So this method is safe but expensive if used to go through
8355  * all values of \a this.
8356  *  \param [in] tupleId - index of tuple of interest.
8357  *  \param [in] compoId - index of component of interest.
8358  *  \return double - value located by \a tupleId and \a compoId.
8359  *  \throw If \a this is not allocated.
8360  *  \throw If condition <em>( 0 <= tupleId < this->getNumberOfTuples() )</em> is violated.
8361  *  \throw If condition <em>( 0 <= compoId < this->getNumberOfComponents() )</em> is violated.
8362  */
8363 int DataArrayInt::getIJSafe(int tupleId, int compoId) const
8364 {
8365   checkAllocated();
8366   if(tupleId<0 || tupleId>=getNumberOfTuples())
8367     {
8368       std::ostringstream oss; oss << "DataArrayInt::getIJSafe : request for tupleId " << tupleId << " should be in [0," << getNumberOfTuples() << ") !";
8369       throw INTERP_KERNEL::Exception(oss.str().c_str());
8370     }
8371   if(compoId<0 || compoId>=getNumberOfComponents())
8372     {
8373       std::ostringstream oss; oss << "DataArrayInt::getIJSafe : request for compoId " << compoId << " should be in [0," << getNumberOfComponents() << ") !";
8374       throw INTERP_KERNEL::Exception(oss.str().c_str());
8375     }
8376   return _mem[tupleId*_info_on_compo.size()+compoId];
8377 }
8378
8379 /*!
8380  * Returns the first value of \a this. 
8381  *  \return int - the last value of \a this array.
8382  *  \throw If \a this is not allocated.
8383  *  \throw If \a this->getNumberOfComponents() != 1.
8384  *  \throw If \a this->getNumberOfTuples() < 1.
8385  */
8386 int DataArrayInt::front() const
8387 {
8388   checkAllocated();
8389   if(getNumberOfComponents()!=1)
8390     throw INTERP_KERNEL::Exception("DataArrayInt::front : number of components not equal to one !");
8391   int nbOfTuples=getNumberOfTuples();
8392   if(nbOfTuples<1)
8393     throw INTERP_KERNEL::Exception("DataArrayInt::front : number of tuples must be >= 1 !");
8394   return *(getConstPointer());
8395 }
8396
8397 /*!
8398  * Returns the last value of \a this. 
8399  *  \return int - the last value of \a this array.
8400  *  \throw If \a this is not allocated.
8401  *  \throw If \a this->getNumberOfComponents() != 1.
8402  *  \throw If \a this->getNumberOfTuples() < 1.
8403  */
8404 int DataArrayInt::back() const
8405 {
8406   checkAllocated();
8407   if(getNumberOfComponents()!=1)
8408     throw INTERP_KERNEL::Exception("DataArrayInt::back : number of components not equal to one !");
8409   int nbOfTuples=getNumberOfTuples();
8410   if(nbOfTuples<1)
8411     throw INTERP_KERNEL::Exception("DataArrayInt::back : number of tuples must be >= 1 !");
8412   return *(getConstPointer()+nbOfTuples-1);
8413 }
8414
8415 /*!
8416  * Assign pointer to one array to a pointer to another appay. Reference counter of
8417  * \a arrayToSet is incremented / decremented.
8418  *  \param [in] newArray - the pointer to array to assign to \a arrayToSet.
8419  *  \param [in,out] arrayToSet - the pointer to array to assign to.
8420  */
8421 void DataArrayInt::SetArrayIn(DataArrayInt *newArray, DataArrayInt* &arrayToSet)
8422 {
8423   if(newArray!=arrayToSet)
8424     {
8425       if(arrayToSet)
8426         arrayToSet->decrRef();
8427       arrayToSet=newArray;
8428       if(arrayToSet)
8429         arrayToSet->incrRef();
8430     }
8431 }
8432
8433 DataArrayIntIterator *DataArrayInt::iterator()
8434 {
8435   return new DataArrayIntIterator(this);
8436 }
8437
8438 /*!
8439  * Creates a new DataArrayInt containing IDs (indices) of tuples holding value equal to a
8440  * given one.
8441  *  \param [in] val - the value to find within \a this.
8442  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8443  *          array using decrRef() as it is no more needed.
8444  *  \throw If \a this is not allocated.
8445  *  \throw If \a this->getNumberOfComponents() != 1.
8446  *  \sa DataArrayInt::getIdsEqualTuple
8447  */
8448 DataArrayInt *DataArrayInt::getIdsEqual(int val) const
8449 {
8450   checkAllocated();
8451   if(getNumberOfComponents()!=1)
8452     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqual : the array must have only one component, you can call 'rearrange' method before !");
8453   const int *cptr(getConstPointer());
8454   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8455   int nbOfTuples=getNumberOfTuples();
8456   for(int i=0;i<nbOfTuples;i++,cptr++)
8457     if(*cptr==val)
8458       ret->pushBackSilent(i);
8459   return ret.retn();
8460 }
8461
8462 /*!
8463  * Creates a new DataArrayInt containing IDs (indices) of tuples holding value \b not
8464  * equal to a given one. 
8465  *  \param [in] val - the value to ignore within \a this.
8466  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8467  *          array using decrRef() as it is no more needed.
8468  *  \throw If \a this is not allocated.
8469  *  \throw If \a this->getNumberOfComponents() != 1.
8470  */
8471 DataArrayInt *DataArrayInt::getIdsNotEqual(int val) const
8472 {
8473   checkAllocated();
8474   if(getNumberOfComponents()!=1)
8475     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsNotEqual : the array must have only one component, you can call 'rearrange' method before !");
8476   const int *cptr(getConstPointer());
8477   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8478   int nbOfTuples=getNumberOfTuples();
8479   for(int i=0;i<nbOfTuples;i++,cptr++)
8480     if(*cptr!=val)
8481       ret->pushBackSilent(i);
8482   return ret.retn();
8483 }
8484
8485 /*!
8486  * Creates a new DataArrayInt containing IDs (indices) of tuples holding tuple equal to those defined by [ \a tupleBg , \a tupleEnd )
8487  * This method is an extension of  DataArrayInt::getIdsEqual method.
8488  *
8489  *  \param [in] tupleBg - the begin (included) of the input tuple to find within \a this.
8490  *  \param [in] tupleEnd - the end (excluded) of the input tuple to find within \a this.
8491  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8492  *          array using decrRef() as it is no more needed.
8493  *  \throw If \a this is not allocated.
8494  *  \throw If \a this->getNumberOfComponents() != std::distance(tupleBg,tupleEnd).
8495  * \throw If \a this->getNumberOfComponents() is equal to 0.
8496  * \sa DataArrayInt::getIdsEqual
8497  */
8498 DataArrayInt *DataArrayInt::getIdsEqualTuple(const int *tupleBg, const int *tupleEnd) const
8499 {
8500   std::size_t nbOfCompoExp(std::distance(tupleBg,tupleEnd));
8501   checkAllocated();
8502   if(getNumberOfComponents()!=(int)nbOfCompoExp)
8503     {
8504       std::ostringstream oss; oss << "DataArrayInt::getIdsEqualTuple : mismatch of number of components. Input tuple has " << nbOfCompoExp << " whereas this array has " << getNumberOfComponents() << " components !";
8505       throw INTERP_KERNEL::Exception(oss.str().c_str());
8506     }
8507   if(nbOfCompoExp==0)
8508     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqualTuple : number of components should be > 0 !");
8509   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8510   const int *bg(begin()),*end2(end()),*work(begin());
8511   while(work!=end2)
8512     {
8513       work=std::search(work,end2,tupleBg,tupleEnd);
8514       if(work!=end2)
8515         {
8516           std::size_t pos(std::distance(bg,work));
8517           if(pos%nbOfCompoExp==0)
8518             ret->pushBackSilent(pos/nbOfCompoExp);
8519           work++;
8520         }
8521     }
8522   return ret.retn();
8523 }
8524
8525 /*!
8526  * Assigns \a newValue to all elements holding \a oldValue within \a this
8527  * one-dimensional array.
8528  *  \param [in] oldValue - the value to replace.
8529  *  \param [in] newValue - the value to assign.
8530  *  \return int - number of replacements performed.
8531  *  \throw If \a this is not allocated.
8532  *  \throw If \a this->getNumberOfComponents() != 1.
8533  */
8534 int DataArrayInt::changeValue(int oldValue, int newValue)
8535 {
8536   checkAllocated();
8537   if(getNumberOfComponents()!=1)
8538     throw INTERP_KERNEL::Exception("DataArrayInt::changeValue : the array must have only one component, you can call 'rearrange' method before !");
8539   int *start=getPointer();
8540   int *end2=start+getNbOfElems();
8541   int ret=0;
8542   for(int *val=start;val!=end2;val++)
8543     {
8544       if(*val==oldValue)
8545         {
8546           *val=newValue;
8547           ret++;
8548         }
8549     }
8550   return ret;
8551 }
8552
8553 /*!
8554  * Creates a new DataArrayInt containing IDs (indices) of tuples holding value equal to
8555  * one of given values.
8556  *  \param [in] valsBg - an array of values to find within \a this array.
8557  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
8558  *              the last value of \a valsBg is \a valsEnd[ -1 ].
8559  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8560  *          array using decrRef() as it is no more needed.
8561  *  \throw If \a this->getNumberOfComponents() != 1.
8562  */
8563 DataArrayInt *DataArrayInt::getIdsEqualList(const int *valsBg, const int *valsEnd) const
8564 {
8565   if(getNumberOfComponents()!=1)
8566     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqualList : the array must have only one component, you can call 'rearrange' method before !");
8567   std::set<int> vals2(valsBg,valsEnd);
8568   const int *cptr=getConstPointer();
8569   std::vector<int> res;
8570   int nbOfTuples=getNumberOfTuples();
8571   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8572   for(int i=0;i<nbOfTuples;i++,cptr++)
8573     if(vals2.find(*cptr)!=vals2.end())
8574       ret->pushBackSilent(i);
8575   return ret.retn();
8576 }
8577
8578 /*!
8579  * Creates a new DataArrayInt containing IDs (indices) of tuples holding values \b not
8580  * equal to any of given values.
8581  *  \param [in] valsBg - an array of values to ignore within \a this array.
8582  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
8583  *              the last value of \a valsBg is \a valsEnd[ -1 ].
8584  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8585  *          array using decrRef() as it is no more needed.
8586  *  \throw If \a this->getNumberOfComponents() != 1.
8587  */
8588 DataArrayInt *DataArrayInt::getIdsNotEqualList(const int *valsBg, const int *valsEnd) const
8589 {
8590   if(getNumberOfComponents()!=1)
8591     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsNotEqualList : the array must have only one component, you can call 'rearrange' method before !");
8592   std::set<int> vals2(valsBg,valsEnd);
8593   const int *cptr=getConstPointer();
8594   std::vector<int> res;
8595   int nbOfTuples=getNumberOfTuples();
8596   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8597   for(int i=0;i<nbOfTuples;i++,cptr++)
8598     if(vals2.find(*cptr)==vals2.end())
8599       ret->pushBackSilent(i);
8600   return ret.retn();
8601 }
8602
8603 /*!
8604  * This method is an extension of DataArrayInt::locateValue method because this method works for DataArrayInt with
8605  * any number of components excepted 0 (an INTERP_KERNEL::Exception is thrown in this case).
8606  * This method searches in \b this is there is a tuple that matched the input parameter \b tupl.
8607  * If any the tuple id is returned. If not -1 is returned.
8608  * 
8609  * This method throws an INTERP_KERNEL::Exception if the number of components in \b this mismatches with the size of
8610  * the input vector. An INTERP_KERNEL::Exception is thrown too if \b this is not allocated.
8611  *
8612  * \return tuple id where \b tupl is. -1 if no such tuple exists in \b this.
8613  * \sa DataArrayInt::search, DataArrayInt::presenceOfTuple.
8614  */
8615 int DataArrayInt::locateTuple(const std::vector<int>& tupl) const
8616 {
8617   checkAllocated();
8618   int nbOfCompo=getNumberOfComponents();
8619   if(nbOfCompo==0)
8620     throw INTERP_KERNEL::Exception("DataArrayInt::locateTuple : 0 components in 'this' !");
8621   if(nbOfCompo!=(int)tupl.size())
8622     {
8623       std::ostringstream oss; oss << "DataArrayInt::locateTuple : 'this' contains " << nbOfCompo << " components and searching for a tuple of length " << tupl.size() << " !";
8624       throw INTERP_KERNEL::Exception(oss.str().c_str());
8625     }
8626   const int *cptr=getConstPointer();
8627   std::size_t nbOfVals=getNbOfElems();
8628   for(const int *work=cptr;work!=cptr+nbOfVals;)
8629     {
8630       work=std::search(work,cptr+nbOfVals,tupl.begin(),tupl.end());
8631       if(work!=cptr+nbOfVals)
8632         {
8633           if(std::distance(cptr,work)%nbOfCompo!=0)
8634             work++;
8635           else
8636             return std::distance(cptr,work)/nbOfCompo;
8637         }
8638     }
8639   return -1;
8640 }
8641
8642 /*!
8643  * This method searches the sequence specified in input parameter \b vals in \b this.
8644  * This works only for DataArrayInt having number of components equal to one (if not an INTERP_KERNEL::Exception will be thrown).
8645  * This method differs from DataArrayInt::locateTuple in that the position is internal raw data is not considered here contrary to DataArrayInt::locateTuple.
8646  * \sa DataArrayInt::locateTuple
8647  */
8648 int DataArrayInt::search(const std::vector<int>& vals) const
8649 {
8650   checkAllocated();
8651   int nbOfCompo=getNumberOfComponents();
8652   if(nbOfCompo!=1)
8653     throw INTERP_KERNEL::Exception("DataArrayInt::search : works only for DataArrayInt instance with one component !");
8654   const int *cptr=getConstPointer();
8655   std::size_t nbOfVals=getNbOfElems();
8656   const int *loc=std::search(cptr,cptr+nbOfVals,vals.begin(),vals.end());
8657   if(loc!=cptr+nbOfVals)
8658     return std::distance(cptr,loc);
8659   return -1;
8660 }
8661
8662 /*!
8663  * This method expects to be called when number of components of this is equal to one.
8664  * This method returns the tuple id, if it exists, of the first tuple equal to \b value.
8665  * If not any tuple contains \b value -1 is returned.
8666  * \sa DataArrayInt::presenceOfValue
8667  */
8668 int DataArrayInt::locateValue(int value) const
8669 {
8670   checkAllocated();
8671   if(getNumberOfComponents()!=1)
8672     throw INTERP_KERNEL::Exception("DataArrayInt::presenceOfValue : the array must have only one component, you can call 'rearrange' method before !");
8673   const int *cptr=getConstPointer();
8674   int nbOfTuples=getNumberOfTuples();
8675   const int *ret=std::find(cptr,cptr+nbOfTuples,value);
8676   if(ret!=cptr+nbOfTuples)
8677     return std::distance(cptr,ret);
8678   return -1;
8679 }
8680
8681 /*!
8682  * This method expects to be called when number of components of this is equal to one.
8683  * This method returns the tuple id, if it exists, of the first tuple so that the value is contained in \b vals.
8684  * If not any tuple contains one of the values contained in 'vals' false is returned.
8685  * \sa DataArrayInt::presenceOfValue
8686  */
8687 int DataArrayInt::locateValue(const std::vector<int>& vals) const
8688 {
8689   checkAllocated();
8690   if(getNumberOfComponents()!=1)
8691     throw INTERP_KERNEL::Exception("DataArrayInt::presenceOfValue : the array must have only one component, you can call 'rearrange' method before !");
8692   std::set<int> vals2(vals.begin(),vals.end());
8693   const int *cptr=getConstPointer();
8694   int nbOfTuples=getNumberOfTuples();
8695   for(const int *w=cptr;w!=cptr+nbOfTuples;w++)
8696     if(vals2.find(*w)!=vals2.end())
8697       return std::distance(cptr,w);
8698   return -1;
8699 }
8700
8701 /*!
8702  * This method returns the number of values in \a this that are equals to input parameter \a value.
8703  * This method only works for single component array.
8704  *
8705  * \return a value in [ 0, \c this->getNumberOfTuples() )
8706  *
8707  * \throw If \a this is not allocated
8708  *
8709  */
8710 int DataArrayInt::count(int value) const
8711 {
8712   int ret=0;
8713   checkAllocated();
8714   if(getNumberOfComponents()!=1)
8715     throw INTERP_KERNEL::Exception("DataArrayInt::count : must be applied on DataArrayInt with only one component, you can call 'rearrange' method before !");
8716   const int *vals=begin();
8717   int nbOfTuples=getNumberOfTuples();
8718   for(int i=0;i<nbOfTuples;i++,vals++)
8719     if(*vals==value)
8720       ret++;
8721   return ret;
8722 }
8723
8724 /*!
8725  * This method is an extension of DataArrayInt::presenceOfValue method because this method works for DataArrayInt with
8726  * any number of components excepted 0 (an INTERP_KERNEL::Exception is thrown in this case).
8727  * This method searches in \b this is there is a tuple that matched the input parameter \b tupl.
8728  * This method throws an INTERP_KERNEL::Exception if the number of components in \b this mismatches with the size of
8729  * the input vector. An INTERP_KERNEL::Exception is thrown too if \b this is not allocated.
8730  * \sa DataArrayInt::locateTuple
8731  */
8732 bool DataArrayInt::presenceOfTuple(const std::vector<int>& tupl) const
8733 {
8734   return locateTuple(tupl)!=-1;
8735 }
8736
8737
8738 /*!
8739  * Returns \a true if a given value is present within \a this one-dimensional array.
8740  *  \param [in] value - the value to find within \a this array.
8741  *  \return bool - \a true in case if \a value is present within \a this array.
8742  *  \throw If \a this is not allocated.
8743  *  \throw If \a this->getNumberOfComponents() != 1.
8744  *  \sa locateValue()
8745  */
8746 bool DataArrayInt::presenceOfValue(int value) const
8747 {
8748   return locateValue(value)!=-1;
8749 }
8750
8751 /*!
8752  * This method expects to be called when number of components of this is equal to one.
8753  * This method returns true if it exists a tuple so that the value is contained in \b vals.
8754  * If not any tuple contains one of the values contained in 'vals' false is returned.
8755  * \sa DataArrayInt::locateValue
8756  */
8757 bool DataArrayInt::presenceOfValue(const std::vector<int>& vals) const
8758 {
8759   return locateValue(vals)!=-1;
8760 }
8761
8762 /*!
8763  * Accumulates values of each component of \a this array.
8764  *  \param [out] res - an array of length \a this->getNumberOfComponents(), allocated 
8765  *         by the caller, that is filled by this method with sum value for each
8766  *         component.
8767  *  \throw If \a this is not allocated.
8768  */
8769 void DataArrayInt::accumulate(int *res) const
8770 {
8771   checkAllocated();
8772   const int *ptr=getConstPointer();
8773   int nbTuple=getNumberOfTuples();
8774   int nbComps=getNumberOfComponents();
8775   std::fill(res,res+nbComps,0);
8776   for(int i=0;i<nbTuple;i++)
8777     std::transform(ptr+i*nbComps,ptr+(i+1)*nbComps,res,res,std::plus<int>());
8778 }
8779
8780 int DataArrayInt::accumulate(int compId) const
8781 {
8782   checkAllocated();
8783   const int *ptr=getConstPointer();
8784   int nbTuple=getNumberOfTuples();
8785   int nbComps=getNumberOfComponents();
8786   if(compId<0 || compId>=nbComps)
8787     throw INTERP_KERNEL::Exception("DataArrayInt::accumulate : Invalid compId specified : No such nb of components !");
8788   int ret=0;
8789   for(int i=0;i<nbTuple;i++)
8790     ret+=ptr[i*nbComps+compId];
8791   return ret;
8792 }
8793
8794 /*!
8795  * This method accumulate using addition tuples in \a this using input index array [ \a bgOfIndex, \a endOfIndex ).
8796  * The returned array will have same number of components than \a this and number of tuples equal to
8797  * \c std::distance(bgOfIndex,endOfIndex) \b minus \b one.
8798  *
8799  * The input index array is expected to be ascendingly sorted in which the all referenced ids should be in [0, \c this->getNumberOfTuples).
8800  *
8801  * \param [in] bgOfIndex - begin (included) of the input index array.
8802  * \param [in] endOfIndex - end (excluded) of the input index array.
8803  * \return DataArrayInt * - the new instance having the same number of components than \a this.
8804  * 
8805  * \throw If bgOfIndex or end is NULL.
8806  * \throw If input index array is not ascendingly sorted.
8807  * \throw If there is an id in [ \a bgOfIndex, \a endOfIndex ) not in [0, \c this->getNumberOfTuples).
8808  * \throw If std::distance(bgOfIndex,endOfIndex)==0.
8809  */
8810 DataArrayInt *DataArrayInt::accumulatePerChunck(const int *bgOfIndex, const int *endOfIndex) const
8811 {
8812   if(!bgOfIndex || !endOfIndex)
8813     throw INTERP_KERNEL::Exception("DataArrayInt::accumulatePerChunck : input pointer NULL !");
8814   checkAllocated();
8815   int nbCompo=getNumberOfComponents();
8816   int nbOfTuples=getNumberOfTuples();
8817   int sz=(int)std::distance(bgOfIndex,endOfIndex);
8818   if(sz<1)
8819     throw INTERP_KERNEL::Exception("DataArrayInt::accumulatePerChunck : invalid size of input index array !");
8820   sz--;
8821   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(sz,nbCompo);
8822   const int *w=bgOfIndex;
8823   if(*w<0 || *w>=nbOfTuples)
8824     throw INTERP_KERNEL::Exception("DataArrayInt::accumulatePerChunck : The first element of the input index not in [0,nbOfTuples) !");
8825   const int *srcPt=begin()+(*w)*nbCompo;
8826   int *tmp=ret->getPointer();
8827   for(int i=0;i<sz;i++,tmp+=nbCompo,w++)
8828     {
8829       std::fill(tmp,tmp+nbCompo,0);
8830       if(w[1]>=w[0])
8831         {
8832           for(int j=w[0];j<w[1];j++,srcPt+=nbCompo)
8833             {
8834               if(j>=0 && j<nbOfTuples)
8835                 std::transform(srcPt,srcPt+nbCompo,tmp,tmp,std::plus<int>());
8836               else
8837                 {
8838                   std::ostringstream oss; oss << "DataArrayInt::accumulatePerChunck : At rank #" << i << " the input index array points to id " << j << " should be in [0," << nbOfTuples << ") !";
8839                   throw INTERP_KERNEL::Exception(oss.str().c_str());
8840                 }
8841             }
8842         }
8843       else
8844         {
8845           std::ostringstream oss; oss << "DataArrayInt::accumulatePerChunck : At rank #" << i << " the input index array is not in ascendingly sorted.";
8846           throw INTERP_KERNEL::Exception(oss.str().c_str());
8847         }
8848     }
8849   ret->copyStringInfoFrom(*this);
8850   return ret.retn();
8851 }
8852
8853 /*!
8854  * Returns a new DataArrayInt by concatenating two given arrays, so that (1) the number
8855  * of tuples in the result array is <em> a1->getNumberOfTuples() + a2->getNumberOfTuples() -
8856  * offsetA2</em> and (2)
8857  * the number of component in the result array is same as that of each of given arrays.
8858  * First \a offsetA2 tuples of \a a2 are skipped and thus are missing from the result array.
8859  * Info on components is copied from the first of the given arrays. Number of components
8860  * in the given arrays must be the same.
8861  *  \param [in] a1 - an array to include in the result array.
8862  *  \param [in] a2 - another array to include in the result array.
8863  *  \param [in] offsetA2 - number of tuples of \a a2 to skip.
8864  *  \return DataArrayInt * - the new instance of DataArrayInt.
8865  *          The caller is to delete this result array using decrRef() as it is no more
8866  *          needed.
8867  *  \throw If either \a a1 or \a a2 is NULL.
8868  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents().
8869  */
8870 DataArrayInt *DataArrayInt::Aggregate(const DataArrayInt *a1, const DataArrayInt *a2, int offsetA2)
8871 {
8872   if(!a1 || !a2)
8873     throw INTERP_KERNEL::Exception("DataArrayInt::Aggregate : input DataArrayInt instance is NULL !");
8874   int nbOfComp=a1->getNumberOfComponents();
8875   if(nbOfComp!=a2->getNumberOfComponents())
8876     throw INTERP_KERNEL::Exception("Nb of components mismatch for array Aggregation !");
8877   int nbOfTuple1=a1->getNumberOfTuples();
8878   int nbOfTuple2=a2->getNumberOfTuples();
8879   DataArrayInt *ret=DataArrayInt::New();
8880   ret->alloc(nbOfTuple1+nbOfTuple2-offsetA2,nbOfComp);
8881   int *pt=std::copy(a1->getConstPointer(),a1->getConstPointer()+nbOfTuple1*nbOfComp,ret->getPointer());
8882   std::copy(a2->getConstPointer()+offsetA2*nbOfComp,a2->getConstPointer()+nbOfTuple2*nbOfComp,pt);
8883   ret->copyStringInfoFrom(*a1);
8884   return ret;
8885 }
8886
8887 /*!
8888  * Returns a new DataArrayInt by concatenating all given arrays, so that (1) the number
8889  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
8890  * the number of component in the result array is same as that of each of given arrays.
8891  * Info on components is copied from the first of the given arrays. Number of components
8892  * in the given arrays must be  the same.
8893  * If the number of non null of elements in \a arr is equal to one the returned object is a copy of it
8894  * not the object itself.
8895  *  \param [in] arr - a sequence of arrays to include in the result array.
8896  *  \return DataArrayInt * - the new instance of DataArrayInt.
8897  *          The caller is to delete this result array using decrRef() as it is no more
8898  *          needed.
8899  *  \throw If all arrays within \a arr are NULL.
8900  *  \throw If getNumberOfComponents() of arrays within \a arr.
8901  */
8902 DataArrayInt *DataArrayInt::Aggregate(const std::vector<const DataArrayInt *>& arr)
8903 {
8904   std::vector<const DataArrayInt *> a;
8905   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
8906     if(*it4)
8907       a.push_back(*it4);
8908   if(a.empty())
8909     throw INTERP_KERNEL::Exception("DataArrayInt::Aggregate : input list must be NON EMPTY !");
8910   std::vector<const DataArrayInt *>::const_iterator it=a.begin();
8911   int nbOfComp=(*it)->getNumberOfComponents();
8912   int nbt=(*it++)->getNumberOfTuples();
8913   for(int i=1;it!=a.end();it++,i++)
8914     {
8915       if((*it)->getNumberOfComponents()!=nbOfComp)
8916         throw INTERP_KERNEL::Exception("DataArrayInt::Aggregate : Nb of components mismatch for array aggregation !");
8917       nbt+=(*it)->getNumberOfTuples();
8918     }
8919   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
8920   ret->alloc(nbt,nbOfComp);
8921   int *pt=ret->getPointer();
8922   for(it=a.begin();it!=a.end();it++)
8923     pt=std::copy((*it)->getConstPointer(),(*it)->getConstPointer()+(*it)->getNbOfElems(),pt);
8924   ret->copyStringInfoFrom(*(a[0]));
8925   return ret.retn();
8926 }
8927
8928 /*!
8929  * This method takes as input a list of DataArrayInt instances \a arrs that represent each a packed index arrays.
8930  * A packed index array is an allocated array with one component, and at least one tuple. The first element
8931  * of each array in \a arrs must be 0. Each array in \a arrs is expected to be increasingly monotonic.
8932  * This method is useful for users that want to aggregate a pair of DataArrayInt representing an indexed data (typically nodal connectivity index in unstructured meshes.
8933  * 
8934  * \return DataArrayInt * - a new object to be managed by the caller.
8935  */
8936 DataArrayInt *DataArrayInt::AggregateIndexes(const std::vector<const DataArrayInt *>& arrs)
8937 {
8938   int retSz=1;
8939   for(std::vector<const DataArrayInt *>::const_iterator it4=arrs.begin();it4!=arrs.end();it4++)
8940     {
8941       if(*it4)
8942         {
8943           (*it4)->checkAllocated();
8944           if((*it4)->getNumberOfComponents()!=1)
8945             {
8946               std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a DataArrayInt instance with nb of compo != 1 at pos " << std::distance(arrs.begin(),it4) << " !";
8947               throw INTERP_KERNEL::Exception(oss.str().c_str());
8948             }
8949           int nbTupl=(*it4)->getNumberOfTuples();
8950           if(nbTupl<1)
8951             {
8952               std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a DataArrayInt instance with nb of tuples < 1 at pos " << std::distance(arrs.begin(),it4) << " !";
8953               throw INTERP_KERNEL::Exception(oss.str().c_str());
8954             }
8955           if((*it4)->front()!=0)
8956             {
8957               std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a DataArrayInt instance with front value != 0 at pos " << std::distance(arrs.begin(),it4) << " !";
8958               throw INTERP_KERNEL::Exception(oss.str().c_str());
8959             }
8960           retSz+=nbTupl-1;
8961         }
8962       else
8963         {
8964           std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a null instance at pos " << std::distance(arrs.begin(),it4) << " !";
8965           throw INTERP_KERNEL::Exception(oss.str().c_str());
8966         }
8967     }
8968   if(arrs.empty())
8969     throw INTERP_KERNEL::Exception("DataArrayInt::AggregateIndexes : input list must be NON EMPTY !");
8970   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
8971   ret->alloc(retSz,1);
8972   int *pt=ret->getPointer(); *pt++=0;
8973   for(std::vector<const DataArrayInt *>::const_iterator it=arrs.begin();it!=arrs.end();it++)
8974     pt=std::transform((*it)->begin()+1,(*it)->end(),pt,std::bind2nd(std::plus<int>(),pt[-1]));
8975   ret->copyStringInfoFrom(*(arrs[0]));
8976   return ret.retn();
8977 }
8978
8979 /*!
8980  * Returns the maximal value and its location within \a this one-dimensional array.
8981  *  \param [out] tupleId - index of the tuple holding the maximal value.
8982  *  \return int - the maximal value among all values of \a this array.
8983  *  \throw If \a this->getNumberOfComponents() != 1
8984  *  \throw If \a this->getNumberOfTuples() < 1
8985  */
8986 int DataArrayInt::getMaxValue(int& tupleId) const
8987 {
8988   checkAllocated();
8989   if(getNumberOfComponents()!=1)
8990     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !");
8991   int nbOfTuples=getNumberOfTuples();
8992   if(nbOfTuples<=0)
8993     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !");
8994   const int *vals=getConstPointer();
8995   const int *loc=std::max_element(vals,vals+nbOfTuples);
8996   tupleId=(int)std::distance(vals,loc);
8997   return *loc;
8998 }
8999
9000 /*!
9001  * Returns the maximal value within \a this array that is allowed to have more than
9002  *  one component.
9003  *  \return int - the maximal value among all values of \a this array.
9004  *  \throw If \a this is not allocated.
9005  */
9006 int DataArrayInt::getMaxValueInArray() const
9007 {
9008   checkAllocated();
9009   const int *loc=std::max_element(begin(),end());
9010   return *loc;
9011 }
9012
9013 /*!
9014  * Returns the minimal value and its location within \a this one-dimensional array.
9015  *  \param [out] tupleId - index of the tuple holding the minimal value.
9016  *  \return int - the minimal value among all values of \a this array.
9017  *  \throw If \a this->getNumberOfComponents() != 1
9018  *  \throw If \a this->getNumberOfTuples() < 1
9019  */
9020 int DataArrayInt::getMinValue(int& tupleId) const
9021 {
9022   checkAllocated();
9023   if(getNumberOfComponents()!=1)
9024     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !");
9025   int nbOfTuples=getNumberOfTuples();
9026   if(nbOfTuples<=0)
9027     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !");
9028   const int *vals=getConstPointer();
9029   const int *loc=std::min_element(vals,vals+nbOfTuples);
9030   tupleId=(int)std::distance(vals,loc);
9031   return *loc;
9032 }
9033
9034 /*!
9035  * Returns the minimal value within \a this array that is allowed to have more than
9036  *  one component.
9037  *  \return int - the minimal value among all values of \a this array.
9038  *  \throw If \a this is not allocated.
9039  */
9040 int DataArrayInt::getMinValueInArray() const
9041 {
9042   checkAllocated();
9043   const int *loc=std::min_element(begin(),end());
9044   return *loc;
9045 }
9046
9047 /*!
9048  * Converts every value of \a this array to its absolute value.
9049  * \b WARNING this method is non const. If a new DataArrayInt instance should be built containing the result of abs DataArrayInt::computeAbs
9050  * should be called instead.
9051  *
9052  * \throw If \a this is not allocated.
9053  * \sa DataArrayInt::computeAbs
9054  */
9055 void DataArrayInt::abs()
9056 {
9057   checkAllocated();
9058   int *ptr(getPointer());
9059   std::size_t nbOfElems(getNbOfElems());
9060   std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun<int,int>(std::abs));
9061   declareAsNew();
9062 }
9063
9064 /*!
9065  * This method builds a new instance of \a this object containing the result of std::abs applied of all elements in \a this.
9066  * This method is a const method (that do not change any values in \a this) contrary to  DataArrayInt::abs method.
9067  *
9068  * \return DataArrayInt * - the new instance of DataArrayInt containing the
9069  *         same number of tuples and component as \a this array.
9070  *         The caller is to delete this result array using decrRef() as it is no more
9071  *         needed.
9072  * \throw If \a this is not allocated.
9073  * \sa DataArrayInt::abs
9074  */
9075 DataArrayInt *DataArrayInt::computeAbs() const
9076 {
9077   checkAllocated();
9078   DataArrayInt *newArr(DataArrayInt::New());
9079   int nbOfTuples(getNumberOfTuples());
9080   int nbOfComp(getNumberOfComponents());
9081   newArr->alloc(nbOfTuples,nbOfComp);
9082   std::transform(begin(),end(),newArr->getPointer(),std::ptr_fun<int,int>(std::abs));
9083   newArr->copyStringInfoFrom(*this);
9084   return newArr;
9085 }
9086
9087 /*!
9088  * Apply a liner function to a given component of \a this array, so that
9089  * an array element <em>(x)</em> becomes \f$ a * x + b \f$.
9090  *  \param [in] a - the first coefficient of the function.
9091  *  \param [in] b - the second coefficient of the function.
9092  *  \param [in] compoId - the index of component to modify.
9093  *  \throw If \a this is not allocated.
9094  */
9095 void DataArrayInt::applyLin(int a, int b, int compoId)
9096 {
9097   checkAllocated();
9098   int *ptr=getPointer()+compoId;
9099   int nbOfComp=getNumberOfComponents();
9100   int nbOfTuple=getNumberOfTuples();
9101   for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
9102     *ptr=a*(*ptr)+b;
9103   declareAsNew();
9104 }
9105
9106 /*!
9107  * Apply a liner function to all elements of \a this array, so that
9108  * an element _x_ becomes \f$ a * x + b \f$.
9109  *  \param [in] a - the first coefficient of the function.
9110  *  \param [in] b - the second coefficient of the function.
9111  *  \throw If \a this is not allocated.
9112  */
9113 void DataArrayInt::applyLin(int a, int b)
9114 {
9115   checkAllocated();
9116   int *ptr=getPointer();
9117   std::size_t nbOfElems=getNbOfElems();
9118   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9119     *ptr=a*(*ptr)+b;
9120   declareAsNew();
9121 }
9122
9123 /*!
9124  * Returns a full copy of \a this array except that sign of all elements is reversed.
9125  *  \return DataArrayInt * - the new instance of DataArrayInt containing the
9126  *          same number of tuples and component as \a this array.
9127  *          The caller is to delete this result array using decrRef() as it is no more
9128  *          needed.
9129  *  \throw If \a this is not allocated.
9130  */
9131 DataArrayInt *DataArrayInt::negate() const
9132 {
9133   checkAllocated();
9134   DataArrayInt *newArr=DataArrayInt::New();
9135   int nbOfTuples=getNumberOfTuples();
9136   int nbOfComp=getNumberOfComponents();
9137   newArr->alloc(nbOfTuples,nbOfComp);
9138   const int *cptr=getConstPointer();
9139   std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate<int>());
9140   newArr->copyStringInfoFrom(*this);
9141   return newArr;
9142 }
9143
9144 /*!
9145  * Modify all elements of \a this array, so that
9146  * an element _x_ becomes \f$ numerator / x \f$.
9147  *  \warning If an exception is thrown because of presence of 0 element in \a this 
9148  *           array, all elements processed before detection of the zero element remain
9149  *           modified.
9150  *  \param [in] numerator - the numerator used to modify array elements.
9151  *  \throw If \a this is not allocated.
9152  *  \throw If there is an element equal to 0 in \a this array.
9153  */
9154 void DataArrayInt::applyInv(int numerator)
9155 {
9156   checkAllocated();
9157   int *ptr=getPointer();
9158   std::size_t nbOfElems=getNbOfElems();
9159   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9160     {
9161       if(*ptr!=0)
9162         {
9163           *ptr=numerator/(*ptr);
9164         }
9165       else
9166         {
9167           std::ostringstream oss; oss << "DataArrayInt::applyInv : presence of null value in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
9168           oss << " !";
9169           throw INTERP_KERNEL::Exception(oss.str().c_str());
9170         }
9171     }
9172   declareAsNew();
9173 }
9174
9175 /*!
9176  * Modify all elements of \a this array, so that
9177  * an element _x_ becomes \f$ x / val \f$.
9178  *  \param [in] val - the denominator used to modify array elements.
9179  *  \throw If \a this is not allocated.
9180  *  \throw If \a val == 0.
9181  */
9182 void DataArrayInt::applyDivideBy(int val)
9183 {
9184   if(val==0)
9185     throw INTERP_KERNEL::Exception("DataArrayInt::applyDivideBy : Trying to divide by 0 !");
9186   checkAllocated();
9187   int *ptr=getPointer();
9188   std::size_t nbOfElems=getNbOfElems();
9189   std::transform(ptr,ptr+nbOfElems,ptr,std::bind2nd(std::divides<int>(),val));
9190   declareAsNew();
9191 }
9192
9193 /*!
9194  * Modify all elements of \a this array, so that
9195  * an element _x_ becomes  <em> x % val </em>.
9196  *  \param [in] val - the divisor used to modify array elements.
9197  *  \throw If \a this is not allocated.
9198  *  \throw If \a val <= 0.
9199  */
9200 void DataArrayInt::applyModulus(int val)
9201 {
9202   if(val<=0)
9203     throw INTERP_KERNEL::Exception("DataArrayInt::applyDivideBy : Trying to operate modulus on value <= 0 !");
9204   checkAllocated();
9205   int *ptr=getPointer();
9206   std::size_t nbOfElems=getNbOfElems();
9207   std::transform(ptr,ptr+nbOfElems,ptr,std::bind2nd(std::modulus<int>(),val));
9208   declareAsNew();
9209 }
9210
9211 /*!
9212  * This method works only on data array with one component.
9213  * This method returns a newly allocated array storing stored ascendantly tuple ids in \b this so that
9214  * this[*id] in [\b vmin,\b vmax)
9215  * 
9216  * \param [in] vmin begin of range. This value is included in range (included).
9217  * \param [in] vmax end of range. This value is \b not included in range (excluded).
9218  * \return a newly allocated data array that the caller should deal with.
9219  *
9220  * \sa DataArrayInt::getIdsNotInRange
9221  */
9222 DataArrayInt *DataArrayInt::getIdsInRange(int vmin, int vmax) const
9223 {
9224   checkAllocated();
9225   if(getNumberOfComponents()!=1)
9226     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsInRange : this must have exactly one component !");
9227   const int *cptr(begin());
9228   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
9229   int nbOfTuples(getNumberOfTuples());
9230   for(int i=0;i<nbOfTuples;i++,cptr++)
9231     if(*cptr>=vmin && *cptr<vmax)
9232       ret->pushBackSilent(i);
9233   return ret.retn();
9234 }
9235
9236 /*!
9237  * This method works only on data array with one component.
9238  * This method returns a newly allocated array storing stored ascendantly tuple ids in \b this so that
9239  * this[*id] \b not in [\b vmin,\b vmax)
9240  * 
9241  * \param [in] vmin begin of range. This value is \b not included in range (excluded).
9242  * \param [in] vmax end of range. This value is included in range (included).
9243  * \return a newly allocated data array that the caller should deal with.
9244  * 
9245  * \sa DataArrayInt::getIdsInRange
9246  */
9247 DataArrayInt *DataArrayInt::getIdsNotInRange(int vmin, int vmax) const
9248 {
9249   checkAllocated();
9250   if(getNumberOfComponents()!=1)
9251     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsNotInRange : this must have exactly one component !");
9252   const int *cptr(getConstPointer());
9253   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
9254   int nbOfTuples(getNumberOfTuples());
9255   for(int i=0;i<nbOfTuples;i++,cptr++)
9256     if(*cptr<vmin || *cptr>=vmax)
9257       ret->pushBackSilent(i);
9258   return ret.retn();
9259 }
9260
9261 /*!
9262  * This method works only on data array with one component.
9263  * This method checks that all ids in \b this are in [ \b vmin, \b vmax ). If there is at least one element in \a this not in [ \b vmin, \b vmax ) an exception will be thrown.
9264  * 
9265  * \param [in] vmin begin of range. This value is included in range (included).
9266  * \param [in] vmax end of range. This value is \b not included in range (excluded).
9267  * \return if all ids in \a this are so that (*this)[i]==i for all i in [ 0, \c this->getNumberOfTuples() ). */
9268 bool DataArrayInt::checkAllIdsInRange(int vmin, int vmax) const
9269 {
9270   checkAllocated();
9271   if(getNumberOfComponents()!=1)
9272     throw INTERP_KERNEL::Exception("DataArrayInt::checkAllIdsInRange : this must have exactly one component !");
9273   int nbOfTuples=getNumberOfTuples();
9274   bool ret=true;
9275   const int *cptr=getConstPointer();
9276   for(int i=0;i<nbOfTuples;i++,cptr++)
9277     {
9278       if(*cptr>=vmin && *cptr<vmax)
9279         { ret=ret && *cptr==i; }
9280       else
9281         {
9282           std::ostringstream oss; oss << "DataArrayInt::checkAllIdsInRange : tuple #" << i << " has value " << *cptr << " should be in [" << vmin << "," << vmax << ") !";
9283           throw INTERP_KERNEL::Exception(oss.str().c_str());
9284         }
9285     }
9286   return ret;
9287 }
9288
9289 /*!
9290  * Modify all elements of \a this array, so that
9291  * an element _x_ becomes <em> val % x </em>.
9292  *  \warning If an exception is thrown because of presence of an element <= 0 in \a this 
9293  *           array, all elements processed before detection of the zero element remain
9294  *           modified.
9295  *  \param [in] val - the divident used to modify array elements.
9296  *  \throw If \a this is not allocated.
9297  *  \throw If there is an element equal to or less than 0 in \a this array.
9298  */
9299 void DataArrayInt::applyRModulus(int val)
9300 {
9301   checkAllocated();
9302   int *ptr=getPointer();
9303   std::size_t nbOfElems=getNbOfElems();
9304   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9305     {
9306       if(*ptr>0)
9307         {
9308           *ptr=val%(*ptr);
9309         }
9310       else
9311         {
9312           std::ostringstream oss; oss << "DataArrayInt::applyRModulus : presence of value <=0 in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
9313           oss << " !";
9314           throw INTERP_KERNEL::Exception(oss.str().c_str());
9315         }
9316     }
9317   declareAsNew();
9318 }
9319
9320 /*!
9321  * Modify all elements of \a this array, so that
9322  * an element _x_ becomes <em> val ^ x </em>.
9323  *  \param [in] val - the value used to apply pow on all array elements.
9324  *  \throw If \a this is not allocated.
9325  *  \throw If \a val < 0.
9326  */
9327 void DataArrayInt::applyPow(int val)
9328 {
9329   checkAllocated();
9330   if(val<0)
9331     throw INTERP_KERNEL::Exception("DataArrayInt::applyPow : input pow in < 0 !");
9332   int *ptr=getPointer();
9333   std::size_t nbOfElems=getNbOfElems();
9334   if(val==0)
9335     {
9336       std::fill(ptr,ptr+nbOfElems,1);
9337       return ;
9338     }
9339   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9340     {
9341       int tmp=1;
9342       for(int j=0;j<val;j++)
9343         tmp*=*ptr;
9344       *ptr=tmp;
9345     }
9346   declareAsNew();
9347 }
9348
9349 /*!
9350  * Modify all elements of \a this array, so that
9351  * an element _x_ becomes \f$ val ^ x \f$.
9352  *  \param [in] val - the value used to apply pow on all array elements.
9353  *  \throw If \a this is not allocated.
9354  *  \throw If there is an element < 0 in \a this array.
9355  *  \warning If an exception is thrown because of presence of 0 element in \a this 
9356  *           array, all elements processed before detection of the zero element remain
9357  *           modified.
9358  */
9359 void DataArrayInt::applyRPow(int val)
9360 {
9361   checkAllocated();
9362   int *ptr=getPointer();
9363   std::size_t nbOfElems=getNbOfElems();
9364   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9365     {
9366       if(*ptr>=0)
9367         {
9368           int tmp=1;
9369           for(int j=0;j<*ptr;j++)
9370             tmp*=val;
9371           *ptr=tmp;
9372         }
9373       else
9374         {
9375           std::ostringstream oss; oss << "DataArrayInt::applyRPow : presence of negative value in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
9376           oss << " !";
9377           throw INTERP_KERNEL::Exception(oss.str().c_str());
9378         }
9379     }
9380   declareAsNew();
9381 }
9382
9383 /*!
9384  * Returns a new DataArrayInt by aggregating two given arrays, so that (1) the number
9385  * of components in the result array is a sum of the number of components of given arrays
9386  * and (2) the number of tuples in the result array is same as that of each of given
9387  * arrays. In other words the i-th tuple of result array includes all components of
9388  * i-th tuples of all given arrays.
9389  * Number of tuples in the given arrays must be the same.
9390  *  \param [in] a1 - an array to include in the result array.
9391  *  \param [in] a2 - another array to include in the result array.
9392  *  \return DataArrayInt * - the new instance of DataArrayInt.
9393  *          The caller is to delete this result array using decrRef() as it is no more
9394  *          needed.
9395  *  \throw If both \a a1 and \a a2 are NULL.
9396  *  \throw If any given array is not allocated.
9397  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
9398  */
9399 DataArrayInt *DataArrayInt::Meld(const DataArrayInt *a1, const DataArrayInt *a2)
9400 {
9401   std::vector<const DataArrayInt *> arr(2);
9402   arr[0]=a1; arr[1]=a2;
9403   return Meld(arr);
9404 }
9405
9406 /*!
9407  * Returns a new DataArrayInt by aggregating all given arrays, so that (1) the number
9408  * of components in the result array is a sum of the number of components of given arrays
9409  * and (2) the number of tuples in the result array is same as that of each of given
9410  * arrays. In other words the i-th tuple of result array includes all components of
9411  * i-th tuples of all given arrays.
9412  * Number of tuples in the given arrays must be  the same.
9413  *  \param [in] arr - a sequence of arrays to include in the result array.
9414  *  \return DataArrayInt * - the new instance of DataArrayInt.
9415  *          The caller is to delete this result array using decrRef() as it is no more
9416  *          needed.
9417  *  \throw If all arrays within \a arr are NULL.
9418  *  \throw If any given array is not allocated.
9419  *  \throw If getNumberOfTuples() of arrays within \a arr is different.
9420  */
9421 DataArrayInt *DataArrayInt::Meld(const std::vector<const DataArrayInt *>& arr)
9422 {
9423   std::vector<const DataArrayInt *> a;
9424   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
9425     if(*it4)
9426       a.push_back(*it4);
9427   if(a.empty())
9428     throw INTERP_KERNEL::Exception("DataArrayInt::Meld : array must be NON empty !");
9429   std::vector<const DataArrayInt *>::const_iterator it;
9430   for(it=a.begin();it!=a.end();it++)
9431     (*it)->checkAllocated();
9432   it=a.begin();
9433   int nbOfTuples=(*it)->getNumberOfTuples();
9434   std::vector<int> nbc(a.size());
9435   std::vector<const int *> pts(a.size());
9436   nbc[0]=(*it)->getNumberOfComponents();
9437   pts[0]=(*it++)->getConstPointer();
9438   for(int i=1;it!=a.end();it++,i++)
9439     {
9440       if(nbOfTuples!=(*it)->getNumberOfTuples())
9441         throw INTERP_KERNEL::Exception("DataArrayInt::meld : mismatch of number of tuples !");
9442       nbc[i]=(*it)->getNumberOfComponents();
9443       pts[i]=(*it)->getConstPointer();
9444     }
9445   int totalNbOfComp=std::accumulate(nbc.begin(),nbc.end(),0);
9446   DataArrayInt *ret=DataArrayInt::New();
9447   ret->alloc(nbOfTuples,totalNbOfComp);
9448   int *retPtr=ret->getPointer();
9449   for(int i=0;i<nbOfTuples;i++)
9450     for(int j=0;j<(int)a.size();j++)
9451       {
9452         retPtr=std::copy(pts[j],pts[j]+nbc[j],retPtr);
9453         pts[j]+=nbc[j];
9454       }
9455   int k=0;
9456   for(int i=0;i<(int)a.size();i++)
9457     for(int j=0;j<nbc[i];j++,k++)
9458       ret->setInfoOnComponent(k,a[i]->getInfoOnComponent(j));
9459   return ret;
9460 }
9461
9462 /*!
9463  * Returns a new DataArrayInt which is a minimal partition of elements of \a groups.
9464  * The i-th item of the result array is an ID of a set of elements belonging to a
9465  * unique set of groups, which the i-th element is a part of. This set of elements
9466  * belonging to a unique set of groups is called \a family, so the result array contains
9467  * IDs of families each element belongs to.
9468  *
9469  * \b Example: if we have two groups of elements: \a group1 [0,4] and \a group2 [ 0,1,2 ],
9470  * then there are 3 families:
9471  * - \a family1 (with ID 1) contains element [0] belonging to ( \a group1 + \a group2 ),
9472  * - \a family2 (with ID 2) contains elements [4] belonging to ( \a group1 ),
9473  * - \a family3 (with ID 3) contains element [1,2] belonging to ( \a group2 ), <br>
9474  * and the result array contains IDs of families [ 1,3,3,0,2 ]. <br> Note a family ID 0 which
9475  * stands for the element #3 which is in none of groups.
9476  *
9477  *  \param [in] groups - sequence of groups of element IDs.
9478  *  \param [in] newNb - total number of elements; it must be more than max ID of element
9479  *         in \a groups.
9480  *  \param [out] fidsOfGroups - IDs of families the elements of each group belong to.
9481  *  \return DataArrayInt * - a new instance of DataArrayInt containing IDs of families
9482  *         each element with ID from range [0, \a newNb ) belongs to. The caller is to
9483  *         delete this array using decrRef() as it is no more needed.
9484  *  \throw If any element ID in \a groups violates condition ( 0 <= ID < \a newNb ).
9485  */
9486 DataArrayInt *DataArrayInt::MakePartition(const std::vector<const DataArrayInt *>& groups, int newNb, std::vector< std::vector<int> >& fidsOfGroups)
9487 {
9488   std::vector<const DataArrayInt *> groups2;
9489   for(std::vector<const DataArrayInt *>::const_iterator it4=groups.begin();it4!=groups.end();it4++)
9490     if(*it4)
9491       groups2.push_back(*it4);
9492   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
9493   ret->alloc(newNb,1);
9494   int *retPtr=ret->getPointer();
9495   std::fill(retPtr,retPtr+newNb,0);
9496   int fid=1;
9497   for(std::vector<const DataArrayInt *>::const_iterator iter=groups2.begin();iter!=groups2.end();iter++)
9498     {
9499       const int *ptr=(*iter)->getConstPointer();
9500       std::size_t nbOfElem=(*iter)->getNbOfElems();
9501       int sfid=fid;
9502       for(int j=0;j<sfid;j++)
9503         {
9504           bool found=false;
9505           for(std::size_t i=0;i<nbOfElem;i++)
9506             {
9507               if(ptr[i]>=0 && ptr[i]<newNb)
9508                 {
9509                   if(retPtr[ptr[i]]==j)
9510                     {
9511                       retPtr[ptr[i]]=fid;
9512                       found=true;
9513                     }
9514                 }
9515               else
9516                 {
9517                   std::ostringstream oss; oss << "DataArrayInt::MakePartition : In group \"" << (*iter)->getName() << "\" in tuple #" << i << " value = " << ptr[i] << " ! Should be in [0," << newNb;
9518                   oss << ") !";
9519                   throw INTERP_KERNEL::Exception(oss.str().c_str());
9520                 }
9521             }
9522           if(found)
9523             fid++;
9524         }
9525     }
9526   fidsOfGroups.clear();
9527   fidsOfGroups.resize(groups2.size());
9528   int grId=0;
9529   for(std::vector<const DataArrayInt *>::const_iterator iter=groups2.begin();iter!=groups2.end();iter++,grId++)
9530     {
9531       std::set<int> tmp;
9532       const int *ptr=(*iter)->getConstPointer();
9533       std::size_t nbOfElem=(*iter)->getNbOfElems();
9534       for(const int *p=ptr;p!=ptr+nbOfElem;p++)
9535         tmp.insert(retPtr[*p]);
9536       fidsOfGroups[grId].insert(fidsOfGroups[grId].end(),tmp.begin(),tmp.end());
9537     }
9538   return ret.retn();
9539 }
9540
9541 /*!
9542  * Returns a new DataArrayInt which contains all elements of given one-dimensional
9543  * arrays. The result array does not contain any duplicates and its values
9544  * are sorted in ascending order.
9545  *  \param [in] arr - sequence of DataArrayInt's to unite.
9546  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
9547  *         array using decrRef() as it is no more needed.
9548  *  \throw If any \a arr[i] is not allocated.
9549  *  \throw If \a arr[i]->getNumberOfComponents() != 1.
9550  */
9551 DataArrayInt *DataArrayInt::BuildUnion(const std::vector<const DataArrayInt *>& arr)
9552 {
9553   std::vector<const DataArrayInt *> a;
9554   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
9555     if(*it4)
9556       a.push_back(*it4);
9557   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9558     {
9559       (*it)->checkAllocated();
9560       if((*it)->getNumberOfComponents()!=1)
9561         throw INTERP_KERNEL::Exception("DataArrayInt::BuildUnion : only single component allowed !");
9562     }
9563   //
9564   std::set<int> r;
9565   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9566     {
9567       const int *pt=(*it)->getConstPointer();
9568       int nbOfTuples=(*it)->getNumberOfTuples();
9569       r.insert(pt,pt+nbOfTuples);
9570     }
9571   DataArrayInt *ret=DataArrayInt::New();
9572   ret->alloc((int)r.size(),1);
9573   std::copy(r.begin(),r.end(),ret->getPointer());
9574   return ret;
9575 }
9576
9577 /*!
9578  * Returns a new DataArrayInt which contains elements present in each of given one-dimensional
9579  * arrays. The result array does not contain any duplicates and its values
9580  * are sorted in ascending order.
9581  *  \param [in] arr - sequence of DataArrayInt's to intersect.
9582  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
9583  *         array using decrRef() as it is no more needed.
9584  *  \throw If any \a arr[i] is not allocated.
9585  *  \throw If \a arr[i]->getNumberOfComponents() != 1.
9586  */
9587 DataArrayInt *DataArrayInt::BuildIntersection(const std::vector<const DataArrayInt *>& arr)
9588 {
9589   std::vector<const DataArrayInt *> a;
9590   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
9591     if(*it4)
9592       a.push_back(*it4);
9593   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9594     {
9595       (*it)->checkAllocated();
9596       if((*it)->getNumberOfComponents()!=1)
9597         throw INTERP_KERNEL::Exception("DataArrayInt::BuildIntersection : only single component allowed !");
9598     }
9599   //
9600   std::set<int> r;
9601   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9602     {
9603       const int *pt=(*it)->getConstPointer();
9604       int nbOfTuples=(*it)->getNumberOfTuples();
9605       std::set<int> s1(pt,pt+nbOfTuples);
9606       if(it!=a.begin())
9607         {
9608           std::set<int> r2;
9609           std::set_intersection(r.begin(),r.end(),s1.begin(),s1.end(),inserter(r2,r2.end()));
9610           r=r2;
9611         }
9612       else
9613         r=s1;
9614     }
9615   DataArrayInt *ret(DataArrayInt::New());
9616   ret->alloc((int)r.size(),1);
9617   std::copy(r.begin(),r.end(),ret->getPointer());
9618   return ret;
9619 }
9620
9621 /*!
9622  * This method allows to put a vector of vector of integer into a more compact data stucture (skyline). 
9623  * This method is not available into python because no available optimized data structure available to map std::vector< std::vector<int> >.
9624  *
9625  * \param [in] v the input data structure to be translate into skyline format.
9626  * \param [out] data the first element of the skyline format. The user is expected to deal with newly allocated array.
9627  * \param [out] dataIndex the second element of the skyline format.
9628  */
9629 void DataArrayInt::PutIntoToSkylineFrmt(const std::vector< std::vector<int> >& v, DataArrayInt *& data, DataArrayInt *& dataIndex)
9630 {
9631   int sz((int)v.size());
9632   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret0(DataArrayInt::New()),ret1(DataArrayInt::New());
9633   ret1->alloc(sz+1,1);
9634   int *pt(ret1->getPointer()); *pt=0;
9635   for(int i=0;i<sz;i++,pt++)
9636     pt[1]=pt[0]+(int)v[i].size();
9637   ret0->alloc(ret1->back(),1);
9638   pt=ret0->getPointer();
9639   for(int i=0;i<sz;i++)
9640     pt=std::copy(v[i].begin(),v[i].end(),pt);
9641   data=ret0.retn(); dataIndex=ret1.retn();
9642 }
9643
9644 /*!
9645  * Returns a new DataArrayInt which contains a complement of elements of \a this
9646  * one-dimensional array. I.e. the result array contains all elements from the range [0,
9647  * \a nbOfElement) not present in \a this array.
9648  *  \param [in] nbOfElement - maximal size of the result array.
9649  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
9650  *         array using decrRef() as it is no more needed.
9651  *  \throw If \a this is not allocated.
9652  *  \throw If \a this->getNumberOfComponents() != 1.
9653  *  \throw If any element \a x of \a this array violates condition ( 0 <= \a x < \a
9654  *         nbOfElement ).
9655  */
9656 DataArrayInt *DataArrayInt::buildComplement(int nbOfElement) const
9657 {
9658    checkAllocated();
9659    if(getNumberOfComponents()!=1)
9660      throw INTERP_KERNEL::Exception("DataArrayInt::buildComplement : only single component allowed !");
9661    std::vector<bool> tmp(nbOfElement);
9662    const int *pt=getConstPointer();
9663    int nbOfTuples=getNumberOfTuples();
9664    for(const int *w=pt;w!=pt+nbOfTuples;w++)
9665      if(*w>=0 && *w<nbOfElement)
9666        tmp[*w]=true;
9667      else
9668        throw INTERP_KERNEL::Exception("DataArrayInt::buildComplement : an element is not in valid range : [0,nbOfElement) !");
9669    int nbOfRetVal=(int)std::count(tmp.begin(),tmp.end(),false);
9670    DataArrayInt *ret=DataArrayInt::New();
9671    ret->alloc(nbOfRetVal,1);
9672    int j=0;
9673    int *retPtr=ret->getPointer();
9674    for(int i=0;i<nbOfElement;i++)
9675      if(!tmp[i])
9676        retPtr[j++]=i;
9677    return ret;
9678 }
9679
9680 /*!
9681  * Returns a new DataArrayInt containing elements of \a this one-dimensional missing
9682  * from an \a other one-dimensional array.
9683  *  \param [in] other - a DataArrayInt containing elements not to include in the result array.
9684  *  \return DataArrayInt * - a new instance of DataArrayInt with one component. The
9685  *         caller is to delete this array using decrRef() as it is no more needed.
9686  *  \throw If \a other is NULL.
9687  *  \throw If \a other is not allocated.
9688  *  \throw If \a other->getNumberOfComponents() != 1.
9689  *  \throw If \a this is not allocated.
9690  *  \throw If \a this->getNumberOfComponents() != 1.
9691  *  \sa DataArrayInt::buildSubstractionOptimized()
9692  */
9693 DataArrayInt *DataArrayInt::buildSubstraction(const DataArrayInt *other) const
9694 {
9695   if(!other)
9696     throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstraction : DataArrayInt pointer in input is NULL !");
9697   checkAllocated();
9698   other->checkAllocated();
9699   if(getNumberOfComponents()!=1)
9700      throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstraction : only single component allowed !");
9701   if(other->getNumberOfComponents()!=1)
9702      throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstraction : only single component allowed for other type !");
9703   const int *pt=getConstPointer();
9704   int nbOfTuples=getNumberOfTuples();
9705   std::set<int> s1(pt,pt+nbOfTuples);
9706   pt=other->getConstPointer();
9707   nbOfTuples=other->getNumberOfTuples();
9708   std::set<int> s2(pt,pt+nbOfTuples);
9709   std::vector<int> r;
9710   std::set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),std::back_insert_iterator< std::vector<int> >(r));
9711   DataArrayInt *ret=DataArrayInt::New();
9712   ret->alloc((int)r.size(),1);
9713   std::copy(r.begin(),r.end(),ret->getPointer());
9714   return ret;
9715 }
9716
9717 /*!
9718  * \a this is expected to have one component and to be sorted ascendingly (as for \a other).
9719  * \a other is expected to be a part of \a this. If not DataArrayInt::buildSubstraction should be called instead.
9720  * 
9721  * \param [in] other an array with one component and expected to be sorted ascendingly.
9722  * \ret list of ids in \a this but not in \a other.
9723  * \sa DataArrayInt::buildSubstraction
9724  */
9725 DataArrayInt *DataArrayInt::buildSubstractionOptimized(const DataArrayInt *other) const
9726 {
9727   static const char *MSG="DataArrayInt::buildSubstractionOptimized : only single component allowed !";
9728   if(!other) throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstractionOptimized : NULL input array !");
9729   checkAllocated(); other->checkAllocated();
9730   if(getNumberOfComponents()!=1) throw INTERP_KERNEL::Exception(MSG);
9731   if(other->getNumberOfComponents()!=1) throw INTERP_KERNEL::Exception(MSG);
9732   const int *pt1Bg(begin()),*pt1End(end()),*pt2Bg(other->begin()),*pt2End(other->end()),*work1(pt1Bg),*work2(pt2Bg);
9733   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
9734   for(;work1!=pt1End;work1++)
9735     {
9736       if(work2!=pt2End && *work1==*work2)
9737         work2++;
9738       else
9739         ret->pushBackSilent(*work1);
9740     }
9741   return ret.retn();
9742 }
9743
9744
9745 /*!
9746  * Returns a new DataArrayInt which contains all elements of \a this and a given
9747  * one-dimensional arrays. The result array does not contain any duplicates
9748  * and its values are sorted in ascending order.
9749  *  \param [in] other - an array to unite with \a this one.
9750  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
9751  *         array using decrRef() as it is no more needed.
9752  *  \throw If \a this or \a other is not allocated.
9753  *  \throw If \a this->getNumberOfComponents() != 1.
9754  *  \throw If \a other->getNumberOfComponents() != 1.
9755  */
9756 DataArrayInt *DataArrayInt::buildUnion(const DataArrayInt *other) const
9757 {
9758   std::vector<const DataArrayInt *>arrs(2);
9759   arrs[0]=this; arrs[1]=other;
9760   return BuildUnion(arrs);
9761 }
9762
9763
9764 /*!
9765  * Returns a new DataArrayInt which contains elements present in both \a this and a given
9766  * one-dimensional arrays. The result array does not contain any duplicates
9767  * and its values are sorted in ascending order.
9768  *  \param [in] other - an array to intersect with \a this one.
9769  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
9770  *         array using decrRef() as it is no more needed.
9771  *  \throw If \a this or \a other is not allocated.
9772  *  \throw If \a this->getNumberOfComponents() != 1.
9773  *  \throw If \a other->getNumberOfComponents() != 1.
9774  */
9775 DataArrayInt *DataArrayInt::buildIntersection(const DataArrayInt *other) const
9776 {
9777   std::vector<const DataArrayInt *>arrs(2);
9778   arrs[0]=this; arrs[1]=other;
9779   return BuildIntersection(arrs);
9780 }
9781
9782 /*!
9783  * This method can be applied on allocated with one component DataArrayInt instance.
9784  * This method is typically relevant for sorted arrays. All consecutive duplicated items in \a this will appear only once in returned DataArrayInt instance.
9785  * Example : if \a this contains [1,2,2,3,3,3,3,4,5,5,7,7,7,19] the returned array will contain [1,2,3,4,5,7,19]
9786  * 
9787  * \return a newly allocated array that contain the result of the unique operation applied on \a this.
9788  * \throw if \a this is not allocated or if \a this has not exactly one component.
9789  */
9790 DataArrayInt *DataArrayInt::buildUnique() const
9791 {
9792   checkAllocated();
9793   if(getNumberOfComponents()!=1)
9794      throw INTERP_KERNEL::Exception("DataArrayInt::buildUnique : only single component allowed !");
9795   int nbOfTuples=getNumberOfTuples();
9796   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp=deepCpy();
9797   int *data=tmp->getPointer();
9798   int *last=std::unique(data,data+nbOfTuples);
9799   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
9800   ret->alloc(std::distance(data,last),1);
9801   std::copy(data,last,ret->getPointer());
9802   return ret.retn();
9803 }
9804
9805 /*!
9806  * Returns a new DataArrayInt which contains size of every of groups described by \a this
9807  * "index" array. Such "index" array is returned for example by 
9808  * \ref ParaMEDMEM::MEDCouplingUMesh::buildDescendingConnectivity
9809  * "MEDCouplingUMesh::buildDescendingConnectivity" and
9810  * \ref ParaMEDMEM::MEDCouplingUMesh::getNodalConnectivityIndex
9811  * "MEDCouplingUMesh::getNodalConnectivityIndex" etc.
9812  * This method preforms the reverse operation of DataArrayInt::computeOffsets2.
9813  *  \return DataArrayInt * - a new instance of DataArrayInt, whose number of tuples
9814  *          equals to \a this->getNumberOfComponents() - 1, and number of components is 1.
9815  *          The caller is to delete this array using decrRef() as it is no more needed. 
9816  *  \throw If \a this is not allocated.
9817  *  \throw If \a this->getNumberOfComponents() != 1.
9818  *  \throw If \a this->getNumberOfTuples() < 2.
9819  *
9820  *  \b Example: <br> 
9821  *         - this contains [1,3,6,7,7,9,15]
9822  *         - result array contains [2,3,1,0,2,6],
9823  *          where 2 = 3 - 1, 3 = 6 - 3, 1 = 7 - 6 etc.
9824  *
9825  * \sa DataArrayInt::computeOffsets2
9826  */
9827 DataArrayInt *DataArrayInt::deltaShiftIndex() const
9828 {
9829   checkAllocated();
9830   if(getNumberOfComponents()!=1)
9831      throw INTERP_KERNEL::Exception("DataArrayInt::deltaShiftIndex : only single component allowed !");
9832   int nbOfTuples=getNumberOfTuples();
9833   if(nbOfTuples<2)
9834     throw INTERP_KERNEL::Exception("DataArrayInt::deltaShiftIndex : 1 tuple at least must be present in 'this' !");
9835   const int *ptr=getConstPointer();
9836   DataArrayInt *ret=DataArrayInt::New();
9837   ret->alloc(nbOfTuples-1,1);
9838   int *out=ret->getPointer();
9839   std::transform(ptr+1,ptr+nbOfTuples,ptr,out,std::minus<int>());
9840   return ret;
9841 }
9842
9843 /*!
9844  * Modifies \a this one-dimensional array so that value of each element \a x
9845  * of \a this array (\a a) is computed as \f$ x_i = \sum_{j=0}^{i-1} a[ j ] \f$.
9846  * Or: for each i>0 new[i]=new[i-1]+old[i-1] for i==0 new[i]=0. Number of tuples
9847  * and components remains the same.<br>
9848  * This method is useful for allToAllV in MPI with contiguous policy. This method
9849  * differs from computeOffsets2() in that the number of tuples is \b not changed by
9850  * this one.
9851  *  \throw If \a this is not allocated.
9852  *  \throw If \a this->getNumberOfComponents() != 1.
9853  *
9854  *  \b Example: <br>
9855  *          - Before \a this contains [3,5,1,2,0,8]
9856  *          - After \a this contains  [0,3,8,9,11,11]<br>
9857  *          Note that the last element 19 = 11 + 8 is missing because size of \a this
9858  *          array is retained and thus there is no space to store the last element.
9859  */
9860 void DataArrayInt::computeOffsets()
9861 {
9862   checkAllocated();
9863   if(getNumberOfComponents()!=1)
9864      throw INTERP_KERNEL::Exception("DataArrayInt::computeOffsets : only single component allowed !");
9865   int nbOfTuples=getNumberOfTuples();
9866   if(nbOfTuples==0)
9867     return ;
9868   int *work=getPointer();
9869   int tmp=work[0];
9870   work[0]=0;
9871   for(int i=1;i<nbOfTuples;i++)
9872     {
9873       int tmp2=work[i];
9874       work[i]=work[i-1]+tmp;
9875       tmp=tmp2;
9876     }
9877   declareAsNew();
9878 }
9879
9880
9881 /*!
9882  * Modifies \a this one-dimensional array so that value of each element \a x
9883  * of \a this array (\a a) is computed as \f$ x_i = \sum_{j=0}^{i-1} a[ j ] \f$.
9884  * Or: for each i>0 new[i]=new[i-1]+old[i-1] for i==0 new[i]=0. Number
9885  * components remains the same and number of tuples is inceamented by one.<br>
9886  * This method is useful for allToAllV in MPI with contiguous policy. This method
9887  * differs from computeOffsets() in that the number of tuples is changed by this one.
9888  * This method preforms the reverse operation of DataArrayInt::deltaShiftIndex.
9889  *  \throw If \a this is not allocated.
9890  *  \throw If \a this->getNumberOfComponents() != 1.
9891  *
9892  *  \b Example: <br>
9893  *          - Before \a this contains [3,5,1,2,0,8]
9894  *          - After \a this contains  [0,3,8,9,11,11,19]<br>
9895  * \sa DataArrayInt::deltaShiftIndex
9896  */
9897 void DataArrayInt::computeOffsets2()
9898 {
9899   checkAllocated();
9900   if(getNumberOfComponents()!=1)
9901     throw INTERP_KERNEL::Exception("DataArrayInt::computeOffsets2 : only single component allowed !");
9902   int nbOfTuples=getNumberOfTuples();
9903   int *ret=(int *)malloc((nbOfTuples+1)*sizeof(int));
9904   if(nbOfTuples==0)
9905     return ;
9906   const int *work=getConstPointer();
9907   ret[0]=0;
9908   for(int i=0;i<nbOfTuples;i++)
9909     ret[i+1]=work[i]+ret[i];
9910   useArray(ret,true,C_DEALLOC,nbOfTuples+1,1);
9911   declareAsNew();
9912 }
9913
9914 /*!
9915  * Returns two new DataArrayInt instances whose contents is computed from that of \a this and \a listOfIds arrays as follows.
9916  * \a this is expected to be an offset format ( as returned by DataArrayInt::computeOffsets2 ) that is to say with one component
9917  * and ** sorted strictly increasingly **. \a listOfIds is expected to be sorted ascendingly (not strictly needed for \a listOfIds).
9918  * This methods searches in \a this, considered as a set of contiguous \c this->getNumberOfComponents() ranges, all ids in \a listOfIds
9919  * filling completely one of the ranges in \a this.
9920  *
9921  * \param [in] listOfIds a list of ids that has to be sorted ascendingly.
9922  * \param [out] rangeIdsFetched the range ids fetched
9923  * \param [out] idsInInputListThatFetch contains the list of ids in \a listOfIds that are \b fully included in a range in \a this. So
9924  *              \a idsInInputListThatFetch is a part of input \a listOfIds.
9925  *
9926  * \sa DataArrayInt::computeOffsets2
9927  *
9928  *  \b Example: <br>
9929  *          - \a this : [0,3,7,9,15,18]
9930  *          - \a listOfIds contains  [0,1,2,3,7,8,15,16,17]
9931  *          - \a rangeIdsFetched result array: [0,2,4]
9932  *          - \a idsInInputListThatFetch result array: [0,1,2,7,8,15,16,17]
9933  * In this example id 3 in input \a listOfIds is alone so it do not appear in output \a idsInInputListThatFetch.
9934  * <br>
9935  */
9936 void DataArrayInt::searchRangesInListOfIds(const DataArrayInt *listOfIds, DataArrayInt *& rangeIdsFetched, DataArrayInt *& idsInInputListThatFetch) const
9937 {
9938   if(!listOfIds)
9939     throw INTERP_KERNEL::Exception("DataArrayInt::searchRangesInListOfIds : input list of ids is null !");
9940   listOfIds->checkAllocated(); checkAllocated();
9941   if(listOfIds->getNumberOfComponents()!=1)
9942     throw INTERP_KERNEL::Exception("DataArrayInt::searchRangesInListOfIds : input list of ids must have exactly one component !");
9943   if(getNumberOfComponents()!=1)
9944     throw INTERP_KERNEL::Exception("DataArrayInt::searchRangesInListOfIds : this must have exactly one component !");
9945   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret0=DataArrayInt::New(); ret0->alloc(0,1);
9946   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=DataArrayInt::New(); ret1->alloc(0,1);
9947   const int *tupEnd(listOfIds->end()),*offBg(begin()),*offEnd(end()-1);
9948   const int *tupPtr(listOfIds->begin()),*offPtr(offBg);
9949   while(tupPtr!=tupEnd && offPtr!=offEnd)
9950     {
9951       if(*tupPtr==*offPtr)
9952         {
9953           int i=offPtr[0];
9954           while(i<offPtr[1] && *tupPtr==i && tupPtr!=tupEnd) { i++; tupPtr++; }
9955           if(i==offPtr[1])
9956             {
9957               ret0->pushBackSilent((int)std::distance(offBg,offPtr));
9958               ret1->pushBackValsSilent(tupPtr-(offPtr[1]-offPtr[0]),tupPtr);
9959               offPtr++;
9960             }
9961         }
9962       else
9963         { if(*tupPtr<*offPtr) tupPtr++; else offPtr++; }
9964     }
9965   rangeIdsFetched=ret0.retn();
9966   idsInInputListThatFetch=ret1.retn();
9967 }
9968
9969 /*!
9970  * Returns a new DataArrayInt whose contents is computed from that of \a this and \a
9971  * offsets arrays as follows. \a offsets is a one-dimensional array considered as an
9972  * "index" array of a "iota" array, thus, whose each element gives an index of a group
9973  * beginning within the "iota" array. And \a this is a one-dimensional array
9974  * considered as a selector of groups described by \a offsets to include into the result array.
9975  *  \throw If \a offsets is NULL.
9976  *  \throw If \a offsets is not allocated.
9977  *  \throw If \a offsets->getNumberOfComponents() != 1.
9978  *  \throw If \a offsets is not monotonically increasing.
9979  *  \throw If \a this is not allocated.
9980  *  \throw If \a this->getNumberOfComponents() != 1.
9981  *  \throw If any element of \a this is not a valid index for \a offsets array.
9982  *
9983  *  \b Example: <br>
9984  *          - \a this: [0,2,3]
9985  *          - \a offsets: [0,3,6,10,14,20]
9986  *          - result array: [0,1,2,6,7,8,9,10,11,12,13] == <br>
9987  *            \c range(0,3) + \c range(6,10) + \c range(10,14) ==<br>
9988  *            \c range( \a offsets[ \a this[0] ], offsets[ \a this[0]+1 ]) + 
9989  *            \c range( \a offsets[ \a this[1] ], offsets[ \a this[1]+1 ]) + 
9990  *            \c range( \a offsets[ \a this[2] ], offsets[ \a this[2]+1 ])
9991  */
9992 DataArrayInt *DataArrayInt::buildExplicitArrByRanges(const DataArrayInt *offsets) const
9993 {
9994   if(!offsets)
9995     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : DataArrayInt pointer in input is NULL !");
9996   checkAllocated();
9997   if(getNumberOfComponents()!=1)
9998      throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : only single component allowed !");
9999   offsets->checkAllocated();
10000   if(offsets->getNumberOfComponents()!=1)
10001      throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : input array should have only single component !");
10002   int othNbTuples=offsets->getNumberOfTuples()-1;
10003   int nbOfTuples=getNumberOfTuples();
10004   int retNbOftuples=0;
10005   const int *work=getConstPointer();
10006   const int *offPtr=offsets->getConstPointer();
10007   for(int i=0;i<nbOfTuples;i++)
10008     {
10009       int val=work[i];
10010       if(val>=0 && val<othNbTuples)
10011         {
10012           int delta=offPtr[val+1]-offPtr[val];
10013           if(delta>=0)
10014             retNbOftuples+=delta;
10015           else
10016             {
10017               std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrByRanges : Tuple #" << val << " of offset array has a delta < 0 !";
10018               throw INTERP_KERNEL::Exception(oss.str().c_str());
10019             }
10020         }
10021       else
10022         {
10023           std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrByRanges : Tuple #" << i << " in this contains " << val;
10024           oss << " whereas offsets array is of size " << othNbTuples+1 << " !";
10025           throw INTERP_KERNEL::Exception(oss.str().c_str());
10026         }
10027     }
10028   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10029   ret->alloc(retNbOftuples,1);
10030   int *retPtr=ret->getPointer();
10031   for(int i=0;i<nbOfTuples;i++)
10032     {
10033       int val=work[i];
10034       int start=offPtr[val];
10035       int off=offPtr[val+1]-start;
10036       for(int j=0;j<off;j++,retPtr++)
10037         *retPtr=start+j;
10038     }
10039   return ret.retn();
10040 }
10041
10042 /*!
10043  * Returns a new DataArrayInt whose contents is computed using \a this that must be a 
10044  * scaled array (monotonically increasing).
10045 from that of \a this and \a
10046  * offsets arrays as follows. \a offsets is a one-dimensional array considered as an
10047  * "index" array of a "iota" array, thus, whose each element gives an index of a group
10048  * beginning within the "iota" array. And \a this is a one-dimensional array
10049  * considered as a selector of groups described by \a offsets to include into the result array.
10050  *  \throw If \a  is NULL.
10051  *  \throw If \a this is not allocated.
10052  *  \throw If \a this->getNumberOfComponents() != 1.
10053  *  \throw If \a this->getNumberOfTuples() == 0.
10054  *  \throw If \a this is not monotonically increasing.
10055  *  \throw If any element of ids in ( \a bg \a stop \a step ) points outside the scale in \a this.
10056  *
10057  *  \b Example: <br>
10058  *          - \a bg , \a stop and \a step : (0,5,2)
10059  *          - \a this: [0,3,6,10,14,20]
10060  *          - result array: [0,0,0, 2,2,2,2, 4,4,4,4,4,4] == <br>
10061  */
10062 DataArrayInt *DataArrayInt::buildExplicitArrOfSliceOnScaledArr(int bg, int stop, int step) const
10063 {
10064   if(!isAllocated())
10065     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr : not allocated array !");
10066   if(getNumberOfComponents()!=1)
10067     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr : number of components is expected to be equal to one !");
10068   int nbOfTuples(getNumberOfTuples());
10069   if(nbOfTuples==0)
10070     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr : number of tuples must be != 0 !");
10071   const int *ids(begin());
10072   int nbOfEltsInSlc(GetNumberOfItemGivenBESRelative(bg,stop,step,"DataArrayInt::buildExplicitArrOfSliceOnScaledArr")),sz(0),pos(bg);
10073   for(int i=0;i<nbOfEltsInSlc;i++,pos+=step)
10074     {
10075       if(pos>=0 && pos<nbOfTuples-1)
10076         {
10077           int delta(ids[pos+1]-ids[pos]);
10078           sz+=delta;
10079           if(delta<0)
10080             {
10081               std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrOfSliceOnScaledArr : At pos #" << i << " of input slice, value is " << pos << " and at this pos this is not monotonically increasing !";
10082               throw INTERP_KERNEL::Exception(oss.str().c_str());
10083             }          
10084         }
10085       else
10086         {
10087           std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrOfSliceOnScaledArr : At pos #" << i << " of input slice, value is " << pos << " should be in [0," << nbOfTuples-1 << ") !";  
10088           throw INTERP_KERNEL::Exception(oss.str().c_str());
10089         }
10090     }
10091   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(sz,1);
10092   int *retPtr(ret->getPointer());
10093   pos=bg;
10094   for(int i=0;i<nbOfEltsInSlc;i++,pos+=step)
10095     {
10096       int delta(ids[pos+1]-ids[pos]);
10097       for(int j=0;j<delta;j++,retPtr++)
10098         *retPtr=pos;
10099     }
10100   return ret.retn();
10101 }
10102
10103 /*!
10104  * Given in input ranges \a ranges, it returns a newly allocated DataArrayInt instance having one component and the same number of tuples than \a this.
10105  * For each tuple at place **i** in \a this it tells which is the first range in \a ranges that contains value \c this->getIJ(i,0) and put the result
10106  * in tuple **i** of returned DataArrayInt.
10107  * If ranges overlapped (in theory it should not) this method do not detect it and always returns the first range.
10108  *
10109  * For example if \a this contains : [1,24,7,8,10,17] and \a ranges contains [(0,3),(3,8),(8,15),(15,22),(22,30)]
10110  * The return DataArrayInt will contain : **[0,4,1,2,2,3]**
10111  * 
10112  * \param [in] ranges typically come from output of MEDCouplingUMesh::ComputeRangesFromTypeDistribution. Each range is specified like this : 1st component is
10113  *             for lower value included and 2nd component is the upper value of corresponding range **excluded**.
10114  * \throw If offsets is a null pointer or does not have 2 components or if \a this is not allocated or \a this do not have exactly one component. To finish an exception
10115  *        is thrown if no ranges in \a ranges contains value in \a this.
10116  * 
10117  * \sa DataArrayInt::findIdInRangeForEachTuple
10118  */
10119 DataArrayInt *DataArrayInt::findRangeIdForEachTuple(const DataArrayInt *ranges) const
10120 {
10121   if(!ranges)
10122     throw INTERP_KERNEL::Exception("DataArrayInt::findRangeIdForEachTuple : null input pointer !");
10123   if(ranges->getNumberOfComponents()!=2)
10124     throw INTERP_KERNEL::Exception("DataArrayInt::findRangeIdForEachTuple : input DataArrayInt instance should have 2 components !");
10125   checkAllocated();
10126   if(getNumberOfComponents()!=1)
10127     throw INTERP_KERNEL::Exception("DataArrayInt::findRangeIdForEachTuple : this should have only one component !");
10128   int nbTuples=getNumberOfTuples();
10129   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbTuples,1);
10130   int nbOfRanges=ranges->getNumberOfTuples();
10131   const int *rangesPtr=ranges->getConstPointer();
10132   int *retPtr=ret->getPointer();
10133   const int *inPtr=getConstPointer();
10134   for(int i=0;i<nbTuples;i++,retPtr++)
10135     {
10136       int val=inPtr[i];
10137       bool found=false;
10138       for(int j=0;j<nbOfRanges && !found;j++)
10139         if(val>=rangesPtr[2*j] && val<rangesPtr[2*j+1])
10140           { *retPtr=j; found=true; }
10141       if(found)
10142         continue;
10143       else
10144         {
10145           std::ostringstream oss; oss << "DataArrayInt::findRangeIdForEachTuple : tuple #" << i << " not found by any ranges !";
10146           throw INTERP_KERNEL::Exception(oss.str().c_str());
10147         }
10148     }
10149   return ret.retn();
10150 }
10151
10152 /*!
10153  * Given in input ranges \a ranges, it returns a newly allocated DataArrayInt instance having one component and the same number of tuples than \a this.
10154  * For each tuple at place **i** in \a this it tells which is the sub position of the first range in \a ranges that contains value \c this->getIJ(i,0) and put the result
10155  * in tuple **i** of returned DataArrayInt.
10156  * If ranges overlapped (in theory it should not) this method do not detect it and always returns the sub position of the first range.
10157  *
10158  * For example if \a this contains : [1,24,7,8,10,17] and \a ranges contains [(0,3),(3,8),(8,15),(15,22),(22,30)]
10159  * The return DataArrayInt will contain : **[1,2,4,0,2,2]**
10160  * This method is often called in pair with DataArrayInt::findRangeIdForEachTuple method.
10161  * 
10162  * \param [in] ranges typically come from output of MEDCouplingUMesh::ComputeRangesFromTypeDistribution. Each range is specified like this : 1st component is
10163  *             for lower value included and 2nd component is the upper value of corresponding range **excluded**.
10164  * \throw If offsets is a null pointer or does not have 2 components or if \a this is not allocated or \a this do not have exactly one component. To finish an exception
10165  *        is thrown if no ranges in \a ranges contains value in \a this.
10166  * \sa DataArrayInt::findRangeIdForEachTuple
10167  */
10168 DataArrayInt *DataArrayInt::findIdInRangeForEachTuple(const DataArrayInt *ranges) const
10169 {
10170   if(!ranges)
10171     throw INTERP_KERNEL::Exception("DataArrayInt::findIdInRangeForEachTuple : null input pointer !");
10172   if(ranges->getNumberOfComponents()!=2)
10173     throw INTERP_KERNEL::Exception("DataArrayInt::findIdInRangeForEachTuple : input DataArrayInt instance should have 2 components !");
10174   checkAllocated();
10175   if(getNumberOfComponents()!=1)
10176     throw INTERP_KERNEL::Exception("DataArrayInt::findIdInRangeForEachTuple : this should have only one component !");
10177   int nbTuples=getNumberOfTuples();
10178   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbTuples,1);
10179   int nbOfRanges=ranges->getNumberOfTuples();
10180   const int *rangesPtr=ranges->getConstPointer();
10181   int *retPtr=ret->getPointer();
10182   const int *inPtr=getConstPointer();
10183   for(int i=0;i<nbTuples;i++,retPtr++)
10184     {
10185       int val=inPtr[i];
10186       bool found=false;
10187       for(int j=0;j<nbOfRanges && !found;j++)
10188         if(val>=rangesPtr[2*j] && val<rangesPtr[2*j+1])
10189           { *retPtr=val-rangesPtr[2*j]; found=true; }
10190       if(found)
10191         continue;
10192       else
10193         {
10194           std::ostringstream oss; oss << "DataArrayInt::findIdInRangeForEachTuple : tuple #" << i << " not found by any ranges !";
10195           throw INTERP_KERNEL::Exception(oss.str().c_str());
10196         }
10197     }
10198   return ret.retn();
10199 }
10200
10201 /*!
10202  * 
10203  * \param [in] nbTimes specifies the nb of times each tuples in \a this will be duplicated contiguouly in returned DataArrayInt instance.
10204  *             \a nbTimes  should be at least equal to 1.
10205  * \return a newly allocated DataArrayInt having one component and number of tuples equal to \a nbTimes * \c this->getNumberOfTuples.
10206  * \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.
10207  */
10208 DataArrayInt *DataArrayInt::duplicateEachTupleNTimes(int nbTimes) const
10209 {
10210   checkAllocated();
10211   if(getNumberOfComponents()!=1)
10212     throw INTERP_KERNEL::Exception("DataArrayInt::duplicateEachTupleNTimes : this should have only one component !");
10213   if(nbTimes<1)
10214     throw INTERP_KERNEL::Exception("DataArrayInt::duplicateEachTupleNTimes : nb times should be >= 1 !");
10215   int nbTuples=getNumberOfTuples();
10216   const int *inPtr=getConstPointer();
10217   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbTimes*nbTuples,1);
10218   int *retPtr=ret->getPointer();
10219   for(int i=0;i<nbTuples;i++,inPtr++)
10220     {
10221       int val=*inPtr;
10222       for(int j=0;j<nbTimes;j++,retPtr++)
10223         *retPtr=val;
10224     }
10225   ret->copyStringInfoFrom(*this);
10226   return ret.retn();
10227 }
10228
10229 /*!
10230  * This method returns all different values found in \a this. This method throws if \a this has not been allocated.
10231  * But the number of components can be different from one.
10232  * \return a newly allocated array (that should be dealt by the caller) containing different values in \a this.
10233  */
10234 DataArrayInt *DataArrayInt::getDifferentValues() const
10235 {
10236   checkAllocated();
10237   std::set<int> ret;
10238   ret.insert(begin(),end());
10239   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret2=DataArrayInt::New(); ret2->alloc((int)ret.size(),1);
10240   std::copy(ret.begin(),ret.end(),ret2->getPointer());
10241   return ret2.retn();
10242 }
10243
10244 /*!
10245  * This method is a refinement of DataArrayInt::getDifferentValues because it returns not only different values in \a this but also, for each of
10246  * them it tells which tuple id have this id.
10247  * This method works only on arrays with one component (if it is not the case call DataArrayInt::rearrange(1) ).
10248  * This method returns two arrays having same size.
10249  * The instances of DataArrayInt in the returned vector have be specially allocated and computed by this method. Each of them should be dealt by the caller of this method.
10250  * Example : if this is equal to [1,0,1,2,0,2,2,-3,2] -> differentIds=[-3,0,1,2] and returned array will be equal to [[7],[1,4],[0,2],[3,5,6,8]]
10251  */
10252 std::vector<DataArrayInt *> DataArrayInt::partitionByDifferentValues(std::vector<int>& differentIds) const
10253 {
10254   checkAllocated();
10255   if(getNumberOfComponents()!=1)
10256     throw INTERP_KERNEL::Exception("DataArrayInt::partitionByDifferentValues : this should have only one component !");
10257   int id=0;
10258   std::map<int,int> m,m2,m3;
10259   for(const int *w=begin();w!=end();w++)
10260     m[*w]++;
10261   differentIds.resize(m.size());
10262   std::vector<DataArrayInt *> ret(m.size());
10263   std::vector<int *> retPtr(m.size());
10264   for(std::map<int,int>::const_iterator it=m.begin();it!=m.end();it++,id++)
10265     {
10266       m2[(*it).first]=id;
10267       ret[id]=DataArrayInt::New();
10268       ret[id]->alloc((*it).second,1);
10269       retPtr[id]=ret[id]->getPointer();
10270       differentIds[id]=(*it).first;
10271     }
10272   id=0;
10273   for(const int *w=begin();w!=end();w++,id++)
10274     {
10275       retPtr[m2[*w]][m3[*w]++]=id;
10276     }
10277   return ret;
10278 }
10279
10280 /*!
10281  * This method split ids in [0, \c this->getNumberOfTuples() ) using \a this array as a field of weight (>=0 each).
10282  * The aim of this method is to return a set of \a nbOfSlices chunk of contiguous ids as balanced as possible.
10283  *
10284  * \param [in] nbOfSlices - number of slices expected.
10285  * \return - a vector having a size equal to \a nbOfSlices giving the start (included) and the stop (excluded) of each chunks.
10286  * 
10287  * \sa DataArray::GetSlice
10288  * \throw If \a this is not allocated or not with exactly one component.
10289  * \throw If an element in \a this if < 0.
10290  */
10291 std::vector< std::pair<int,int> > DataArrayInt::splitInBalancedSlices(int nbOfSlices) const
10292 {
10293   if(!isAllocated() || getNumberOfComponents()!=1)
10294     throw INTERP_KERNEL::Exception("DataArrayInt::splitInBalancedSlices : this array should have number of components equal to one and must be allocated !");
10295   if(nbOfSlices<=0)
10296     throw INTERP_KERNEL::Exception("DataArrayInt::splitInBalancedSlices : number of slices must be >= 1 !");
10297   int sum(accumulate(0)),nbOfTuples(getNumberOfTuples());
10298   int sumPerSlc(sum/nbOfSlices),pos(0);
10299   const int *w(begin());
10300   std::vector< std::pair<int,int> > ret(nbOfSlices);
10301   for(int i=0;i<nbOfSlices;i++)
10302     {
10303       std::pair<int,int> p(pos,-1);
10304       int locSum(0);
10305       while(locSum<sumPerSlc && pos<nbOfTuples) { pos++; locSum+=*w++; }
10306       if(i!=nbOfSlices-1)
10307         p.second=pos;
10308       else
10309         p.second=nbOfTuples;
10310       ret[i]=p;
10311     }
10312   return ret;
10313 }
10314
10315 /*!
10316  * Returns a new DataArrayInt that is a sum of two given arrays. There are 3
10317  * valid cases.
10318  * 1.  The arrays have same number of tuples and components. Then each value of
10319  *   the result array (_a_) is a sum of the corresponding values of \a a1 and \a a2,
10320  *   i.e.: _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, j ].
10321  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
10322  *   component. Then
10323  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, 0 ].
10324  * 3.  The arrays have same number of components and one array, say _a2_, has one
10325  *   tuple. Then
10326  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ 0, j ].
10327  *
10328  * Info on components is copied either from the first array (in the first case) or from
10329  * the array with maximal number of elements (getNbOfElems()).
10330  *  \param [in] a1 - an array to sum up.
10331  *  \param [in] a2 - another array to sum up.
10332  *  \return DataArrayInt * - the new instance of DataArrayInt.
10333  *          The caller is to delete this result array using decrRef() as it is no more
10334  *          needed.
10335  *  \throw If either \a a1 or \a a2 is NULL.
10336  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
10337  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
10338  *         none of them has number of tuples or components equal to 1.
10339  */
10340 DataArrayInt *DataArrayInt::Add(const DataArrayInt *a1, const DataArrayInt *a2)
10341 {
10342   if(!a1 || !a2)
10343     throw INTERP_KERNEL::Exception("DataArrayInt::Add : input DataArrayInt instance is NULL !");
10344   int nbOfTuple=a1->getNumberOfTuples();
10345   int nbOfTuple2=a2->getNumberOfTuples();
10346   int nbOfComp=a1->getNumberOfComponents();
10347   int nbOfComp2=a2->getNumberOfComponents();
10348   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=0;
10349   if(nbOfTuple==nbOfTuple2)
10350     {
10351       if(nbOfComp==nbOfComp2)
10352         {
10353           ret=DataArrayInt::New();
10354           ret->alloc(nbOfTuple,nbOfComp);
10355           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<int>());
10356           ret->copyStringInfoFrom(*a1);
10357         }
10358       else
10359         {
10360           int nbOfCompMin,nbOfCompMax;
10361           const DataArrayInt *aMin, *aMax;
10362           if(nbOfComp>nbOfComp2)
10363             {
10364               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
10365               aMin=a2; aMax=a1;
10366             }
10367           else
10368             {
10369               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
10370               aMin=a1; aMax=a2;
10371             }
10372           if(nbOfCompMin==1)
10373             {
10374               ret=DataArrayInt::New();
10375               ret->alloc(nbOfTuple,nbOfCompMax);
10376               const int *aMinPtr=aMin->getConstPointer();
10377               const int *aMaxPtr=aMax->getConstPointer();
10378               int *res=ret->getPointer();
10379               for(int i=0;i<nbOfTuple;i++)
10380                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::plus<int>(),aMinPtr[i]));
10381               ret->copyStringInfoFrom(*aMax);
10382             }
10383           else
10384             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
10385         }
10386     }
10387   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
10388     {
10389       if(nbOfComp==nbOfComp2)
10390         {
10391           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
10392           const DataArrayInt *aMin=nbOfTuple>nbOfTuple2?a2:a1;
10393           const DataArrayInt *aMax=nbOfTuple>nbOfTuple2?a1:a2;
10394           const int *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
10395           ret=DataArrayInt::New();
10396           ret->alloc(nbOfTupleMax,nbOfComp);
10397           int *res=ret->getPointer();
10398           for(int i=0;i<nbOfTupleMax;i++)
10399             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::plus<int>());
10400           ret->copyStringInfoFrom(*aMax);
10401         }
10402       else
10403         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
10404     }
10405   else
10406     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !");
10407   return ret.retn();
10408 }
10409
10410 /*!
10411  * Adds values of another DataArrayInt to values of \a this one. There are 3
10412  * valid cases.
10413  * 1.  The arrays have same number of tuples and components. Then each value of
10414  *   \a other array is added to the corresponding value of \a this array, i.e.:
10415  *   _a_ [ i, j ] += _other_ [ i, j ].
10416  * 2.  The arrays have same number of tuples and \a other array has one component. Then
10417  *   _a_ [ i, j ] += _other_ [ i, 0 ].
10418  * 3.  The arrays have same number of components and \a other array has one tuple. Then
10419  *   _a_ [ i, j ] += _a2_ [ 0, j ].
10420  *
10421  *  \param [in] other - an array to add to \a this one.
10422  *  \throw If \a other is NULL.
10423  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
10424  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
10425  *         \a other has number of both tuples and components not equal to 1.
10426  */
10427 void DataArrayInt::addEqual(const DataArrayInt *other)
10428 {
10429   if(!other)
10430     throw INTERP_KERNEL::Exception("DataArrayInt::addEqual : input DataArrayInt instance is NULL !");
10431   const char *msg="Nb of tuples mismatch for DataArrayInt::addEqual  !";
10432   checkAllocated(); other->checkAllocated();
10433   int nbOfTuple=getNumberOfTuples();
10434   int nbOfTuple2=other->getNumberOfTuples();
10435   int nbOfComp=getNumberOfComponents();
10436   int nbOfComp2=other->getNumberOfComponents();
10437   if(nbOfTuple==nbOfTuple2)
10438     {
10439       if(nbOfComp==nbOfComp2)
10440         {
10441           std::transform(begin(),end(),other->begin(),getPointer(),std::plus<int>());
10442         }
10443       else if(nbOfComp2==1)
10444         {
10445           int *ptr=getPointer();
10446           const int *ptrc=other->getConstPointer();
10447           for(int i=0;i<nbOfTuple;i++)
10448             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::plus<int>(),*ptrc++));
10449         }
10450       else
10451         throw INTERP_KERNEL::Exception(msg);
10452     }
10453   else if(nbOfTuple2==1)
10454     {
10455       if(nbOfComp2==nbOfComp)
10456         {
10457           int *ptr=getPointer();
10458           const int *ptrc=other->getConstPointer();
10459           for(int i=0;i<nbOfTuple;i++)
10460             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::plus<int>());
10461         }
10462       else
10463         throw INTERP_KERNEL::Exception(msg);
10464     }
10465   else
10466     throw INTERP_KERNEL::Exception(msg);
10467   declareAsNew();
10468 }
10469
10470 /*!
10471  * Returns a new DataArrayInt that is a subtraction of two given arrays. There are 3
10472  * valid cases.
10473  * 1.  The arrays have same number of tuples and components. Then each value of
10474  *   the result array (_a_) is a subtraction of the corresponding values of \a a1 and
10475  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, j ].
10476  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
10477  *   component. Then
10478  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, 0 ].
10479  * 3.  The arrays have same number of components and one array, say _a2_, has one
10480  *   tuple. Then
10481  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ 0, j ].
10482  *
10483  * Info on components is copied either from the first array (in the first case) or from
10484  * the array with maximal number of elements (getNbOfElems()).
10485  *  \param [in] a1 - an array to subtract from.
10486  *  \param [in] a2 - an array to subtract.
10487  *  \return DataArrayInt * - the new instance of DataArrayInt.
10488  *          The caller is to delete this result array using decrRef() as it is no more
10489  *          needed.
10490  *  \throw If either \a a1 or \a a2 is NULL.
10491  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
10492  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
10493  *         none of them has number of tuples or components equal to 1.
10494  */
10495 DataArrayInt *DataArrayInt::Substract(const DataArrayInt *a1, const DataArrayInt *a2)
10496 {
10497   if(!a1 || !a2)
10498     throw INTERP_KERNEL::Exception("DataArrayInt::Substract : input DataArrayInt instance is NULL !");
10499   int nbOfTuple1=a1->getNumberOfTuples();
10500   int nbOfTuple2=a2->getNumberOfTuples();
10501   int nbOfComp1=a1->getNumberOfComponents();
10502   int nbOfComp2=a2->getNumberOfComponents();
10503   if(nbOfTuple2==nbOfTuple1)
10504     {
10505       if(nbOfComp1==nbOfComp2)
10506         {
10507           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10508           ret->alloc(nbOfTuple2,nbOfComp1);
10509           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus<int>());
10510           ret->copyStringInfoFrom(*a1);
10511           return ret.retn();
10512         }
10513       else if(nbOfComp2==1)
10514         {
10515           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10516           ret->alloc(nbOfTuple1,nbOfComp1);
10517           const int *a2Ptr=a2->getConstPointer();
10518           const int *a1Ptr=a1->getConstPointer();
10519           int *res=ret->getPointer();
10520           for(int i=0;i<nbOfTuple1;i++)
10521             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::minus<int>(),a2Ptr[i]));
10522           ret->copyStringInfoFrom(*a1);
10523           return ret.retn();
10524         }
10525       else
10526         {
10527           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
10528           return 0;
10529         }
10530     }
10531   else if(nbOfTuple2==1)
10532     {
10533       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
10534       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10535       ret->alloc(nbOfTuple1,nbOfComp1);
10536       const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
10537       int *pt=ret->getPointer();
10538       for(int i=0;i<nbOfTuple1;i++)
10539         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::minus<int>());
10540       ret->copyStringInfoFrom(*a1);
10541       return ret.retn();
10542     }
10543   else
10544     {
10545       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception
10546       return 0;
10547     }
10548 }
10549
10550 /*!
10551  * Subtract values of another DataArrayInt from values of \a this one. There are 3
10552  * valid cases.
10553  * 1.  The arrays have same number of tuples and components. Then each value of
10554  *   \a other array is subtracted from the corresponding value of \a this array, i.e.:
10555  *   _a_ [ i, j ] -= _other_ [ i, j ].
10556  * 2.  The arrays have same number of tuples and \a other array has one component. Then
10557  *   _a_ [ i, j ] -= _other_ [ i, 0 ].
10558  * 3.  The arrays have same number of components and \a other array has one tuple. Then
10559  *   _a_ [ i, j ] -= _a2_ [ 0, j ].
10560  *
10561  *  \param [in] other - an array to subtract from \a this one.
10562  *  \throw If \a other is NULL.
10563  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
10564  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
10565  *         \a other has number of both tuples and components not equal to 1.
10566  */
10567 void DataArrayInt::substractEqual(const DataArrayInt *other)
10568 {
10569   if(!other)
10570     throw INTERP_KERNEL::Exception("DataArrayInt::substractEqual : input DataArrayInt instance is NULL !");
10571   const char *msg="Nb of tuples mismatch for DataArrayInt::substractEqual  !";
10572   checkAllocated(); other->checkAllocated();
10573   int nbOfTuple=getNumberOfTuples();
10574   int nbOfTuple2=other->getNumberOfTuples();
10575   int nbOfComp=getNumberOfComponents();
10576   int nbOfComp2=other->getNumberOfComponents();
10577   if(nbOfTuple==nbOfTuple2)
10578     {
10579       if(nbOfComp==nbOfComp2)
10580         {
10581           std::transform(begin(),end(),other->begin(),getPointer(),std::minus<int>());
10582         }
10583       else if(nbOfComp2==1)
10584         {
10585           int *ptr=getPointer();
10586           const int *ptrc=other->getConstPointer();
10587           for(int i=0;i<nbOfTuple;i++)
10588             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::minus<int>(),*ptrc++));
10589         }
10590       else
10591         throw INTERP_KERNEL::Exception(msg);
10592     }
10593   else if(nbOfTuple2==1)
10594     {
10595       int *ptr=getPointer();
10596       const int *ptrc=other->getConstPointer();
10597       for(int i=0;i<nbOfTuple;i++)
10598         std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::minus<int>());
10599     }
10600   else
10601     throw INTERP_KERNEL::Exception(msg);
10602   declareAsNew();
10603 }
10604
10605 /*!
10606  * Returns a new DataArrayInt that is a product of two given arrays. There are 3
10607  * valid cases.
10608  * 1.  The arrays have same number of tuples and components. Then each value of
10609  *   the result array (_a_) is a product of the corresponding values of \a a1 and
10610  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, j ].
10611  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
10612  *   component. Then
10613  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, 0 ].
10614  * 3.  The arrays have same number of components and one array, say _a2_, has one
10615  *   tuple. Then
10616  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ 0, j ].
10617  *
10618  * Info on components is copied either from the first array (in the first case) or from
10619  * the array with maximal number of elements (getNbOfElems()).
10620  *  \param [in] a1 - a factor array.
10621  *  \param [in] a2 - another factor array.
10622  *  \return DataArrayInt * - the new instance of DataArrayInt.
10623  *          The caller is to delete this result array using decrRef() as it is no more
10624  *          needed.
10625  *  \throw If either \a a1 or \a a2 is NULL.
10626  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
10627  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
10628  *         none of them has number of tuples or components equal to 1.
10629  */
10630 DataArrayInt *DataArrayInt::Multiply(const DataArrayInt *a1, const DataArrayInt *a2)
10631 {
10632   if(!a1 || !a2)
10633     throw INTERP_KERNEL::Exception("DataArrayInt::Multiply : input DataArrayInt instance is NULL !");
10634   int nbOfTuple=a1->getNumberOfTuples();
10635   int nbOfTuple2=a2->getNumberOfTuples();
10636   int nbOfComp=a1->getNumberOfComponents();
10637   int nbOfComp2=a2->getNumberOfComponents();
10638   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=0;
10639   if(nbOfTuple==nbOfTuple2)
10640     {
10641       if(nbOfComp==nbOfComp2)
10642         {
10643           ret=DataArrayInt::New();
10644           ret->alloc(nbOfTuple,nbOfComp);
10645           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies<int>());
10646           ret->copyStringInfoFrom(*a1);
10647         }
10648       else
10649         {
10650           int nbOfCompMin,nbOfCompMax;
10651           const DataArrayInt *aMin, *aMax;
10652           if(nbOfComp>nbOfComp2)
10653             {
10654               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
10655               aMin=a2; aMax=a1;
10656             }
10657           else
10658             {
10659               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
10660               aMin=a1; aMax=a2;
10661             }
10662           if(nbOfCompMin==1)
10663             {
10664               ret=DataArrayInt::New();
10665               ret->alloc(nbOfTuple,nbOfCompMax);
10666               const int *aMinPtr=aMin->getConstPointer();
10667               const int *aMaxPtr=aMax->getConstPointer();
10668               int *res=ret->getPointer();
10669               for(int i=0;i<nbOfTuple;i++)
10670                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::multiplies<int>(),aMinPtr[i]));
10671               ret->copyStringInfoFrom(*aMax);
10672             }
10673           else
10674             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
10675         }
10676     }
10677   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
10678     {
10679       if(nbOfComp==nbOfComp2)
10680         {
10681           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
10682           const DataArrayInt *aMin=nbOfTuple>nbOfTuple2?a2:a1;
10683           const DataArrayInt *aMax=nbOfTuple>nbOfTuple2?a1:a2;
10684           const int *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
10685           ret=DataArrayInt::New();
10686           ret->alloc(nbOfTupleMax,nbOfComp);
10687           int *res=ret->getPointer();
10688           for(int i=0;i<nbOfTupleMax;i++)
10689             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::multiplies<int>());
10690           ret->copyStringInfoFrom(*aMax);
10691         }
10692       else
10693         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
10694     }
10695   else
10696     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !");
10697   return ret.retn();
10698 }
10699
10700
10701 /*!
10702  * Multiply values of another DataArrayInt to values of \a this one. There are 3
10703  * valid cases.
10704  * 1.  The arrays have same number of tuples and components. Then each value of
10705  *   \a other array is multiplied to the corresponding value of \a this array, i.e.:
10706  *   _a_ [ i, j ] *= _other_ [ i, j ].
10707  * 2.  The arrays have same number of tuples and \a other array has one component. Then
10708  *   _a_ [ i, j ] *= _other_ [ i, 0 ].
10709  * 3.  The arrays have same number of components and \a other array has one tuple. Then
10710  *   _a_ [ i, j ] *= _a2_ [ 0, j ].
10711  *
10712  *  \param [in] other - an array to multiply to \a this one.
10713  *  \throw If \a other is NULL.
10714  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
10715  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
10716  *         \a other has number of both tuples and components not equal to 1.
10717  */
10718 void DataArrayInt::multiplyEqual(const DataArrayInt *other)
10719 {
10720   if(!other)
10721     throw INTERP_KERNEL::Exception("DataArrayInt::multiplyEqual : input DataArrayInt instance is NULL !");
10722   const char *msg="Nb of tuples mismatch for DataArrayInt::multiplyEqual !";
10723   checkAllocated(); other->checkAllocated();
10724   int nbOfTuple=getNumberOfTuples();
10725   int nbOfTuple2=other->getNumberOfTuples();
10726   int nbOfComp=getNumberOfComponents();
10727   int nbOfComp2=other->getNumberOfComponents();
10728   if(nbOfTuple==nbOfTuple2)
10729     {
10730       if(nbOfComp==nbOfComp2)
10731         {
10732           std::transform(begin(),end(),other->begin(),getPointer(),std::multiplies<int>());
10733         }
10734       else if(nbOfComp2==1)
10735         {
10736           int *ptr=getPointer();
10737           const int *ptrc=other->getConstPointer();
10738           for(int i=0;i<nbOfTuple;i++)
10739             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::multiplies<int>(),*ptrc++));    
10740         }
10741       else
10742         throw INTERP_KERNEL::Exception(msg);
10743     }
10744   else if(nbOfTuple2==1)
10745     {
10746       if(nbOfComp2==nbOfComp)
10747         {
10748           int *ptr=getPointer();
10749           const int *ptrc=other->getConstPointer();
10750           for(int i=0;i<nbOfTuple;i++)
10751             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::multiplies<int>());
10752         }
10753       else
10754         throw INTERP_KERNEL::Exception(msg);
10755     }
10756   else
10757     throw INTERP_KERNEL::Exception(msg);
10758   declareAsNew();
10759 }
10760
10761
10762 /*!
10763  * Returns a new DataArrayInt that is a division of two given arrays. There are 3
10764  * valid cases.
10765  * 1.  The arrays have same number of tuples and components. Then each value of
10766  *   the result array (_a_) is a division of the corresponding values of \a a1 and
10767  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, j ].
10768  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
10769  *   component. Then
10770  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, 0 ].
10771  * 3.  The arrays have same number of components and one array, say _a2_, has one
10772  *   tuple. Then
10773  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ 0, j ].
10774  *
10775  * Info on components is copied either from the first array (in the first case) or from
10776  * the array with maximal number of elements (getNbOfElems()).
10777  *  \warning No check of division by zero is performed!
10778  *  \param [in] a1 - a numerator array.
10779  *  \param [in] a2 - a denominator array.
10780  *  \return DataArrayInt * - the new instance of DataArrayInt.
10781  *          The caller is to delete this result array using decrRef() as it is no more
10782  *          needed.
10783  *  \throw If either \a a1 or \a a2 is NULL.
10784  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
10785  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
10786  *         none of them has number of tuples or components equal to 1.
10787  */
10788 DataArrayInt *DataArrayInt::Divide(const DataArrayInt *a1, const DataArrayInt *a2)
10789 {
10790   if(!a1 || !a2)
10791     throw INTERP_KERNEL::Exception("DataArrayInt::Divide : input DataArrayInt instance is NULL !");
10792   int nbOfTuple1=a1->getNumberOfTuples();
10793   int nbOfTuple2=a2->getNumberOfTuples();
10794   int nbOfComp1=a1->getNumberOfComponents();
10795   int nbOfComp2=a2->getNumberOfComponents();
10796   if(nbOfTuple2==nbOfTuple1)
10797     {
10798       if(nbOfComp1==nbOfComp2)
10799         {
10800           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10801           ret->alloc(nbOfTuple2,nbOfComp1);
10802           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides<int>());
10803           ret->copyStringInfoFrom(*a1);
10804           return ret.retn();
10805         }
10806       else if(nbOfComp2==1)
10807         {
10808           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10809           ret->alloc(nbOfTuple1,nbOfComp1);
10810           const int *a2Ptr=a2->getConstPointer();
10811           const int *a1Ptr=a1->getConstPointer();
10812           int *res=ret->getPointer();
10813           for(int i=0;i<nbOfTuple1;i++)
10814             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::divides<int>(),a2Ptr[i]));
10815           ret->copyStringInfoFrom(*a1);
10816           return ret.retn();
10817         }
10818       else
10819         {
10820           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
10821           return 0;
10822         }
10823     }
10824   else if(nbOfTuple2==1)
10825     {
10826       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
10827       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10828       ret->alloc(nbOfTuple1,nbOfComp1);
10829       const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
10830       int *pt=ret->getPointer();
10831       for(int i=0;i<nbOfTuple1;i++)
10832         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::divides<int>());
10833       ret->copyStringInfoFrom(*a1);
10834       return ret.retn();
10835     }
10836   else
10837     {
10838       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception
10839       return 0;
10840     }
10841 }
10842
10843 /*!
10844  * Divide values of \a this array by values of another DataArrayInt. There are 3
10845  * valid cases.
10846  * 1.  The arrays have same number of tuples and components. Then each value of
10847  *    \a this array is divided by the corresponding value of \a other one, i.e.:
10848  *   _a_ [ i, j ] /= _other_ [ i, j ].
10849  * 2.  The arrays have same number of tuples and \a other array has one component. Then
10850  *   _a_ [ i, j ] /= _other_ [ i, 0 ].
10851  * 3.  The arrays have same number of components and \a other array has one tuple. Then
10852  *   _a_ [ i, j ] /= _a2_ [ 0, j ].
10853  *
10854  *  \warning No check of division by zero is performed!
10855  *  \param [in] other - an array to divide \a this one by.
10856  *  \throw If \a other is NULL.
10857  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
10858  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
10859  *         \a other has number of both tuples and components not equal to 1.
10860  */
10861 void DataArrayInt::divideEqual(const DataArrayInt *other)
10862 {
10863   if(!other)
10864     throw INTERP_KERNEL::Exception("DataArrayInt::divideEqual : input DataArrayInt instance is NULL !");
10865   const char *msg="Nb of tuples mismatch for DataArrayInt::divideEqual !";
10866   checkAllocated(); other->checkAllocated();
10867   int nbOfTuple=getNumberOfTuples();
10868   int nbOfTuple2=other->getNumberOfTuples();
10869   int nbOfComp=getNumberOfComponents();
10870   int nbOfComp2=other->getNumberOfComponents();
10871   if(nbOfTuple==nbOfTuple2)
10872     {
10873       if(nbOfComp==nbOfComp2)
10874         {
10875           std::transform(begin(),end(),other->begin(),getPointer(),std::divides<int>());
10876         }
10877       else if(nbOfComp2==1)
10878         {
10879           int *ptr=getPointer();
10880           const int *ptrc=other->getConstPointer();
10881           for(int i=0;i<nbOfTuple;i++)
10882             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::divides<int>(),*ptrc++));
10883         }
10884       else
10885         throw INTERP_KERNEL::Exception(msg);
10886     }
10887   else if(nbOfTuple2==1)
10888     {
10889       if(nbOfComp2==nbOfComp)
10890         {
10891           int *ptr=getPointer();
10892           const int *ptrc=other->getConstPointer();
10893           for(int i=0;i<nbOfTuple;i++)
10894             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::divides<int>());
10895         }
10896       else
10897         throw INTERP_KERNEL::Exception(msg);
10898     }
10899   else
10900     throw INTERP_KERNEL::Exception(msg);
10901   declareAsNew();
10902 }
10903
10904
10905 /*!
10906  * Returns a new DataArrayInt that is a modulus of two given arrays. There are 3
10907  * valid cases.
10908  * 1.  The arrays have same number of tuples and components. Then each value of
10909  *   the result array (_a_) is a division of the corresponding values of \a a1 and
10910  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] % _a2_ [ i, j ].
10911  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
10912  *   component. Then
10913  *   _a_ [ i, j ] = _a1_ [ i, j ] % _a2_ [ i, 0 ].
10914  * 3.  The arrays have same number of components and one array, say _a2_, has one
10915  *   tuple. Then
10916  *   _a_ [ i, j ] = _a1_ [ i, j ] % _a2_ [ 0, j ].
10917  *
10918  * Info on components is copied either from the first array (in the first case) or from
10919  * the array with maximal number of elements (getNbOfElems()).
10920  *  \warning No check of division by zero is performed!
10921  *  \param [in] a1 - a dividend array.
10922  *  \param [in] a2 - a divisor array.
10923  *  \return DataArrayInt * - the new instance of DataArrayInt.
10924  *          The caller is to delete this result array using decrRef() as it is no more
10925  *          needed.
10926  *  \throw If either \a a1 or \a a2 is NULL.
10927  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
10928  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
10929  *         none of them has number of tuples or components equal to 1.
10930  */
10931 DataArrayInt *DataArrayInt::Modulus(const DataArrayInt *a1, const DataArrayInt *a2)
10932 {
10933     if(!a1 || !a2)
10934     throw INTERP_KERNEL::Exception("DataArrayInt::Modulus : input DataArrayInt instance is NULL !");
10935   int nbOfTuple1=a1->getNumberOfTuples();
10936   int nbOfTuple2=a2->getNumberOfTuples();
10937   int nbOfComp1=a1->getNumberOfComponents();
10938   int nbOfComp2=a2->getNumberOfComponents();
10939   if(nbOfTuple2==nbOfTuple1)
10940     {
10941       if(nbOfComp1==nbOfComp2)
10942         {
10943           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10944           ret->alloc(nbOfTuple2,nbOfComp1);
10945           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::modulus<int>());
10946           ret->copyStringInfoFrom(*a1);
10947           return ret.retn();
10948         }
10949       else if(nbOfComp2==1)
10950         {
10951           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10952           ret->alloc(nbOfTuple1,nbOfComp1);
10953           const int *a2Ptr=a2->getConstPointer();
10954           const int *a1Ptr=a1->getConstPointer();
10955           int *res=ret->getPointer();
10956           for(int i=0;i<nbOfTuple1;i++)
10957             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::modulus<int>(),a2Ptr[i]));
10958           ret->copyStringInfoFrom(*a1);
10959           return ret.retn();
10960         }
10961       else
10962         {
10963           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Modulus !");
10964           return 0;
10965         }
10966     }
10967   else if(nbOfTuple2==1)
10968     {
10969       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Modulus !");
10970       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10971       ret->alloc(nbOfTuple1,nbOfComp1);
10972       const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
10973       int *pt=ret->getPointer();
10974       for(int i=0;i<nbOfTuple1;i++)
10975         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::modulus<int>());
10976       ret->copyStringInfoFrom(*a1);
10977       return ret.retn();
10978     }
10979   else
10980     {
10981       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Modulus !");//will always throw an exception
10982       return 0;
10983     }
10984 }
10985
10986 /*!
10987  * Modify \a this array so that each value becomes a modulus of division of this value by
10988  * a value of another DataArrayInt. There are 3 valid cases.
10989  * 1.  The arrays have same number of tuples and components. Then each value of
10990  *    \a this array is divided by the corresponding value of \a other one, i.e.:
10991  *   _a_ [ i, j ] %= _other_ [ i, j ].
10992  * 2.  The arrays have same number of tuples and \a other array has one component. Then
10993  *   _a_ [ i, j ] %= _other_ [ i, 0 ].
10994  * 3.  The arrays have same number of components and \a other array has one tuple. Then
10995  *   _a_ [ i, j ] %= _a2_ [ 0, j ].
10996  *
10997  *  \warning No check of division by zero is performed!
10998  *  \param [in] other - a divisor array.
10999  *  \throw If \a other is NULL.
11000  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
11001  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
11002  *         \a other has number of both tuples and components not equal to 1.
11003  */
11004 void DataArrayInt::modulusEqual(const DataArrayInt *other)
11005 {
11006   if(!other)
11007     throw INTERP_KERNEL::Exception("DataArrayInt::modulusEqual : input DataArrayInt instance is NULL !");
11008   const char *msg="Nb of tuples mismatch for DataArrayInt::modulusEqual !";
11009   checkAllocated(); other->checkAllocated();
11010   int nbOfTuple=getNumberOfTuples();
11011   int nbOfTuple2=other->getNumberOfTuples();
11012   int nbOfComp=getNumberOfComponents();
11013   int nbOfComp2=other->getNumberOfComponents();
11014   if(nbOfTuple==nbOfTuple2)
11015     {
11016       if(nbOfComp==nbOfComp2)
11017         {
11018           std::transform(begin(),end(),other->begin(),getPointer(),std::modulus<int>());
11019         }
11020       else if(nbOfComp2==1)
11021         {
11022           if(nbOfComp2==nbOfComp)
11023             {
11024               int *ptr=getPointer();
11025               const int *ptrc=other->getConstPointer();
11026               for(int i=0;i<nbOfTuple;i++)
11027                 std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::modulus<int>(),*ptrc++));
11028             }
11029           else
11030             throw INTERP_KERNEL::Exception(msg);
11031         }
11032       else
11033         throw INTERP_KERNEL::Exception(msg);
11034     }
11035   else if(nbOfTuple2==1)
11036     {
11037       int *ptr=getPointer();
11038       const int *ptrc=other->getConstPointer();
11039       for(int i=0;i<nbOfTuple;i++)
11040         std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::modulus<int>());
11041     }
11042   else
11043     throw INTERP_KERNEL::Exception(msg);
11044   declareAsNew();
11045 }
11046
11047 /*!
11048  * Returns a new DataArrayInt that is the result of pow of two given arrays. There are 3
11049  * valid cases.
11050  *
11051  *  \param [in] a1 - an array to pow up.
11052  *  \param [in] a2 - another array to sum up.
11053  *  \return DataArrayInt * - the new instance of DataArrayInt.
11054  *          The caller is to delete this result array using decrRef() as it is no more
11055  *          needed.
11056  *  \throw If either \a a1 or \a a2 is NULL.
11057  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
11058  *  \throw If \a a1->getNumberOfComponents() != 1 or \a a2->getNumberOfComponents() != 1.
11059  *  \throw If there is a negative value in \a a2.
11060  */
11061 DataArrayInt *DataArrayInt::Pow(const DataArrayInt *a1, const DataArrayInt *a2)
11062 {
11063   if(!a1 || !a2)
11064     throw INTERP_KERNEL::Exception("DataArrayInt::Pow : at least one of input instances is null !");
11065   int nbOfTuple=a1->getNumberOfTuples();
11066   int nbOfTuple2=a2->getNumberOfTuples();
11067   int nbOfComp=a1->getNumberOfComponents();
11068   int nbOfComp2=a2->getNumberOfComponents();
11069   if(nbOfTuple!=nbOfTuple2)
11070     throw INTERP_KERNEL::Exception("DataArrayInt::Pow : number of tuples mismatches !");
11071   if(nbOfComp!=1 || nbOfComp2!=1)
11072     throw INTERP_KERNEL::Exception("DataArrayInt::Pow : number of components of both arrays must be equal to 1 !");
11073   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbOfTuple,1);
11074   const int *ptr1(a1->begin()),*ptr2(a2->begin());
11075   int *ptr=ret->getPointer();
11076   for(int i=0;i<nbOfTuple;i++,ptr1++,ptr2++,ptr++)
11077     {
11078       if(*ptr2>=0)
11079         {
11080           int tmp=1;
11081           for(int j=0;j<*ptr2;j++)
11082             tmp*=*ptr1;
11083           *ptr=tmp;
11084         }
11085       else
11086         {
11087           std::ostringstream oss; oss << "DataArrayInt::Pow : on tuple #" << i << " of a2 value is < 0 (" << *ptr2 << ") !";
11088           throw INTERP_KERNEL::Exception(oss.str().c_str());
11089         }
11090     }
11091   return ret.retn();
11092 }
11093
11094 /*!
11095  * Apply pow on values of another DataArrayInt to values of \a this one.
11096  *
11097  *  \param [in] other - an array to pow to \a this one.
11098  *  \throw If \a other is NULL.
11099  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples()
11100  *  \throw If \a this->getNumberOfComponents() != 1 or \a other->getNumberOfComponents() != 1
11101  *  \throw If there is a negative value in \a other.
11102  */
11103 void DataArrayInt::powEqual(const DataArrayInt *other)
11104 {
11105   if(!other)
11106     throw INTERP_KERNEL::Exception("DataArrayInt::powEqual : input instance is null !");
11107   int nbOfTuple=getNumberOfTuples();
11108   int nbOfTuple2=other->getNumberOfTuples();
11109   int nbOfComp=getNumberOfComponents();
11110   int nbOfComp2=other->getNumberOfComponents();
11111   if(nbOfTuple!=nbOfTuple2)
11112     throw INTERP_KERNEL::Exception("DataArrayInt::powEqual : number of tuples mismatches !");
11113   if(nbOfComp!=1 || nbOfComp2!=1)
11114     throw INTERP_KERNEL::Exception("DataArrayInt::powEqual : number of components of both arrays must be equal to 1 !");
11115   int *ptr=getPointer();
11116   const int *ptrc=other->begin();
11117   for(int i=0;i<nbOfTuple;i++,ptrc++,ptr++)
11118     {
11119       if(*ptrc>=0)
11120         {
11121           int tmp=1;
11122           for(int j=0;j<*ptrc;j++)
11123             tmp*=*ptr;
11124           *ptr=tmp;
11125         }
11126       else
11127         {
11128           std::ostringstream oss; oss << "DataArrayInt::powEqual : on tuple #" << i << " of other value is < 0 (" << *ptrc << ") !";
11129           throw INTERP_KERNEL::Exception(oss.str().c_str());
11130         }
11131     }
11132   declareAsNew();
11133 }
11134
11135 /*!
11136  * Returns a C array which is a renumbering map in "Old to New" mode for the input array.
11137  * This map, if applied to \a start array, would make it sorted. For example, if
11138  * \a start array contents are [9,10,0,6,4,11,3,7] then the contents of the result array is
11139  * [5,6,0,3,2,7,1,4].
11140  *  \param [in] start - pointer to the first element of the array for which the
11141  *         permutation map is computed.
11142  *  \param [in] end - pointer specifying the end of the array \a start, so that
11143  *         the last value of \a start is \a end[ -1 ].
11144  *  \return int * - the result permutation array that the caller is to delete as it is no
11145  *         more needed.
11146  *  \throw If there are equal values in the input array.
11147  */
11148 int *DataArrayInt::CheckAndPreparePermutation(const int *start, const int *end)
11149 {
11150   std::size_t sz=std::distance(start,end);
11151   int *ret=(int *)malloc(sz*sizeof(int));
11152   int *work=new int[sz];
11153   std::copy(start,end,work);
11154   std::sort(work,work+sz);
11155   if(std::unique(work,work+sz)!=work+sz)
11156     {
11157       delete [] work;
11158       free(ret);
11159       throw INTERP_KERNEL::Exception("Some elements are equals in the specified array !");
11160     }
11161   std::map<int,int> m;
11162   for(int *workPt=work;workPt!=work+sz;workPt++)
11163     m[*workPt]=(int)std::distance(work,workPt);
11164   int *iter2=ret;
11165   for(const int *iter=start;iter!=end;iter++,iter2++)
11166     *iter2=m[*iter];
11167   delete [] work;
11168   return ret;
11169 }
11170
11171 /*!
11172  * Returns a new DataArrayInt containing an arithmetic progression
11173  * that is equal to the sequence returned by Python \c range(\a begin,\a  end,\a  step )
11174  * function.
11175  *  \param [in] begin - the start value of the result sequence.
11176  *  \param [in] end - limiting value, so that every value of the result array is less than
11177  *              \a end.
11178  *  \param [in] step - specifies the increment or decrement.
11179  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
11180  *          array using decrRef() as it is no more needed.
11181  *  \throw If \a step == 0.
11182  *  \throw If \a end < \a begin && \a step > 0.
11183  *  \throw If \a end > \a begin && \a step < 0.
11184  */
11185 DataArrayInt *DataArrayInt::Range(int begin, int end, int step)
11186 {
11187   int nbOfTuples=GetNumberOfItemGivenBESRelative(begin,end,step,"DataArrayInt::Range");
11188   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11189   ret->alloc(nbOfTuples,1);
11190   int *ptr=ret->getPointer();
11191   if(step>0)
11192     {
11193       for(int i=begin;i<end;i+=step,ptr++)
11194         *ptr=i;
11195     }
11196   else
11197     {
11198       for(int i=begin;i>end;i+=step,ptr++)
11199         *ptr=i;
11200     }
11201   return ret.retn();
11202 }
11203
11204 /*!
11205  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11206  * Server side.
11207  */
11208 void DataArrayInt::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
11209 {
11210   tinyInfo.resize(2);
11211   if(isAllocated())
11212     {
11213       tinyInfo[0]=getNumberOfTuples();
11214       tinyInfo[1]=getNumberOfComponents();
11215     }
11216   else
11217     {
11218       tinyInfo[0]=-1;
11219       tinyInfo[1]=-1;
11220     }
11221 }
11222
11223 /*!
11224  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11225  * Server side.
11226  */
11227 void DataArrayInt::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
11228 {
11229   if(isAllocated())
11230     {
11231       int nbOfCompo=getNumberOfComponents();
11232       tinyInfo.resize(nbOfCompo+1);
11233       tinyInfo[0]=getName();
11234       for(int i=0;i<nbOfCompo;i++)
11235         tinyInfo[i+1]=getInfoOnComponent(i);
11236     }
11237   else
11238     {
11239       tinyInfo.resize(1);
11240       tinyInfo[0]=getName();
11241     }
11242 }
11243
11244 /*!
11245  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11246  * This method returns if a feeding is needed.
11247  */
11248 bool DataArrayInt::resizeForUnserialization(const std::vector<int>& tinyInfoI)
11249 {
11250   int nbOfTuple=tinyInfoI[0];
11251   int nbOfComp=tinyInfoI[1];
11252   if(nbOfTuple!=-1 || nbOfComp!=-1)
11253     {
11254       alloc(nbOfTuple,nbOfComp);
11255       return true;
11256     }
11257   return false;
11258 }
11259
11260 /*!
11261  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11262  * This method returns if a feeding is needed.
11263  */
11264 void DataArrayInt::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<std::string>& tinyInfoS)
11265 {
11266   setName(tinyInfoS[0]);
11267   if(isAllocated())
11268     {
11269       int nbOfCompo=tinyInfoI[1];
11270       for(int i=0;i<nbOfCompo;i++)
11271         setInfoOnComponent(i,tinyInfoS[i+1]);
11272     }
11273 }
11274
11275 DataArrayIntIterator::DataArrayIntIterator(DataArrayInt *da):_da(da),_pt(0),_tuple_id(0),_nb_comp(0),_nb_tuple(0)
11276 {
11277   if(_da)
11278     {
11279       _da->incrRef();
11280       if(_da->isAllocated())
11281         {
11282           _nb_comp=da->getNumberOfComponents();
11283           _nb_tuple=da->getNumberOfTuples();
11284           _pt=da->getPointer();
11285         }
11286     }
11287 }
11288
11289 DataArrayIntIterator::~DataArrayIntIterator()
11290 {
11291   if(_da)
11292     _da->decrRef();
11293 }
11294
11295 DataArrayIntTuple *DataArrayIntIterator::nextt()
11296 {
11297   if(_tuple_id<_nb_tuple)
11298     {
11299       _tuple_id++;
11300       DataArrayIntTuple *ret=new DataArrayIntTuple(_pt,_nb_comp);
11301       _pt+=_nb_comp;
11302       return ret;
11303     }
11304   else
11305     return 0;
11306 }
11307
11308 DataArrayIntTuple::DataArrayIntTuple(int *pt, int nbOfComp):_pt(pt),_nb_of_compo(nbOfComp)
11309 {
11310 }
11311
11312 std::string DataArrayIntTuple::repr() const
11313 {
11314   std::ostringstream oss; oss << "(";
11315   for(int i=0;i<_nb_of_compo-1;i++)
11316     oss << _pt[i] << ", ";
11317   oss << _pt[_nb_of_compo-1] << ")";
11318   return oss.str();
11319 }
11320
11321 int DataArrayIntTuple::intValue() const
11322 {
11323   if(_nb_of_compo==1)
11324     return *_pt;
11325   throw INTERP_KERNEL::Exception("DataArrayIntTuple::intValue : DataArrayIntTuple instance has not exactly 1 component -> Not possible to convert it into an integer !");
11326 }
11327
11328 /*!
11329  * This method returns a newly allocated instance the caller should dealed with by a ParaMEDMEM::DataArrayInt::decrRef.
11330  * This method performs \b no copy of data. The content is only referenced using ParaMEDMEM::DataArrayInt::useArray with ownership set to \b false.
11331  * This method throws an INTERP_KERNEL::Exception is it is impossible to match sizes of \b this that is too say \b nbOfCompo=this->_nb_of_elem and \bnbOfTuples==1 or
11332  * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
11333  */
11334 DataArrayInt *DataArrayIntTuple::buildDAInt(int nbOfTuples, int nbOfCompo) const
11335 {
11336   if((_nb_of_compo==nbOfCompo && nbOfTuples==1) || (_nb_of_compo==nbOfTuples && nbOfCompo==1))
11337     {
11338       DataArrayInt *ret=DataArrayInt::New();
11339       ret->useExternalArrayWithRWAccess(_pt,nbOfTuples,nbOfCompo);
11340       return ret;
11341     }
11342   else
11343     {
11344       std::ostringstream oss; oss << "DataArrayIntTuple::buildDAInt : unable to build a requested DataArrayInt instance with nbofTuple=" << nbOfTuples << " and nbOfCompo=" << nbOfCompo;
11345       oss << ".\nBecause the number of elements in this is " << _nb_of_compo << " !";
11346       throw INTERP_KERNEL::Exception(oss.str().c_str());
11347     }
11348 }