Salome HOME
merge conflict
[modules/med.git] / src / medtool / src / MEDCoupling / MEDCouplingMemArray.cxx
1 // Copyright (C) 2007-2015  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::getDirectChildrenWithNull() 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  * This method put in info format the result of the merge of \a var and \a unit.
427  * The standard format for that is "var [unit]".
428  * Inversely you can retrieve the var part or the unit part of info string using resp. GetVarNameFromInfo and GetUnitFromInfo.
429  */
430 std::string DataArray::BuildInfoFromVarAndUnit(const std::string& var, const std::string& unit)
431 {
432   std::ostringstream oss;
433   oss << var << " [" << unit << "]";
434   return oss.str();
435 }
436
437 /*!
438  * Returns a new DataArray by concatenating all given arrays, so that (1) the number
439  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
440  * the number of component in the result array is same as that of each of given arrays.
441  * Info on components is copied from the first of the given arrays. Number of components
442  * in the given arrays must be  the same.
443  *  \param [in] arrs - a sequence of arrays to include in the result array. All arrays must have the same type.
444  *  \return DataArray * - the new instance of DataArray (that can be either DataArrayInt, DataArrayDouble, DataArrayChar).
445  *          The caller is to delete this result array using decrRef() as it is no more
446  *          needed.
447  *  \throw If all arrays within \a arrs are NULL.
448  *  \throw If all not null arrays in \a arrs have not the same type.
449  *  \throw If getNumberOfComponents() of arrays within \a arrs.
450  */
451 DataArray *DataArray::Aggregate(const std::vector<const DataArray *>& arrs)
452 {
453   std::vector<const DataArray *> arr2;
454   for(std::vector<const DataArray *>::const_iterator it=arrs.begin();it!=arrs.end();it++)
455     if(*it)
456       arr2.push_back(*it);
457   if(arr2.empty())
458     throw INTERP_KERNEL::Exception("DataArray::Aggregate : only null instance in input vector !");
459   std::vector<const DataArrayDouble *> arrd;
460   std::vector<const DataArrayInt *> arri;
461   std::vector<const DataArrayChar *> arrc;
462   for(std::vector<const DataArray *>::const_iterator it=arr2.begin();it!=arr2.end();it++)
463     {
464       const DataArrayDouble *a=dynamic_cast<const DataArrayDouble *>(*it);
465       if(a)
466         { arrd.push_back(a); continue; }
467       const DataArrayInt *b=dynamic_cast<const DataArrayInt *>(*it);
468       if(b)
469         { arri.push_back(b); continue; }
470       const DataArrayChar *c=dynamic_cast<const DataArrayChar *>(*it);
471       if(c)
472         { arrc.push_back(c); continue; }
473       throw INTERP_KERNEL::Exception("DataArray::Aggregate : presence of not null instance in inuput that is not in [DataArrayDouble, DataArrayInt, DataArrayChar] !");
474     }
475   if(arr2.size()==arrd.size())
476     return DataArrayDouble::Aggregate(arrd);
477   if(arr2.size()==arri.size())
478     return DataArrayInt::Aggregate(arri);
479   if(arr2.size()==arrc.size())
480     return DataArrayChar::Aggregate(arrc);
481   throw INTERP_KERNEL::Exception("DataArray::Aggregate : all input arrays must have the same type !");
482 }
483
484 /*!
485  * Sets information on a component specified by an index.
486  * To know more on format of this information
487  * see \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
488  *  \warning Don't pass NULL as \a info!
489  *  \param [in] i - the index (zero based) of the component of interest.
490  *  \param [in] info - the string containing the information.
491  *  \throw If \a i is not a valid component index.
492  */
493 void DataArray::setInfoOnComponent(int i, const std::string& info)
494 {
495   if(i<(int)_info_on_compo.size() && i>=0)
496     _info_on_compo[i]=info;
497   else
498     {
499       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();
500       throw INTERP_KERNEL::Exception(oss.str().c_str());
501     }
502 }
503
504 /*!
505  * Sets information on all components. This method can change number of components
506  * at certain conditions; if the conditions are not respected, an exception is thrown.
507  * The number of components can be changed in \a this only if \a this is not allocated.
508  * The condition of number of components must not be changed.
509  *
510  * To know more on format of the component information see
511  * \ref MEDCouplingArrayBasicsCompoName "DataArrays infos".
512  *  \param [in] info - a vector of component infos.
513  *  \throw If \a this->getNumberOfComponents() != \a info.size() && \a this->isAllocated()
514  */
515 void DataArray::setInfoAndChangeNbOfCompo(const std::vector<std::string>& info)
516 {
517   if(getNumberOfComponents()!=(int)info.size())
518     {
519       if(!isAllocated())
520         _info_on_compo=info;
521       else
522         {
523           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 !";
524           throw INTERP_KERNEL::Exception(oss.str().c_str());
525         }
526     }
527   else
528     _info_on_compo=info;
529 }
530
531 void DataArray::checkNbOfTuples(int nbOfTuples, const std::string& msg) const
532 {
533   if(getNumberOfTuples()!=nbOfTuples)
534     {
535       std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " <<  nbOfTuples << " having " << getNumberOfTuples() << " !";
536       throw INTERP_KERNEL::Exception(oss.str().c_str());
537     }
538 }
539
540 void DataArray::checkNbOfComps(int nbOfCompo, const std::string& msg) const
541 {
542   if(getNumberOfComponents()!=nbOfCompo)
543     {
544       std::ostringstream oss; oss << msg << " : mismatch number of components : expected " << nbOfCompo << " having " << getNumberOfComponents() << " !";
545       throw INTERP_KERNEL::Exception(oss.str().c_str());
546     }
547 }
548
549 void DataArray::checkNbOfElems(std::size_t nbOfElems, const std::string& msg) const
550 {
551   if(getNbOfElems()!=nbOfElems)
552     {
553       std::ostringstream oss; oss << msg << " : mismatch number of elems : Expected " << nbOfElems << " having " << getNbOfElems() << " !";
554       throw INTERP_KERNEL::Exception(oss.str().c_str());
555     }
556 }
557
558 void DataArray::checkNbOfTuplesAndComp(const DataArray& other, const std::string& msg) const
559 {
560   if(getNumberOfTuples()!=other.getNumberOfTuples())
561     {
562       std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " <<  other.getNumberOfTuples() << " having " << getNumberOfTuples() << " !";
563       throw INTERP_KERNEL::Exception(oss.str().c_str());
564     }
565   if(getNumberOfComponents()!=other.getNumberOfComponents())
566     {
567       std::ostringstream oss; oss << msg << " : mismatch number of components : expected " << other.getNumberOfComponents() << " having " << getNumberOfComponents() << " !";
568       throw INTERP_KERNEL::Exception(oss.str().c_str());
569     }
570 }
571
572 void DataArray::checkNbOfTuplesAndComp(int nbOfTuples, int nbOfCompo, const std::string& msg) const
573 {
574   checkNbOfTuples(nbOfTuples,msg);
575   checkNbOfComps(nbOfCompo,msg);
576 }
577
578 /*!
579  * Simply this method checks that \b value is in [0,\b ref).
580  */
581 void DataArray::CheckValueInRange(int ref, int value, const std::string& msg)
582 {
583   if(value<0 || value>=ref)
584     {
585       std::ostringstream oss; oss << "DataArray::CheckValueInRange : " << msg  << " ! Expected in range [0," << ref << "[ having " << value << " !";
586       throw INTERP_KERNEL::Exception(oss.str().c_str());
587     }
588 }
589
590 /*!
591  * This method checks that [\b start, \b end) is compliant with ref length \b value.
592  * typicaly start in [0,\b value) and end in [0,\b value). If value==start and start==end, it is supported.
593  */
594 void DataArray::CheckValueInRangeEx(int value, int start, int end, const std::string& msg)
595 {
596   if(start<0 || start>=value)
597     {
598       if(value!=start || end!=start)
599         {
600           std::ostringstream oss; oss << "DataArray::CheckValueInRangeEx : " << msg  << " ! Expected start " << start << " of input range, in [0," << value << "[ !";
601           throw INTERP_KERNEL::Exception(oss.str().c_str());
602         }
603     }
604   if(end<0 || end>value)
605     {
606       std::ostringstream oss; oss << "DataArray::CheckValueInRangeEx : " << msg  << " ! Expected end " << end << " of input range, in [0," << value << "] !";
607       throw INTERP_KERNEL::Exception(oss.str().c_str());
608     }
609 }
610
611 void DataArray::CheckClosingParInRange(int ref, int value, const std::string& msg)
612 {
613   if(value<0 || value>ref)
614     {
615       std::ostringstream oss; oss << "DataArray::CheckClosingParInRange : " << msg  << " ! Expected input range in [0," << ref << "] having closing open parenthesis " << value << " !";
616       throw INTERP_KERNEL::Exception(oss.str().c_str());
617     }
618 }
619
620 /*!
621  * 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, 
622  * typically it is a whole slice of tuples of DataArray or cells, nodes of a mesh...
623  *
624  * The input \a sliceId should be an id in [0, \a nbOfSlices) that specifies the slice of work.
625  *
626  * \param [in] start - the start of the input slice of the whole work to perform splitted into slices.
627  * \param [in] stop - the stop of the input slice of the whole work to perform splitted into slices.
628  * \param [in] step - the step (that can be <0) of the input slice of the whole work to perform splitted into slices.
629  * \param [in] sliceId - the slice id considered
630  * \param [in] nbOfSlices - the number of slices (typically the number of cores on which the work is expected to be sliced)
631  * \param [out] startSlice - the start of the slice considered
632  * \param [out] stopSlice - the stop of the slice consided
633  * 
634  * \throw If \a step == 0
635  * \throw If \a nbOfSlices not > 0
636  * \throw If \a sliceId not in [0,nbOfSlices)
637  */
638 void DataArray::GetSlice(int start, int stop, int step, int sliceId, int nbOfSlices, int& startSlice, int& stopSlice)
639 {
640   if(nbOfSlices<=0)
641     {
642       std::ostringstream oss; oss << "DataArray::GetSlice : nbOfSlices (" << nbOfSlices << ") must be > 0 !";
643       throw INTERP_KERNEL::Exception(oss.str().c_str());
644     }
645   if(sliceId<0 || sliceId>=nbOfSlices)
646     {
647       std::ostringstream oss; oss << "DataArray::GetSlice : sliceId (" << nbOfSlices << ") must be in [0 , nbOfSlices (" << nbOfSlices << ") ) !";
648       throw INTERP_KERNEL::Exception(oss.str().c_str());
649     }
650   int nbElems=GetNumberOfItemGivenBESRelative(start,stop,step,"DataArray::GetSlice");
651   int minNbOfElemsPerSlice=nbElems/nbOfSlices;
652   startSlice=start+minNbOfElemsPerSlice*step*sliceId;
653   if(sliceId<nbOfSlices-1)
654     stopSlice=start+minNbOfElemsPerSlice*step*(sliceId+1);
655   else
656     stopSlice=stop;
657 }
658
659 int DataArray::GetNumberOfItemGivenBES(int begin, int end, int step, const std::string& msg)
660 {
661   if(end<begin)
662     {
663       std::ostringstream oss; oss << msg << " : end before begin !";
664       throw INTERP_KERNEL::Exception(oss.str().c_str());
665     }
666   if(end==begin)
667     return 0;
668   if(step<=0)
669     {
670       std::ostringstream oss; oss << msg << " : invalid step should be > 0 !";
671       throw INTERP_KERNEL::Exception(oss.str().c_str());
672     }
673   return (end-1-begin)/step+1;
674 }
675
676 int DataArray::GetNumberOfItemGivenBESRelative(int begin, int end, int step, const std::string& msg)
677 {
678   if(step==0)
679     throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES : step=0 is not allowed !");
680   if(end<begin && step>0)
681     {
682       std::ostringstream oss; oss << msg << " : end before begin whereas step is positive !";
683       throw INTERP_KERNEL::Exception(oss.str().c_str());
684     }
685   if(begin<end && step<0)
686     {
687       std::ostringstream oss; oss << msg << " : invalid step should be > 0 !";
688       throw INTERP_KERNEL::Exception(oss.str().c_str());
689     }
690   if(begin!=end)
691     return (std::max(begin,end)-1-std::min(begin,end))/std::abs(step)+1;
692   else
693     return 0;
694 }
695
696 int DataArray::GetPosOfItemGivenBESRelativeNoThrow(int value, int begin, int end, int step)
697 {
698   if(step!=0)
699     {
700       if(step>0)
701         {
702           if(begin<=value && value<end)
703             {
704               if((value-begin)%step==0)
705                 return (value-begin)/step;
706               else
707                 return -1;
708             }
709           else
710             return -1;
711         }
712       else
713         {
714           if(begin>=value && value>end)
715             {
716               if((begin-value)%(-step)==0)
717                 return (begin-value)/(-step);
718               else
719                 return -1;
720             }
721           else
722             return -1;
723         }
724     }
725   else
726     return -1;
727 }
728
729 /*!
730  * Returns a new instance of DataArrayDouble. The caller is to delete this array
731  * using decrRef() as it is no more needed. 
732  */
733 DataArrayDouble *DataArrayDouble::New()
734 {
735   return new DataArrayDouble;
736 }
737
738 /*!
739  * Checks if raw data is allocated. Read more on the raw data
740  * in \ref MEDCouplingArrayBasicsTuplesAndCompo "DataArrays infos" for more information.
741  *  \return bool - \a true if the raw data is allocated, \a false else.
742  */
743 bool DataArrayDouble::isAllocated() const
744 {
745   return getConstPointer()!=0;
746 }
747
748 /*!
749  * Checks if raw data is allocated and throws an exception if it is not the case.
750  *  \throw If the raw data is not allocated.
751  */
752 void DataArrayDouble::checkAllocated() const
753 {
754   if(!isAllocated())
755     throw INTERP_KERNEL::Exception("DataArrayDouble::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !");
756 }
757
758 /*!
759  * This method desallocated \a this without modification of informations relative to the components.
760  * After call of this method, DataArrayDouble::isAllocated will return false.
761  * If \a this is already not allocated, \a this is let unchanged.
762  */
763 void DataArrayDouble::desallocate()
764 {
765   _mem.destroy();
766 }
767
768 std::size_t DataArrayDouble::getHeapMemorySizeWithoutChildren() const
769 {
770   std::size_t sz(_mem.getNbOfElemAllocated());
771   sz*=sizeof(double);
772   return DataArray::getHeapMemorySizeWithoutChildren()+sz;
773 }
774
775 /*!
776  * Returns the only one value in \a this, if and only if number of elements
777  * (nb of tuples * nb of components) is equal to 1, and that \a this is allocated.
778  *  \return double - the sole value stored in \a this array.
779  *  \throw If at least one of conditions stated above is not fulfilled.
780  */
781 double DataArrayDouble::doubleValue() const
782 {
783   if(isAllocated())
784     {
785       if(getNbOfElems()==1)
786         {
787           return *getConstPointer();
788         }
789       else
790         throw INTERP_KERNEL::Exception("DataArrayDouble::doubleValue : DataArrayDouble instance is allocated but number of elements is not equal to 1 !");
791     }
792   else
793     throw INTERP_KERNEL::Exception("DataArrayDouble::doubleValue : DataArrayDouble instance is not allocated !");
794 }
795
796 /*!
797  * Checks the number of tuples.
798  *  \return bool - \a true if getNumberOfTuples() == 0, \a false else.
799  *  \throw If \a this is not allocated.
800  */
801 bool DataArrayDouble::empty() const
802 {
803   checkAllocated();
804   return getNumberOfTuples()==0;
805 }
806
807 /*!
808  * Returns a full copy of \a this. For more info on copying data arrays see
809  * \ref MEDCouplingArrayBasicsCopyDeep.
810  *  \return DataArrayDouble * - a new instance of DataArrayDouble. The caller is to
811  *          delete this array using decrRef() as it is no more needed. 
812  */
813 DataArrayDouble *DataArrayDouble::deepCpy() const
814 {
815   return new DataArrayDouble(*this);
816 }
817
818 /*!
819  * Returns either a \a deep or \a shallow copy of this array. For more info see
820  * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
821  *  \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
822  *  \return DataArrayDouble * - either a new instance of DataArrayDouble (if \a dCpy
823  *          == \a true) or \a this instance (if \a dCpy == \a false).
824  */
825 DataArrayDouble *DataArrayDouble::performCpy(bool dCpy) const
826 {
827   if(dCpy)
828     return deepCpy();
829   else
830     {
831       incrRef();
832       return const_cast<DataArrayDouble *>(this);
833     }
834 }
835
836 /*!
837  * Copies all the data from another DataArrayDouble. For more info see
838  * \ref MEDCouplingArrayBasicsCopyDeepAssign.
839  *  \param [in] other - another instance of DataArrayDouble to copy data from.
840  *  \throw If the \a other is not allocated.
841  */
842 void DataArrayDouble::cpyFrom(const DataArrayDouble& other)
843 {
844   other.checkAllocated();
845   int nbOfTuples=other.getNumberOfTuples();
846   int nbOfComp=other.getNumberOfComponents();
847   allocIfNecessary(nbOfTuples,nbOfComp);
848   std::size_t nbOfElems=(std::size_t)nbOfTuples*nbOfComp;
849   double *pt=getPointer();
850   const double *ptI=other.getConstPointer();
851   for(std::size_t i=0;i<nbOfElems;i++)
852     pt[i]=ptI[i];
853   copyStringInfoFrom(other);
854 }
855
856 /*!
857  * This method reserve nbOfElems elements in memory ( nbOfElems*8 bytes ) \b without impacting the number of tuples in \a this.
858  * 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.
859  * If \a this has not already been allocated, number of components is set to one.
860  * This method allows to reduce number of reallocations on invokation of DataArrayDouble::pushBackSilent and DataArrayDouble::pushBackValsSilent on \a this.
861  * 
862  * \sa DataArrayDouble::pack, DataArrayDouble::pushBackSilent, DataArrayDouble::pushBackValsSilent
863  */
864 void DataArrayDouble::reserve(std::size_t nbOfElems)
865 {
866   int nbCompo=getNumberOfComponents();
867   if(nbCompo==1)
868     {
869       _mem.reserve(nbOfElems);
870     }
871   else if(nbCompo==0)
872     {
873       _mem.reserve(nbOfElems);
874       _info_on_compo.resize(1);
875     }
876   else
877     throw INTERP_KERNEL::Exception("DataArrayDouble::reserve : not available for DataArrayDouble with number of components different than 1 !");
878 }
879
880 /*!
881  * 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
882  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
883  *
884  * \param [in] val the value to be added in \a this
885  * \throw If \a this has already been allocated with number of components different from one.
886  * \sa DataArrayDouble::pushBackValsSilent
887  */
888 void DataArrayDouble::pushBackSilent(double val)
889 {
890   int nbCompo=getNumberOfComponents();
891   if(nbCompo==1)
892     _mem.pushBack(val);
893   else if(nbCompo==0)
894     {
895       _info_on_compo.resize(1);
896       _mem.pushBack(val);
897     }
898   else
899     throw INTERP_KERNEL::Exception("DataArrayDouble::pushBackSilent : not available for DataArrayDouble with number of components different than 1 !");
900 }
901
902 /*!
903  * 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
904  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
905  *
906  *  \param [in] valsBg - an array of values to push at the end of \c this.
907  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
908  *              the last value of \a valsBg is \a valsEnd[ -1 ].
909  * \throw If \a this has already been allocated with number of components different from one.
910  * \sa DataArrayDouble::pushBackSilent
911  */
912 void DataArrayDouble::pushBackValsSilent(const double *valsBg, const double *valsEnd)
913 {
914   int nbCompo=getNumberOfComponents();
915   if(nbCompo==1)
916     _mem.insertAtTheEnd(valsBg,valsEnd);
917   else if(nbCompo==0)
918     {
919       _info_on_compo.resize(1);
920       _mem.insertAtTheEnd(valsBg,valsEnd);
921     }
922   else
923     throw INTERP_KERNEL::Exception("DataArrayDouble::pushBackValsSilent : not available for DataArrayDouble with number of components different than 1 !");
924 }
925
926 /*!
927  * This method returns silently ( without updating time label in \a this ) the last value, if any and suppress it.
928  * \throw If \a this is already empty.
929  * \throw If \a this has number of components different from one.
930  */
931 double DataArrayDouble::popBackSilent()
932 {
933   if(getNumberOfComponents()==1)
934     return _mem.popBack();
935   else
936     throw INTERP_KERNEL::Exception("DataArrayDouble::popBackSilent : not available for DataArrayDouble with number of components different than 1 !");
937 }
938
939 /*!
940  * 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.
941  *
942  * \sa DataArrayDouble::getHeapMemorySizeWithoutChildren, DataArrayDouble::reserve
943  */
944 void DataArrayDouble::pack() const
945 {
946   _mem.pack();
947 }
948
949 /*!
950  * Allocates the raw data in memory. If exactly same memory as needed already
951  * allocated, it is not re-allocated.
952  *  \param [in] nbOfTuple - number of tuples of data to allocate.
953  *  \param [in] nbOfCompo - number of components of data to allocate.
954  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
955  */
956 void DataArrayDouble::allocIfNecessary(int nbOfTuple, int nbOfCompo)
957 {
958   if(isAllocated())
959     {
960       if(nbOfTuple!=getNumberOfTuples() || nbOfCompo!=getNumberOfComponents())
961         alloc(nbOfTuple,nbOfCompo);
962     }
963   else
964     alloc(nbOfTuple,nbOfCompo);
965 }
966
967 /*!
968  * Allocates the raw data in memory. If the memory was already allocated, then it is
969  * freed and re-allocated. See an example of this method use
970  * \ref MEDCouplingArraySteps1WC "here".
971  *  \param [in] nbOfTuple - number of tuples of data to allocate.
972  *  \param [in] nbOfCompo - number of components of data to allocate.
973  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
974  */
975 void DataArrayDouble::alloc(int nbOfTuple, int nbOfCompo)
976 {
977   if(nbOfTuple<0 || nbOfCompo<0)
978     throw INTERP_KERNEL::Exception("DataArrayDouble::alloc : request for negative length of data !");
979   _info_on_compo.resize(nbOfCompo);
980   _mem.alloc(nbOfCompo*(std::size_t)nbOfTuple);
981   declareAsNew();
982 }
983
984 /*!
985  * Assign zero to all values in \a this array. To know more on filling arrays see
986  * \ref MEDCouplingArrayFill.
987  * \throw If \a this is not allocated.
988  */
989 void DataArrayDouble::fillWithZero()
990 {
991   checkAllocated();
992   _mem.fillWithValue(0.);
993   declareAsNew();
994 }
995
996 /*!
997  * Assign \a val to all values in \a this array. To know more on filling arrays see
998  * \ref MEDCouplingArrayFill.
999  *  \param [in] val - the value to fill with.
1000  *  \throw If \a this is not allocated.
1001  */
1002 void DataArrayDouble::fillWithValue(double val)
1003 {
1004   checkAllocated();
1005   _mem.fillWithValue(val);
1006   declareAsNew();
1007 }
1008
1009 /*!
1010  * Set all values in \a this array so that the i-th element equals to \a init + i
1011  * (i starts from zero). To know more on filling arrays see \ref MEDCouplingArrayFill.
1012  *  \param [in] init - value to assign to the first element of array.
1013  *  \throw If \a this->getNumberOfComponents() != 1
1014  *  \throw If \a this is not allocated.
1015  */
1016 void DataArrayDouble::iota(double init)
1017 {
1018   checkAllocated();
1019   if(getNumberOfComponents()!=1)
1020     throw INTERP_KERNEL::Exception("DataArrayDouble::iota : works only for arrays with only one component, you can call 'rearrange' method before !");
1021   double *ptr=getPointer();
1022   int ntuples=getNumberOfTuples();
1023   for(int i=0;i<ntuples;i++)
1024     ptr[i]=init+double(i);
1025   declareAsNew();
1026 }
1027
1028 /*!
1029  * Checks if all values in \a this array are equal to \a val at precision \a eps.
1030  *  \param [in] val - value to check equality of array values to.
1031  *  \param [in] eps - precision to check the equality.
1032  *  \return bool - \a true if all values are in range (_val_ - _eps_; _val_ + _eps_),
1033  *                 \a false else.
1034  *  \throw If \a this->getNumberOfComponents() != 1
1035  *  \throw If \a this is not allocated.
1036  */
1037 bool DataArrayDouble::isUniform(double val, double eps) const
1038 {
1039   checkAllocated();
1040   if(getNumberOfComponents()!=1)
1041     throw INTERP_KERNEL::Exception("DataArrayDouble::isUniform : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before !");
1042   int nbOfTuples=getNumberOfTuples();
1043   const double *w=getConstPointer();
1044   const double *end2=w+nbOfTuples;
1045   const double vmin=val-eps;
1046   const double vmax=val+eps;
1047   for(;w!=end2;w++)
1048     if(*w<vmin || *w>vmax)
1049       return false;
1050   return true;
1051 }
1052
1053 /*!
1054  * Sorts values of the array.
1055  *  \param [in] asc - \a true means ascending order, \a false, descending.
1056  *  \throw If \a this is not allocated.
1057  *  \throw If \a this->getNumberOfComponents() != 1.
1058  */
1059 void DataArrayDouble::sort(bool asc)
1060 {
1061   checkAllocated();
1062   if(getNumberOfComponents()!=1)
1063     throw INTERP_KERNEL::Exception("DataArrayDouble::sort : only supported with 'this' array with ONE component !");
1064   _mem.sort(asc);
1065   declareAsNew();
1066 }
1067
1068 /*!
1069  * Reverse the array values.
1070  *  \throw If \a this->getNumberOfComponents() < 1.
1071  *  \throw If \a this is not allocated.
1072  */
1073 void DataArrayDouble::reverse()
1074 {
1075   checkAllocated();
1076   _mem.reverse(getNumberOfComponents());
1077   declareAsNew();
1078 }
1079
1080 /*!
1081  * Checks that \a this array is consistently **increasing** or **decreasing** in value,
1082  * with at least absolute difference value of |\a eps| at each step.
1083  * If not an exception is thrown.
1084  *  \param [in] increasing - if \a true, the array values should be increasing.
1085  *  \param [in] eps - minimal absolute difference between the neighbor values at which 
1086  *                    the values are considered different.
1087  *  \throw If sequence of values is not strictly monotonic in agreement with \a
1088  *         increasing arg.
1089  *  \throw If \a this->getNumberOfComponents() != 1.
1090  *  \throw If \a this is not allocated.
1091  */
1092 void DataArrayDouble::checkMonotonic(bool increasing, double eps) const
1093 {
1094   if(!isMonotonic(increasing,eps))
1095     {
1096       if (increasing)
1097         throw INTERP_KERNEL::Exception("DataArrayDouble::checkMonotonic : 'this' is not INCREASING monotonic !");
1098       else
1099         throw INTERP_KERNEL::Exception("DataArrayDouble::checkMonotonic : 'this' is not DECREASING monotonic !");
1100     }
1101 }
1102
1103 /*!
1104  * Checks that \a this array is consistently **increasing** or **decreasing** in value,
1105  * with at least absolute difference value of |\a eps| at each step.
1106  *  \param [in] increasing - if \a true, array values should be increasing.
1107  *  \param [in] eps - minimal absolute difference between the neighbor values at which 
1108  *                    the values are considered different.
1109  *  \return bool - \a true if values change in accordance with \a increasing arg.
1110  *  \throw If \a this->getNumberOfComponents() != 1.
1111  *  \throw If \a this is not allocated.
1112  */
1113 bool DataArrayDouble::isMonotonic(bool increasing, double eps) const
1114 {
1115   checkAllocated();
1116   if(getNumberOfComponents()!=1)
1117     throw INTERP_KERNEL::Exception("DataArrayDouble::isMonotonic : only supported with 'this' array with ONE component !");
1118   int nbOfElements=getNumberOfTuples();
1119   const double *ptr=getConstPointer();
1120   if(nbOfElements==0)
1121     return true;
1122   double ref=ptr[0];
1123   double absEps=fabs(eps);
1124   if(increasing)
1125     {
1126       for(int i=1;i<nbOfElements;i++)
1127         {
1128           if(ptr[i]<(ref+absEps))
1129             return false;
1130           ref=ptr[i];
1131         }
1132       return true;
1133     }
1134   else
1135     {
1136       for(int i=1;i<nbOfElements;i++)
1137         {
1138           if(ptr[i]>(ref-absEps))
1139             return false;
1140           ref=ptr[i];
1141         }
1142       return true;
1143     }
1144 }
1145
1146 /*!
1147  * Returns a textual and human readable representation of \a this instance of
1148  * DataArrayDouble. This text is shown when a DataArrayDouble is printed in Python.
1149  * \return std::string - text describing \a this DataArrayDouble.
1150  *
1151  * \sa reprNotTooLong, reprZip
1152  */
1153 std::string DataArrayDouble::repr() const
1154 {
1155   std::ostringstream ret;
1156   reprStream(ret);
1157   return ret.str();
1158 }
1159
1160 std::string DataArrayDouble::reprZip() const
1161 {
1162   std::ostringstream ret;
1163   reprZipStream(ret);
1164   return ret.str();
1165 }
1166
1167 /*!
1168  * This method is close to repr method except that when \a this has more than 1000 tuples, all tuples are not
1169  * printed out to avoid to consume too much space in interpretor.
1170  * \sa repr
1171  */
1172 std::string DataArrayDouble::reprNotTooLong() const
1173 {
1174   std::ostringstream ret;
1175   reprNotTooLongStream(ret);
1176   return ret.str();
1177 }
1178
1179 void DataArrayDouble::writeVTK(std::ostream& ofs, int indent, const std::string& nameInFile, DataArrayByte *byteArr) const
1180 {
1181   static const char SPACE[4]={' ',' ',' ',' '};
1182   checkAllocated();
1183   std::string idt(indent,' ');
1184   ofs.precision(17);
1185   ofs << idt << "<DataArray type=\"Float32\" Name=\"" << nameInFile << "\" NumberOfComponents=\"" << getNumberOfComponents() << "\"";
1186   //
1187   bool areAllEmpty(true);
1188   for(std::vector<std::string>::const_iterator it=_info_on_compo.begin();it!=_info_on_compo.end();it++)
1189     if(!(*it).empty())
1190       areAllEmpty=false;
1191   if(!areAllEmpty)
1192     for(std::size_t i=0;i<_info_on_compo.size();i++)
1193       ofs << " ComponentName" << i << "=\"" << _info_on_compo[i] << "\"";
1194   //
1195   if(byteArr)
1196     {
1197       ofs << " format=\"appended\" offset=\"" << byteArr->getNumberOfTuples() << "\">";
1198       INTERP_KERNEL::AutoPtr<float> tmp(new float[getNbOfElems()]);
1199       float *pt(tmp);
1200       // to make Visual C++ happy : instead of std::copy(begin(),end(),(float *)tmp);
1201       for(const double *src=begin();src!=end();src++,pt++)
1202         *pt=float(*src);
1203       const char *data(reinterpret_cast<const char *>((float *)tmp));
1204       std::size_t sz(getNbOfElems()*sizeof(float));
1205       byteArr->insertAtTheEnd(data,data+sz);
1206       byteArr->insertAtTheEnd(SPACE,SPACE+4);
1207     }
1208   else
1209     {
1210       ofs << " RangeMin=\"" << getMinValueInArray() << "\" RangeMax=\"" << getMaxValueInArray() << "\" format=\"ascii\">\n" << idt;
1211       std::copy(begin(),end(),std::ostream_iterator<double>(ofs," "));
1212     }
1213   ofs << std::endl << idt << "</DataArray>\n";
1214 }
1215
1216 void DataArrayDouble::reprStream(std::ostream& stream) const
1217 {
1218   stream << "Name of double array : \"" << _name << "\"\n";
1219   reprWithoutNameStream(stream);
1220 }
1221
1222 void DataArrayDouble::reprZipStream(std::ostream& stream) const
1223 {
1224   stream << "Name of double array : \"" << _name << "\"\n";
1225   reprZipWithoutNameStream(stream);
1226 }
1227
1228 void DataArrayDouble::reprNotTooLongStream(std::ostream& stream) const
1229 {
1230   stream << "Name of double array : \"" << _name << "\"\n";
1231   reprNotTooLongWithoutNameStream(stream);
1232 }
1233
1234 void DataArrayDouble::reprWithoutNameStream(std::ostream& stream) const
1235 {
1236   DataArray::reprWithoutNameStream(stream);
1237   stream.precision(17);
1238   _mem.repr(getNumberOfComponents(),stream);
1239 }
1240
1241 void DataArrayDouble::reprZipWithoutNameStream(std::ostream& stream) const
1242 {
1243   DataArray::reprWithoutNameStream(stream);
1244   stream.precision(17);
1245   _mem.reprZip(getNumberOfComponents(),stream);
1246 }
1247
1248 void DataArrayDouble::reprNotTooLongWithoutNameStream(std::ostream& stream) const
1249 {
1250   DataArray::reprWithoutNameStream(stream);
1251   stream.precision(17);
1252   _mem.reprNotTooLong(getNumberOfComponents(),stream);
1253 }
1254
1255 void DataArrayDouble::reprCppStream(const std::string& varName, std::ostream& stream) const
1256 {
1257   int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents();
1258   const double *data=getConstPointer();
1259   stream.precision(17);
1260   stream << "DataArrayDouble *" << varName << "=DataArrayDouble::New();" << std::endl;
1261   if(nbTuples*nbComp>=1)
1262     {
1263       stream << "const double " << varName << "Data[" << nbTuples*nbComp << "]={";
1264       std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<double>(stream,","));
1265       stream << data[nbTuples*nbComp-1] << "};" << std::endl;
1266       stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
1267     }
1268   else
1269     stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
1270   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
1271 }
1272
1273 /*!
1274  * Method that gives a quick overvien of \a this for python.
1275  */
1276 void DataArrayDouble::reprQuickOverview(std::ostream& stream) const
1277 {
1278   static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
1279   stream << "DataArrayDouble C++ instance at " << this << ". ";
1280   if(isAllocated())
1281     {
1282       int nbOfCompo=(int)_info_on_compo.size();
1283       if(nbOfCompo>=1)
1284         {
1285           int nbOfTuples=getNumberOfTuples();
1286           stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
1287           reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
1288         }
1289       else
1290         stream << "Number of components : 0.";
1291     }
1292   else
1293     stream << "*** No data allocated ****";
1294 }
1295
1296 void DataArrayDouble::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
1297 {
1298   const double *data=begin();
1299   int nbOfTuples=getNumberOfTuples();
1300   int nbOfCompo=(int)_info_on_compo.size();
1301   std::ostringstream oss2; oss2 << "[";
1302   oss2.precision(17);
1303   std::string oss2Str(oss2.str());
1304   bool isFinished=true;
1305   for(int i=0;i<nbOfTuples && isFinished;i++)
1306     {
1307       if(nbOfCompo>1)
1308         {
1309           oss2 << "(";
1310           for(int j=0;j<nbOfCompo;j++,data++)
1311             {
1312               oss2 << *data;
1313               if(j!=nbOfCompo-1) oss2 << ", ";
1314             }
1315           oss2 << ")";
1316         }
1317       else
1318         oss2 << *data++;
1319       if(i!=nbOfTuples-1) oss2 << ", ";
1320       std::string oss3Str(oss2.str());
1321       if(oss3Str.length()<maxNbOfByteInRepr)
1322         oss2Str=oss3Str;
1323       else
1324         isFinished=false;
1325     }
1326   stream << oss2Str;
1327   if(!isFinished)
1328     stream << "... ";
1329   stream << "]";
1330 }
1331
1332 /*!
1333  * Equivalent to DataArrayDouble::isEqual except that if false the reason of
1334  * mismatch is given.
1335  * 
1336  * \param [in] other the instance to be compared with \a this
1337  * \param [in] prec the precision to compare numeric data of the arrays.
1338  * \param [out] reason In case of inequality returns the reason.
1339  * \sa DataArrayDouble::isEqual
1340  */
1341 bool DataArrayDouble::isEqualIfNotWhy(const DataArrayDouble& other, double prec, std::string& reason) const
1342 {
1343   if(!areInfoEqualsIfNotWhy(other,reason))
1344     return false;
1345   return _mem.isEqual(other._mem,prec,reason);
1346 }
1347
1348 /*!
1349  * Checks if \a this and another DataArrayDouble are fully equal. For more info see
1350  * \ref MEDCouplingArrayBasicsCompare.
1351  *  \param [in] other - an instance of DataArrayDouble to compare with \a this one.
1352  *  \param [in] prec - precision value to compare numeric data of the arrays.
1353  *  \return bool - \a true if the two arrays are equal, \a false else.
1354  */
1355 bool DataArrayDouble::isEqual(const DataArrayDouble& other, double prec) const
1356 {
1357   std::string tmp;
1358   return isEqualIfNotWhy(other,prec,tmp);
1359 }
1360
1361 /*!
1362  * Checks if values of \a this and another DataArrayDouble are equal. For more info see
1363  * \ref MEDCouplingArrayBasicsCompare.
1364  *  \param [in] other - an instance of DataArrayDouble to compare with \a this one.
1365  *  \param [in] prec - precision value to compare numeric data of the arrays.
1366  *  \return bool - \a true if the values of two arrays are equal, \a false else.
1367  */
1368 bool DataArrayDouble::isEqualWithoutConsideringStr(const DataArrayDouble& other, double prec) const
1369 {
1370   std::string tmp;
1371   return _mem.isEqual(other._mem,prec,tmp);
1372 }
1373
1374 /*!
1375  * Changes number of tuples in the array. If the new number of tuples is smaller
1376  * than the current number the array is truncated, otherwise the array is extended.
1377  *  \param [in] nbOfTuples - new number of tuples. 
1378  *  \throw If \a this is not allocated.
1379  *  \throw If \a nbOfTuples is negative.
1380  */
1381 void DataArrayDouble::reAlloc(int nbOfTuples)
1382 {
1383   if(nbOfTuples<0)
1384     throw INTERP_KERNEL::Exception("DataArrayDouble::reAlloc : input new number of tuples should be >=0 !");
1385   checkAllocated();
1386   _mem.reAlloc(getNumberOfComponents()*(std::size_t)nbOfTuples);
1387   declareAsNew();
1388 }
1389
1390 /*!
1391  * Creates a new DataArrayInt and assigns all (textual and numerical) data of \a this
1392  * array to the new one.
1393  *  \return DataArrayInt * - the new instance of DataArrayInt.
1394  */
1395 DataArrayInt *DataArrayDouble::convertToIntArr() const
1396 {
1397   DataArrayInt *ret=DataArrayInt::New();
1398   ret->alloc(getNumberOfTuples(),getNumberOfComponents());
1399   int *dest=ret->getPointer();
1400   // to make Visual C++ happy : instead of std::size_t nbOfVals=getNbOfElems(); std::copy(src,src+nbOfVals,dest);
1401   for(const double *src=begin();src!=end();src++,dest++)
1402     *dest=(int)*src;
1403   ret->copyStringInfoFrom(*this);
1404   return ret;
1405 }
1406
1407 /*!
1408  * Returns a new DataArrayDouble holding the same values as \a this array but differently
1409  * arranged in memory. If \a this array holds 2 components of 3 values:
1410  * \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$, then the result array holds these values arranged
1411  * as follows: \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$.
1412  *  \warning Do not confuse this method with transpose()!
1413  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1414  *          is to delete using decrRef() as it is no more needed.
1415  *  \throw If \a this is not allocated.
1416  */
1417 DataArrayDouble *DataArrayDouble::fromNoInterlace() const
1418 {
1419   if(_mem.isNull())
1420     throw INTERP_KERNEL::Exception("DataArrayDouble::fromNoInterlace : Not defined array !");
1421   double *tab=_mem.fromNoInterlace(getNumberOfComponents());
1422   DataArrayDouble *ret=DataArrayDouble::New();
1423   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
1424   return ret;
1425 }
1426
1427 /*!
1428  * Returns a new DataArrayDouble holding the same values as \a this array but differently
1429  * arranged in memory. If \a this array holds 2 components of 3 values:
1430  * \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$, then the result array holds these values arranged
1431  * as follows: \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$.
1432  *  \warning Do not confuse this method with transpose()!
1433  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1434  *          is to delete using decrRef() as it is no more needed.
1435  *  \throw If \a this is not allocated.
1436  */
1437 DataArrayDouble *DataArrayDouble::toNoInterlace() const
1438 {
1439   if(_mem.isNull())
1440     throw INTERP_KERNEL::Exception("DataArrayDouble::toNoInterlace : Not defined array !");
1441   double *tab=_mem.toNoInterlace(getNumberOfComponents());
1442   DataArrayDouble *ret=DataArrayDouble::New();
1443   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
1444   return ret;
1445 }
1446
1447 /*!
1448  * Permutes values of \a this array as required by \a old2New array. The values are
1449  * permuted so that \c new[ \a old2New[ i ]] = \c old[ i ]. Number of tuples remains
1450  * the same as in \c this one.
1451  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
1452  * For more info on renumbering see \ref numbering.
1453  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1454  *     giving a new position for i-th old value.
1455  */
1456 void DataArrayDouble::renumberInPlace(const int *old2New)
1457 {
1458   checkAllocated();
1459   int nbTuples=getNumberOfTuples();
1460   int nbOfCompo=getNumberOfComponents();
1461   double *tmp=new double[nbTuples*nbOfCompo];
1462   const double *iptr=getConstPointer();
1463   for(int i=0;i<nbTuples;i++)
1464     {
1465       int v=old2New[i];
1466       if(v>=0 && v<nbTuples)
1467         std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),tmp+nbOfCompo*v);
1468       else
1469         {
1470           std::ostringstream oss; oss << "DataArrayDouble::renumberInPlace : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
1471           throw INTERP_KERNEL::Exception(oss.str().c_str());
1472         }
1473     }
1474   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
1475   delete [] tmp;
1476   declareAsNew();
1477 }
1478
1479 /*!
1480  * Permutes values of \a this array as required by \a new2Old array. The values are
1481  * permuted so that \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of tuples remains
1482  * the same as in \c this one.
1483  * For more info on renumbering see \ref numbering.
1484  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
1485  *     giving a previous position of i-th new value.
1486  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1487  *          is to delete using decrRef() as it is no more needed.
1488  */
1489 void DataArrayDouble::renumberInPlaceR(const int *new2Old)
1490 {
1491   checkAllocated();
1492   int nbTuples=getNumberOfTuples();
1493   int nbOfCompo=getNumberOfComponents();
1494   double *tmp=new double[nbTuples*nbOfCompo];
1495   const double *iptr=getConstPointer();
1496   for(int i=0;i<nbTuples;i++)
1497     {
1498       int v=new2Old[i];
1499       if(v>=0 && v<nbTuples)
1500         std::copy(iptr+nbOfCompo*v,iptr+nbOfCompo*(v+1),tmp+nbOfCompo*i);
1501       else
1502         {
1503           std::ostringstream oss; oss << "DataArrayDouble::renumberInPlaceR : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
1504           throw INTERP_KERNEL::Exception(oss.str().c_str());
1505         }
1506     }
1507   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
1508   delete [] tmp;
1509   declareAsNew();
1510 }
1511
1512 /*!
1513  * Returns a copy of \a this array with values permuted as required by \a old2New array.
1514  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ].
1515  * Number of tuples in the result array remains the same as in \c this one.
1516  * If a permutation reduction is needed, renumberAndReduce() should be used.
1517  * For more info on renumbering see \ref numbering.
1518  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1519  *          giving a new position for i-th old value.
1520  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1521  *          is to delete using decrRef() as it is no more needed.
1522  *  \throw If \a this is not allocated.
1523  */
1524 DataArrayDouble *DataArrayDouble::renumber(const int *old2New) const
1525 {
1526   checkAllocated();
1527   int nbTuples=getNumberOfTuples();
1528   int nbOfCompo=getNumberOfComponents();
1529   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1530   ret->alloc(nbTuples,nbOfCompo);
1531   ret->copyStringInfoFrom(*this);
1532   const double *iptr=getConstPointer();
1533   double *optr=ret->getPointer();
1534   for(int i=0;i<nbTuples;i++)
1535     std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),optr+nbOfCompo*old2New[i]);
1536   ret->copyStringInfoFrom(*this);
1537   return ret.retn();
1538 }
1539
1540 /*!
1541  * Returns a copy of \a this array with values permuted as required by \a new2Old array.
1542  * The values are permuted so that  \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of
1543  * tuples in the result array remains the same as in \c this one.
1544  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
1545  * For more info on renumbering see \ref numbering.
1546  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
1547  *     giving a previous position of i-th new value.
1548  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1549  *          is to delete using decrRef() as it is no more needed.
1550  */
1551 DataArrayDouble *DataArrayDouble::renumberR(const int *new2Old) const
1552 {
1553   checkAllocated();
1554   int nbTuples=getNumberOfTuples();
1555   int nbOfCompo=getNumberOfComponents();
1556   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1557   ret->alloc(nbTuples,nbOfCompo);
1558   ret->copyStringInfoFrom(*this);
1559   const double *iptr=getConstPointer();
1560   double *optr=ret->getPointer();
1561   for(int i=0;i<nbTuples;i++)
1562     std::copy(iptr+nbOfCompo*new2Old[i],iptr+nbOfCompo*(new2Old[i]+1),optr+i*nbOfCompo);
1563   ret->copyStringInfoFrom(*this);
1564   return ret.retn();
1565 }
1566
1567 /*!
1568  * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1569  * of size \a newNbOfTuple and it's values are permuted as required by \a old2New array.
1570  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ] for all
1571  * \a old2New[ i ] >= 0. In other words every i-th tuple in \a this array, for which 
1572  * \a old2New[ i ] is negative, is missing from the result array.
1573  * For more info on renumbering see \ref numbering.
1574  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
1575  *     giving a new position for i-th old tuple and giving negative position for
1576  *     for i-th old tuple that should be omitted.
1577  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1578  *          is to delete using decrRef() as it is no more needed.
1579  */
1580 DataArrayDouble *DataArrayDouble::renumberAndReduce(const int *old2New, int newNbOfTuple) const
1581 {
1582   checkAllocated();
1583   int nbTuples=getNumberOfTuples();
1584   int nbOfCompo=getNumberOfComponents();
1585   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1586   ret->alloc(newNbOfTuple,nbOfCompo);
1587   const double *iptr=getConstPointer();
1588   double *optr=ret->getPointer();
1589   for(int i=0;i<nbTuples;i++)
1590     {
1591       int w=old2New[i];
1592       if(w>=0)
1593         std::copy(iptr+i*nbOfCompo,iptr+(i+1)*nbOfCompo,optr+w*nbOfCompo);
1594     }
1595   ret->copyStringInfoFrom(*this);
1596   return ret.retn();
1597 }
1598
1599 /*!
1600  * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1601  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
1602  * \a new2OldBg array.
1603  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
1604  * This method is equivalent to renumberAndReduce() except that convention in input is
1605  * \c new2old and \b not \c old2new.
1606  * For more info on renumbering see \ref numbering.
1607  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
1608  *              tuple index in \a this array to fill the i-th tuple in the new array.
1609  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
1610  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
1611  *              \a new2OldBg <= \a pi < \a new2OldEnd.
1612  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1613  *          is to delete using decrRef() as it is no more needed.
1614  */
1615 DataArrayDouble *DataArrayDouble::selectByTupleId(const int *new2OldBg, const int *new2OldEnd) const
1616 {
1617   checkAllocated();
1618   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1619   int nbComp=getNumberOfComponents();
1620   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
1621   ret->copyStringInfoFrom(*this);
1622   double *pt=ret->getPointer();
1623   const double *srcPt=getConstPointer();
1624   int i=0;
1625   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
1626     std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
1627   ret->copyStringInfoFrom(*this);
1628   return ret.retn();
1629 }
1630
1631 /*!
1632  * Returns a shorten and permuted copy of \a this array. The new DataArrayDouble is
1633  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
1634  * \a new2OldBg array.
1635  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
1636  * This method is equivalent to renumberAndReduce() except that convention in input is
1637  * \c new2old and \b not \c old2new.
1638  * This method is equivalent to selectByTupleId() except that it prevents coping data
1639  * from behind the end of \a this array.
1640  * For more info on renumbering see \ref numbering.
1641  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
1642  *              tuple index in \a this array to fill the i-th tuple in the new array.
1643  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
1644  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
1645  *              \a new2OldBg <= \a pi < \a new2OldEnd.
1646  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1647  *          is to delete using decrRef() as it is no more needed.
1648  *  \throw If \a new2OldEnd - \a new2OldBg > \a this->getNumberOfTuples().
1649  */
1650 DataArrayDouble *DataArrayDouble::selectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const
1651 {
1652   checkAllocated();
1653   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1654   int nbComp=getNumberOfComponents();
1655   int oldNbOfTuples=getNumberOfTuples();
1656   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
1657   ret->copyStringInfoFrom(*this);
1658   double *pt=ret->getPointer();
1659   const double *srcPt=getConstPointer();
1660   int i=0;
1661   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
1662     if(*w>=0 && *w<oldNbOfTuples)
1663       std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
1664     else
1665       throw INTERP_KERNEL::Exception("DataArrayDouble::selectByTupleIdSafe : some ids has been detected to be out of [0,this->getNumberOfTuples) !");
1666   ret->copyStringInfoFrom(*this);
1667   return ret.retn();
1668 }
1669
1670 /*!
1671  * Returns a shorten copy of \a this array. The new DataArrayDouble contains every
1672  * (\a bg + \c i * \a step)-th tuple of \a this array located before the \a end2-th
1673  * tuple. Indices of the selected tuples are the same as ones returned by the Python
1674  * command \c range( \a bg, \a end2, \a step ).
1675  * This method is equivalent to selectByTupleIdSafe() except that the input array is
1676  * not constructed explicitly.
1677  * For more info on renumbering see \ref numbering.
1678  *  \param [in] bg - index of the first tuple to copy from \a this array.
1679  *  \param [in] end2 - index of the tuple before which the tuples to copy are located.
1680  *  \param [in] step - index increment to get index of the next tuple to copy.
1681  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1682  *          is to delete using decrRef() as it is no more needed.
1683  *  \sa DataArrayDouble::substr.
1684  */
1685 DataArrayDouble *DataArrayDouble::selectByTupleId2(int bg, int end2, int step) const
1686 {
1687   checkAllocated();
1688   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1689   int nbComp=getNumberOfComponents();
1690   int newNbOfTuples=GetNumberOfItemGivenBESRelative(bg,end2,step,"DataArrayDouble::selectByTupleId2 : ");
1691   ret->alloc(newNbOfTuples,nbComp);
1692   double *pt=ret->getPointer();
1693   const double *srcPt=getConstPointer()+bg*nbComp;
1694   for(int i=0;i<newNbOfTuples;i++,srcPt+=step*nbComp)
1695     std::copy(srcPt,srcPt+nbComp,pt+i*nbComp);
1696   ret->copyStringInfoFrom(*this);
1697   return ret.retn();
1698 }
1699
1700 /*!
1701  * Returns a shorten copy of \a this array. The new DataArrayDouble contains ranges
1702  * of tuples specified by \a ranges parameter.
1703  * For more info on renumbering see \ref numbering.
1704  *  \param [in] ranges - std::vector of std::pair's each of which defines a range
1705  *              of tuples in [\c begin,\c end) format.
1706  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1707  *          is to delete using decrRef() as it is no more needed.
1708  *  \throw If \a end < \a begin.
1709  *  \throw If \a end > \a this->getNumberOfTuples().
1710  *  \throw If \a this is not allocated.
1711  */
1712 DataArray *DataArrayDouble::selectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const
1713 {
1714   checkAllocated();
1715   int nbOfComp=getNumberOfComponents();
1716   int nbOfTuplesThis=getNumberOfTuples();
1717   if(ranges.empty())
1718     {
1719       DataArrayDouble *ret=DataArrayDouble::New();
1720       ret->alloc(0,nbOfComp);
1721       ret->copyStringInfoFrom(*this);
1722       return ret;
1723     }
1724   int ref=ranges.front().first;
1725   int nbOfTuples=0;
1726   bool isIncreasing=true;
1727   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
1728     {
1729       if((*it).first<=(*it).second)
1730         {
1731           if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
1732             {
1733               nbOfTuples+=(*it).second-(*it).first;
1734               if(isIncreasing)
1735                 isIncreasing=ref<=(*it).first;
1736               ref=(*it).second;
1737             }
1738           else
1739             {
1740               std::ostringstream oss; oss << "DataArrayDouble::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
1741               oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !";
1742               throw INTERP_KERNEL::Exception(oss.str().c_str());
1743             }
1744         }
1745       else
1746         {
1747           std::ostringstream oss; oss << "DataArrayDouble::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
1748           oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !";
1749           throw INTERP_KERNEL::Exception(oss.str().c_str());
1750         }
1751     }
1752   if(isIncreasing && nbOfTuplesThis==nbOfTuples)
1753     return deepCpy();
1754   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1755   ret->alloc(nbOfTuples,nbOfComp);
1756   ret->copyStringInfoFrom(*this);
1757   const double *src=getConstPointer();
1758   double *work=ret->getPointer();
1759   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
1760     work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work);
1761   return ret.retn();
1762 }
1763
1764 /*!
1765  * Returns a shorten copy of \a this array. The new DataArrayDouble contains all
1766  * tuples starting from the \a tupleIdBg-th tuple and including all tuples located before
1767  * the \a tupleIdEnd-th one. This methods has a similar behavior as std::string::substr().
1768  * This method is a specialization of selectByTupleId2().
1769  *  \param [in] tupleIdBg - index of the first tuple to copy from \a this array.
1770  *  \param [in] tupleIdEnd - index of the tuple before which the tuples to copy are located.
1771  *          If \a tupleIdEnd == -1, all the tuples till the end of \a this array are copied.
1772  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1773  *          is to delete using decrRef() as it is no more needed.
1774  *  \throw If \a tupleIdBg < 0.
1775  *  \throw If \a tupleIdBg > \a this->getNumberOfTuples().
1776     \throw If \a tupleIdEnd != -1 && \a tupleIdEnd < \a this->getNumberOfTuples().
1777  *  \sa DataArrayDouble::selectByTupleId2
1778  */
1779 DataArrayDouble *DataArrayDouble::substr(int tupleIdBg, int tupleIdEnd) const
1780 {
1781   checkAllocated();
1782   int nbt=getNumberOfTuples();
1783   if(tupleIdBg<0)
1784     throw INTERP_KERNEL::Exception("DataArrayDouble::substr : The tupleIdBg parameter must be greater than 0 !");
1785   if(tupleIdBg>nbt)
1786     throw INTERP_KERNEL::Exception("DataArrayDouble::substr : The tupleIdBg parameter is greater than number of tuples !");
1787   int trueEnd=tupleIdEnd;
1788   if(tupleIdEnd!=-1)
1789     {
1790       if(tupleIdEnd>nbt)
1791         throw INTERP_KERNEL::Exception("DataArrayDouble::substr : The tupleIdBg parameter is greater or equal than number of tuples !");
1792     }
1793   else
1794     trueEnd=nbt;
1795   int nbComp=getNumberOfComponents();
1796   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1797   ret->alloc(trueEnd-tupleIdBg,nbComp);
1798   ret->copyStringInfoFrom(*this);
1799   std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer());
1800   return ret.retn();
1801 }
1802
1803 /*!
1804  * Returns a shorten or extended copy of \a this array. If \a newNbOfComp is less
1805  * than \a this->getNumberOfComponents() then the result array is shorten as each tuple
1806  * is truncated to have \a newNbOfComp components, keeping first components. If \a
1807  * newNbOfComp is more than \a this->getNumberOfComponents() then the result array is
1808  * expanded as each tuple is populated with \a dftValue to have \a newNbOfComp
1809  * components.  
1810  *  \param [in] newNbOfComp - number of components for the new array to have.
1811  *  \param [in] dftValue - value assigned to new values added to the new array.
1812  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1813  *          is to delete using decrRef() as it is no more needed.
1814  *  \throw If \a this is not allocated.
1815  */
1816 DataArrayDouble *DataArrayDouble::changeNbOfComponents(int newNbOfComp, double dftValue) const
1817 {
1818   checkAllocated();
1819   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
1820   ret->alloc(getNumberOfTuples(),newNbOfComp);
1821   const double *oldc=getConstPointer();
1822   double *nc=ret->getPointer();
1823   int nbOfTuples=getNumberOfTuples();
1824   int oldNbOfComp=getNumberOfComponents();
1825   int dim=std::min(oldNbOfComp,newNbOfComp);
1826   for(int i=0;i<nbOfTuples;i++)
1827     {
1828       int j=0;
1829       for(;j<dim;j++)
1830         nc[newNbOfComp*i+j]=oldc[i*oldNbOfComp+j];
1831       for(;j<newNbOfComp;j++)
1832         nc[newNbOfComp*i+j]=dftValue;
1833     }
1834   ret->setName(getName());
1835   for(int i=0;i<dim;i++)
1836     ret->setInfoOnComponent(i,getInfoOnComponent(i));
1837   ret->setName(getName());
1838   return ret.retn();
1839 }
1840
1841 /*!
1842  * Changes the number of components within \a this array so that its raw data **does
1843  * not** change, instead splitting this data into tuples changes.
1844  *  \warning This method erases all (name and unit) component info set before!
1845  *  \param [in] newNbOfComp - number of components for \a this array to have.
1846  *  \throw If \a this is not allocated
1847  *  \throw If getNbOfElems() % \a newNbOfCompo != 0.
1848  *  \throw If \a newNbOfCompo is lower than 1.
1849  *  \throw If the rearrange method would lead to a number of tuples higher than 2147483647 (maximal capacity of int32 !).
1850  *  \warning This method erases all (name and unit) component info set before!
1851  */
1852 void DataArrayDouble::rearrange(int newNbOfCompo)
1853 {
1854   checkAllocated();
1855   if(newNbOfCompo<1)
1856     throw INTERP_KERNEL::Exception("DataArrayDouble::rearrange : input newNbOfCompo must be > 0 !");
1857   std::size_t nbOfElems=getNbOfElems();
1858   if(nbOfElems%newNbOfCompo!=0)
1859     throw INTERP_KERNEL::Exception("DataArrayDouble::rearrange : nbOfElems%newNbOfCompo!=0 !");
1860   if(nbOfElems/newNbOfCompo>(std::size_t)std::numeric_limits<int>::max())
1861     throw INTERP_KERNEL::Exception("DataArrayDouble::rearrange : the rearrangement leads to too high number of tuples (> 2147483647) !");
1862   _info_on_compo.clear();
1863   _info_on_compo.resize(newNbOfCompo);
1864   declareAsNew();
1865 }
1866
1867 /*!
1868  * Changes the number of components within \a this array to be equal to its number
1869  * of tuples, and inversely its number of tuples to become equal to its number of 
1870  * components. So that its raw data **does not** change, instead splitting this
1871  * data into tuples changes.
1872  *  \warning This method erases all (name and unit) component info set before!
1873  *  \warning Do not confuse this method with fromNoInterlace() and toNoInterlace()!
1874  *  \throw If \a this is not allocated.
1875  *  \sa rearrange()
1876  */
1877 void DataArrayDouble::transpose()
1878 {
1879   checkAllocated();
1880   int nbOfTuples=getNumberOfTuples();
1881   rearrange(nbOfTuples);
1882 }
1883
1884 /*!
1885  * Returns a copy of \a this array composed of selected components.
1886  * The new DataArrayDouble has the same number of tuples but includes components
1887  * specified by \a compoIds parameter. So that getNbOfElems() of the result array
1888  * can be either less, same or more than \a this->getNbOfElems().
1889  *  \param [in] compoIds - sequence of zero based indices of components to include
1890  *              into the new array.
1891  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
1892  *          is to delete using decrRef() as it is no more needed.
1893  *  \throw If \a this is not allocated.
1894  *  \throw If a component index (\a i) is not valid: 
1895  *         \a i < 0 || \a i >= \a this->getNumberOfComponents().
1896  *
1897  *  \if ENABLE_EXAMPLES
1898  *  \ref py_mcdataarraydouble_KeepSelectedComponents "Here is a Python example".
1899  *  \endif
1900  */
1901 DataArrayDouble *DataArrayDouble::keepSelectedComponents(const std::vector<int>& compoIds) const
1902 {
1903   checkAllocated();
1904   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
1905   std::size_t newNbOfCompo=compoIds.size();
1906   int oldNbOfCompo=getNumberOfComponents();
1907   for(std::vector<int>::const_iterator it=compoIds.begin();it!=compoIds.end();it++)
1908     if((*it)<0 || (*it)>=oldNbOfCompo)
1909       {
1910         std::ostringstream oss; oss << "DataArrayDouble::keepSelectedComponents : invalid requested component : " << *it << " whereas it should be in [0," << oldNbOfCompo << ") !";
1911         throw INTERP_KERNEL::Exception(oss.str().c_str());
1912       }
1913   int nbOfTuples=getNumberOfTuples();
1914   ret->alloc(nbOfTuples,(int)newNbOfCompo);
1915   ret->copyPartOfStringInfoFrom(*this,compoIds);
1916   const double *oldc=getConstPointer();
1917   double *nc=ret->getPointer();
1918   for(int i=0;i<nbOfTuples;i++)
1919     for(std::size_t j=0;j<newNbOfCompo;j++,nc++)
1920       *nc=oldc[i*oldNbOfCompo+compoIds[j]];
1921   return ret.retn();
1922 }
1923
1924 /*!
1925  * Appends components of another array to components of \a this one, tuple by tuple.
1926  * So that the number of tuples of \a this array remains the same and the number of 
1927  * components increases.
1928  *  \param [in] other - the DataArrayDouble to append to \a this one.
1929  *  \throw If \a this is not allocated.
1930  *  \throw If \a this and \a other arrays have different number of tuples.
1931  *
1932  *  \if ENABLE_EXAMPLES
1933  *  \ref cpp_mcdataarraydouble_meldwith "Here is a C++ example".
1934  *
1935  *  \ref py_mcdataarraydouble_meldwith "Here is a Python example".
1936  *  \endif
1937  */
1938 void DataArrayDouble::meldWith(const DataArrayDouble *other)
1939 {
1940   checkAllocated();
1941   other->checkAllocated();
1942   int nbOfTuples=getNumberOfTuples();
1943   if(nbOfTuples!=other->getNumberOfTuples())
1944     throw INTERP_KERNEL::Exception("DataArrayDouble::meldWith : mismatch of number of tuples !");
1945   int nbOfComp1=getNumberOfComponents();
1946   int nbOfComp2=other->getNumberOfComponents();
1947   double *newArr=(double *)malloc((nbOfTuples*(nbOfComp1+nbOfComp2))*sizeof(double));
1948   double *w=newArr;
1949   const double *inp1=getConstPointer();
1950   const double *inp2=other->getConstPointer();
1951   for(int i=0;i<nbOfTuples;i++,inp1+=nbOfComp1,inp2+=nbOfComp2)
1952     {
1953       w=std::copy(inp1,inp1+nbOfComp1,w);
1954       w=std::copy(inp2,inp2+nbOfComp2,w);
1955     }
1956   useArray(newArr,true,C_DEALLOC,nbOfTuples,nbOfComp1+nbOfComp2);
1957   std::vector<int> compIds(nbOfComp2);
1958   for(int i=0;i<nbOfComp2;i++)
1959     compIds[i]=nbOfComp1+i;
1960   copyPartOfStringInfoFrom2(compIds,*other);
1961 }
1962
1963 /*!
1964  * This method checks that all tuples in \a other are in \a this.
1965  * If true, the output param \a tupleIds contains the tuples ids of \a this that correspond to tupes in \a this.
1966  * 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.
1967  *
1968  * \param [in] other - the array having the same number of components than \a this.
1969  * \param [out] tupleIds - the tuple ids containing the same number of tuples than \a other has.
1970  * \sa DataArrayDouble::findCommonTuples
1971  */
1972 bool DataArrayDouble::areIncludedInMe(const DataArrayDouble *other, double prec, DataArrayInt *&tupleIds) const
1973 {
1974   if(!other)
1975     throw INTERP_KERNEL::Exception("DataArrayDouble::areIncludedInMe : input array is NULL !");
1976   checkAllocated(); other->checkAllocated();
1977   if(getNumberOfComponents()!=other->getNumberOfComponents())
1978     throw INTERP_KERNEL::Exception("DataArrayDouble::areIncludedInMe : the number of components does not match !");
1979   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> a=DataArrayDouble::Aggregate(this,other);
1980   DataArrayInt *c=0,*ci=0;
1981   a->findCommonTuples(prec,getNumberOfTuples(),c,ci);
1982   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cSafe(c),ciSafe(ci);
1983   int newNbOfTuples=-1;
1984   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ids=DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(a->getNumberOfTuples(),c->begin(),ci->begin(),ci->end(),newNbOfTuples);
1985   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=ids->selectByTupleId2(getNumberOfTuples(),a->getNumberOfTuples(),1);
1986   tupleIds=ret1.retn();
1987   return newNbOfTuples==getNumberOfTuples();
1988 }
1989
1990 /*!
1991  * Searches for tuples coincident within \a prec tolerance. Each tuple is considered
1992  * as coordinates of a point in getNumberOfComponents()-dimensional space. The
1993  * distance separating two points is computed with the infinite norm.
1994  *
1995  * Indices of coincident tuples are stored in output arrays.
1996  * A pair of arrays (\a comm, \a commIndex) is called "Surjective Format 2".
1997  *
1998  * This method is typically used by MEDCouplingPointSet::findCommonNodes() and
1999  * MEDCouplingUMesh::mergeNodes().
2000  *  \param [in] prec - minimal absolute distance between two tuples (infinite norm) at which they are
2001  *              considered not coincident.
2002  *  \param [in] limitTupleId - limit tuple id. If all tuples within a group of coincident
2003  *              tuples have id strictly lower than \a limitTupleId then they are not returned.
2004  *  \param [out] comm - the array holding ids (== indices) of coincident tuples. 
2005  *               \a comm->getNumberOfComponents() == 1. 
2006  *               \a comm->getNumberOfTuples() == \a commIndex->back().
2007  *  \param [out] commIndex - the array dividing all indices stored in \a comm into
2008  *               groups of (indices of) coincident tuples. Its every value is a tuple
2009  *               index where a next group of tuples begins. For example the second
2010  *               group of tuples in \a comm is described by following range of indices:
2011  *               [ \a commIndex[1], \a commIndex[2] ). \a commIndex->getNumberOfTuples()-1
2012  *               gives the number of groups of coincident tuples.
2013  *  \throw If \a this is not allocated.
2014  *  \throw If the number of components is not in [1,2,3,4].
2015  *
2016  *  \if ENABLE_EXAMPLES
2017  *  \ref cpp_mcdataarraydouble_findcommontuples "Here is a C++ example".
2018  *
2019  *  \ref py_mcdataarraydouble_findcommontuples  "Here is a Python example".
2020  *  \endif
2021  *  \sa DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(), DataArrayDouble::areIncludedInMe
2022  */
2023 void DataArrayDouble::findCommonTuples(double prec, int limitTupleId, DataArrayInt *&comm, DataArrayInt *&commIndex) const
2024 {
2025   checkAllocated();
2026   int nbOfCompo=getNumberOfComponents();
2027   if ((nbOfCompo<1) || (nbOfCompo>4)) //test before work
2028     throw INTERP_KERNEL::Exception("DataArrayDouble::findCommonTuples : Unexpected spacedim of coords. Must be 1, 2, 3 or 4.");
2029
2030   int nbOfTuples=getNumberOfTuples();
2031   //
2032   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> c(DataArrayInt::New()),cI(DataArrayInt::New()); c->alloc(0,1); cI->pushBackSilent(0);
2033   switch(nbOfCompo)
2034   {
2035     case 4:
2036       findCommonTuplesAlg<4>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
2037       break;
2038     case 3:
2039       findCommonTuplesAlg<3>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
2040       break;
2041     case 2:
2042       findCommonTuplesAlg<2>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
2043       break;
2044     case 1:
2045       findCommonTuplesAlg<1>(begin(),nbOfTuples,limitTupleId,prec,c,cI);
2046       break;
2047     default:
2048       throw INTERP_KERNEL::Exception("DataArrayDouble::findCommonTuples : nb of components managed are 1,2,3 and 4 ! not implemented for other number of components !");
2049   }
2050   comm=c.retn();
2051   commIndex=cI.retn();
2052 }
2053
2054 /*!
2055  * 
2056  * \param [in] nbTimes specifies the nb of times each tuples in \a this will be duplicated contiguouly in returned DataArrayDouble instance.
2057  *             \a nbTimes  should be at least equal to 1.
2058  * \return a newly allocated DataArrayDouble having one component and number of tuples equal to \a nbTimes * \c this->getNumberOfTuples.
2059  * \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.
2060  */
2061 DataArrayDouble *DataArrayDouble::duplicateEachTupleNTimes(int nbTimes) const
2062 {
2063   checkAllocated();
2064   if(getNumberOfComponents()!=1)
2065     throw INTERP_KERNEL::Exception("DataArrayDouble::duplicateEachTupleNTimes : this should have only one component !");
2066   if(nbTimes<1)
2067     throw INTERP_KERNEL::Exception("DataArrayDouble::duplicateEachTupleNTimes : nb times should be >= 1 !");
2068   int nbTuples=getNumberOfTuples();
2069   const double *inPtr=getConstPointer();
2070   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(nbTimes*nbTuples,1);
2071   double *retPtr=ret->getPointer();
2072   for(int i=0;i<nbTuples;i++,inPtr++)
2073     {
2074       double val=*inPtr;
2075       for(int j=0;j<nbTimes;j++,retPtr++)
2076         *retPtr=val;
2077     }
2078   ret->copyStringInfoFrom(*this);
2079   return ret.retn();
2080 }
2081
2082 /*!
2083  * This methods returns the minimal distance between the two set of points \a this and \a other.
2084  * So \a this and \a other have to have the same number of components. If not an INTERP_KERNEL::Exception will be thrown.
2085  * This method works only if number of components of \a this (equal to those of \a other) is in 1, 2 or 3.
2086  *
2087  * \param [out] thisTupleId the tuple id in \a this corresponding to the returned minimal distance
2088  * \param [out] otherTupleId the tuple id in \a other corresponding to the returned minimal distance
2089  * \return the minimal distance between the two set of points \a this and \a other.
2090  * \sa DataArrayDouble::findClosestTupleId
2091  */
2092 double DataArrayDouble::minimalDistanceTo(const DataArrayDouble *other, int& thisTupleId, int& otherTupleId) const
2093 {
2094   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> part1=findClosestTupleId(other);
2095   int nbOfCompo(getNumberOfComponents());
2096   int otherNbTuples(other->getNumberOfTuples());
2097   const double *thisPt(begin()),*otherPt(other->begin());
2098   const int *part1Pt(part1->begin());
2099   double ret=std::numeric_limits<double>::max();
2100   for(int i=0;i<otherNbTuples;i++,part1Pt++,otherPt+=nbOfCompo)
2101     {
2102       double tmp(0.);
2103       for(int j=0;j<nbOfCompo;j++)
2104         tmp+=(otherPt[j]-thisPt[nbOfCompo*(*part1Pt)+j])*(otherPt[j]-thisPt[nbOfCompo*(*part1Pt)+j]);
2105       if(tmp<ret)
2106         { ret=tmp; thisTupleId=*part1Pt; otherTupleId=i; }
2107     }
2108   return sqrt(ret);
2109 }
2110
2111 /*!
2112  * This methods returns for each tuple in \a other which tuple in \a this is the closest.
2113  * So \a this and \a other have to have the same number of components. If not an INTERP_KERNEL::Exception will be thrown.
2114  * This method works only if number of components of \a this (equal to those of \a other) is in 1, 2 or 3.
2115  *
2116  * \return a newly allocated (new object to be dealt by the caller) DataArrayInt having \c other->getNumberOfTuples() tuples and one components.
2117  * \sa DataArrayDouble::minimalDistanceTo
2118  */
2119 DataArrayInt *DataArrayDouble::findClosestTupleId(const DataArrayDouble *other) const
2120 {
2121   if(!other)
2122     throw INTERP_KERNEL::Exception("DataArrayDouble::findClosestTupleId : other instance is NULL !");
2123   checkAllocated(); other->checkAllocated();
2124   int nbOfCompo=getNumberOfComponents();
2125   if(nbOfCompo!=other->getNumberOfComponents())
2126     {
2127       std::ostringstream oss; oss << "DataArrayDouble::findClosestTupleId : number of components in this is " << nbOfCompo;
2128       oss << ", whereas number of components in other is " << other->getNumberOfComponents() << "! Should be equal !";
2129       throw INTERP_KERNEL::Exception(oss.str().c_str());
2130     }
2131   int nbOfTuples=other->getNumberOfTuples();
2132   int thisNbOfTuples=getNumberOfTuples();
2133   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbOfTuples,1);
2134   double bounds[6];
2135   getMinMaxPerComponent(bounds);
2136   switch(nbOfCompo)
2137   {
2138     case 3:
2139       {
2140         double xDelta(fabs(bounds[1]-bounds[0])),yDelta(fabs(bounds[3]-bounds[2])),zDelta(fabs(bounds[5]-bounds[4]));
2141         double delta=std::max(xDelta,yDelta); delta=std::max(delta,zDelta);
2142         double characSize=pow((delta*delta*delta)/((double)thisNbOfTuples),1./3.);
2143         BBTreePts<3,int> myTree(begin(),0,0,getNumberOfTuples(),characSize*1e-12);
2144         FindClosestTupleIdAlg<3>(myTree,3.*characSize*characSize,other->begin(),nbOfTuples,begin(),thisNbOfTuples,ret->getPointer());
2145         break;
2146       }
2147     case 2:
2148       {
2149         double xDelta(fabs(bounds[1]-bounds[0])),yDelta(fabs(bounds[3]-bounds[2]));
2150         double delta=std::max(xDelta,yDelta);
2151         double characSize=sqrt(delta/(double)thisNbOfTuples);
2152         BBTreePts<2,int> myTree(begin(),0,0,getNumberOfTuples(),characSize*1e-12);
2153         FindClosestTupleIdAlg<2>(myTree,2.*characSize*characSize,other->begin(),nbOfTuples,begin(),thisNbOfTuples,ret->getPointer());
2154         break;
2155       }
2156     case 1:
2157       {
2158         double characSize=fabs(bounds[1]-bounds[0])/thisNbOfTuples;
2159         BBTreePts<1,int> myTree(begin(),0,0,getNumberOfTuples(),characSize*1e-12);
2160         FindClosestTupleIdAlg<1>(myTree,1.*characSize*characSize,other->begin(),nbOfTuples,begin(),thisNbOfTuples,ret->getPointer());
2161         break;
2162       }
2163     default:
2164       throw INTERP_KERNEL::Exception("Unexpected spacedim of coords for findClosestTupleId. Must be 1, 2 or 3.");
2165   }
2166   return ret.retn();
2167 }
2168
2169 /*!
2170  * This method expects that \a this and \a otherBBoxFrmt arrays are bounding box arrays ( as the output of MEDCouplingPointSet::getBoundingBoxForBBTree method ).
2171  * 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
2172  * how many bounding boxes in \a otherBBoxFrmt.
2173  * So, this method expects that \a this and \a otherBBoxFrmt have the same number of components.
2174  *
2175  * \param [in] otherBBoxFrmt - It is an array .
2176  * \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.
2177  * \sa MEDCouplingPointSet::getBoundingBoxForBBTree
2178  * \throw If \a this and \a otherBBoxFrmt have not the same number of components.
2179  * \throw If \a this and \a otherBBoxFrmt number of components is not even (BBox format).
2180  */
2181 DataArrayInt *DataArrayDouble::computeNbOfInteractionsWith(const DataArrayDouble *otherBBoxFrmt, double eps) const
2182 {
2183   if(!otherBBoxFrmt)
2184     throw INTERP_KERNEL::Exception("DataArrayDouble::computeNbOfInteractionsWith : input array is NULL !");
2185   if(!isAllocated() || !otherBBoxFrmt->isAllocated())
2186     throw INTERP_KERNEL::Exception("DataArrayDouble::computeNbOfInteractionsWith : this and input array must be allocated !");
2187   int nbOfComp(getNumberOfComponents()),nbOfTuples(getNumberOfTuples());
2188   if(nbOfComp!=otherBBoxFrmt->getNumberOfComponents())
2189     {
2190       std::ostringstream oss; oss << "DataArrayDouble::computeNbOfInteractionsWith : this number of components (" << nbOfComp << ") must be equal to the number of components of input array (" << otherBBoxFrmt->getNumberOfComponents() << ") !";
2191       throw INTERP_KERNEL::Exception(oss.str().c_str());
2192     }
2193   if(nbOfComp%2!=0)
2194     {
2195       std::ostringstream oss; oss << "DataArrayDouble::computeNbOfInteractionsWith : Number of components (" << nbOfComp << ") is not even ! It should be to be compatible with bbox format !";
2196       throw INTERP_KERNEL::Exception(oss.str().c_str());
2197     }
2198   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(nbOfTuples,1);
2199   const double *thisBBPtr(begin());
2200   int *retPtr(ret->getPointer());
2201   switch(nbOfComp/2)
2202   {
2203     case 3:
2204       {
2205         BBTree<3,int> bbt(otherBBoxFrmt->begin(),0,0,otherBBoxFrmt->getNumberOfTuples(),eps);
2206         for(int i=0;i<nbOfTuples;i++,retPtr++,thisBBPtr+=nbOfComp)
2207           *retPtr=bbt.getNbOfIntersectingElems(thisBBPtr);
2208         break;
2209       }
2210     case 2:
2211       {
2212         BBTree<2,int> bbt(otherBBoxFrmt->begin(),0,0,otherBBoxFrmt->getNumberOfTuples(),eps);
2213         for(int i=0;i<nbOfTuples;i++,retPtr++,thisBBPtr+=nbOfComp)
2214           *retPtr=bbt.getNbOfIntersectingElems(thisBBPtr);
2215         break;
2216       }
2217     case 1:
2218       {
2219         BBTree<1,int> bbt(otherBBoxFrmt->begin(),0,0,otherBBoxFrmt->getNumberOfTuples(),eps);
2220         for(int i=0;i<nbOfTuples;i++,retPtr++,thisBBPtr+=nbOfComp)
2221           *retPtr=bbt.getNbOfIntersectingElems(thisBBPtr);
2222         break;
2223       }
2224     default:
2225       throw INTERP_KERNEL::Exception("DataArrayDouble::computeNbOfInteractionsWith : space dimension supported are [1,2,3] !");
2226   }
2227
2228   return ret.retn();
2229 }
2230
2231 /*!
2232  * Returns a copy of \a this array by excluding coincident tuples. Each tuple is
2233  * considered as coordinates of a point in getNumberOfComponents()-dimensional
2234  * space. The distance between tuples is computed using norm2. If several tuples are
2235  * not far each from other than \a prec, only one of them remains in the result
2236  * array. The order of tuples in the result array is same as in \a this one except
2237  * that coincident tuples are excluded.
2238  *  \param [in] prec - minimal absolute distance between two tuples at which they are
2239  *              considered not coincident.
2240  *  \param [in] limitTupleId - limit tuple id. If all tuples within a group of coincident
2241  *              tuples have id strictly lower than \a limitTupleId then they are not excluded.
2242  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
2243  *          is to delete using decrRef() as it is no more needed.
2244  *  \throw If \a this is not allocated.
2245  *  \throw If the number of components is not in [1,2,3,4].
2246  *
2247  *  \if ENABLE_EXAMPLES
2248  *  \ref py_mcdataarraydouble_getdifferentvalues "Here is a Python example".
2249  *  \endif
2250  */
2251 DataArrayDouble *DataArrayDouble::getDifferentValues(double prec, int limitTupleId) const
2252 {
2253   checkAllocated();
2254   DataArrayInt *c0=0,*cI0=0;
2255   findCommonTuples(prec,limitTupleId,c0,cI0);
2256   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> c(c0),cI(cI0);
2257   int newNbOfTuples=-1;
2258   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(getNumberOfTuples(),c0->begin(),cI0->begin(),cI0->end(),newNbOfTuples);
2259   return renumberAndReduce(o2n->getConstPointer(),newNbOfTuples);
2260 }
2261
2262 /*!
2263  * Copy all components in a specified order from another DataArrayDouble.
2264  * Both numerical and textual data is copied. The number of tuples in \a this and
2265  * the other array can be different.
2266  *  \param [in] a - the array to copy data from.
2267  *  \param [in] compoIds - sequence of zero based indices of components, data of which is
2268  *              to be copied.
2269  *  \throw If \a a is NULL.
2270  *  \throw If \a compoIds.size() != \a a->getNumberOfComponents().
2271  *  \throw If \a compoIds[i] < 0 or \a compoIds[i] > \a this->getNumberOfComponents().
2272  *
2273  *  \if ENABLE_EXAMPLES
2274  *  \ref py_mcdataarraydouble_setselectedcomponents "Here is a Python example".
2275  *  \endif
2276  */
2277 void DataArrayDouble::setSelectedComponents(const DataArrayDouble *a, const std::vector<int>& compoIds)
2278 {
2279   if(!a)
2280     throw INTERP_KERNEL::Exception("DataArrayDouble::setSelectedComponents : input DataArrayDouble is NULL !");
2281   checkAllocated();
2282   copyPartOfStringInfoFrom2(compoIds,*a);
2283   std::size_t partOfCompoSz=compoIds.size();
2284   int nbOfCompo=getNumberOfComponents();
2285   int nbOfTuples=std::min(getNumberOfTuples(),a->getNumberOfTuples());
2286   const double *ac=a->getConstPointer();
2287   double *nc=getPointer();
2288   for(int i=0;i<nbOfTuples;i++)
2289     for(std::size_t j=0;j<partOfCompoSz;j++,ac++)
2290       nc[nbOfCompo*i+compoIds[j]]=*ac;
2291 }
2292
2293 /*!
2294  * Copy all values from another DataArrayDouble into specified tuples and components
2295  * of \a this array. Textual data is not copied.
2296  * The tree parameters defining set of indices of tuples and components are similar to
2297  * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
2298  *  \param [in] a - the array to copy values from.
2299  *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
2300  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
2301  *              are located.
2302  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
2303  *  \param [in] bgComp - index of the first component of \a this array to assign values to.
2304  *  \param [in] endComp - index of the component before which the components to assign
2305  *              to are located.
2306  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2307  *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
2308  *              must be equal to the number of columns to assign to, else an
2309  *              exception is thrown; if \a false, then it is only required that \a
2310  *              a->getNbOfElems() equals to number of values to assign to (this condition
2311  *              must be respected even if \a strictCompoCompare is \a true). The number of 
2312  *              values to assign to is given by following Python expression:
2313  *              \a nbTargetValues = 
2314  *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
2315  *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2316  *  \throw If \a a is NULL.
2317  *  \throw If \a a is not allocated.
2318  *  \throw If \a this is not allocated.
2319  *  \throw If parameters specifying tuples and components to assign to do not give a
2320  *            non-empty range of increasing indices.
2321  *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
2322  *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
2323  *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2324  *
2325  *  \if ENABLE_EXAMPLES
2326  *  \ref py_mcdataarraydouble_setpartofvalues1 "Here is a Python example".
2327  *  \endif
2328  */
2329 void DataArrayDouble::setPartOfValues1(const DataArrayDouble *a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
2330 {
2331   if(!a)
2332     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues1 : input DataArrayDouble is NULL !");
2333   const char msg[]="DataArrayDouble::setPartOfValues1";
2334   checkAllocated();
2335   a->checkAllocated();
2336   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2337   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2338   int nbComp=getNumberOfComponents();
2339   int nbOfTuples=getNumberOfTuples();
2340   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2341   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2342   bool assignTech=true;
2343   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2344     {
2345       if(strictCompoCompare)
2346         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2347     }
2348   else
2349     {
2350       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2351       assignTech=false;
2352     }
2353   const double *srcPt=a->getConstPointer();
2354   double *pt=getPointer()+bgTuples*nbComp+bgComp;
2355   if(assignTech)
2356     {
2357       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2358         for(int j=0;j<newNbOfComp;j++,srcPt++)
2359           pt[j*stepComp]=*srcPt;
2360     }
2361   else
2362     {
2363       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2364         {
2365           const double *srcPt2=srcPt;
2366           for(int j=0;j<newNbOfComp;j++,srcPt2++)
2367             pt[j*stepComp]=*srcPt2;
2368         }
2369     }
2370 }
2371
2372 /*!
2373  * Assign a given value to values at specified tuples and components of \a this array.
2374  * The tree parameters defining set of indices of tuples and components are similar to
2375  * the tree parameters of the Python function \c range(\c start,\c stop,\c step)..
2376  *  \param [in] a - the value to assign.
2377  *  \param [in] bgTuples - index of the first tuple of \a this array to assign to.
2378  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
2379  *              are located.
2380  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
2381  *  \param [in] bgComp - index of the first component of \a this array to assign to.
2382  *  \param [in] endComp - index of the component before which the components to assign
2383  *              to are located.
2384  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2385  *  \throw If \a this is not allocated.
2386  *  \throw If parameters specifying tuples and components to assign to, do not give a
2387  *            non-empty range of increasing indices or indices are out of a valid range
2388  *            for \c this array.
2389  *
2390  *  \if ENABLE_EXAMPLES
2391  *  \ref py_mcdataarraydouble_setpartofvaluessimple1 "Here is a Python example".
2392  *  \endif
2393  */
2394 void DataArrayDouble::setPartOfValuesSimple1(double a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp)
2395 {
2396   const char msg[]="DataArrayDouble::setPartOfValuesSimple1";
2397   checkAllocated();
2398   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2399   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2400   int nbComp=getNumberOfComponents();
2401   int nbOfTuples=getNumberOfTuples();
2402   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2403   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2404   double *pt=getPointer()+bgTuples*nbComp+bgComp;
2405   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2406     for(int j=0;j<newNbOfComp;j++)
2407       pt[j*stepComp]=a;
2408 }
2409
2410 /*!
2411  * Copy all values from another DataArrayDouble (\a a) into specified tuples and 
2412  * components of \a this array. Textual data is not copied.
2413  * The tuples and components to assign to are defined by C arrays of indices.
2414  * There are two *modes of usage*:
2415  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
2416  *   of \a a is assigned to its own location within \a this array. 
2417  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
2418  *   components of every specified tuple of \a this array. In this mode it is required
2419  *   that \a a->getNumberOfComponents() equals to the number of specified components.
2420  *
2421  *  \param [in] a - the array to copy values from.
2422  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2423  *              assign values of \a a to.
2424  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2425  *              pointer to a tuple index <em>(pi)</em> varies as this: 
2426  *              \a bgTuples <= \a pi < \a endTuples.
2427  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
2428  *              assign values of \a a to.
2429  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
2430  *              pointer to a component index <em>(pi)</em> varies as this: 
2431  *              \a bgComp <= \a pi < \a endComp.
2432  *  \param [in] strictCompoCompare - this parameter is checked only if the
2433  *               *mode of usage* is the first; if it is \a true (default), 
2434  *               then \a a->getNumberOfComponents() must be equal 
2435  *               to the number of specified columns, else this is not required.
2436  *  \throw If \a a is NULL.
2437  *  \throw If \a a is not allocated.
2438  *  \throw If \a this is not allocated.
2439  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
2440  *         out of a valid range for \a this array.
2441  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
2442  *         if <em> a->getNumberOfComponents() != (endComp - bgComp) </em>.
2443  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
2444  *         <em> a->getNumberOfComponents() != (endComp - bgComp)</em>.
2445  *
2446  *  \if ENABLE_EXAMPLES
2447  *  \ref py_mcdataarraydouble_setpartofvalues2 "Here is a Python example".
2448  *  \endif
2449  */
2450 void DataArrayDouble::setPartOfValues2(const DataArrayDouble *a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
2451 {
2452   if(!a)
2453     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues2 : input DataArrayDouble is NULL !");
2454   const char msg[]="DataArrayDouble::setPartOfValues2";
2455   checkAllocated();
2456   a->checkAllocated();
2457   int nbComp=getNumberOfComponents();
2458   int nbOfTuples=getNumberOfTuples();
2459   for(const int *z=bgComp;z!=endComp;z++)
2460     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2461   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
2462   int newNbOfComp=(int)std::distance(bgComp,endComp);
2463   bool assignTech=true;
2464   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2465     {
2466       if(strictCompoCompare)
2467         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2468     }
2469   else
2470     {
2471       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2472       assignTech=false;
2473     }
2474   double *pt=getPointer();
2475   const double *srcPt=a->getConstPointer();
2476   if(assignTech)
2477     {    
2478       for(const int *w=bgTuples;w!=endTuples;w++)
2479         {
2480           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2481           for(const int *z=bgComp;z!=endComp;z++,srcPt++)
2482             {    
2483               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt;
2484             }
2485         }
2486     }
2487   else
2488     {
2489       for(const int *w=bgTuples;w!=endTuples;w++)
2490         {
2491           const double *srcPt2=srcPt;
2492           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2493           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
2494             {    
2495               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt2;
2496             }
2497         }
2498     }
2499 }
2500
2501 /*!
2502  * Assign a given value to values at specified tuples and components of \a this array.
2503  * The tuples and components to assign to are defined by C arrays of indices.
2504  *  \param [in] a - the value to assign.
2505  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2506  *              assign \a a to.
2507  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2508  *              pointer to a tuple index (\a pi) varies as this: 
2509  *              \a bgTuples <= \a pi < \a endTuples.
2510  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
2511  *              assign \a a to.
2512  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
2513  *              pointer to a component index (\a pi) varies as this: 
2514  *              \a bgComp <= \a pi < \a endComp.
2515  *  \throw If \a this is not allocated.
2516  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
2517  *         out of a valid range for \a this array.
2518  *
2519  *  \if ENABLE_EXAMPLES
2520  *  \ref py_mcdataarraydouble_setpartofvaluessimple2 "Here is a Python example".
2521  *  \endif
2522  */
2523 void DataArrayDouble::setPartOfValuesSimple2(double a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp)
2524 {
2525   checkAllocated();
2526   int nbComp=getNumberOfComponents();
2527   int nbOfTuples=getNumberOfTuples();
2528   for(const int *z=bgComp;z!=endComp;z++)
2529     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2530   double *pt=getPointer();
2531   for(const int *w=bgTuples;w!=endTuples;w++)
2532     for(const int *z=bgComp;z!=endComp;z++)
2533       {
2534         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2535         pt[(std::size_t)(*w)*nbComp+(*z)]=a;
2536       }
2537 }
2538
2539 /*!
2540  * Copy all values from another DataArrayDouble (\a a) into specified tuples and 
2541  * components of \a this array. Textual data is not copied.
2542  * The tuples to assign to are defined by a C array of indices.
2543  * The components to assign to are defined by three values similar to parameters of
2544  * the Python function \c range(\c start,\c stop,\c step).
2545  * There are two *modes of usage*:
2546  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
2547  *   of \a a is assigned to its own location within \a this array. 
2548  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
2549  *   components of every specified tuple of \a this array. In this mode it is required
2550  *   that \a a->getNumberOfComponents() equals to the number of specified components.
2551  *
2552  *  \param [in] a - the array to copy values from.
2553  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2554  *              assign values of \a a to.
2555  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2556  *              pointer to a tuple index <em>(pi)</em> varies as this: 
2557  *              \a bgTuples <= \a pi < \a endTuples.
2558  *  \param [in] bgComp - index of the first component of \a this array to assign to.
2559  *  \param [in] endComp - index of the component before which the components to assign
2560  *              to are located.
2561  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2562  *  \param [in] strictCompoCompare - this parameter is checked only in the first
2563  *               *mode of usage*; if \a strictCompoCompare is \a true (default), 
2564  *               then \a a->getNumberOfComponents() must be equal 
2565  *               to the number of specified columns, else this is not required.
2566  *  \throw If \a a is NULL.
2567  *  \throw If \a a is not allocated.
2568  *  \throw If \a this is not allocated.
2569  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
2570  *         \a this array.
2571  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
2572  *         if <em> a->getNumberOfComponents()</em> is unequal to the number of components
2573  *         defined by <em>(bgComp,endComp,stepComp)</em>.
2574  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
2575  *         <em> a->getNumberOfComponents()</em> is unequal to the number of components
2576  *         defined by <em>(bgComp,endComp,stepComp)</em>.
2577  *  \throw If parameters specifying components to assign to, do not give a
2578  *            non-empty range of increasing indices or indices are out of a valid range
2579  *            for \c this array.
2580  *
2581  *  \if ENABLE_EXAMPLES
2582  *  \ref py_mcdataarraydouble_setpartofvalues3 "Here is a Python example".
2583  *  \endif
2584  */
2585 void DataArrayDouble::setPartOfValues3(const DataArrayDouble *a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
2586 {
2587   if(!a)
2588     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues3 : input DataArrayDouble is NULL !");
2589   const char msg[]="DataArrayDouble::setPartOfValues3";
2590   checkAllocated();
2591   a->checkAllocated();
2592   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2593   int nbComp=getNumberOfComponents();
2594   int nbOfTuples=getNumberOfTuples();
2595   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2596   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
2597   bool assignTech=true;
2598   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2599     {
2600       if(strictCompoCompare)
2601         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2602     }
2603   else
2604     {
2605       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2606       assignTech=false;
2607     }
2608   double *pt=getPointer()+bgComp;
2609   const double *srcPt=a->getConstPointer();
2610   if(assignTech)
2611     {
2612       for(const int *w=bgTuples;w!=endTuples;w++)
2613         for(int j=0;j<newNbOfComp;j++,srcPt++)
2614           {
2615             DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2616             pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt;
2617           }
2618     }
2619   else
2620     {
2621       for(const int *w=bgTuples;w!=endTuples;w++)
2622         {
2623           const double *srcPt2=srcPt;
2624           for(int j=0;j<newNbOfComp;j++,srcPt2++)
2625             {
2626               DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2627               pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt2;
2628             }
2629         }
2630     }
2631 }
2632
2633 /*!
2634  * Assign a given value to values at specified tuples and components of \a this array.
2635  * The tuples to assign to are defined by a C array of indices.
2636  * The components to assign to are defined by three values similar to parameters of
2637  * the Python function \c range(\c start,\c stop,\c step).
2638  *  \param [in] a - the value to assign.
2639  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
2640  *              assign \a a to.
2641  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
2642  *              pointer to a tuple index <em>(pi)</em> varies as this: 
2643  *              \a bgTuples <= \a pi < \a endTuples.
2644  *  \param [in] bgComp - index of the first component of \a this array to assign to.
2645  *  \param [in] endComp - index of the component before which the components to assign
2646  *              to are located.
2647  *  \param [in] stepComp - index increment to get index of the next component to assign to.
2648  *  \throw If \a this is not allocated.
2649  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
2650  *         \a this array.
2651  *  \throw If parameters specifying components to assign to, do not give a
2652  *            non-empty range of increasing indices or indices are out of a valid range
2653  *            for \c this array.
2654  *
2655  *  \if ENABLE_EXAMPLES
2656  *  \ref py_mcdataarraydouble_setpartofvaluessimple3 "Here is a Python example".
2657  *  \endif
2658  */
2659 void DataArrayDouble::setPartOfValuesSimple3(double a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp)
2660 {
2661   const char msg[]="DataArrayDouble::setPartOfValuesSimple3";
2662   checkAllocated();
2663   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
2664   int nbComp=getNumberOfComponents();
2665   int nbOfTuples=getNumberOfTuples();
2666   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
2667   double *pt=getPointer()+bgComp;
2668   for(const int *w=bgTuples;w!=endTuples;w++)
2669     for(int j=0;j<newNbOfComp;j++)
2670       {
2671         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
2672         pt[(std::size_t)(*w)*nbComp+j*stepComp]=a;
2673       }
2674 }
2675
2676 /*!
2677  * Copy all values from another DataArrayDouble into specified tuples and components
2678  * of \a this array. Textual data is not copied.
2679  * The tree parameters defining set of indices of tuples and components are similar to
2680  * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
2681  *  \param [in] a - the array to copy values from.
2682  *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
2683  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
2684  *              are located.
2685  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
2686  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
2687  *              assign \a a to.
2688  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
2689  *              pointer to a component index (\a pi) varies as this: 
2690  *              \a bgComp <= \a pi < \a endComp.
2691  *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
2692  *              must be equal to the number of columns to assign to, else an
2693  *              exception is thrown; if \a false, then it is only required that \a
2694  *              a->getNbOfElems() equals to number of values to assign to (this condition
2695  *              must be respected even if \a strictCompoCompare is \a true). The number of 
2696  *              values to assign to is given by following Python expression:
2697  *              \a nbTargetValues = 
2698  *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
2699  *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2700  *  \throw If \a a is NULL.
2701  *  \throw If \a a is not allocated.
2702  *  \throw If \a this is not allocated.
2703  *  \throw If parameters specifying tuples and components to assign to do not give a
2704  *            non-empty range of increasing indices.
2705  *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
2706  *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
2707  *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
2708  *
2709  */
2710 void DataArrayDouble::setPartOfValues4(const DataArrayDouble *a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
2711 {
2712   if(!a)
2713     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValues4 : input DataArrayDouble is NULL !");
2714   const char msg[]="DataArrayDouble::setPartOfValues4";
2715   checkAllocated();
2716   a->checkAllocated();
2717   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2718   int newNbOfComp=(int)std::distance(bgComp,endComp);
2719   int nbComp=getNumberOfComponents();
2720   for(const int *z=bgComp;z!=endComp;z++)
2721     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2722   int nbOfTuples=getNumberOfTuples();
2723   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2724   bool assignTech=true;
2725   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
2726     {
2727       if(strictCompoCompare)
2728         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
2729     }
2730   else
2731     {
2732       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
2733       assignTech=false;
2734     }
2735   const double *srcPt=a->getConstPointer();
2736   double *pt=getPointer()+bgTuples*nbComp;
2737   if(assignTech)
2738     {
2739       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2740         for(const int *z=bgComp;z!=endComp;z++,srcPt++)
2741           pt[*z]=*srcPt;
2742     }
2743   else
2744     {
2745       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2746         {
2747           const double *srcPt2=srcPt;
2748           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
2749             pt[*z]=*srcPt2;
2750         }
2751     }
2752 }
2753
2754 void DataArrayDouble::setPartOfValuesSimple4(double a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp)
2755 {
2756   const char msg[]="DataArrayDouble::setPartOfValuesSimple4";
2757   checkAllocated();
2758   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
2759   int nbComp=getNumberOfComponents();
2760   for(const int *z=bgComp;z!=endComp;z++)
2761     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
2762   int nbOfTuples=getNumberOfTuples();
2763   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
2764   double *pt=getPointer()+bgTuples*nbComp;
2765   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
2766     for(const int *z=bgComp;z!=endComp;z++)
2767       pt[*z]=a;
2768 }
2769
2770 /*!
2771  * Copy some tuples from another DataArrayDouble into specified tuples
2772  * of \a this array. Textual data is not copied. Both arrays must have equal number of
2773  * components.
2774  * Both the tuples to assign and the tuples to assign to are defined by a DataArrayInt.
2775  * All components of selected tuples are copied.
2776  *  \param [in] a - the array to copy values from.
2777  *  \param [in] tuplesSelec - the array specifying both source tuples of \a a and
2778  *              target tuples of \a this. \a tuplesSelec has two components, and the
2779  *              first component specifies index of the source tuple and the second
2780  *              one specifies index of the target tuple.
2781  *  \throw If \a this is not allocated.
2782  *  \throw If \a a is NULL.
2783  *  \throw If \a a is not allocated.
2784  *  \throw If \a tuplesSelec is NULL.
2785  *  \throw If \a tuplesSelec is not allocated.
2786  *  \throw If <em>this->getNumberOfComponents() != a->getNumberOfComponents()</em>.
2787  *  \throw If \a tuplesSelec->getNumberOfComponents() != 2.
2788  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
2789  *         the corresponding (\a this or \a a) array.
2790  */
2791 void DataArrayDouble::setPartOfValuesAdv(const DataArrayDouble *a, const DataArrayInt *tuplesSelec)
2792 {
2793   if(!a || !tuplesSelec)
2794     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValuesAdv : input DataArrayDouble is NULL !");
2795   checkAllocated();
2796   a->checkAllocated();
2797   tuplesSelec->checkAllocated();
2798   int nbOfComp=getNumberOfComponents();
2799   if(nbOfComp!=a->getNumberOfComponents())
2800     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValuesAdv : This and a do not have the same number of components !");
2801   if(tuplesSelec->getNumberOfComponents()!=2)
2802     throw INTERP_KERNEL::Exception("DataArrayDouble::setPartOfValuesAdv : Expecting to have a tuple selector DataArrayInt instance with exactly 2 components !");
2803   int thisNt=getNumberOfTuples();
2804   int aNt=a->getNumberOfTuples();
2805   double *valsToSet=getPointer();
2806   const double *valsSrc=a->getConstPointer();
2807   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple+=2)
2808     {
2809       if(tuple[1]>=0 && tuple[1]<aNt)
2810         {
2811           if(tuple[0]>=0 && tuple[0]<thisNt)
2812             std::copy(valsSrc+nbOfComp*tuple[1],valsSrc+nbOfComp*(tuple[1]+1),valsToSet+nbOfComp*tuple[0]);
2813           else
2814             {
2815               std::ostringstream oss; oss << "DataArrayDouble::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
2816               oss << " of 'tuplesSelec' request of tuple id #" << tuple[0] << " in 'this' ! It should be in [0," << thisNt << ") !";
2817               throw INTERP_KERNEL::Exception(oss.str().c_str());
2818             }
2819         }
2820       else
2821         {
2822           std::ostringstream oss; oss << "DataArrayDouble::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
2823           oss << " of 'tuplesSelec' request of tuple id #" << tuple[1] << " in 'a' ! It should be in [0," << aNt << ") !";
2824           throw INTERP_KERNEL::Exception(oss.str().c_str());
2825         }
2826     }
2827 }
2828
2829 /*!
2830  * Copy some tuples from another DataArrayDouble (\a aBase) into contiguous tuples
2831  * of \a this array. Textual data is not copied. Both arrays must have equal number of
2832  * components.
2833  * The tuples to assign to are defined by index of the first tuple, and
2834  * their number is defined by \a tuplesSelec->getNumberOfTuples().
2835  * The tuples to copy are defined by values of a DataArrayInt.
2836  * All components of selected tuples are copied.
2837  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
2838  *              values to.
2839  *  \param [in] aBase - the array to copy values from.
2840  *  \param [in] tuplesSelec - the array specifying tuples of \a a to copy.
2841  *  \throw If \a this is not allocated.
2842  *  \throw If \a aBase is NULL.
2843  *  \throw If \a aBase is not allocated.
2844  *  \throw If \a tuplesSelec is NULL.
2845  *  \throw If \a tuplesSelec is not allocated.
2846  *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
2847  *  \throw If \a tuplesSelec->getNumberOfComponents() != 1.
2848  *  \throw If <em>tupleIdStart + tuplesSelec->getNumberOfTuples() > this->getNumberOfTuples().</em>
2849  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
2850  *         \a aBase array.
2851  */
2852 void DataArrayDouble::setContigPartOfSelectedValues(int tupleIdStart, const DataArray *aBase, const DataArrayInt *tuplesSelec)
2853 {
2854   if(!aBase || !tuplesSelec)
2855     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : input DataArray is NULL !");
2856   const DataArrayDouble *a=dynamic_cast<const DataArrayDouble *>(aBase);
2857   if(!a)
2858     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : input DataArray aBase is not a DataArrayDouble !");
2859   checkAllocated();
2860   a->checkAllocated();
2861   tuplesSelec->checkAllocated();
2862   int nbOfComp=getNumberOfComponents();
2863   if(nbOfComp!=a->getNumberOfComponents())
2864     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : This and a do not have the same number of components !");
2865   if(tuplesSelec->getNumberOfComponents()!=1)
2866     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : Expecting to have a tuple selector DataArrayInt instance with exactly 1 component !");
2867   int thisNt=getNumberOfTuples();
2868   int aNt=a->getNumberOfTuples();
2869   int nbOfTupleToWrite=tuplesSelec->getNumberOfTuples();
2870   double *valsToSet=getPointer()+tupleIdStart*nbOfComp;
2871   if(tupleIdStart+nbOfTupleToWrite>thisNt)
2872     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : invalid number range of values to write !");
2873   const double *valsSrc=a->getConstPointer();
2874   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple++,valsToSet+=nbOfComp)
2875     {
2876       if(*tuple>=0 && *tuple<aNt)
2877         {
2878           std::copy(valsSrc+nbOfComp*(*tuple),valsSrc+nbOfComp*(*tuple+1),valsToSet);
2879         }
2880       else
2881         {
2882           std::ostringstream oss; oss << "DataArrayDouble::setContigPartOfSelectedValues : Tuple #" << std::distance(tuplesSelec->begin(),tuple);
2883           oss << " of 'tuplesSelec' request of tuple id #" << *tuple << " in 'a' ! It should be in [0," << aNt << ") !";
2884           throw INTERP_KERNEL::Exception(oss.str().c_str());
2885         }
2886     }
2887 }
2888
2889 /*!
2890  * Copy some tuples from another DataArrayDouble (\a aBase) into contiguous tuples
2891  * of \a this array. Textual data is not copied. Both arrays must have equal number of
2892  * components.
2893  * The tuples to copy are defined by three values similar to parameters of
2894  * the Python function \c range(\c start,\c stop,\c step).
2895  * The tuples to assign to are defined by index of the first tuple, and
2896  * their number is defined by number of tuples to copy.
2897  * All components of selected tuples are copied.
2898  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
2899  *              values to.
2900  *  \param [in] aBase - the array to copy values from.
2901  *  \param [in] bg - index of the first tuple to copy of the array \a aBase.
2902  *  \param [in] end2 - index of the tuple of \a aBase before which the tuples to copy
2903  *              are located.
2904  *  \param [in] step - index increment to get index of the next tuple to copy.
2905  *  \throw If \a this is not allocated.
2906  *  \throw If \a aBase is NULL.
2907  *  \throw If \a aBase is not allocated.
2908  *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
2909  *  \throw If <em>tupleIdStart + len(range(bg,end2,step)) > this->getNumberOfTuples().</em>
2910  *  \throw If parameters specifying tuples to copy, do not give a
2911  *            non-empty range of increasing indices or indices are out of a valid range
2912  *            for the array \a aBase.
2913  */
2914 void DataArrayDouble::setContigPartOfSelectedValues2(int tupleIdStart, const DataArray *aBase, int bg, int end2, int step)
2915 {
2916   if(!aBase)
2917     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : input DataArray is NULL !");
2918   const DataArrayDouble *a=dynamic_cast<const DataArrayDouble *>(aBase);
2919   if(!a)
2920     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : input DataArray aBase is not a DataArrayDouble !");
2921   checkAllocated();
2922   a->checkAllocated();
2923   int nbOfComp=getNumberOfComponents();
2924   const char msg[]="DataArrayDouble::setContigPartOfSelectedValues2";
2925   int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg);
2926   if(nbOfComp!=a->getNumberOfComponents())
2927     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : This and a do not have the same number of components !");
2928   int thisNt=getNumberOfTuples();
2929   int aNt=a->getNumberOfTuples();
2930   double *valsToSet=getPointer()+tupleIdStart*nbOfComp;
2931   if(tupleIdStart+nbOfTupleToWrite>thisNt)
2932     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid number range of values to write !");
2933   if(end2>aNt)
2934     throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid range of values to read !");
2935   const double *valsSrc=a->getConstPointer()+bg*nbOfComp;
2936   for(int i=0;i<nbOfTupleToWrite;i++,valsToSet+=nbOfComp,valsSrc+=step*nbOfComp)
2937     {
2938       std::copy(valsSrc,valsSrc+nbOfComp,valsToSet);
2939     }
2940 }
2941
2942 /*!
2943  * Returns a value located at specified tuple and component.
2944  * This method is equivalent to DataArrayDouble::getIJ() except that validity of
2945  * parameters is checked. So this method is safe but expensive if used to go through
2946  * all values of \a this.
2947  *  \param [in] tupleId - index of tuple of interest.
2948  *  \param [in] compoId - index of component of interest.
2949  *  \return double - value located by \a tupleId and \a compoId.
2950  *  \throw If \a this is not allocated.
2951  *  \throw If condition <em>( 0 <= tupleId < this->getNumberOfTuples() )</em> is violated.
2952  *  \throw If condition <em>( 0 <= compoId < this->getNumberOfComponents() )</em> is violated.
2953  */
2954 double DataArrayDouble::getIJSafe(int tupleId, int compoId) const
2955 {
2956   checkAllocated();
2957   if(tupleId<0 || tupleId>=getNumberOfTuples())
2958     {
2959       std::ostringstream oss; oss << "DataArrayDouble::getIJSafe : request for tupleId " << tupleId << " should be in [0," << getNumberOfTuples() << ") !";
2960       throw INTERP_KERNEL::Exception(oss.str().c_str());
2961     }
2962   if(compoId<0 || compoId>=getNumberOfComponents())
2963     {
2964       std::ostringstream oss; oss << "DataArrayDouble::getIJSafe : request for compoId " << compoId << " should be in [0," << getNumberOfComponents() << ") !";
2965       throw INTERP_KERNEL::Exception(oss.str().c_str());
2966     }
2967   return _mem[tupleId*_info_on_compo.size()+compoId];
2968 }
2969
2970 /*!
2971  * Returns the first value of \a this. 
2972  *  \return double - the last value of \a this array.
2973  *  \throw If \a this is not allocated.
2974  *  \throw If \a this->getNumberOfComponents() != 1.
2975  *  \throw If \a this->getNumberOfTuples() < 1.
2976  */
2977 double DataArrayDouble::front() const
2978 {
2979   checkAllocated();
2980   if(getNumberOfComponents()!=1)
2981     throw INTERP_KERNEL::Exception("DataArrayDouble::front : number of components not equal to one !");
2982   int nbOfTuples=getNumberOfTuples();
2983   if(nbOfTuples<1)
2984     throw INTERP_KERNEL::Exception("DataArrayDouble::front : number of tuples must be >= 1 !");
2985   return *(getConstPointer());
2986 }
2987
2988 /*!
2989  * Returns the last value of \a this. 
2990  *  \return double - the last value of \a this array.
2991  *  \throw If \a this is not allocated.
2992  *  \throw If \a this->getNumberOfComponents() != 1.
2993  *  \throw If \a this->getNumberOfTuples() < 1.
2994  */
2995 double DataArrayDouble::back() const
2996 {
2997   checkAllocated();
2998   if(getNumberOfComponents()!=1)
2999     throw INTERP_KERNEL::Exception("DataArrayDouble::back : number of components not equal to one !");
3000   int nbOfTuples=getNumberOfTuples();
3001   if(nbOfTuples<1)
3002     throw INTERP_KERNEL::Exception("DataArrayDouble::back : number of tuples must be >= 1 !");
3003   return *(getConstPointer()+nbOfTuples-1);
3004 }
3005
3006 void DataArrayDouble::SetArrayIn(DataArrayDouble *newArray, DataArrayDouble* &arrayToSet)
3007 {
3008   if(newArray!=arrayToSet)
3009     {
3010       if(arrayToSet)
3011         arrayToSet->decrRef();
3012       arrayToSet=newArray;
3013       if(arrayToSet)
3014         arrayToSet->incrRef();
3015     }
3016 }
3017
3018 /*!
3019  * Sets a C array to be used as raw data of \a this. The previously set info
3020  *  of components is retained and re-sized. 
3021  * For more info see \ref MEDCouplingArraySteps1.
3022  *  \param [in] array - the C array to be used as raw data of \a this.
3023  *  \param [in] ownership - if \a true, \a array will be deallocated at destruction of \a this.
3024  *  \param [in] type - specifies how to deallocate \a array. If \a type == ParaMEDMEM::CPP_DEALLOC,
3025  *                     \c delete [] \c array; will be called. If \a type == ParaMEDMEM::C_DEALLOC,
3026  *                     \c free(\c array ) will be called.
3027  *  \param [in] nbOfTuple - new number of tuples in \a this.
3028  *  \param [in] nbOfCompo - new number of components in \a this.
3029  */
3030 void DataArrayDouble::useArray(const double *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo)
3031 {
3032   _info_on_compo.resize(nbOfCompo);
3033   _mem.useArray(array,ownership,type,(std::size_t)nbOfTuple*nbOfCompo);
3034   declareAsNew();
3035 }
3036
3037 void DataArrayDouble::useExternalArrayWithRWAccess(const double *array, int nbOfTuple, int nbOfCompo)
3038 {
3039   _info_on_compo.resize(nbOfCompo);
3040   _mem.useExternalArrayWithRWAccess(array,(std::size_t)nbOfTuple*nbOfCompo);
3041   declareAsNew();
3042 }
3043
3044 /*!
3045  * Checks if 0.0 value is present in \a this array. If it is the case, an exception
3046  * is thrown.
3047  * \throw If zero is found in \a this array.
3048  */
3049 void DataArrayDouble::checkNoNullValues() const
3050 {
3051   const double *tmp=getConstPointer();
3052   std::size_t nbOfElems=getNbOfElems();
3053   const double *where=std::find(tmp,tmp+nbOfElems,0.);
3054   if(where!=tmp+nbOfElems)
3055     throw INTERP_KERNEL::Exception("A value 0.0 have been detected !");
3056 }
3057
3058 /*!
3059  * Computes minimal and maximal value in each component. An output array is filled
3060  * with \c 2 * \a this->getNumberOfComponents() values, so the caller is to allocate
3061  * enough memory before calling this method.
3062  *  \param [out] bounds - array of size at least 2 *\a this->getNumberOfComponents().
3063  *               It is filled as follows:<br>
3064  *               \a bounds[0] = \c min_of_component_0 <br>
3065  *               \a bounds[1] = \c max_of_component_0 <br>
3066  *               \a bounds[2] = \c min_of_component_1 <br>
3067  *               \a bounds[3] = \c max_of_component_1 <br>
3068  *               ...
3069  */
3070 void DataArrayDouble::getMinMaxPerComponent(double *bounds) const
3071 {
3072   checkAllocated();
3073   int dim=getNumberOfComponents();
3074   for (int idim=0; idim<dim; idim++)
3075     {
3076       bounds[idim*2]=std::numeric_limits<double>::max();
3077       bounds[idim*2+1]=-std::numeric_limits<double>::max();
3078     } 
3079   const double *ptr=getConstPointer();
3080   int nbOfTuples=getNumberOfTuples();
3081   for(int i=0;i<nbOfTuples;i++)
3082     {
3083       for(int idim=0;idim<dim;idim++)
3084         {
3085           if(bounds[idim*2]>ptr[i*dim+idim])
3086             {
3087               bounds[idim*2]=ptr[i*dim+idim];
3088             }
3089           if(bounds[idim*2+1]<ptr[i*dim+idim])
3090             {
3091               bounds[idim*2+1]=ptr[i*dim+idim];
3092             }
3093         }
3094     }
3095 }
3096
3097 /*!
3098  * This method retrieves a newly allocated DataArrayDouble instance having same number of tuples than \a this and twice number of components than \a this
3099  * to store both the min and max per component of each tuples. 
3100  * \param [in] epsilon the width of the bbox (identical in each direction) - 0.0 by default
3101  *
3102  * \return a newly created DataArrayDouble instance having \c this->getNumberOfTuples() tuples and 2 * \c this->getNumberOfComponent() components
3103  *
3104  * \throw If \a this is not allocated yet.
3105  */
3106 DataArrayDouble *DataArrayDouble::computeBBoxPerTuple(double epsilon) const
3107 {
3108   checkAllocated();
3109   const double *dataPtr=getConstPointer();
3110   int nbOfCompo=getNumberOfComponents();
3111   int nbTuples=getNumberOfTuples();
3112   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> bbox=DataArrayDouble::New();
3113   bbox->alloc(nbTuples,2*nbOfCompo);
3114   double *bboxPtr=bbox->getPointer();
3115   for(int i=0;i<nbTuples;i++)
3116     {
3117       for(int j=0;j<nbOfCompo;j++)
3118         {
3119           bboxPtr[2*nbOfCompo*i+2*j]=dataPtr[nbOfCompo*i+j]-epsilon;
3120           bboxPtr[2*nbOfCompo*i+2*j+1]=dataPtr[nbOfCompo*i+j]+epsilon;
3121         }
3122     }
3123   return bbox.retn();
3124 }
3125
3126 /*!
3127  * For each tuples **t** in \a other, this method retrieves tuples in \a this that are equal to **t**.
3128  * Two tuples are considered equal if the euclidian distance between the two tuples is lower than \a eps.
3129  * 
3130  * \param [in] other a DataArrayDouble having same number of components than \a this.
3131  * \param [in] eps absolute precision representing distance (using infinite norm) between 2 tuples behind which 2 tuples are considered equal.
3132  * \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.
3133  *             \a cI allows to extract information in \a c.
3134  * \param [out] cI is an indirection array that allows to extract the data contained in \a c.
3135  *
3136  * \throw In case of:
3137  *  - \a this is not allocated
3138  *  - \a other is not allocated or null
3139  *  - \a this and \a other do not have the same number of components
3140  *  - if number of components of \a this is not in [1,2,3]
3141  *
3142  * \sa MEDCouplingPointSet::getNodeIdsNearPoints, DataArrayDouble::getDifferentValues
3143  */
3144 void DataArrayDouble::computeTupleIdsNearTuples(const DataArrayDouble *other, double eps, DataArrayInt *& c, DataArrayInt *& cI) const
3145 {
3146   if(!other)
3147     throw INTERP_KERNEL::Exception("DataArrayDouble::computeTupleIdsNearTuples : input pointer other is null !");
3148   checkAllocated();
3149   other->checkAllocated();
3150   int nbOfCompo=getNumberOfComponents();
3151   int otherNbOfCompo=other->getNumberOfComponents();
3152   if(nbOfCompo!=otherNbOfCompo)
3153     throw INTERP_KERNEL::Exception("DataArrayDouble::computeTupleIdsNearTuples : number of components should be equal between this and other !");
3154   int nbOfTuplesOther=other->getNumberOfTuples();
3155   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cArr(DataArrayInt::New()),cIArr(DataArrayInt::New()); cArr->alloc(0,1); cIArr->pushBackSilent(0);
3156   switch(nbOfCompo)
3157   {
3158     case 3:
3159       {
3160         BBTreePts<3,int> myTree(begin(),0,0,getNumberOfTuples(),eps);
3161         FindTupleIdsNearTuplesAlg<3>(myTree,other->getConstPointer(),nbOfTuplesOther,eps,cArr,cIArr);
3162         break;
3163       }
3164     case 2:
3165       {
3166         BBTreePts<2,int> myTree(begin(),0,0,getNumberOfTuples(),eps);
3167         FindTupleIdsNearTuplesAlg<2>(myTree,other->getConstPointer(),nbOfTuplesOther,eps,cArr,cIArr);
3168         break;
3169       }
3170     case 1:
3171       {
3172         BBTreePts<1,int> myTree(begin(),0,0,getNumberOfTuples(),eps);
3173         FindTupleIdsNearTuplesAlg<1>(myTree,other->getConstPointer(),nbOfTuplesOther,eps,cArr,cIArr);
3174         break;
3175       }
3176     default:
3177       throw INTERP_KERNEL::Exception("Unexpected spacedim of coords for computeTupleIdsNearTuples. Must be 1, 2 or 3.");
3178   }
3179   c=cArr.retn(); cI=cIArr.retn();
3180 }
3181
3182 /*!
3183  * 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
3184  * around origin of 'radius' 1.
3185  * 
3186  * \param [in] eps absolute epsilon. under that value of delta between max and min no scale is performed.
3187  */
3188 void DataArrayDouble::recenterForMaxPrecision(double eps)
3189 {
3190   checkAllocated();
3191   int dim=getNumberOfComponents();
3192   std::vector<double> bounds(2*dim);
3193   getMinMaxPerComponent(&bounds[0]);
3194   for(int i=0;i<dim;i++)
3195     {
3196       double delta=bounds[2*i+1]-bounds[2*i];
3197       double offset=(bounds[2*i]+bounds[2*i+1])/2.;
3198       if(delta>eps)
3199         applyLin(1./delta,-offset/delta,i);
3200       else
3201         applyLin(1.,-offset,i);
3202     }
3203 }
3204
3205 /*!
3206  * Returns the maximal value and its location within \a this one-dimensional array.
3207  *  \param [out] tupleId - index of the tuple holding the maximal value.
3208  *  \return double - the maximal value among all values of \a this array.
3209  *  \throw If \a this->getNumberOfComponents() != 1
3210  *  \throw If \a this->getNumberOfTuples() < 1
3211  */
3212 double DataArrayDouble::getMaxValue(int& tupleId) const
3213 {
3214   checkAllocated();
3215   if(getNumberOfComponents()!=1)
3216     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 !");
3217   int nbOfTuples=getNumberOfTuples();
3218   if(nbOfTuples<=0)
3219     throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : array exists but number of tuples must be > 0 !");
3220   const double *vals=getConstPointer();
3221   const double *loc=std::max_element(vals,vals+nbOfTuples);
3222   tupleId=(int)std::distance(vals,loc);
3223   return *loc;
3224 }
3225
3226 /*!
3227  * Returns the maximal value within \a this array that is allowed to have more than
3228  *  one component.
3229  *  \return double - the maximal value among all values of \a this array.
3230  *  \throw If \a this is not allocated.
3231  */
3232 double DataArrayDouble::getMaxValueInArray() const
3233 {
3234   checkAllocated();
3235   const double *loc=std::max_element(begin(),end());
3236   return *loc;
3237 }
3238
3239 /*!
3240  * Returns the maximal value and all its locations within \a this one-dimensional array.
3241  *  \param [out] tupleIds - a new instance of DataArrayInt containg indices of
3242  *               tuples holding the maximal value. The caller is to delete it using
3243  *               decrRef() as it is no more needed.
3244  *  \return double - the maximal value among all values of \a this array.
3245  *  \throw If \a this->getNumberOfComponents() != 1
3246  *  \throw If \a this->getNumberOfTuples() < 1
3247  */
3248 double DataArrayDouble::getMaxValue2(DataArrayInt*& tupleIds) const
3249 {
3250   int tmp;
3251   tupleIds=0;
3252   double ret=getMaxValue(tmp);
3253   tupleIds=getIdsInRange(ret,ret);
3254   return ret;
3255 }
3256
3257 /*!
3258  * Returns the minimal value and its location within \a this one-dimensional array.
3259  *  \param [out] tupleId - index of the tuple holding the minimal value.
3260  *  \return double - the minimal value among all values of \a this array.
3261  *  \throw If \a this->getNumberOfComponents() != 1
3262  *  \throw If \a this->getNumberOfTuples() < 1
3263  */
3264 double DataArrayDouble::getMinValue(int& tupleId) const
3265 {
3266   checkAllocated();
3267   if(getNumberOfComponents()!=1)
3268     throw INTERP_KERNEL::Exception("DataArrayDouble::getMinValue : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before call 'getMinValueInArray' method !");
3269   int nbOfTuples=getNumberOfTuples();
3270   if(nbOfTuples<=0)
3271     throw INTERP_KERNEL::Exception("DataArrayDouble::getMinValue : array exists but number of tuples must be > 0 !");
3272   const double *vals=getConstPointer();
3273   const double *loc=std::min_element(vals,vals+nbOfTuples);
3274   tupleId=(int)std::distance(vals,loc);
3275   return *loc;
3276 }
3277
3278 /*!
3279  * Returns the minimal value within \a this array that is allowed to have more than
3280  *  one component.
3281  *  \return double - the minimal value among all values of \a this array.
3282  *  \throw If \a this is not allocated.
3283  */
3284 double DataArrayDouble::getMinValueInArray() const
3285 {
3286   checkAllocated();
3287   const double *loc=std::min_element(begin(),end());
3288   return *loc;
3289 }
3290
3291 /*!
3292  * Returns the minimal value and all its locations within \a this one-dimensional array.
3293  *  \param [out] tupleIds - a new instance of DataArrayInt containg indices of
3294  *               tuples holding the minimal value. The caller is to delete it using
3295  *               decrRef() as it is no more needed.
3296  *  \return double - the minimal value among all values of \a this array.
3297  *  \throw If \a this->getNumberOfComponents() != 1
3298  *  \throw If \a this->getNumberOfTuples() < 1
3299  */
3300 double DataArrayDouble::getMinValue2(DataArrayInt*& tupleIds) const
3301 {
3302   int tmp;
3303   tupleIds=0;
3304   double ret=getMinValue(tmp);
3305   tupleIds=getIdsInRange(ret,ret);
3306   return ret;
3307 }
3308
3309 /*!
3310  * 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.
3311  * This method only works for single component array.
3312  *
3313  * \return a value in [ 0, \c this->getNumberOfTuples() )
3314  *
3315  * \throw If \a this is not allocated
3316  *
3317  */
3318 int DataArrayDouble::count(double value, double eps) const
3319 {
3320   int ret=0;
3321   checkAllocated();
3322   if(getNumberOfComponents()!=1)
3323     throw INTERP_KERNEL::Exception("DataArrayDouble::count : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before !");
3324   const double *vals=begin();
3325   int nbOfTuples=getNumberOfTuples();
3326   for(int i=0;i<nbOfTuples;i++,vals++)
3327     if(fabs(*vals-value)<=eps)
3328       ret++;
3329   return ret;
3330 }
3331
3332 /*!
3333  * Returns the average value of \a this one-dimensional array.
3334  *  \return double - the average value over all values of \a this array.
3335  *  \throw If \a this->getNumberOfComponents() != 1
3336  *  \throw If \a this->getNumberOfTuples() < 1
3337  */
3338 double DataArrayDouble::getAverageValue() const
3339 {
3340   if(getNumberOfComponents()!=1)
3341     throw INTERP_KERNEL::Exception("DataArrayDouble::getAverageValue : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before !");
3342   int nbOfTuples=getNumberOfTuples();
3343   if(nbOfTuples<=0)
3344     throw INTERP_KERNEL::Exception("DataArrayDouble::getAverageValue : array exists but number of tuples must be > 0 !");
3345   const double *vals=getConstPointer();
3346   double ret=std::accumulate(vals,vals+nbOfTuples,0.);
3347   return ret/nbOfTuples;
3348 }
3349
3350 /*!
3351  * Returns the Euclidean norm of the vector defined by \a this array.
3352  *  \return double - the value of the Euclidean norm, i.e.
3353  *          the square root of the inner product of vector.
3354  *  \throw If \a this is not allocated.
3355  */
3356 double DataArrayDouble::norm2() const
3357 {
3358   checkAllocated();
3359   double ret=0.;
3360   std::size_t nbOfElems=getNbOfElems();
3361   const double *pt=getConstPointer();
3362   for(std::size_t i=0;i<nbOfElems;i++,pt++)
3363     ret+=(*pt)*(*pt);
3364   return sqrt(ret);
3365 }
3366
3367 /*!
3368  * Returns the maximum norm of the vector defined by \a this array.
3369  * This method works even if the number of components is diferent from one.
3370  * If the number of elements in \a this is 0, -1. is returned.
3371  *  \return double - the value of the maximum norm, i.e.
3372  *          the maximal absolute value among values of \a this array (whatever its number of components).
3373  *  \throw If \a this is not allocated.
3374  */
3375 double DataArrayDouble::normMax() const
3376 {
3377   checkAllocated();
3378   double ret(-1.);
3379   std::size_t nbOfElems(getNbOfElems());
3380   const double *pt(getConstPointer());
3381   for(std::size_t i=0;i<nbOfElems;i++,pt++)
3382     {
3383       double val(std::abs(*pt));
3384       if(val>ret)
3385         ret=val;
3386     }
3387   return ret;
3388 }
3389
3390 /*!
3391  * Returns the minimum norm (absolute value) of the vector defined by \a this array.
3392  * This method works even if the number of components is diferent from one.
3393  * If the number of elements in \a this is 0, std::numeric_limits<double>::max() is returned.
3394  *  \return double - the value of the minimum norm, i.e.
3395  *          the minimal absolute value among values of \a this array (whatever its number of components).
3396  *  \throw If \a this is not allocated.
3397  */
3398 double DataArrayDouble::normMin() const
3399 {
3400   checkAllocated();
3401   double ret(std::numeric_limits<double>::max());
3402   std::size_t nbOfElems(getNbOfElems());
3403   const double *pt(getConstPointer());
3404   for(std::size_t i=0;i<nbOfElems;i++,pt++)
3405     {
3406       double val(std::abs(*pt));
3407       if(val<ret)
3408         ret=val;
3409     }
3410   return ret;
3411 }
3412
3413 /*!
3414  * Accumulates values of each component of \a this array.
3415  *  \param [out] res - an array of length \a this->getNumberOfComponents(), allocated 
3416  *         by the caller, that is filled by this method with sum value for each
3417  *         component.
3418  *  \throw If \a this is not allocated.
3419  */
3420 void DataArrayDouble::accumulate(double *res) const
3421 {
3422   checkAllocated();
3423   const double *ptr=getConstPointer();
3424   int nbTuple=getNumberOfTuples();
3425   int nbComps=getNumberOfComponents();
3426   std::fill(res,res+nbComps,0.);
3427   for(int i=0;i<nbTuple;i++)
3428     std::transform(ptr+i*nbComps,ptr+(i+1)*nbComps,res,res,std::plus<double>());
3429 }
3430
3431 /*!
3432  * This method returns the min distance from an external tuple defined by [ \a tupleBg , \a tupleEnd ) to \a this and
3433  * the first tuple in \a this that matches the returned distance. If there is no tuples in \a this an exception will be thrown.
3434  *
3435  *
3436  * \a this is expected to be allocated and expected to have a number of components equal to the distance from \a tupleBg to
3437  * \a tupleEnd. If not an exception will be thrown.
3438  *
3439  * \param [in] tupleBg start pointer (included) of input external tuple
3440  * \param [in] tupleEnd end pointer (not included) of input external tuple
3441  * \param [out] tupleId the tuple id in \a this that matches the min of distance between \a this and input external tuple
3442  * \return the min distance.
3443  * \sa MEDCouplingUMesh::distanceToPoint
3444  */
3445 double DataArrayDouble::distanceToTuple(const double *tupleBg, const double *tupleEnd, int& tupleId) const
3446 {
3447   checkAllocated();
3448   int nbTuple=getNumberOfTuples();
3449   int nbComps=getNumberOfComponents();
3450   if(nbComps!=(int)std::distance(tupleBg,tupleEnd))
3451     { 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()); }
3452   if(nbTuple==0)
3453     throw INTERP_KERNEL::Exception("DataArrayDouble::distanceToTuple : no tuple in this ! No distance to compute !");
3454   double ret0=std::numeric_limits<double>::max();
3455   tupleId=-1;
3456   const double *work=getConstPointer();
3457   for(int i=0;i<nbTuple;i++)
3458     {
3459       double val=0.;
3460       for(int j=0;j<nbComps;j++,work++) 
3461         val+=(*work-tupleBg[j])*((*work-tupleBg[j]));
3462       if(val>=ret0)
3463         continue;
3464       else
3465         { ret0=val; tupleId=i; }
3466     }
3467   return sqrt(ret0);
3468 }
3469
3470 /*!
3471  * Accumulate values of the given component of \a this array.
3472  *  \param [in] compId - the index of the component of interest.
3473  *  \return double - a sum value of \a compId-th component.
3474  *  \throw If \a this is not allocated.
3475  *  \throw If \a the condition ( 0 <= \a compId < \a this->getNumberOfComponents() ) is
3476  *         not respected.
3477  */
3478 double DataArrayDouble::accumulate(int compId) const
3479 {
3480   checkAllocated();
3481   const double *ptr=getConstPointer();
3482   int nbTuple=getNumberOfTuples();
3483   int nbComps=getNumberOfComponents();
3484   if(compId<0 || compId>=nbComps)
3485     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulate : Invalid compId specified : No such nb of components !");
3486   double ret=0.;
3487   for(int i=0;i<nbTuple;i++)
3488     ret+=ptr[i*nbComps+compId];
3489   return ret;
3490 }
3491
3492 /*!
3493  * This method accumulate using addition tuples in \a this using input index array [ \a bgOfIndex, \a endOfIndex ).
3494  * The returned array will have same number of components than \a this and number of tuples equal to
3495  * \c std::distance(bgOfIndex,endOfIndex) \b minus \b one.
3496  *
3497  * The input index array is expected to be ascendingly sorted in which the all referenced ids should be in [0, \c this->getNumberOfTuples).
3498  * 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.
3499  *
3500  * \param [in] bgOfIndex - begin (included) of the input index array.
3501  * \param [in] endOfIndex - end (excluded) of the input index array.
3502  * \return DataArrayDouble * - the new instance having the same number of components than \a this.
3503  * 
3504  * \throw If bgOfIndex or end is NULL.
3505  * \throw If input index array is not ascendingly sorted.
3506  * \throw If there is an id in [ \a bgOfIndex, \a endOfIndex ) not in [0, \c this->getNumberOfTuples).
3507  * \throw If std::distance(bgOfIndex,endOfIndex)==0.
3508  */
3509 DataArrayDouble *DataArrayDouble::accumulatePerChunck(const int *bgOfIndex, const int *endOfIndex) const
3510 {
3511   if(!bgOfIndex || !endOfIndex)
3512     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : input pointer NULL !");
3513   checkAllocated();
3514   int nbCompo=getNumberOfComponents();
3515   int nbOfTuples=getNumberOfTuples();
3516   int sz=(int)std::distance(bgOfIndex,endOfIndex);
3517   if(sz<1)
3518     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : invalid size of input index array !");
3519   sz--;
3520   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(sz,nbCompo);
3521   const int *w=bgOfIndex;
3522   if(*w<0 || *w>=nbOfTuples)
3523     throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : The first element of the input index not in [0,nbOfTuples) !");
3524   const double *srcPt=begin()+(*w)*nbCompo;
3525   double *tmp=ret->getPointer();
3526   for(int i=0;i<sz;i++,tmp+=nbCompo,w++)
3527     {
3528       std::fill(tmp,tmp+nbCompo,0.);
3529       if(w[1]>=w[0])
3530         {
3531           for(int j=w[0];j<w[1];j++,srcPt+=nbCompo)
3532             {
3533               if(j>=0 && j<nbOfTuples)
3534                 std::transform(srcPt,srcPt+nbCompo,tmp,tmp,std::plus<double>());
3535               else
3536                 {
3537                   std::ostringstream oss; oss << "DataArrayDouble::accumulatePerChunck : At rank #" << i << " the input index array points to id " << j << " should be in [0," << nbOfTuples << ") !";
3538                   throw INTERP_KERNEL::Exception(oss.str().c_str());
3539                 }
3540             }
3541         }
3542       else
3543         {
3544           std::ostringstream oss; oss << "DataArrayDouble::accumulatePerChunck : At rank #" << i << " the input index array is not in ascendingly sorted.";
3545           throw INTERP_KERNEL::Exception(oss.str().c_str());
3546         }
3547     }
3548   ret->copyStringInfoFrom(*this);
3549   return ret.retn();
3550 }
3551
3552 /*!
3553  * Converts each 2D point defined by the tuple of \a this array from the Polar to the
3554  * Cartesian coordinate system. The two components of the tuple of \a this array are 
3555  * considered to contain (1) radius and (2) angle of the point in the Polar CS.
3556  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3557  *          contains X and Y coordinates of the point in the Cartesian CS. The caller
3558  *          is to delete this array using decrRef() as it is no more needed. The array
3559  *          does not contain any textual info on components.
3560  *  \throw If \a this->getNumberOfComponents() != 2.
3561  */
3562 DataArrayDouble *DataArrayDouble::fromPolarToCart() const
3563 {
3564   checkAllocated();
3565   int nbOfComp=getNumberOfComponents();
3566   if(nbOfComp!=2)
3567     throw INTERP_KERNEL::Exception("DataArrayDouble::fromPolarToCart : must be an array with exactly 2 components !");
3568   int nbOfTuple=getNumberOfTuples();
3569   DataArrayDouble *ret=DataArrayDouble::New();
3570   ret->alloc(nbOfTuple,2);
3571   double *w=ret->getPointer();
3572   const double *wIn=getConstPointer();
3573   for(int i=0;i<nbOfTuple;i++,w+=2,wIn+=2)
3574     {
3575       w[0]=wIn[0]*cos(wIn[1]);
3576       w[1]=wIn[0]*sin(wIn[1]);
3577     }
3578   return ret;
3579 }
3580
3581 /*!
3582  * Converts each 3D point defined by the tuple of \a this array from the Cylindrical to
3583  * the Cartesian coordinate system. The three components of the tuple of \a this array 
3584  * are considered to contain (1) radius, (2) azimuth and (3) altitude of the point in
3585  * the Cylindrical CS.
3586  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3587  *          contains X, Y and Z coordinates of the point in the Cartesian CS. The info
3588  *          on the third component is copied from \a this array. The caller
3589  *          is to delete this array using decrRef() as it is no more needed. 
3590  *  \throw If \a this->getNumberOfComponents() != 3.
3591  */
3592 DataArrayDouble *DataArrayDouble::fromCylToCart() const
3593 {
3594   checkAllocated();
3595   int nbOfComp=getNumberOfComponents();
3596   if(nbOfComp!=3)
3597     throw INTERP_KERNEL::Exception("DataArrayDouble::fromCylToCart : must be an array with exactly 3 components !");
3598   int nbOfTuple=getNumberOfTuples();
3599   DataArrayDouble *ret=DataArrayDouble::New();
3600   ret->alloc(getNumberOfTuples(),3);
3601   double *w=ret->getPointer();
3602   const double *wIn=getConstPointer();
3603   for(int i=0;i<nbOfTuple;i++,w+=3,wIn+=3)
3604     {
3605       w[0]=wIn[0]*cos(wIn[1]);
3606       w[1]=wIn[0]*sin(wIn[1]);
3607       w[2]=wIn[2];
3608     }
3609   ret->setInfoOnComponent(2,getInfoOnComponent(2));
3610   return ret;
3611 }
3612
3613 /*!
3614  * Converts each 3D point defined by the tuple of \a this array from the Spherical to
3615  * the Cartesian coordinate system. The three components of the tuple of \a this array 
3616  * are considered to contain (1) radius, (2) polar angle and (3) azimuthal angle of the
3617  * point in the Cylindrical CS.
3618  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3619  *          contains X, Y and Z coordinates of the point in the Cartesian CS. The info
3620  *          on the third component is copied from \a this array. The caller
3621  *          is to delete this array using decrRef() as it is no more needed.
3622  *  \throw If \a this->getNumberOfComponents() != 3.
3623  */
3624 DataArrayDouble *DataArrayDouble::fromSpherToCart() const
3625 {
3626   checkAllocated();
3627   int nbOfComp=getNumberOfComponents();
3628   if(nbOfComp!=3)
3629     throw INTERP_KERNEL::Exception("DataArrayDouble::fromSpherToCart : must be an array with exactly 3 components !");
3630   int nbOfTuple=getNumberOfTuples();
3631   DataArrayDouble *ret=DataArrayDouble::New();
3632   ret->alloc(getNumberOfTuples(),3);
3633   double *w=ret->getPointer();
3634   const double *wIn=getConstPointer();
3635   for(int i=0;i<nbOfTuple;i++,w+=3,wIn+=3)
3636     {
3637       w[0]=wIn[0]*cos(wIn[2])*sin(wIn[1]);
3638       w[1]=wIn[0]*sin(wIn[2])*sin(wIn[1]);
3639       w[2]=wIn[0]*cos(wIn[1]);
3640     }
3641   return ret;
3642 }
3643
3644 /*!
3645  * Computes the doubly contracted product of every tensor defined by the tuple of \a this
3646  * array contating 6 components.
3647  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3648  *          is calculated from the tuple <em>(t)</em> of \a this array as follows:
3649  *          \f$ t[0]^2+t[1]^2+t[2]^2+2*t[3]^2+2*t[4]^2+2*t[5]^2\f$.
3650  *         The caller is to delete this result array using decrRef() as it is no more needed. 
3651  *  \throw If \a this->getNumberOfComponents() != 6.
3652  */
3653 DataArrayDouble *DataArrayDouble::doublyContractedProduct() const
3654 {
3655   checkAllocated();
3656   int nbOfComp=getNumberOfComponents();
3657   if(nbOfComp!=6)
3658     throw INTERP_KERNEL::Exception("DataArrayDouble::doublyContractedProduct : must be an array with exactly 6 components !");
3659   DataArrayDouble *ret=DataArrayDouble::New();
3660   int nbOfTuple=getNumberOfTuples();
3661   ret->alloc(nbOfTuple,1);
3662   const double *src=getConstPointer();
3663   double *dest=ret->getPointer();
3664   for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
3665     *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];
3666   return ret;
3667 }
3668
3669 /*!
3670  * Computes the determinant of every square matrix defined by the tuple of \a this
3671  * array, which contains either 4, 6 or 9 components. The case of 6 components
3672  * corresponds to that of the upper triangular matrix.
3673  *  \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
3674  *          is the determinant of matrix of the corresponding tuple of \a this array.
3675  *          The caller is to delete this result array using decrRef() as it is no more
3676  *          needed. 
3677  *  \throw If \a this->getNumberOfComponents() is not in [4,6,9].
3678  */
3679 DataArrayDouble *DataArrayDouble::determinant() const
3680 {
3681   checkAllocated();
3682   DataArrayDouble *ret=DataArrayDouble::New();
3683   int nbOfTuple=getNumberOfTuples();
3684   ret->alloc(nbOfTuple,1);
3685   const double *src=getConstPointer();
3686   double *dest=ret->getPointer();
3687   switch(getNumberOfComponents())
3688   {
3689     case 6:
3690       for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
3691         *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];
3692       return ret;
3693     case 4:
3694       for(int i=0;i<nbOfTuple;i++,dest++,src+=4)
3695         *dest=src[0]*src[3]-src[1]*src[2];
3696       return ret;
3697     case 9:
3698       for(int i=0;i<nbOfTuple;i++,dest++,src+=9)
3699         *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];
3700       return ret;
3701     default:
3702       ret->decrRef();
3703       throw INTERP_KERNEL::Exception("DataArrayDouble::determinant : Invalid number of components ! must be in 4,6,9 !");
3704   }
3705 }
3706
3707 /*!
3708  * Computes 3 eigenvalues of every upper triangular matrix defined by the tuple of
3709  * \a this array, which contains 6 components.
3710  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing 3
3711  *          components, whose each tuple contains the eigenvalues of the matrix of
3712  *          corresponding tuple of \a this array. 
3713  *          The caller is to delete this result array using decrRef() as it is no more
3714  *          needed. 
3715  *  \throw If \a this->getNumberOfComponents() != 6.
3716  */
3717 DataArrayDouble *DataArrayDouble::eigenValues() const
3718 {
3719   checkAllocated();
3720   int nbOfComp=getNumberOfComponents();
3721   if(nbOfComp!=6)
3722     throw INTERP_KERNEL::Exception("DataArrayDouble::eigenValues : must be an array with exactly 6 components !");
3723   DataArrayDouble *ret=DataArrayDouble::New();
3724   int nbOfTuple=getNumberOfTuples();
3725   ret->alloc(nbOfTuple,3);
3726   const double *src=getConstPointer();
3727   double *dest=ret->getPointer();
3728   for(int i=0;i<nbOfTuple;i++,dest+=3,src+=6)
3729     INTERP_KERNEL::computeEigenValues6(src,dest);
3730   return ret;
3731 }
3732
3733 /*!
3734  * Computes 3 eigenvectors of every upper triangular matrix defined by the tuple of
3735  * \a this array, which contains 6 components.
3736  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing 9
3737  *          components, whose each tuple contains 3 eigenvectors of the matrix of
3738  *          corresponding tuple of \a this array.
3739  *          The caller is to delete this result array using decrRef() as it is no more
3740  *          needed.
3741  *  \throw If \a this->getNumberOfComponents() != 6.
3742  */
3743 DataArrayDouble *DataArrayDouble::eigenVectors() const
3744 {
3745   checkAllocated();
3746   int nbOfComp=getNumberOfComponents();
3747   if(nbOfComp!=6)
3748     throw INTERP_KERNEL::Exception("DataArrayDouble::eigenVectors : must be an array with exactly 6 components !");
3749   DataArrayDouble *ret=DataArrayDouble::New();
3750   int nbOfTuple=getNumberOfTuples();
3751   ret->alloc(nbOfTuple,9);
3752   const double *src=getConstPointer();
3753   double *dest=ret->getPointer();
3754   for(int i=0;i<nbOfTuple;i++,src+=6)
3755     {
3756       double tmp[3];
3757       INTERP_KERNEL::computeEigenValues6(src,tmp);
3758       for(int j=0;j<3;j++,dest+=3)
3759         INTERP_KERNEL::computeEigenVectorForEigenValue6(src,tmp[j],1e-12,dest);
3760     }
3761   return ret;
3762 }
3763
3764 /*!
3765  * Computes the inverse matrix of every matrix defined by the tuple of \a this
3766  * array, which contains either 4, 6 or 9 components. The case of 6 components
3767  * corresponds to that of the upper triangular matrix.
3768  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3769  *          same number of components as \a this one, whose each tuple is the inverse
3770  *          matrix of the matrix of corresponding tuple of \a this array. 
3771  *          The caller is to delete this result array using decrRef() as it is no more
3772  *          needed. 
3773  *  \throw If \a this->getNumberOfComponents() is not in [4,6,9].
3774  */
3775 DataArrayDouble *DataArrayDouble::inverse() const
3776 {
3777   checkAllocated();
3778   int nbOfComp=getNumberOfComponents();
3779   if(nbOfComp!=6 && nbOfComp!=9 && nbOfComp!=4)
3780     throw INTERP_KERNEL::Exception("DataArrayDouble::inversion : must be an array with 4,6 or 9 components !");
3781   DataArrayDouble *ret=DataArrayDouble::New();
3782   int nbOfTuple=getNumberOfTuples();
3783   ret->alloc(nbOfTuple,nbOfComp);
3784   const double *src=getConstPointer();
3785   double *dest=ret->getPointer();
3786   if(nbOfComp==6)
3787     for(int i=0;i<nbOfTuple;i++,dest+=6,src+=6)
3788       {
3789         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];
3790         dest[0]=(src[1]*src[2]-src[4]*src[4])/det;
3791         dest[1]=(src[0]*src[2]-src[5]*src[5])/det;
3792         dest[2]=(src[0]*src[1]-src[3]*src[3])/det;
3793         dest[3]=(src[5]*src[4]-src[3]*src[2])/det;
3794         dest[4]=(src[5]*src[3]-src[0]*src[4])/det;
3795         dest[5]=(src[3]*src[4]-src[1]*src[5])/det;
3796       }
3797   else if(nbOfComp==4)
3798     for(int i=0;i<nbOfTuple;i++,dest+=4,src+=4)
3799       {
3800         double det=src[0]*src[3]-src[1]*src[2];
3801         dest[0]=src[3]/det;
3802         dest[1]=-src[1]/det;
3803         dest[2]=-src[2]/det;
3804         dest[3]=src[0]/det;
3805       }
3806   else
3807     for(int i=0;i<nbOfTuple;i++,dest+=9,src+=9)
3808       {
3809         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];
3810         dest[0]=(src[4]*src[8]-src[7]*src[5])/det;
3811         dest[1]=(src[7]*src[2]-src[1]*src[8])/det;
3812         dest[2]=(src[1]*src[5]-src[4]*src[2])/det;
3813         dest[3]=(src[6]*src[5]-src[3]*src[8])/det;
3814         dest[4]=(src[0]*src[8]-src[6]*src[2])/det;
3815         dest[5]=(src[2]*src[3]-src[0]*src[5])/det;
3816         dest[6]=(src[3]*src[7]-src[6]*src[4])/det;
3817         dest[7]=(src[6]*src[1]-src[0]*src[7])/det;
3818         dest[8]=(src[0]*src[4]-src[1]*src[3])/det;
3819       }
3820   return ret;
3821 }
3822
3823 /*!
3824  * Computes the trace of every matrix defined by the tuple of \a this
3825  * array, which contains either 4, 6 or 9 components. The case of 6 components
3826  * corresponds to that of the upper triangular matrix.
3827  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing 
3828  *          1 component, whose each tuple is the trace of
3829  *          the matrix of corresponding tuple of \a this array. 
3830  *          The caller is to delete this result array using decrRef() as it is no more
3831  *          needed. 
3832  *  \throw If \a this->getNumberOfComponents() is not in [4,6,9].
3833  */
3834 DataArrayDouble *DataArrayDouble::trace() const
3835 {
3836   checkAllocated();
3837   int nbOfComp=getNumberOfComponents();
3838   if(nbOfComp!=6 && nbOfComp!=9 && nbOfComp!=4)
3839     throw INTERP_KERNEL::Exception("DataArrayDouble::trace : must be an array with 4,6 or 9 components !");
3840   DataArrayDouble *ret=DataArrayDouble::New();
3841   int nbOfTuple=getNumberOfTuples();
3842   ret->alloc(nbOfTuple,1);
3843   const double *src=getConstPointer();
3844   double *dest=ret->getPointer();
3845   if(nbOfComp==6)
3846     for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
3847       *dest=src[0]+src[1]+src[2];
3848   else if(nbOfComp==4)
3849     for(int i=0;i<nbOfTuple;i++,dest++,src+=4)
3850       *dest=src[0]+src[3];
3851   else
3852     for(int i=0;i<nbOfTuple;i++,dest++,src+=9)
3853       *dest=src[0]+src[4]+src[8];
3854   return ret;
3855 }
3856
3857 /*!
3858  * Computes the stress deviator tensor of every stress tensor defined by the tuple of
3859  * \a this array, which contains 6 components.
3860  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3861  *          same number of components and tuples as \a this array.
3862  *          The caller is to delete this result array using decrRef() as it is no more
3863  *          needed.
3864  *  \throw If \a this->getNumberOfComponents() != 6.
3865  */
3866 DataArrayDouble *DataArrayDouble::deviator() const
3867 {
3868   checkAllocated();
3869   int nbOfComp=getNumberOfComponents();
3870   if(nbOfComp!=6)
3871     throw INTERP_KERNEL::Exception("DataArrayDouble::deviator : must be an array with exactly 6 components !");
3872   DataArrayDouble *ret=DataArrayDouble::New();
3873   int nbOfTuple=getNumberOfTuples();
3874   ret->alloc(nbOfTuple,6);
3875   const double *src=getConstPointer();
3876   double *dest=ret->getPointer();
3877   for(int i=0;i<nbOfTuple;i++,dest+=6,src+=6)
3878     {
3879       double tr=(src[0]+src[1]+src[2])/3.;
3880       dest[0]=src[0]-tr;
3881       dest[1]=src[1]-tr;
3882       dest[2]=src[2]-tr;
3883       dest[3]=src[3];
3884       dest[4]=src[4];
3885       dest[5]=src[5];
3886     }
3887   return ret;
3888 }
3889
3890 /*!
3891  * Computes the magnitude of every vector defined by the tuple of
3892  * \a this array.
3893  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3894  *          same number of tuples as \a this array and one component.
3895  *          The caller is to delete this result array using decrRef() as it is no more
3896  *          needed.
3897  *  \throw If \a this is not allocated.
3898  */
3899 DataArrayDouble *DataArrayDouble::magnitude() const
3900 {
3901   checkAllocated();
3902   int nbOfComp=getNumberOfComponents();
3903   DataArrayDouble *ret=DataArrayDouble::New();
3904   int nbOfTuple=getNumberOfTuples();
3905   ret->alloc(nbOfTuple,1);
3906   const double *src=getConstPointer();
3907   double *dest=ret->getPointer();
3908   for(int i=0;i<nbOfTuple;i++,dest++)
3909     {
3910       double sum=0.;
3911       for(int j=0;j<nbOfComp;j++,src++)
3912         sum+=(*src)*(*src);
3913       *dest=sqrt(sum);
3914     }
3915   return ret;
3916 }
3917
3918 /*!
3919  * Computes for each tuple the sum of number of components values in the tuple and return it.
3920  * 
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  */
3927 DataArrayDouble *DataArrayDouble::sumPerTuple() const
3928 {
3929   checkAllocated();
3930   int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
3931   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
3932   ret->alloc(nbOfTuple,1);
3933   const double *src(getConstPointer());
3934   double *dest(ret->getPointer());
3935   for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
3936     *dest=std::accumulate(src,src+nbOfComp,0.);
3937   return ret.retn();
3938 }
3939
3940 /*!
3941  * Computes the maximal value within every tuple of \a this array.
3942  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3943  *          same number of tuples as \a this array and one component.
3944  *          The caller is to delete this result array using decrRef() as it is no more
3945  *          needed.
3946  *  \throw If \a this is not allocated.
3947  *  \sa DataArrayDouble::maxPerTupleWithCompoId
3948  */
3949 DataArrayDouble *DataArrayDouble::maxPerTuple() const
3950 {
3951   checkAllocated();
3952   int nbOfComp=getNumberOfComponents();
3953   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
3954   int nbOfTuple=getNumberOfTuples();
3955   ret->alloc(nbOfTuple,1);
3956   const double *src=getConstPointer();
3957   double *dest=ret->getPointer();
3958   for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
3959     *dest=*std::max_element(src,src+nbOfComp);
3960   return ret.retn();
3961 }
3962
3963 /*!
3964  * Computes the maximal value within every tuple of \a this array and it returns the first component
3965  * id for each tuple that corresponds to the maximal value within the tuple.
3966  * 
3967  *  \param [out] compoIdOfMaxPerTuple - the new new instance of DataArrayInt containing the
3968  *          same number of tuples and only one component.
3969  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
3970  *          same number of tuples as \a this array and one component.
3971  *          The caller is to delete this result array using decrRef() as it is no more
3972  *          needed.
3973  *  \throw If \a this is not allocated.
3974  *  \sa DataArrayDouble::maxPerTuple
3975  */
3976 DataArrayDouble *DataArrayDouble::maxPerTupleWithCompoId(DataArrayInt* &compoIdOfMaxPerTuple) const
3977 {
3978   checkAllocated();
3979   int nbOfComp=getNumberOfComponents();
3980   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret0=DataArrayDouble::New();
3981   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=DataArrayInt::New();
3982   int nbOfTuple=getNumberOfTuples();
3983   ret0->alloc(nbOfTuple,1); ret1->alloc(nbOfTuple,1);
3984   const double *src=getConstPointer();
3985   double *dest=ret0->getPointer(); int *dest1=ret1->getPointer();
3986   for(int i=0;i<nbOfTuple;i++,dest++,dest1++,src+=nbOfComp)
3987     {
3988       const double *loc=std::max_element(src,src+nbOfComp);
3989       *dest=*loc;
3990       *dest1=(int)std::distance(src,loc);
3991     }
3992   compoIdOfMaxPerTuple=ret1.retn();
3993   return ret0.retn();
3994 }
3995
3996 /*!
3997  * This method returns a newly allocated DataArrayDouble instance having one component and \c this->getNumberOfTuples() * \c this->getNumberOfTuples() tuples.
3998  * \n This returned array contains the euclidian distance for each tuple in \a this. 
3999  * \n So the returned array can be seen as a dense symmetrical matrix whose diagonal elements are equal to 0.
4000  * \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)
4001  *
4002  * \warning use this method with care because it can leads to big amount of consumed memory !
4003  * 
4004  * \return A newly allocated (huge) ParaMEDMEM::DataArrayDouble instance that the caller should deal with.
4005  *
4006  * \throw If \a this is not allocated.
4007  *
4008  * \sa DataArrayDouble::buildEuclidianDistanceDenseMatrixWith
4009  */
4010 DataArrayDouble *DataArrayDouble::buildEuclidianDistanceDenseMatrix() const
4011 {
4012   checkAllocated();
4013   int nbOfComp=getNumberOfComponents();
4014   int nbOfTuples=getNumberOfTuples();
4015   const double *inData=getConstPointer();
4016   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
4017   ret->alloc(nbOfTuples*nbOfTuples,1);
4018   double *outData=ret->getPointer();
4019   for(int i=0;i<nbOfTuples;i++)
4020     {
4021       outData[i*nbOfTuples+i]=0.;
4022       for(int j=i+1;j<nbOfTuples;j++)
4023         {
4024           double dist=0.;
4025           for(int k=0;k<nbOfComp;k++)
4026             { double delta=inData[i*nbOfComp+k]-inData[j*nbOfComp+k]; dist+=delta*delta; }
4027           dist=sqrt(dist);
4028           outData[i*nbOfTuples+j]=dist;
4029           outData[j*nbOfTuples+i]=dist;
4030         }
4031     }
4032   return ret.retn();
4033 }
4034
4035 /*!
4036  * This method returns a newly allocated DataArrayDouble instance having one component and \c this->getNumberOfTuples() * \c other->getNumberOfTuples() tuples.
4037  * \n This returned array contains the euclidian distance for each tuple in \a other with each tuple in \a this. 
4038  * \n So the returned array can be seen as a dense rectangular matrix with \c other->getNumberOfTuples() rows and \c this->getNumberOfTuples() columns.
4039  * \n Output rectangular matrix is sorted along rows.
4040  * \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)
4041  *
4042  * \warning use this method with care because it can leads to big amount of consumed memory !
4043  * 
4044  * \param [in] other DataArrayDouble instance having same number of components than \a this.
4045  * \return A newly allocated (huge) ParaMEDMEM::DataArrayDouble instance that the caller should deal with.
4046  *
4047  * \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.
4048  *
4049  * \sa DataArrayDouble::buildEuclidianDistanceDenseMatrix
4050  */
4051 DataArrayDouble *DataArrayDouble::buildEuclidianDistanceDenseMatrixWith(const DataArrayDouble *other) const
4052 {
4053   if(!other)
4054     throw INTERP_KERNEL::Exception("DataArrayDouble::buildEuclidianDistanceDenseMatrixWith : input parameter is null !");
4055   checkAllocated();
4056   other->checkAllocated();
4057   int nbOfComp=getNumberOfComponents();
4058   int otherNbOfComp=other->getNumberOfComponents();
4059   if(nbOfComp!=otherNbOfComp)
4060     {
4061       std::ostringstream oss; oss << "DataArrayDouble::buildEuclidianDistanceDenseMatrixWith : this nb of compo=" << nbOfComp << " and other nb of compo=" << otherNbOfComp << ". It should match !";
4062       throw INTERP_KERNEL::Exception(oss.str().c_str());
4063     }
4064   int nbOfTuples=getNumberOfTuples();
4065   int otherNbOfTuples=other->getNumberOfTuples();
4066   const double *inData=getConstPointer();
4067   const double *inDataOther=other->getConstPointer();
4068   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
4069   ret->alloc(otherNbOfTuples*nbOfTuples,1);
4070   double *outData=ret->getPointer();
4071   for(int i=0;i<otherNbOfTuples;i++,inDataOther+=nbOfComp)
4072     {
4073       for(int j=0;j<nbOfTuples;j++)
4074         {
4075           double dist=0.;
4076           for(int k=0;k<nbOfComp;k++)
4077             { double delta=inDataOther[k]-inData[j*nbOfComp+k]; dist+=delta*delta; }
4078           dist=sqrt(dist);
4079           outData[i*nbOfTuples+j]=dist;
4080         }
4081     }
4082   return ret.retn();
4083 }
4084
4085 /*!
4086  * Sorts value within every tuple of \a this array.
4087  *  \param [in] asc - if \a true, the values are sorted in ascending order, else,
4088  *              in descending order.
4089  *  \throw If \a this is not allocated.
4090  */
4091 void DataArrayDouble::sortPerTuple(bool asc)
4092 {
4093   checkAllocated();
4094   double *pt=getPointer();
4095   int nbOfTuple=getNumberOfTuples();
4096   int nbOfComp=getNumberOfComponents();
4097   if(asc)
4098     for(int i=0;i<nbOfTuple;i++,pt+=nbOfComp)
4099       std::sort(pt,pt+nbOfComp);
4100   else
4101     for(int i=0;i<nbOfTuple;i++,pt+=nbOfComp)
4102       std::sort(pt,pt+nbOfComp,std::greater<double>());
4103   declareAsNew();
4104 }
4105
4106 /*!
4107  * Converts every value of \a this array to its absolute value.
4108  * \b WARNING this method is non const. If a new DataArrayDouble instance should be built containing the result of abs DataArrayDouble::computeAbs
4109  * should be called instead.
4110  *
4111  * \throw If \a this is not allocated.
4112  * \sa DataArrayDouble::computeAbs
4113  */
4114 void DataArrayDouble::abs()
4115 {
4116   checkAllocated();
4117   double *ptr(getPointer());
4118   std::size_t nbOfElems(getNbOfElems());
4119   std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun<double,double>(fabs));
4120   declareAsNew();
4121 }
4122
4123 /*!
4124  * This method builds a new instance of \a this object containing the result of std::abs applied of all elements in \a this.
4125  * This method is a const method (that do not change any values in \a this) contrary to  DataArrayDouble::abs method.
4126  *
4127  * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4128  *         same number of tuples and component as \a this array.
4129  *         The caller is to delete this result array using decrRef() as it is no more
4130  *         needed.
4131  * \throw If \a this is not allocated.
4132  * \sa DataArrayDouble::abs
4133  */
4134 DataArrayDouble *DataArrayDouble::computeAbs() const
4135 {
4136   checkAllocated();
4137   DataArrayDouble *newArr(DataArrayDouble::New());
4138   int nbOfTuples(getNumberOfTuples());
4139   int nbOfComp(getNumberOfComponents());
4140   newArr->alloc(nbOfTuples,nbOfComp);
4141   std::transform(begin(),end(),newArr->getPointer(),std::ptr_fun<double,double>(fabs));
4142   newArr->copyStringInfoFrom(*this);
4143   return newArr;
4144 }
4145
4146 /*!
4147  * Apply a linear function to a given component of \a this array, so that
4148  * an array element <em>(x)</em> becomes \f$ a * x + b \f$.
4149  *  \param [in] a - the first coefficient of the function.
4150  *  \param [in] b - the second coefficient of the function.
4151  *  \param [in] compoId - the index of component to modify.
4152  *  \throw If \a this is not allocated, or \a compoId is not in [0,\c this->getNumberOfComponents() ).
4153  */
4154 void DataArrayDouble::applyLin(double a, double b, int compoId)
4155 {
4156   checkAllocated();
4157   double *ptr(getPointer()+compoId);
4158   int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
4159   if(compoId<0 || compoId>=nbOfComp)
4160     {
4161       std::ostringstream oss; oss << "DataArrayDouble::applyLin : The compoId requested (" << compoId << ") is not valid ! Must be in [0," << nbOfComp << ") !";
4162       throw INTERP_KERNEL::Exception(oss.str().c_str());
4163     }
4164   for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
4165     *ptr=a*(*ptr)+b;
4166   declareAsNew();
4167 }
4168
4169 /*!
4170  * Apply a linear function to all elements of \a this array, so that
4171  * an element _x_ becomes \f$ a * x + b \f$.
4172  *  \param [in] a - the first coefficient of the function.
4173  *  \param [in] b - the second coefficient of the function.
4174  *  \throw If \a this is not allocated.
4175  */
4176 void DataArrayDouble::applyLin(double a, double b)
4177 {
4178   checkAllocated();
4179   double *ptr=getPointer();
4180   std::size_t nbOfElems=getNbOfElems();
4181   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4182     *ptr=a*(*ptr)+b;
4183   declareAsNew();
4184 }
4185
4186 /*!
4187  * Modify all elements of \a this array, so that
4188  * an element _x_ becomes \f$ numerator / x \f$.
4189  *  \warning If an exception is thrown because of presence of 0.0 element in \a this 
4190  *           array, all elements processed before detection of the zero element remain
4191  *           modified.
4192  *  \param [in] numerator - the numerator used to modify array elements.
4193  *  \throw If \a this is not allocated.
4194  *  \throw If there is an element equal to 0.0 in \a this array.
4195  */
4196 void DataArrayDouble::applyInv(double numerator)
4197 {
4198   checkAllocated();
4199   double *ptr=getPointer();
4200   std::size_t nbOfElems=getNbOfElems();
4201   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4202     {
4203       if(std::abs(*ptr)>std::numeric_limits<double>::min())
4204         {
4205           *ptr=numerator/(*ptr);
4206         }
4207       else
4208         {
4209           std::ostringstream oss; oss << "DataArrayDouble::applyInv : presence of null value in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
4210           oss << " !";
4211           throw INTERP_KERNEL::Exception(oss.str().c_str());
4212         }
4213     }
4214   declareAsNew();
4215 }
4216
4217 /*!
4218  * Returns a full copy of \a this array except that sign of all elements is reversed.
4219  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4220  *          same number of tuples and component as \a this array.
4221  *          The caller is to delete this result array using decrRef() as it is no more
4222  *          needed.
4223  *  \throw If \a this is not allocated.
4224  */
4225 DataArrayDouble *DataArrayDouble::negate() const
4226 {
4227   checkAllocated();
4228   DataArrayDouble *newArr=DataArrayDouble::New();
4229   int nbOfTuples=getNumberOfTuples();
4230   int nbOfComp=getNumberOfComponents();
4231   newArr->alloc(nbOfTuples,nbOfComp);
4232   const double *cptr=getConstPointer();
4233   std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate<double>());
4234   newArr->copyStringInfoFrom(*this);
4235   return newArr;
4236 }
4237
4238 /*!
4239  * Modify all elements of \a this array, so that
4240  * an element _x_ becomes <em> val ^ x </em>. Contrary to DataArrayInt::applyPow
4241  * all values in \a this have to be >= 0 if val is \b not integer.
4242  *  \param [in] val - the value used to apply pow on all array elements.
4243  *  \throw If \a this is not allocated.
4244  *  \warning If an exception is thrown because of presence of 0 element in \a this 
4245  *           array and \a val is \b not integer, all elements processed before detection of the zero element remain
4246  *           modified.
4247  */
4248 void DataArrayDouble::applyPow(double val)
4249 {
4250   checkAllocated();
4251   double *ptr=getPointer();
4252   std::size_t nbOfElems=getNbOfElems();
4253   int val2=(int)val;
4254   bool isInt=((double)val2)==val;
4255   if(!isInt)
4256     {
4257       for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4258         {
4259           if(*ptr>=0)
4260             *ptr=pow(*ptr,val);
4261           else
4262             {
4263               std::ostringstream oss; oss << "DataArrayDouble::applyPow (double) : At elem # " << i << " value is " << *ptr << " ! must be >=0. !";
4264               throw INTERP_KERNEL::Exception(oss.str().c_str());
4265             }
4266         }
4267     }
4268   else
4269     {
4270       for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4271         *ptr=pow(*ptr,val2);
4272     }
4273   declareAsNew();
4274 }
4275
4276 /*!
4277  * Modify all elements of \a this array, so that
4278  * an element _x_ becomes \f$ val ^ x \f$.
4279  *  \param [in] val - the value used to apply pow on all array elements.
4280  *  \throw If \a this is not allocated.
4281  *  \throw If \a val < 0.
4282  *  \warning If an exception is thrown because of presence of 0 element in \a this 
4283  *           array, all elements processed before detection of the zero element remain
4284  *           modified.
4285  */
4286 void DataArrayDouble::applyRPow(double val)
4287 {
4288   checkAllocated();
4289   if(val<0.)
4290     throw INTERP_KERNEL::Exception("DataArrayDouble::applyRPow : the input value has to be >= 0 !");
4291   double *ptr=getPointer();
4292   std::size_t nbOfElems=getNbOfElems();
4293   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
4294     *ptr=pow(val,*ptr);
4295   declareAsNew();
4296 }
4297
4298 /*!
4299  * Returns a new DataArrayDouble created from \a this one by applying \a
4300  * FunctionToEvaluate to every tuple of \a this array. Textual data is not copied.
4301  * For more info see \ref MEDCouplingArrayApplyFunc
4302  *  \param [in] nbOfComp - number of components in the result array.
4303  *  \param [in] func - the \a FunctionToEvaluate declared as 
4304  *              \c bool (*\a func)(\c const \c double *\a pos, \c double *\a res), 
4305  *              where \a pos points to the first component of a tuple of \a this array
4306  *              and \a res points to the first component of a tuple of the result array.
4307  *              Note that length (number of components) of \a pos can differ from
4308  *              that of \a res.
4309  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4310  *          same number of tuples as \a this array.
4311  *          The caller is to delete this result array using decrRef() as it is no more
4312  *          needed.
4313  *  \throw If \a this is not allocated.
4314  *  \throw If \a func returns \a false.
4315  */
4316 DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, FunctionToEvaluate func) const
4317 {
4318   checkAllocated();
4319   DataArrayDouble *newArr=DataArrayDouble::New();
4320   int nbOfTuples=getNumberOfTuples();
4321   int oldNbOfComp=getNumberOfComponents();
4322   newArr->alloc(nbOfTuples,nbOfComp);
4323   const double *ptr=getConstPointer();
4324   double *ptrToFill=newArr->getPointer();
4325   for(int i=0;i<nbOfTuples;i++)
4326     {
4327       if(!func(ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp))
4328         {
4329           std::ostringstream oss; oss << "For tuple # " << i << " with value (";
4330           std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
4331           oss << ") : Evaluation of function failed !";
4332           newArr->decrRef();
4333           throw INTERP_KERNEL::Exception(oss.str().c_str());
4334         }
4335     }
4336   return newArr;
4337 }
4338
4339 /*!
4340  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4341  * tuple of \a this array. Textual data is not copied.
4342  * For more info see \ref MEDCouplingArrayApplyFunc1.
4343  *  \param [in] nbOfComp - number of components in the result array.
4344  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4345  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4346  *  \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
4347  *              If false the computation is carried on without any notification. When false the evaluation is a little faster.
4348  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4349  *          same number of tuples as \a this array and \a nbOfComp components.
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(int nbOfComp, const std::string& func, bool isSafe) const
4356 {
4357   INTERP_KERNEL::ExprParser expr(func);
4358   expr.parse();
4359   std::set<std::string> vars;
4360   expr.getTrueSetOfVars(vars);
4361   std::vector<std::string> varsV(vars.begin(),vars.end());
4362   return applyFunc3(nbOfComp,varsV,func,isSafe);
4363 }
4364
4365 /*!
4366  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4367  * tuple of \a this array. Textual data is not copied. This method works by tuples (whatever its size).
4368  * If \a this is a one component array, call applyFuncOnThis instead that performs the same work faster.
4369  *
4370  * For more info see \ref MEDCouplingArrayApplyFunc0.
4371  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4372  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4373  *  \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
4374  *                       If false the computation is carried on without any notification. When false the evaluation is a little faster.
4375  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4376  *          same number of tuples and components as \a this array.
4377  *          The caller is to delete this result array using decrRef() as it is no more
4378  *          needed.
4379  *  \sa applyFuncOnThis
4380  *  \throw If \a this is not allocated.
4381  *  \throw If computing \a func fails.
4382  */
4383 DataArrayDouble *DataArrayDouble::applyFunc(const std::string& func, bool isSafe) const
4384 {
4385   int nbOfComp(getNumberOfComponents());
4386   if(nbOfComp<=0)
4387     throw INTERP_KERNEL::Exception("DataArrayDouble::applyFunc : output number of component must be > 0 !");
4388   checkAllocated();
4389   int nbOfTuples(getNumberOfTuples());
4390   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> newArr(DataArrayDouble::New());
4391   newArr->alloc(nbOfTuples,nbOfComp);
4392   INTERP_KERNEL::ExprParser expr(func);
4393   expr.parse();
4394   std::set<std::string> vars;
4395   expr.getTrueSetOfVars(vars);
4396   if((int)vars.size()>1)
4397     {
4398       std::ostringstream oss; oss << "DataArrayDouble::applyFunc : this method works only with at most one var func expression ! If you need to map comps on variables please use applyFunc2 or applyFunc3 instead ! Vars in expr are : ";
4399       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
4400       throw INTERP_KERNEL::Exception(oss.str().c_str());
4401     }
4402   if(vars.empty())
4403     {
4404       expr.prepareFastEvaluator();
4405       newArr->rearrange(1);
4406       newArr->fillWithValue(expr.evaluateDouble());
4407       newArr->rearrange(nbOfComp);
4408       return newArr.retn();
4409     }
4410   std::vector<std::string> vars2(vars.begin(),vars.end());
4411   double buff,*ptrToFill(newArr->getPointer());
4412   const double *ptr(begin());
4413   std::vector<double> stck;
4414   expr.prepareExprEvaluationDouble(vars2,1,1,0,&buff,&buff+1);
4415   expr.prepareFastEvaluator();
4416   if(!isSafe)
4417     {
4418       for(int i=0;i<nbOfTuples;i++)
4419         {
4420           for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
4421             {
4422               buff=*ptr;
4423               expr.evaluateDoubleInternal(stck);
4424               *ptrToFill=stck.back();
4425               stck.pop_back();
4426             }
4427         }
4428     }
4429   else
4430     {
4431       for(int i=0;i<nbOfTuples;i++)
4432         {
4433           for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
4434             {
4435               buff=*ptr;
4436               try
4437               {
4438                   expr.evaluateDoubleInternalSafe(stck);
4439               }
4440               catch(INTERP_KERNEL::Exception& e)
4441               {
4442                   std::ostringstream oss; oss << "For tuple # " << i << " component # " << iComp << " with value (";
4443                   oss << buff;
4444                   oss << ") : Evaluation of function failed !" << e.what();
4445                   throw INTERP_KERNEL::Exception(oss.str().c_str());
4446               }
4447               *ptrToFill=stck.back();
4448               stck.pop_back();
4449             }
4450         }
4451     }
4452   return newArr.retn();
4453 }
4454
4455 /*!
4456  * This method is a non const method that modify the array in \a this.
4457  * This method only works on one component array. It means that function \a func must
4458  * contain at most one variable.
4459  * This method is a specialization of applyFunc method with one parameter on one component array.
4460  *
4461  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4462  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4463  *  \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
4464  *              If false the computation is carried on without any notification. When false the evaluation is a little faster.
4465  *
4466  * \sa applyFunc
4467  */
4468 void DataArrayDouble::applyFuncOnThis(const std::string& func, bool isSafe)
4469 {
4470   int nbOfComp(getNumberOfComponents());
4471   if(nbOfComp<=0)
4472     throw INTERP_KERNEL::Exception("DataArrayDouble::applyFuncOnThis : output number of component must be > 0 !");
4473   checkAllocated();
4474   int nbOfTuples(getNumberOfTuples());
4475   INTERP_KERNEL::ExprParser expr(func);
4476   expr.parse();
4477   std::set<std::string> vars;
4478   expr.getTrueSetOfVars(vars);
4479   if((int)vars.size()>1)
4480     {
4481       std::ostringstream oss; oss << "DataArrayDouble::applyFuncOnThis : this method works only with at most one var func expression ! If you need to map comps on variables please use applyFunc2 or applyFunc3 instead ! Vars in expr are : ";
4482       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
4483       throw INTERP_KERNEL::Exception(oss.str().c_str());
4484     }
4485   if(vars.empty())
4486     {
4487       expr.prepareFastEvaluator();
4488       std::vector<std::string> compInfo(getInfoOnComponents());
4489       rearrange(1);
4490       fillWithValue(expr.evaluateDouble());
4491       rearrange(nbOfComp);
4492       setInfoOnComponents(compInfo);
4493       return ;
4494     }
4495   std::vector<std::string> vars2(vars.begin(),vars.end());
4496   double buff,*ptrToFill(getPointer());
4497   const double *ptr(begin());
4498   std::vector<double> stck;
4499   expr.prepareExprEvaluationDouble(vars2,1,1,0,&buff,&buff+1);
4500   expr.prepareFastEvaluator();
4501   if(!isSafe)
4502     {
4503       for(int i=0;i<nbOfTuples;i++)
4504         {
4505           for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
4506             {
4507               buff=*ptr;
4508               expr.evaluateDoubleInternal(stck);
4509               *ptrToFill=stck.back();
4510               stck.pop_back();
4511             }
4512         }
4513     }
4514   else
4515     {
4516       for(int i=0;i<nbOfTuples;i++)
4517         {
4518           for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
4519             {
4520               buff=*ptr;
4521               try
4522               {
4523                   expr.evaluateDoubleInternalSafe(stck);
4524               }
4525               catch(INTERP_KERNEL::Exception& e)
4526               {
4527                   std::ostringstream oss; oss << "For tuple # " << i << " component # " << iComp << " with value (";
4528                   oss << buff;
4529                   oss << ") : Evaluation of function failed !" << e.what();
4530                   throw INTERP_KERNEL::Exception(oss.str().c_str());
4531               }
4532               *ptrToFill=stck.back();
4533               stck.pop_back();
4534             }
4535         }
4536     }
4537 }
4538
4539 /*!
4540  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4541  * tuple of \a this array. Textual data is not copied.
4542  * For more info see \ref MEDCouplingArrayApplyFunc2.
4543  *  \param [in] nbOfComp - number of components in the result array.
4544  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4545  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4546  *  \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
4547  *              If false the computation is carried on without any notification. When false the evaluation is a little faster.
4548  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4549  *          same number of tuples as \a this array.
4550  *          The caller is to delete this result array using decrRef() as it is no more
4551  *          needed.
4552  *  \throw If \a this is not allocated.
4553  *  \throw If \a func contains vars that are not in \a this->getInfoOnComponent().
4554  *  \throw If computing \a func fails.
4555  */
4556 DataArrayDouble *DataArrayDouble::applyFunc2(int nbOfComp, const std::string& func, bool isSafe) const
4557 {
4558   return applyFunc3(nbOfComp,getVarsOnComponent(),func,isSafe);
4559 }
4560
4561 /*!
4562  * Returns a new DataArrayDouble created from \a this one by applying a function to every
4563  * tuple of \a this array. Textual data is not copied.
4564  * For more info see \ref MEDCouplingArrayApplyFunc3.
4565  *  \param [in] nbOfComp - number of components in the result array.
4566  *  \param [in] varsOrder - sequence of vars defining their order.
4567  *  \param [in] func - the expression defining how to transform a tuple of \a this array.
4568  *              Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
4569  *  \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
4570  *              If false the computation is carried on without any notification. When false the evaluation is a little faster.
4571  *  \return DataArrayDouble * - the new instance of DataArrayDouble containing the
4572  *          same number of tuples as \a this array.
4573  *          The caller is to delete this result array using decrRef() as it is no more
4574  *          needed.
4575  *  \throw If \a this is not allocated.
4576  *  \throw If \a func contains vars not in \a varsOrder.
4577  *  \throw If computing \a func fails.
4578  */
4579 DataArrayDouble *DataArrayDouble::applyFunc3(int nbOfComp, const std::vector<std::string>& varsOrder, const std::string& func, bool isSafe) const
4580 {
4581   if(nbOfComp<=0)
4582     throw INTERP_KERNEL::Exception("DataArrayDouble::applyFunc3 : output number of component must be > 0 !");
4583   std::vector<std::string> varsOrder2(varsOrder);
4584   int oldNbOfComp(getNumberOfComponents());
4585   for(int i=(int)varsOrder.size();i<oldNbOfComp;i++)
4586     varsOrder2.push_back(std::string());
4587   checkAllocated();
4588   int nbOfTuples(getNumberOfTuples());
4589   INTERP_KERNEL::ExprParser expr(func);
4590   expr.parse();
4591   std::set<std::string> vars;
4592   expr.getTrueSetOfVars(vars);
4593   if((int)vars.size()>oldNbOfComp)
4594     {
4595       std::ostringstream oss; oss << "The field has " << oldNbOfComp << " components and there are ";
4596       oss << vars.size() << " variables : ";
4597       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
4598       throw INTERP_KERNEL::Exception(oss.str().c_str());
4599     }
4600   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> newArr(DataArrayDouble::New());
4601   newArr->alloc(nbOfTuples,nbOfComp);
4602   INTERP_KERNEL::AutoPtr<double> buff(new double[oldNbOfComp]);
4603   double *buffPtr(buff),*ptrToFill;
4604   std::vector<double> stck;
4605   for(int iComp=0;iComp<nbOfComp;iComp++)
4606     {
4607       expr.prepareExprEvaluationDouble(varsOrder2,oldNbOfComp,nbOfComp,iComp,buffPtr,buffPtr+oldNbOfComp);
4608       expr.prepareFastEvaluator();
4609       const double *ptr(getConstPointer());
4610       ptrToFill=newArr->getPointer()+iComp;
4611       if(!isSafe)
4612         {
4613           for(int i=0;i<nbOfTuples;i++,ptrToFill+=nbOfComp,ptr+=oldNbOfComp)
4614             {
4615               std::copy(ptr,ptr+oldNbOfComp,buffPtr);
4616               expr.evaluateDoubleInternal(stck);
4617               *ptrToFill=stck.back();
4618               stck.pop_back();
4619             }
4620         }
4621       else
4622         {
4623           for(int i=0;i<nbOfTuples;i++,ptrToFill+=nbOfComp,ptr+=oldNbOfComp)
4624             {
4625               std::copy(ptr,ptr+oldNbOfComp,buffPtr);
4626               try
4627               {
4628                   expr.evaluateDoubleInternalSafe(stck);
4629                   *ptrToFill=stck.back();
4630                   stck.pop_back();
4631               }
4632               catch(INTERP_KERNEL::Exception& e)
4633               {
4634                   std::ostringstream oss; oss << "For tuple # " << i << " with value (";
4635                   std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
4636                   oss << ") : Evaluation of function failed !" << e.what();
4637                   throw INTERP_KERNEL::Exception(oss.str().c_str());
4638               }
4639             }
4640         }
4641     }
4642   return newArr.retn();
4643 }
4644
4645 void DataArrayDouble::applyFuncFast32(const std::string& func)
4646 {
4647   checkAllocated();
4648   INTERP_KERNEL::ExprParser expr(func);
4649   expr.parse();
4650   char *funcStr=expr.compileX86();
4651   MYFUNCPTR funcPtr;
4652   *((void **)&funcPtr)=funcStr;//he he...
4653   //
4654   double *ptr=getPointer();
4655   int nbOfComp=getNumberOfComponents();
4656   int nbOfTuples=getNumberOfTuples();
4657   int nbOfElems=nbOfTuples*nbOfComp;
4658   for(int i=0;i<nbOfElems;i++,ptr++)
4659     *ptr=funcPtr(*ptr);
4660   declareAsNew();
4661 }
4662
4663 void DataArrayDouble::applyFuncFast64(const std::string& func)
4664 {
4665   checkAllocated();
4666   INTERP_KERNEL::ExprParser expr(func);
4667   expr.parse();
4668   char *funcStr=expr.compileX86_64();
4669   MYFUNCPTR funcPtr;
4670   *((void **)&funcPtr)=funcStr;//he he...
4671   //
4672   double *ptr=getPointer();
4673   int nbOfComp=getNumberOfComponents();
4674   int nbOfTuples=getNumberOfTuples();
4675   int nbOfElems=nbOfTuples*nbOfComp;
4676   for(int i=0;i<nbOfElems;i++,ptr++)
4677     *ptr=funcPtr(*ptr);
4678   declareAsNew();
4679 }
4680
4681 DataArrayDoubleIterator *DataArrayDouble::iterator()
4682 {
4683   return new DataArrayDoubleIterator(this);
4684 }
4685
4686 /*!
4687  * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
4688  * array whose values are within a given range. Textual data is not copied.
4689  *  \param [in] vmin - a lowest acceptable value (included).
4690  *  \param [in] vmax - a greatest acceptable value (included).
4691  *  \return DataArrayInt * - the new instance of DataArrayInt.
4692  *          The caller is to delete this result array using decrRef() as it is no more
4693  *          needed.
4694  *  \throw If \a this->getNumberOfComponents() != 1.
4695  *
4696  *  \sa DataArrayDouble::getIdsNotInRange
4697  *
4698  *  \if ENABLE_EXAMPLES
4699  *  \ref cpp_mcdataarraydouble_getidsinrange "Here is a C++ example".<br>
4700  *  \ref py_mcdataarraydouble_getidsinrange "Here is a Python example".
4701  *  \endif
4702  */
4703 DataArrayInt *DataArrayDouble::getIdsInRange(double vmin, double vmax) const
4704 {
4705   checkAllocated();
4706   if(getNumberOfComponents()!=1)
4707     throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsInRange : this must have exactly one component !");
4708   const double *cptr(begin());
4709   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
4710   int nbOfTuples(getNumberOfTuples());
4711   for(int i=0;i<nbOfTuples;i++,cptr++)
4712     if(*cptr>=vmin && *cptr<=vmax)
4713       ret->pushBackSilent(i);
4714   return ret.retn();
4715 }
4716
4717 /*!
4718  * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
4719  * array whose values are not within a given range. Textual data is not copied.
4720  *  \param [in] vmin - a lowest not acceptable value (excluded).
4721  *  \param [in] vmax - a greatest not acceptable value (excluded).
4722  *  \return DataArrayInt * - the new instance of DataArrayInt.
4723  *          The caller is to delete this result array using decrRef() as it is no more
4724  *          needed.
4725  *  \throw If \a this->getNumberOfComponents() != 1.
4726  *
4727  *  \sa DataArrayDouble::getIdsInRange
4728  */
4729 DataArrayInt *DataArrayDouble::getIdsNotInRange(double vmin, double vmax) const
4730 {
4731   checkAllocated();
4732   if(getNumberOfComponents()!=1)
4733     throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsNotInRange : this must have exactly one component !");
4734   const double *cptr(begin());
4735   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
4736   int nbOfTuples(getNumberOfTuples());
4737   for(int i=0;i<nbOfTuples;i++,cptr++)
4738     if(*cptr<vmin || *cptr>vmax)
4739       ret->pushBackSilent(i);
4740   return ret.retn();
4741 }
4742
4743 /*!
4744  * Returns a new DataArrayDouble by concatenating two given arrays, so that (1) the number
4745  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
4746  * the number of component in the result array is same as that of each of given arrays.
4747  * Info on components is copied from the first of the given arrays. Number of components
4748  * in the given arrays must be  the same.
4749  *  \param [in] a1 - an array to include in the result array.
4750  *  \param [in] a2 - another array to include in the result array.
4751  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4752  *          The caller is to delete this result array using decrRef() as it is no more
4753  *          needed.
4754  *  \throw If both \a a1 and \a a2 are NULL.
4755  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents().
4756  */
4757 DataArrayDouble *DataArrayDouble::Aggregate(const DataArrayDouble *a1, const DataArrayDouble *a2)
4758 {
4759   std::vector<const DataArrayDouble *> tmp(2);
4760   tmp[0]=a1; tmp[1]=a2;
4761   return Aggregate(tmp);
4762 }
4763
4764 /*!
4765  * Returns a new DataArrayDouble by concatenating all given arrays, so that (1) the number
4766  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
4767  * the number of component in the result array is same as that of each of given arrays.
4768  * Info on components is copied from the first of the given arrays. Number of components
4769  * in the given arrays must be  the same.
4770  * If the number of non null of elements in \a arr is equal to one the returned object is a copy of it
4771  * not the object itself.
4772  *  \param [in] arr - a sequence of arrays to include in the result array.
4773  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4774  *          The caller is to delete this result array using decrRef() as it is no more
4775  *          needed.
4776  *  \throw If all arrays within \a arr are NULL.
4777  *  \throw If getNumberOfComponents() of arrays within \a arr.
4778  */
4779 DataArrayDouble *DataArrayDouble::Aggregate(const std::vector<const DataArrayDouble *>& arr)
4780 {
4781   std::vector<const DataArrayDouble *> a;
4782   for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
4783     if(*it4)
4784       a.push_back(*it4);
4785   if(a.empty())
4786     throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : input list must contain at least one NON EMPTY DataArrayDouble !");
4787   std::vector<const DataArrayDouble *>::const_iterator it=a.begin();
4788   int nbOfComp=(*it)->getNumberOfComponents();
4789   int nbt=(*it++)->getNumberOfTuples();
4790   for(int i=1;it!=a.end();it++,i++)
4791     {
4792       if((*it)->getNumberOfComponents()!=nbOfComp)
4793         throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : Nb of components mismatch for array aggregation !");
4794       nbt+=(*it)->getNumberOfTuples();
4795     }
4796   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
4797   ret->alloc(nbt,nbOfComp);
4798   double *pt=ret->getPointer();
4799   for(it=a.begin();it!=a.end();it++)
4800     pt=std::copy((*it)->getConstPointer(),(*it)->getConstPointer()+(*it)->getNbOfElems(),pt);
4801   ret->copyStringInfoFrom(*(a[0]));
4802   return ret.retn();
4803 }
4804
4805 /*!
4806  * Returns a new DataArrayDouble by aggregating two given arrays, so that (1) the number
4807  * of components in the result array is a sum of the number of components of given arrays
4808  * and (2) the number of tuples in the result array is same as that of each of given
4809  * arrays. In other words the i-th tuple of result array includes all components of
4810  * i-th tuples of all given arrays.
4811  * Number of tuples in the given arrays must be  the same.
4812  *  \param [in] a1 - an array to include in the result array.
4813  *  \param [in] a2 - another array to include in the result array.
4814  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4815  *          The caller is to delete this result array using decrRef() as it is no more
4816  *          needed.
4817  *  \throw If both \a a1 and \a a2 are NULL.
4818  *  \throw If any given array is not allocated.
4819  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4820  */
4821 DataArrayDouble *DataArrayDouble::Meld(const DataArrayDouble *a1, const DataArrayDouble *a2)
4822 {
4823   std::vector<const DataArrayDouble *> arr(2);
4824   arr[0]=a1; arr[1]=a2;
4825   return Meld(arr);
4826 }
4827
4828 /*!
4829  * Returns a new DataArrayDouble by aggregating all given arrays, so that (1) the number
4830  * of components in the result array is a sum of the number of components of given arrays
4831  * and (2) the number of tuples in the result array is same as that of each of given
4832  * arrays. In other words the i-th tuple of result array includes all components of
4833  * i-th tuples of all given arrays.
4834  * Number of tuples in the given arrays must be  the same.
4835  *  \param [in] arr - a sequence of arrays to include in the result array.
4836  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4837  *          The caller is to delete this result array using decrRef() as it is no more
4838  *          needed.
4839  *  \throw If all arrays within \a arr are NULL.
4840  *  \throw If any given array is not allocated.
4841  *  \throw If getNumberOfTuples() of arrays within \a arr is different.
4842  */
4843 DataArrayDouble *DataArrayDouble::Meld(const std::vector<const DataArrayDouble *>& arr)
4844 {
4845   std::vector<const DataArrayDouble *> a;
4846   for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
4847     if(*it4)
4848       a.push_back(*it4);
4849   if(a.empty())
4850     throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : input list must contain at least one NON EMPTY DataArrayDouble !");
4851   std::vector<const DataArrayDouble *>::const_iterator it;
4852   for(it=a.begin();it!=a.end();it++)
4853     (*it)->checkAllocated();
4854   it=a.begin();
4855   int nbOfTuples=(*it)->getNumberOfTuples();
4856   std::vector<int> nbc(a.size());
4857   std::vector<const double *> pts(a.size());
4858   nbc[0]=(*it)->getNumberOfComponents();
4859   pts[0]=(*it++)->getConstPointer();
4860   for(int i=1;it!=a.end();it++,i++)
4861     {
4862       if(nbOfTuples!=(*it)->getNumberOfTuples())
4863         throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : mismatch of number of tuples !");
4864       nbc[i]=(*it)->getNumberOfComponents();
4865       pts[i]=(*it)->getConstPointer();
4866     }
4867   int totalNbOfComp=std::accumulate(nbc.begin(),nbc.end(),0);
4868   DataArrayDouble *ret=DataArrayDouble::New();
4869   ret->alloc(nbOfTuples,totalNbOfComp);
4870   double *retPtr=ret->getPointer();
4871   for(int i=0;i<nbOfTuples;i++)
4872     for(int j=0;j<(int)a.size();j++)
4873       {
4874         retPtr=std::copy(pts[j],pts[j]+nbc[j],retPtr);
4875         pts[j]+=nbc[j];
4876       }
4877   int k=0;
4878   for(int i=0;i<(int)a.size();i++)
4879     for(int j=0;j<nbc[i];j++,k++)
4880       ret->setInfoOnComponent(k,a[i]->getInfoOnComponent(j));
4881   return ret;
4882 }
4883
4884 /*!
4885  * Returns a new DataArrayDouble containing a dot product of two given arrays, so that
4886  * the i-th tuple of the result array is a sum of products of j-th components of i-th
4887  * tuples of given arrays (\f$ a_i = \sum_{j=1}^n a1_j * a2_j \f$).
4888  * Info on components and name is copied from the first of the given arrays.
4889  * Number of tuples and components in the given arrays must be the same.
4890  *  \param [in] a1 - a given array.
4891  *  \param [in] a2 - another given array.
4892  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4893  *          The caller is to delete this result array using decrRef() as it is no more
4894  *          needed.
4895  *  \throw If either \a a1 or \a a2 is NULL.
4896  *  \throw If any given array is not allocated.
4897  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4898  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
4899  */
4900 DataArrayDouble *DataArrayDouble::Dot(const DataArrayDouble *a1, const DataArrayDouble *a2)
4901 {
4902   if(!a1 || !a2)
4903     throw INTERP_KERNEL::Exception("DataArrayDouble::Dot : input DataArrayDouble instance is NULL !");
4904   a1->checkAllocated();
4905   a2->checkAllocated();
4906   int nbOfComp=a1->getNumberOfComponents();
4907   if(nbOfComp!=a2->getNumberOfComponents())
4908     throw INTERP_KERNEL::Exception("Nb of components mismatch for array Dot !");
4909   int nbOfTuple=a1->getNumberOfTuples();
4910   if(nbOfTuple!=a2->getNumberOfTuples())
4911     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Dot !");
4912   DataArrayDouble *ret=DataArrayDouble::New();
4913   ret->alloc(nbOfTuple,1);
4914   double *retPtr=ret->getPointer();
4915   const double *a1Ptr=a1->getConstPointer();
4916   const double *a2Ptr=a2->getConstPointer();
4917   for(int i=0;i<nbOfTuple;i++)
4918     {
4919       double sum=0.;
4920       for(int j=0;j<nbOfComp;j++)
4921         sum+=a1Ptr[i*nbOfComp+j]*a2Ptr[i*nbOfComp+j];
4922       retPtr[i]=sum;
4923     }
4924   ret->setInfoOnComponent(0,a1->getInfoOnComponent(0));
4925   ret->setName(a1->getName());
4926   return ret;
4927 }
4928
4929 /*!
4930  * Returns a new DataArrayDouble containing a cross product of two given arrays, so that
4931  * the i-th tuple of the result array contains 3 components of a vector which is a cross
4932  * product of two vectors defined by the i-th tuples of given arrays.
4933  * Info on components is copied from the first of the given arrays.
4934  * Number of tuples in the given arrays must be the same.
4935  * Number of components in the given arrays must be 3.
4936  *  \param [in] a1 - a given array.
4937  *  \param [in] a2 - another given array.
4938  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4939  *          The caller is to delete this result array using decrRef() as it is no more
4940  *          needed.
4941  *  \throw If either \a a1 or \a a2 is NULL.
4942  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4943  *  \throw If \a a1->getNumberOfComponents() != 3
4944  *  \throw If \a a2->getNumberOfComponents() != 3
4945  */
4946 DataArrayDouble *DataArrayDouble::CrossProduct(const DataArrayDouble *a1, const DataArrayDouble *a2)
4947 {
4948   if(!a1 || !a2)
4949     throw INTERP_KERNEL::Exception("DataArrayDouble::CrossProduct : input DataArrayDouble instance is NULL !");
4950   int nbOfComp=a1->getNumberOfComponents();
4951   if(nbOfComp!=a2->getNumberOfComponents())
4952     throw INTERP_KERNEL::Exception("Nb of components mismatch for array crossProduct !");
4953   if(nbOfComp!=3)
4954     throw INTERP_KERNEL::Exception("Nb of components must be equal to 3 for array crossProduct !");
4955   int nbOfTuple=a1->getNumberOfTuples();
4956   if(nbOfTuple!=a2->getNumberOfTuples())
4957     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array crossProduct !");
4958   DataArrayDouble *ret=DataArrayDouble::New();
4959   ret->alloc(nbOfTuple,3);
4960   double *retPtr=ret->getPointer();
4961   const double *a1Ptr=a1->getConstPointer();
4962   const double *a2Ptr=a2->getConstPointer();
4963   for(int i=0;i<nbOfTuple;i++)
4964     {
4965       retPtr[3*i]=a1Ptr[3*i+1]*a2Ptr[3*i+2]-a1Ptr[3*i+2]*a2Ptr[3*i+1];
4966       retPtr[3*i+1]=a1Ptr[3*i+2]*a2Ptr[3*i]-a1Ptr[3*i]*a2Ptr[3*i+2];
4967       retPtr[3*i+2]=a1Ptr[3*i]*a2Ptr[3*i+1]-a1Ptr[3*i+1]*a2Ptr[3*i];
4968     }
4969   ret->copyStringInfoFrom(*a1);
4970   return ret;
4971 }
4972
4973 /*!
4974  * Returns a new DataArrayDouble containing maximal values of two given arrays.
4975  * Info on components is copied from the first of the given arrays.
4976  * Number of tuples and components in the given arrays must be the same.
4977  *  \param [in] a1 - an array to compare values with another one.
4978  *  \param [in] a2 - another array to compare values with the first one.
4979  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
4980  *          The caller is to delete this result array using decrRef() as it is no more
4981  *          needed.
4982  *  \throw If either \a a1 or \a a2 is NULL.
4983  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
4984  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
4985  */
4986 DataArrayDouble *DataArrayDouble::Max(const DataArrayDouble *a1, const DataArrayDouble *a2)
4987 {
4988   if(!a1 || !a2)
4989     throw INTERP_KERNEL::Exception("DataArrayDouble::Max : input DataArrayDouble instance is NULL !");
4990   int nbOfComp=a1->getNumberOfComponents();
4991   if(nbOfComp!=a2->getNumberOfComponents())
4992     throw INTERP_KERNEL::Exception("Nb of components mismatch for array Max !");
4993   int nbOfTuple=a1->getNumberOfTuples();
4994   if(nbOfTuple!=a2->getNumberOfTuples())
4995     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Max !");
4996   DataArrayDouble *ret=DataArrayDouble::New();
4997   ret->alloc(nbOfTuple,nbOfComp);
4998   double *retPtr=ret->getPointer();
4999   const double *a1Ptr=a1->getConstPointer();
5000   const double *a2Ptr=a2->getConstPointer();
5001   int nbElem=nbOfTuple*nbOfComp;
5002   for(int i=0;i<nbElem;i++)
5003     retPtr[i]=std::max(a1Ptr[i],a2Ptr[i]);
5004   ret->copyStringInfoFrom(*a1);
5005   return ret;
5006 }
5007
5008 /*!
5009  * Returns a new DataArrayDouble containing minimal values of two given arrays.
5010  * Info on components is copied from the first of the given arrays.
5011  * Number of tuples and components in the given arrays must be the same.
5012  *  \param [in] a1 - an array to compare values with another one.
5013  *  \param [in] a2 - another array to compare values with the first one.
5014  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5015  *          The caller is to delete this result array using decrRef() as it is no more
5016  *          needed.
5017  *  \throw If either \a a1 or \a a2 is NULL.
5018  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
5019  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
5020  */
5021 DataArrayDouble *DataArrayDouble::Min(const DataArrayDouble *a1, const DataArrayDouble *a2)
5022 {
5023   if(!a1 || !a2)
5024     throw INTERP_KERNEL::Exception("DataArrayDouble::Min : input DataArrayDouble instance is NULL !");
5025   int nbOfComp=a1->getNumberOfComponents();
5026   if(nbOfComp!=a2->getNumberOfComponents())
5027     throw INTERP_KERNEL::Exception("Nb of components mismatch for array min !");
5028   int nbOfTuple=a1->getNumberOfTuples();
5029   if(nbOfTuple!=a2->getNumberOfTuples())
5030     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array min !");
5031   DataArrayDouble *ret=DataArrayDouble::New();
5032   ret->alloc(nbOfTuple,nbOfComp);
5033   double *retPtr=ret->getPointer();
5034   const double *a1Ptr=a1->getConstPointer();
5035   const double *a2Ptr=a2->getConstPointer();
5036   int nbElem=nbOfTuple*nbOfComp;
5037   for(int i=0;i<nbElem;i++)
5038     retPtr[i]=std::min(a1Ptr[i],a2Ptr[i]);
5039   ret->copyStringInfoFrom(*a1);
5040   return ret;
5041 }
5042
5043 /*!
5044  * Returns a new DataArrayDouble that is a sum of two given arrays. There are 3
5045  * valid cases.
5046  * 1.  The arrays have same number of tuples and components. Then each value of
5047  *   the result array (_a_) is a sum of the corresponding values of \a a1 and \a a2,
5048  *   i.e.: _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, j ].
5049  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
5050  *   component. Then
5051  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, 0 ].
5052  * 3.  The arrays have same number of components and one array, say _a2_, has one
5053  *   tuple. Then
5054  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ 0, j ].
5055  *
5056  * Info on components is copied either from the first array (in the first case) or from
5057  * the array with maximal number of elements (getNbOfElems()).
5058  *  \param [in] a1 - an array to sum up.
5059  *  \param [in] a2 - another array to sum up.
5060  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5061  *          The caller is to delete this result array using decrRef() as it is no more
5062  *          needed.
5063  *  \throw If either \a a1 or \a a2 is NULL.
5064  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
5065  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
5066  *         none of them has number of tuples or components equal to 1.
5067  */
5068 DataArrayDouble *DataArrayDouble::Add(const DataArrayDouble *a1, const DataArrayDouble *a2)
5069 {
5070   if(!a1 || !a2)
5071     throw INTERP_KERNEL::Exception("DataArrayDouble::Add : input DataArrayDouble instance is NULL !");
5072   int nbOfTuple=a1->getNumberOfTuples();
5073   int nbOfTuple2=a2->getNumberOfTuples();
5074   int nbOfComp=a1->getNumberOfComponents();
5075   int nbOfComp2=a2->getNumberOfComponents();
5076   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=0;
5077   if(nbOfTuple==nbOfTuple2)
5078     {
5079       if(nbOfComp==nbOfComp2)
5080         {
5081           ret=DataArrayDouble::New();
5082           ret->alloc(nbOfTuple,nbOfComp);
5083           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<double>());
5084           ret->copyStringInfoFrom(*a1);
5085         }
5086       else
5087         {
5088           int nbOfCompMin,nbOfCompMax;
5089           const DataArrayDouble *aMin, *aMax;
5090           if(nbOfComp>nbOfComp2)
5091             {
5092               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
5093               aMin=a2; aMax=a1;
5094             }
5095           else
5096             {
5097               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
5098               aMin=a1; aMax=a2;
5099             }
5100           if(nbOfCompMin==1)
5101             {
5102               ret=DataArrayDouble::New();
5103               ret->alloc(nbOfTuple,nbOfCompMax);
5104               const double *aMinPtr=aMin->getConstPointer();
5105               const double *aMaxPtr=aMax->getConstPointer();
5106               double *res=ret->getPointer();
5107               for(int i=0;i<nbOfTuple;i++)
5108                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::plus<double>(),aMinPtr[i]));
5109               ret->copyStringInfoFrom(*aMax);
5110             }
5111           else
5112             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
5113         }
5114     }
5115   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
5116     {
5117       if(nbOfComp==nbOfComp2)
5118         {
5119           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
5120           const DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1;
5121           const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2;
5122           const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
5123           ret=DataArrayDouble::New();
5124           ret->alloc(nbOfTupleMax,nbOfComp);
5125           double *res=ret->getPointer();
5126           for(int i=0;i<nbOfTupleMax;i++)
5127             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::plus<double>());
5128           ret->copyStringInfoFrom(*aMax);
5129         }
5130       else
5131         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
5132     }
5133   else
5134     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !");
5135   return ret.retn();
5136 }
5137
5138 /*!
5139  * Adds values of another DataArrayDouble to values of \a this one. There are 3
5140  * valid cases.
5141  * 1.  The arrays have same number of tuples and components. Then each value of
5142  *   \a other array is added to the corresponding value of \a this array, i.e.:
5143  *   _a_ [ i, j ] += _other_ [ i, j ].
5144  * 2.  The arrays have same number of tuples and \a other array has one component. Then
5145  *   _a_ [ i, j ] += _other_ [ i, 0 ].
5146  * 3.  The arrays have same number of components and \a other array has one tuple. Then
5147  *   _a_ [ i, j ] += _a2_ [ 0, j ].
5148  *
5149  *  \param [in] other - an array to add to \a this one.
5150  *  \throw If \a other is NULL.
5151  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5152  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5153  *         \a other has number of both tuples and components not equal to 1.
5154  */
5155 void DataArrayDouble::addEqual(const DataArrayDouble *other)
5156 {
5157   if(!other)
5158     throw INTERP_KERNEL::Exception("DataArrayDouble::addEqual : input DataArrayDouble instance is NULL !");
5159   const char *msg="Nb of tuples mismatch for DataArrayDouble::addEqual  !";
5160   checkAllocated();
5161   other->checkAllocated();
5162   int nbOfTuple=getNumberOfTuples();
5163   int nbOfTuple2=other->getNumberOfTuples();
5164   int nbOfComp=getNumberOfComponents();
5165   int nbOfComp2=other->getNumberOfComponents();
5166   if(nbOfTuple==nbOfTuple2)
5167     {
5168       if(nbOfComp==nbOfComp2)
5169         {
5170           std::transform(begin(),end(),other->begin(),getPointer(),std::plus<double>());
5171         }
5172       else if(nbOfComp2==1)
5173         {
5174           double *ptr=getPointer();
5175           const double *ptrc=other->getConstPointer();
5176           for(int i=0;i<nbOfTuple;i++)
5177             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::plus<double>(),*ptrc++));
5178         }
5179       else
5180         throw INTERP_KERNEL::Exception(msg);
5181     }
5182   else if(nbOfTuple2==1)
5183     {
5184       if(nbOfComp2==nbOfComp)
5185         {
5186           double *ptr=getPointer();
5187           const double *ptrc=other->getConstPointer();
5188           for(int i=0;i<nbOfTuple;i++)
5189             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::plus<double>());
5190         }
5191       else
5192         throw INTERP_KERNEL::Exception(msg);
5193     }
5194   else
5195     throw INTERP_KERNEL::Exception(msg);
5196   declareAsNew();
5197 }
5198
5199 /*!
5200  * Returns a new DataArrayDouble that is a subtraction of two given arrays. There are 3
5201  * valid cases.
5202  * 1.  The arrays have same number of tuples and components. Then each value of
5203  *   the result array (_a_) is a subtraction of the corresponding values of \a a1 and
5204  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, j ].
5205  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
5206  *   component. Then
5207  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, 0 ].
5208  * 3.  The arrays have same number of components and one array, say _a2_, has one
5209  *   tuple. Then
5210  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ 0, j ].
5211  *
5212  * Info on components is copied either from the first array (in the first case) or from
5213  * the array with maximal number of elements (getNbOfElems()).
5214  *  \param [in] a1 - an array to subtract from.
5215  *  \param [in] a2 - an array to subtract.
5216  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5217  *          The caller is to delete this result array using decrRef() as it is no more
5218  *          needed.
5219  *  \throw If either \a a1 or \a a2 is NULL.
5220  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
5221  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
5222  *         none of them has number of tuples or components equal to 1.
5223  */
5224 DataArrayDouble *DataArrayDouble::Substract(const DataArrayDouble *a1, const DataArrayDouble *a2)
5225 {
5226   if(!a1 || !a2)
5227     throw INTERP_KERNEL::Exception("DataArrayDouble::Substract : input DataArrayDouble instance is NULL !");
5228   int nbOfTuple1=a1->getNumberOfTuples();
5229   int nbOfTuple2=a2->getNumberOfTuples();
5230   int nbOfComp1=a1->getNumberOfComponents();
5231   int nbOfComp2=a2->getNumberOfComponents();
5232   if(nbOfTuple2==nbOfTuple1)
5233     {
5234       if(nbOfComp1==nbOfComp2)
5235         {
5236           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5237           ret->alloc(nbOfTuple2,nbOfComp1);
5238           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus<double>());
5239           ret->copyStringInfoFrom(*a1);
5240           return ret.retn();
5241         }
5242       else if(nbOfComp2==1)
5243         {
5244           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5245           ret->alloc(nbOfTuple1,nbOfComp1);
5246           const double *a2Ptr=a2->getConstPointer();
5247           const double *a1Ptr=a1->getConstPointer();
5248           double *res=ret->getPointer();
5249           for(int i=0;i<nbOfTuple1;i++)
5250             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::minus<double>(),a2Ptr[i]));
5251           ret->copyStringInfoFrom(*a1);
5252           return ret.retn();
5253         }
5254       else
5255         {
5256           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
5257           return 0;
5258         }
5259     }
5260   else if(nbOfTuple2==1)
5261     {
5262       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
5263       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5264       ret->alloc(nbOfTuple1,nbOfComp1);
5265       const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
5266       double *pt=ret->getPointer();
5267       for(int i=0;i<nbOfTuple1;i++)
5268         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::minus<double>());
5269       ret->copyStringInfoFrom(*a1);
5270       return ret.retn();
5271     }
5272   else
5273     {
5274       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception
5275       return 0;
5276     }
5277 }
5278
5279 /*!
5280  * Subtract values of another DataArrayDouble from values of \a this one. There are 3
5281  * valid cases.
5282  * 1.  The arrays have same number of tuples and components. Then each value of
5283  *   \a other array is subtracted from the corresponding value of \a this array, i.e.:
5284  *   _a_ [ i, j ] -= _other_ [ i, j ].
5285  * 2.  The arrays have same number of tuples and \a other array has one component. Then
5286  *   _a_ [ i, j ] -= _other_ [ i, 0 ].
5287  * 3.  The arrays have same number of components and \a other array has one tuple. Then
5288  *   _a_ [ i, j ] -= _a2_ [ 0, j ].
5289  *
5290  *  \param [in] other - an array to subtract from \a this one.
5291  *  \throw If \a other is NULL.
5292  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5293  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5294  *         \a other has number of both tuples and components not equal to 1.
5295  */
5296 void DataArrayDouble::substractEqual(const DataArrayDouble *other)
5297 {
5298   if(!other)
5299     throw INTERP_KERNEL::Exception("DataArrayDouble::substractEqual : input DataArrayDouble instance is NULL !");
5300   const char *msg="Nb of tuples mismatch for DataArrayDouble::substractEqual  !";
5301   checkAllocated();
5302   other->checkAllocated();
5303   int nbOfTuple=getNumberOfTuples();
5304   int nbOfTuple2=other->getNumberOfTuples();
5305   int nbOfComp=getNumberOfComponents();
5306   int nbOfComp2=other->getNumberOfComponents();
5307   if(nbOfTuple==nbOfTuple2)
5308     {
5309       if(nbOfComp==nbOfComp2)
5310         {
5311           std::transform(begin(),end(),other->begin(),getPointer(),std::minus<double>());
5312         }
5313       else if(nbOfComp2==1)
5314         {
5315           double *ptr=getPointer();
5316           const double *ptrc=other->getConstPointer();
5317           for(int i=0;i<nbOfTuple;i++)
5318             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::minus<double>(),*ptrc++)); 
5319         }
5320       else
5321         throw INTERP_KERNEL::Exception(msg);
5322     }
5323   else if(nbOfTuple2==1)
5324     {
5325       if(nbOfComp2==nbOfComp)
5326         {
5327           double *ptr=getPointer();
5328           const double *ptrc=other->getConstPointer();
5329           for(int i=0;i<nbOfTuple;i++)
5330             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::minus<double>());
5331         }
5332       else
5333         throw INTERP_KERNEL::Exception(msg);
5334     }
5335   else
5336     throw INTERP_KERNEL::Exception(msg);
5337   declareAsNew();
5338 }
5339
5340 /*!
5341  * Returns a new DataArrayDouble that is a product of two given arrays. There are 3
5342  * valid cases.
5343  * 1.  The arrays have same number of tuples and components. Then each value of
5344  *   the result array (_a_) is a product of the corresponding values of \a a1 and
5345  *   \a a2, i.e. _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, j ].
5346  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
5347  *   component. Then
5348  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, 0 ].
5349  * 3.  The arrays have same number of components and one array, say _a2_, has one
5350  *   tuple. Then
5351  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ 0, j ].
5352  *
5353  * Info on components is copied either from the first array (in the first case) or from
5354  * the array with maximal number of elements (getNbOfElems()).
5355  *  \param [in] a1 - a factor array.
5356  *  \param [in] a2 - another factor array.
5357  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5358  *          The caller is to delete this result array using decrRef() as it is no more
5359  *          needed.
5360  *  \throw If either \a a1 or \a a2 is NULL.
5361  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
5362  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
5363  *         none of them has number of tuples or components equal to 1.
5364  */
5365 DataArrayDouble *DataArrayDouble::Multiply(const DataArrayDouble *a1, const DataArrayDouble *a2)
5366 {
5367   if(!a1 || !a2)
5368     throw INTERP_KERNEL::Exception("DataArrayDouble::Multiply : input DataArrayDouble instance is NULL !");
5369   int nbOfTuple=a1->getNumberOfTuples();
5370   int nbOfTuple2=a2->getNumberOfTuples();
5371   int nbOfComp=a1->getNumberOfComponents();
5372   int nbOfComp2=a2->getNumberOfComponents();
5373   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=0;
5374   if(nbOfTuple==nbOfTuple2)
5375     {
5376       if(nbOfComp==nbOfComp2)
5377         {
5378           ret=DataArrayDouble::New();
5379           ret->alloc(nbOfTuple,nbOfComp);
5380           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies<double>());
5381           ret->copyStringInfoFrom(*a1);
5382         }
5383       else
5384         {
5385           int nbOfCompMin,nbOfCompMax;
5386           const DataArrayDouble *aMin, *aMax;
5387           if(nbOfComp>nbOfComp2)
5388             {
5389               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
5390               aMin=a2; aMax=a1;
5391             }
5392           else
5393             {
5394               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
5395               aMin=a1; aMax=a2;
5396             }
5397           if(nbOfCompMin==1)
5398             {
5399               ret=DataArrayDouble::New();
5400               ret->alloc(nbOfTuple,nbOfCompMax);
5401               const double *aMinPtr=aMin->getConstPointer();
5402               const double *aMaxPtr=aMax->getConstPointer();
5403               double *res=ret->getPointer();
5404               for(int i=0;i<nbOfTuple;i++)
5405                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::multiplies<double>(),aMinPtr[i]));
5406               ret->copyStringInfoFrom(*aMax);
5407             }
5408           else
5409             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
5410         }
5411     }
5412   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
5413     {
5414       if(nbOfComp==nbOfComp2)
5415         {
5416           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
5417           const DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1;
5418           const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2;
5419           const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
5420           ret=DataArrayDouble::New();
5421           ret->alloc(nbOfTupleMax,nbOfComp);
5422           double *res=ret->getPointer();
5423           for(int i=0;i<nbOfTupleMax;i++)
5424             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::multiplies<double>());
5425           ret->copyStringInfoFrom(*aMax);
5426         }
5427       else
5428         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
5429     }
5430   else
5431     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !");
5432   return ret.retn();
5433 }
5434
5435 /*!
5436  * Multiply values of another DataArrayDouble to values of \a this one. There are 3
5437  * valid cases.
5438  * 1.  The arrays have same number of tuples and components. Then each value of
5439  *   \a other array is multiplied to the corresponding value of \a this array, i.e.
5440  *   _this_ [ i, j ] *= _other_ [ i, j ].
5441  * 2.  The arrays have same number of tuples and \a other array has one component. Then
5442  *   _this_ [ i, j ] *= _other_ [ i, 0 ].
5443  * 3.  The arrays have same number of components and \a other array has one tuple. Then
5444  *   _this_ [ i, j ] *= _a2_ [ 0, j ].
5445  *
5446  *  \param [in] other - an array to multiply to \a this one.
5447  *  \throw If \a other is NULL.
5448  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5449  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5450  *         \a other has number of both tuples and components not equal to 1.
5451  */
5452 void DataArrayDouble::multiplyEqual(const DataArrayDouble *other)
5453 {
5454   if(!other)
5455     throw INTERP_KERNEL::Exception("DataArrayDouble::multiplyEqual : input DataArrayDouble instance is NULL !");
5456   const char *msg="Nb of tuples mismatch for DataArrayDouble::multiplyEqual !";
5457   checkAllocated();
5458   other->checkAllocated();
5459   int nbOfTuple=getNumberOfTuples();
5460   int nbOfTuple2=other->getNumberOfTuples();
5461   int nbOfComp=getNumberOfComponents();
5462   int nbOfComp2=other->getNumberOfComponents();
5463   if(nbOfTuple==nbOfTuple2)
5464     {
5465       if(nbOfComp==nbOfComp2)
5466         {
5467           std::transform(begin(),end(),other->begin(),getPointer(),std::multiplies<double>());
5468         }
5469       else if(nbOfComp2==1)
5470         {
5471           double *ptr=getPointer();
5472           const double *ptrc=other->getConstPointer();
5473           for(int i=0;i<nbOfTuple;i++)
5474             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::multiplies<double>(),*ptrc++));
5475         }
5476       else
5477         throw INTERP_KERNEL::Exception(msg);
5478     }
5479   else if(nbOfTuple2==1)
5480     {
5481       if(nbOfComp2==nbOfComp)
5482         {
5483           double *ptr=getPointer();
5484           const double *ptrc=other->getConstPointer();
5485           for(int i=0;i<nbOfTuple;i++)
5486             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::multiplies<double>());
5487         }
5488       else
5489         throw INTERP_KERNEL::Exception(msg);
5490     }
5491   else
5492     throw INTERP_KERNEL::Exception(msg);
5493   declareAsNew();
5494 }
5495
5496 /*!
5497  * Returns a new DataArrayDouble that is a division of two given arrays. There are 3
5498  * valid cases.
5499  * 1.  The arrays have same number of tuples and components. Then each value of
5500  *   the result array (_a_) is a division of the corresponding values of \a a1 and
5501  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, j ].
5502  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
5503  *   component. Then
5504  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, 0 ].
5505  * 3.  The arrays have same number of components and one array, say _a2_, has one
5506  *   tuple. Then
5507  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ 0, j ].
5508  *
5509  * Info on components is copied either from the first array (in the first case) or from
5510  * the array with maximal number of elements (getNbOfElems()).
5511  *  \warning No check of division by zero is performed!
5512  *  \param [in] a1 - a numerator array.
5513  *  \param [in] a2 - a denominator array.
5514  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5515  *          The caller is to delete this result array using decrRef() as it is no more
5516  *          needed.
5517  *  \throw If either \a a1 or \a a2 is NULL.
5518  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
5519  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
5520  *         none of them has number of tuples or components equal to 1.
5521  */
5522 DataArrayDouble *DataArrayDouble::Divide(const DataArrayDouble *a1, const DataArrayDouble *a2)
5523 {
5524   if(!a1 || !a2)
5525     throw INTERP_KERNEL::Exception("DataArrayDouble::Divide : input DataArrayDouble instance is NULL !");
5526   int nbOfTuple1=a1->getNumberOfTuples();
5527   int nbOfTuple2=a2->getNumberOfTuples();
5528   int nbOfComp1=a1->getNumberOfComponents();
5529   int nbOfComp2=a2->getNumberOfComponents();
5530   if(nbOfTuple2==nbOfTuple1)
5531     {
5532       if(nbOfComp1==nbOfComp2)
5533         {
5534           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5535           ret->alloc(nbOfTuple2,nbOfComp1);
5536           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides<double>());
5537           ret->copyStringInfoFrom(*a1);
5538           return ret.retn();
5539         }
5540       else if(nbOfComp2==1)
5541         {
5542           MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5543           ret->alloc(nbOfTuple1,nbOfComp1);
5544           const double *a2Ptr=a2->getConstPointer();
5545           const double *a1Ptr=a1->getConstPointer();
5546           double *res=ret->getPointer();
5547           for(int i=0;i<nbOfTuple1;i++)
5548             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::divides<double>(),a2Ptr[i]));
5549           ret->copyStringInfoFrom(*a1);
5550           return ret.retn();
5551         }
5552       else
5553         {
5554           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
5555           return 0;
5556         }
5557     }
5558   else if(nbOfTuple2==1)
5559     {
5560       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
5561       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
5562       ret->alloc(nbOfTuple1,nbOfComp1);
5563       const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
5564       double *pt=ret->getPointer();
5565       for(int i=0;i<nbOfTuple1;i++)
5566         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::divides<double>());
5567       ret->copyStringInfoFrom(*a1);
5568       return ret.retn();
5569     }
5570   else
5571     {
5572       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception
5573       return 0;
5574     }
5575 }
5576
5577 /*!
5578  * Divide values of \a this array by values of another DataArrayDouble. There are 3
5579  * valid cases.
5580  * 1.  The arrays have same number of tuples and components. Then each value of
5581  *    \a this array is divided by the corresponding value of \a other one, i.e.:
5582  *   _a_ [ i, j ] /= _other_ [ i, j ].
5583  * 2.  The arrays have same number of tuples and \a other array has one component. Then
5584  *   _a_ [ i, j ] /= _other_ [ i, 0 ].
5585  * 3.  The arrays have same number of components and \a other array has one tuple. Then
5586  *   _a_ [ i, j ] /= _a2_ [ 0, j ].
5587  *
5588  *  \warning No check of division by zero is performed!
5589  *  \param [in] other - an array to divide \a this one by.
5590  *  \throw If \a other is NULL.
5591  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
5592  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
5593  *         \a other has number of both tuples and components not equal to 1.
5594  */
5595 void DataArrayDouble::divideEqual(const DataArrayDouble *other)
5596 {
5597   if(!other)
5598     throw INTERP_KERNEL::Exception("DataArrayDouble::divideEqual : input DataArrayDouble instance is NULL !");
5599   const char *msg="Nb of tuples mismatch for DataArrayDouble::divideEqual !";
5600   checkAllocated();
5601   other->checkAllocated();
5602   int nbOfTuple=getNumberOfTuples();
5603   int nbOfTuple2=other->getNumberOfTuples();
5604   int nbOfComp=getNumberOfComponents();
5605   int nbOfComp2=other->getNumberOfComponents();
5606   if(nbOfTuple==nbOfTuple2)
5607     {
5608       if(nbOfComp==nbOfComp2)
5609         {
5610           std::transform(begin(),end(),other->begin(),getPointer(),std::divides<double>());
5611         }
5612       else if(nbOfComp2==1)
5613         {
5614           double *ptr=getPointer();
5615           const double *ptrc=other->getConstPointer();
5616           for(int i=0;i<nbOfTuple;i++)
5617             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::divides<double>(),*ptrc++));
5618         }
5619       else
5620         throw INTERP_KERNEL::Exception(msg);
5621     }
5622   else if(nbOfTuple2==1)
5623     {
5624       if(nbOfComp2==nbOfComp)
5625         {
5626           double *ptr=getPointer();
5627           const double *ptrc=other->getConstPointer();
5628           for(int i=0;i<nbOfTuple;i++)
5629             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::divides<double>());
5630         }
5631       else
5632         throw INTERP_KERNEL::Exception(msg);
5633     }
5634   else
5635     throw INTERP_KERNEL::Exception(msg);
5636   declareAsNew();
5637 }
5638
5639 /*!
5640  * Returns a new DataArrayDouble that is the result of pow of two given arrays. There are 3
5641  * valid cases.
5642  *
5643  *  \param [in] a1 - an array to pow up.
5644  *  \param [in] a2 - another array to sum up.
5645  *  \return DataArrayDouble * - the new instance of DataArrayDouble.
5646  *          The caller is to delete this result array using decrRef() as it is no more
5647  *          needed.
5648  *  \throw If either \a a1 or \a a2 is NULL.
5649  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
5650  *  \throw If \a a1->getNumberOfComponents() != 1 or \a a2->getNumberOfComponents() != 1.
5651  *  \throw If there is a negative value in \a a1.
5652  */
5653 DataArrayDouble *DataArrayDouble::Pow(const DataArrayDouble *a1, const DataArrayDouble *a2)
5654 {
5655   if(!a1 || !a2)
5656     throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : at least one of input instances is null !");
5657   int nbOfTuple=a1->getNumberOfTuples();
5658   int nbOfTuple2=a2->getNumberOfTuples();
5659   int nbOfComp=a1->getNumberOfComponents();
5660   int nbOfComp2=a2->getNumberOfComponents();
5661   if(nbOfTuple!=nbOfTuple2)
5662     throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of tuples mismatches !");
5663   if(nbOfComp!=1 || nbOfComp2!=1)
5664     throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of components of both arrays must be equal to 1 !");
5665   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(nbOfTuple,1);
5666   const double *ptr1(a1->begin()),*ptr2(a2->begin());
5667   double *ptr=ret->getPointer();
5668   for(int i=0;i<nbOfTuple;i++,ptr1++,ptr2++,ptr++)
5669     {
5670       if(*ptr1>=0)
5671         {
5672           *ptr=pow(*ptr1,*ptr2);
5673         }
5674       else
5675         {
5676           std::ostringstream oss; oss << "DataArrayDouble::Pow : on tuple #" << i << " of a1 value is < 0 (" << *ptr1 << ") !";
5677           throw INTERP_KERNEL::Exception(oss.str().c_str());
5678         }
5679     }
5680   return ret.retn();
5681 }
5682
5683 /*!
5684  * Apply pow on values of another DataArrayDouble to values of \a this one.
5685  *
5686  *  \param [in] other - an array to pow to \a this one.
5687  *  \throw If \a other is NULL.
5688  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples()
5689  *  \throw If \a this->getNumberOfComponents() != 1 or \a other->getNumberOfComponents() != 1
5690  *  \throw If there is a negative value in \a this.
5691  */
5692 void DataArrayDouble::powEqual(const DataArrayDouble *other)
5693 {
5694   if(!other)
5695     throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : input instance is null !");
5696   int nbOfTuple=getNumberOfTuples();
5697   int nbOfTuple2=other->getNumberOfTuples();
5698   int nbOfComp=getNumberOfComponents();
5699   int nbOfComp2=other->getNumberOfComponents();
5700   if(nbOfTuple!=nbOfTuple2)
5701     throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of tuples mismatches !");
5702   if(nbOfComp!=1 || nbOfComp2!=1)
5703     throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of components of both arrays must be equal to 1 !");
5704   double *ptr=getPointer();
5705   const double *ptrc=other->begin();
5706   for(int i=0;i<nbOfTuple;i++,ptrc++,ptr++)
5707     {
5708       if(*ptr>=0)
5709         *ptr=pow(*ptr,*ptrc);
5710       else
5711         {
5712           std::ostringstream oss; oss << "DataArrayDouble::powEqual : on tuple #" << i << " of this value is < 0 (" << *ptr << ") !";
5713           throw INTERP_KERNEL::Exception(oss.str().c_str());
5714         }
5715     }
5716   declareAsNew();
5717 }
5718
5719 /*!
5720  * This method is \b NOT wrapped into python because it can be useful only for performance reasons in C++ context.
5721  * All values in \a this must be 0. or 1. within eps error. 0 means false, 1 means true.
5722  * If an another value than 0 or 1 appear (within eps precision) an INTERP_KERNEL::Exception will be thrown.
5723  *
5724  * \throw if \a this is not allocated.
5725  * \throw if \a this has not exactly one component.
5726  */
5727 std::vector<bool> DataArrayDouble::toVectorOfBool(double eps) const
5728 {
5729   checkAllocated();
5730   if(getNumberOfComponents()!=1)
5731     throw INTERP_KERNEL::Exception("DataArrayDouble::toVectorOfBool : must be applied on single component array !");
5732   int nbt(getNumberOfTuples());
5733   std::vector<bool> ret(nbt);
5734   const double *pt(begin());
5735   for(int i=0;i<nbt;i++)
5736     {
5737       if(fabs(pt[i])<eps)
5738         ret[i]=false;
5739       else if(fabs(pt[i]-1.)<eps)
5740         ret[i]=true;
5741       else
5742         {
5743           std::ostringstream oss; oss << "DataArrayDouble::toVectorOfBool : the tuple #" << i << " has value " << pt[i] << " is invalid ! must be 0. or 1. !";
5744           throw INTERP_KERNEL::Exception(oss.str().c_str());
5745         }
5746     }
5747   return ret;
5748 }
5749
5750 /*!
5751  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5752  * Server side.
5753  */
5754 void DataArrayDouble::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
5755 {
5756   tinyInfo.resize(2);
5757   if(isAllocated())
5758     {
5759       tinyInfo[0]=getNumberOfTuples();
5760       tinyInfo[1]=getNumberOfComponents();
5761     }
5762   else
5763     {
5764       tinyInfo[0]=-1;
5765       tinyInfo[1]=-1;
5766     }
5767 }
5768
5769 /*!
5770  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5771  * Server side.
5772  */
5773 void DataArrayDouble::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
5774 {
5775   if(isAllocated())
5776     {
5777       int nbOfCompo=getNumberOfComponents();
5778       tinyInfo.resize(nbOfCompo+1);
5779       tinyInfo[0]=getName();
5780       for(int i=0;i<nbOfCompo;i++)
5781         tinyInfo[i+1]=getInfoOnComponent(i);
5782     }
5783   else
5784     {
5785       tinyInfo.resize(1);
5786       tinyInfo[0]=getName();
5787     }
5788 }
5789
5790 /*!
5791  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5792  * This method returns if a feeding is needed.
5793  */
5794 bool DataArrayDouble::resizeForUnserialization(const std::vector<int>& tinyInfoI)
5795 {
5796   int nbOfTuple=tinyInfoI[0];
5797   int nbOfComp=tinyInfoI[1];
5798   if(nbOfTuple!=-1 || nbOfComp!=-1)
5799     {
5800       alloc(nbOfTuple,nbOfComp);
5801       return true;
5802     }
5803   return false;
5804 }
5805
5806 /*!
5807  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
5808  */
5809 void DataArrayDouble::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<std::string>& tinyInfoS)
5810 {
5811   setName(tinyInfoS[0]);
5812   if(isAllocated())
5813     {
5814       int nbOfCompo=getNumberOfComponents();
5815       for(int i=0;i<nbOfCompo;i++)
5816         setInfoOnComponent(i,tinyInfoS[i+1]);
5817     }
5818 }
5819
5820 DataArrayDoubleIterator::DataArrayDoubleIterator(DataArrayDouble *da):_da(da),_tuple_id(0),_nb_comp(0),_nb_tuple(0)
5821 {
5822   if(_da)
5823     {
5824       _da->incrRef();
5825       if(_da->isAllocated())
5826         {
5827           _nb_comp=da->getNumberOfComponents();
5828           _nb_tuple=da->getNumberOfTuples();
5829           _pt=da->getPointer();
5830         }
5831     }
5832 }
5833
5834 DataArrayDoubleIterator::~DataArrayDoubleIterator()
5835 {
5836   if(_da)
5837     _da->decrRef();
5838 }
5839
5840 DataArrayDoubleTuple *DataArrayDoubleIterator::nextt()
5841 {
5842   if(_tuple_id<_nb_tuple)
5843     {
5844       _tuple_id++;
5845       DataArrayDoubleTuple *ret=new DataArrayDoubleTuple(_pt,_nb_comp);
5846       _pt+=_nb_comp;
5847       return ret;
5848     }
5849   else
5850     return 0;
5851 }
5852
5853 DataArrayDoubleTuple::DataArrayDoubleTuple(double *pt, int nbOfComp):_pt(pt),_nb_of_compo(nbOfComp)
5854 {
5855 }
5856
5857
5858 std::string DataArrayDoubleTuple::repr() const
5859 {
5860   std::ostringstream oss; oss.precision(17); oss << "(";
5861   for(int i=0;i<_nb_of_compo-1;i++)
5862     oss << _pt[i] << ", ";
5863   oss << _pt[_nb_of_compo-1] << ")";
5864   return oss.str();
5865 }
5866
5867 double DataArrayDoubleTuple::doubleValue() const
5868 {
5869   if(_nb_of_compo==1)
5870     return *_pt;
5871   throw INTERP_KERNEL::Exception("DataArrayDoubleTuple::doubleValue : DataArrayDoubleTuple instance has not exactly 1 component -> Not possible to convert it into a double precision float !");
5872 }
5873
5874 /*!
5875  * This method returns a newly allocated instance the caller should dealed with by a ParaMEDMEM::DataArrayDouble::decrRef.
5876  * This method performs \b no copy of data. The content is only referenced using ParaMEDMEM::DataArrayDouble::useArray with ownership set to \b false.
5877  * 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
5878  * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
5879  */
5880 DataArrayDouble *DataArrayDoubleTuple::buildDADouble(int nbOfTuples, int nbOfCompo) const
5881 {
5882   if((_nb_of_compo==nbOfCompo && nbOfTuples==1) || (_nb_of_compo==nbOfTuples && nbOfCompo==1))
5883     {
5884       DataArrayDouble *ret=DataArrayDouble::New();
5885       ret->useExternalArrayWithRWAccess(_pt,nbOfTuples,nbOfCompo);
5886       return ret;
5887     }
5888   else
5889     {
5890       std::ostringstream oss; oss << "DataArrayDoubleTuple::buildDADouble : unable to build a requested DataArrayDouble instance with nbofTuple=" << nbOfTuples << " and nbOfCompo=" << nbOfCompo;
5891       oss << ".\nBecause the number of elements in this is " << _nb_of_compo << " !";
5892       throw INTERP_KERNEL::Exception(oss.str().c_str());
5893     }
5894 }
5895
5896 /*!
5897  * Returns a new instance of DataArrayInt. The caller is to delete this array
5898  * using decrRef() as it is no more needed. 
5899  */
5900 DataArrayInt *DataArrayInt::New()
5901 {
5902   return new DataArrayInt;
5903 }
5904
5905 /*!
5906  * Checks if raw data is allocated. Read more on the raw data
5907  * in \ref MEDCouplingArrayBasicsTuplesAndCompo "DataArrays infos" for more information.
5908  *  \return bool - \a true if the raw data is allocated, \a false else.
5909  */
5910 bool DataArrayInt::isAllocated() const
5911 {
5912   return getConstPointer()!=0;
5913 }
5914
5915 /*!
5916  * Checks if raw data is allocated and throws an exception if it is not the case.
5917  *  \throw If the raw data is not allocated.
5918  */
5919 void DataArrayInt::checkAllocated() const
5920 {
5921   if(!isAllocated())
5922     throw INTERP_KERNEL::Exception("DataArrayInt::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !");
5923 }
5924
5925 /*!
5926  * This method desallocated \a this without modification of informations relative to the components.
5927  * After call of this method, DataArrayInt::isAllocated will return false.
5928  * If \a this is already not allocated, \a this is let unchanged.
5929  */
5930 void DataArrayInt::desallocate()
5931 {
5932   _mem.destroy();
5933 }
5934
5935 std::size_t DataArrayInt::getHeapMemorySizeWithoutChildren() const
5936 {
5937   std::size_t sz(_mem.getNbOfElemAllocated());
5938   sz*=sizeof(int);
5939   return DataArray::getHeapMemorySizeWithoutChildren()+sz;
5940 }
5941
5942 /*!
5943  * Returns the only one value in \a this, if and only if number of elements
5944  * (nb of tuples * nb of components) is equal to 1, and that \a this is allocated.
5945  *  \return double - the sole value stored in \a this array.
5946  *  \throw If at least one of conditions stated above is not fulfilled.
5947  */
5948 int DataArrayInt::intValue() const
5949 {
5950   if(isAllocated())
5951     {
5952       if(getNbOfElems()==1)
5953         {
5954           return *getConstPointer();
5955         }
5956       else
5957         throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is allocated but number of elements is not equal to 1 !");
5958     }
5959   else
5960     throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is not allocated !");
5961 }
5962
5963 /*!
5964  * Returns an integer value characterizing \a this array, which is useful for a quick
5965  * comparison of many instances of DataArrayInt.
5966  *  \return int - the hash value.
5967  *  \throw If \a this is not allocated.
5968  */
5969 int DataArrayInt::getHashCode() const
5970 {
5971   checkAllocated();
5972   std::size_t nbOfElems=getNbOfElems();
5973   int ret=nbOfElems*65536;
5974   int delta=3;
5975   if(nbOfElems>48)
5976     delta=nbOfElems/8;
5977   int ret0=0;
5978   const int *pt=begin();
5979   for(std::size_t i=0;i<nbOfElems;i+=delta)
5980     ret0+=pt[i] & 0x1FFF;
5981   return ret+ret0;
5982 }
5983
5984 /*!
5985  * Checks the number of tuples.
5986  *  \return bool - \a true if getNumberOfTuples() == 0, \a false else.
5987  *  \throw If \a this is not allocated.
5988  */
5989 bool DataArrayInt::empty() const
5990 {
5991   checkAllocated();
5992   return getNumberOfTuples()==0;
5993 }
5994
5995 /*!
5996  * Returns a full copy of \a this. For more info on copying data arrays see
5997  * \ref MEDCouplingArrayBasicsCopyDeep.
5998  *  \return DataArrayInt * - a new instance of DataArrayInt.
5999  */
6000 DataArrayInt *DataArrayInt::deepCpy() const
6001 {
6002   return new DataArrayInt(*this);
6003 }
6004
6005 /*!
6006  * Returns either a \a deep or \a shallow copy of this array. For more info see
6007  * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
6008  *  \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
6009  *  \return DataArrayInt * - either a new instance of DataArrayInt (if \a dCpy
6010  *          == \a true) or \a this instance (if \a dCpy == \a false).
6011  */
6012 DataArrayInt *DataArrayInt::performCpy(bool dCpy) const
6013 {
6014   if(dCpy)
6015     return deepCpy();
6016   else
6017     {
6018       incrRef();
6019       return const_cast<DataArrayInt *>(this);
6020     }
6021 }
6022
6023 /*!
6024  * Copies all the data from another DataArrayInt. For more info see
6025  * \ref MEDCouplingArrayBasicsCopyDeepAssign.
6026  *  \param [in] other - another instance of DataArrayInt to copy data from.
6027  *  \throw If the \a other is not allocated.
6028  */
6029 void DataArrayInt::cpyFrom(const DataArrayInt& other)
6030 {
6031   other.checkAllocated();
6032   int nbOfTuples=other.getNumberOfTuples();
6033   int nbOfComp=other.getNumberOfComponents();
6034   allocIfNecessary(nbOfTuples,nbOfComp);
6035   std::size_t nbOfElems=(std::size_t)nbOfTuples*nbOfComp;
6036   int *pt=getPointer();
6037   const int *ptI=other.getConstPointer();
6038   for(std::size_t i=0;i<nbOfElems;i++)
6039     pt[i]=ptI[i];
6040   copyStringInfoFrom(other);
6041 }
6042
6043 /*!
6044  * This method reserve nbOfElems elements in memory ( nbOfElems*4 bytes ) \b without impacting the number of tuples in \a this.
6045  * 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.
6046  * If \a this has not already been allocated, number of components is set to one.
6047  * This method allows to reduce number of reallocations on invokation of DataArrayInt::pushBackSilent and DataArrayInt::pushBackValsSilent on \a this.
6048  * 
6049  * \sa DataArrayInt::pack, DataArrayInt::pushBackSilent, DataArrayInt::pushBackValsSilent
6050  */
6051 void DataArrayInt::reserve(std::size_t nbOfElems)
6052 {
6053   int nbCompo=getNumberOfComponents();
6054   if(nbCompo==1)
6055     {
6056       _mem.reserve(nbOfElems);
6057     }
6058   else if(nbCompo==0)
6059     {
6060       _mem.reserve(nbOfElems);
6061       _info_on_compo.resize(1);
6062     }
6063   else
6064     throw INTERP_KERNEL::Exception("DataArrayInt::reserve : not available for DataArrayInt with number of components different than 1 !");
6065 }
6066
6067 /*!
6068  * 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
6069  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
6070  *
6071  * \param [in] val the value to be added in \a this
6072  * \throw If \a this has already been allocated with number of components different from one.
6073  * \sa DataArrayInt::pushBackValsSilent
6074  */
6075 void DataArrayInt::pushBackSilent(int val)
6076 {
6077   int nbCompo=getNumberOfComponents();
6078   if(nbCompo==1)
6079     _mem.pushBack(val);
6080   else if(nbCompo==0)
6081     {
6082       _info_on_compo.resize(1);
6083       _mem.pushBack(val);
6084     }
6085   else
6086     throw INTERP_KERNEL::Exception("DataArrayInt::pushBackSilent : not available for DataArrayInt with number of components different than 1 !");
6087 }
6088
6089 /*!
6090  * 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
6091  * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
6092  *
6093  *  \param [in] valsBg - an array of values to push at the end of \c this.
6094  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
6095  *              the last value of \a valsBg is \a valsEnd[ -1 ].
6096  * \throw If \a this has already been allocated with number of components different from one.
6097  * \sa DataArrayInt::pushBackSilent
6098  */
6099 void DataArrayInt::pushBackValsSilent(const int *valsBg, const int *valsEnd)
6100 {
6101   int nbCompo=getNumberOfComponents();
6102   if(nbCompo==1)
6103     _mem.insertAtTheEnd(valsBg,valsEnd);
6104   else if(nbCompo==0)
6105     {
6106       _info_on_compo.resize(1);
6107       _mem.insertAtTheEnd(valsBg,valsEnd);
6108     }
6109   else
6110     throw INTERP_KERNEL::Exception("DataArrayInt::pushBackValsSilent : not available for DataArrayInt with number of components different than 1 !");
6111 }
6112
6113 /*!
6114  * This method returns silently ( without updating time label in \a this ) the last value, if any and suppress it.
6115  * \throw If \a this is already empty.
6116  * \throw If \a this has number of components different from one.
6117  */
6118 int DataArrayInt::popBackSilent()
6119 {
6120   if(getNumberOfComponents()==1)
6121     return _mem.popBack();
6122   else
6123     throw INTERP_KERNEL::Exception("DataArrayInt::popBackSilent : not available for DataArrayInt with number of components different than 1 !");
6124 }
6125
6126 /*!
6127  * 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.
6128  *
6129  * \sa DataArrayInt::getHeapMemorySizeWithoutChildren, DataArrayInt::reserve
6130  */
6131 void DataArrayInt::pack() const
6132 {
6133   _mem.pack();
6134 }
6135
6136 /*!
6137  * Allocates the raw data in memory. If exactly as same memory as needed already
6138  * allocated, it is not re-allocated.
6139  *  \param [in] nbOfTuple - number of tuples of data to allocate.
6140  *  \param [in] nbOfCompo - number of components of data to allocate.
6141  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
6142  */
6143 void DataArrayInt::allocIfNecessary(int nbOfTuple, int nbOfCompo)
6144 {
6145   if(isAllocated())
6146     {
6147       if(nbOfTuple!=getNumberOfTuples() || nbOfCompo!=getNumberOfComponents())
6148         alloc(nbOfTuple,nbOfCompo);
6149     }
6150   else
6151     alloc(nbOfTuple,nbOfCompo);
6152 }
6153
6154 /*!
6155  * Allocates the raw data in memory. If the memory was already allocated, then it is
6156  * freed and re-allocated. See an example of this method use
6157  * \ref MEDCouplingArraySteps1WC "here".
6158  *  \param [in] nbOfTuple - number of tuples of data to allocate.
6159  *  \param [in] nbOfCompo - number of components of data to allocate.
6160  *  \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
6161  */
6162 void DataArrayInt::alloc(int nbOfTuple, int nbOfCompo)
6163 {
6164   if(nbOfTuple<0 || nbOfCompo<0)
6165     throw INTERP_KERNEL::Exception("DataArrayInt::alloc : request for negative length of data !");
6166   _info_on_compo.resize(nbOfCompo);
6167   _mem.alloc(nbOfCompo*(std::size_t)nbOfTuple);
6168   declareAsNew();
6169 }
6170
6171 /*!
6172  * Assign zero to all values in \a this array. To know more on filling arrays see
6173  * \ref MEDCouplingArrayFill.
6174  * \throw If \a this is not allocated.
6175  */
6176 void DataArrayInt::fillWithZero()
6177 {
6178   checkAllocated();
6179   _mem.fillWithValue(0);
6180   declareAsNew();
6181 }
6182
6183 /*!
6184  * Assign \a val to all values in \a this array. To know more on filling arrays see
6185  * \ref MEDCouplingArrayFill.
6186  *  \param [in] val - the value to fill with.
6187  *  \throw If \a this is not allocated.
6188  */
6189 void DataArrayInt::fillWithValue(int val)
6190 {
6191   checkAllocated();
6192   _mem.fillWithValue(val);
6193   declareAsNew();
6194 }
6195
6196 /*!
6197  * Set all values in \a this array so that the i-th element equals to \a init + i
6198  * (i starts from zero). To know more on filling arrays see \ref MEDCouplingArrayFill.
6199  *  \param [in] init - value to assign to the first element of array.
6200  *  \throw If \a this->getNumberOfComponents() != 1
6201  *  \throw If \a this is not allocated.
6202  */
6203 void DataArrayInt::iota(int init)
6204 {
6205   checkAllocated();
6206   if(getNumberOfComponents()!=1)
6207     throw INTERP_KERNEL::Exception("DataArrayInt::iota : works only for arrays with only one component, you can call 'rearrange' method before !");
6208   int *ptr=getPointer();
6209   int ntuples=getNumberOfTuples();
6210   for(int i=0;i<ntuples;i++)
6211     ptr[i]=init+i;
6212   declareAsNew();
6213 }
6214
6215 /*!
6216  * Returns a textual and human readable representation of \a this instance of
6217  * DataArrayInt. This text is shown when a DataArrayInt is printed in Python.
6218  * \return std::string - text describing \a this DataArrayInt.
6219  * 
6220  * \sa reprNotTooLong, reprZip
6221  */
6222 std::string DataArrayInt::repr() const
6223 {
6224   std::ostringstream ret;
6225   reprStream(ret);
6226   return ret.str();
6227 }
6228
6229 std::string DataArrayInt::reprZip() const
6230 {
6231   std::ostringstream ret;
6232   reprZipStream(ret);
6233   return ret.str();
6234 }
6235
6236 /*!
6237  * This method is close to repr method except that when \a this has more than 1000 tuples, all tuples are not
6238  * printed out to avoid to consume too much space in interpretor.
6239  * \sa repr
6240  */
6241 std::string DataArrayInt::reprNotTooLong() const
6242 {
6243   std::ostringstream ret;
6244   reprNotTooLongStream(ret);
6245   return ret.str();
6246 }
6247
6248 void DataArrayInt::writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const
6249 {
6250   static const char SPACE[4]={' ',' ',' ',' '};
6251   checkAllocated();
6252   std::string idt(indent,' ');
6253   ofs << idt << "<DataArray type=\"" << type << "\" Name=\"" << nameInFile << "\" NumberOfComponents=\"" << getNumberOfComponents() << "\"";
6254   if(byteArr)
6255     {
6256       ofs << " format=\"appended\" offset=\"" << byteArr->getNumberOfTuples() << "\">";
6257       if(std::string(type)=="Int32")
6258         {
6259           const char *data(reinterpret_cast<const char *>(begin()));
6260           std::size_t sz(getNbOfElems()*sizeof(int));
6261           byteArr->insertAtTheEnd(data,data+sz);
6262           byteArr->insertAtTheEnd(SPACE,SPACE+4);
6263         }
6264       else if(std::string(type)=="Int8")
6265         {
6266           INTERP_KERNEL::AutoPtr<char> tmp(new char[getNbOfElems()]);
6267           std::copy(begin(),end(),(char *)tmp);
6268           byteArr->insertAtTheEnd((char *)tmp,(char *)tmp+getNbOfElems());
6269           byteArr->insertAtTheEnd(SPACE,SPACE+4);
6270         }
6271       else if(std::string(type)=="UInt8")
6272         {
6273           INTERP_KERNEL::AutoPtr<unsigned char> tmp(new unsigned char[getNbOfElems()]);
6274           std::copy(begin(),end(),(unsigned char *)tmp);
6275           byteArr->insertAtTheEnd((unsigned char *)tmp,(unsigned char *)tmp+getNbOfElems());
6276           byteArr->insertAtTheEnd(SPACE,SPACE+4);
6277         }
6278       else
6279         throw INTERP_KERNEL::Exception("DataArrayInt::writeVTK : Only Int32, Int8 and UInt8 supported !");
6280     }
6281   else
6282     {
6283       ofs << " RangeMin=\"" << getMinValueInArray() << "\" RangeMax=\"" << getMaxValueInArray() << "\" format=\"ascii\">\n" << idt;
6284       std::copy(begin(),end(),std::ostream_iterator<int>(ofs," "));
6285     }
6286   ofs << std::endl << idt << "</DataArray>\n";
6287 }
6288
6289 void DataArrayInt::reprStream(std::ostream& stream) const
6290 {
6291   stream << "Name of int array : \"" << _name << "\"\n";
6292   reprWithoutNameStream(stream);
6293 }
6294
6295 void DataArrayInt::reprZipStream(std::ostream& stream) const
6296 {
6297   stream << "Name of int array : \"" << _name << "\"\n";
6298   reprZipWithoutNameStream(stream);
6299 }
6300
6301 void DataArrayInt::reprNotTooLongStream(std::ostream& stream) const
6302 {
6303   stream << "Name of int array : \"" << _name << "\"\n";
6304   reprNotTooLongWithoutNameStream(stream);
6305 }
6306
6307 void DataArrayInt::reprWithoutNameStream(std::ostream& stream) const
6308 {
6309   DataArray::reprWithoutNameStream(stream);
6310   _mem.repr(getNumberOfComponents(),stream);
6311 }
6312
6313 void DataArrayInt::reprZipWithoutNameStream(std::ostream& stream) const
6314 {
6315   DataArray::reprWithoutNameStream(stream);
6316   _mem.reprZip(getNumberOfComponents(),stream);
6317 }
6318
6319 void DataArrayInt::reprNotTooLongWithoutNameStream(std::ostream& stream) const
6320 {
6321   DataArray::reprWithoutNameStream(stream);
6322   stream.precision(17);
6323   _mem.reprNotTooLong(getNumberOfComponents(),stream);
6324 }
6325
6326 void DataArrayInt::reprCppStream(const std::string& varName, std::ostream& stream) const
6327 {
6328   int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents();
6329   const int *data=getConstPointer();
6330   stream << "DataArrayInt *" << varName << "=DataArrayInt::New();" << std::endl;
6331   if(nbTuples*nbComp>=1)
6332     {
6333       stream << "const int " << varName << "Data[" << nbTuples*nbComp << "]={";
6334       std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<int>(stream,","));
6335       stream << data[nbTuples*nbComp-1] << "};" << std::endl;
6336       stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
6337     }
6338   else
6339     stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
6340   stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
6341 }
6342
6343 /*!
6344  * Method that gives a quick overvien of \a this for python.
6345  */
6346 void DataArrayInt::reprQuickOverview(std::ostream& stream) const
6347 {
6348   static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
6349   stream << "DataArrayInt C++ instance at " << this << ". ";
6350   if(isAllocated())
6351     {
6352       int nbOfCompo=(int)_info_on_compo.size();
6353       if(nbOfCompo>=1)
6354         {
6355           int nbOfTuples=getNumberOfTuples();
6356           stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
6357           reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
6358         }
6359       else
6360         stream << "Number of components : 0.";
6361     }
6362   else
6363     stream << "*** No data allocated ****";
6364 }
6365
6366 void DataArrayInt::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
6367 {
6368   const int *data=begin();
6369   int nbOfTuples=getNumberOfTuples();
6370   int nbOfCompo=(int)_info_on_compo.size();
6371   std::ostringstream oss2; oss2 << "[";
6372   std::string oss2Str(oss2.str());
6373   bool isFinished=true;
6374   for(int i=0;i<nbOfTuples && isFinished;i++)
6375     {
6376       if(nbOfCompo>1)
6377         {
6378           oss2 << "(";
6379           for(int j=0;j<nbOfCompo;j++,data++)
6380             {
6381               oss2 << *data;
6382               if(j!=nbOfCompo-1) oss2 << ", ";
6383             }
6384           oss2 << ")";
6385         }
6386       else
6387         oss2 << *data++;
6388       if(i!=nbOfTuples-1) oss2 << ", ";
6389       std::string oss3Str(oss2.str());
6390       if(oss3Str.length()<maxNbOfByteInRepr)
6391         oss2Str=oss3Str;
6392       else
6393         isFinished=false;
6394     }
6395   stream << oss2Str;
6396   if(!isFinished)
6397     stream << "... ";
6398   stream << "]";
6399 }
6400
6401 /*!
6402  * Modifies in place \a this one-dimensional array so that each value \a v = \a indArrBg[ \a v ],
6403  * i.e. a current value is used as in index to get a new value from \a indArrBg.
6404  *  \param [in] indArrBg - pointer to the first element of array of new values to assign
6405  *         to \a this array.
6406  *  \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
6407  *              the last value of \a indArrBg is \a indArrEnd[ -1 ].
6408  *  \throw If \a this->getNumberOfComponents() != 1
6409  *  \throw If any value of \a this can't be used as a valid index for 
6410  *         [\a indArrBg, \a indArrEnd).
6411  *
6412  *  \sa replaceOneValByInThis
6413  */
6414 void DataArrayInt::transformWithIndArr(const int *indArrBg, const int *indArrEnd)
6415 {
6416   checkAllocated();
6417   if(getNumberOfComponents()!=1)
6418     throw INTERP_KERNEL::Exception("Call transformWithIndArr method on DataArrayInt with only one component, you can call 'rearrange' method before !");
6419   int nbElemsIn((int)std::distance(indArrBg,indArrEnd)),nbOfTuples(getNumberOfTuples()),*pt(getPointer());
6420   for(int i=0;i<nbOfTuples;i++,pt++)
6421     {
6422       if(*pt>=0 && *pt<nbElemsIn)
6423         *pt=indArrBg[*pt];
6424       else
6425         {
6426           std::ostringstream oss; oss << "DataArrayInt::transformWithIndArr : error on tuple #" << i << " of this value is " << *pt << ", should be in [0," << nbElemsIn << ") !";
6427           throw INTERP_KERNEL::Exception(oss.str().c_str());
6428         }
6429     }
6430   declareAsNew();
6431 }
6432
6433 /*!
6434  * Modifies in place \a this one-dimensional array like this : each id in \a this so that this[id] equal to \a valToBeReplaced will be replaced at the same place by \a replacedBy.
6435  *
6436  * \param [in] valToBeReplaced - the value in \a this to be replaced.
6437  * \param [in] replacedBy - the value taken by each tuple previously equal to \a valToBeReplaced.
6438  *
6439  * \sa DataArrayInt::transformWithIndArr
6440  */
6441 void DataArrayInt::replaceOneValByInThis(int valToBeReplaced, int replacedBy)
6442 {
6443   checkAllocated();
6444   if(getNumberOfComponents()!=1)
6445     throw INTERP_KERNEL::Exception("Call replaceOneValByInThis method on DataArrayInt with only one component, you can call 'rearrange' method before !");
6446   if(valToBeReplaced==replacedBy)
6447     return ;
6448   int nbOfTuples(getNumberOfTuples()),*pt(getPointer());
6449   for(int i=0;i<nbOfTuples;i++,pt++)
6450     {
6451       if(*pt==valToBeReplaced)
6452         *pt=replacedBy;
6453     }
6454 }
6455
6456 /*!
6457  * Computes distribution of values of \a this one-dimensional array between given value
6458  * ranges (casts). This method is typically useful for entity number spliting by types,
6459  * for example. 
6460  *  \warning The values contained in \a arrBg should be sorted ascendently. No
6461  *           check of this is be done. If not, the result is not warranted. 
6462  *  \param [in] arrBg - the array of ascending values defining the value ranges. The i-th
6463  *         value of \a arrBg (\a arrBg[ i ]) gives the lowest value of the i-th range,
6464  *         and the greatest value of the i-th range equals to \a arrBg[ i+1 ] - 1. \a
6465  *         arrBg containing \a n values defines \a n-1 ranges. The last value of \a arrBg
6466  *         should be more than every value in \a this array.
6467  *  \param [in] arrEnd - specifies the end of the array \a arrBg, so that
6468  *              the last value of \a arrBg is \a arrEnd[ -1 ].
6469  *  \param [out] castArr - a new instance of DataArrayInt, of same size as \a this array
6470  *         (same number of tuples and components), the caller is to delete 
6471  *         using decrRef() as it is no more needed.
6472  *         This array contains indices of ranges for every value of \a this array. I.e.
6473  *         the i-th value of \a castArr gives the index of range the i-th value of \a this
6474  *         belongs to. Or, in other words, this parameter contains for each tuple in \a
6475  *         this in which cast it holds.
6476  *  \param [out] rankInsideCast - a new instance of DataArrayInt, of same size as \a this
6477  *         array, the caller is to delete using decrRef() as it is no more needed.
6478  *         This array contains ranks of values of \a this array within ranges
6479  *         they belongs to. I.e. the i-th value of \a rankInsideCast gives the rank of
6480  *         the i-th value of \a this array within the \a castArr[ i ]-th range, to which
6481  *         the i-th value of \a this belongs to. Or, in other words, this param contains 
6482  *         for each tuple its rank inside its cast. The rank is computed as difference
6483  *         between the value and the lowest value of range.
6484  *  \param [out] castsPresent - a new instance of DataArrayInt, containing indices of
6485  *         ranges (casts) to which at least one value of \a this array belongs.
6486  *         Or, in other words, this param contains the casts that \a this contains.
6487  *         The caller is to delete this array using decrRef() as it is no more needed.
6488  *
6489  * \b Example: If \a this contains [6,5,0,3,2,7,8,1,4] and \a arrBg contains [0,4,9] then
6490  *            the output of this method will be : 
6491  * - \a castArr       : [1,1,0,0,0,1,1,0,1]
6492  * - \a rankInsideCast: [2,1,0,3,2,3,4,1,0]
6493  * - \a castsPresent  : [0,1]
6494  *
6495  * I.e. values of \a this array belong to 2 ranges: #0 and #1. Value 6 belongs to the
6496  * range #1 and its rank within this range is 2; etc.
6497  *
6498  *  \throw If \a this->getNumberOfComponents() != 1.
6499  *  \throw If \a arrEnd - arrBg < 2.
6500  *  \throw If any value of \a this is not less than \a arrEnd[-1].
6501  */
6502 void DataArrayInt::splitByValueRange(const int *arrBg, const int *arrEnd,
6503                                      DataArrayInt *& castArr, DataArrayInt *& rankInsideCast, DataArrayInt *& castsPresent) const
6504 {
6505   checkAllocated();
6506   if(getNumberOfComponents()!=1)
6507     throw INTERP_KERNEL::Exception("Call splitByValueRange  method on DataArrayInt with only one component, you can call 'rearrange' method before !");
6508   int nbOfTuples=getNumberOfTuples();
6509   std::size_t nbOfCast=std::distance(arrBg,arrEnd);
6510   if(nbOfCast<2)
6511     throw INTERP_KERNEL::Exception("DataArrayInt::splitByValueRange : The input array giving the cast range values should be of size >=2 !");
6512   nbOfCast--;
6513   const int *work=getConstPointer();
6514   typedef std::reverse_iterator<const int *> rintstart;
6515   rintstart bg(arrEnd);//OK no problem because size of 'arr' is greater or equal 2
6516   rintstart end2(arrBg);
6517   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=DataArrayInt::New();
6518   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret2=DataArrayInt::New();
6519   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret3=DataArrayInt::New();
6520   ret1->alloc(nbOfTuples,1);
6521   ret2->alloc(nbOfTuples,1);
6522   int *ret1Ptr=ret1->getPointer();
6523   int *ret2Ptr=ret2->getPointer();
6524   std::set<std::size_t> castsDetected;
6525   for(int i=0;i<nbOfTuples;i++)
6526     {
6527       rintstart res=std::find_if(bg,end2,std::bind2nd(std::less_equal<int>(), work[i]));
6528       std::size_t pos=std::distance(bg,res);
6529       std::size_t pos2=nbOfCast-pos;
6530       if(pos2<nbOfCast)
6531         {
6532           ret1Ptr[i]=(int)pos2;
6533           ret2Ptr[i]=work[i]-arrBg[pos2];
6534           castsDetected.insert(pos2);
6535         }
6536       else
6537         {
6538           std::ostringstream oss; oss << "DataArrayInt::splitByValueRange : At rank #" << i << " the value is " << work[i] << " should be in [0," << *bg << ") !";
6539           throw INTERP_KERNEL::Exception(oss.str().c_str());
6540         }
6541     }
6542   ret3->alloc((int)castsDetected.size(),1);
6543   std::copy(castsDetected.begin(),castsDetected.end(),ret3->getPointer());
6544   castArr=ret1.retn();
6545   rankInsideCast=ret2.retn();
6546   castsPresent=ret3.retn();
6547 }
6548
6549 /*!
6550  * This method look at \a this if it can be considered as a range defined by the 3-tuple ( \a strt , \a sttoopp , \a stteepp ).
6551  * If false is returned the tuple must be ignored. If true is returned \a this can be considered by a range( \a strt , \a sttoopp , \a stteepp ).
6552  * This method works only if \a this is allocated and single component. If not an exception will be thrown.
6553  *
6554  * \param [out] strt - the start of the range (included) if true is returned.
6555  * \param [out] sttoopp - the end of the range (not included) if true is returned.
6556  * \param [out] stteepp - the step of the range if true is returned.
6557  * \return the verdict of the check.
6558  *
6559  * \sa DataArray::GetNumberOfItemGivenBES
6560  */
6561 bool DataArrayInt::isRange(int& strt, int& sttoopp, int& stteepp) const
6562 {
6563   checkAllocated();
6564   if(getNumberOfComponents()!=1)
6565     throw INTERP_KERNEL::Exception("DataArrayInt::isRange : this must be single component array !");
6566   int nbTuples(getNumberOfTuples());
6567   if(nbTuples==0)
6568     { strt=0; sttoopp=0; stteepp=1; return true; }
6569   const int *pt(begin());
6570   strt=*pt; 
6571   if(nbTuples==1)
6572     { sttoopp=strt+1; stteepp=1; return true; }
6573   strt=*pt; sttoopp=pt[nbTuples-1];
6574   if(strt==sttoopp)
6575     return false;
6576   if(sttoopp>strt)
6577     {
6578       sttoopp++;
6579       int a(sttoopp-1-strt),tmp(strt);
6580       if(a%(nbTuples-1)!=0)
6581         return false;
6582       stteepp=a/(nbTuples-1);
6583       for(int i=0;i<nbTuples;i++,tmp+=stteepp)
6584         if(pt[i]!=tmp)
6585           return false;
6586       return true;
6587     }
6588   else
6589     {
6590       sttoopp--;
6591       int a(strt-sttoopp-1),tmp(strt);
6592       if(a%(nbTuples-1)!=0)
6593         return false;
6594       stteepp=-(a/(nbTuples-1));
6595       for(int i=0;i<nbTuples;i++,tmp+=stteepp)
6596         if(pt[i]!=tmp)
6597           return false;
6598       return true;
6599     }
6600 }
6601
6602 /*!
6603  * Creates a one-dimensional DataArrayInt (\a res) whose contents are computed from 
6604  * values of \a this (\a a) and the given (\a indArr) arrays as follows:
6605  * \a res[ \a indArr[ \a a[ i ]]] = i. I.e. for each value in place i \a v = \a a[ i ],
6606  * new value in place \a indArr[ \a v ] is i.
6607  *  \param [in] indArrBg - the array holding indices within the result array to assign
6608  *         indices of values of \a this array pointing to values of \a indArrBg.
6609  *  \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
6610  *              the last value of \a indArrBg is \a indArrEnd[ -1 ].
6611  *  \return DataArrayInt * - the new instance of DataArrayInt.
6612  *          The caller is to delete this result array using decrRef() as it is no more
6613  *          needed.
6614  *  \throw If \a this->getNumberOfComponents() != 1.
6615  *  \throw If any value of \a this array is not a valid index for \a indArrBg array.
6616  *  \throw If any value of \a indArrBg is not a valid index for \a this array.
6617  */
6618 DataArrayInt *DataArrayInt::transformWithIndArrR(const int *indArrBg, const int *indArrEnd) const
6619 {
6620   checkAllocated();
6621   if(getNumberOfComponents()!=1)
6622     throw INTERP_KERNEL::Exception("Call transformWithIndArrR method on DataArrayInt with only one component, you can call 'rearrange' method before !");
6623   int nbElemsIn=(int)std::distance(indArrBg,indArrEnd);
6624   int nbOfTuples=getNumberOfTuples();
6625   const int *pt=getConstPointer();
6626   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6627   ret->alloc(nbOfTuples,1);
6628   ret->fillWithValue(-1);
6629   int *tmp=ret->getPointer();
6630   for(int i=0;i<nbOfTuples;i++,pt++)
6631     {
6632       if(*pt>=0 && *pt<nbElemsIn)
6633         {
6634           int pos=indArrBg[*pt];
6635           if(pos>=0 && pos<nbOfTuples)
6636             tmp[pos]=i;
6637           else
6638             {
6639               std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value of new pos is " << pos << " ( indArrBg[" << *pt << "]) ! Should be in [0," << nbOfTuples << ") !";
6640               throw INTERP_KERNEL::Exception(oss.str().c_str());
6641             }
6642         }
6643       else
6644         {
6645           std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value is " << *pt << " and indirectionnal array as a size equal to " << nbElemsIn << " !";
6646           throw INTERP_KERNEL::Exception(oss.str().c_str());
6647         }
6648     }
6649   return ret.retn();
6650 }
6651
6652 /*!
6653  * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
6654  * from values of \a this array, which is supposed to contain a renumbering map in 
6655  * "Old to New" mode. The result array contains a renumbering map in "New to Old" mode.
6656  * To know how to use the renumbering maps see \ref numbering.
6657  *  \param [in] newNbOfElem - the number of tuples in the result array.
6658  *  \return DataArrayInt * - the new instance of DataArrayInt.
6659  *          The caller is to delete this result array using decrRef() as it is no more
6660  *          needed.
6661  * 
6662  *  \if ENABLE_EXAMPLES
6663  *  \ref cpp_mcdataarrayint_invertarrayo2n2n2o "Here is a C++ example".<br>
6664  *  \ref py_mcdataarrayint_invertarrayo2n2n2o  "Here is a Python example".
6665  *  \endif
6666  */
6667 DataArrayInt *DataArrayInt::invertArrayO2N2N2O(int newNbOfElem) const
6668 {
6669   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6670   ret->alloc(newNbOfElem,1);
6671   int nbOfOldNodes=getNumberOfTuples();
6672   const int *old2New=getConstPointer();
6673   int *pt=ret->getPointer();
6674   for(int i=0;i!=nbOfOldNodes;i++)
6675     {
6676       int newp(old2New[i]);
6677       if(newp!=-1)
6678         {
6679           if(newp>=0 && newp<newNbOfElem)
6680             pt[newp]=i;
6681           else
6682             {
6683               std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2O : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
6684               throw INTERP_KERNEL::Exception(oss.str().c_str());
6685             }
6686         }
6687     }
6688   return ret.retn();
6689 }
6690
6691 /*!
6692  * This method is similar to DataArrayInt::invertArrayO2N2N2O except that 
6693  * 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]
6694  */
6695 DataArrayInt *DataArrayInt::invertArrayO2N2N2OBis(int newNbOfElem) const
6696 {
6697   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6698   ret->alloc(newNbOfElem,1);
6699   int nbOfOldNodes=getNumberOfTuples();
6700   const int *old2New=getConstPointer();
6701   int *pt=ret->getPointer();
6702   for(int i=nbOfOldNodes-1;i>=0;i--)
6703     {
6704       int newp(old2New[i]);
6705       if(newp!=-1)
6706         {
6707           if(newp>=0 && newp<newNbOfElem)
6708             pt[newp]=i;
6709           else
6710             {
6711               std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2OBis : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
6712               throw INTERP_KERNEL::Exception(oss.str().c_str());
6713             }
6714         }
6715     }
6716   return ret.retn();
6717 }
6718
6719 /*!
6720  * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
6721  * from values of \a this array, which is supposed to contain a renumbering map in 
6722  * "New to Old" mode. The result array contains a renumbering map in "Old to New" mode.
6723  * To know how to use the renumbering maps see \ref numbering.
6724  *  \param [in] newNbOfElem - the number of tuples in the result array.
6725  *  \return DataArrayInt * - the new instance of DataArrayInt.
6726  *          The caller is to delete this result array using decrRef() as it is no more
6727  *          needed.
6728  * 
6729  *  \if ENABLE_EXAMPLES
6730  *  \ref cpp_mcdataarrayint_invertarrayn2o2o2n "Here is a C++ example".
6731  *
6732  *  \ref py_mcdataarrayint_invertarrayn2o2o2n "Here is a Python example".
6733  *  \endif
6734  */
6735 DataArrayInt *DataArrayInt::invertArrayN2O2O2N(int oldNbOfElem) const
6736 {
6737   checkAllocated();
6738   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
6739   ret->alloc(oldNbOfElem,1);
6740   const int *new2Old=getConstPointer();
6741   int *pt=ret->getPointer();
6742   std::fill(pt,pt+oldNbOfElem,-1);
6743   int nbOfNewElems=getNumberOfTuples();
6744   for(int i=0;i<nbOfNewElems;i++)
6745     {
6746       int v(new2Old[i]);
6747       if(v>=0 && v<oldNbOfElem)
6748         pt[v]=i;
6749       else
6750         {
6751           std::ostringstream oss; oss << "DataArrayInt::invertArrayN2O2O2N : in new id #" << i << " old value is " << v << " expected to be in [0," << oldNbOfElem << ") !";
6752           throw INTERP_KERNEL::Exception(oss.str().c_str());
6753         }
6754     }
6755   return ret.retn();
6756 }
6757
6758 /*!
6759  * Equivalent to DataArrayInt::isEqual except that if false the reason of
6760  * mismatch is given.
6761  * 
6762  * \param [in] other the instance to be compared with \a this
6763  * \param [out] reason In case of inequality returns the reason.
6764  * \sa DataArrayInt::isEqual
6765  */
6766 bool DataArrayInt::isEqualIfNotWhy(const DataArrayInt& other, std::string& reason) const
6767 {
6768   if(!areInfoEqualsIfNotWhy(other,reason))
6769     return false;
6770   return _mem.isEqual(other._mem,0,reason);
6771 }
6772
6773 /*!
6774  * Checks if \a this and another DataArrayInt are fully equal. For more info see
6775  * \ref MEDCouplingArrayBasicsCompare.
6776  *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
6777  *  \return bool - \a true if the two arrays are equal, \a false else.
6778  */
6779 bool DataArrayInt::isEqual(const DataArrayInt& other) const
6780 {
6781   std::string tmp;
6782   return isEqualIfNotWhy(other,tmp);
6783 }
6784
6785 /*!
6786  * Checks if values of \a this and another DataArrayInt are equal. For more info see
6787  * \ref MEDCouplingArrayBasicsCompare.
6788  *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
6789  *  \return bool - \a true if the values of two arrays are equal, \a false else.
6790  */
6791 bool DataArrayInt::isEqualWithoutConsideringStr(const DataArrayInt& other) const
6792 {
6793   std::string tmp;
6794   return _mem.isEqual(other._mem,0,tmp);
6795 }
6796
6797 /*!
6798  * Checks if values of \a this and another DataArrayInt are equal. Comparison is
6799  * performed on sorted value sequences.
6800  * For more info see\ref MEDCouplingArrayBasicsCompare.
6801  *  \param [in] other - an instance of DataArrayInt to compare with \a this one.
6802  *  \return bool - \a true if the sorted values of two arrays are equal, \a false else.
6803  */
6804 bool DataArrayInt::isEqualWithoutConsideringStrAndOrder(const DataArrayInt& other) const
6805 {
6806   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a=deepCpy();
6807   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> b=other.deepCpy();
6808   a->sort();
6809   b->sort();
6810   return a->isEqualWithoutConsideringStr(*b);
6811 }
6812
6813 /*!
6814  * This method compares content of input vector \a v and \a this.
6815  * 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.
6816  * For performance reasons \a this is expected to be sorted ascendingly. If not an exception will be thrown.
6817  *
6818  * \param [in] v - the vector of 'flags' to be compared with \a this.
6819  *
6820  * \throw If \a this is not sorted ascendingly.
6821  * \throw If \a this has not exactly one component.
6822  * \throw If \a this is not allocated.
6823  */
6824 bool DataArrayInt::isFittingWith(const std::vector<bool>& v) const
6825 {
6826   checkAllocated();
6827   if(getNumberOfComponents()!=1)
6828     throw INTERP_KERNEL::Exception("DataArrayInt::isFittingWith : number of components of this should be equal to one !");
6829   const int *w(begin()),*end2(end());
6830   int refVal=-std::numeric_limits<int>::max();
6831   int i=0;
6832   std::vector<bool>::const_iterator it(v.begin());
6833   for(;it!=v.end();it++,i++)
6834     {
6835       if(*it)
6836         {
6837           if(w!=end2)
6838             {
6839               if(*w++==i)
6840                 {
6841                   if(i>refVal)
6842                     refVal=i;
6843                   else
6844                     {
6845                       std::ostringstream oss; oss << "DataArrayInt::isFittingWith : At pos #" << std::distance(begin(),w-1) << " this is not sorted ascendingly !";
6846                       throw INTERP_KERNEL::Exception(oss.str().c_str());
6847                     }
6848                 }
6849               else
6850                 return false;
6851             }
6852           else
6853             return false;
6854         }
6855     }
6856   return w==end2;
6857 }
6858
6859 /*!
6860  * This method assumes that \a this has one component and is allocated. This method scans all tuples in \a this and for all tuple equal to \a val
6861  * put True to the corresponding entry in \a vec.
6862  * \a vec is expected to be with the same size than the number of tuples of \a this.
6863  */
6864 void DataArrayInt::switchOnTupleEqualTo(int val, std::vector<bool>& vec) const
6865 {
6866   checkAllocated();
6867   if(getNumberOfComponents()!=1)
6868     throw INTERP_KERNEL::Exception("DataArrayInt::switchOnTupleEqualTo : number of components of this should be equal to one !");
6869   int nbOfTuples(getNumberOfTuples());
6870   if(nbOfTuples!=(int)vec.size())
6871     throw INTERP_KERNEL::Exception("DataArrayInt::switchOnTupleEqualTo : number of tuples of this should be equal to size of input vector of bool !");
6872   const int *pt(begin());
6873   for(int i=0;i<nbOfTuples;i++)
6874     if(pt[i]==val)
6875       vec[i]=true;
6876 }
6877
6878 /*!
6879  * Sorts values of the array.
6880  *  \param [in] asc - \a true means ascending order, \a false, descending.
6881  *  \throw If \a this is not allocated.
6882  *  \throw If \a this->getNumberOfComponents() != 1.
6883  */
6884 void DataArrayInt::sort(bool asc)
6885 {
6886   checkAllocated();
6887   if(getNumberOfComponents()!=1)
6888     throw INTERP_KERNEL::Exception("DataArrayInt::sort : only supported with 'this' array with ONE component !");
6889   _mem.sort(asc);
6890   declareAsNew();
6891 }
6892
6893 /*!
6894  * Computes for each tuple the sum of number of components values in the tuple and return it.
6895  * 
6896  * \return DataArrayInt * - the new instance of DataArrayInt containing the
6897  *          same number of tuples as \a this array and one component.
6898  *          The caller is to delete this result array using decrRef() as it is no more
6899  *          needed.
6900  *  \throw If \a this is not allocated.
6901  */
6902 DataArrayInt *DataArrayInt::sumPerTuple() const
6903 {
6904   checkAllocated();
6905   int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
6906   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
6907   ret->alloc(nbOfTuple,1);
6908   const int *src(getConstPointer());
6909   int *dest(ret->getPointer());
6910   for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
6911     *dest=std::accumulate(src,src+nbOfComp,0);
6912   return ret.retn();
6913 }
6914
6915 /*!
6916  * Reverse the array values.
6917  *  \throw If \a this->getNumberOfComponents() < 1.
6918  *  \throw If \a this is not allocated.
6919  */
6920 void DataArrayInt::reverse()
6921 {
6922   checkAllocated();
6923   _mem.reverse(getNumberOfComponents());
6924   declareAsNew();
6925 }
6926
6927 /*!
6928  * Checks that \a this array is consistently **increasing** or **decreasing** in value.
6929  * If not an exception is thrown.
6930  *  \param [in] increasing - if \a true, the array values should be increasing.
6931  *  \throw If sequence of values is not strictly monotonic in agreement with \a
6932  *         increasing arg.
6933  *  \throw If \a this->getNumberOfComponents() != 1.
6934  *  \throw If \a this is not allocated.
6935  */
6936 void DataArrayInt::checkMonotonic(bool increasing) const
6937 {
6938   if(!isMonotonic(increasing))
6939     {
6940       if (increasing)
6941         throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not INCREASING monotonic !");
6942       else
6943         throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not DECREASING monotonic !");
6944     }
6945 }
6946
6947 /*!
6948  * Checks that \a this array is consistently **increasing** or **decreasing** in value.
6949  *  \param [in] increasing - if \a true, array values should be increasing.
6950  *  \return bool - \a true if values change in accordance with \a increasing arg.
6951  *  \throw If \a this->getNumberOfComponents() != 1.
6952  *  \throw If \a this is not allocated.
6953  */
6954 bool DataArrayInt::isMonotonic(bool increasing) const
6955 {
6956   checkAllocated();
6957   if(getNumberOfComponents()!=1)
6958     throw INTERP_KERNEL::Exception("DataArrayInt::isMonotonic : only supported with 'this' array with ONE component !");
6959   int nbOfElements=getNumberOfTuples();
6960   const int *ptr=getConstPointer();
6961   if(nbOfElements==0)
6962     return true;
6963   int ref=ptr[0];
6964   if(increasing)
6965     {
6966       for(int i=1;i<nbOfElements;i++)
6967         {
6968           if(ptr[i]>=ref)
6969             ref=ptr[i];
6970           else
6971             return false;
6972         }
6973     }
6974   else
6975     {
6976       for(int i=1;i<nbOfElements;i++)
6977         {
6978           if(ptr[i]<=ref)
6979             ref=ptr[i];
6980           else
6981             return false;
6982         }
6983     }
6984   return true;
6985 }
6986
6987 /*!
6988  * This method check that array consistently INCREASING or DECREASING in value.
6989  */
6990 bool DataArrayInt::isStrictlyMonotonic(bool increasing) const
6991 {
6992   checkAllocated();
6993   if(getNumberOfComponents()!=1)
6994     throw INTERP_KERNEL::Exception("DataArrayInt::isStrictlyMonotonic : only supported with 'this' array with ONE component !");
6995   int nbOfElements=getNumberOfTuples();
6996   const int *ptr=getConstPointer();
6997   if(nbOfElements==0)
6998     return true;
6999   int ref=ptr[0];
7000   if(increasing)
7001     {
7002       for(int i=1;i<nbOfElements;i++)
7003         {
7004           if(ptr[i]>ref)
7005             ref=ptr[i];
7006           else
7007             return false;
7008         }
7009     }
7010   else
7011     {
7012       for(int i=1;i<nbOfElements;i++)
7013         {
7014           if(ptr[i]<ref)
7015             ref=ptr[i];
7016           else
7017             return false;
7018         }
7019     }
7020   return true;
7021 }
7022
7023 /*!
7024  * This method check that array consistently INCREASING or DECREASING in value.
7025  */
7026 void DataArrayInt::checkStrictlyMonotonic(bool increasing) const
7027 {
7028   if(!isStrictlyMonotonic(increasing))
7029     {
7030       if (increasing)
7031         throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly INCREASING monotonic !");
7032       else
7033         throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly DECREASING monotonic !");
7034     }
7035 }
7036
7037 /*!
7038  * Creates a new one-dimensional DataArrayInt of the same size as \a this and a given
7039  * one-dimensional arrays that must be of the same length. The result array describes
7040  * correspondence between \a this and \a other arrays, so that 
7041  * <em> other.getIJ(i,0) == this->getIJ(ret->getIJ(i),0)</em>. If such a permutation is
7042  * not possible because some element in \a other is not in \a this, an exception is thrown.
7043  *  \param [in] other - an array to compute permutation to.
7044  *  \return DataArrayInt * - a new instance of DataArrayInt, which is a permutation array
7045  * from \a this to \a other. The caller is to delete this array using decrRef() as it is
7046  * no more needed.
7047  *  \throw If \a this->getNumberOfComponents() != 1.
7048  *  \throw If \a other->getNumberOfComponents() != 1.
7049  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples().
7050  *  \throw If \a other includes a value which is not in \a this array.
7051  * 
7052  *  \if ENABLE_EXAMPLES
7053  *  \ref cpp_mcdataarrayint_buildpermutationarr "Here is a C++ example".
7054  *
7055  *  \ref py_mcdataarrayint_buildpermutationarr "Here is a Python example".
7056  *  \endif
7057  */
7058 DataArrayInt *DataArrayInt::buildPermutationArr(const DataArrayInt& other) const
7059 {
7060   checkAllocated();
7061   if(getNumberOfComponents()!=1 || other.getNumberOfComponents()!=1)
7062     throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' have to have exactly ONE component !");
7063   int nbTuple=getNumberOfTuples();
7064   other.checkAllocated();
7065   if(nbTuple!=other.getNumberOfTuples())
7066     throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' must have the same number of tuple !");
7067   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7068   ret->alloc(nbTuple,1);
7069   ret->fillWithValue(-1);
7070   const int *pt=getConstPointer();
7071   std::map<int,int> mm;
7072   for(int i=0;i<nbTuple;i++)
7073     mm[pt[i]]=i;
7074   pt=other.getConstPointer();
7075   int *retToFill=ret->getPointer();
7076   for(int i=0;i<nbTuple;i++)
7077     {
7078       std::map<int,int>::const_iterator it=mm.find(pt[i]);
7079       if(it==mm.end())
7080         {
7081           std::ostringstream oss; oss << "DataArrayInt::buildPermutationArr : Arrays mismatch : element (" << pt[i] << ") in 'other' not findable in 'this' !";
7082           throw INTERP_KERNEL::Exception(oss.str().c_str());
7083         }
7084       retToFill[i]=(*it).second;
7085     }
7086   return ret.retn();
7087 }
7088
7089 /*!
7090  * Sets a C array to be used as raw data of \a this. The previously set info
7091  *  of components is retained and re-sized. 
7092  * For more info see \ref MEDCouplingArraySteps1.
7093  *  \param [in] array - the C array to be used as raw data of \a this.
7094  *  \param [in] ownership - if \a true, \a array will be deallocated at destruction of \a this.
7095  *  \param [in] type - specifies how to deallocate \a array. If \a type == ParaMEDMEM::CPP_DEALLOC,
7096  *                     \c delete [] \c array; will be called. If \a type == ParaMEDMEM::C_DEALLOC,
7097  *                     \c free(\c array ) will be called.
7098  *  \param [in] nbOfTuple - new number of tuples in \a this.
7099  *  \param [in] nbOfCompo - new number of components in \a this.
7100  */
7101 void DataArrayInt::useArray(const int *array, bool ownership,  DeallocType type, int nbOfTuple, int nbOfCompo)
7102 {
7103   _info_on_compo.resize(nbOfCompo);
7104   _mem.useArray(array,ownership,type,nbOfTuple*nbOfCompo);
7105   declareAsNew();
7106 }
7107
7108 void DataArrayInt::useExternalArrayWithRWAccess(const int *array, int nbOfTuple, int nbOfCompo)
7109 {
7110   _info_on_compo.resize(nbOfCompo);
7111   _mem.useExternalArrayWithRWAccess(array,nbOfTuple*nbOfCompo);
7112   declareAsNew();
7113 }
7114
7115 /*!
7116  * Returns a new DataArrayInt holding the same values as \a this array but differently
7117  * arranged in memory. If \a this array holds 2 components of 3 values:
7118  * \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$, then the result array holds these values arranged
7119  * as follows: \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$.
7120  *  \warning Do not confuse this method with transpose()!
7121  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7122  *          is to delete using decrRef() as it is no more needed.
7123  *  \throw If \a this is not allocated.
7124  */
7125 DataArrayInt *DataArrayInt::fromNoInterlace() const
7126 {
7127   checkAllocated();
7128   if(_mem.isNull())
7129     throw INTERP_KERNEL::Exception("DataArrayInt::fromNoInterlace : Not defined array !");
7130   int *tab=_mem.fromNoInterlace(getNumberOfComponents());
7131   DataArrayInt *ret=DataArrayInt::New();
7132   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
7133   return ret;
7134 }
7135
7136 /*!
7137  * Returns a new DataArrayInt holding the same values as \a this array but differently
7138  * arranged in memory. If \a this array holds 2 components of 3 values:
7139  * \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$, then the result array holds these values arranged
7140  * as follows: \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$.
7141  *  \warning Do not confuse this method with transpose()!
7142  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7143  *          is to delete using decrRef() as it is no more needed.
7144  *  \throw If \a this is not allocated.
7145  */
7146 DataArrayInt *DataArrayInt::toNoInterlace() const
7147 {
7148   checkAllocated();
7149   if(_mem.isNull())
7150     throw INTERP_KERNEL::Exception("DataArrayInt::toNoInterlace : Not defined array !");
7151   int *tab=_mem.toNoInterlace(getNumberOfComponents());
7152   DataArrayInt *ret=DataArrayInt::New();
7153   ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
7154   return ret;
7155 }
7156
7157 /*!
7158  * Permutes values of \a this array as required by \a old2New array. The values are
7159  * permuted so that \c new[ \a old2New[ i ]] = \c old[ i ]. Number of tuples remains
7160  * the same as in \c this one.
7161  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
7162  * For more info on renumbering see \ref numbering.
7163  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
7164  *     giving a new position for i-th old value.
7165  */
7166 void DataArrayInt::renumberInPlace(const int *old2New)
7167 {
7168   checkAllocated();
7169   int nbTuples=getNumberOfTuples();
7170   int nbOfCompo=getNumberOfComponents();
7171   int *tmp=new int[nbTuples*nbOfCompo];
7172   const int *iptr=getConstPointer();
7173   for(int i=0;i<nbTuples;i++)
7174     {
7175       int v=old2New[i];
7176       if(v>=0 && v<nbTuples)
7177         std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),tmp+nbOfCompo*v);
7178       else
7179         {
7180           std::ostringstream oss; oss << "DataArrayInt::renumberInPlace : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
7181           throw INTERP_KERNEL::Exception(oss.str().c_str());
7182         }
7183     }
7184   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
7185   delete [] tmp;
7186   declareAsNew();
7187 }
7188
7189 /*!
7190  * Permutes values of \a this array as required by \a new2Old array. The values are
7191  * permuted so that \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of tuples remains
7192  * the same as in \c this one.
7193  * For more info on renumbering see \ref numbering.
7194  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
7195  *     giving a previous position of i-th new value.
7196  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7197  *          is to delete using decrRef() as it is no more needed.
7198  */
7199 void DataArrayInt::renumberInPlaceR(const int *new2Old)
7200 {
7201   checkAllocated();
7202   int nbTuples=getNumberOfTuples();
7203   int nbOfCompo=getNumberOfComponents();
7204   int *tmp=new int[nbTuples*nbOfCompo];
7205   const int *iptr=getConstPointer();
7206   for(int i=0;i<nbTuples;i++)
7207     {
7208       int v=new2Old[i];
7209       if(v>=0 && v<nbTuples)
7210         std::copy(iptr+nbOfCompo*v,iptr+nbOfCompo*(v+1),tmp+nbOfCompo*i);
7211       else
7212         {
7213           std::ostringstream oss; oss << "DataArrayInt::renumberInPlaceR : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
7214           throw INTERP_KERNEL::Exception(oss.str().c_str());
7215         }
7216     }
7217   std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
7218   delete [] tmp;
7219   declareAsNew();
7220 }
7221
7222 /*!
7223  * Returns a copy of \a this array with values permuted as required by \a old2New array.
7224  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ].
7225  * Number of tuples in the result array remains the same as in \c this one.
7226  * If a permutation reduction is needed, renumberAndReduce() should be used.
7227  * For more info on renumbering see \ref numbering.
7228  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
7229  *          giving a new position for i-th old value.
7230  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7231  *          is to delete using decrRef() as it is no more needed.
7232  *  \throw If \a this is not allocated.
7233  */
7234 DataArrayInt *DataArrayInt::renumber(const int *old2New) const
7235 {
7236   checkAllocated();
7237   int nbTuples=getNumberOfTuples();
7238   int nbOfCompo=getNumberOfComponents();
7239   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7240   ret->alloc(nbTuples,nbOfCompo);
7241   ret->copyStringInfoFrom(*this);
7242   const int *iptr=getConstPointer();
7243   int *optr=ret->getPointer();
7244   for(int i=0;i<nbTuples;i++)
7245     std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),optr+nbOfCompo*old2New[i]);
7246   ret->copyStringInfoFrom(*this);
7247   return ret.retn();
7248 }
7249
7250 /*!
7251  * Returns a copy of \a this array with values permuted as required by \a new2Old array.
7252  * The values are permuted so that  \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of
7253  * tuples in the result array remains the same as in \c this one.
7254  * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
7255  * For more info on renumbering see \ref numbering.
7256  *  \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
7257  *     giving a previous position of i-th new value.
7258  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7259  *          is to delete using decrRef() as it is no more needed.
7260  */
7261 DataArrayInt *DataArrayInt::renumberR(const int *new2Old) const
7262 {
7263   checkAllocated();
7264   int nbTuples=getNumberOfTuples();
7265   int nbOfCompo=getNumberOfComponents();
7266   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7267   ret->alloc(nbTuples,nbOfCompo);
7268   ret->copyStringInfoFrom(*this);
7269   const int *iptr=getConstPointer();
7270   int *optr=ret->getPointer();
7271   for(int i=0;i<nbTuples;i++)
7272     std::copy(iptr+nbOfCompo*new2Old[i],iptr+nbOfCompo*(new2Old[i]+1),optr+nbOfCompo*i);
7273   ret->copyStringInfoFrom(*this);
7274   return ret.retn();
7275 }
7276
7277 /*!
7278  * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
7279  * of size \a newNbOfTuple and it's values are permuted as required by \a old2New array.
7280  * The values are permuted so that  \c new[ \a old2New[ i ]] = \c old[ i ] for all
7281  * \a old2New[ i ] >= 0. In other words every i-th tuple in \a this array, for which 
7282  * \a old2New[ i ] is negative, is missing from the result array.
7283  * For more info on renumbering see \ref numbering.
7284  *  \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
7285  *     giving a new position for i-th old tuple and giving negative position for
7286  *     for i-th old tuple that should be omitted.
7287  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7288  *          is to delete using decrRef() as it is no more needed.
7289  */
7290 DataArrayInt *DataArrayInt::renumberAndReduce(const int *old2New, int newNbOfTuple) const
7291 {
7292   checkAllocated();
7293   int nbTuples=getNumberOfTuples();
7294   int nbOfCompo=getNumberOfComponents();
7295   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7296   ret->alloc(newNbOfTuple,nbOfCompo);
7297   const int *iptr=getConstPointer();
7298   int *optr=ret->getPointer();
7299   for(int i=0;i<nbTuples;i++)
7300     {
7301       int w=old2New[i];
7302       if(w>=0)
7303         std::copy(iptr+i*nbOfCompo,iptr+(i+1)*nbOfCompo,optr+w*nbOfCompo);
7304     }
7305   ret->copyStringInfoFrom(*this);
7306   return ret.retn();
7307 }
7308
7309 /*!
7310  * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
7311  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
7312  * \a new2OldBg array.
7313  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
7314  * This method is equivalent to renumberAndReduce() except that convention in input is
7315  * \c new2old and \b not \c old2new.
7316  * For more info on renumbering see \ref numbering.
7317  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
7318  *              tuple index in \a this array to fill the i-th tuple in the new array.
7319  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
7320  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
7321  *              \a new2OldBg <= \a pi < \a new2OldEnd.
7322  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7323  *          is to delete using decrRef() as it is no more needed.
7324  */
7325 DataArrayInt *DataArrayInt::selectByTupleId(const int *new2OldBg, const int *new2OldEnd) const
7326 {
7327   checkAllocated();
7328   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7329   int nbComp=getNumberOfComponents();
7330   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
7331   ret->copyStringInfoFrom(*this);
7332   int *pt=ret->getPointer();
7333   const int *srcPt=getConstPointer();
7334   int i=0;
7335   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
7336     std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
7337   ret->copyStringInfoFrom(*this);
7338   return ret.retn();
7339 }
7340
7341 /*!
7342  * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
7343  * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
7344  * \a new2OldBg array.
7345  * The values are permuted so that  \c new[ i ] = \c old[ \a new2OldBg[ i ]].
7346  * This method is equivalent to renumberAndReduce() except that convention in input is
7347  * \c new2old and \b not \c old2new.
7348  * This method is equivalent to selectByTupleId() except that it prevents coping data
7349  * from behind the end of \a this array.
7350  * For more info on renumbering see \ref numbering.
7351  *  \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
7352  *              tuple index in \a this array to fill the i-th tuple in the new array.
7353  *  \param [in] new2OldEnd - specifies the end of the permutation array that starts at
7354  *              \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
7355  *              \a new2OldBg <= \a pi < \a new2OldEnd.
7356  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7357  *          is to delete using decrRef() as it is no more needed.
7358  *  \throw If \a new2OldEnd - \a new2OldBg > \a this->getNumberOfTuples().
7359  */
7360 DataArrayInt *DataArrayInt::selectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const
7361 {
7362   checkAllocated();
7363   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7364   int nbComp=getNumberOfComponents();
7365   int oldNbOfTuples=getNumberOfTuples();
7366   ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
7367   ret->copyStringInfoFrom(*this);
7368   int *pt=ret->getPointer();
7369   const int *srcPt=getConstPointer();
7370   int i=0;
7371   for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
7372     if(*w>=0 && *w<oldNbOfTuples)
7373       std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
7374     else
7375       throw INTERP_KERNEL::Exception("DataArrayInt::selectByTupleIdSafe : some ids has been detected to be out of [0,this->getNumberOfTuples) !");
7376   ret->copyStringInfoFrom(*this);
7377   return ret.retn();
7378 }
7379
7380 /*!
7381  * Returns a shorten copy of \a this array. The new DataArrayInt contains every
7382  * (\a bg + \c i * \a step)-th tuple of \a this array located before the \a end2-th
7383  * tuple. Indices of the selected tuples are the same as ones returned by the Python
7384  * command \c range( \a bg, \a end2, \a step ).
7385  * This method is equivalent to selectByTupleIdSafe() except that the input array is
7386  * not constructed explicitly.
7387  * For more info on renumbering see \ref numbering.
7388  *  \param [in] bg - index of the first tuple to copy from \a this array.
7389  *  \param [in] end2 - index of the tuple before which the tuples to copy are located.
7390  *  \param [in] step - index increment to get index of the next tuple to copy.
7391  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7392  *          is to delete using decrRef() as it is no more needed.
7393  *  \sa DataArrayInt::substr.
7394  */
7395 DataArrayInt *DataArrayInt::selectByTupleId2(int bg, int end2, int step) const
7396 {
7397   checkAllocated();
7398   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7399   int nbComp=getNumberOfComponents();
7400   int newNbOfTuples=GetNumberOfItemGivenBESRelative(bg,end2,step,"DataArrayInt::selectByTupleId2 : ");
7401   ret->alloc(newNbOfTuples,nbComp);
7402   int *pt=ret->getPointer();
7403   const int *srcPt=getConstPointer()+bg*nbComp;
7404   for(int i=0;i<newNbOfTuples;i++,srcPt+=step*nbComp)
7405     std::copy(srcPt,srcPt+nbComp,pt+i*nbComp);
7406   ret->copyStringInfoFrom(*this);
7407   return ret.retn();
7408 }
7409
7410 /*!
7411  * Returns a shorten copy of \a this array. The new DataArrayInt contains ranges
7412  * of tuples specified by \a ranges parameter.
7413  * For more info on renumbering see \ref numbering.
7414  *  \param [in] ranges - std::vector of std::pair's each of which defines a range
7415  *              of tuples in [\c begin,\c end) format.
7416  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7417  *          is to delete using decrRef() as it is no more needed.
7418  *  \throw If \a end < \a begin.
7419  *  \throw If \a end > \a this->getNumberOfTuples().
7420  *  \throw If \a this is not allocated.
7421  */
7422 DataArray *DataArrayInt::selectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const
7423 {
7424   checkAllocated();
7425   int nbOfComp=getNumberOfComponents();
7426   int nbOfTuplesThis=getNumberOfTuples();
7427   if(ranges.empty())
7428     {
7429       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7430       ret->alloc(0,nbOfComp);
7431       ret->copyStringInfoFrom(*this);
7432       return ret.retn();
7433     }
7434   int ref=ranges.front().first;
7435   int nbOfTuples=0;
7436   bool isIncreasing=true;
7437   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
7438     {
7439       if((*it).first<=(*it).second)
7440         {
7441           if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
7442             {
7443               nbOfTuples+=(*it).second-(*it).first;
7444               if(isIncreasing)
7445                 isIncreasing=ref<=(*it).first;
7446               ref=(*it).second;
7447             }
7448           else
7449             {
7450               std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
7451               oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !";
7452               throw INTERP_KERNEL::Exception(oss.str().c_str());
7453             }
7454         }
7455       else
7456         {
7457           std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
7458           oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !";
7459           throw INTERP_KERNEL::Exception(oss.str().c_str());
7460         }
7461     }
7462   if(isIncreasing && nbOfTuplesThis==nbOfTuples)
7463     return deepCpy();
7464   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7465   ret->alloc(nbOfTuples,nbOfComp);
7466   ret->copyStringInfoFrom(*this);
7467   const int *src=getConstPointer();
7468   int *work=ret->getPointer();
7469   for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
7470     work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work);
7471   return ret.retn();
7472 }
7473
7474 /*!
7475  * Returns a new DataArrayInt containing a renumbering map in "Old to New" mode.
7476  * This map, if applied to \a this array, would make it sorted. For example, if
7477  * \a this array contents are [9,10,0,6,4,11,3,7] then the contents of the result array
7478  * are [5,6,0,3,2,7,1,4]; if this result array (\a res) is used as an argument in call
7479  * \a this->renumber(\a res) then the returned array contains [0,3,4,6,7,9,10,11].
7480  * This method is useful for renumbering (in MED file for example). For more info
7481  * on renumbering see \ref numbering.
7482  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7483  *          array using decrRef() as it is no more needed.
7484  *  \throw If \a this is not allocated.
7485  *  \throw If \a this->getNumberOfComponents() != 1.
7486  *  \throw If there are equal values in \a this array.
7487  */
7488 DataArrayInt *DataArrayInt::checkAndPreparePermutation() const
7489 {
7490   checkAllocated();
7491   if(getNumberOfComponents()!=1)
7492     throw INTERP_KERNEL::Exception("DataArrayInt::checkAndPreparePermutation : number of components must == 1 !");
7493   int nbTuples=getNumberOfTuples();
7494   const int *pt=getConstPointer();
7495   int *pt2=CheckAndPreparePermutation(pt,pt+nbTuples);
7496   DataArrayInt *ret=DataArrayInt::New();
7497   ret->useArray(pt2,true,C_DEALLOC,nbTuples,1);
7498   return ret;
7499 }
7500
7501 /*!
7502  * 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
7503  * input array \a ids2.
7504  * \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.
7505  * 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
7506  * inversely.
7507  * In case of success (no throw) : \c ids1->renumber(ret)->isEqual(ids2) where \a ret is the return of this method.
7508  *
7509  * \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7510  *          array using decrRef() as it is no more needed.
7511  * \throw If either ids1 or ids2 is null not allocated or not with one components.
7512  * 
7513  */
7514 DataArrayInt *DataArrayInt::FindPermutationFromFirstToSecond(const DataArrayInt *ids1, const DataArrayInt *ids2)
7515 {
7516   if(!ids1 || !ids2)
7517     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two input arrays must be not null !");
7518   if(!ids1->isAllocated() || !ids2->isAllocated())
7519     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two input arrays must be allocated !");
7520   if(ids1->getNumberOfComponents()!=1 || ids2->getNumberOfComponents()!=1)
7521     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two input arrays have exactly one component !");
7522   if(ids1->getNumberOfTuples()!=ids2->getNumberOfTuples())
7523     {
7524       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 !";
7525       throw INTERP_KERNEL::Exception(oss.str().c_str());
7526     }
7527   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(ids1->deepCpy());
7528   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p2(ids2->deepCpy());
7529   p1->sort(true); p2->sort(true);
7530   if(!p1->isEqualWithoutConsideringStr(*p2))
7531     throw INTERP_KERNEL::Exception("DataArrayInt::FindPermutationFromFirstToSecond : the two arrays are not lying on same ids ! Impossible to find a permutation between the 2 arrays !");
7532   p1=ids1->checkAndPreparePermutation();
7533   p2=ids2->checkAndPreparePermutation();
7534   p2=p2->invertArrayO2N2N2O(p2->getNumberOfTuples());
7535   p2=p2->selectByTupleIdSafe(p1->begin(),p1->end());
7536   return p2.retn();
7537 }
7538
7539 /*!
7540  * Returns two arrays describing a surjective mapping from \a this set of values (\a A)
7541  * onto a set of values of size \a targetNb (\a B). The surjective function is 
7542  * \a B[ \a A[ i ]] = i. That is to say that for each \a id in [0,\a targetNb), where \a
7543  * targetNb < \a this->getNumberOfTuples(), there exists at least one tupleId (\a tid) so
7544  * that <em> this->getIJ( tid, 0 ) == id</em>. <br>
7545  * The first of out arrays returns indices of elements of \a this array, grouped by their
7546  * place in the set \a B. The second out array is the index of the first one; it shows how
7547  * many elements of \a A are mapped into each element of \a B. <br>
7548  * For more info on
7549  * mapping and its usage in renumbering see \ref numbering. <br>
7550  * \b Example:
7551  * - \a this: [0,3,2,3,2,2,1,2]
7552  * - \a targetNb: 4
7553  * - \a arr:  [0,  6,  2,4,5,7,  1,3]
7554  * - \a arrI: [0,1,2,6,8]
7555  *
7556  * This result means: <br>
7557  * the element of \a B 0 encounters within \a A once (\a arrI[ 0+1 ] - \a arrI[ 0 ]) and
7558  * its index within \a A is 0 ( \a arr[ 0:1 ] == \a arr[ \a arrI[ 0 ] : \a arrI[ 0+1 ]]);<br>
7559  * the element of \a B 2 encounters within \a A 4 times (\a arrI[ 2+1 ] - \a arrI[ 2 ]) and
7560  * its indices within \a A are [2,4,5,7] ( \a arr[ 2:6 ] == \a arr[ \a arrI[ 2 ] : 
7561  * \a arrI[ 2+1 ]]); <br> etc.
7562  *  \param [in] targetNb - the size of the set \a B. \a targetNb must be equal or more
7563  *         than the maximal value of \a A.
7564  *  \param [out] arr - a new instance of DataArrayInt returning indices of
7565  *         elements of \a this, grouped by their place in the set \a B. The caller is to delete
7566  *         this array using decrRef() as it is no more needed.
7567  *  \param [out] arrI - a new instance of DataArrayInt returning size of groups of equal
7568  *         elements of \a this. The caller is to delete this array using decrRef() as it
7569  *         is no more needed.
7570  *  \throw If \a this is not allocated.
7571  *  \throw If \a this->getNumberOfComponents() != 1.
7572  *  \throw If any value in \a this is more or equal to \a targetNb.
7573  */
7574 void DataArrayInt::changeSurjectiveFormat(int targetNb, DataArrayInt *&arr, DataArrayInt *&arrI) const
7575 {
7576   checkAllocated();
7577   if(getNumberOfComponents()!=1)
7578     throw INTERP_KERNEL::Exception("DataArrayInt::changeSurjectiveFormat : number of components must == 1 !");
7579   int nbOfTuples=getNumberOfTuples();
7580   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
7581   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> retI(DataArrayInt::New());
7582   retI->alloc(targetNb+1,1);
7583   const int *input=getConstPointer();
7584   std::vector< std::vector<int> > tmp(targetNb);
7585   for(int i=0;i<nbOfTuples;i++)
7586     {
7587       int tmp2=input[i];
7588       if(tmp2>=0 && tmp2<targetNb)
7589         tmp[tmp2].push_back(i);
7590       else
7591         {
7592           std::ostringstream oss; oss << "DataArrayInt::changeSurjectiveFormat : At pos " << i << " presence of element " << tmp2 << " ! should be in [0," << targetNb << ") !";
7593           throw INTERP_KERNEL::Exception(oss.str().c_str());
7594         }
7595     }
7596   int *retIPtr=retI->getPointer();
7597   *retIPtr=0;
7598   for(std::vector< std::vector<int> >::const_iterator it1=tmp.begin();it1!=tmp.end();it1++,retIPtr++)
7599     retIPtr[1]=retIPtr[0]+(int)((*it1).size());
7600   if(nbOfTuples!=retI->getIJ(targetNb,0))
7601     throw INTERP_KERNEL::Exception("DataArrayInt::changeSurjectiveFormat : big problem should never happen !");
7602   ret->alloc(nbOfTuples,1);
7603   int *retPtr=ret->getPointer();
7604   for(std::vector< std::vector<int> >::const_iterator it1=tmp.begin();it1!=tmp.end();it1++)
7605     retPtr=std::copy((*it1).begin(),(*it1).end(),retPtr);
7606   arr=ret.retn();
7607   arrI=retI.retn();
7608 }
7609
7610
7611 /*!
7612  * Returns a new DataArrayInt containing a renumbering map in "Old to New" mode computed
7613  * from a zip representation of a surjective format (returned e.g. by
7614  * \ref ParaMEDMEM::DataArrayDouble::findCommonTuples() "DataArrayDouble::findCommonTuples()"
7615  * for example). The result array minimizes the permutation. <br>
7616  * For more info on renumbering see \ref numbering. <br>
7617  * \b Example: <br>
7618  * - \a nbOfOldTuples: 10 
7619  * - \a arr          : [0,3, 5,7,9]
7620  * - \a arrIBg       : [0,2,5]
7621  * - \a newNbOfTuples: 7
7622  * - result array    : [0,1,2,0,3,4,5,4,6,4]
7623  *
7624  *  \param [in] nbOfOldTuples - number of tuples in the initial array \a arr.
7625  *  \param [in] arr - the array of tuple indices grouped by \a arrIBg array.
7626  *  \param [in] arrIBg - the array dividing all indices stored in \a arr into groups of
7627  *         (indices of) equal values. Its every element (except the last one) points to
7628  *         the first element of a group of equal values.
7629  *  \param [in] arrIEnd - specifies the end of \a arrIBg, so that the last element of \a
7630  *          arrIBg is \a arrIEnd[ -1 ].
7631  *  \param [out] newNbOfTuples - number of tuples after surjection application.
7632  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7633  *          array using decrRef() as it is no more needed.
7634  *  \throw If any value of \a arr breaks condition ( 0 <= \a arr[ i ] < \a nbOfOldTuples ).
7635  */
7636 DataArrayInt *DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTuples, const int *arr, const int *arrIBg, const int *arrIEnd, int &newNbOfTuples)
7637 {
7638   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7639   ret->alloc(nbOfOldTuples,1);
7640   int *pt=ret->getPointer();
7641   std::fill(pt,pt+nbOfOldTuples,-1);
7642   int nbOfGrps=((int)std::distance(arrIBg,arrIEnd))-1;
7643   const int *cIPtr=arrIBg;
7644   for(int i=0;i<nbOfGrps;i++)
7645     pt[arr[cIPtr[i]]]=-(i+2);
7646   int newNb=0;
7647   for(int iNode=0;iNode<nbOfOldTuples;iNode++)
7648     {
7649       if(pt[iNode]<0)
7650         {
7651           if(pt[iNode]==-1)
7652             pt[iNode]=newNb++;
7653           else
7654             {
7655               int grpId=-(pt[iNode]+2);
7656               for(int j=cIPtr[grpId];j<cIPtr[grpId+1];j++)
7657                 {
7658                   if(arr[j]>=0 && arr[j]<nbOfOldTuples)
7659                     pt[arr[j]]=newNb;
7660                   else
7661                     {
7662                       std::ostringstream oss; oss << "DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2 : With element #" << j << " value is " << arr[j] << " should be in [0," << nbOfOldTuples << ") !";
7663                       throw INTERP_KERNEL::Exception(oss.str().c_str());
7664                     }
7665                 }
7666               newNb++;
7667             }
7668         }
7669     }
7670   newNbOfTuples=newNb;
7671   return ret.retn();
7672 }
7673
7674 /*!
7675  * Returns a new DataArrayInt containing a renumbering map in "New to Old" mode,
7676  * which if applied to \a this array would make it sorted ascendingly.
7677  * For more info on renumbering see \ref numbering. <br>
7678  * \b Example: <br>
7679  * - \a this: [2,0,1,1,0,1,2,0,1,1,0,0]
7680  * - result: [10,0,5,6,1,7,11,2,8,9,3,4]
7681  * - after applying result to \a this: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] 
7682  *
7683  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
7684  *          array using decrRef() as it is no more needed.
7685  *  \throw If \a this is not allocated.
7686  *  \throw If \a this->getNumberOfComponents() != 1.
7687  */
7688 DataArrayInt *DataArrayInt::buildPermArrPerLevel() const
7689 {
7690   checkAllocated();
7691   if(getNumberOfComponents()!=1)
7692     throw INTERP_KERNEL::Exception("DataArrayInt::buildPermArrPerLevel : number of components must == 1 !");
7693   int nbOfTuples=getNumberOfTuples();
7694   const int *pt=getConstPointer();
7695   std::map<int,int> m;
7696   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7697   ret->alloc(nbOfTuples,1);
7698   int *opt=ret->getPointer();
7699   for(int i=0;i<nbOfTuples;i++,pt++,opt++)
7700     {
7701       int val=*pt;
7702       std::map<int,int>::iterator it=m.find(val);
7703       if(it!=m.end())
7704         {
7705           *opt=(*it).second;
7706           (*it).second++;
7707         }
7708       else
7709         {
7710           *opt=0;
7711           m.insert(std::pair<int,int>(val,1));
7712         }
7713     }
7714   int sum=0;
7715   for(std::map<int,int>::iterator it=m.begin();it!=m.end();it++)
7716     {
7717       int vt=(*it).second;
7718       (*it).second=sum;
7719       sum+=vt;
7720     }
7721   pt=getConstPointer();
7722   opt=ret->getPointer();
7723   for(int i=0;i<nbOfTuples;i++,pt++,opt++)
7724     *opt+=m[*pt];
7725   //
7726   return ret.retn();
7727 }
7728
7729 /*!
7730  * Checks if contents of \a this array are equal to that of an array filled with
7731  * iota(). This method is particularly useful for DataArrayInt instances that represent
7732  * a renumbering array to check the real need in renumbering. In this case it is better to use isIdentity2
7733  * method of isIdentity method.
7734  *
7735  *  \return bool - \a true if \a this array contents == \a range( \a this->getNumberOfTuples())
7736  *  \throw If \a this is not allocated.
7737  *  \throw If \a this->getNumberOfComponents() != 1.
7738  *  \sa isIdentity2
7739  */
7740 bool DataArrayInt::isIdentity() const
7741 {
7742   checkAllocated();
7743   if(getNumberOfComponents()!=1)
7744     return false;
7745   int nbOfTuples(getNumberOfTuples());
7746   const int *pt=getConstPointer();
7747   for(int i=0;i<nbOfTuples;i++,pt++)
7748     if(*pt!=i)
7749       return false;
7750   return true;
7751 }
7752
7753 /*!
7754  * This method is stronger than isIdentity method. This method checks than \a this can be considered as an identity function
7755  * of a set having \a sizeExpected elements into itself.
7756  *
7757  * \param [in] sizeExpected - The number of elements
7758  * \return bool - \a true if \a this array contents == \a range( \a this->getNumberOfTuples()) and if \a this has \a sizeExpected tuples in it.
7759  *
7760  *  \throw If \a this is not allocated.
7761  *  \throw If \a this->getNumberOfComponents() != 1.
7762  * \sa isIdentity
7763  */
7764 bool DataArrayInt::isIdentity2(int sizeExpected) const
7765 {
7766   bool ret0(isIdentity());
7767   if(!ret0)
7768     return false;
7769   return getNumberOfTuples()==sizeExpected;
7770 }
7771
7772 /*!
7773  * Checks if all values in \a this array are equal to \a val.
7774  *  \param [in] val - value to check equality of array values to.
7775  *  \return bool - \a true if all values are \a val.
7776  *  \throw If \a this is not allocated.
7777  *  \throw If \a this->getNumberOfComponents() != 1
7778  */
7779 bool DataArrayInt::isUniform(int val) const
7780 {
7781   checkAllocated();
7782   if(getNumberOfComponents()!=1)
7783     throw INTERP_KERNEL::Exception("DataArrayInt::isUniform : must be applied on DataArrayInt with only one component, you can call 'rearrange' method before !");
7784   int nbOfTuples=getNumberOfTuples();
7785   const int *w=getConstPointer();
7786   const int *end2=w+nbOfTuples;
7787   for(;w!=end2;w++)
7788     if(*w!=val)
7789       return false;
7790   return true;
7791 }
7792
7793 /*!
7794  * Creates a new DataArrayDouble and assigns all (textual and numerical) data of \a this
7795  * array to the new one.
7796  *  \return DataArrayDouble * - the new instance of DataArrayInt.
7797  */
7798 DataArrayDouble *DataArrayInt::convertToDblArr() const
7799 {
7800   checkAllocated();
7801   DataArrayDouble *ret=DataArrayDouble::New();
7802   ret->alloc(getNumberOfTuples(),getNumberOfComponents());
7803   std::size_t nbOfVals=getNbOfElems();
7804   const int *src=getConstPointer();
7805   double *dest=ret->getPointer();
7806   std::copy(src,src+nbOfVals,dest);
7807   ret->copyStringInfoFrom(*this);
7808   return ret;
7809 }
7810
7811 /*!
7812  * Returns a shorten copy of \a this array. The new DataArrayInt contains all
7813  * tuples starting from the \a tupleIdBg-th tuple and including all tuples located before
7814  * the \a tupleIdEnd-th one. This methods has a similar behavior as std::string::substr().
7815  * This method is a specialization of selectByTupleId2().
7816  *  \param [in] tupleIdBg - index of the first tuple to copy from \a this array.
7817  *  \param [in] tupleIdEnd - index of the tuple before which the tuples to copy are located.
7818  *          If \a tupleIdEnd == -1, all the tuples till the end of \a this array are copied.
7819  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7820  *          is to delete using decrRef() as it is no more needed.
7821  *  \throw If \a tupleIdBg < 0.
7822  *  \throw If \a tupleIdBg > \a this->getNumberOfTuples().
7823     \throw If \a tupleIdEnd != -1 && \a tupleIdEnd < \a this->getNumberOfTuples().
7824  *  \sa DataArrayInt::selectByTupleId2
7825  */
7826 DataArrayInt *DataArrayInt::substr(int tupleIdBg, int tupleIdEnd) const
7827 {
7828   checkAllocated();
7829   int nbt=getNumberOfTuples();
7830   if(tupleIdBg<0)
7831     throw INTERP_KERNEL::Exception("DataArrayInt::substr : The tupleIdBg parameter must be greater than 0 !");
7832   if(tupleIdBg>nbt)
7833     throw INTERP_KERNEL::Exception("DataArrayInt::substr : The tupleIdBg parameter is greater than number of tuples !");
7834   int trueEnd=tupleIdEnd;
7835   if(tupleIdEnd!=-1)
7836     {
7837       if(tupleIdEnd>nbt)
7838         throw INTERP_KERNEL::Exception("DataArrayInt::substr : The tupleIdBg parameter is greater or equal than number of tuples !");
7839     }
7840   else
7841     trueEnd=nbt;
7842   int nbComp=getNumberOfComponents();
7843   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7844   ret->alloc(trueEnd-tupleIdBg,nbComp);
7845   ret->copyStringInfoFrom(*this);
7846   std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer());
7847   return ret.retn();
7848 }
7849
7850 /*!
7851  * Changes the number of components within \a this array so that its raw data **does
7852  * not** change, instead splitting this data into tuples changes.
7853  *  \warning This method erases all (name and unit) component info set before!
7854  *  \param [in] newNbOfComp - number of components for \a this array to have.
7855  *  \throw If \a this is not allocated
7856  *  \throw If getNbOfElems() % \a newNbOfCompo != 0.
7857  *  \throw If \a newNbOfCompo is lower than 1.
7858  *  \throw If the rearrange method would lead to a number of tuples higher than 2147483647 (maximal capacity of int32 !).
7859  *  \warning This method erases all (name and unit) component info set before!
7860  */
7861 void DataArrayInt::rearrange(int newNbOfCompo)
7862 {
7863   checkAllocated();
7864   if(newNbOfCompo<1)
7865     throw INTERP_KERNEL::Exception("DataArrayInt::rearrange : input newNbOfCompo must be > 0 !");
7866   std::size_t nbOfElems=getNbOfElems();
7867   if(nbOfElems%newNbOfCompo!=0)
7868     throw INTERP_KERNEL::Exception("DataArrayInt::rearrange : nbOfElems%newNbOfCompo!=0 !");
7869   if(nbOfElems/newNbOfCompo>(std::size_t)std::numeric_limits<int>::max())
7870     throw INTERP_KERNEL::Exception("DataArrayInt::rearrange : the rearrangement leads to too high number of tuples (> 2147483647) !");
7871   _info_on_compo.clear();
7872   _info_on_compo.resize(newNbOfCompo);
7873   declareAsNew();
7874 }
7875
7876 /*!
7877  * Changes the number of components within \a this array to be equal to its number
7878  * of tuples, and inversely its number of tuples to become equal to its number of 
7879  * components. So that its raw data **does not** change, instead splitting this
7880  * data into tuples changes.
7881  *  \warning This method erases all (name and unit) component info set before!
7882  *  \warning Do not confuse this method with fromNoInterlace() and toNoInterlace()!
7883  *  \throw If \a this is not allocated.
7884  *  \sa rearrange()
7885  */
7886 void DataArrayInt::transpose()
7887 {
7888   checkAllocated();
7889   int nbOfTuples=getNumberOfTuples();
7890   rearrange(nbOfTuples);
7891 }
7892
7893 /*!
7894  * Returns a shorten or extended copy of \a this array. If \a newNbOfComp is less
7895  * than \a this->getNumberOfComponents() then the result array is shorten as each tuple
7896  * is truncated to have \a newNbOfComp components, keeping first components. If \a
7897  * newNbOfComp is more than \a this->getNumberOfComponents() then the result array is
7898  * expanded as each tuple is populated with \a dftValue to have \a newNbOfComp
7899  * components.  
7900  *  \param [in] newNbOfComp - number of components for the new array to have.
7901  *  \param [in] dftValue - value assigned to new values added to the new array.
7902  *  \return DataArrayDouble * - the new instance of DataArrayDouble that the caller
7903  *          is to delete using decrRef() as it is no more needed.
7904  *  \throw If \a this is not allocated.
7905  */
7906 DataArrayInt *DataArrayInt::changeNbOfComponents(int newNbOfComp, int dftValue) const
7907 {
7908   checkAllocated();
7909   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
7910   ret->alloc(getNumberOfTuples(),newNbOfComp);
7911   const int *oldc=getConstPointer();
7912   int *nc=ret->getPointer();
7913   int nbOfTuples=getNumberOfTuples();
7914   int oldNbOfComp=getNumberOfComponents();
7915   int dim=std::min(oldNbOfComp,newNbOfComp);
7916   for(int i=0;i<nbOfTuples;i++)
7917     {
7918       int j=0;
7919       for(;j<dim;j++)
7920         nc[newNbOfComp*i+j]=oldc[i*oldNbOfComp+j];
7921       for(;j<newNbOfComp;j++)
7922         nc[newNbOfComp*i+j]=dftValue;
7923     }
7924   ret->setName(getName());
7925   for(int i=0;i<dim;i++)
7926     ret->setInfoOnComponent(i,getInfoOnComponent(i));
7927   ret->setName(getName());
7928   return ret.retn();
7929 }
7930
7931 /*!
7932  * Changes number of tuples in the array. If the new number of tuples is smaller
7933  * than the current number the array is truncated, otherwise the array is extended.
7934  *  \param [in] nbOfTuples - new number of tuples. 
7935  *  \throw If \a this is not allocated.
7936  *  \throw If \a nbOfTuples is negative.
7937  */
7938 void DataArrayInt::reAlloc(int nbOfTuples)
7939 {
7940   if(nbOfTuples<0)
7941     throw INTERP_KERNEL::Exception("DataArrayInt::reAlloc : input new number of tuples should be >=0 !");
7942   checkAllocated();
7943   _mem.reAlloc(getNumberOfComponents()*(std::size_t)nbOfTuples);
7944   declareAsNew();
7945 }
7946
7947
7948 /*!
7949  * Returns a copy of \a this array composed of selected components.
7950  * The new DataArrayInt has the same number of tuples but includes components
7951  * specified by \a compoIds parameter. So that getNbOfElems() of the result array
7952  * can be either less, same or more than \a this->getNbOfElems().
7953  *  \param [in] compoIds - sequence of zero based indices of components to include
7954  *              into the new array.
7955  *  \return DataArrayInt * - the new instance of DataArrayInt that the caller
7956  *          is to delete using decrRef() as it is no more needed.
7957  *  \throw If \a this is not allocated.
7958  *  \throw If a component index (\a i) is not valid: 
7959  *         \a i < 0 || \a i >= \a this->getNumberOfComponents().
7960  *
7961  *  \if ENABLE_EXAMPLES
7962  *  \ref py_mcdataarrayint_keepselectedcomponents "Here is a Python example".
7963  *  \endif
7964  */
7965 DataArrayInt *DataArrayInt::keepSelectedComponents(const std::vector<int>& compoIds) const
7966 {
7967   checkAllocated();
7968   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
7969   int newNbOfCompo=(int)compoIds.size();
7970   int oldNbOfCompo=getNumberOfComponents();
7971   for(std::vector<int>::const_iterator it=compoIds.begin();it!=compoIds.end();it++)
7972     DataArray::CheckValueInRange(oldNbOfCompo,(*it),"keepSelectedComponents invalid requested component");
7973   int nbOfTuples=getNumberOfTuples();
7974   ret->alloc(nbOfTuples,newNbOfCompo);
7975   ret->copyPartOfStringInfoFrom(*this,compoIds);
7976   const int *oldc=getConstPointer();
7977   int *nc=ret->getPointer();
7978   for(int i=0;i<nbOfTuples;i++)
7979     for(int j=0;j<newNbOfCompo;j++,nc++)
7980       *nc=oldc[i*oldNbOfCompo+compoIds[j]];
7981   return ret.retn();
7982 }
7983
7984 /*!
7985  * Appends components of another array to components of \a this one, tuple by tuple.
7986  * So that the number of tuples of \a this array remains the same and the number of 
7987  * components increases.
7988  *  \param [in] other - the DataArrayInt to append to \a this one.
7989  *  \throw If \a this is not allocated.
7990  *  \throw If \a this and \a other arrays have different number of tuples.
7991  *
7992  *  \if ENABLE_EXAMPLES
7993  *  \ref cpp_mcdataarrayint_meldwith "Here is a C++ example".
7994  *
7995  *  \ref py_mcdataarrayint_meldwith "Here is a Python example".
7996  *  \endif
7997  */
7998 void DataArrayInt::meldWith(const DataArrayInt *other)
7999 {
8000   if(!other)
8001     throw INTERP_KERNEL::Exception("DataArrayInt::meldWith : DataArrayInt pointer in input is NULL !");
8002   checkAllocated();
8003   other->checkAllocated();
8004   int nbOfTuples=getNumberOfTuples();
8005   if(nbOfTuples!=other->getNumberOfTuples())
8006     throw INTERP_KERNEL::Exception("DataArrayInt::meldWith : mismatch of number of tuples !");
8007   int nbOfComp1=getNumberOfComponents();
8008   int nbOfComp2=other->getNumberOfComponents();
8009   int *newArr=(int *)malloc(nbOfTuples*(nbOfComp1+nbOfComp2)*sizeof(int));
8010   int *w=newArr;
8011   const int *inp1=getConstPointer();
8012   const int *inp2=other->getConstPointer();
8013   for(int i=0;i<nbOfTuples;i++,inp1+=nbOfComp1,inp2+=nbOfComp2)
8014     {
8015       w=std::copy(inp1,inp1+nbOfComp1,w);
8016       w=std::copy(inp2,inp2+nbOfComp2,w);
8017     }
8018   useArray(newArr,true,C_DEALLOC,nbOfTuples,nbOfComp1+nbOfComp2);
8019   std::vector<int> compIds(nbOfComp2);
8020   for(int i=0;i<nbOfComp2;i++)
8021     compIds[i]=nbOfComp1+i;
8022   copyPartOfStringInfoFrom2(compIds,*other);
8023 }
8024
8025 /*!
8026  * Copy all components in a specified order from another DataArrayInt.
8027  * The specified components become the first ones in \a this array.
8028  * Both numerical and textual data is copied. The number of tuples in \a this and
8029  * the other array can be different.
8030  *  \param [in] a - the array to copy data from.
8031  *  \param [in] compoIds - sequence of zero based indices of components, data of which is
8032  *              to be copied.
8033  *  \throw If \a a is NULL.
8034  *  \throw If \a compoIds.size() != \a a->getNumberOfComponents().
8035  *  \throw If \a compoIds[i] < 0 or \a compoIds[i] > \a this->getNumberOfComponents().
8036  *
8037  *  \if ENABLE_EXAMPLES
8038  *  \ref py_mcdataarrayint_setselectedcomponents "Here is a Python example".
8039  *  \endif
8040  */
8041 void DataArrayInt::setSelectedComponents(const DataArrayInt *a, const std::vector<int>& compoIds)
8042 {
8043   if(!a)
8044     throw INTERP_KERNEL::Exception("DataArrayInt::setSelectedComponents : input DataArrayInt is NULL !");
8045   checkAllocated();
8046   a->checkAllocated();
8047   copyPartOfStringInfoFrom2(compoIds,*a);
8048   std::size_t partOfCompoSz=compoIds.size();
8049   int nbOfCompo=getNumberOfComponents();
8050   int nbOfTuples=std::min(getNumberOfTuples(),a->getNumberOfTuples());
8051   const int *ac=a->getConstPointer();
8052   int *nc=getPointer();
8053   for(int i=0;i<nbOfTuples;i++)
8054     for(std::size_t j=0;j<partOfCompoSz;j++,ac++)
8055       nc[nbOfCompo*i+compoIds[j]]=*ac;
8056 }
8057
8058 /*!
8059  * Copy all values from another DataArrayInt into specified tuples and components
8060  * of \a this array. Textual data is not copied.
8061  * The tree parameters defining set of indices of tuples and components are similar to
8062  * the tree parameters of the Python function \c range(\c start,\c stop,\c step).
8063  *  \param [in] a - the array to copy values from.
8064  *  \param [in] bgTuples - index of the first tuple of \a this array to assign values to.
8065  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
8066  *              are located.
8067  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
8068  *  \param [in] bgComp - index of the first component of \a this array to assign values to.
8069  *  \param [in] endComp - index of the component before which the components to assign
8070  *              to are located.
8071  *  \param [in] stepComp - index increment to get index of the next component to assign to.
8072  *  \param [in] strictCompoCompare - if \a true (by default), then \a a->getNumberOfComponents() 
8073  *              must be equal to the number of columns to assign to, else an
8074  *              exception is thrown; if \a false, then it is only required that \a
8075  *              a->getNbOfElems() equals to number of values to assign to (this condition
8076  *              must be respected even if \a strictCompoCompare is \a true). The number of 
8077  *              values to assign to is given by following Python expression:
8078  *              \a nbTargetValues = 
8079  *              \c len(\c range(\a bgTuples,\a endTuples,\a stepTuples)) *
8080  *              \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
8081  *  \throw If \a a is NULL.
8082  *  \throw If \a a is not allocated.
8083  *  \throw If \a this is not allocated.
8084  *  \throw If parameters specifying tuples and components to assign to do not give a
8085  *            non-empty range of increasing indices.
8086  *  \throw If \a a->getNbOfElems() != \a nbTargetValues.
8087  *  \throw If \a strictCompoCompare == \a true && \a a->getNumberOfComponents() !=
8088  *            \c len(\c range(\a bgComp,\a endComp,\a stepComp)).
8089  *
8090  *  \if ENABLE_EXAMPLES
8091  *  \ref py_mcdataarrayint_setpartofvalues1 "Here is a Python example".
8092  *  \endif
8093  */
8094 void DataArrayInt::setPartOfValues1(const DataArrayInt *a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
8095 {
8096   if(!a)
8097     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues1 : DataArrayInt pointer in input is NULL !");
8098   const char msg[]="DataArrayInt::setPartOfValues1";
8099   checkAllocated();
8100   a->checkAllocated();
8101   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
8102   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
8103   int nbComp=getNumberOfComponents();
8104   int nbOfTuples=getNumberOfTuples();
8105   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
8106   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
8107   bool assignTech=true;
8108   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
8109     {
8110       if(strictCompoCompare)
8111         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
8112     }
8113   else
8114     {
8115       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
8116       assignTech=false;
8117     }
8118   int *pt=getPointer()+bgTuples*nbComp+bgComp;
8119   const int *srcPt=a->getConstPointer();
8120   if(assignTech)
8121     {
8122       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8123         for(int j=0;j<newNbOfComp;j++,srcPt++)
8124           pt[j*stepComp]=*srcPt;
8125     }
8126   else
8127     {
8128       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8129         {
8130           const int *srcPt2=srcPt;
8131           for(int j=0;j<newNbOfComp;j++,srcPt2++)
8132             pt[j*stepComp]=*srcPt2;
8133         }
8134     }
8135 }
8136
8137 /*!
8138  * Assign a given value to values at specified tuples and components of \a this array.
8139  * The tree parameters defining set of indices of tuples and components are similar to
8140  * the tree parameters of the Python function \c range(\c start,\c stop,\c step)..
8141  *  \param [in] a - the value to assign.
8142  *  \param [in] bgTuples - index of the first tuple of \a this array to assign to.
8143  *  \param [in] endTuples - index of the tuple before which the tuples to assign to
8144  *              are located.
8145  *  \param [in] stepTuples - index increment to get index of the next tuple to assign to.
8146  *  \param [in] bgComp - index of the first component of \a this array to assign to.
8147  *  \param [in] endComp - index of the component before which the components to assign
8148  *              to are located.
8149  *  \param [in] stepComp - index increment to get index of the next component to assign to.
8150  *  \throw If \a this is not allocated.
8151  *  \throw If parameters specifying tuples and components to assign to, do not give a
8152  *            non-empty range of increasing indices or indices are out of a valid range
8153  *            for \c this array.
8154  *
8155  *  \if ENABLE_EXAMPLES
8156  *  \ref py_mcdataarrayint_setpartofvaluessimple1 "Here is a Python example".
8157  *  \endif
8158  */
8159 void DataArrayInt::setPartOfValuesSimple1(int a, int bgTuples, int endTuples, int stepTuples, int bgComp, int endComp, int stepComp)
8160 {
8161   const char msg[]="DataArrayInt::setPartOfValuesSimple1";
8162   checkAllocated();
8163   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
8164   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
8165   int nbComp=getNumberOfComponents();
8166   int nbOfTuples=getNumberOfTuples();
8167   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
8168   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
8169   int *pt=getPointer()+bgTuples*nbComp+bgComp;
8170   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8171     for(int j=0;j<newNbOfComp;j++)
8172       pt[j*stepComp]=a;
8173 }
8174
8175
8176 /*!
8177  * Copy all values from another DataArrayInt (\a a) into specified tuples and 
8178  * components of \a this array. Textual data is not copied.
8179  * The tuples and components to assign to are defined by C arrays of indices.
8180  * There are two *modes of usage*:
8181  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
8182  *   of \a a is assigned to its own location within \a this array. 
8183  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
8184  *   components of every specified tuple of \a this array. In this mode it is required
8185  *   that \a a->getNumberOfComponents() equals to the number of specified components.
8186  * 
8187  *  \param [in] a - the array to copy values from.
8188  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
8189  *              assign values of \a a to.
8190  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
8191  *              pointer to a tuple index <em>(pi)</em> varies as this: 
8192  *              \a bgTuples <= \a pi < \a endTuples.
8193  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
8194  *              assign values of \a a to.
8195  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
8196  *              pointer to a component index <em>(pi)</em> varies as this: 
8197  *              \a bgComp <= \a pi < \a endComp.
8198  *  \param [in] strictCompoCompare - this parameter is checked only if the
8199  *               *mode of usage* is the first; if it is \a true (default), 
8200  *               then \a a->getNumberOfComponents() must be equal 
8201  *               to the number of specified columns, else this is not required.
8202  *  \throw If \a a is NULL.
8203  *  \throw If \a a is not allocated.
8204  *  \throw If \a this is not allocated.
8205  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
8206  *         out of a valid range for \a this array.
8207  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
8208  *         if <em> a->getNumberOfComponents() != (endComp - bgComp) </em>.
8209  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
8210  *         <em> a->getNumberOfComponents() != (endComp - bgComp)</em>.
8211  *
8212  *  \if ENABLE_EXAMPLES
8213  *  \ref py_mcdataarrayint_setpartofvalues2 "Here is a Python example".
8214  *  \endif
8215  */
8216 void DataArrayInt::setPartOfValues2(const DataArrayInt *a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
8217 {
8218   if(!a)
8219     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues2 : DataArrayInt pointer in input is NULL !");
8220   const char msg[]="DataArrayInt::setPartOfValues2";
8221   checkAllocated();
8222   a->checkAllocated();
8223   int nbComp=getNumberOfComponents();
8224   int nbOfTuples=getNumberOfTuples();
8225   for(const int *z=bgComp;z!=endComp;z++)
8226     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
8227   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
8228   int newNbOfComp=(int)std::distance(bgComp,endComp);
8229   bool assignTech=true;
8230   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
8231     {
8232       if(strictCompoCompare)
8233         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
8234     }
8235   else
8236     {
8237       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
8238       assignTech=false;
8239     }
8240   int *pt=getPointer();
8241   const int *srcPt=a->getConstPointer();
8242   if(assignTech)
8243     {    
8244       for(const int *w=bgTuples;w!=endTuples;w++)
8245         {
8246           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8247           for(const int *z=bgComp;z!=endComp;z++,srcPt++)
8248             {    
8249               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt;
8250             }
8251         }
8252     }
8253   else
8254     {
8255       for(const int *w=bgTuples;w!=endTuples;w++)
8256         {
8257           const int *srcPt2=srcPt;
8258           DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8259           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
8260             {    
8261               pt[(std::size_t)(*w)*nbComp+(*z)]=*srcPt2;
8262             }
8263         }
8264     }
8265 }
8266
8267 /*!
8268  * Assign a given value to values at specified tuples and components of \a this array.
8269  * The tuples and components to assign to are defined by C arrays of indices.
8270  *  \param [in] a - the value to assign.
8271  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
8272  *              assign \a a to.
8273  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
8274  *              pointer to a tuple index (\a pi) varies as this: 
8275  *              \a bgTuples <= \a pi < \a endTuples.
8276  *  \param [in] bgComp - pointer to an array of component indices of \a this array to
8277  *              assign \a a to.
8278  *  \param [in] endComp - specifies the end of the array \a bgTuples, so that
8279  *              pointer to a component index (\a pi) varies as this: 
8280  *              \a bgComp <= \a pi < \a endComp.
8281  *  \throw If \a this is not allocated.
8282  *  \throw If any index of tuple/component given by <em>bgTuples / bgComp</em> is
8283  *         out of a valid range for \a this array.
8284  *
8285  *  \if ENABLE_EXAMPLES
8286  *  \ref py_mcdataarrayint_setpartofvaluessimple2 "Here is a Python example".
8287  *  \endif
8288  */
8289 void DataArrayInt::setPartOfValuesSimple2(int a, const int *bgTuples, const int *endTuples, const int *bgComp, const int *endComp)
8290 {
8291   checkAllocated();
8292   int nbComp=getNumberOfComponents();
8293   int nbOfTuples=getNumberOfTuples();
8294   for(const int *z=bgComp;z!=endComp;z++)
8295     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
8296   int *pt=getPointer();
8297   for(const int *w=bgTuples;w!=endTuples;w++)
8298     for(const int *z=bgComp;z!=endComp;z++)
8299       {
8300         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8301         pt[(std::size_t)(*w)*nbComp+(*z)]=a;
8302       }
8303 }
8304
8305 /*!
8306  * Copy all values from another DataArrayInt (\a a) into specified tuples and 
8307  * components of \a this array. Textual data is not copied.
8308  * The tuples to assign to are defined by a C array of indices.
8309  * The components to assign to are defined by three values similar to parameters of
8310  * the Python function \c range(\c start,\c stop,\c step).
8311  * There are two *modes of usage*:
8312  * - If \a a->getNbOfElems() equals to number of values to assign to, then every value
8313  *   of \a a is assigned to its own location within \a this array. 
8314  * - If \a a includes one tuple, then all values of \a a are assigned to the specified
8315  *   components of every specified tuple of \a this array. In this mode it is required
8316  *   that \a a->getNumberOfComponents() equals to the number of specified components.
8317  *
8318  *  \param [in] a - the array to copy values from.
8319  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
8320  *              assign values of \a a to.
8321  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
8322  *              pointer to a tuple index <em>(pi)</em> varies as this: 
8323  *              \a bgTuples <= \a pi < \a endTuples.
8324  *  \param [in] bgComp - index of the first component of \a this array to assign to.
8325  *  \param [in] endComp - index of the component before which the components to assign
8326  *              to are located.
8327  *  \param [in] stepComp - index increment to get index of the next component to assign to.
8328  *  \param [in] strictCompoCompare - this parameter is checked only in the first
8329  *               *mode of usage*; if \a strictCompoCompare is \a true (default), 
8330  *               then \a a->getNumberOfComponents() must be equal 
8331  *               to the number of specified columns, else this is not required.
8332  *  \throw If \a a is NULL.
8333  *  \throw If \a a is not allocated.
8334  *  \throw If \a this is not allocated.
8335  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
8336  *         \a this array.
8337  *  \throw In the first *mode of usage*, if <em>strictCompoCompare == true </em> and
8338  *         if <em> a->getNumberOfComponents()</em> is unequal to the number of components
8339  *         defined by <em>(bgComp,endComp,stepComp)</em>.
8340  *  \throw In the second *mode of usage*, if \a a->getNumberOfTuples() != 1 or
8341  *         <em> a->getNumberOfComponents()</em> is unequal to the number of components
8342  *         defined by <em>(bgComp,endComp,stepComp)</em>.
8343  *  \throw If parameters specifying components to assign to, do not give a
8344  *            non-empty range of increasing indices or indices are out of a valid range
8345  *            for \c this array.
8346  *
8347  *  \if ENABLE_EXAMPLES
8348  *  \ref py_mcdataarrayint_setpartofvalues3 "Here is a Python example".
8349  *  \endif
8350  */
8351 void DataArrayInt::setPartOfValues3(const DataArrayInt *a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare)
8352 {
8353   if(!a)
8354     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues3 : DataArrayInt pointer in input is NULL !");
8355   const char msg[]="DataArrayInt::setPartOfValues3";
8356   checkAllocated();
8357   a->checkAllocated();
8358   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
8359   int nbComp=getNumberOfComponents();
8360   int nbOfTuples=getNumberOfTuples();
8361   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
8362   int newNbOfTuples=(int)std::distance(bgTuples,endTuples);
8363   bool assignTech=true;
8364   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
8365     {
8366       if(strictCompoCompare)
8367         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
8368     }
8369   else
8370     {
8371       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
8372       assignTech=false;
8373     }
8374   int *pt=getPointer()+bgComp;
8375   const int *srcPt=a->getConstPointer();
8376   if(assignTech)
8377     {
8378       for(const int *w=bgTuples;w!=endTuples;w++)
8379         for(int j=0;j<newNbOfComp;j++,srcPt++)
8380           {
8381             DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8382             pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt;
8383           }
8384     }
8385   else
8386     {
8387       for(const int *w=bgTuples;w!=endTuples;w++)
8388         {
8389           const int *srcPt2=srcPt;
8390           for(int j=0;j<newNbOfComp;j++,srcPt2++)
8391             {
8392               DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8393               pt[(std::size_t)(*w)*nbComp+j*stepComp]=*srcPt2;
8394             }
8395         }
8396     }
8397 }
8398
8399 /*!
8400  * Assign a given value to values at specified tuples and components of \a this array.
8401  * The tuples to assign to are defined by a C array of indices.
8402  * The components to assign to are defined by three values similar to parameters of
8403  * the Python function \c range(\c start,\c stop,\c step).
8404  *  \param [in] a - the value to assign.
8405  *  \param [in] bgTuples - pointer to an array of tuple indices of \a this array to
8406  *              assign \a a to.
8407  *  \param [in] endTuples - specifies the end of the array \a bgTuples, so that
8408  *              pointer to a tuple index <em>(pi)</em> varies as this: 
8409  *              \a bgTuples <= \a pi < \a endTuples.
8410  *  \param [in] bgComp - index of the first component of \a this array to assign to.
8411  *  \param [in] endComp - index of the component before which the components to assign
8412  *              to are located.
8413  *  \param [in] stepComp - index increment to get index of the next component to assign to.
8414  *  \throw If \a this is not allocated.
8415  *  \throw If any index of tuple given by \a bgTuples is out of a valid range for 
8416  *         \a this array.
8417  *  \throw If parameters specifying components to assign to, do not give a
8418  *            non-empty range of increasing indices or indices are out of a valid range
8419  *            for \c this array.
8420  *
8421  *  \if ENABLE_EXAMPLES
8422  *  \ref py_mcdataarrayint_setpartofvaluessimple3 "Here is a Python example".
8423  *  \endif
8424  */
8425 void DataArrayInt::setPartOfValuesSimple3(int a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp)
8426 {
8427   const char msg[]="DataArrayInt::setPartOfValuesSimple3";
8428   checkAllocated();
8429   int newNbOfComp=DataArray::GetNumberOfItemGivenBES(bgComp,endComp,stepComp,msg);
8430   int nbComp=getNumberOfComponents();
8431   int nbOfTuples=getNumberOfTuples();
8432   DataArray::CheckValueInRangeEx(nbComp,bgComp,endComp,"invalid component value");
8433   int *pt=getPointer()+bgComp;
8434   for(const int *w=bgTuples;w!=endTuples;w++)
8435     for(int j=0;j<newNbOfComp;j++)
8436       {
8437         DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id");
8438         pt[(std::size_t)(*w)*nbComp+j*stepComp]=a;
8439       }
8440 }
8441
8442 void DataArrayInt::setPartOfValues4(const DataArrayInt *a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp, bool strictCompoCompare)
8443 {
8444   if(!a)
8445     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValues4 : input DataArrayInt is NULL !");
8446   const char msg[]="DataArrayInt::setPartOfValues4";
8447   checkAllocated();
8448   a->checkAllocated();
8449   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
8450   int newNbOfComp=(int)std::distance(bgComp,endComp);
8451   int nbComp=getNumberOfComponents();
8452   for(const int *z=bgComp;z!=endComp;z++)
8453     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
8454   int nbOfTuples=getNumberOfTuples();
8455   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
8456   bool assignTech=true;
8457   if(a->getNbOfElems()==(std::size_t)newNbOfTuples*newNbOfComp)
8458     {
8459       if(strictCompoCompare)
8460         a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg);
8461     }
8462   else
8463     {
8464       a->checkNbOfTuplesAndComp(1,newNbOfComp,msg);
8465       assignTech=false;
8466     }
8467   const int *srcPt=a->getConstPointer();
8468   int *pt=getPointer()+bgTuples*nbComp;
8469   if(assignTech)
8470     {
8471       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8472         for(const int *z=bgComp;z!=endComp;z++,srcPt++)
8473           pt[*z]=*srcPt;
8474     }
8475   else
8476     {
8477       for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8478         {
8479           const int *srcPt2=srcPt;
8480           for(const int *z=bgComp;z!=endComp;z++,srcPt2++)
8481             pt[*z]=*srcPt2;
8482         }
8483     }
8484 }
8485
8486 void DataArrayInt::setPartOfValuesSimple4(int a, int bgTuples, int endTuples, int stepTuples, const int *bgComp, const int *endComp)
8487 {
8488   const char msg[]="DataArrayInt::setPartOfValuesSimple4";
8489   checkAllocated();
8490   int newNbOfTuples=DataArray::GetNumberOfItemGivenBES(bgTuples,endTuples,stepTuples,msg);
8491   int nbComp=getNumberOfComponents();
8492   for(const int *z=bgComp;z!=endComp;z++)
8493     DataArray::CheckValueInRange(nbComp,*z,"invalid component id");
8494   int nbOfTuples=getNumberOfTuples();
8495   DataArray::CheckValueInRangeEx(nbOfTuples,bgTuples,endTuples,"invalid tuple value");
8496   int *pt=getPointer()+bgTuples*nbComp;
8497   for(int i=0;i<newNbOfTuples;i++,pt+=stepTuples*nbComp)
8498     for(const int *z=bgComp;z!=endComp;z++)
8499       pt[*z]=a;
8500 }
8501
8502 /*!
8503  * Copy some tuples from another DataArrayInt into specified tuples
8504  * of \a this array. Textual data is not copied. Both arrays must have equal number of
8505  * components.
8506  * Both the tuples to assign and the tuples to assign to are defined by a DataArrayInt.
8507  * All components of selected tuples are copied.
8508  *  \param [in] a - the array to copy values from.
8509  *  \param [in] tuplesSelec - the array specifying both source tuples of \a a and
8510  *              target tuples of \a this. \a tuplesSelec has two components, and the
8511  *              first component specifies index of the source tuple and the second
8512  *              one specifies index of the target tuple.
8513  *  \throw If \a this is not allocated.
8514  *  \throw If \a a is NULL.
8515  *  \throw If \a a is not allocated.
8516  *  \throw If \a tuplesSelec is NULL.
8517  *  \throw If \a tuplesSelec is not allocated.
8518  *  \throw If <em>this->getNumberOfComponents() != a->getNumberOfComponents()</em>.
8519  *  \throw If \a tuplesSelec->getNumberOfComponents() != 2.
8520  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
8521  *         the corresponding (\a this or \a a) array.
8522  */
8523 void DataArrayInt::setPartOfValuesAdv(const DataArrayInt *a, const DataArrayInt *tuplesSelec)
8524 {
8525   if(!a || !tuplesSelec)
8526     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValuesAdv : DataArrayInt pointer in input is NULL !");
8527   checkAllocated();
8528   a->checkAllocated();
8529   tuplesSelec->checkAllocated();
8530   int nbOfComp=getNumberOfComponents();
8531   if(nbOfComp!=a->getNumberOfComponents())
8532     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValuesAdv : This and a do not have the same number of components !");
8533   if(tuplesSelec->getNumberOfComponents()!=2)
8534     throw INTERP_KERNEL::Exception("DataArrayInt::setPartOfValuesAdv : Expecting to have a tuple selector DataArrayInt instance with exactly 2 components !");
8535   int thisNt=getNumberOfTuples();
8536   int aNt=a->getNumberOfTuples();
8537   int *valsToSet=getPointer();
8538   const int *valsSrc=a->getConstPointer();
8539   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple+=2)
8540     {
8541       if(tuple[1]>=0 && tuple[1]<aNt)
8542         {
8543           if(tuple[0]>=0 && tuple[0]<thisNt)
8544             std::copy(valsSrc+nbOfComp*tuple[1],valsSrc+nbOfComp*(tuple[1]+1),valsToSet+nbOfComp*tuple[0]);
8545           else
8546             {
8547               std::ostringstream oss; oss << "DataArrayInt::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
8548               oss << " of 'tuplesSelec' request of tuple id #" << tuple[0] << " in 'this' ! It should be in [0," << thisNt << ") !";
8549               throw INTERP_KERNEL::Exception(oss.str().c_str());
8550             }
8551         }
8552       else
8553         {
8554           std::ostringstream oss; oss << "DataArrayInt::setPartOfValuesAdv : Tuple #" << std::distance(tuplesSelec->begin(),tuple)/2;
8555           oss << " of 'tuplesSelec' request of tuple id #" << tuple[1] << " in 'a' ! It should be in [0," << aNt << ") !";
8556           throw INTERP_KERNEL::Exception(oss.str().c_str());
8557         }
8558     }
8559 }
8560
8561 /*!
8562  * Copy some tuples from another DataArrayInt (\a aBase) into contiguous tuples
8563  * of \a this array. Textual data is not copied. Both arrays must have equal number of
8564  * components.
8565  * The tuples to assign to are defined by index of the first tuple, and
8566  * their number is defined by \a tuplesSelec->getNumberOfTuples().
8567  * The tuples to copy are defined by values of a DataArrayInt.
8568  * All components of selected tuples are copied.
8569  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
8570  *              values to.
8571  *  \param [in] aBase - the array to copy values from.
8572  *  \param [in] tuplesSelec - the array specifying tuples of \a aBase to copy.
8573  *  \throw If \a this is not allocated.
8574  *  \throw If \a aBase is NULL.
8575  *  \throw If \a aBase is not allocated.
8576  *  \throw If \a tuplesSelec is NULL.
8577  *  \throw If \a tuplesSelec is not allocated.
8578  *  \throw If <em>this->getNumberOfComponents() != a->getNumberOfComponents()</em>.
8579  *  \throw If \a tuplesSelec->getNumberOfComponents() != 1.
8580  *  \throw If <em>tupleIdStart + tuplesSelec->getNumberOfTuples() > this->getNumberOfTuples().</em>
8581  *  \throw If any tuple index given by \a tuplesSelec is out of a valid range for 
8582  *         \a aBase array.
8583  */
8584 void DataArrayInt::setContigPartOfSelectedValues(int tupleIdStart, const DataArray *aBase, const DataArrayInt *tuplesSelec)
8585 {
8586   if(!aBase || !tuplesSelec)
8587     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : input DataArray is NULL !");
8588   const DataArrayInt *a=dynamic_cast<const DataArrayInt *>(aBase);
8589   if(!a)
8590     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : input DataArray aBase is not a DataArrayInt !");
8591   checkAllocated();
8592   a->checkAllocated();
8593   tuplesSelec->checkAllocated();
8594   int nbOfComp=getNumberOfComponents();
8595   if(nbOfComp!=a->getNumberOfComponents())
8596     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : This and a do not have the same number of components !");
8597   if(tuplesSelec->getNumberOfComponents()!=1)
8598     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : Expecting to have a tuple selector DataArrayInt instance with exactly 1 component !");
8599   int thisNt=getNumberOfTuples();
8600   int aNt=a->getNumberOfTuples();
8601   int nbOfTupleToWrite=tuplesSelec->getNumberOfTuples();
8602   int *valsToSet=getPointer()+tupleIdStart*nbOfComp;
8603   if(tupleIdStart+nbOfTupleToWrite>thisNt)
8604     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : invalid number range of values to write !");
8605   const int *valsSrc=a->getConstPointer();
8606   for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple++,valsToSet+=nbOfComp)
8607     {
8608       if(*tuple>=0 && *tuple<aNt)
8609         {
8610           std::copy(valsSrc+nbOfComp*(*tuple),valsSrc+nbOfComp*(*tuple+1),valsToSet);
8611         }
8612       else
8613         {
8614           std::ostringstream oss; oss << "DataArrayInt::setContigPartOfSelectedValues : Tuple #" << std::distance(tuplesSelec->begin(),tuple);
8615           oss << " of 'tuplesSelec' request of tuple id #" << *tuple << " in 'a' ! It should be in [0," << aNt << ") !";
8616           throw INTERP_KERNEL::Exception(oss.str().c_str());
8617         }
8618     }
8619 }
8620
8621 /*!
8622  * Copy some tuples from another DataArrayInt (\a aBase) into contiguous tuples
8623  * of \a this array. Textual data is not copied. Both arrays must have equal number of
8624  * components.
8625  * The tuples to copy are defined by three values similar to parameters of
8626  * the Python function \c range(\c start,\c stop,\c step).
8627  * The tuples to assign to are defined by index of the first tuple, and
8628  * their number is defined by number of tuples to copy.
8629  * All components of selected tuples are copied.
8630  *  \param [in] tupleIdStart - index of the first tuple of \a this array to assign
8631  *              values to.
8632  *  \param [in] aBase - the array to copy values from.
8633  *  \param [in] bg - index of the first tuple to copy of the array \a aBase.
8634  *  \param [in] end2 - index of the tuple of \a aBase before which the tuples to copy
8635  *              are located.
8636  *  \param [in] step - index increment to get index of the next tuple to copy.
8637  *  \throw If \a this is not allocated.
8638  *  \throw If \a aBase is NULL.
8639  *  \throw If \a aBase is not allocated.
8640  *  \throw If <em>this->getNumberOfComponents() != aBase->getNumberOfComponents()</em>.
8641  *  \throw If <em>tupleIdStart + len(range(bg,end2,step)) > this->getNumberOfTuples().</em>
8642  *  \throw If parameters specifying tuples to copy, do not give a
8643  *            non-empty range of increasing indices or indices are out of a valid range
8644  *            for the array \a aBase.
8645  */
8646 void DataArrayInt::setContigPartOfSelectedValues2(int tupleIdStart, const DataArray *aBase, int bg, int end2, int step)
8647 {
8648   if(!aBase)
8649     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : input DataArray is NULL !");
8650   const DataArrayInt *a=dynamic_cast<const DataArrayInt *>(aBase);
8651   if(!a)
8652     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : input DataArray aBase is not a DataArrayInt !");
8653   checkAllocated();
8654   a->checkAllocated();
8655   int nbOfComp=getNumberOfComponents();
8656   const char msg[]="DataArrayInt::setContigPartOfSelectedValues2";
8657   int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg);
8658   if(nbOfComp!=a->getNumberOfComponents())
8659     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : This and a do not have the same number of components !");
8660   int thisNt=getNumberOfTuples();
8661   int aNt=a->getNumberOfTuples();
8662   int *valsToSet=getPointer()+tupleIdStart*nbOfComp;
8663   if(tupleIdStart+nbOfTupleToWrite>thisNt)
8664     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid number range of values to write !");
8665   if(end2>aNt)
8666     throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid range of values to read !");
8667   const int *valsSrc=a->getConstPointer()+bg*nbOfComp;
8668   for(int i=0;i<nbOfTupleToWrite;i++,valsToSet+=nbOfComp,valsSrc+=step*nbOfComp)
8669     {
8670       std::copy(valsSrc,valsSrc+nbOfComp,valsToSet);
8671     }
8672 }
8673
8674 /*!
8675  * Returns a value located at specified tuple and component.
8676  * This method is equivalent to DataArrayInt::getIJ() except that validity of
8677  * parameters is checked. So this method is safe but expensive if used to go through
8678  * all values of \a this.
8679  *  \param [in] tupleId - index of tuple of interest.
8680  *  \param [in] compoId - index of component of interest.
8681  *  \return double - value located by \a tupleId and \a compoId.
8682  *  \throw If \a this is not allocated.
8683  *  \throw If condition <em>( 0 <= tupleId < this->getNumberOfTuples() )</em> is violated.
8684  *  \throw If condition <em>( 0 <= compoId < this->getNumberOfComponents() )</em> is violated.
8685  */
8686 int DataArrayInt::getIJSafe(int tupleId, int compoId) const
8687 {
8688   checkAllocated();
8689   if(tupleId<0 || tupleId>=getNumberOfTuples())
8690     {
8691       std::ostringstream oss; oss << "DataArrayInt::getIJSafe : request for tupleId " << tupleId << " should be in [0," << getNumberOfTuples() << ") !";
8692       throw INTERP_KERNEL::Exception(oss.str().c_str());
8693     }
8694   if(compoId<0 || compoId>=getNumberOfComponents())
8695     {
8696       std::ostringstream oss; oss << "DataArrayInt::getIJSafe : request for compoId " << compoId << " should be in [0," << getNumberOfComponents() << ") !";
8697       throw INTERP_KERNEL::Exception(oss.str().c_str());
8698     }
8699   return _mem[tupleId*_info_on_compo.size()+compoId];
8700 }
8701
8702 /*!
8703  * Returns the first value of \a this. 
8704  *  \return int - the last value of \a this array.
8705  *  \throw If \a this is not allocated.
8706  *  \throw If \a this->getNumberOfComponents() != 1.
8707  *  \throw If \a this->getNumberOfTuples() < 1.
8708  */
8709 int DataArrayInt::front() const
8710 {
8711   checkAllocated();
8712   if(getNumberOfComponents()!=1)
8713     throw INTERP_KERNEL::Exception("DataArrayInt::front : number of components not equal to one !");
8714   int nbOfTuples=getNumberOfTuples();
8715   if(nbOfTuples<1)
8716     throw INTERP_KERNEL::Exception("DataArrayInt::front : number of tuples must be >= 1 !");
8717   return *(getConstPointer());
8718 }
8719
8720 /*!
8721  * Returns the last value of \a this. 
8722  *  \return int - the last value of \a this array.
8723  *  \throw If \a this is not allocated.
8724  *  \throw If \a this->getNumberOfComponents() != 1.
8725  *  \throw If \a this->getNumberOfTuples() < 1.
8726  */
8727 int DataArrayInt::back() const
8728 {
8729   checkAllocated();
8730   if(getNumberOfComponents()!=1)
8731     throw INTERP_KERNEL::Exception("DataArrayInt::back : number of components not equal to one !");
8732   int nbOfTuples=getNumberOfTuples();
8733   if(nbOfTuples<1)
8734     throw INTERP_KERNEL::Exception("DataArrayInt::back : number of tuples must be >= 1 !");
8735   return *(getConstPointer()+nbOfTuples-1);
8736 }
8737
8738 /*!
8739  * Assign pointer to one array to a pointer to another appay. Reference counter of
8740  * \a arrayToSet is incremented / decremented.
8741  *  \param [in] newArray - the pointer to array to assign to \a arrayToSet.
8742  *  \param [in,out] arrayToSet - the pointer to array to assign to.
8743  */
8744 void DataArrayInt::SetArrayIn(DataArrayInt *newArray, DataArrayInt* &arrayToSet)
8745 {
8746   if(newArray!=arrayToSet)
8747     {
8748       if(arrayToSet)
8749         arrayToSet->decrRef();
8750       arrayToSet=newArray;
8751       if(arrayToSet)
8752         arrayToSet->incrRef();
8753     }
8754 }
8755
8756 DataArrayIntIterator *DataArrayInt::iterator()
8757 {
8758   return new DataArrayIntIterator(this);
8759 }
8760
8761 /*!
8762  * Creates a new DataArrayInt containing IDs (indices) of tuples holding value equal to a
8763  * given one. The ids are sorted in the ascending order.
8764  *  \param [in] val - the value to find within \a this.
8765  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8766  *          array using decrRef() as it is no more needed.
8767  *  \throw If \a this is not allocated.
8768  *  \throw If \a this->getNumberOfComponents() != 1.
8769  *  \sa DataArrayInt::getIdsEqualTuple
8770  */
8771 DataArrayInt *DataArrayInt::getIdsEqual(int val) const
8772 {
8773   checkAllocated();
8774   if(getNumberOfComponents()!=1)
8775     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqual : the array must have only one component, you can call 'rearrange' method before !");
8776   const int *cptr(getConstPointer());
8777   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8778   int nbOfTuples=getNumberOfTuples();
8779   for(int i=0;i<nbOfTuples;i++,cptr++)
8780     if(*cptr==val)
8781       ret->pushBackSilent(i);
8782   return ret.retn();
8783 }
8784
8785 /*!
8786  * Creates a new DataArrayInt containing IDs (indices) of tuples holding value \b not
8787  * equal to a given one. 
8788  *  \param [in] val - the value to ignore within \a this.
8789  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8790  *          array using decrRef() as it is no more needed.
8791  *  \throw If \a this is not allocated.
8792  *  \throw If \a this->getNumberOfComponents() != 1.
8793  */
8794 DataArrayInt *DataArrayInt::getIdsNotEqual(int val) const
8795 {
8796   checkAllocated();
8797   if(getNumberOfComponents()!=1)
8798     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsNotEqual : the array must have only one component, you can call 'rearrange' method before !");
8799   const int *cptr(getConstPointer());
8800   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8801   int nbOfTuples=getNumberOfTuples();
8802   for(int i=0;i<nbOfTuples;i++,cptr++)
8803     if(*cptr!=val)
8804       ret->pushBackSilent(i);
8805   return ret.retn();
8806 }
8807
8808 /*!
8809  * Creates a new DataArrayInt containing IDs (indices) of tuples holding tuple equal to those defined by [ \a tupleBg , \a tupleEnd )
8810  * This method is an extension of  DataArrayInt::getIdsEqual method.
8811  *
8812  *  \param [in] tupleBg - the begin (included) of the input tuple to find within \a this.
8813  *  \param [in] tupleEnd - the end (excluded) of the input tuple to find within \a this.
8814  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8815  *          array using decrRef() as it is no more needed.
8816  *  \throw If \a this is not allocated.
8817  *  \throw If \a this->getNumberOfComponents() != std::distance(tupleBg,tupleEnd).
8818  * \throw If \a this->getNumberOfComponents() is equal to 0.
8819  * \sa DataArrayInt::getIdsEqual
8820  */
8821 DataArrayInt *DataArrayInt::getIdsEqualTuple(const int *tupleBg, const int *tupleEnd) const
8822 {
8823   std::size_t nbOfCompoExp(std::distance(tupleBg,tupleEnd));
8824   checkAllocated();
8825   if(getNumberOfComponents()!=(int)nbOfCompoExp)
8826     {
8827       std::ostringstream oss; oss << "DataArrayInt::getIdsEqualTuple : mismatch of number of components. Input tuple has " << nbOfCompoExp << " whereas this array has " << getNumberOfComponents() << " components !";
8828       throw INTERP_KERNEL::Exception(oss.str().c_str());
8829     }
8830   if(nbOfCompoExp==0)
8831     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqualTuple : number of components should be > 0 !");
8832   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8833   const int *bg(begin()),*end2(end()),*work(begin());
8834   while(work!=end2)
8835     {
8836       work=std::search(work,end2,tupleBg,tupleEnd);
8837       if(work!=end2)
8838         {
8839           std::size_t pos(std::distance(bg,work));
8840           if(pos%nbOfCompoExp==0)
8841             ret->pushBackSilent(pos/nbOfCompoExp);
8842           work++;
8843         }
8844     }
8845   return ret.retn();
8846 }
8847
8848 /*!
8849  * Assigns \a newValue to all elements holding \a oldValue within \a this
8850  * one-dimensional array.
8851  *  \param [in] oldValue - the value to replace.
8852  *  \param [in] newValue - the value to assign.
8853  *  \return int - number of replacements performed.
8854  *  \throw If \a this is not allocated.
8855  *  \throw If \a this->getNumberOfComponents() != 1.
8856  */
8857 int DataArrayInt::changeValue(int oldValue, int newValue)
8858 {
8859   checkAllocated();
8860   if(getNumberOfComponents()!=1)
8861     throw INTERP_KERNEL::Exception("DataArrayInt::changeValue : the array must have only one component, you can call 'rearrange' method before !");
8862   int *start=getPointer();
8863   int *end2=start+getNbOfElems();
8864   int ret=0;
8865   for(int *val=start;val!=end2;val++)
8866     {
8867       if(*val==oldValue)
8868         {
8869           *val=newValue;
8870           ret++;
8871         }
8872     }
8873   return ret;
8874 }
8875
8876 /*!
8877  * Creates a new DataArrayInt containing IDs (indices) of tuples holding value equal to
8878  * one of given values.
8879  *  \param [in] valsBg - an array of values to find within \a this array.
8880  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
8881  *              the last value of \a valsBg is \a valsEnd[ -1 ].
8882  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8883  *          array using decrRef() as it is no more needed.
8884  *  \throw If \a this->getNumberOfComponents() != 1.
8885  */
8886 DataArrayInt *DataArrayInt::getIdsEqualList(const int *valsBg, const int *valsEnd) const
8887 {
8888   if(getNumberOfComponents()!=1)
8889     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqualList : the array must have only one component, you can call 'rearrange' method before !");
8890   std::set<int> vals2(valsBg,valsEnd);
8891   const int *cptr=getConstPointer();
8892   std::vector<int> res;
8893   int nbOfTuples=getNumberOfTuples();
8894   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8895   for(int i=0;i<nbOfTuples;i++,cptr++)
8896     if(vals2.find(*cptr)!=vals2.end())
8897       ret->pushBackSilent(i);
8898   return ret.retn();
8899 }
8900
8901 /*!
8902  * Creates a new DataArrayInt containing IDs (indices) of tuples holding values \b not
8903  * equal to any of given values.
8904  *  \param [in] valsBg - an array of values to ignore within \a this array.
8905  *  \param [in] valsEnd - specifies the end of the array \a valsBg, so that
8906  *              the last value of \a valsBg is \a valsEnd[ -1 ].
8907  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
8908  *          array using decrRef() as it is no more needed.
8909  *  \throw If \a this->getNumberOfComponents() != 1.
8910  */
8911 DataArrayInt *DataArrayInt::getIdsNotEqualList(const int *valsBg, const int *valsEnd) const
8912 {
8913   if(getNumberOfComponents()!=1)
8914     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsNotEqualList : the array must have only one component, you can call 'rearrange' method before !");
8915   std::set<int> vals2(valsBg,valsEnd);
8916   const int *cptr=getConstPointer();
8917   std::vector<int> res;
8918   int nbOfTuples=getNumberOfTuples();
8919   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
8920   for(int i=0;i<nbOfTuples;i++,cptr++)
8921     if(vals2.find(*cptr)==vals2.end())
8922       ret->pushBackSilent(i);
8923   return ret.retn();
8924 }
8925
8926 /*!
8927  * This method is an extension of DataArrayInt::locateValue method because this method works for DataArrayInt with
8928  * any number of components excepted 0 (an INTERP_KERNEL::Exception is thrown in this case).
8929  * This method searches in \b this is there is a tuple that matched the input parameter \b tupl.
8930  * If any the tuple id is returned. If not -1 is returned.
8931  * 
8932  * This method throws an INTERP_KERNEL::Exception if the number of components in \b this mismatches with the size of
8933  * the input vector. An INTERP_KERNEL::Exception is thrown too if \b this is not allocated.
8934  *
8935  * \return tuple id where \b tupl is. -1 if no such tuple exists in \b this.
8936  * \sa DataArrayInt::search, DataArrayInt::presenceOfTuple.
8937  */
8938 int DataArrayInt::locateTuple(const std::vector<int>& tupl) const
8939 {
8940   checkAllocated();
8941   int nbOfCompo=getNumberOfComponents();
8942   if(nbOfCompo==0)
8943     throw INTERP_KERNEL::Exception("DataArrayInt::locateTuple : 0 components in 'this' !");
8944   if(nbOfCompo!=(int)tupl.size())
8945     {
8946       std::ostringstream oss; oss << "DataArrayInt::locateTuple : 'this' contains " << nbOfCompo << " components and searching for a tuple of length " << tupl.size() << " !";
8947       throw INTERP_KERNEL::Exception(oss.str().c_str());
8948     }
8949   const int *cptr=getConstPointer();
8950   std::size_t nbOfVals=getNbOfElems();
8951   for(const int *work=cptr;work!=cptr+nbOfVals;)
8952     {
8953       work=std::search(work,cptr+nbOfVals,tupl.begin(),tupl.end());
8954       if(work!=cptr+nbOfVals)
8955         {
8956           if(std::distance(cptr,work)%nbOfCompo!=0)
8957             work++;
8958           else
8959             return std::distance(cptr,work)/nbOfCompo;
8960         }
8961     }
8962   return -1;
8963 }
8964
8965 /*!
8966  * This method searches the sequence specified in input parameter \b vals in \b this.
8967  * This works only for DataArrayInt having number of components equal to one (if not an INTERP_KERNEL::Exception will be thrown).
8968  * This method differs from DataArrayInt::locateTuple in that the position is internal raw data is not considered here contrary to DataArrayInt::locateTuple.
8969  * \sa DataArrayInt::locateTuple
8970  */
8971 int DataArrayInt::search(const std::vector<int>& vals) const
8972 {
8973   checkAllocated();
8974   int nbOfCompo=getNumberOfComponents();
8975   if(nbOfCompo!=1)
8976     throw INTERP_KERNEL::Exception("DataArrayInt::search : works only for DataArrayInt instance with one component !");
8977   const int *cptr=getConstPointer();
8978   std::size_t nbOfVals=getNbOfElems();
8979   const int *loc=std::search(cptr,cptr+nbOfVals,vals.begin(),vals.end());
8980   if(loc!=cptr+nbOfVals)
8981     return std::distance(cptr,loc);
8982   return -1;
8983 }
8984
8985 /*!
8986  * This method expects to be called when number of components of this is equal to one.
8987  * This method returns the tuple id, if it exists, of the first tuple equal to \b value.
8988  * If not any tuple contains \b value -1 is returned.
8989  * \sa DataArrayInt::presenceOfValue
8990  */
8991 int DataArrayInt::locateValue(int value) const
8992 {
8993   checkAllocated();
8994   if(getNumberOfComponents()!=1)
8995     throw INTERP_KERNEL::Exception("DataArrayInt::presenceOfValue : the array must have only one component, you can call 'rearrange' method before !");
8996   const int *cptr=getConstPointer();
8997   int nbOfTuples=getNumberOfTuples();
8998   const int *ret=std::find(cptr,cptr+nbOfTuples,value);
8999   if(ret!=cptr+nbOfTuples)
9000     return std::distance(cptr,ret);
9001   return -1;
9002 }
9003
9004 /*!
9005  * This method expects to be called when number of components of this is equal to one.
9006  * This method returns the tuple id, if it exists, of the first tuple so that the value is contained in \b vals.
9007  * If not any tuple contains one of the values contained in 'vals' false is returned.
9008  * \sa DataArrayInt::presenceOfValue
9009  */
9010 int DataArrayInt::locateValue(const std::vector<int>& vals) const
9011 {
9012   checkAllocated();
9013   if(getNumberOfComponents()!=1)
9014     throw INTERP_KERNEL::Exception("DataArrayInt::presenceOfValue : the array must have only one component, you can call 'rearrange' method before !");
9015   std::set<int> vals2(vals.begin(),vals.end());
9016   const int *cptr=getConstPointer();
9017   int nbOfTuples=getNumberOfTuples();
9018   for(const int *w=cptr;w!=cptr+nbOfTuples;w++)
9019     if(vals2.find(*w)!=vals2.end())
9020       return std::distance(cptr,w);
9021   return -1;
9022 }
9023
9024 /*!
9025  * This method returns the number of values in \a this that are equals to input parameter \a value.
9026  * This method only works for single component array.
9027  *
9028  * \return a value in [ 0, \c this->getNumberOfTuples() )
9029  *
9030  * \throw If \a this is not allocated
9031  *
9032  */
9033 int DataArrayInt::count(int value) const
9034 {
9035   int ret=0;
9036   checkAllocated();
9037   if(getNumberOfComponents()!=1)
9038     throw INTERP_KERNEL::Exception("DataArrayInt::count : must be applied on DataArrayInt with only one component, you can call 'rearrange' method before !");
9039   const int *vals=begin();
9040   int nbOfTuples=getNumberOfTuples();
9041   for(int i=0;i<nbOfTuples;i++,vals++)
9042     if(*vals==value)
9043       ret++;
9044   return ret;
9045 }
9046
9047 /*!
9048  * This method is an extension of DataArrayInt::presenceOfValue method because this method works for DataArrayInt with
9049  * any number of components excepted 0 (an INTERP_KERNEL::Exception is thrown in this case).
9050  * This method searches in \b this is there is a tuple that matched the input parameter \b tupl.
9051  * This method throws an INTERP_KERNEL::Exception if the number of components in \b this mismatches with the size of
9052  * the input vector. An INTERP_KERNEL::Exception is thrown too if \b this is not allocated.
9053  * \sa DataArrayInt::locateTuple
9054  */
9055 bool DataArrayInt::presenceOfTuple(const std::vector<int>& tupl) const
9056 {
9057   return locateTuple(tupl)!=-1;
9058 }
9059
9060
9061 /*!
9062  * Returns \a true if a given value is present within \a this one-dimensional array.
9063  *  \param [in] value - the value to find within \a this array.
9064  *  \return bool - \a true in case if \a value is present within \a this array.
9065  *  \throw If \a this is not allocated.
9066  *  \throw If \a this->getNumberOfComponents() != 1.
9067  *  \sa locateValue()
9068  */
9069 bool DataArrayInt::presenceOfValue(int value) const
9070 {
9071   return locateValue(value)!=-1;
9072 }
9073
9074 /*!
9075  * This method expects to be called when number of components of this is equal to one.
9076  * This method returns true if it exists a tuple so that the value is contained in \b vals.
9077  * If not any tuple contains one of the values contained in 'vals' false is returned.
9078  * \sa DataArrayInt::locateValue
9079  */
9080 bool DataArrayInt::presenceOfValue(const std::vector<int>& vals) const
9081 {
9082   return locateValue(vals)!=-1;
9083 }
9084
9085 /*!
9086  * Accumulates values of each component of \a this array.
9087  *  \param [out] res - an array of length \a this->getNumberOfComponents(), allocated 
9088  *         by the caller, that is filled by this method with sum value for each
9089  *         component.
9090  *  \throw If \a this is not allocated.
9091  */
9092 void DataArrayInt::accumulate(int *res) const
9093 {
9094   checkAllocated();
9095   const int *ptr=getConstPointer();
9096   int nbTuple=getNumberOfTuples();
9097   int nbComps=getNumberOfComponents();
9098   std::fill(res,res+nbComps,0);
9099   for(int i=0;i<nbTuple;i++)
9100     std::transform(ptr+i*nbComps,ptr+(i+1)*nbComps,res,res,std::plus<int>());
9101 }
9102
9103 int DataArrayInt::accumulate(int compId) const
9104 {
9105   checkAllocated();
9106   const int *ptr=getConstPointer();
9107   int nbTuple=getNumberOfTuples();
9108   int nbComps=getNumberOfComponents();
9109   if(compId<0 || compId>=nbComps)
9110     throw INTERP_KERNEL::Exception("DataArrayInt::accumulate : Invalid compId specified : No such nb of components !");
9111   int ret=0;
9112   for(int i=0;i<nbTuple;i++)
9113     ret+=ptr[i*nbComps+compId];
9114   return ret;
9115 }
9116
9117 /*!
9118  * This method accumulate using addition tuples in \a this using input index array [ \a bgOfIndex, \a endOfIndex ).
9119  * The returned array will have same number of components than \a this and number of tuples equal to
9120  * \c std::distance(bgOfIndex,endOfIndex) \b minus \b one.
9121  *
9122  * The input index array is expected to be ascendingly sorted in which the all referenced ids should be in [0, \c this->getNumberOfTuples).
9123  *
9124  * \param [in] bgOfIndex - begin (included) of the input index array.
9125  * \param [in] endOfIndex - end (excluded) of the input index array.
9126  * \return DataArrayInt * - the new instance having the same number of components than \a this.
9127  * 
9128  * \throw If bgOfIndex or end is NULL.
9129  * \throw If input index array is not ascendingly sorted.
9130  * \throw If there is an id in [ \a bgOfIndex, \a endOfIndex ) not in [0, \c this->getNumberOfTuples).
9131  * \throw If std::distance(bgOfIndex,endOfIndex)==0.
9132  */
9133 DataArrayInt *DataArrayInt::accumulatePerChunck(const int *bgOfIndex, const int *endOfIndex) const
9134 {
9135   if(!bgOfIndex || !endOfIndex)
9136     throw INTERP_KERNEL::Exception("DataArrayInt::accumulatePerChunck : input pointer NULL !");
9137   checkAllocated();
9138   int nbCompo=getNumberOfComponents();
9139   int nbOfTuples=getNumberOfTuples();
9140   int sz=(int)std::distance(bgOfIndex,endOfIndex);
9141   if(sz<1)
9142     throw INTERP_KERNEL::Exception("DataArrayInt::accumulatePerChunck : invalid size of input index array !");
9143   sz--;
9144   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(sz,nbCompo);
9145   const int *w=bgOfIndex;
9146   if(*w<0 || *w>=nbOfTuples)
9147     throw INTERP_KERNEL::Exception("DataArrayInt::accumulatePerChunck : The first element of the input index not in [0,nbOfTuples) !");
9148   const int *srcPt=begin()+(*w)*nbCompo;
9149   int *tmp=ret->getPointer();
9150   for(int i=0;i<sz;i++,tmp+=nbCompo,w++)
9151     {
9152       std::fill(tmp,tmp+nbCompo,0);
9153       if(w[1]>=w[0])
9154         {
9155           for(int j=w[0];j<w[1];j++,srcPt+=nbCompo)
9156             {
9157               if(j>=0 && j<nbOfTuples)
9158                 std::transform(srcPt,srcPt+nbCompo,tmp,tmp,std::plus<int>());
9159               else
9160                 {
9161                   std::ostringstream oss; oss << "DataArrayInt::accumulatePerChunck : At rank #" << i << " the input index array points to id " << j << " should be in [0," << nbOfTuples << ") !";
9162                   throw INTERP_KERNEL::Exception(oss.str().c_str());
9163                 }
9164             }
9165         }
9166       else
9167         {
9168           std::ostringstream oss; oss << "DataArrayInt::accumulatePerChunck : At rank #" << i << " the input index array is not in ascendingly sorted.";
9169           throw INTERP_KERNEL::Exception(oss.str().c_str());
9170         }
9171     }
9172   ret->copyStringInfoFrom(*this);
9173   return ret.retn();
9174 }
9175
9176 /*!
9177  * Returns a new DataArrayInt by concatenating two given arrays, so that (1) the number
9178  * of tuples in the result array is <em> a1->getNumberOfTuples() + a2->getNumberOfTuples() -
9179  * offsetA2</em> and (2)
9180  * the number of component in the result array is same as that of each of given arrays.
9181  * First \a offsetA2 tuples of \a a2 are skipped and thus are missing from the result array.
9182  * Info on components is copied from the first of the given arrays. Number of components
9183  * in the given arrays must be the same.
9184  *  \param [in] a1 - an array to include in the result array.
9185  *  \param [in] a2 - another array to include in the result array.
9186  *  \param [in] offsetA2 - number of tuples of \a a2 to skip.
9187  *  \return DataArrayInt * - the new instance of DataArrayInt.
9188  *          The caller is to delete this result array using decrRef() as it is no more
9189  *          needed.
9190  *  \throw If either \a a1 or \a a2 is NULL.
9191  *  \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents().
9192  */
9193 DataArrayInt *DataArrayInt::Aggregate(const DataArrayInt *a1, const DataArrayInt *a2, int offsetA2)
9194 {
9195   if(!a1 || !a2)
9196     throw INTERP_KERNEL::Exception("DataArrayInt::Aggregate : input DataArrayInt instance is NULL !");
9197   int nbOfComp=a1->getNumberOfComponents();
9198   if(nbOfComp!=a2->getNumberOfComponents())
9199     throw INTERP_KERNEL::Exception("Nb of components mismatch for array Aggregation !");
9200   int nbOfTuple1=a1->getNumberOfTuples();
9201   int nbOfTuple2=a2->getNumberOfTuples();
9202   DataArrayInt *ret=DataArrayInt::New();
9203   ret->alloc(nbOfTuple1+nbOfTuple2-offsetA2,nbOfComp);
9204   int *pt=std::copy(a1->getConstPointer(),a1->getConstPointer()+nbOfTuple1*nbOfComp,ret->getPointer());
9205   std::copy(a2->getConstPointer()+offsetA2*nbOfComp,a2->getConstPointer()+nbOfTuple2*nbOfComp,pt);
9206   ret->copyStringInfoFrom(*a1);
9207   return ret;
9208 }
9209
9210 /*!
9211  * Returns a new DataArrayInt by concatenating all given arrays, so that (1) the number
9212  * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
9213  * the number of component in the result array is same as that of each of given arrays.
9214  * Info on components is copied from the first of the given arrays. Number of components
9215  * in the given arrays must be  the same.
9216  * If the number of non null of elements in \a arr is equal to one the returned object is a copy of it
9217  * not the object itself.
9218  *  \param [in] arr - a sequence of arrays to include in the result array.
9219  *  \return DataArrayInt * - the new instance of DataArrayInt.
9220  *          The caller is to delete this result array using decrRef() as it is no more
9221  *          needed.
9222  *  \throw If all arrays within \a arr are NULL.
9223  *  \throw If getNumberOfComponents() of arrays within \a arr.
9224  */
9225 DataArrayInt *DataArrayInt::Aggregate(const std::vector<const DataArrayInt *>& arr)
9226 {
9227   std::vector<const DataArrayInt *> a;
9228   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
9229     if(*it4)
9230       a.push_back(*it4);
9231   if(a.empty())
9232     throw INTERP_KERNEL::Exception("DataArrayInt::Aggregate : input list must be NON EMPTY !");
9233   std::vector<const DataArrayInt *>::const_iterator it=a.begin();
9234   int nbOfComp=(*it)->getNumberOfComponents();
9235   int nbt=(*it++)->getNumberOfTuples();
9236   for(int i=1;it!=a.end();it++,i++)
9237     {
9238       if((*it)->getNumberOfComponents()!=nbOfComp)
9239         throw INTERP_KERNEL::Exception("DataArrayInt::Aggregate : Nb of components mismatch for array aggregation !");
9240       nbt+=(*it)->getNumberOfTuples();
9241     }
9242   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
9243   ret->alloc(nbt,nbOfComp);
9244   int *pt=ret->getPointer();
9245   for(it=a.begin();it!=a.end();it++)
9246     pt=std::copy((*it)->getConstPointer(),(*it)->getConstPointer()+(*it)->getNbOfElems(),pt);
9247   ret->copyStringInfoFrom(*(a[0]));
9248   return ret.retn();
9249 }
9250
9251 /*!
9252  * This method takes as input a list of DataArrayInt instances \a arrs that represent each a packed index arrays.
9253  * A packed index array is an allocated array with one component, and at least one tuple. The first element
9254  * of each array in \a arrs must be 0. Each array in \a arrs is expected to be increasingly monotonic.
9255  * 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.
9256  * 
9257  * \return DataArrayInt * - a new object to be managed by the caller.
9258  */
9259 DataArrayInt *DataArrayInt::AggregateIndexes(const std::vector<const DataArrayInt *>& arrs)
9260 {
9261   int retSz=1;
9262   for(std::vector<const DataArrayInt *>::const_iterator it4=arrs.begin();it4!=arrs.end();it4++)
9263     {
9264       if(*it4)
9265         {
9266           (*it4)->checkAllocated();
9267           if((*it4)->getNumberOfComponents()!=1)
9268             {
9269               std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a DataArrayInt instance with nb of compo != 1 at pos " << std::distance(arrs.begin(),it4) << " !";
9270               throw INTERP_KERNEL::Exception(oss.str().c_str());
9271             }
9272           int nbTupl=(*it4)->getNumberOfTuples();
9273           if(nbTupl<1)
9274             {
9275               std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a DataArrayInt instance with nb of tuples < 1 at pos " << std::distance(arrs.begin(),it4) << " !";
9276               throw INTERP_KERNEL::Exception(oss.str().c_str());
9277             }
9278           if((*it4)->front()!=0)
9279             {
9280               std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a DataArrayInt instance with front value != 0 at pos " << std::distance(arrs.begin(),it4) << " !";
9281               throw INTERP_KERNEL::Exception(oss.str().c_str());
9282             }
9283           retSz+=nbTupl-1;
9284         }
9285       else
9286         {
9287           std::ostringstream oss; oss << "DataArrayInt::AggregateIndexes : presence of a null instance at pos " << std::distance(arrs.begin(),it4) << " !";
9288           throw INTERP_KERNEL::Exception(oss.str().c_str());
9289         }
9290     }
9291   if(arrs.empty())
9292     throw INTERP_KERNEL::Exception("DataArrayInt::AggregateIndexes : input list must be NON EMPTY !");
9293   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
9294   ret->alloc(retSz,1);
9295   int *pt=ret->getPointer(); *pt++=0;
9296   for(std::vector<const DataArrayInt *>::const_iterator it=arrs.begin();it!=arrs.end();it++)
9297     pt=std::transform((*it)->begin()+1,(*it)->end(),pt,std::bind2nd(std::plus<int>(),pt[-1]));
9298   ret->copyStringInfoFrom(*(arrs[0]));
9299   return ret.retn();
9300 }
9301
9302 /*!
9303  * Returns the maximal value and its location within \a this one-dimensional array.
9304  *  \param [out] tupleId - index of the tuple holding the maximal value.
9305  *  \return int - the maximal value among all values of \a this array.
9306  *  \throw If \a this->getNumberOfComponents() != 1
9307  *  \throw If \a this->getNumberOfTuples() < 1
9308  */
9309 int DataArrayInt::getMaxValue(int& tupleId) const
9310 {
9311   checkAllocated();
9312   if(getNumberOfComponents()!=1)
9313     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !");
9314   int nbOfTuples=getNumberOfTuples();
9315   if(nbOfTuples<=0)
9316     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !");
9317   const int *vals=getConstPointer();
9318   const int *loc=std::max_element(vals,vals+nbOfTuples);
9319   tupleId=(int)std::distance(vals,loc);
9320   return *loc;
9321 }
9322
9323 /*!
9324  * Returns the maximal value within \a this array that is allowed to have more than
9325  *  one component.
9326  *  \return int - the maximal value among all values of \a this array.
9327  *  \throw If \a this is not allocated.
9328  */
9329 int DataArrayInt::getMaxValueInArray() const
9330 {
9331   checkAllocated();
9332   const int *loc=std::max_element(begin(),end());
9333   return *loc;
9334 }
9335
9336 /*!
9337  * Returns the minimal value and its location within \a this one-dimensional array.
9338  *  \param [out] tupleId - index of the tuple holding the minimal value.
9339  *  \return int - the minimal value among all values of \a this array.
9340  *  \throw If \a this->getNumberOfComponents() != 1
9341  *  \throw If \a this->getNumberOfTuples() < 1
9342  */
9343 int DataArrayInt::getMinValue(int& tupleId) const
9344 {
9345   checkAllocated();
9346   if(getNumberOfComponents()!=1)
9347     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !");
9348   int nbOfTuples=getNumberOfTuples();
9349   if(nbOfTuples<=0)
9350     throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !");
9351   const int *vals=getConstPointer();
9352   const int *loc=std::min_element(vals,vals+nbOfTuples);
9353   tupleId=(int)std::distance(vals,loc);
9354   return *loc;
9355 }
9356
9357 /*!
9358  * Returns the minimal value within \a this array that is allowed to have more than
9359  *  one component.
9360  *  \return int - the minimal value among all values of \a this array.
9361  *  \throw If \a this is not allocated.
9362  */
9363 int DataArrayInt::getMinValueInArray() const
9364 {
9365   checkAllocated();
9366   const int *loc=std::min_element(begin(),end());
9367   return *loc;
9368 }
9369
9370 /*!
9371  * Returns in a single walk in \a this the min value and the max value in \a this.
9372  * \a this is expected to be single component array.
9373  *
9374  * \param [out] minValue - the min value in \a this.
9375  * \param [out] maxValue - the max value in \a this.
9376  *
9377  * \sa getMinValueInArray, getMinValue, getMaxValueInArray, getMaxValue
9378  */
9379 void DataArrayInt::getMinMaxValues(int& minValue, int& maxValue) const
9380 {
9381   checkAllocated();
9382   if(getNumberOfComponents()!=1)
9383     throw INTERP_KERNEL::Exception("DataArrayInt::getMinMaxValues : must be applied on DataArrayInt with only one component !");
9384   int nbTuples(getNumberOfTuples());
9385   const int *pt(begin());
9386   minValue=std::numeric_limits<int>::max(); maxValue=-std::numeric_limits<int>::max();
9387   for(int i=0;i<nbTuples;i++,pt++)
9388     {
9389       if(*pt<minValue)
9390         minValue=*pt;
9391       if(*pt>maxValue)
9392         maxValue=*pt;
9393     }
9394 }
9395
9396 /*!
9397  * Converts every value of \a this array to its absolute value.
9398  * \b WARNING this method is non const. If a new DataArrayInt instance should be built containing the result of abs DataArrayInt::computeAbs
9399  * should be called instead.
9400  *
9401  * \throw If \a this is not allocated.
9402  * \sa DataArrayInt::computeAbs
9403  */
9404 void DataArrayInt::abs()
9405 {
9406   checkAllocated();
9407   int *ptr(getPointer());
9408   std::size_t nbOfElems(getNbOfElems());
9409   std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun<int,int>(std::abs));
9410   declareAsNew();
9411 }
9412
9413 /*!
9414  * This method builds a new instance of \a this object containing the result of std::abs applied of all elements in \a this.
9415  * This method is a const method (that do not change any values in \a this) contrary to  DataArrayInt::abs method.
9416  *
9417  * \return DataArrayInt * - the new instance of DataArrayInt containing the
9418  *         same number of tuples and component as \a this array.
9419  *         The caller is to delete this result array using decrRef() as it is no more
9420  *         needed.
9421  * \throw If \a this is not allocated.
9422  * \sa DataArrayInt::abs
9423  */
9424 DataArrayInt *DataArrayInt::computeAbs() const
9425 {
9426   checkAllocated();
9427   DataArrayInt *newArr(DataArrayInt::New());
9428   int nbOfTuples(getNumberOfTuples());
9429   int nbOfComp(getNumberOfComponents());
9430   newArr->alloc(nbOfTuples,nbOfComp);
9431   std::transform(begin(),end(),newArr->getPointer(),std::ptr_fun<int,int>(std::abs));
9432   newArr->copyStringInfoFrom(*this);
9433   return newArr;
9434 }
9435
9436 /*!
9437  * Apply a liner function to a given component of \a this array, so that
9438  * an array element <em>(x)</em> becomes \f$ a * x + b \f$.
9439  *  \param [in] a - the first coefficient of the function.
9440  *  \param [in] b - the second coefficient of the function.
9441  *  \param [in] compoId - the index of component to modify.
9442  *  \throw If \a this is not allocated.
9443  */
9444 void DataArrayInt::applyLin(int a, int b, int compoId)
9445 {
9446   checkAllocated();
9447   int *ptr=getPointer()+compoId;
9448   int nbOfComp=getNumberOfComponents();
9449   int nbOfTuple=getNumberOfTuples();
9450   for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
9451     *ptr=a*(*ptr)+b;
9452   declareAsNew();
9453 }
9454
9455 /*!
9456  * Apply a liner function to all elements of \a this array, so that
9457  * an element _x_ becomes \f$ a * x + b \f$.
9458  *  \param [in] a - the first coefficient of the function.
9459  *  \param [in] b - the second coefficient of the function.
9460  *  \throw If \a this is not allocated.
9461  */
9462 void DataArrayInt::applyLin(int a, int b)
9463 {
9464   checkAllocated();
9465   int *ptr=getPointer();
9466   std::size_t nbOfElems=getNbOfElems();
9467   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9468     *ptr=a*(*ptr)+b;
9469   declareAsNew();
9470 }
9471
9472 /*!
9473  * Returns a full copy of \a this array except that sign of all elements is reversed.
9474  *  \return DataArrayInt * - the new instance of DataArrayInt containing the
9475  *          same number of tuples and component as \a this array.
9476  *          The caller is to delete this result array using decrRef() as it is no more
9477  *          needed.
9478  *  \throw If \a this is not allocated.
9479  */
9480 DataArrayInt *DataArrayInt::negate() const
9481 {
9482   checkAllocated();
9483   DataArrayInt *newArr=DataArrayInt::New();
9484   int nbOfTuples=getNumberOfTuples();
9485   int nbOfComp=getNumberOfComponents();
9486   newArr->alloc(nbOfTuples,nbOfComp);
9487   const int *cptr=getConstPointer();
9488   std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate<int>());
9489   newArr->copyStringInfoFrom(*this);
9490   return newArr;
9491 }
9492
9493 /*!
9494  * Modify all elements of \a this array, so that
9495  * an element _x_ becomes \f$ numerator / x \f$.
9496  *  \warning If an exception is thrown because of presence of 0 element in \a this 
9497  *           array, all elements processed before detection of the zero element remain
9498  *           modified.
9499  *  \param [in] numerator - the numerator used to modify array elements.
9500  *  \throw If \a this is not allocated.
9501  *  \throw If there is an element equal to 0 in \a this array.
9502  */
9503 void DataArrayInt::applyInv(int numerator)
9504 {
9505   checkAllocated();
9506   int *ptr=getPointer();
9507   std::size_t nbOfElems=getNbOfElems();
9508   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9509     {
9510       if(*ptr!=0)
9511         {
9512           *ptr=numerator/(*ptr);
9513         }
9514       else
9515         {
9516           std::ostringstream oss; oss << "DataArrayInt::applyInv : presence of null value in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
9517           oss << " !";
9518           throw INTERP_KERNEL::Exception(oss.str().c_str());
9519         }
9520     }
9521   declareAsNew();
9522 }
9523
9524 /*!
9525  * Modify all elements of \a this array, so that
9526  * an element _x_ becomes \f$ x / val \f$.
9527  *  \param [in] val - the denominator used to modify array elements.
9528  *  \throw If \a this is not allocated.
9529  *  \throw If \a val == 0.
9530  */
9531 void DataArrayInt::applyDivideBy(int val)
9532 {
9533   if(val==0)
9534     throw INTERP_KERNEL::Exception("DataArrayInt::applyDivideBy : Trying to divide by 0 !");
9535   checkAllocated();
9536   int *ptr=getPointer();
9537   std::size_t nbOfElems=getNbOfElems();
9538   std::transform(ptr,ptr+nbOfElems,ptr,std::bind2nd(std::divides<int>(),val));
9539   declareAsNew();
9540 }
9541
9542 /*!
9543  * Modify all elements of \a this array, so that
9544  * an element _x_ becomes  <em> x % val </em>.
9545  *  \param [in] val - the divisor used to modify array elements.
9546  *  \throw If \a this is not allocated.
9547  *  \throw If \a val <= 0.
9548  */
9549 void DataArrayInt::applyModulus(int val)
9550 {
9551   if(val<=0)
9552     throw INTERP_KERNEL::Exception("DataArrayInt::applyDivideBy : Trying to operate modulus on value <= 0 !");
9553   checkAllocated();
9554   int *ptr=getPointer();
9555   std::size_t nbOfElems=getNbOfElems();
9556   std::transform(ptr,ptr+nbOfElems,ptr,std::bind2nd(std::modulus<int>(),val));
9557   declareAsNew();
9558 }
9559
9560 /*!
9561  * This method works only on data array with one component.
9562  * This method returns a newly allocated array storing stored ascendantly tuple ids in \b this so that
9563  * this[*id] in [\b vmin,\b vmax)
9564  * 
9565  * \param [in] vmin begin of range. This value is included in range (included).
9566  * \param [in] vmax end of range. This value is \b not included in range (excluded).
9567  * \return a newly allocated data array that the caller should deal with.
9568  *
9569  * \sa DataArrayInt::getIdsNotInRange , DataArrayInt::getIdsStrictlyNegative
9570  */
9571 DataArrayInt *DataArrayInt::getIdsInRange(int vmin, int vmax) const
9572 {
9573   checkAllocated();
9574   if(getNumberOfComponents()!=1)
9575     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsInRange : this must have exactly one component !");
9576   const int *cptr(begin());
9577   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
9578   int nbOfTuples(getNumberOfTuples());
9579   for(int i=0;i<nbOfTuples;i++,cptr++)
9580     if(*cptr>=vmin && *cptr<vmax)
9581       ret->pushBackSilent(i);
9582   return ret.retn();
9583 }
9584
9585 /*!
9586  * This method works only on data array with one component.
9587  * This method returns a newly allocated array storing stored ascendantly tuple ids in \b this so that
9588  * this[*id] \b not in [\b vmin,\b vmax)
9589  * 
9590  * \param [in] vmin begin of range. This value is \b not included in range (excluded).
9591  * \param [in] vmax end of range. This value is included in range (included).
9592  * \return a newly allocated data array that the caller should deal with.
9593  * 
9594  * \sa DataArrayInt::getIdsInRange , DataArrayInt::getIdsStrictlyNegative
9595  */
9596 DataArrayInt *DataArrayInt::getIdsNotInRange(int vmin, int vmax) const
9597 {
9598   checkAllocated();
9599   if(getNumberOfComponents()!=1)
9600     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsNotInRange : this must have exactly one component !");
9601   const int *cptr(getConstPointer());
9602   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
9603   int nbOfTuples(getNumberOfTuples());
9604   for(int i=0;i<nbOfTuples;i++,cptr++)
9605     if(*cptr<vmin || *cptr>=vmax)
9606       ret->pushBackSilent(i);
9607   return ret.retn();
9608 }
9609
9610 /*!
9611  * This method works only on data array with one component. This method returns a newly allocated array storing stored ascendantly of tuple ids in \a this so that this[id]<0.
9612  *
9613  * \return a newly allocated data array that the caller should deal with.
9614  * \sa DataArrayInt::getIdsInRange
9615  */
9616 DataArrayInt *DataArrayInt::getIdsStrictlyNegative() const
9617 {
9618   checkAllocated();
9619   if(getNumberOfComponents()!=1)
9620     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsStrictlyNegative : this must have exactly one component !");
9621   const int *cptr(getConstPointer());
9622   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
9623   int nbOfTuples(getNumberOfTuples());
9624   for(int i=0;i<nbOfTuples;i++,cptr++)
9625     if(*cptr<0)
9626       ret->pushBackSilent(i);
9627   return ret.retn();
9628 }
9629
9630 /*!
9631  * This method works only on data array with one component.
9632  * 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.
9633  * 
9634  * \param [in] vmin begin of range. This value is included in range (included).
9635  * \param [in] vmax end of range. This value is \b not included in range (excluded).
9636  * \return if all ids in \a this are so that (*this)[i]==i for all i in [ 0, \c this->getNumberOfTuples() ). */
9637 bool DataArrayInt::checkAllIdsInRange(int vmin, int vmax) const
9638 {
9639   checkAllocated();
9640   if(getNumberOfComponents()!=1)
9641     throw INTERP_KERNEL::Exception("DataArrayInt::checkAllIdsInRange : this must have exactly one component !");
9642   int nbOfTuples=getNumberOfTuples();
9643   bool ret=true;
9644   const int *cptr=getConstPointer();
9645   for(int i=0;i<nbOfTuples;i++,cptr++)
9646     {
9647       if(*cptr>=vmin && *cptr<vmax)
9648         { ret=ret && *cptr==i; }
9649       else
9650         {
9651           std::ostringstream oss; oss << "DataArrayInt::checkAllIdsInRange : tuple #" << i << " has value " << *cptr << " should be in [" << vmin << "," << vmax << ") !";
9652           throw INTERP_KERNEL::Exception(oss.str().c_str());
9653         }
9654     }
9655   return ret;
9656 }
9657
9658 /*!
9659  * Modify all elements of \a this array, so that
9660  * an element _x_ becomes <em> val % x </em>.
9661  *  \warning If an exception is thrown because of presence of an element <= 0 in \a this 
9662  *           array, all elements processed before detection of the zero element remain
9663  *           modified.
9664  *  \param [in] val - the divident used to modify array elements.
9665  *  \throw If \a this is not allocated.
9666  *  \throw If there is an element equal to or less than 0 in \a this array.
9667  */
9668 void DataArrayInt::applyRModulus(int val)
9669 {
9670   checkAllocated();
9671   int *ptr=getPointer();
9672   std::size_t nbOfElems=getNbOfElems();
9673   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9674     {
9675       if(*ptr>0)
9676         {
9677           *ptr=val%(*ptr);
9678         }
9679       else
9680         {
9681           std::ostringstream oss; oss << "DataArrayInt::applyRModulus : presence of value <=0 in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
9682           oss << " !";
9683           throw INTERP_KERNEL::Exception(oss.str().c_str());
9684         }
9685     }
9686   declareAsNew();
9687 }
9688
9689 /*!
9690  * Modify all elements of \a this array, so that
9691  * an element _x_ becomes <em> val ^ x </em>.
9692  *  \param [in] val - the value used to apply pow on all array elements.
9693  *  \throw If \a this is not allocated.
9694  *  \throw If \a val < 0.
9695  */
9696 void DataArrayInt::applyPow(int val)
9697 {
9698   checkAllocated();
9699   if(val<0)
9700     throw INTERP_KERNEL::Exception("DataArrayInt::applyPow : input pow in < 0 !");
9701   int *ptr=getPointer();
9702   std::size_t nbOfElems=getNbOfElems();
9703   if(val==0)
9704     {
9705       std::fill(ptr,ptr+nbOfElems,1);
9706       return ;
9707     }
9708   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9709     {
9710       int tmp=1;
9711       for(int j=0;j<val;j++)
9712         tmp*=*ptr;
9713       *ptr=tmp;
9714     }
9715   declareAsNew();
9716 }
9717
9718 /*!
9719  * Modify all elements of \a this array, so that
9720  * an element _x_ becomes \f$ val ^ x \f$.
9721  *  \param [in] val - the value used to apply pow on all array elements.
9722  *  \throw If \a this is not allocated.
9723  *  \throw If there is an element < 0 in \a this array.
9724  *  \warning If an exception is thrown because of presence of 0 element in \a this 
9725  *           array, all elements processed before detection of the zero element remain
9726  *           modified.
9727  */
9728 void DataArrayInt::applyRPow(int val)
9729 {
9730   checkAllocated();
9731   int *ptr=getPointer();
9732   std::size_t nbOfElems=getNbOfElems();
9733   for(std::size_t i=0;i<nbOfElems;i++,ptr++)
9734     {
9735       if(*ptr>=0)
9736         {
9737           int tmp=1;
9738           for(int j=0;j<*ptr;j++)
9739             tmp*=val;
9740           *ptr=tmp;
9741         }
9742       else
9743         {
9744           std::ostringstream oss; oss << "DataArrayInt::applyRPow : presence of negative value in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
9745           oss << " !";
9746           throw INTERP_KERNEL::Exception(oss.str().c_str());
9747         }
9748     }
9749   declareAsNew();
9750 }
9751
9752 /*!
9753  * Returns a new DataArrayInt by aggregating two given arrays, so that (1) the number
9754  * of components in the result array is a sum of the number of components of given arrays
9755  * and (2) the number of tuples in the result array is same as that of each of given
9756  * arrays. In other words the i-th tuple of result array includes all components of
9757  * i-th tuples of all given arrays.
9758  * Number of tuples in the given arrays must be the same.
9759  *  \param [in] a1 - an array to include in the result array.
9760  *  \param [in] a2 - another array to include in the result array.
9761  *  \return DataArrayInt * - the new instance of DataArrayInt.
9762  *          The caller is to delete this result array using decrRef() as it is no more
9763  *          needed.
9764  *  \throw If both \a a1 and \a a2 are NULL.
9765  *  \throw If any given array is not allocated.
9766  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
9767  */
9768 DataArrayInt *DataArrayInt::Meld(const DataArrayInt *a1, const DataArrayInt *a2)
9769 {
9770   std::vector<const DataArrayInt *> arr(2);
9771   arr[0]=a1; arr[1]=a2;
9772   return Meld(arr);
9773 }
9774
9775 /*!
9776  * Returns a new DataArrayInt by aggregating all given arrays, so that (1) the number
9777  * of components in the result array is a sum of the number of components of given arrays
9778  * and (2) the number of tuples in the result array is same as that of each of given
9779  * arrays. In other words the i-th tuple of result array includes all components of
9780  * i-th tuples of all given arrays.
9781  * Number of tuples in the given arrays must be  the same.
9782  *  \param [in] arr - a sequence of arrays to include in the result array.
9783  *  \return DataArrayInt * - the new instance of DataArrayInt.
9784  *          The caller is to delete this result array using decrRef() as it is no more
9785  *          needed.
9786  *  \throw If all arrays within \a arr are NULL.
9787  *  \throw If any given array is not allocated.
9788  *  \throw If getNumberOfTuples() of arrays within \a arr is different.
9789  */
9790 DataArrayInt *DataArrayInt::Meld(const std::vector<const DataArrayInt *>& arr)
9791 {
9792   std::vector<const DataArrayInt *> a;
9793   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
9794     if(*it4)
9795       a.push_back(*it4);
9796   if(a.empty())
9797     throw INTERP_KERNEL::Exception("DataArrayInt::Meld : array must be NON empty !");
9798   std::vector<const DataArrayInt *>::const_iterator it;
9799   for(it=a.begin();it!=a.end();it++)
9800     (*it)->checkAllocated();
9801   it=a.begin();
9802   int nbOfTuples=(*it)->getNumberOfTuples();
9803   std::vector<int> nbc(a.size());
9804   std::vector<const int *> pts(a.size());
9805   nbc[0]=(*it)->getNumberOfComponents();
9806   pts[0]=(*it++)->getConstPointer();
9807   for(int i=1;it!=a.end();it++,i++)
9808     {
9809       if(nbOfTuples!=(*it)->getNumberOfTuples())
9810         throw INTERP_KERNEL::Exception("DataArrayInt::meld : mismatch of number of tuples !");
9811       nbc[i]=(*it)->getNumberOfComponents();
9812       pts[i]=(*it)->getConstPointer();
9813     }
9814   int totalNbOfComp=std::accumulate(nbc.begin(),nbc.end(),0);
9815   DataArrayInt *ret=DataArrayInt::New();
9816   ret->alloc(nbOfTuples,totalNbOfComp);
9817   int *retPtr=ret->getPointer();
9818   for(int i=0;i<nbOfTuples;i++)
9819     for(int j=0;j<(int)a.size();j++)
9820       {
9821         retPtr=std::copy(pts[j],pts[j]+nbc[j],retPtr);
9822         pts[j]+=nbc[j];
9823       }
9824   int k=0;
9825   for(int i=0;i<(int)a.size();i++)
9826     for(int j=0;j<nbc[i];j++,k++)
9827       ret->setInfoOnComponent(k,a[i]->getInfoOnComponent(j));
9828   return ret;
9829 }
9830
9831 /*!
9832  * Returns a new DataArrayInt which is a minimal partition of elements of \a groups.
9833  * The i-th item of the result array is an ID of a set of elements belonging to a
9834  * unique set of groups, which the i-th element is a part of. This set of elements
9835  * belonging to a unique set of groups is called \a family, so the result array contains
9836  * IDs of families each element belongs to.
9837  *
9838  * \b Example: if we have two groups of elements: \a group1 [0,4] and \a group2 [ 0,1,2 ],
9839  * then there are 3 families:
9840  * - \a family1 (with ID 1) contains element [0] belonging to ( \a group1 + \a group2 ),
9841  * - \a family2 (with ID 2) contains elements [4] belonging to ( \a group1 ),
9842  * - \a family3 (with ID 3) contains element [1,2] belonging to ( \a group2 ), <br>
9843  * and the result array contains IDs of families [ 1,3,3,0,2 ]. <br> Note a family ID 0 which
9844  * stands for the element #3 which is in none of groups.
9845  *
9846  *  \param [in] groups - sequence of groups of element IDs.
9847  *  \param [in] newNb - total number of elements; it must be more than max ID of element
9848  *         in \a groups.
9849  *  \param [out] fidsOfGroups - IDs of families the elements of each group belong to.
9850  *  \return DataArrayInt * - a new instance of DataArrayInt containing IDs of families
9851  *         each element with ID from range [0, \a newNb ) belongs to. The caller is to
9852  *         delete this array using decrRef() as it is no more needed.
9853  *  \throw If any element ID in \a groups violates condition ( 0 <= ID < \a newNb ).
9854  */
9855 DataArrayInt *DataArrayInt::MakePartition(const std::vector<const DataArrayInt *>& groups, int newNb, std::vector< std::vector<int> >& fidsOfGroups)
9856 {
9857   std::vector<const DataArrayInt *> groups2;
9858   for(std::vector<const DataArrayInt *>::const_iterator it4=groups.begin();it4!=groups.end();it4++)
9859     if(*it4)
9860       groups2.push_back(*it4);
9861   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
9862   ret->alloc(newNb,1);
9863   int *retPtr=ret->getPointer();
9864   std::fill(retPtr,retPtr+newNb,0);
9865   int fid=1;
9866   for(std::vector<const DataArrayInt *>::const_iterator iter=groups2.begin();iter!=groups2.end();iter++)
9867     {
9868       const int *ptr=(*iter)->getConstPointer();
9869       std::size_t nbOfElem=(*iter)->getNbOfElems();
9870       int sfid=fid;
9871       for(int j=0;j<sfid;j++)
9872         {
9873           bool found=false;
9874           for(std::size_t i=0;i<nbOfElem;i++)
9875             {
9876               if(ptr[i]>=0 && ptr[i]<newNb)
9877                 {
9878                   if(retPtr[ptr[i]]==j)
9879                     {
9880                       retPtr[ptr[i]]=fid;
9881                       found=true;
9882                     }
9883                 }
9884               else
9885                 {
9886                   std::ostringstream oss; oss << "DataArrayInt::MakePartition : In group \"" << (*iter)->getName() << "\" in tuple #" << i << " value = " << ptr[i] << " ! Should be in [0," << newNb;
9887                   oss << ") !";
9888                   throw INTERP_KERNEL::Exception(oss.str().c_str());
9889                 }
9890             }
9891           if(found)
9892             fid++;
9893         }
9894     }
9895   fidsOfGroups.clear();
9896   fidsOfGroups.resize(groups2.size());
9897   int grId=0;
9898   for(std::vector<const DataArrayInt *>::const_iterator iter=groups2.begin();iter!=groups2.end();iter++,grId++)
9899     {
9900       std::set<int> tmp;
9901       const int *ptr=(*iter)->getConstPointer();
9902       std::size_t nbOfElem=(*iter)->getNbOfElems();
9903       for(const int *p=ptr;p!=ptr+nbOfElem;p++)
9904         tmp.insert(retPtr[*p]);
9905       fidsOfGroups[grId].insert(fidsOfGroups[grId].end(),tmp.begin(),tmp.end());
9906     }
9907   return ret.retn();
9908 }
9909
9910 /*!
9911  * Returns a new DataArrayInt which contains all elements of given one-dimensional
9912  * arrays. The result array does not contain any duplicates and its values
9913  * are sorted in ascending order.
9914  *  \param [in] arr - sequence of DataArrayInt's to unite.
9915  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
9916  *         array using decrRef() as it is no more needed.
9917  *  \throw If any \a arr[i] is not allocated.
9918  *  \throw If \a arr[i]->getNumberOfComponents() != 1.
9919  */
9920 DataArrayInt *DataArrayInt::BuildUnion(const std::vector<const DataArrayInt *>& arr)
9921 {
9922   std::vector<const DataArrayInt *> a;
9923   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
9924     if(*it4)
9925       a.push_back(*it4);
9926   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9927     {
9928       (*it)->checkAllocated();
9929       if((*it)->getNumberOfComponents()!=1)
9930         throw INTERP_KERNEL::Exception("DataArrayInt::BuildUnion : only single component allowed !");
9931     }
9932   //
9933   std::set<int> r;
9934   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9935     {
9936       const int *pt=(*it)->getConstPointer();
9937       int nbOfTuples=(*it)->getNumberOfTuples();
9938       r.insert(pt,pt+nbOfTuples);
9939     }
9940   DataArrayInt *ret=DataArrayInt::New();
9941   ret->alloc((int)r.size(),1);
9942   std::copy(r.begin(),r.end(),ret->getPointer());
9943   return ret;
9944 }
9945
9946 /*!
9947  * Returns a new DataArrayInt which contains elements present in each of given one-dimensional
9948  * arrays. The result array does not contain any duplicates and its values
9949  * are sorted in ascending order.
9950  *  \param [in] arr - sequence of DataArrayInt's to intersect.
9951  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
9952  *         array using decrRef() as it is no more needed.
9953  *  \throw If any \a arr[i] is not allocated.
9954  *  \throw If \a arr[i]->getNumberOfComponents() != 1.
9955  */
9956 DataArrayInt *DataArrayInt::BuildIntersection(const std::vector<const DataArrayInt *>& arr)
9957 {
9958   std::vector<const DataArrayInt *> a;
9959   for(std::vector<const DataArrayInt *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
9960     if(*it4)
9961       a.push_back(*it4);
9962   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9963     {
9964       (*it)->checkAllocated();
9965       if((*it)->getNumberOfComponents()!=1)
9966         throw INTERP_KERNEL::Exception("DataArrayInt::BuildIntersection : only single component allowed !");
9967     }
9968   //
9969   std::set<int> r;
9970   for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
9971     {
9972       const int *pt=(*it)->getConstPointer();
9973       int nbOfTuples=(*it)->getNumberOfTuples();
9974       std::set<int> s1(pt,pt+nbOfTuples);
9975       if(it!=a.begin())
9976         {
9977           std::set<int> r2;
9978           std::set_intersection(r.begin(),r.end(),s1.begin(),s1.end(),inserter(r2,r2.end()));
9979           r=r2;
9980         }
9981       else
9982         r=s1;
9983     }
9984   DataArrayInt *ret(DataArrayInt::New());
9985   ret->alloc((int)r.size(),1);
9986   std::copy(r.begin(),r.end(),ret->getPointer());
9987   return ret;
9988 }
9989
9990 /// @cond INTERNAL
9991 namespace ParaMEDMEMImpl
9992 {
9993   class OpSwitchedOn
9994   {
9995   public:
9996     OpSwitchedOn(int *pt):_pt(pt),_cnt(0) { }
9997     void operator()(const bool& b) { if(b) *_pt++=_cnt; _cnt++; }
9998   private:
9999     int *_pt;
10000     int _cnt;
10001   };
10002
10003   class OpSwitchedOff
10004   {
10005   public:
10006     OpSwitchedOff(int *pt):_pt(pt),_cnt(0) { }
10007     void operator()(const bool& b) { if(!b) *_pt++=_cnt; _cnt++; }
10008   private:
10009     int *_pt;
10010     int _cnt;
10011   };
10012 }
10013 /// @endcond
10014
10015 /*!
10016  * This method returns the list of ids in ascending mode so that v[id]==true.
10017  */
10018 DataArrayInt *DataArrayInt::BuildListOfSwitchedOn(const std::vector<bool>& v)
10019 {
10020   int sz((int)std::count(v.begin(),v.end(),true));
10021   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(sz,1);
10022   std::for_each(v.begin(),v.end(),ParaMEDMEMImpl::OpSwitchedOn(ret->getPointer()));
10023   return ret.retn();
10024 }
10025
10026 /*!
10027  * This method returns the list of ids in ascending mode so that v[id]==false.
10028  */
10029 DataArrayInt *DataArrayInt::BuildListOfSwitchedOff(const std::vector<bool>& v)
10030 {
10031   int sz((int)std::count(v.begin(),v.end(),false));
10032   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(sz,1);
10033   std::for_each(v.begin(),v.end(),ParaMEDMEMImpl::OpSwitchedOff(ret->getPointer()));
10034   return ret.retn();
10035 }
10036
10037 /*!
10038  * This method allows to put a vector of vector of integer into a more compact data stucture (skyline). 
10039  * This method is not available into python because no available optimized data structure available to map std::vector< std::vector<int> >.
10040  *
10041  * \param [in] v the input data structure to be translate into skyline format.
10042  * \param [out] data the first element of the skyline format. The user is expected to deal with newly allocated array.
10043  * \param [out] dataIndex the second element of the skyline format.
10044  */
10045 void DataArrayInt::PutIntoToSkylineFrmt(const std::vector< std::vector<int> >& v, DataArrayInt *& data, DataArrayInt *& dataIndex)
10046 {
10047   int sz((int)v.size());
10048   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret0(DataArrayInt::New()),ret1(DataArrayInt::New());
10049   ret1->alloc(sz+1,1);
10050   int *pt(ret1->getPointer()); *pt=0;
10051   for(int i=0;i<sz;i++,pt++)
10052     pt[1]=pt[0]+(int)v[i].size();
10053   ret0->alloc(ret1->back(),1);
10054   pt=ret0->getPointer();
10055   for(int i=0;i<sz;i++)
10056     pt=std::copy(v[i].begin(),v[i].end(),pt);
10057   data=ret0.retn(); dataIndex=ret1.retn();
10058 }
10059
10060 /*!
10061  * Returns a new DataArrayInt which contains a complement of elements of \a this
10062  * one-dimensional array. I.e. the result array contains all elements from the range [0,
10063  * \a nbOfElement) not present in \a this array.
10064  *  \param [in] nbOfElement - maximal size of the result array.
10065  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
10066  *         array using decrRef() as it is no more needed.
10067  *  \throw If \a this is not allocated.
10068  *  \throw If \a this->getNumberOfComponents() != 1.
10069  *  \throw If any element \a x of \a this array violates condition ( 0 <= \a x < \a
10070  *         nbOfElement ).
10071  */
10072 DataArrayInt *DataArrayInt::buildComplement(int nbOfElement) const
10073 {
10074   checkAllocated();
10075   if(getNumberOfComponents()!=1)
10076     throw INTERP_KERNEL::Exception("DataArrayInt::buildComplement : only single component allowed !");
10077   std::vector<bool> tmp(nbOfElement);
10078   const int *pt=getConstPointer();
10079   int nbOfTuples=getNumberOfTuples();
10080   for(const int *w=pt;w!=pt+nbOfTuples;w++)
10081     if(*w>=0 && *w<nbOfElement)
10082       tmp[*w]=true;
10083     else
10084       throw INTERP_KERNEL::Exception("DataArrayInt::buildComplement : an element is not in valid range : [0,nbOfElement) !");
10085   int nbOfRetVal=(int)std::count(tmp.begin(),tmp.end(),false);
10086   DataArrayInt *ret=DataArrayInt::New();
10087   ret->alloc(nbOfRetVal,1);
10088   int j=0;
10089   int *retPtr=ret->getPointer();
10090   for(int i=0;i<nbOfElement;i++)
10091     if(!tmp[i])
10092       retPtr[j++]=i;
10093   return ret;
10094 }
10095
10096 /*!
10097  * Returns a new DataArrayInt containing elements of \a this one-dimensional missing
10098  * from an \a other one-dimensional array.
10099  *  \param [in] other - a DataArrayInt containing elements not to include in the result array.
10100  *  \return DataArrayInt * - a new instance of DataArrayInt with one component. The
10101  *         caller is to delete this array using decrRef() as it is no more needed.
10102  *  \throw If \a other is NULL.
10103  *  \throw If \a other is not allocated.
10104  *  \throw If \a other->getNumberOfComponents() != 1.
10105  *  \throw If \a this is not allocated.
10106  *  \throw If \a this->getNumberOfComponents() != 1.
10107  *  \sa DataArrayInt::buildSubstractionOptimized()
10108  */
10109 DataArrayInt *DataArrayInt::buildSubstraction(const DataArrayInt *other) const
10110 {
10111   if(!other)
10112     throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstraction : DataArrayInt pointer in input is NULL !");
10113   checkAllocated();
10114   other->checkAllocated();
10115   if(getNumberOfComponents()!=1)
10116     throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstraction : only single component allowed !");
10117   if(other->getNumberOfComponents()!=1)
10118     throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstraction : only single component allowed for other type !");
10119   const int *pt=getConstPointer();
10120   int nbOfTuples=getNumberOfTuples();
10121   std::set<int> s1(pt,pt+nbOfTuples);
10122   pt=other->getConstPointer();
10123   nbOfTuples=other->getNumberOfTuples();
10124   std::set<int> s2(pt,pt+nbOfTuples);
10125   std::vector<int> r;
10126   std::set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),std::back_insert_iterator< std::vector<int> >(r));
10127   DataArrayInt *ret=DataArrayInt::New();
10128   ret->alloc((int)r.size(),1);
10129   std::copy(r.begin(),r.end(),ret->getPointer());
10130   return ret;
10131 }
10132
10133 /*!
10134  * \a this is expected to have one component and to be sorted ascendingly (as for \a other).
10135  * \a other is expected to be a part of \a this. If not DataArrayInt::buildSubstraction should be called instead.
10136  * 
10137  * \param [in] other an array with one component and expected to be sorted ascendingly.
10138  * \ret list of ids in \a this but not in \a other.
10139  * \sa DataArrayInt::buildSubstraction
10140  */
10141 DataArrayInt *DataArrayInt::buildSubstractionOptimized(const DataArrayInt *other) const
10142 {
10143   static const char *MSG="DataArrayInt::buildSubstractionOptimized : only single component allowed !";
10144   if(!other) throw INTERP_KERNEL::Exception("DataArrayInt::buildSubstractionOptimized : NULL input array !");
10145   checkAllocated(); other->checkAllocated();
10146   if(getNumberOfComponents()!=1) throw INTERP_KERNEL::Exception(MSG);
10147   if(other->getNumberOfComponents()!=1) throw INTERP_KERNEL::Exception(MSG);
10148   const int *pt1Bg(begin()),*pt1End(end()),*pt2Bg(other->begin()),*pt2End(other->end());
10149   const int *work1(pt1Bg),*work2(pt2Bg);
10150   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
10151   for(;work1!=pt1End;work1++)
10152     {
10153       if(work2!=pt2End && *work1==*work2)
10154         work2++;
10155       else
10156         ret->pushBackSilent(*work1);
10157     }
10158   return ret.retn();
10159 }
10160
10161
10162 /*!
10163  * Returns a new DataArrayInt which contains all elements of \a this and a given
10164  * one-dimensional arrays. The result array does not contain any duplicates
10165  * and its values are sorted in ascending order.
10166  *  \param [in] other - an array to unite with \a this one.
10167  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
10168  *         array using decrRef() as it is no more needed.
10169  *  \throw If \a this or \a other is not allocated.
10170  *  \throw If \a this->getNumberOfComponents() != 1.
10171  *  \throw If \a other->getNumberOfComponents() != 1.
10172  */
10173 DataArrayInt *DataArrayInt::buildUnion(const DataArrayInt *other) const
10174 {
10175   std::vector<const DataArrayInt *>arrs(2);
10176   arrs[0]=this; arrs[1]=other;
10177   return BuildUnion(arrs);
10178 }
10179
10180
10181 /*!
10182  * Returns a new DataArrayInt which contains elements present in both \a this and a given
10183  * one-dimensional arrays. The result array does not contain any duplicates
10184  * and its values are sorted in ascending order.
10185  *  \param [in] other - an array to intersect with \a this one.
10186  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
10187  *         array using decrRef() as it is no more needed.
10188  *  \throw If \a this or \a other is not allocated.
10189  *  \throw If \a this->getNumberOfComponents() != 1.
10190  *  \throw If \a other->getNumberOfComponents() != 1.
10191  */
10192 DataArrayInt *DataArrayInt::buildIntersection(const DataArrayInt *other) const
10193 {
10194   std::vector<const DataArrayInt *>arrs(2);
10195   arrs[0]=this; arrs[1]=other;
10196   return BuildIntersection(arrs);
10197 }
10198
10199 /*!
10200  * This method can be applied on allocated with one component DataArrayInt instance.
10201  * This method is typically relevant for sorted arrays. All consecutive duplicated items in \a this will appear only once in returned DataArrayInt instance.
10202  * 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]
10203  * 
10204  * \return a newly allocated array that contain the result of the unique operation applied on \a this.
10205  * \throw if \a this is not allocated or if \a this has not exactly one component.
10206  * \sa DataArrayInt::buildUniqueNotSorted
10207  */
10208 DataArrayInt *DataArrayInt::buildUnique() const
10209 {
10210   checkAllocated();
10211   if(getNumberOfComponents()!=1)
10212     throw INTERP_KERNEL::Exception("DataArrayInt::buildUnique : only single component allowed !");
10213   int nbOfTuples=getNumberOfTuples();
10214   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp=deepCpy();
10215   int *data=tmp->getPointer();
10216   int *last=std::unique(data,data+nbOfTuples);
10217   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10218   ret->alloc(std::distance(data,last),1);
10219   std::copy(data,last,ret->getPointer());
10220   return ret.retn();
10221 }
10222
10223 /*!
10224  * This method can be applied on allocated with one component DataArrayInt instance.
10225  * This method keep elements only once by keeping the same order in \a this that is not expected to be sorted.
10226  *
10227  * \return a newly allocated array that contain the result of the unique operation applied on \a this.
10228  *
10229  * \throw if \a this is not allocated or if \a this has not exactly one component.
10230  *
10231  * \sa DataArrayInt::buildUnique
10232  */
10233 DataArrayInt *DataArrayInt::buildUniqueNotSorted() const
10234 {
10235   checkAllocated();
10236     if(getNumberOfComponents()!=1)
10237       throw INTERP_KERNEL::Exception("DataArrayInt::buildUniqueNotSorted : only single component allowed !");
10238   int minVal,maxVal;
10239   getMinMaxValues(minVal,maxVal);
10240   std::vector<bool> b(maxVal-minVal+1,false);
10241   const int *ptBg(begin()),*endBg(end());
10242   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
10243   for(const int *pt=ptBg;pt!=endBg;pt++)
10244     {
10245       if(!b[*pt-minVal])
10246         {
10247           ret->pushBackSilent(*pt);
10248           b[*pt-minVal]=true;
10249         }
10250     }
10251   ret->copyStringInfoFrom(*this);
10252   return ret.retn();
10253 }
10254
10255 /*!
10256  * Returns a new DataArrayInt which contains size of every of groups described by \a this
10257  * "index" array. Such "index" array is returned for example by 
10258  * \ref ParaMEDMEM::MEDCouplingUMesh::buildDescendingConnectivity
10259  * "MEDCouplingUMesh::buildDescendingConnectivity" and
10260  * \ref ParaMEDMEM::MEDCouplingUMesh::getNodalConnectivityIndex
10261  * "MEDCouplingUMesh::getNodalConnectivityIndex" etc.
10262  * This method preforms the reverse operation of DataArrayInt::computeOffsets2.
10263  *  \return DataArrayInt * - a new instance of DataArrayInt, whose number of tuples
10264  *          equals to \a this->getNumberOfComponents() - 1, and number of components is 1.
10265  *          The caller is to delete this array using decrRef() as it is no more needed. 
10266  *  \throw If \a this is not allocated.
10267  *  \throw If \a this->getNumberOfComponents() != 1.
10268  *  \throw If \a this->getNumberOfTuples() < 2.
10269  *
10270  *  \b Example: <br> 
10271  *         - this contains [1,3,6,7,7,9,15]
10272  *         - result array contains [2,3,1,0,2,6],
10273  *          where 2 = 3 - 1, 3 = 6 - 3, 1 = 7 - 6 etc.
10274  *
10275  * \sa DataArrayInt::computeOffsets2
10276  */
10277 DataArrayInt *DataArrayInt::deltaShiftIndex() const
10278 {
10279   checkAllocated();
10280   if(getNumberOfComponents()!=1)
10281     throw INTERP_KERNEL::Exception("DataArrayInt::deltaShiftIndex : only single component allowed !");
10282   int nbOfTuples=getNumberOfTuples();
10283   if(nbOfTuples<2)
10284     throw INTERP_KERNEL::Exception("DataArrayInt::deltaShiftIndex : 1 tuple at least must be present in 'this' !");
10285   const int *ptr=getConstPointer();
10286   DataArrayInt *ret=DataArrayInt::New();
10287   ret->alloc(nbOfTuples-1,1);
10288   int *out=ret->getPointer();
10289   std::transform(ptr+1,ptr+nbOfTuples,ptr,out,std::minus<int>());
10290   return ret;
10291 }
10292
10293 /*!
10294  * Modifies \a this one-dimensional array so that value of each element \a x
10295  * of \a this array (\a a) is computed as \f$ x_i = \sum_{j=0}^{i-1} a[ j ] \f$.
10296  * Or: for each i>0 new[i]=new[i-1]+old[i-1] for i==0 new[i]=0. Number of tuples
10297  * and components remains the same.<br>
10298  * This method is useful for allToAllV in MPI with contiguous policy. This method
10299  * differs from computeOffsets2() in that the number of tuples is \b not changed by
10300  * this one.
10301  *  \throw If \a this is not allocated.
10302  *  \throw If \a this->getNumberOfComponents() != 1.
10303  *
10304  *  \b Example: <br>
10305  *          - Before \a this contains [3,5,1,2,0,8]
10306  *          - After \a this contains  [0,3,8,9,11,11]<br>
10307  *          Note that the last element 19 = 11 + 8 is missing because size of \a this
10308  *          array is retained and thus there is no space to store the last element.
10309  */
10310 void DataArrayInt::computeOffsets()
10311 {
10312   checkAllocated();
10313   if(getNumberOfComponents()!=1)
10314     throw INTERP_KERNEL::Exception("DataArrayInt::computeOffsets : only single component allowed !");
10315   int nbOfTuples=getNumberOfTuples();
10316   if(nbOfTuples==0)
10317     return ;
10318   int *work=getPointer();
10319   int tmp=work[0];
10320   work[0]=0;
10321   for(int i=1;i<nbOfTuples;i++)
10322     {
10323       int tmp2=work[i];
10324       work[i]=work[i-1]+tmp;
10325       tmp=tmp2;
10326     }
10327   declareAsNew();
10328 }
10329
10330
10331 /*!
10332  * Modifies \a this one-dimensional array so that value of each element \a x
10333  * of \a this array (\a a) is computed as \f$ x_i = \sum_{j=0}^{i-1} a[ j ] \f$.
10334  * Or: for each i>0 new[i]=new[i-1]+old[i-1] for i==0 new[i]=0. Number
10335  * components remains the same and number of tuples is inceamented by one.<br>
10336  * This method is useful for allToAllV in MPI with contiguous policy. This method
10337  * differs from computeOffsets() in that the number of tuples is changed by this one.
10338  * This method preforms the reverse operation of DataArrayInt::deltaShiftIndex.
10339  *  \throw If \a this is not allocated.
10340  *  \throw If \a this->getNumberOfComponents() != 1.
10341  *
10342  *  \b Example: <br>
10343  *          - Before \a this contains [3,5,1,2,0,8]
10344  *          - After \a this contains  [0,3,8,9,11,11,19]<br>
10345  * \sa DataArrayInt::deltaShiftIndex
10346  */
10347 void DataArrayInt::computeOffsets2()
10348 {
10349   checkAllocated();
10350   if(getNumberOfComponents()!=1)
10351     throw INTERP_KERNEL::Exception("DataArrayInt::computeOffsets2 : only single component allowed !");
10352   int nbOfTuples=getNumberOfTuples();
10353   int *ret=(int *)malloc((nbOfTuples+1)*sizeof(int));
10354   if(nbOfTuples==0)
10355     return ;
10356   const int *work=getConstPointer();
10357   ret[0]=0;
10358   for(int i=0;i<nbOfTuples;i++)
10359     ret[i+1]=work[i]+ret[i];
10360   useArray(ret,true,C_DEALLOC,nbOfTuples+1,1);
10361   declareAsNew();
10362 }
10363
10364 /*!
10365  * Returns two new DataArrayInt instances whose contents is computed from that of \a this and \a listOfIds arrays as follows.
10366  * \a this is expected to be an offset format ( as returned by DataArrayInt::computeOffsets2 ) that is to say with one component
10367  * and ** sorted strictly increasingly **. \a listOfIds is expected to be sorted ascendingly (not strictly needed for \a listOfIds).
10368  * This methods searches in \a this, considered as a set of contiguous \c this->getNumberOfComponents() ranges, all ids in \a listOfIds
10369  * filling completely one of the ranges in \a this.
10370  *
10371  * \param [in] listOfIds a list of ids that has to be sorted ascendingly.
10372  * \param [out] rangeIdsFetched the range ids fetched
10373  * \param [out] idsInInputListThatFetch contains the list of ids in \a listOfIds that are \b fully included in a range in \a this. So
10374  *              \a idsInInputListThatFetch is a part of input \a listOfIds.
10375  *
10376  * \sa DataArrayInt::computeOffsets2
10377  *
10378  *  \b Example: <br>
10379  *          - \a this : [0,3,7,9,15,18]
10380  *          - \a listOfIds contains  [0,1,2,3,7,8,15,16,17]
10381  *          - \a rangeIdsFetched result array: [0,2,4]
10382  *          - \a idsInInputListThatFetch result array: [0,1,2,7,8,15,16,17]
10383  * In this example id 3 in input \a listOfIds is alone so it do not appear in output \a idsInInputListThatFetch.
10384  * <br>
10385  */
10386 void DataArrayInt::searchRangesInListOfIds(const DataArrayInt *listOfIds, DataArrayInt *& rangeIdsFetched, DataArrayInt *& idsInInputListThatFetch) const
10387 {
10388   if(!listOfIds)
10389     throw INTERP_KERNEL::Exception("DataArrayInt::searchRangesInListOfIds : input list of ids is null !");
10390   listOfIds->checkAllocated(); checkAllocated();
10391   if(listOfIds->getNumberOfComponents()!=1)
10392     throw INTERP_KERNEL::Exception("DataArrayInt::searchRangesInListOfIds : input list of ids must have exactly one component !");
10393   if(getNumberOfComponents()!=1)
10394     throw INTERP_KERNEL::Exception("DataArrayInt::searchRangesInListOfIds : this must have exactly one component !");
10395   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret0=DataArrayInt::New(); ret0->alloc(0,1);
10396   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=DataArrayInt::New(); ret1->alloc(0,1);
10397   const int *tupEnd(listOfIds->end()),*offBg(begin()),*offEnd(end()-1);
10398   const int *tupPtr(listOfIds->begin()),*offPtr(offBg);
10399   while(tupPtr!=tupEnd && offPtr!=offEnd)
10400     {
10401       if(*tupPtr==*offPtr)
10402         {
10403           int i=offPtr[0];
10404           while(i<offPtr[1] && *tupPtr==i && tupPtr!=tupEnd) { i++; tupPtr++; }
10405           if(i==offPtr[1])
10406             {
10407               ret0->pushBackSilent((int)std::distance(offBg,offPtr));
10408               ret1->pushBackValsSilent(tupPtr-(offPtr[1]-offPtr[0]),tupPtr);
10409               offPtr++;
10410             }
10411         }
10412       else
10413         { if(*tupPtr<*offPtr) tupPtr++; else offPtr++; }
10414     }
10415   rangeIdsFetched=ret0.retn();
10416   idsInInputListThatFetch=ret1.retn();
10417 }
10418
10419 /*!
10420  * Returns a new DataArrayInt whose contents is computed from that of \a this and \a
10421  * offsets arrays as follows. \a offsets is a one-dimensional array considered as an
10422  * "index" array of a "iota" array, thus, whose each element gives an index of a group
10423  * beginning within the "iota" array. And \a this is a one-dimensional array
10424  * considered as a selector of groups described by \a offsets to include into the result array.
10425  *  \throw If \a offsets is NULL.
10426  *  \throw If \a offsets is not allocated.
10427  *  \throw If \a offsets->getNumberOfComponents() != 1.
10428  *  \throw If \a offsets is not monotonically increasing.
10429  *  \throw If \a this is not allocated.
10430  *  \throw If \a this->getNumberOfComponents() != 1.
10431  *  \throw If any element of \a this is not a valid index for \a offsets array.
10432  *
10433  *  \b Example: <br>
10434  *          - \a this: [0,2,3]
10435  *          - \a offsets: [0,3,6,10,14,20]
10436  *          - result array: [0,1,2,6,7,8,9,10,11,12,13] == <br>
10437  *            \c range(0,3) + \c range(6,10) + \c range(10,14) ==<br>
10438  *            \c range( \a offsets[ \a this[0] ], offsets[ \a this[0]+1 ]) + 
10439  *            \c range( \a offsets[ \a this[1] ], offsets[ \a this[1]+1 ]) + 
10440  *            \c range( \a offsets[ \a this[2] ], offsets[ \a this[2]+1 ])
10441  */
10442 DataArrayInt *DataArrayInt::buildExplicitArrByRanges(const DataArrayInt *offsets) const
10443 {
10444   if(!offsets)
10445     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : DataArrayInt pointer in input is NULL !");
10446   checkAllocated();
10447   if(getNumberOfComponents()!=1)
10448     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : only single component allowed !");
10449   offsets->checkAllocated();
10450   if(offsets->getNumberOfComponents()!=1)
10451     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrByRanges : input array should have only single component !");
10452   int othNbTuples=offsets->getNumberOfTuples()-1;
10453   int nbOfTuples=getNumberOfTuples();
10454   int retNbOftuples=0;
10455   const int *work=getConstPointer();
10456   const int *offPtr=offsets->getConstPointer();
10457   for(int i=0;i<nbOfTuples;i++)
10458     {
10459       int val=work[i];
10460       if(val>=0 && val<othNbTuples)
10461         {
10462           int delta=offPtr[val+1]-offPtr[val];
10463           if(delta>=0)
10464             retNbOftuples+=delta;
10465           else
10466             {
10467               std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrByRanges : Tuple #" << val << " of offset array has a delta < 0 !";
10468               throw INTERP_KERNEL::Exception(oss.str().c_str());
10469             }
10470         }
10471       else
10472         {
10473           std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrByRanges : Tuple #" << i << " in this contains " << val;
10474           oss << " whereas offsets array is of size " << othNbTuples+1 << " !";
10475           throw INTERP_KERNEL::Exception(oss.str().c_str());
10476         }
10477     }
10478   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
10479   ret->alloc(retNbOftuples,1);
10480   int *retPtr=ret->getPointer();
10481   for(int i=0;i<nbOfTuples;i++)
10482     {
10483       int val=work[i];
10484       int start=offPtr[val];
10485       int off=offPtr[val+1]-start;
10486       for(int j=0;j<off;j++,retPtr++)
10487         *retPtr=start+j;
10488     }
10489   return ret.retn();
10490 }
10491
10492 /*!
10493  * Returns a new DataArrayInt whose contents is computed using \a this that must be a 
10494  * scaled array (monotonically increasing).
10495 from that of \a this and \a
10496  * offsets arrays as follows. \a offsets is a one-dimensional array considered as an
10497  * "index" array of a "iota" array, thus, whose each element gives an index of a group
10498  * beginning within the "iota" array. And \a this is a one-dimensional array
10499  * considered as a selector of groups described by \a offsets to include into the result array.
10500  *  \throw If \a  is NULL.
10501  *  \throw If \a this is not allocated.
10502  *  \throw If \a this->getNumberOfComponents() != 1.
10503  *  \throw If \a this->getNumberOfTuples() == 0.
10504  *  \throw If \a this is not monotonically increasing.
10505  *  \throw If any element of ids in ( \a bg \a stop \a step ) points outside the scale in \a this.
10506  *
10507  *  \b Example: <br>
10508  *          - \a bg , \a stop and \a step : (0,5,2)
10509  *          - \a this: [0,3,6,10,14,20]
10510  *          - result array: [0,0,0, 2,2,2,2, 4,4,4,4,4,4] == <br>
10511  */
10512 DataArrayInt *DataArrayInt::buildExplicitArrOfSliceOnScaledArr(int bg, int stop, int step) const
10513 {
10514   if(!isAllocated())
10515     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr : not allocated array !");
10516   if(getNumberOfComponents()!=1)
10517     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr : number of components is expected to be equal to one !");
10518   int nbOfTuples(getNumberOfTuples());
10519   if(nbOfTuples==0)
10520     throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr : number of tuples must be != 0 !");
10521   const int *ids(begin());
10522   int nbOfEltsInSlc(GetNumberOfItemGivenBESRelative(bg,stop,step,"DataArrayInt::buildExplicitArrOfSliceOnScaledArr")),sz(0),pos(bg);
10523   for(int i=0;i<nbOfEltsInSlc;i++,pos+=step)
10524     {
10525       if(pos>=0 && pos<nbOfTuples-1)
10526         {
10527           int delta(ids[pos+1]-ids[pos]);
10528           sz+=delta;
10529           if(delta<0)
10530             {
10531               std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrOfSliceOnScaledArr : At pos #" << i << " of input slice, value is " << pos << " and at this pos this is not monotonically increasing !";
10532               throw INTERP_KERNEL::Exception(oss.str().c_str());
10533             }          
10534         }
10535       else
10536         {
10537           std::ostringstream oss; oss << "DataArrayInt::buildExplicitArrOfSliceOnScaledArr : At pos #" << i << " of input slice, value is " << pos << " should be in [0," << nbOfTuples-1 << ") !";  
10538           throw INTERP_KERNEL::Exception(oss.str().c_str());
10539         }
10540     }
10541   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(sz,1);
10542   int *retPtr(ret->getPointer());
10543   pos=bg;
10544   for(int i=0;i<nbOfEltsInSlc;i++,pos+=step)
10545     {
10546       int delta(ids[pos+1]-ids[pos]);
10547       for(int j=0;j<delta;j++,retPtr++)
10548         *retPtr=pos;
10549     }
10550   return ret.retn();
10551 }
10552
10553 /*!
10554  * 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.
10555  * 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
10556  * in tuple **i** of returned DataArrayInt.
10557  * If ranges overlapped (in theory it should not) this method do not detect it and always returns the first range.
10558  *
10559  * 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)]
10560  * The return DataArrayInt will contain : **[0,4,1,2,2,3]**
10561  * 
10562  * \param [in] ranges typically come from output of MEDCouplingUMesh::ComputeRangesFromTypeDistribution. Each range is specified like this : 1st component is
10563  *             for lower value included and 2nd component is the upper value of corresponding range **excluded**.
10564  * \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
10565  *        is thrown if no ranges in \a ranges contains value in \a this.
10566  * 
10567  * \sa DataArrayInt::findIdInRangeForEachTuple
10568  */
10569 DataArrayInt *DataArrayInt::findRangeIdForEachTuple(const DataArrayInt *ranges) const
10570 {
10571   if(!ranges)
10572     throw INTERP_KERNEL::Exception("DataArrayInt::findRangeIdForEachTuple : null input pointer !");
10573   if(ranges->getNumberOfComponents()!=2)
10574     throw INTERP_KERNEL::Exception("DataArrayInt::findRangeIdForEachTuple : input DataArrayInt instance should have 2 components !");
10575   checkAllocated();
10576   if(getNumberOfComponents()!=1)
10577     throw INTERP_KERNEL::Exception("DataArrayInt::findRangeIdForEachTuple : this should have only one component !");
10578   int nbTuples=getNumberOfTuples();
10579   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbTuples,1);
10580   int nbOfRanges=ranges->getNumberOfTuples();
10581   const int *rangesPtr=ranges->getConstPointer();
10582   int *retPtr=ret->getPointer();
10583   const int *inPtr=getConstPointer();
10584   for(int i=0;i<nbTuples;i++,retPtr++)
10585     {
10586       int val=inPtr[i];
10587       bool found=false;
10588       for(int j=0;j<nbOfRanges && !found;j++)
10589         if(val>=rangesPtr[2*j] && val<rangesPtr[2*j+1])
10590           { *retPtr=j; found=true; }
10591       if(found)
10592         continue;
10593       else
10594         {
10595           std::ostringstream oss; oss << "DataArrayInt::findRangeIdForEachTuple : tuple #" << i << " not found by any ranges !";
10596           throw INTERP_KERNEL::Exception(oss.str().c_str());
10597         }
10598     }
10599   return ret.retn();
10600 }
10601
10602 /*!
10603  * 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.
10604  * 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
10605  * in tuple **i** of returned DataArrayInt.
10606  * If ranges overlapped (in theory it should not) this method do not detect it and always returns the sub position of the first range.
10607  *
10608  * 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)]
10609  * The return DataArrayInt will contain : **[1,2,4,0,2,2]**
10610  * This method is often called in pair with DataArrayInt::findRangeIdForEachTuple method.
10611  * 
10612  * \param [in] ranges typically come from output of MEDCouplingUMesh::ComputeRangesFromTypeDistribution. Each range is specified like this : 1st component is
10613  *             for lower value included and 2nd component is the upper value of corresponding range **excluded**.
10614  * \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
10615  *        is thrown if no ranges in \a ranges contains value in \a this.
10616  * \sa DataArrayInt::findRangeIdForEachTuple
10617  */
10618 DataArrayInt *DataArrayInt::findIdInRangeForEachTuple(const DataArrayInt *ranges) const
10619 {
10620   if(!ranges)
10621     throw INTERP_KERNEL::Exception("DataArrayInt::findIdInRangeForEachTuple : null input pointer !");
10622   if(ranges->getNumberOfComponents()!=2)
10623     throw INTERP_KERNEL::Exception("DataArrayInt::findIdInRangeForEachTuple : input DataArrayInt instance should have 2 components !");
10624   checkAllocated();
10625   if(getNumberOfComponents()!=1)
10626     throw INTERP_KERNEL::Exception("DataArrayInt::findIdInRangeForEachTuple : this should have only one component !");
10627   int nbTuples=getNumberOfTuples();
10628   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbTuples,1);
10629   int nbOfRanges=ranges->getNumberOfTuples();
10630   const int *rangesPtr=ranges->getConstPointer();
10631   int *retPtr=ret->getPointer();
10632   const int *inPtr=getConstPointer();
10633   for(int i=0;i<nbTuples;i++,retPtr++)
10634     {
10635       int val=inPtr[i];
10636       bool found=false;
10637       for(int j=0;j<nbOfRanges && !found;j++)
10638         if(val>=rangesPtr[2*j] && val<rangesPtr[2*j+1])
10639           { *retPtr=val-rangesPtr[2*j]; found=true; }
10640       if(found)
10641         continue;
10642       else
10643         {
10644           std::ostringstream oss; oss << "DataArrayInt::findIdInRangeForEachTuple : tuple #" << i << " not found by any ranges !";
10645           throw INTERP_KERNEL::Exception(oss.str().c_str());
10646         }
10647     }
10648   return ret.retn();
10649 }
10650
10651 /*!
10652  * \b WARNING this method is a \b non \a const \b method. This method works tuple by tuple. Each tuple is expected to be pairs (number of components must be equal to 2).
10653  * This method rearrange each pair in \a this so that, tuple with id \b tid will be after the call \c this->getIJ(tid,0)==this->getIJ(tid-1,1) and \c this->getIJ(tid,1)==this->getIJ(tid+1,0).
10654  * If it is impossible to reach such condition an exception will be thrown ! \b WARNING In case of throw \a this can be partially modified !
10655  * If this method has correctly worked, \a this will be able to be considered as a linked list.
10656  * This method does nothing if number of tuples is lower of equal to 1.
10657  *
10658  * This method is useful for users having an unstructured mesh having only SEG2 to rearrange internaly the connectibity without any coordinates consideration.
10659  *
10660  * \sa MEDCouplingUMesh::orderConsecutiveCells1D
10661  */
10662 void DataArrayInt::sortEachPairToMakeALinkedList()
10663 {
10664   checkAllocated();
10665   if(getNumberOfComponents()!=2)
10666     throw INTERP_KERNEL::Exception("DataArrayInt::sortEachPairToMakeALinkedList : Only works on DataArrayInt instance with nb of components equal to 2 !");
10667   int nbOfTuples(getNumberOfTuples());
10668   if(nbOfTuples<=1)
10669     return ;
10670   int *conn(getPointer());
10671   for(int i=1;i<nbOfTuples;i++,conn+=2)
10672     {
10673       if(i>1)
10674         {
10675           if(conn[2]==conn[3])
10676             {
10677               std::ostringstream oss; oss << "DataArrayInt::sortEachPairToMakeALinkedList : In the tuple #" << i << " presence of a pair filled with same ids !";
10678               throw INTERP_KERNEL::Exception(oss.str().c_str());
10679             }
10680           if(conn[2]!=conn[1] && conn[3]==conn[1] && conn[2]!=conn[0])
10681             std::swap(conn[2],conn[3]);
10682           //not(conn[2]==conn[1] && conn[3]!=conn[1] && conn[3]!=conn[0])
10683           if(conn[2]!=conn[1] || conn[3]==conn[1] || conn[3]==conn[0])
10684             {
10685               std::ostringstream oss; oss << "DataArrayInt::sortEachPairToMakeALinkedList : In the tuple #" << i << " something is invalid !";
10686               throw INTERP_KERNEL::Exception(oss.str().c_str());
10687             }
10688         }
10689       else
10690         {
10691           if(conn[0]==conn[1] || conn[2]==conn[3])
10692             throw INTERP_KERNEL::Exception("DataArrayInt::sortEachPairToMakeALinkedList : In the 2 first tuples presence of a pair filled with same ids !");
10693           int tmp[4];
10694           std::set<int> s;
10695           s.insert(conn,conn+4);
10696           if(s.size()!=3)
10697             throw INTERP_KERNEL::Exception("DataArrayInt::sortEachPairToMakeALinkedList : This can't be considered as a linked list regarding 2 first tuples !");
10698           if(std::count(conn,conn+4,conn[0])==2)
10699             {
10700               tmp[0]=conn[1];
10701               tmp[1]=conn[0];
10702               tmp[2]=conn[0];
10703               if(conn[2]==conn[0])
10704                 { tmp[3]=conn[3]; }
10705               else
10706                 { tmp[3]=conn[2];}
10707               std::copy(tmp,tmp+4,conn);
10708             }
10709         }
10710     }
10711 }
10712
10713 /*!
10714  * 
10715  * \param [in] nbTimes specifies the nb of times each tuples in \a this will be duplicated contiguouly in returned DataArrayInt instance.
10716  *             \a nbTimes  should be at least equal to 1.
10717  * \return a newly allocated DataArrayInt having one component and number of tuples equal to \a nbTimes * \c this->getNumberOfTuples.
10718  * \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.
10719  */
10720 DataArrayInt *DataArrayInt::duplicateEachTupleNTimes(int nbTimes) const
10721 {
10722   checkAllocated();
10723   if(getNumberOfComponents()!=1)
10724     throw INTERP_KERNEL::Exception("DataArrayInt::duplicateEachTupleNTimes : this should have only one component !");
10725   if(nbTimes<1)
10726     throw INTERP_KERNEL::Exception("DataArrayInt::duplicateEachTupleNTimes : nb times should be >= 1 !");
10727   int nbTuples=getNumberOfTuples();
10728   const int *inPtr=getConstPointer();
10729   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbTimes*nbTuples,1);
10730   int *retPtr=ret->getPointer();
10731   for(int i=0;i<nbTuples;i++,inPtr++)
10732     {
10733       int val=*inPtr;
10734       for(int j=0;j<nbTimes;j++,retPtr++)
10735         *retPtr=val;
10736     }
10737   ret->copyStringInfoFrom(*this);
10738   return ret.retn();
10739 }
10740
10741 /*!
10742  * This method returns all different values found in \a this. This method throws if \a this has not been allocated.
10743  * But the number of components can be different from one.
10744  * \return a newly allocated array (that should be dealt by the caller) containing different values in \a this.
10745  */
10746 DataArrayInt *DataArrayInt::getDifferentValues() const
10747 {
10748   checkAllocated();
10749   std::set<int> ret;
10750   ret.insert(begin(),end());
10751   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret2=DataArrayInt::New(); ret2->alloc((int)ret.size(),1);
10752   std::copy(ret.begin(),ret.end(),ret2->getPointer());
10753   return ret2.retn();
10754 }
10755
10756 /*!
10757  * This method is a refinement of DataArrayInt::getDifferentValues because it returns not only different values in \a this but also, for each of
10758  * them it tells which tuple id have this id.
10759  * This method works only on arrays with one component (if it is not the case call DataArrayInt::rearrange(1) ).
10760  * This method returns two arrays having same size.
10761  * 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.
10762  * 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]]
10763  */
10764 std::vector<DataArrayInt *> DataArrayInt::partitionByDifferentValues(std::vector<int>& differentIds) const
10765 {
10766   checkAllocated();
10767   if(getNumberOfComponents()!=1)
10768     throw INTERP_KERNEL::Exception("DataArrayInt::partitionByDifferentValues : this should have only one component !");
10769   int id=0;
10770   std::map<int,int> m,m2,m3;
10771   for(const int *w=begin();w!=end();w++)
10772     m[*w]++;
10773   differentIds.resize(m.size());
10774   std::vector<DataArrayInt *> ret(m.size());
10775   std::vector<int *> retPtr(m.size());
10776   for(std::map<int,int>::const_iterator it=m.begin();it!=m.end();it++,id++)
10777     {
10778       m2[(*it).first]=id;
10779       ret[id]=DataArrayInt::New();
10780       ret[id]->alloc((*it).second,1);
10781       retPtr[id]=ret[id]->getPointer();
10782       differentIds[id]=(*it).first;
10783     }
10784   id=0;
10785   for(const int *w=begin();w!=end();w++,id++)
10786     {
10787       retPtr[m2[*w]][m3[*w]++]=id;
10788     }
10789   return ret;
10790 }
10791
10792 /*!
10793  * This method split ids in [0, \c this->getNumberOfTuples() ) using \a this array as a field of weight (>=0 each).
10794  * The aim of this method is to return a set of \a nbOfSlices chunk of contiguous ids as balanced as possible.
10795  *
10796  * \param [in] nbOfSlices - number of slices expected.
10797  * \return - a vector having a size equal to \a nbOfSlices giving the start (included) and the stop (excluded) of each chunks.
10798  * 
10799  * \sa DataArray::GetSlice
10800  * \throw If \a this is not allocated or not with exactly one component.
10801  * \throw If an element in \a this if < 0.
10802  */
10803 std::vector< std::pair<int,int> > DataArrayInt::splitInBalancedSlices(int nbOfSlices) const
10804 {
10805   if(!isAllocated() || getNumberOfComponents()!=1)
10806     throw INTERP_KERNEL::Exception("DataArrayInt::splitInBalancedSlices : this array should have number of components equal to one and must be allocated !");
10807   if(nbOfSlices<=0)
10808     throw INTERP_KERNEL::Exception("DataArrayInt::splitInBalancedSlices : number of slices must be >= 1 !");
10809   int sum(accumulate(0)),nbOfTuples(getNumberOfTuples());
10810   int sumPerSlc(sum/nbOfSlices),pos(0);
10811   const int *w(begin());
10812   std::vector< std::pair<int,int> > ret(nbOfSlices);
10813   for(int i=0;i<nbOfSlices;i++)
10814     {
10815       std::pair<int,int> p(pos,-1);
10816       int locSum(0);
10817       while(locSum<sumPerSlc && pos<nbOfTuples) { pos++; locSum+=*w++; }
10818       if(i!=nbOfSlices-1)
10819         p.second=pos;
10820       else
10821         p.second=nbOfTuples;
10822       ret[i]=p;
10823     }
10824   return ret;
10825 }
10826
10827 /*!
10828  * Returns a new DataArrayInt that is a sum of two given arrays. There are 3
10829  * valid cases.
10830  * 1.  The arrays have same number of tuples and components. Then each value of
10831  *   the result array (_a_) is a sum of the corresponding values of \a a1 and \a a2,
10832  *   i.e.: _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, j ].
10833  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
10834  *   component. Then
10835  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, 0 ].
10836  * 3.  The arrays have same number of components and one array, say _a2_, has one
10837  *   tuple. Then
10838  *   _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ 0, j ].
10839  *
10840  * Info on components is copied either from the first array (in the first case) or from
10841  * the array with maximal number of elements (getNbOfElems()).
10842  *  \param [in] a1 - an array to sum up.
10843  *  \param [in] a2 - another array to sum up.
10844  *  \return DataArrayInt * - the new instance of DataArrayInt.
10845  *          The caller is to delete this result array using decrRef() as it is no more
10846  *          needed.
10847  *  \throw If either \a a1 or \a a2 is NULL.
10848  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
10849  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
10850  *         none of them has number of tuples or components equal to 1.
10851  */
10852 DataArrayInt *DataArrayInt::Add(const DataArrayInt *a1, const DataArrayInt *a2)
10853 {
10854   if(!a1 || !a2)
10855     throw INTERP_KERNEL::Exception("DataArrayInt::Add : input DataArrayInt instance is NULL !");
10856   int nbOfTuple=a1->getNumberOfTuples();
10857   int nbOfTuple2=a2->getNumberOfTuples();
10858   int nbOfComp=a1->getNumberOfComponents();
10859   int nbOfComp2=a2->getNumberOfComponents();
10860   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=0;
10861   if(nbOfTuple==nbOfTuple2)
10862     {
10863       if(nbOfComp==nbOfComp2)
10864         {
10865           ret=DataArrayInt::New();
10866           ret->alloc(nbOfTuple,nbOfComp);
10867           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<int>());
10868           ret->copyStringInfoFrom(*a1);
10869         }
10870       else
10871         {
10872           int nbOfCompMin,nbOfCompMax;
10873           const DataArrayInt *aMin, *aMax;
10874           if(nbOfComp>nbOfComp2)
10875             {
10876               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
10877               aMin=a2; aMax=a1;
10878             }
10879           else
10880             {
10881               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
10882               aMin=a1; aMax=a2;
10883             }
10884           if(nbOfCompMin==1)
10885             {
10886               ret=DataArrayInt::New();
10887               ret->alloc(nbOfTuple,nbOfCompMax);
10888               const int *aMinPtr=aMin->getConstPointer();
10889               const int *aMaxPtr=aMax->getConstPointer();
10890               int *res=ret->getPointer();
10891               for(int i=0;i<nbOfTuple;i++)
10892                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::plus<int>(),aMinPtr[i]));
10893               ret->copyStringInfoFrom(*aMax);
10894             }
10895           else
10896             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
10897         }
10898     }
10899   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
10900     {
10901       if(nbOfComp==nbOfComp2)
10902         {
10903           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
10904           const DataArrayInt *aMin=nbOfTuple>nbOfTuple2?a2:a1;
10905           const DataArrayInt *aMax=nbOfTuple>nbOfTuple2?a1:a2;
10906           const int *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
10907           ret=DataArrayInt::New();
10908           ret->alloc(nbOfTupleMax,nbOfComp);
10909           int *res=ret->getPointer();
10910           for(int i=0;i<nbOfTupleMax;i++)
10911             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::plus<int>());
10912           ret->copyStringInfoFrom(*aMax);
10913         }
10914       else
10915         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
10916     }
10917   else
10918     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !");
10919   return ret.retn();
10920 }
10921
10922 /*!
10923  * Adds values of another DataArrayInt to values of \a this one. There are 3
10924  * valid cases.
10925  * 1.  The arrays have same number of tuples and components. Then each value of
10926  *   \a other array is added to the corresponding value of \a this array, i.e.:
10927  *   _a_ [ i, j ] += _other_ [ i, j ].
10928  * 2.  The arrays have same number of tuples and \a other array has one component. Then
10929  *   _a_ [ i, j ] += _other_ [ i, 0 ].
10930  * 3.  The arrays have same number of components and \a other array has one tuple. Then
10931  *   _a_ [ i, j ] += _a2_ [ 0, j ].
10932  *
10933  *  \param [in] other - an array to add to \a this one.
10934  *  \throw If \a other is NULL.
10935  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
10936  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
10937  *         \a other has number of both tuples and components not equal to 1.
10938  */
10939 void DataArrayInt::addEqual(const DataArrayInt *other)
10940 {
10941   if(!other)
10942     throw INTERP_KERNEL::Exception("DataArrayInt::addEqual : input DataArrayInt instance is NULL !");
10943   const char *msg="Nb of tuples mismatch for DataArrayInt::addEqual  !";
10944   checkAllocated(); other->checkAllocated();
10945   int nbOfTuple=getNumberOfTuples();
10946   int nbOfTuple2=other->getNumberOfTuples();
10947   int nbOfComp=getNumberOfComponents();
10948   int nbOfComp2=other->getNumberOfComponents();
10949   if(nbOfTuple==nbOfTuple2)
10950     {
10951       if(nbOfComp==nbOfComp2)
10952         {
10953           std::transform(begin(),end(),other->begin(),getPointer(),std::plus<int>());
10954         }
10955       else if(nbOfComp2==1)
10956         {
10957           int *ptr=getPointer();
10958           const int *ptrc=other->getConstPointer();
10959           for(int i=0;i<nbOfTuple;i++)
10960             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::plus<int>(),*ptrc++));
10961         }
10962       else
10963         throw INTERP_KERNEL::Exception(msg);
10964     }
10965   else if(nbOfTuple2==1)
10966     {
10967       if(nbOfComp2==nbOfComp)
10968         {
10969           int *ptr=getPointer();
10970           const int *ptrc=other->getConstPointer();
10971           for(int i=0;i<nbOfTuple;i++)
10972             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::plus<int>());
10973         }
10974       else
10975         throw INTERP_KERNEL::Exception(msg);
10976     }
10977   else
10978     throw INTERP_KERNEL::Exception(msg);
10979   declareAsNew();
10980 }
10981
10982 /*!
10983  * Returns a new DataArrayInt that is a subtraction of two given arrays. There are 3
10984  * valid cases.
10985  * 1.  The arrays have same number of tuples and components. Then each value of
10986  *   the result array (_a_) is a subtraction of the corresponding values of \a a1 and
10987  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, j ].
10988  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
10989  *   component. Then
10990  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, 0 ].
10991  * 3.  The arrays have same number of components and one array, say _a2_, has one
10992  *   tuple. Then
10993  *   _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ 0, j ].
10994  *
10995  * Info on components is copied either from the first array (in the first case) or from
10996  * the array with maximal number of elements (getNbOfElems()).
10997  *  \param [in] a1 - an array to subtract from.
10998  *  \param [in] a2 - an array to subtract.
10999  *  \return DataArrayInt * - the new instance of DataArrayInt.
11000  *          The caller is to delete this result array using decrRef() as it is no more
11001  *          needed.
11002  *  \throw If either \a a1 or \a a2 is NULL.
11003  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
11004  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
11005  *         none of them has number of tuples or components equal to 1.
11006  */
11007 DataArrayInt *DataArrayInt::Substract(const DataArrayInt *a1, const DataArrayInt *a2)
11008 {
11009   if(!a1 || !a2)
11010     throw INTERP_KERNEL::Exception("DataArrayInt::Substract : input DataArrayInt instance is NULL !");
11011   int nbOfTuple1=a1->getNumberOfTuples();
11012   int nbOfTuple2=a2->getNumberOfTuples();
11013   int nbOfComp1=a1->getNumberOfComponents();
11014   int nbOfComp2=a2->getNumberOfComponents();
11015   if(nbOfTuple2==nbOfTuple1)
11016     {
11017       if(nbOfComp1==nbOfComp2)
11018         {
11019           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11020           ret->alloc(nbOfTuple2,nbOfComp1);
11021           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus<int>());
11022           ret->copyStringInfoFrom(*a1);
11023           return ret.retn();
11024         }
11025       else if(nbOfComp2==1)
11026         {
11027           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11028           ret->alloc(nbOfTuple1,nbOfComp1);
11029           const int *a2Ptr=a2->getConstPointer();
11030           const int *a1Ptr=a1->getConstPointer();
11031           int *res=ret->getPointer();
11032           for(int i=0;i<nbOfTuple1;i++)
11033             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::minus<int>(),a2Ptr[i]));
11034           ret->copyStringInfoFrom(*a1);
11035           return ret.retn();
11036         }
11037       else
11038         {
11039           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
11040           return 0;
11041         }
11042     }
11043   else if(nbOfTuple2==1)
11044     {
11045       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
11046       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11047       ret->alloc(nbOfTuple1,nbOfComp1);
11048       const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
11049       int *pt=ret->getPointer();
11050       for(int i=0;i<nbOfTuple1;i++)
11051         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::minus<int>());
11052       ret->copyStringInfoFrom(*a1);
11053       return ret.retn();
11054     }
11055   else
11056     {
11057       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception
11058       return 0;
11059     }
11060 }
11061
11062 /*!
11063  * Subtract values of another DataArrayInt from values of \a this one. There are 3
11064  * valid cases.
11065  * 1.  The arrays have same number of tuples and components. Then each value of
11066  *   \a other array is subtracted from the corresponding value of \a this array, i.e.:
11067  *   _a_ [ i, j ] -= _other_ [ i, j ].
11068  * 2.  The arrays have same number of tuples and \a other array has one component. Then
11069  *   _a_ [ i, j ] -= _other_ [ i, 0 ].
11070  * 3.  The arrays have same number of components and \a other array has one tuple. Then
11071  *   _a_ [ i, j ] -= _a2_ [ 0, j ].
11072  *
11073  *  \param [in] other - an array to subtract from \a this one.
11074  *  \throw If \a other is NULL.
11075  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
11076  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
11077  *         \a other has number of both tuples and components not equal to 1.
11078  */
11079 void DataArrayInt::substractEqual(const DataArrayInt *other)
11080 {
11081   if(!other)
11082     throw INTERP_KERNEL::Exception("DataArrayInt::substractEqual : input DataArrayInt instance is NULL !");
11083   const char *msg="Nb of tuples mismatch for DataArrayInt::substractEqual  !";
11084   checkAllocated(); other->checkAllocated();
11085   int nbOfTuple=getNumberOfTuples();
11086   int nbOfTuple2=other->getNumberOfTuples();
11087   int nbOfComp=getNumberOfComponents();
11088   int nbOfComp2=other->getNumberOfComponents();
11089   if(nbOfTuple==nbOfTuple2)
11090     {
11091       if(nbOfComp==nbOfComp2)
11092         {
11093           std::transform(begin(),end(),other->begin(),getPointer(),std::minus<int>());
11094         }
11095       else if(nbOfComp2==1)
11096         {
11097           int *ptr=getPointer();
11098           const int *ptrc=other->getConstPointer();
11099           for(int i=0;i<nbOfTuple;i++)
11100             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::minus<int>(),*ptrc++));
11101         }
11102       else
11103         throw INTERP_KERNEL::Exception(msg);
11104     }
11105   else if(nbOfTuple2==1)
11106     {
11107       int *ptr=getPointer();
11108       const int *ptrc=other->getConstPointer();
11109       for(int i=0;i<nbOfTuple;i++)
11110         std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::minus<int>());
11111     }
11112   else
11113     throw INTERP_KERNEL::Exception(msg);
11114   declareAsNew();
11115 }
11116
11117 /*!
11118  * Returns a new DataArrayInt that is a product of two given arrays. There are 3
11119  * valid cases.
11120  * 1.  The arrays have same number of tuples and components. Then each value of
11121  *   the result array (_a_) is a product of the corresponding values of \a a1 and
11122  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, j ].
11123  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
11124  *   component. Then
11125  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, 0 ].
11126  * 3.  The arrays have same number of components and one array, say _a2_, has one
11127  *   tuple. Then
11128  *   _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ 0, j ].
11129  *
11130  * Info on components is copied either from the first array (in the first case) or from
11131  * the array with maximal number of elements (getNbOfElems()).
11132  *  \param [in] a1 - a factor array.
11133  *  \param [in] a2 - another factor array.
11134  *  \return DataArrayInt * - the new instance of DataArrayInt.
11135  *          The caller is to delete this result array using decrRef() as it is no more
11136  *          needed.
11137  *  \throw If either \a a1 or \a a2 is NULL.
11138  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
11139  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
11140  *         none of them has number of tuples or components equal to 1.
11141  */
11142 DataArrayInt *DataArrayInt::Multiply(const DataArrayInt *a1, const DataArrayInt *a2)
11143 {
11144   if(!a1 || !a2)
11145     throw INTERP_KERNEL::Exception("DataArrayInt::Multiply : input DataArrayInt instance is NULL !");
11146   int nbOfTuple=a1->getNumberOfTuples();
11147   int nbOfTuple2=a2->getNumberOfTuples();
11148   int nbOfComp=a1->getNumberOfComponents();
11149   int nbOfComp2=a2->getNumberOfComponents();
11150   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=0;
11151   if(nbOfTuple==nbOfTuple2)
11152     {
11153       if(nbOfComp==nbOfComp2)
11154         {
11155           ret=DataArrayInt::New();
11156           ret->alloc(nbOfTuple,nbOfComp);
11157           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies<int>());
11158           ret->copyStringInfoFrom(*a1);
11159         }
11160       else
11161         {
11162           int nbOfCompMin,nbOfCompMax;
11163           const DataArrayInt *aMin, *aMax;
11164           if(nbOfComp>nbOfComp2)
11165             {
11166               nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
11167               aMin=a2; aMax=a1;
11168             }
11169           else
11170             {
11171               nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
11172               aMin=a1; aMax=a2;
11173             }
11174           if(nbOfCompMin==1)
11175             {
11176               ret=DataArrayInt::New();
11177               ret->alloc(nbOfTuple,nbOfCompMax);
11178               const int *aMinPtr=aMin->getConstPointer();
11179               const int *aMaxPtr=aMax->getConstPointer();
11180               int *res=ret->getPointer();
11181               for(int i=0;i<nbOfTuple;i++)
11182                 res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::multiplies<int>(),aMinPtr[i]));
11183               ret->copyStringInfoFrom(*aMax);
11184             }
11185           else
11186             throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
11187         }
11188     }
11189   else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
11190     {
11191       if(nbOfComp==nbOfComp2)
11192         {
11193           int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
11194           const DataArrayInt *aMin=nbOfTuple>nbOfTuple2?a2:a1;
11195           const DataArrayInt *aMax=nbOfTuple>nbOfTuple2?a1:a2;
11196           const int *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
11197           ret=DataArrayInt::New();
11198           ret->alloc(nbOfTupleMax,nbOfComp);
11199           int *res=ret->getPointer();
11200           for(int i=0;i<nbOfTupleMax;i++)
11201             res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::multiplies<int>());
11202           ret->copyStringInfoFrom(*aMax);
11203         }
11204       else
11205         throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
11206     }
11207   else
11208     throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !");
11209   return ret.retn();
11210 }
11211
11212
11213 /*!
11214  * Multiply values of another DataArrayInt to values of \a this one. There are 3
11215  * valid cases.
11216  * 1.  The arrays have same number of tuples and components. Then each value of
11217  *   \a other array is multiplied to the corresponding value of \a this array, i.e.:
11218  *   _a_ [ i, j ] *= _other_ [ i, j ].
11219  * 2.  The arrays have same number of tuples and \a other array has one component. Then
11220  *   _a_ [ i, j ] *= _other_ [ i, 0 ].
11221  * 3.  The arrays have same number of components and \a other array has one tuple. Then
11222  *   _a_ [ i, j ] *= _a2_ [ 0, j ].
11223  *
11224  *  \param [in] other - an array to multiply to \a this one.
11225  *  \throw If \a other is NULL.
11226  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
11227  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
11228  *         \a other has number of both tuples and components not equal to 1.
11229  */
11230 void DataArrayInt::multiplyEqual(const DataArrayInt *other)
11231 {
11232   if(!other)
11233     throw INTERP_KERNEL::Exception("DataArrayInt::multiplyEqual : input DataArrayInt instance is NULL !");
11234   const char *msg="Nb of tuples mismatch for DataArrayInt::multiplyEqual !";
11235   checkAllocated(); other->checkAllocated();
11236   int nbOfTuple=getNumberOfTuples();
11237   int nbOfTuple2=other->getNumberOfTuples();
11238   int nbOfComp=getNumberOfComponents();
11239   int nbOfComp2=other->getNumberOfComponents();
11240   if(nbOfTuple==nbOfTuple2)
11241     {
11242       if(nbOfComp==nbOfComp2)
11243         {
11244           std::transform(begin(),end(),other->begin(),getPointer(),std::multiplies<int>());
11245         }
11246       else if(nbOfComp2==1)
11247         {
11248           int *ptr=getPointer();
11249           const int *ptrc=other->getConstPointer();
11250           for(int i=0;i<nbOfTuple;i++)
11251             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::multiplies<int>(),*ptrc++));    
11252         }
11253       else
11254         throw INTERP_KERNEL::Exception(msg);
11255     }
11256   else if(nbOfTuple2==1)
11257     {
11258       if(nbOfComp2==nbOfComp)
11259         {
11260           int *ptr=getPointer();
11261           const int *ptrc=other->getConstPointer();
11262           for(int i=0;i<nbOfTuple;i++)
11263             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::multiplies<int>());
11264         }
11265       else
11266         throw INTERP_KERNEL::Exception(msg);
11267     }
11268   else
11269     throw INTERP_KERNEL::Exception(msg);
11270   declareAsNew();
11271 }
11272
11273
11274 /*!
11275  * Returns a new DataArrayInt that is a division of two given arrays. There are 3
11276  * valid cases.
11277  * 1.  The arrays have same number of tuples and components. Then each value of
11278  *   the result array (_a_) is a division of the corresponding values of \a a1 and
11279  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, j ].
11280  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
11281  *   component. Then
11282  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, 0 ].
11283  * 3.  The arrays have same number of components and one array, say _a2_, has one
11284  *   tuple. Then
11285  *   _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ 0, j ].
11286  *
11287  * Info on components is copied either from the first array (in the first case) or from
11288  * the array with maximal number of elements (getNbOfElems()).
11289  *  \warning No check of division by zero is performed!
11290  *  \param [in] a1 - a numerator array.
11291  *  \param [in] a2 - a denominator array.
11292  *  \return DataArrayInt * - the new instance of DataArrayInt.
11293  *          The caller is to delete this result array using decrRef() as it is no more
11294  *          needed.
11295  *  \throw If either \a a1 or \a a2 is NULL.
11296  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
11297  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
11298  *         none of them has number of tuples or components equal to 1.
11299  */
11300 DataArrayInt *DataArrayInt::Divide(const DataArrayInt *a1, const DataArrayInt *a2)
11301 {
11302   if(!a1 || !a2)
11303     throw INTERP_KERNEL::Exception("DataArrayInt::Divide : input DataArrayInt instance is NULL !");
11304   int nbOfTuple1=a1->getNumberOfTuples();
11305   int nbOfTuple2=a2->getNumberOfTuples();
11306   int nbOfComp1=a1->getNumberOfComponents();
11307   int nbOfComp2=a2->getNumberOfComponents();
11308   if(nbOfTuple2==nbOfTuple1)
11309     {
11310       if(nbOfComp1==nbOfComp2)
11311         {
11312           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11313           ret->alloc(nbOfTuple2,nbOfComp1);
11314           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides<int>());
11315           ret->copyStringInfoFrom(*a1);
11316           return ret.retn();
11317         }
11318       else if(nbOfComp2==1)
11319         {
11320           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11321           ret->alloc(nbOfTuple1,nbOfComp1);
11322           const int *a2Ptr=a2->getConstPointer();
11323           const int *a1Ptr=a1->getConstPointer();
11324           int *res=ret->getPointer();
11325           for(int i=0;i<nbOfTuple1;i++)
11326             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::divides<int>(),a2Ptr[i]));
11327           ret->copyStringInfoFrom(*a1);
11328           return ret.retn();
11329         }
11330       else
11331         {
11332           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
11333           return 0;
11334         }
11335     }
11336   else if(nbOfTuple2==1)
11337     {
11338       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
11339       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11340       ret->alloc(nbOfTuple1,nbOfComp1);
11341       const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
11342       int *pt=ret->getPointer();
11343       for(int i=0;i<nbOfTuple1;i++)
11344         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::divides<int>());
11345       ret->copyStringInfoFrom(*a1);
11346       return ret.retn();
11347     }
11348   else
11349     {
11350       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception
11351       return 0;
11352     }
11353 }
11354
11355 /*!
11356  * Divide values of \a this array by values of another DataArrayInt. There are 3
11357  * valid cases.
11358  * 1.  The arrays have same number of tuples and components. Then each value of
11359  *    \a this array is divided by the corresponding value of \a other one, i.e.:
11360  *   _a_ [ i, j ] /= _other_ [ i, j ].
11361  * 2.  The arrays have same number of tuples and \a other array has one component. Then
11362  *   _a_ [ i, j ] /= _other_ [ i, 0 ].
11363  * 3.  The arrays have same number of components and \a other array has one tuple. Then
11364  *   _a_ [ i, j ] /= _a2_ [ 0, j ].
11365  *
11366  *  \warning No check of division by zero is performed!
11367  *  \param [in] other - an array to divide \a this one by.
11368  *  \throw If \a other is NULL.
11369  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
11370  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
11371  *         \a other has number of both tuples and components not equal to 1.
11372  */
11373 void DataArrayInt::divideEqual(const DataArrayInt *other)
11374 {
11375   if(!other)
11376     throw INTERP_KERNEL::Exception("DataArrayInt::divideEqual : input DataArrayInt instance is NULL !");
11377   const char *msg="Nb of tuples mismatch for DataArrayInt::divideEqual !";
11378   checkAllocated(); other->checkAllocated();
11379   int nbOfTuple=getNumberOfTuples();
11380   int nbOfTuple2=other->getNumberOfTuples();
11381   int nbOfComp=getNumberOfComponents();
11382   int nbOfComp2=other->getNumberOfComponents();
11383   if(nbOfTuple==nbOfTuple2)
11384     {
11385       if(nbOfComp==nbOfComp2)
11386         {
11387           std::transform(begin(),end(),other->begin(),getPointer(),std::divides<int>());
11388         }
11389       else if(nbOfComp2==1)
11390         {
11391           int *ptr=getPointer();
11392           const int *ptrc=other->getConstPointer();
11393           for(int i=0;i<nbOfTuple;i++)
11394             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::divides<int>(),*ptrc++));
11395         }
11396       else
11397         throw INTERP_KERNEL::Exception(msg);
11398     }
11399   else if(nbOfTuple2==1)
11400     {
11401       if(nbOfComp2==nbOfComp)
11402         {
11403           int *ptr=getPointer();
11404           const int *ptrc=other->getConstPointer();
11405           for(int i=0;i<nbOfTuple;i++)
11406             std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::divides<int>());
11407         }
11408       else
11409         throw INTERP_KERNEL::Exception(msg);
11410     }
11411   else
11412     throw INTERP_KERNEL::Exception(msg);
11413   declareAsNew();
11414 }
11415
11416
11417 /*!
11418  * Returns a new DataArrayInt that is a modulus of two given arrays. There are 3
11419  * valid cases.
11420  * 1.  The arrays have same number of tuples and components. Then each value of
11421  *   the result array (_a_) is a division of the corresponding values of \a a1 and
11422  *   \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] % _a2_ [ i, j ].
11423  * 2.  The arrays have same number of tuples and one array, say _a2_, has one
11424  *   component. Then
11425  *   _a_ [ i, j ] = _a1_ [ i, j ] % _a2_ [ i, 0 ].
11426  * 3.  The arrays have same number of components and one array, say _a2_, has one
11427  *   tuple. Then
11428  *   _a_ [ i, j ] = _a1_ [ i, j ] % _a2_ [ 0, j ].
11429  *
11430  * Info on components is copied either from the first array (in the first case) or from
11431  * the array with maximal number of elements (getNbOfElems()).
11432  *  \warning No check of division by zero is performed!
11433  *  \param [in] a1 - a dividend array.
11434  *  \param [in] a2 - a divisor array.
11435  *  \return DataArrayInt * - the new instance of DataArrayInt.
11436  *          The caller is to delete this result array using decrRef() as it is no more
11437  *          needed.
11438  *  \throw If either \a a1 or \a a2 is NULL.
11439  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
11440  *         \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
11441  *         none of them has number of tuples or components equal to 1.
11442  */
11443 DataArrayInt *DataArrayInt::Modulus(const DataArrayInt *a1, const DataArrayInt *a2)
11444 {
11445   if(!a1 || !a2)
11446     throw INTERP_KERNEL::Exception("DataArrayInt::Modulus : input DataArrayInt instance is NULL !");
11447   int nbOfTuple1=a1->getNumberOfTuples();
11448   int nbOfTuple2=a2->getNumberOfTuples();
11449   int nbOfComp1=a1->getNumberOfComponents();
11450   int nbOfComp2=a2->getNumberOfComponents();
11451   if(nbOfTuple2==nbOfTuple1)
11452     {
11453       if(nbOfComp1==nbOfComp2)
11454         {
11455           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11456           ret->alloc(nbOfTuple2,nbOfComp1);
11457           std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::modulus<int>());
11458           ret->copyStringInfoFrom(*a1);
11459           return ret.retn();
11460         }
11461       else if(nbOfComp2==1)
11462         {
11463           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11464           ret->alloc(nbOfTuple1,nbOfComp1);
11465           const int *a2Ptr=a2->getConstPointer();
11466           const int *a1Ptr=a1->getConstPointer();
11467           int *res=ret->getPointer();
11468           for(int i=0;i<nbOfTuple1;i++)
11469             res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::modulus<int>(),a2Ptr[i]));
11470           ret->copyStringInfoFrom(*a1);
11471           return ret.retn();
11472         }
11473       else
11474         {
11475           a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Modulus !");
11476           return 0;
11477         }
11478     }
11479   else if(nbOfTuple2==1)
11480     {
11481       a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Modulus !");
11482       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11483       ret->alloc(nbOfTuple1,nbOfComp1);
11484       const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
11485       int *pt=ret->getPointer();
11486       for(int i=0;i<nbOfTuple1;i++)
11487         pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::modulus<int>());
11488       ret->copyStringInfoFrom(*a1);
11489       return ret.retn();
11490     }
11491   else
11492     {
11493       a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Modulus !");//will always throw an exception
11494       return 0;
11495     }
11496 }
11497
11498 /*!
11499  * Modify \a this array so that each value becomes a modulus of division of this value by
11500  * a value of another DataArrayInt. There are 3 valid cases.
11501  * 1.  The arrays have same number of tuples and components. Then each value of
11502  *    \a this array is divided by the corresponding value of \a other one, i.e.:
11503  *   _a_ [ i, j ] %= _other_ [ i, j ].
11504  * 2.  The arrays have same number of tuples and \a other array has one component. Then
11505  *   _a_ [ i, j ] %= _other_ [ i, 0 ].
11506  * 3.  The arrays have same number of components and \a other array has one tuple. Then
11507  *   _a_ [ i, j ] %= _a2_ [ 0, j ].
11508  *
11509  *  \warning No check of division by zero is performed!
11510  *  \param [in] other - a divisor array.
11511  *  \throw If \a other is NULL.
11512  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
11513  *         \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
11514  *         \a other has number of both tuples and components not equal to 1.
11515  */
11516 void DataArrayInt::modulusEqual(const DataArrayInt *other)
11517 {
11518   if(!other)
11519     throw INTERP_KERNEL::Exception("DataArrayInt::modulusEqual : input DataArrayInt instance is NULL !");
11520   const char *msg="Nb of tuples mismatch for DataArrayInt::modulusEqual !";
11521   checkAllocated(); other->checkAllocated();
11522   int nbOfTuple=getNumberOfTuples();
11523   int nbOfTuple2=other->getNumberOfTuples();
11524   int nbOfComp=getNumberOfComponents();
11525   int nbOfComp2=other->getNumberOfComponents();
11526   if(nbOfTuple==nbOfTuple2)
11527     {
11528       if(nbOfComp==nbOfComp2)
11529         {
11530           std::transform(begin(),end(),other->begin(),getPointer(),std::modulus<int>());
11531         }
11532       else if(nbOfComp2==1)
11533         {
11534           if(nbOfComp2==nbOfComp)
11535             {
11536               int *ptr=getPointer();
11537               const int *ptrc=other->getConstPointer();
11538               for(int i=0;i<nbOfTuple;i++)
11539                 std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::modulus<int>(),*ptrc++));
11540             }
11541           else
11542             throw INTERP_KERNEL::Exception(msg);
11543         }
11544       else
11545         throw INTERP_KERNEL::Exception(msg);
11546     }
11547   else if(nbOfTuple2==1)
11548     {
11549       int *ptr=getPointer();
11550       const int *ptrc=other->getConstPointer();
11551       for(int i=0;i<nbOfTuple;i++)
11552         std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::modulus<int>());
11553     }
11554   else
11555     throw INTERP_KERNEL::Exception(msg);
11556   declareAsNew();
11557 }
11558
11559 /*!
11560  * Returns a new DataArrayInt that is the result of pow of two given arrays. There are 3
11561  * valid cases.
11562  *
11563  *  \param [in] a1 - an array to pow up.
11564  *  \param [in] a2 - another array to sum up.
11565  *  \return DataArrayInt * - the new instance of DataArrayInt.
11566  *          The caller is to delete this result array using decrRef() as it is no more
11567  *          needed.
11568  *  \throw If either \a a1 or \a a2 is NULL.
11569  *  \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
11570  *  \throw If \a a1->getNumberOfComponents() != 1 or \a a2->getNumberOfComponents() != 1.
11571  *  \throw If there is a negative value in \a a2.
11572  */
11573 DataArrayInt *DataArrayInt::Pow(const DataArrayInt *a1, const DataArrayInt *a2)
11574 {
11575   if(!a1 || !a2)
11576     throw INTERP_KERNEL::Exception("DataArrayInt::Pow : at least one of input instances is null !");
11577   int nbOfTuple=a1->getNumberOfTuples();
11578   int nbOfTuple2=a2->getNumberOfTuples();
11579   int nbOfComp=a1->getNumberOfComponents();
11580   int nbOfComp2=a2->getNumberOfComponents();
11581   if(nbOfTuple!=nbOfTuple2)
11582     throw INTERP_KERNEL::Exception("DataArrayInt::Pow : number of tuples mismatches !");
11583   if(nbOfComp!=1 || nbOfComp2!=1)
11584     throw INTERP_KERNEL::Exception("DataArrayInt::Pow : number of components of both arrays must be equal to 1 !");
11585   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbOfTuple,1);
11586   const int *ptr1(a1->begin()),*ptr2(a2->begin());
11587   int *ptr=ret->getPointer();
11588   for(int i=0;i<nbOfTuple;i++,ptr1++,ptr2++,ptr++)
11589     {
11590       if(*ptr2>=0)
11591         {
11592           int tmp=1;
11593           for(int j=0;j<*ptr2;j++)
11594             tmp*=*ptr1;
11595           *ptr=tmp;
11596         }
11597       else
11598         {
11599           std::ostringstream oss; oss << "DataArrayInt::Pow : on tuple #" << i << " of a2 value is < 0 (" << *ptr2 << ") !";
11600           throw INTERP_KERNEL::Exception(oss.str().c_str());
11601         }
11602     }
11603   return ret.retn();
11604 }
11605
11606 /*!
11607  * Apply pow on values of another DataArrayInt to values of \a this one.
11608  *
11609  *  \param [in] other - an array to pow to \a this one.
11610  *  \throw If \a other is NULL.
11611  *  \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples()
11612  *  \throw If \a this->getNumberOfComponents() != 1 or \a other->getNumberOfComponents() != 1
11613  *  \throw If there is a negative value in \a other.
11614  */
11615 void DataArrayInt::powEqual(const DataArrayInt *other)
11616 {
11617   if(!other)
11618     throw INTERP_KERNEL::Exception("DataArrayInt::powEqual : input instance is null !");
11619   int nbOfTuple=getNumberOfTuples();
11620   int nbOfTuple2=other->getNumberOfTuples();
11621   int nbOfComp=getNumberOfComponents();
11622   int nbOfComp2=other->getNumberOfComponents();
11623   if(nbOfTuple!=nbOfTuple2)
11624     throw INTERP_KERNEL::Exception("DataArrayInt::powEqual : number of tuples mismatches !");
11625   if(nbOfComp!=1 || nbOfComp2!=1)
11626     throw INTERP_KERNEL::Exception("DataArrayInt::powEqual : number of components of both arrays must be equal to 1 !");
11627   int *ptr=getPointer();
11628   const int *ptrc=other->begin();
11629   for(int i=0;i<nbOfTuple;i++,ptrc++,ptr++)
11630     {
11631       if(*ptrc>=0)
11632         {
11633           int tmp=1;
11634           for(int j=0;j<*ptrc;j++)
11635             tmp*=*ptr;
11636           *ptr=tmp;
11637         }
11638       else
11639         {
11640           std::ostringstream oss; oss << "DataArrayInt::powEqual : on tuple #" << i << " of other value is < 0 (" << *ptrc << ") !";
11641           throw INTERP_KERNEL::Exception(oss.str().c_str());
11642         }
11643     }
11644   declareAsNew();
11645 }
11646
11647 /*!
11648  * Returns a C array which is a renumbering map in "Old to New" mode for the input array.
11649  * This map, if applied to \a start array, would make it sorted. For example, if
11650  * \a start array contents are [9,10,0,6,4,11,3,7] then the contents of the result array is
11651  * [5,6,0,3,2,7,1,4].
11652  *  \param [in] start - pointer to the first element of the array for which the
11653  *         permutation map is computed.
11654  *  \param [in] end - pointer specifying the end of the array \a start, so that
11655  *         the last value of \a start is \a end[ -1 ].
11656  *  \return int * - the result permutation array that the caller is to delete as it is no
11657  *         more needed.
11658  *  \throw If there are equal values in the input array.
11659  */
11660 int *DataArrayInt::CheckAndPreparePermutation(const int *start, const int *end)
11661 {
11662   std::size_t sz=std::distance(start,end);
11663   int *ret=(int *)malloc(sz*sizeof(int));
11664   int *work=new int[sz];
11665   std::copy(start,end,work);
11666   std::sort(work,work+sz);
11667   if(std::unique(work,work+sz)!=work+sz)
11668     {
11669       delete [] work;
11670       free(ret);
11671       throw INTERP_KERNEL::Exception("Some elements are equals in the specified array !");
11672     }
11673   std::map<int,int> m;
11674   for(int *workPt=work;workPt!=work+sz;workPt++)
11675     m[*workPt]=(int)std::distance(work,workPt);
11676   int *iter2=ret;
11677   for(const int *iter=start;iter!=end;iter++,iter2++)
11678     *iter2=m[*iter];
11679   delete [] work;
11680   return ret;
11681 }
11682
11683 /*!
11684  * Returns a new DataArrayInt containing an arithmetic progression
11685  * that is equal to the sequence returned by Python \c range(\a begin,\a  end,\a  step )
11686  * function.
11687  *  \param [in] begin - the start value of the result sequence.
11688  *  \param [in] end - limiting value, so that every value of the result array is less than
11689  *              \a end.
11690  *  \param [in] step - specifies the increment or decrement.
11691  *  \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
11692  *          array using decrRef() as it is no more needed.
11693  *  \throw If \a step == 0.
11694  *  \throw If \a end < \a begin && \a step > 0.
11695  *  \throw If \a end > \a begin && \a step < 0.
11696  */
11697 DataArrayInt *DataArrayInt::Range(int begin, int end, int step)
11698 {
11699   int nbOfTuples=GetNumberOfItemGivenBESRelative(begin,end,step,"DataArrayInt::Range");
11700   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
11701   ret->alloc(nbOfTuples,1);
11702   int *ptr=ret->getPointer();
11703   if(step>0)
11704     {
11705       for(int i=begin;i<end;i+=step,ptr++)
11706         *ptr=i;
11707     }
11708   else
11709     {
11710       for(int i=begin;i>end;i+=step,ptr++)
11711         *ptr=i;
11712     }
11713   return ret.retn();
11714 }
11715
11716 /*!
11717  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11718  * Server side.
11719  */
11720 void DataArrayInt::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
11721 {
11722   tinyInfo.resize(2);
11723   if(isAllocated())
11724     {
11725       tinyInfo[0]=getNumberOfTuples();
11726       tinyInfo[1]=getNumberOfComponents();
11727     }
11728   else
11729     {
11730       tinyInfo[0]=-1;
11731       tinyInfo[1]=-1;
11732     }
11733 }
11734
11735 /*!
11736  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11737  * Server side.
11738  */
11739 void DataArrayInt::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
11740 {
11741   if(isAllocated())
11742     {
11743       int nbOfCompo=getNumberOfComponents();
11744       tinyInfo.resize(nbOfCompo+1);
11745       tinyInfo[0]=getName();
11746       for(int i=0;i<nbOfCompo;i++)
11747         tinyInfo[i+1]=getInfoOnComponent(i);
11748     }
11749   else
11750     {
11751       tinyInfo.resize(1);
11752       tinyInfo[0]=getName();
11753     }
11754 }
11755
11756 /*!
11757  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11758  * This method returns if a feeding is needed.
11759  */
11760 bool DataArrayInt::resizeForUnserialization(const std::vector<int>& tinyInfoI)
11761 {
11762   int nbOfTuple=tinyInfoI[0];
11763   int nbOfComp=tinyInfoI[1];
11764   if(nbOfTuple!=-1 || nbOfComp!=-1)
11765     {
11766       alloc(nbOfTuple,nbOfComp);
11767       return true;
11768     }
11769   return false;
11770 }
11771
11772 /*!
11773  * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
11774  * This method returns if a feeding is needed.
11775  */
11776 void DataArrayInt::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<std::string>& tinyInfoS)
11777 {
11778   setName(tinyInfoS[0]);
11779   if(isAllocated())
11780     {
11781       int nbOfCompo=tinyInfoI[1];
11782       for(int i=0;i<nbOfCompo;i++)
11783         setInfoOnComponent(i,tinyInfoS[i+1]);
11784     }
11785 }
11786
11787 DataArrayIntIterator::DataArrayIntIterator(DataArrayInt *da):_da(da),_pt(0),_tuple_id(0),_nb_comp(0),_nb_tuple(0)
11788 {
11789   if(_da)
11790     {
11791       _da->incrRef();
11792       if(_da->isAllocated())
11793         {
11794           _nb_comp=da->getNumberOfComponents();
11795           _nb_tuple=da->getNumberOfTuples();
11796           _pt=da->getPointer();
11797         }
11798     }
11799 }
11800
11801 DataArrayIntIterator::~DataArrayIntIterator()
11802 {
11803   if(_da)
11804     _da->decrRef();
11805 }
11806
11807 DataArrayIntTuple *DataArrayIntIterator::nextt()
11808 {
11809   if(_tuple_id<_nb_tuple)
11810     {
11811       _tuple_id++;
11812       DataArrayIntTuple *ret=new DataArrayIntTuple(_pt,_nb_comp);
11813       _pt+=_nb_comp;
11814       return ret;
11815     }
11816   else
11817     return 0;
11818 }
11819
11820 DataArrayIntTuple::DataArrayIntTuple(int *pt, int nbOfComp):_pt(pt),_nb_of_compo(nbOfComp)
11821 {
11822 }
11823
11824 std::string DataArrayIntTuple::repr() const
11825 {
11826   std::ostringstream oss; oss << "(";
11827   for(int i=0;i<_nb_of_compo-1;i++)
11828     oss << _pt[i] << ", ";
11829   oss << _pt[_nb_of_compo-1] << ")";
11830   return oss.str();
11831 }
11832
11833 int DataArrayIntTuple::intValue() const
11834 {
11835   if(_nb_of_compo==1)
11836     return *_pt;
11837   throw INTERP_KERNEL::Exception("DataArrayIntTuple::intValue : DataArrayIntTuple instance has not exactly 1 component -> Not possible to convert it into an integer !");
11838 }
11839
11840 /*!
11841  * This method returns a newly allocated instance the caller should dealed with by a ParaMEDMEM::DataArrayInt::decrRef.
11842  * This method performs \b no copy of data. The content is only referenced using ParaMEDMEM::DataArrayInt::useArray with ownership set to \b false.
11843  * 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
11844  * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
11845  */
11846 DataArrayInt *DataArrayIntTuple::buildDAInt(int nbOfTuples, int nbOfCompo) const
11847 {
11848   if((_nb_of_compo==nbOfCompo && nbOfTuples==1) || (_nb_of_compo==nbOfTuples && nbOfCompo==1))
11849     {
11850       DataArrayInt *ret=DataArrayInt::New();
11851       ret->useExternalArrayWithRWAccess(_pt,nbOfTuples,nbOfCompo);
11852       return ret;
11853     }
11854   else
11855     {
11856       std::ostringstream oss; oss << "DataArrayIntTuple::buildDAInt : unable to build a requested DataArrayInt instance with nbofTuple=" << nbOfTuples << " and nbOfCompo=" << nbOfCompo;
11857       oss << ".\nBecause the number of elements in this is " << _nb_of_compo << " !";
11858       throw INTERP_KERNEL::Exception(oss.str().c_str());
11859     }
11860 }