- if(conn[0]==conn[1] || conn[2]==conn[3])
- throw INTERP_KERNEL::Exception("DataArrayInt::sortEachPairToMakeALinkedList : In the 2 first tuples presence of a pair filled with same ids !");
- int tmp[4];
- std::set<int> s;
- s.insert(conn,conn+4);
- if(s.size()!=3)
- throw INTERP_KERNEL::Exception("DataArrayInt::sortEachPairToMakeALinkedList : This can't be considered as a linked list regarding 2 first tuples !");
- if(std::count(conn,conn+4,conn[0])==2)
- {
- tmp[0]=conn[1];
- tmp[1]=conn[0];
- tmp[2]=conn[0];
- if(conn[2]==conn[0])
- { tmp[3]=conn[3]; }
- else
- { tmp[3]=conn[2];}
- std::copy(tmp,tmp+4,conn);
- }
- else
- {//here we are sure to have (std::count(conn,conn+4,conn[1])==2)
- if(conn[1]==conn[3])
- std::swap(conn[2],conn[3]);
- }
- }
- }
-}
-
-/*!
- *
- * \param [in] nbTimes specifies the nb of times each tuples in \a this will be duplicated contiguouly in returned DataArrayInt instance.
- * \a nbTimes should be at least equal to 1.
- * \return a newly allocated DataArrayInt having one component and number of tuples equal to \a nbTimes * \c this->getNumberOfTuples.
- * \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.
- */
-DataArrayInt *DataArrayInt::duplicateEachTupleNTimes(int nbTimes) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::duplicateEachTupleNTimes : this should have only one component !");
- if(nbTimes<1)
- throw INTERP_KERNEL::Exception("DataArrayInt::duplicateEachTupleNTimes : nb times should be >= 1 !");
- int nbTuples=getNumberOfTuples();
- const int *inPtr=getConstPointer();
- MCAuto<DataArrayInt> ret=DataArrayInt::New(); ret->alloc(nbTimes*nbTuples,1);
- int *retPtr=ret->getPointer();
- for(int i=0;i<nbTuples;i++,inPtr++)
- {
- int val=*inPtr;
- for(int j=0;j<nbTimes;j++,retPtr++)
- *retPtr=val;
- }
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * This method returns all different values found in \a this. This method throws if \a this has not been allocated.
- * But the number of components can be different from one.
- * \return a newly allocated array (that should be dealt by the caller) containing different values in \a this.
- */
-DataArrayInt *DataArrayInt::getDifferentValues() const
-{
- checkAllocated();
- std::set<int> ret;
- ret.insert(begin(),end());
- MCAuto<DataArrayInt> ret2=DataArrayInt::New(); ret2->alloc((int)ret.size(),1);
- std::copy(ret.begin(),ret.end(),ret2->getPointer());
- return ret2.retn();
-}
-
-/*!
- * This method is a refinement of DataArrayInt::getDifferentValues because it returns not only different values in \a this but also, for each of
- * them it tells which tuple id have this id.
- * This method works only on arrays with one component (if it is not the case call DataArrayInt::rearrange(1) ).
- * This method returns two arrays having same size.
- * 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.
- * 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]]
- */
-std::vector<DataArrayInt *> DataArrayInt::partitionByDifferentValues(std::vector<int>& differentIds) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::partitionByDifferentValues : this should have only one component !");
- int id=0;
- std::map<int,int> m,m2,m3;
- for(const int *w=begin();w!=end();w++)
- m[*w]++;
- differentIds.resize(m.size());
- std::vector<DataArrayInt *> ret(m.size());
- std::vector<int *> retPtr(m.size());
- for(std::map<int,int>::const_iterator it=m.begin();it!=m.end();it++,id++)
- {
- m2[(*it).first]=id;
- ret[id]=DataArrayInt::New();
- ret[id]->alloc((*it).second,1);
- retPtr[id]=ret[id]->getPointer();
- differentIds[id]=(*it).first;
- }
- id=0;
- for(const int *w=begin();w!=end();w++,id++)
- {
- retPtr[m2[*w]][m3[*w]++]=id;
- }
- return ret;
-}
-
-/*!
- * This method split ids in [0, \c this->getNumberOfTuples() ) using \a this array as a field of weight (>=0 each).
- * The aim of this method is to return a set of \a nbOfSlices chunk of contiguous ids as balanced as possible.
- *
- * \param [in] nbOfSlices - number of slices expected.
- * \return - a vector having a size equal to \a nbOfSlices giving the start (included) and the stop (excluded) of each chunks.
- *
- * \sa DataArray::GetSlice
- * \throw If \a this is not allocated or not with exactly one component.
- * \throw If an element in \a this if < 0.
- */
-std::vector< std::pair<int,int> > DataArrayInt::splitInBalancedSlices(int nbOfSlices) const
-{
- if(!isAllocated() || getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::splitInBalancedSlices : this array should have number of components equal to one and must be allocated !");
- if(nbOfSlices<=0)
- throw INTERP_KERNEL::Exception("DataArrayInt::splitInBalancedSlices : number of slices must be >= 1 !");
- int sum(accumulate(0)),nbOfTuples(getNumberOfTuples());
- int sumPerSlc(sum/nbOfSlices),pos(0);
- const int *w(begin());
- std::vector< std::pair<int,int> > ret(nbOfSlices);
- for(int i=0;i<nbOfSlices;i++)
- {
- std::pair<int,int> p(pos,-1);
- int locSum(0);
- while(locSum<sumPerSlc && pos<nbOfTuples) { pos++; locSum+=*w++; }
- if(i!=nbOfSlices-1)
- p.second=pos;
- else
- p.second=nbOfTuples;
- ret[i]=p;
- }
- return ret;
-}
-
-/*!
- * Returns a new DataArrayInt that is a sum of two given arrays. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * the result array (_a_) is a sum of the corresponding values of \a a1 and \a a2,
- * i.e.: _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, j ].
- * 2. The arrays have same number of tuples and one array, say _a2_, has one
- * component. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, 0 ].
- * 3. The arrays have same number of components and one array, say _a2_, has one
- * tuple. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ 0, j ].
- *
- * Info on components is copied either from the first array (in the first case) or from
- * the array with maximal number of elements (getNbOfElems()).
- * \param [in] a1 - an array to sum up.
- * \param [in] a2 - another array to sum up.
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
- * \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
- * none of them has number of tuples or components equal to 1.
- */
-DataArrayInt *DataArrayInt::Add(const DataArrayInt *a1, const DataArrayInt *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayInt::Add : input DataArrayInt instance is NULL !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- MCAuto<DataArrayInt> ret=0;
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- ret=DataArrayInt::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<int>());
- ret->copyStringInfoFrom(*a1);
- }
- else
- {
- int nbOfCompMin,nbOfCompMax;
- const DataArrayInt *aMin, *aMax;
- if(nbOfComp>nbOfComp2)
- {
- nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
- aMin=a2; aMax=a1;
- }
- else
- {
- nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
- aMin=a1; aMax=a2;
- }
- if(nbOfCompMin==1)
- {
- ret=DataArrayInt::New();
- ret->alloc(nbOfTuple,nbOfCompMax);
- const int *aMinPtr=aMin->getConstPointer();
- const int *aMaxPtr=aMax->getConstPointer();
- int *res=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++)
- res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::plus<int>(),aMinPtr[i]));
- ret->copyStringInfoFrom(*aMax);
- }
- else
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
- }
- }
- else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
- {
- if(nbOfComp==nbOfComp2)
- {
- int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
- const DataArrayInt *aMin=nbOfTuple>nbOfTuple2?a2:a1;
- const DataArrayInt *aMax=nbOfTuple>nbOfTuple2?a1:a2;
- const int *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
- ret=DataArrayInt::New();
- ret->alloc(nbOfTupleMax,nbOfComp);
- int *res=ret->getPointer();
- for(int i=0;i<nbOfTupleMax;i++)
- res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::plus<int>());
- ret->copyStringInfoFrom(*aMax);
- }
- else
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
- }
- else
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !");
- return ret.retn();
-}
-
-/*!
- * Adds values of another DataArrayInt to values of \a this one. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * \a other array is added to the corresponding value of \a this array, i.e.:
- * _a_ [ i, j ] += _other_ [ i, j ].
- * 2. The arrays have same number of tuples and \a other array has one component. Then
- * _a_ [ i, j ] += _other_ [ i, 0 ].
- * 3. The arrays have same number of components and \a other array has one tuple. Then
- * _a_ [ i, j ] += _a2_ [ 0, j ].
- *
- * \param [in] other - an array to add to \a this one.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
- * \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
- * \a other has number of both tuples and components not equal to 1.
- */
-void DataArrayInt::addEqual(const DataArrayInt *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayInt::addEqual : input DataArrayInt instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayInt::addEqual !";
- checkAllocated(); other->checkAllocated();
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- std::transform(begin(),end(),other->begin(),getPointer(),std::plus<int>());
- }
- else if(nbOfComp2==1)
- {
- int *ptr=getPointer();
- const int *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::plus<int>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- int *ptr=getPointer();
- const int *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::plus<int>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayInt that is a subtraction of two given arrays. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * the result array (_a_) is a subtraction of the corresponding values of \a a1 and
- * \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, j ].
- * 2. The arrays have same number of tuples and one array, say _a2_, has one
- * component. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, 0 ].
- * 3. The arrays have same number of components and one array, say _a2_, has one
- * tuple. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ 0, j ].
- *
- * Info on components is copied either from the first array (in the first case) or from
- * the array with maximal number of elements (getNbOfElems()).
- * \param [in] a1 - an array to subtract from.
- * \param [in] a2 - an array to subtract.
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
- * \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
- * none of them has number of tuples or components equal to 1.
- */
-DataArrayInt *DataArrayInt::Substract(const DataArrayInt *a1, const DataArrayInt *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayInt::Substract : input DataArrayInt instance is NULL !");
- int nbOfTuple1=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp1=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- if(nbOfTuple2==nbOfTuple1)
- {
- if(nbOfComp1==nbOfComp2)
- {
- MCAuto<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbOfTuple2,nbOfComp1);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus<int>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else if(nbOfComp2==1)
- {
- MCAuto<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const int *a2Ptr=a2->getConstPointer();
- const int *a1Ptr=a1->getConstPointer();
- int *res=ret->getPointer();
- for(int i=0;i<nbOfTuple1;i++)
- res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::minus<int>(),a2Ptr[i]));
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else
- {
- a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
- return 0;
- }
- }
- else if(nbOfTuple2==1)
- {
- a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !");
- MCAuto<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
- int *pt=ret->getPointer();
- for(int i=0;i<nbOfTuple1;i++)
- pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::minus<int>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else
- {
- a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception
- return 0;
- }
-}
-
-/*!
- * Subtract values of another DataArrayInt from values of \a this one. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * \a other array is subtracted from the corresponding value of \a this array, i.e.:
- * _a_ [ i, j ] -= _other_ [ i, j ].
- * 2. The arrays have same number of tuples and \a other array has one component. Then
- * _a_ [ i, j ] -= _other_ [ i, 0 ].
- * 3. The arrays have same number of components and \a other array has one tuple. Then
- * _a_ [ i, j ] -= _a2_ [ 0, j ].
- *
- * \param [in] other - an array to subtract from \a this one.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
- * \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
- * \a other has number of both tuples and components not equal to 1.
- */
-void DataArrayInt::substractEqual(const DataArrayInt *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayInt::substractEqual : input DataArrayInt instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayInt::substractEqual !";
- checkAllocated(); other->checkAllocated();
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- std::transform(begin(),end(),other->begin(),getPointer(),std::minus<int>());
- }
- else if(nbOfComp2==1)
- {
- int *ptr=getPointer();
- const int *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::minus<int>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- int *ptr=getPointer();
- const int *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::minus<int>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayInt that is a product of two given arrays. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * the result array (_a_) is a product of the corresponding values of \a a1 and
- * \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, j ].
- * 2. The arrays have same number of tuples and one array, say _a2_, has one
- * component. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, 0 ].
- * 3. The arrays have same number of components and one array, say _a2_, has one
- * tuple. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ 0, j ].
- *
- * Info on components is copied either from the first array (in the first case) or from
- * the array with maximal number of elements (getNbOfElems()).
- * \param [in] a1 - a factor array.
- * \param [in] a2 - another factor array.
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
- * \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
- * none of them has number of tuples or components equal to 1.
- */
-DataArrayInt *DataArrayInt::Multiply(const DataArrayInt *a1, const DataArrayInt *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayInt::Multiply : input DataArrayInt instance is NULL !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- MCAuto<DataArrayInt> ret=0;
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- ret=DataArrayInt::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies<int>());
- ret->copyStringInfoFrom(*a1);
- }
- else
- {
- int nbOfCompMin,nbOfCompMax;
- const DataArrayInt *aMin, *aMax;
- if(nbOfComp>nbOfComp2)