- checkAllocated();
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- expr.prepareExprEvaluationVec();
- //
- DataArrayDouble *newArr=DataArrayDouble::New();
- int nbOfTuples=getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- newArr->alloc(nbOfTuples,nbOfComp);
- const double *ptr=getConstPointer();
- double *ptrToFill=newArr->getPointer();
- for(int i=0;i<nbOfTuples;i++)
- {
- try
- {
- expr.evaluateExpr(nbOfComp,ptr+i*nbOfComp,ptrToFill+i*nbOfComp);
- }
- catch(INTERP_KERNEL::Exception& e)
- {
- std::ostringstream oss; oss << "For tuple # " << i << " with value (";
- std::copy(ptr+nbOfComp*i,ptr+nbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
- oss << ") : Evaluation of function failed ! " << e.what();
- newArr->decrRef();
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return newArr;
-}
-
-/*!
- * Returns a new DataArrayDouble created from \a this one by applying a function to every
- * tuple of \a this array. Textual data is not copied.
- * For more info see \ref MEDCouplingArrayApplyFunc2.
- * \param [in] nbOfComp - number of components in the result array.
- * \param [in] func - the expression defining how to transform a tuple of \a this array.
- * Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples as \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this is not allocated.
- * \throw If \a func contains vars that are not in \a this->getInfoOnComponent().
- * \throw If computing \a func fails.
- */
-DataArrayDouble *DataArrayDouble::applyFunc2(int nbOfComp, const std::string& func) const
-{
- checkAllocated();
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- std::set<std::string> vars;
- expr.getTrueSetOfVars(vars);
- int oldNbOfComp=getNumberOfComponents();
- if((int)vars.size()>oldNbOfComp)
- {
- std::ostringstream oss; oss << "The field has " << oldNbOfComp << " components and there are ";
- oss << vars.size() << " variables : ";
- std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- expr.prepareExprEvaluation(getVarsOnComponent(),oldNbOfComp,nbOfComp);
- //
- DataArrayDouble *newArr=DataArrayDouble::New();
- int nbOfTuples=getNumberOfTuples();
- newArr->alloc(nbOfTuples,nbOfComp);
- const double *ptr=getConstPointer();
- double *ptrToFill=newArr->getPointer();
- for(int i=0;i<nbOfTuples;i++)
- {
- try
- {
- expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
- }
- catch(INTERP_KERNEL::Exception& e)
- {
- std::ostringstream oss; oss << "For tuple # " << i << " with value (";
- std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
- oss << ") : Evaluation of function failed !" << e.what();
- newArr->decrRef();
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return newArr;
-}
-
-/*!
- * Returns a new DataArrayDouble created from \a this one by applying a function to every
- * tuple of \a this array. Textual data is not copied.
- * For more info see \ref MEDCouplingArrayApplyFunc3.
- * \param [in] nbOfComp - number of components in the result array.
- * \param [in] varsOrder - sequence of vars defining their order.
- * \param [in] func - the expression defining how to transform a tuple of \a this array.
- * Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples as \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this is not allocated.
- * \throw If \a func contains vars not in \a varsOrder.
- * \throw If computing \a func fails.
- */
-DataArrayDouble *DataArrayDouble::applyFunc3(int nbOfComp, const std::vector<std::string>& varsOrder, const std::string& func) const
-{
- checkAllocated();
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- std::set<std::string> vars;
- expr.getTrueSetOfVars(vars);
- int oldNbOfComp=getNumberOfComponents();
- if((int)vars.size()>oldNbOfComp)
- {
- std::ostringstream oss; oss << "The field has " << oldNbOfComp << " components and there are ";
- oss << vars.size() << " variables : ";
- std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- expr.prepareExprEvaluation(varsOrder,oldNbOfComp,nbOfComp);
- //
- DataArrayDouble *newArr=DataArrayDouble::New();
- int nbOfTuples=getNumberOfTuples();
- newArr->alloc(nbOfTuples,nbOfComp);
- const double *ptr=getConstPointer();
- double *ptrToFill=newArr->getPointer();
- for(int i=0;i<nbOfTuples;i++)
- {
- try
- {
- expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
- }
- catch(INTERP_KERNEL::Exception& e)
- {
- std::ostringstream oss; oss << "For tuple # " << i << " with value (";
- std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
- oss << ") : Evaluation of function failed !" << e.what();
- newArr->decrRef();
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return newArr;
-}
-
-void DataArrayDouble::applyFuncFast32(const std::string& func)
-{
- checkAllocated();
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- char *funcStr=expr.compileX86();
- MYFUNCPTR funcPtr;
- *((void **)&funcPtr)=funcStr;//he he...
- //
- double *ptr=getPointer();
- int nbOfComp=getNumberOfComponents();
- int nbOfTuples=getNumberOfTuples();
- int nbOfElems=nbOfTuples*nbOfComp;
- for(int i=0;i<nbOfElems;i++,ptr++)
- *ptr=funcPtr(*ptr);
- declareAsNew();
-}
-
-void DataArrayDouble::applyFuncFast64(const std::string& func)
-{
- checkAllocated();
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- char *funcStr=expr.compileX86_64();
- MYFUNCPTR funcPtr;
- *((void **)&funcPtr)=funcStr;//he he...
- //
- double *ptr=getPointer();
- int nbOfComp=getNumberOfComponents();
- int nbOfTuples=getNumberOfTuples();
- int nbOfElems=nbOfTuples*nbOfComp;
- for(int i=0;i<nbOfElems;i++,ptr++)
- *ptr=funcPtr(*ptr);
- declareAsNew();
-}
-
-DataArrayDoubleIterator *DataArrayDouble::iterator()
-{
- return new DataArrayDoubleIterator(this);
-}
-
-/*!
- * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
- * array whose values are within a given range. Textual data is not copied.
- * \param [in] vmin - a lowest acceptable value (included).
- * \param [in] vmax - a greatest acceptable value (included).
- * \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 \a this->getNumberOfComponents() != 1.
- *
- * \sa DataArrayDouble::getIdsNotInRange
- *
- * \if ENABLE_EXAMPLES
- * \ref cpp_mcdataarraydouble_getidsinrange "Here is a C++ example".<br>
- * \ref py_mcdataarraydouble_getidsinrange "Here is a Python example".
- * \endif
- */
-DataArrayInt *DataArrayDouble::getIdsInRange(double vmin, double vmax) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsInRange : this must have exactly one component !");
- const double *cptr(begin());
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
- int nbOfTuples(getNumberOfTuples());
- for(int i=0;i<nbOfTuples;i++,cptr++)
- if(*cptr>=vmin && *cptr<=vmax)
- ret->pushBackSilent(i);
- return ret.retn();
-}
-
-/*!
- * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
- * array whose values are not within a given range. Textual data is not copied.
- * \param [in] vmin - a lowest not acceptable value (excluded).
- * \param [in] vmax - a greatest not acceptable value (excluded).
- * \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 \a this->getNumberOfComponents() != 1.
- *
- * \sa DataArrayDouble::getIdsInRange
- */
-DataArrayInt *DataArrayDouble::getIdsNotInRange(double vmin, double vmax) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsNotInRange : this must have exactly one component !");
- const double *cptr(begin());
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
- int nbOfTuples(getNumberOfTuples());
- for(int i=0;i<nbOfTuples;i++,cptr++)
- if(*cptr<vmin || *cptr>vmax)
- ret->pushBackSilent(i);
- return ret.retn();
-}
-
-/*!
- * Returns a new DataArrayDouble by concatenating two given arrays, so that (1) the number
- * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
- * the number of component in the result array is same as that of each of given arrays.
- * Info on components is copied from the first of the given arrays. Number of components
- * in the given arrays must be the same.
- * \param [in] a1 - an array to include in the result array.
- * \param [in] a2 - another array to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If both \a a1 and \a a2 are NULL.
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents().
- */
-DataArrayDouble *DataArrayDouble::Aggregate(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- std::vector<const DataArrayDouble *> tmp(2);
- tmp[0]=a1; tmp[1]=a2;
- return Aggregate(tmp);
-}
-
-/*!
- * Returns a new DataArrayDouble by concatenating all given arrays, so that (1) the number
- * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
- * the number of component in the result array is same as that of each of given arrays.
- * Info on components is copied from the first of the given arrays. Number of components
- * in the given arrays must be the same.
- * If the number of non null of elements in \a arr is equal to one the returned object is a copy of it
- * not the object itself.
- * \param [in] arr - a sequence of arrays to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If all arrays within \a arr are NULL.
- * \throw If getNumberOfComponents() of arrays within \a arr.
- */
-DataArrayDouble *DataArrayDouble::Aggregate(const std::vector<const DataArrayDouble *>& arr)
-{
- std::vector<const DataArrayDouble *> a;
- for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
- if(*it4)
- a.push_back(*it4);
- if(a.empty())
- throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : input list must contain at least one NON EMPTY DataArrayDouble !");
- std::vector<const DataArrayDouble *>::const_iterator it=a.begin();
- int nbOfComp=(*it)->getNumberOfComponents();
- int nbt=(*it++)->getNumberOfTuples();
- for(int i=1;it!=a.end();it++,i++)
- {
- if((*it)->getNumberOfComponents()!=nbOfComp)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : Nb of components mismatch for array aggregation !");
- nbt+=(*it)->getNumberOfTuples();
- }
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbt,nbOfComp);
- double *pt=ret->getPointer();
- for(it=a.begin();it!=a.end();it++)
- pt=std::copy((*it)->getConstPointer(),(*it)->getConstPointer()+(*it)->getNbOfElems(),pt);
- ret->copyStringInfoFrom(*(a[0]));
- return ret.retn();
-}
-
-/*!
- * Returns a new DataArrayDouble by aggregating two given arrays, so that (1) the number
- * of components in the result array is a sum of the number of components of given arrays
- * and (2) the number of tuples in the result array is same as that of each of given
- * arrays. In other words the i-th tuple of result array includes all components of
- * i-th tuples of all given arrays.
- * Number of tuples in the given arrays must be the same.
- * \param [in] a1 - an array to include in the result array.
- * \param [in] a2 - another array to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If both \a a1 and \a a2 are NULL.
- * \throw If any given array is not allocated.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- */
-DataArrayDouble *DataArrayDouble::Meld(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- std::vector<const DataArrayDouble *> arr(2);
- arr[0]=a1; arr[1]=a2;
- return Meld(arr);
-}
-
-/*!
- * Returns a new DataArrayDouble by aggregating all given arrays, so that (1) the number
- * of components in the result array is a sum of the number of components of given arrays
- * and (2) the number of tuples in the result array is same as that of each of given
- * arrays. In other words the i-th tuple of result array includes all components of
- * i-th tuples of all given arrays.
- * Number of tuples in the given arrays must be the same.
- * \param [in] arr - a sequence of arrays to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If all arrays within \a arr are NULL.
- * \throw If any given array is not allocated.
- * \throw If getNumberOfTuples() of arrays within \a arr is different.
- */
-DataArrayDouble *DataArrayDouble::Meld(const std::vector<const DataArrayDouble *>& arr)
-{
- std::vector<const DataArrayDouble *> a;
- for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
- if(*it4)
- a.push_back(*it4);
- if(a.empty())
- throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : input list must contain at least one NON EMPTY DataArrayDouble !");
- std::vector<const DataArrayDouble *>::const_iterator it;
- for(it=a.begin();it!=a.end();it++)
- (*it)->checkAllocated();
- it=a.begin();
- int nbOfTuples=(*it)->getNumberOfTuples();
- std::vector<int> nbc(a.size());
- std::vector<const double *> pts(a.size());
- nbc[0]=(*it)->getNumberOfComponents();
- pts[0]=(*it++)->getConstPointer();
- for(int i=1;it!=a.end();it++,i++)
- {
- if(nbOfTuples!=(*it)->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : mismatch of number of tuples !");
- nbc[i]=(*it)->getNumberOfComponents();
- pts[i]=(*it)->getConstPointer();
- }
- int totalNbOfComp=std::accumulate(nbc.begin(),nbc.end(),0);
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuples,totalNbOfComp);
- double *retPtr=ret->getPointer();
- for(int i=0;i<nbOfTuples;i++)
- for(int j=0;j<(int)a.size();j++)
- {
- retPtr=std::copy(pts[j],pts[j]+nbc[j],retPtr);
- pts[j]+=nbc[j];
- }
- int k=0;
- for(int i=0;i<(int)a.size();i++)
- for(int j=0;j<nbc[i];j++,k++)
- ret->setInfoOnComponent(k,a[i]->getInfoOnComponent(j));
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing a dot product of two given arrays, so that
- * the i-th tuple of the result array is a sum of products of j-th components of i-th
- * tuples of given arrays (\f$ a_i = \sum_{j=1}^n a1_j * a2_j \f$).
- * Info on components and name is copied from the first of the given arrays.
- * Number of tuples and components in the given arrays must be the same.
- * \param [in] a1 - a given array.
- * \param [in] a2 - another given array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * 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 any given array is not allocated.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
- */
-DataArrayDouble *DataArrayDouble::Dot(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Dot : input DataArrayDouble instance is NULL !");
- a1->checkAllocated();
- a2->checkAllocated();
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Dot !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Dot !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,1);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- {
- double sum=0.;
- for(int j=0;j<nbOfComp;j++)
- sum+=a1Ptr[i*nbOfComp+j]*a2Ptr[i*nbOfComp+j];
- retPtr[i]=sum;
- }
- ret->setInfoOnComponent(0,a1->getInfoOnComponent(0));
- ret->setName(a1->getName());
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing a cross product of two given arrays, so that
- * the i-th tuple of the result array contains 3 components of a vector which is a cross
- * product of two vectors defined by the i-th tuples of given arrays.
- * Info on components is copied from the first of the given arrays.
- * Number of tuples in the given arrays must be the same.
- * Number of components in the given arrays must be 3.
- * \param [in] a1 - a given array.
- * \param [in] a2 - another given array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * 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()
- * \throw If \a a1->getNumberOfComponents() != 3
- * \throw If \a a2->getNumberOfComponents() != 3
- */
-DataArrayDouble *DataArrayDouble::CrossProduct(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::CrossProduct : input DataArrayDouble instance is NULL !");
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array crossProduct !");
- if(nbOfComp!=3)
- throw INTERP_KERNEL::Exception("Nb of components must be equal to 3 for array crossProduct !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array crossProduct !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,3);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- {
- retPtr[3*i]=a1Ptr[3*i+1]*a2Ptr[3*i+2]-a1Ptr[3*i+2]*a2Ptr[3*i+1];
- retPtr[3*i+1]=a1Ptr[3*i+2]*a2Ptr[3*i]-a1Ptr[3*i]*a2Ptr[3*i+2];
- retPtr[3*i+2]=a1Ptr[3*i]*a2Ptr[3*i+1]-a1Ptr[3*i+1]*a2Ptr[3*i];
- }
- ret->copyStringInfoFrom(*a1);
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing maximal values of two given arrays.
- * Info on components is copied from the first of the given arrays.
- * Number of tuples and components in the given arrays must be the same.
- * \param [in] a1 - an array to compare values with another one.
- * \param [in] a2 - another array to compare values with the first one.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * 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()
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
- */
-DataArrayDouble *DataArrayDouble::Max(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Max : input DataArrayDouble instance is NULL !");
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Max !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Max !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- int nbElem=nbOfTuple*nbOfComp;
- for(int i=0;i<nbElem;i++)
- retPtr[i]=std::max(a1Ptr[i],a2Ptr[i]);
- ret->copyStringInfoFrom(*a1);
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing minimal values of two given arrays.
- * Info on components is copied from the first of the given arrays.
- * Number of tuples and components in the given arrays must be the same.
- * \param [in] a1 - an array to compare values with another one.
- * \param [in] a2 - another array to compare values with the first one.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * 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()
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
- */
-DataArrayDouble *DataArrayDouble::Min(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Min : input DataArrayDouble instance is NULL !");
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array min !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array min !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- int nbElem=nbOfTuple*nbOfComp;
- for(int i=0;i<nbElem;i++)
- retPtr[i]=std::min(a1Ptr[i],a2Ptr[i]);
- ret->copyStringInfoFrom(*a1);
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble 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 DataArrayDouble * - the new instance of DataArrayDouble.
- * 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.
- */
-DataArrayDouble *DataArrayDouble::Add(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Add : input DataArrayDouble instance is NULL !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=0;
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<double>());
- ret->copyStringInfoFrom(*a1);
- }
- else
- {
- int nbOfCompMin,nbOfCompMax;
- const DataArrayDouble *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=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfCompMax);
- const double *aMinPtr=aMin->getConstPointer();
- const double *aMaxPtr=aMax->getConstPointer();
- double *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<double>(),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 DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1;
- const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2;
- const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
- ret=DataArrayDouble::New();
- ret->alloc(nbOfTupleMax,nbOfComp);
- double *res=ret->getPointer();
- for(int i=0;i<nbOfTupleMax;i++)
- res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::plus<double>());
- 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 DataArrayDouble 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 DataArrayDouble::addEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::addEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::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<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *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<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *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<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble 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 DataArrayDouble * - the new instance of DataArrayDouble.
- * 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.
- */
-DataArrayDouble *DataArrayDouble::Substract(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Substract : input DataArrayDouble 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)
- {
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple2,nbOfComp1);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus<double>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else if(nbOfComp2==1)
- {
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const double *a2Ptr=a2->getConstPointer();
- const double *a1Ptr=a1->getConstPointer();
- double *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<double>(),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 !");
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
- double *pt=ret->getPointer();
- for(int i=0;i<nbOfTuple1;i++)
- pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::minus<double>());
- 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 DataArrayDouble 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 DataArrayDouble::substractEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::substractEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::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<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *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<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *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<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble 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 DataArrayDouble * - the new instance of DataArrayDouble.
- * 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.
- */
-DataArrayDouble *DataArrayDouble::Multiply(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Multiply : input DataArrayDouble instance is NULL !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=0;
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies<double>());
- ret->copyStringInfoFrom(*a1);
- }
- else
- {
- int nbOfCompMin,nbOfCompMax;
- const DataArrayDouble *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=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfCompMax);
- const double *aMinPtr=aMin->getConstPointer();
- const double *aMaxPtr=aMax->getConstPointer();
- double *res=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++)
- res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::multiplies<double>(),aMinPtr[i]));
- ret->copyStringInfoFrom(*aMax);
- }
- else
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
- }
- }
- else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1))
- {
- if(nbOfComp==nbOfComp2)
- {
- int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2);
- const DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1;
- const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2;
- const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer();
- ret=DataArrayDouble::New();
- ret->alloc(nbOfTupleMax,nbOfComp);
- double *res=ret->getPointer();
- for(int i=0;i<nbOfTupleMax;i++)
- res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::multiplies<double>());
- ret->copyStringInfoFrom(*aMax);
- }
- else
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
- }
- else
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !");
- return ret.retn();
-}
-
-/*!
- * Multiply values of another DataArrayDouble 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 multiplied to the corresponding value of \a this array, i.e.
- * _this_ [ i, j ] *= _other_ [ i, j ].
- * 2. The arrays have same number of tuples and \a other array has one component. Then
- * _this_ [ i, j ] *= _other_ [ i, 0 ].
- * 3. The arrays have same number of components and \a other array has one tuple. Then
- * _this_ [ i, j ] *= _a2_ [ 0, j ].
- *
- * \param [in] other - an array to multiply 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 DataArrayDouble::multiplyEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::multiplyEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::multiplyEqual !";
- 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::multiplies<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *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::multiplies<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::multiplies<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble that is a division 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 division 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()).
- * \warning No check of division by zero is performed!
- * \param [in] a1 - a numerator array.
- * \param [in] a2 - a denominator array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * 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.
- */
-DataArrayDouble *DataArrayDouble::Divide(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Divide : input DataArrayDouble 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)
- {
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple2,nbOfComp1);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides<double>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else if(nbOfComp2==1)
- {
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const double *a2Ptr=a2->getConstPointer();
- const double *a1Ptr=a1->getConstPointer();
- double *res=ret->getPointer();
- for(int i=0;i<nbOfTuple1;i++)
- res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::divides<double>(),a2Ptr[i]));
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else
- {
- a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
- return 0;
- }
- }
- else if(nbOfTuple2==1)
- {
- a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !");
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
- double *pt=ret->getPointer();
- for(int i=0;i<nbOfTuple1;i++)
- pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::divides<double>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else
- {
- a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception
- return 0;
- }
-}
-
-/*!
- * Divide values of \a this array by values of another DataArrayDouble. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * \a this array is divided by the corresponding value of \a other one, 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 ].
- *
- * \warning No check of division by zero is performed!
- * \param [in] other - an array to divide \a this one by.
- * \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 DataArrayDouble::divideEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::divideEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::divideEqual !";
- 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::divides<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *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::divides<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::divides<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble that is the result of pow of two given arrays. There are 3
- * valid cases.
- *
- * \param [in] a1 - an array to pow up.
- * \param [in] a2 - another array to sum up.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * 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()
- * \throw If \a a1->getNumberOfComponents() != 1 or \a a2->getNumberOfComponents() != 1.
- * \throw If there is a negative value in \a a1.
- */
-DataArrayDouble *DataArrayDouble::Pow(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : at least one of input instances is null !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- if(nbOfTuple!=nbOfTuple2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of tuples mismatches !");
- if(nbOfComp!=1 || nbOfComp2!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of components of both arrays must be equal to 1 !");
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(nbOfTuple,1);
- const double *ptr1(a1->begin()),*ptr2(a2->begin());
- double *ptr=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,ptr1++,ptr2++,ptr++)
- {
- if(*ptr1>=0)
- {
- *ptr=pow(*ptr1,*ptr2);
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::Pow : on tuple #" << i << " of a1 value is < 0 (" << *ptr1 << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return ret.retn();
-}
-
-/*!
- * Apply pow on values of another DataArrayDouble to values of \a this one.
- *
- * \param [in] other - an array to pow to \a this one.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples()
- * \throw If \a this->getNumberOfComponents() != 1 or \a other->getNumberOfComponents() != 1
- * \throw If there is a negative value in \a this.
- */
-void DataArrayDouble::powEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : input instance is null !");
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple!=nbOfTuple2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of tuples mismatches !");
- if(nbOfComp!=1 || nbOfComp2!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of components of both arrays must be equal to 1 !");
- double *ptr=getPointer();
- const double *ptrc=other->begin();
- for(int i=0;i<nbOfTuple;i++,ptrc++,ptr++)
- {
- if(*ptr>=0)
- *ptr=pow(*ptr,*ptrc);
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::powEqual : on tuple #" << i << " of this value is < 0 (" << *ptr << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- declareAsNew();
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- * Server side.
- */
-void DataArrayDouble::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
-{
- tinyInfo.resize(2);
- if(isAllocated())
- {
- tinyInfo[0]=getNumberOfTuples();
- tinyInfo[1]=getNumberOfComponents();
- }
- else
- {
- tinyInfo[0]=-1;
- tinyInfo[1]=-1;
- }
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- * Server side.
- */
-void DataArrayDouble::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
-{
- if(isAllocated())
- {
- int nbOfCompo=getNumberOfComponents();
- tinyInfo.resize(nbOfCompo+1);
- tinyInfo[0]=getName();
- for(int i=0;i<nbOfCompo;i++)
- tinyInfo[i+1]=getInfoOnComponent(i);
- }
- else
- {
- tinyInfo.resize(1);
- tinyInfo[0]=getName();
- }
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- * This method returns if a feeding is needed.
- */
-bool DataArrayDouble::resizeForUnserialization(const std::vector<int>& tinyInfoI)
-{
- int nbOfTuple=tinyInfoI[0];
- int nbOfComp=tinyInfoI[1];
- if(nbOfTuple!=-1 || nbOfComp!=-1)
- {
- alloc(nbOfTuple,nbOfComp);
- return true;
- }
- return false;
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- */
-void DataArrayDouble::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<std::string>& tinyInfoS)
-{
- setName(tinyInfoS[0]);
- if(isAllocated())
- {
- int nbOfCompo=getNumberOfComponents();
- for(int i=0;i<nbOfCompo;i++)
- setInfoOnComponent(i,tinyInfoS[i+1]);
- }
-}
-
-DataArrayDoubleIterator::DataArrayDoubleIterator(DataArrayDouble *da):_da(da),_tuple_id(0),_nb_comp(0),_nb_tuple(0)
-{
- if(_da)
- {
- _da->incrRef();
- if(_da->isAllocated())
- {
- _nb_comp=da->getNumberOfComponents();
- _nb_tuple=da->getNumberOfTuples();
- _pt=da->getPointer();
- }
- }
-}
-
-DataArrayDoubleIterator::~DataArrayDoubleIterator()
-{
- if(_da)
- _da->decrRef();
-}
-
-DataArrayDoubleTuple *DataArrayDoubleIterator::nextt()
-{
- if(_tuple_id<_nb_tuple)
- {
- _tuple_id++;
- DataArrayDoubleTuple *ret=new DataArrayDoubleTuple(_pt,_nb_comp);
- _pt+=_nb_comp;
- return ret;
- }
- else
- return 0;
-}
-
-DataArrayDoubleTuple::DataArrayDoubleTuple(double *pt, int nbOfComp):_pt(pt),_nb_of_compo(nbOfComp)
-{
-}
-
-
-std::string DataArrayDoubleTuple::repr() const
-{
- std::ostringstream oss; oss.precision(17); oss << "(";
- for(int i=0;i<_nb_of_compo-1;i++)
- oss << _pt[i] << ", ";
- oss << _pt[_nb_of_compo-1] << ")";
- return oss.str();
-}
-
-double DataArrayDoubleTuple::doubleValue() const
-{
- if(_nb_of_compo==1)
- return *_pt;
- throw INTERP_KERNEL::Exception("DataArrayDoubleTuple::doubleValue : DataArrayDoubleTuple instance has not exactly 1 component -> Not possible to convert it into a double precision float !");
-}
-
-/*!
- * This method returns a newly allocated instance the caller should dealed with by a ParaMEDMEM::DataArrayDouble::decrRef.
- * This method performs \b no copy of data. The content is only referenced using ParaMEDMEM::DataArrayDouble::useArray with ownership set to \b false.
- * 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
- * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
- */
-DataArrayDouble *DataArrayDoubleTuple::buildDADouble(int nbOfTuples, int nbOfCompo) const
-{
- if((_nb_of_compo==nbOfCompo && nbOfTuples==1) || (_nb_of_compo==nbOfTuples && nbOfCompo==1))
- {
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->useExternalArrayWithRWAccess(_pt,nbOfTuples,nbOfCompo);
- return ret;
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayDoubleTuple::buildDADouble : unable to build a requested DataArrayDouble instance with nbofTuple=" << nbOfTuples << " and nbOfCompo=" << nbOfCompo;
- oss << ".\nBecause the number of elements in this is " << _nb_of_compo << " !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
-}
-
-/*!
- * Returns a new instance of DataArrayInt. The caller is to delete this array
- * using decrRef() as it is no more needed.
- */
-DataArrayInt *DataArrayInt::New()
-{
- return new DataArrayInt;
-}
-
-/*!
- * Checks if raw data is allocated. Read more on the raw data
- * in \ref MEDCouplingArrayBasicsTuplesAndCompo "DataArrays infos" for more information.
- * \return bool - \a true if the raw data is allocated, \a false else.
- */
-bool DataArrayInt::isAllocated() const
-{
- return getConstPointer()!=0;
-}
-
-/*!
- * Checks if raw data is allocated and throws an exception if it is not the case.
- * \throw If the raw data is not allocated.
- */
-void DataArrayInt::checkAllocated() const
-{
- if(!isAllocated())
- throw INTERP_KERNEL::Exception("DataArrayInt::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !");
-}
-
-/*!
- * This method desallocated \a this without modification of informations relative to the components.
- * After call of this method, DataArrayInt::isAllocated will return false.
- * If \a this is already not allocated, \a this is let unchanged.
- */
-void DataArrayInt::desallocate()
-{
- _mem.destroy();
-}
-
-std::size_t DataArrayInt::getHeapMemorySizeWithoutChildren() const
-{
- std::size_t sz(_mem.getNbOfElemAllocated());
- sz*=sizeof(int);
- return DataArray::getHeapMemorySizeWithoutChildren()+sz;
-}
-
-/*!
- * Returns the only one value in \a this, if and only if number of elements
- * (nb of tuples * nb of components) is equal to 1, and that \a this is allocated.
- * \return double - the sole value stored in \a this array.
- * \throw If at least one of conditions stated above is not fulfilled.
- */
-int DataArrayInt::intValue() const
-{
- if(isAllocated())
- {
- if(getNbOfElems()==1)
- {
- return *getConstPointer();
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is allocated but number of elements is not equal to 1 !");
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is not allocated !");
-}
-
-/*!
- * Returns an integer value characterizing \a this array, which is useful for a quick
- * comparison of many instances of DataArrayInt.
- * \return int - the hash value.
- * \throw If \a this is not allocated.
- */
-int DataArrayInt::getHashCode() const
-{
- checkAllocated();
- std::size_t nbOfElems=getNbOfElems();
- int ret=nbOfElems*65536;
- int delta=3;
- if(nbOfElems>48)
- delta=nbOfElems/8;
- int ret0=0;
- const int *pt=begin();
- for(std::size_t i=0;i<nbOfElems;i+=delta)
- ret0+=pt[i] & 0x1FFF;
- return ret+ret0;
-}
-
-/*!
- * Checks the number of tuples.
- * \return bool - \a true if getNumberOfTuples() == 0, \a false else.
- * \throw If \a this is not allocated.
- */
-bool DataArrayInt::empty() const
-{
- checkAllocated();
- return getNumberOfTuples()==0;
-}
-
-/*!
- * Returns a full copy of \a this. For more info on copying data arrays see
- * \ref MEDCouplingArrayBasicsCopyDeep.
- * \return DataArrayInt * - a new instance of DataArrayInt.
- */
-DataArrayInt *DataArrayInt::deepCpy() const
-{
- return new DataArrayInt(*this);
-}
-
-/*!
- * Returns either a \a deep or \a shallow copy of this array. For more info see
- * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
- * \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
- * \return DataArrayInt * - either a new instance of DataArrayInt (if \a dCpy
- * == \a true) or \a this instance (if \a dCpy == \a false).
- */
-DataArrayInt *DataArrayInt::performCpy(bool dCpy) const
-{
- if(dCpy)
- return deepCpy();
- else
- {
- incrRef();
- return const_cast<DataArrayInt *>(this);
- }
-}
-
-/*!
- * Copies all the data from another DataArrayInt. For more info see
- * \ref MEDCouplingArrayBasicsCopyDeepAssign.
- * \param [in] other - another instance of DataArrayInt to copy data from.
- * \throw If the \a other is not allocated.
- */
-void DataArrayInt::cpyFrom(const DataArrayInt& other)
-{
- other.checkAllocated();
- int nbOfTuples=other.getNumberOfTuples();
- int nbOfComp=other.getNumberOfComponents();
- allocIfNecessary(nbOfTuples,nbOfComp);
- std::size_t nbOfElems=(std::size_t)nbOfTuples*nbOfComp;
- int *pt=getPointer();
- const int *ptI=other.getConstPointer();
- for(std::size_t i=0;i<nbOfElems;i++)
- pt[i]=ptI[i];
- copyStringInfoFrom(other);
-}
-
-/*!
- * This method reserve nbOfElems elements in memory ( nbOfElems*4 bytes ) \b without impacting the number of tuples in \a this.
- * 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.
- * If \a this has not already been allocated, number of components is set to one.
- * This method allows to reduce number of reallocations on invokation of DataArrayInt::pushBackSilent and DataArrayInt::pushBackValsSilent on \a this.
- *
- * \sa DataArrayInt::pack, DataArrayInt::pushBackSilent, DataArrayInt::pushBackValsSilent
- */
-void DataArrayInt::reserve(std::size_t nbOfElems)
-{
- int nbCompo=getNumberOfComponents();
- if(nbCompo==1)
- {
- _mem.reserve(nbOfElems);
- }
- else if(nbCompo==0)
- {
- _mem.reserve(nbOfElems);
- _info_on_compo.resize(1);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::reserve : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * 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
- * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
- *
- * \param [in] val the value to be added in \a this
- * \throw If \a this has already been allocated with number of components different from one.
- * \sa DataArrayInt::pushBackValsSilent
- */
-void DataArrayInt::pushBackSilent(int val)
-{
- int nbCompo=getNumberOfComponents();
- if(nbCompo==1)
- _mem.pushBack(val);
- else if(nbCompo==0)
- {
- _info_on_compo.resize(1);
- _mem.pushBack(val);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::pushBackSilent : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * 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
- * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
- *
- * \param [in] valsBg - an array of values to push at the end of \this.
- * \param [in] valsEnd - specifies the end of the array \a valsBg, so that
- * the last value of \a valsBg is \a valsEnd[ -1 ].
- * \throw If \a this has already been allocated with number of components different from one.
- * \sa DataArrayInt::pushBackSilent
- */
-void DataArrayInt::pushBackValsSilent(const int *valsBg, const int *valsEnd)
-{
- int nbCompo=getNumberOfComponents();
- if(nbCompo==1)
- _mem.insertAtTheEnd(valsBg,valsEnd);
- else if(nbCompo==0)
- {
- _info_on_compo.resize(1);
- _mem.insertAtTheEnd(valsBg,valsEnd);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::pushBackValsSilent : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * This method returns silently ( without updating time label in \a this ) the last value, if any and suppress it.
- * \throw If \a this is already empty.
- * \throw If \a this has number of components different from one.
- */
-int DataArrayInt::popBackSilent()
-{
- if(getNumberOfComponents()==1)
- return _mem.popBack();
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::popBackSilent : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * 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.
- *
- * \sa DataArrayInt::getHeapMemorySizeWithoutChildren, DataArrayInt::reserve
- */
-void DataArrayInt::pack() const
-{
- _mem.pack();
-}
-
-/*!
- * Allocates the raw data in memory. If exactly as same memory as needed already
- * allocated, it is not re-allocated.
- * \param [in] nbOfTuple - number of tuples of data to allocate.
- * \param [in] nbOfCompo - number of components of data to allocate.
- * \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
- */
-void DataArrayInt::allocIfNecessary(int nbOfTuple, int nbOfCompo)
-{
- if(isAllocated())
- {
- if(nbOfTuple!=getNumberOfTuples() || nbOfCompo!=getNumberOfComponents())
- alloc(nbOfTuple,nbOfCompo);
- }
- else
- alloc(nbOfTuple,nbOfCompo);
-}
-
-/*!
- * Allocates the raw data in memory. If the memory was already allocated, then it is
- * freed and re-allocated. See an example of this method use
- * \ref MEDCouplingArraySteps1WC "here".
- * \param [in] nbOfTuple - number of tuples of data to allocate.
- * \param [in] nbOfCompo - number of components of data to allocate.
- * \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
- */
-void DataArrayInt::alloc(int nbOfTuple, int nbOfCompo)
-{
- if(nbOfTuple<0 || nbOfCompo<0)
- throw INTERP_KERNEL::Exception("DataArrayInt::alloc : request for negative length of data !");
- _info_on_compo.resize(nbOfCompo);
- _mem.alloc(nbOfCompo*(std::size_t)nbOfTuple);
- declareAsNew();
-}
-
-/*!
- * Assign zero to all values in \a this array. To know more on filling arrays see
- * \ref MEDCouplingArrayFill.
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::fillWithZero()
-{
- checkAllocated();
- _mem.fillWithValue(0);
- declareAsNew();
-}
-
-/*!
- * Assign \a val to all values in \a this array. To know more on filling arrays see
- * \ref MEDCouplingArrayFill.
- * \param [in] val - the value to fill with.
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::fillWithValue(int val)
-{
- checkAllocated();
- _mem.fillWithValue(val);
- declareAsNew();
-}
-
-/*!
- * Set all values in \a this array so that the i-th element equals to \a init + i
- * (i starts from zero). To know more on filling arrays see \ref MEDCouplingArrayFill.
- * \param [in] init - value to assign to the first element of array.
- * \throw If \a this->getNumberOfComponents() != 1
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::iota(int init)
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::iota : works only for arrays with only one component, you can call 'rearrange' method before !");
- int *ptr=getPointer();
- int ntuples=getNumberOfTuples();
- for(int i=0;i<ntuples;i++)
- ptr[i]=init+i;
- declareAsNew();
-}
-
-/*!
- * Returns a textual and human readable representation of \a this instance of
- * DataArrayInt. This text is shown when a DataArrayInt is printed in Python.
- * \return std::string - text describing \a this DataArrayInt.
- */
-std::string DataArrayInt::repr() const
-{
- std::ostringstream ret;
- reprStream(ret);
- return ret.str();
-}
-
-std::string DataArrayInt::reprZip() const
-{
- std::ostringstream ret;
- reprZipStream(ret);
- return ret.str();
-}
-
-void DataArrayInt::writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const
-{
- static const char SPACE[4]={' ',' ',' ',' '};
- checkAllocated();
- std::string idt(indent,' ');
- ofs << idt << "<DataArray type=\"" << type << "\" Name=\"" << nameInFile << "\" NumberOfComponents=\"" << getNumberOfComponents() << "\"";
- if(byteArr)
- {
- ofs << " format=\"appended\" offset=\"" << byteArr->getNumberOfTuples() << "\">";
- if(std::string(type)=="Int32")
- {
- const char *data(reinterpret_cast<const char *>(begin()));
- std::size_t sz(getNbOfElems()*sizeof(int));
- byteArr->insertAtTheEnd(data,data+sz);
- byteArr->insertAtTheEnd(SPACE,SPACE+4);
- }
- else if(std::string(type)=="Int8")
- {
- INTERP_KERNEL::AutoPtr<char> tmp(new char[getNbOfElems()]);
- std::copy(begin(),end(),(char *)tmp);
- byteArr->insertAtTheEnd((char *)tmp,(char *)tmp+getNbOfElems());
- byteArr->insertAtTheEnd(SPACE,SPACE+4);
- }
- else if(std::string(type)=="UInt8")
- {
- INTERP_KERNEL::AutoPtr<unsigned char> tmp(new unsigned char[getNbOfElems()]);
- std::copy(begin(),end(),(unsigned char *)tmp);
- byteArr->insertAtTheEnd((unsigned char *)tmp,(unsigned char *)tmp+getNbOfElems());
- byteArr->insertAtTheEnd(SPACE,SPACE+4);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::writeVTK : Only Int32, Int8 and UInt8 supported !");
- }
- else
- {
- ofs << " RangeMin=\"" << getMinValueInArray() << "\" RangeMax=\"" << getMaxValueInArray() << "\" format=\"ascii\">\n" << idt;
- std::copy(begin(),end(),std::ostream_iterator<int>(ofs," "));
- }
- ofs << std::endl << idt << "</DataArray>\n";
-}
-
-void DataArrayInt::reprStream(std::ostream& stream) const
-{
- stream << "Name of int array : \"" << _name << "\"\n";
- reprWithoutNameStream(stream);
-}
-
-void DataArrayInt::reprZipStream(std::ostream& stream) const
-{
- stream << "Name of int array : \"" << _name << "\"\n";
- reprZipWithoutNameStream(stream);
-}
-
-void DataArrayInt::reprWithoutNameStream(std::ostream& stream) const
-{
- DataArray::reprWithoutNameStream(stream);
- _mem.repr(getNumberOfComponents(),stream);
-}
-
-void DataArrayInt::reprZipWithoutNameStream(std::ostream& stream) const
-{
- DataArray::reprWithoutNameStream(stream);
- _mem.reprZip(getNumberOfComponents(),stream);
-}
-
-void DataArrayInt::reprCppStream(const std::string& varName, std::ostream& stream) const
-{
- int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents();
- const int *data=getConstPointer();
- stream << "DataArrayInt *" << varName << "=DataArrayInt::New();" << std::endl;
- if(nbTuples*nbComp>=1)
- {
- stream << "const int " << varName << "Data[" << nbTuples*nbComp << "]={";
- std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<int>(stream,","));
- stream << data[nbTuples*nbComp-1] << "};" << std::endl;
- stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
- }
- else
- stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
- stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
-}
-
-/*!
- * Method that gives a quick overvien of \a this for python.
- */
-void DataArrayInt::reprQuickOverview(std::ostream& stream) const
-{
- static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
- stream << "DataArrayInt C++ instance at " << this << ". ";
- if(isAllocated())
- {
- int nbOfCompo=(int)_info_on_compo.size();
- if(nbOfCompo>=1)
- {
- int nbOfTuples=getNumberOfTuples();
- stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
- reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
- }
- else
- stream << "Number of components : 0.";
- }
- else
- stream << "*** No data allocated ****";
-}
-
-void DataArrayInt::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
-{
- const int *data=begin();
- int nbOfTuples=getNumberOfTuples();
- int nbOfCompo=(int)_info_on_compo.size();
- std::ostringstream oss2; oss2 << "[";
- std::string oss2Str(oss2.str());
- bool isFinished=true;
- for(int i=0;i<nbOfTuples && isFinished;i++)
- {
- if(nbOfCompo>1)
- {
- oss2 << "(";
- for(int j=0;j<nbOfCompo;j++,data++)
- {
- oss2 << *data;
- if(j!=nbOfCompo-1) oss2 << ", ";
- }
- oss2 << ")";
- }
- else
- oss2 << *data++;
- if(i!=nbOfTuples-1) oss2 << ", ";
- std::string oss3Str(oss2.str());
- if(oss3Str.length()<maxNbOfByteInRepr)
- oss2Str=oss3Str;
- else
- isFinished=false;
- }
- stream << oss2Str;
- if(!isFinished)
- stream << "... ";
- stream << "]";
-}
-
-/*!
- * Modifies \a this one-dimensional array so that each value \a v = \a indArrBg[ \a v ],
- * i.e. a current value is used as in index to get a new value from \a indArrBg.
- * \param [in] indArrBg - pointer to the first element of array of new values to assign
- * to \a this array.
- * \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
- * the last value of \a indArrBg is \a indArrEnd[ -1 ].
- * \throw If \a this->getNumberOfComponents() != 1
- * \throw If any value of \a this can't be used as a valid index for
- * [\a indArrBg, \a indArrEnd).
- */
-void DataArrayInt::transformWithIndArr(const int *indArrBg, const int *indArrEnd)
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("Call transformWithIndArr method on DataArrayInt with only one component, you can call 'rearrange' method before !");
- int nbElemsIn=(int)std::distance(indArrBg,indArrEnd);
- int nbOfTuples=getNumberOfTuples();
- int *pt=getPointer();
- for(int i=0;i<nbOfTuples;i++,pt++)
- {
- if(*pt>=0 && *pt<nbElemsIn)
- *pt=indArrBg[*pt];
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::transformWithIndArr : error on tuple #" << i << " of this value is " << *pt << ", should be in [0," << nbElemsIn << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- declareAsNew();
-}
-
-/*!
- * Computes distribution of values of \a this one-dimensional array between given value
- * ranges (casts). This method is typically useful for entity number spliting by types,
- * for example.
- * \warning The values contained in \a arrBg should be sorted ascendently. No
- * check of this is be done. If not, the result is not warranted.
- * \param [in] arrBg - the array of ascending values defining the value ranges. The i-th
- * value of \a arrBg (\a arrBg[ i ]) gives the lowest value of the i-th range,
- * and the greatest value of the i-th range equals to \a arrBg[ i+1 ] - 1. \a
- * arrBg containing \a n values defines \a n-1 ranges. The last value of \a arrBg
- * should be more than every value in \a this array.
- * \param [in] arrEnd - specifies the end of the array \a arrBg, so that
- * the last value of \a arrBg is \a arrEnd[ -1 ].
- * \param [out] castArr - a new instance of DataArrayInt, of same size as \a this array
- * (same number of tuples and components), the caller is to delete
- * using decrRef() as it is no more needed.
- * This array contains indices of ranges for every value of \a this array. I.e.
- * the i-th value of \a castArr gives the index of range the i-th value of \a this
- * belongs to. Or, in other words, this parameter contains for each tuple in \a
- * this in which cast it holds.
- * \param [out] rankInsideCast - a new instance of DataArrayInt, of same size as \a this
- * array, the caller is to delete using decrRef() as it is no more needed.
- * This array contains ranks of values of \a this array within ranges
- * they belongs to. I.e. the i-th value of \a rankInsideCast gives the rank of
- * the i-th value of \a this array within the \a castArr[ i ]-th range, to which
- * the i-th value of \a this belongs to. Or, in other words, this param contains
- * for each tuple its rank inside its cast. The rank is computed as difference
- * between the value and the lowest value of range.
- * \param [out] castsPresent - a new instance of DataArrayInt, containing indices of
- * ranges (casts) to which at least one value of \a this array belongs.
- * Or, in other words, this param contains the casts that \a this contains.
- * The caller is to delete this array using decrRef() as it is no more needed.
- *
- * \b Example: If \a this contains [6,5,0,3,2,7,8,1,4] and \a arrBg contains [0,4,9] then
- * the output of this method will be :
- * - \a castArr : [1,1,0,0,0,1,1,0,1]
- * - \a rankInsideCast: [2,1,0,3,2,3,4,1,0]
- * - \a castsPresent : [0,1]
- *
- * I.e. values of \a this array belong to 2 ranges: #0 and #1. Value 6 belongs to the
- * range #1 and its rank within this range is 2; etc.
- *
- * \throw If \a this->getNumberOfComponents() != 1.
- * \throw If \a arrEnd - arrBg < 2.
- * \throw If any value of \a this is not less than \a arrEnd[-1].
- */
-void DataArrayInt::splitByValueRange(const int *arrBg, const int *arrEnd,
- DataArrayInt *& castArr, DataArrayInt *& rankInsideCast, DataArrayInt *& castsPresent) const
- {
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("Call splitByValueRange method on DataArrayInt with only one component, you can call 'rearrange' method before !");
- int nbOfTuples=getNumberOfTuples();
- std::size_t nbOfCast=std::distance(arrBg,arrEnd);
- if(nbOfCast<2)
- throw INTERP_KERNEL::Exception("DataArrayInt::splitByValueRange : The input array giving the cast range values should be of size >=2 !");
- nbOfCast--;
- const int *work=getConstPointer();
- typedef std::reverse_iterator<const int *> rintstart;
- rintstart bg(arrEnd);//OK no problem because size of 'arr' is greater or equal 2
- rintstart end2(arrBg);
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret1=DataArrayInt::New();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret2=DataArrayInt::New();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret3=DataArrayInt::New();
- ret1->alloc(nbOfTuples,1);
- ret2->alloc(nbOfTuples,1);
- int *ret1Ptr=ret1->getPointer();
- int *ret2Ptr=ret2->getPointer();
- std::set<std::size_t> castsDetected;
- for(int i=0;i<nbOfTuples;i++)
- {
- rintstart res=std::find_if(bg,end2,std::bind2nd(std::less_equal<int>(), work[i]));
- std::size_t pos=std::distance(bg,res);
- std::size_t pos2=nbOfCast-pos;
- if(pos2<nbOfCast)
- {
- ret1Ptr[i]=(int)pos2;
- ret2Ptr[i]=work[i]-arrBg[pos2];
- castsDetected.insert(pos2);
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::splitByValueRange : At rank #" << i << " the value is " << work[i] << " should be in [0," << *bg << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- ret3->alloc((int)castsDetected.size(),1);
- std::copy(castsDetected.begin(),castsDetected.end(),ret3->getPointer());
- castArr=ret1.retn();
- rankInsideCast=ret2.retn();
- castsPresent=ret3.retn();
- }
-
-/*!
- * Creates a one-dimensional DataArrayInt (\a res) whose contents are computed from
- * values of \a this (\a a) and the given (\a indArr) arrays as follows:
- * \a res[ \a indArr[ \a a[ i ]]] = i. I.e. for each value in place i \a v = \a a[ i ],
- * new value in place \a indArr[ \a v ] is i.
- * \param [in] indArrBg - the array holding indices within the result array to assign
- * indices of values of \a this array pointing to values of \a indArrBg.
- * \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
- * the last value of \a indArrBg is \a indArrEnd[ -1 ].
- * \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 \a this->getNumberOfComponents() != 1.
- * \throw If any value of \a this array is not a valid index for \a indArrBg array.
- * \throw If any value of \a indArrBg is not a valid index for \a this array.
- */
-DataArrayInt *DataArrayInt::transformWithIndArrR(const int *indArrBg, const int *indArrEnd) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("Call transformWithIndArrR method on DataArrayInt with only one component, you can call 'rearrange' method before !");
- int nbElemsIn=(int)std::distance(indArrBg,indArrEnd);
- int nbOfTuples=getNumberOfTuples();
- const int *pt=getConstPointer();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbOfTuples,1);
- ret->fillWithValue(-1);
- int *tmp=ret->getPointer();
- for(int i=0;i<nbOfTuples;i++,pt++)
- {
- if(*pt>=0 && *pt<nbElemsIn)
- {
- int pos=indArrBg[*pt];
- if(pos>=0 && pos<nbOfTuples)
- tmp[pos]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value of new pos is " << pos << " ( indArrBg[" << *pt << "]) ! Should be in [0," << nbOfTuples << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value is " << *pt << " and indirectionnal array as a size equal to " << nbElemsIn << " !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return ret.retn();
-}
-
-/*!
- * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
- * from values of \a this array, which is supposed to contain a renumbering map in
- * "Old to New" mode. The result array contains a renumbering map in "New to Old" mode.
- * To know how to use the renumbering maps see \ref MEDCouplingArrayRenumbering.
- * \param [in] newNbOfElem - the number of tuples in the result array.
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- *
- * \if ENABLE_EXAMPLES
- * \ref cpp_mcdataarrayint_invertarrayo2n2n2o "Here is a C++ example".<br>
- * \ref py_mcdataarrayint_invertarrayo2n2n2o "Here is a Python example".
- * \endif
- */
-DataArrayInt *DataArrayInt::invertArrayO2N2N2O(int newNbOfElem) const
-{
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(newNbOfElem,1);
- int nbOfOldNodes=getNumberOfTuples();
- const int *old2New=getConstPointer();
- int *pt=ret->getPointer();
- for(int i=0;i!=nbOfOldNodes;i++)
- {
- int newp(old2New[i]);
- if(newp!=-1)
- {
- if(newp>=0 && newp<newNbOfElem)
- pt[newp]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2O : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- }
- return ret.retn();
-}
-
-/*!
- * This method is similar to DataArrayInt::invertArrayO2N2N2O except that
- * 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]
- */
-DataArrayInt *DataArrayInt::invertArrayO2N2N2OBis(int newNbOfElem) const
-{
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(newNbOfElem,1);
- int nbOfOldNodes=getNumberOfTuples();
- const int *old2New=getConstPointer();
- int *pt=ret->getPointer();
- for(int i=nbOfOldNodes-1;i>=0;i--)
- {
- int newp(old2New[i]);
- if(newp!=-1)
- {
- if(newp>=0 && newp<newNbOfElem)
- pt[newp]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2OBis : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- }
- return ret.retn();
-}
-
-/*!
- * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
- * from values of \a this array, which is supposed to contain a renumbering map in
- * "New to Old" mode. The result array contains a renumbering map in "Old to New" mode.
- * To know how to use the renumbering maps see \ref MEDCouplingArrayRenumbering.
- * \param [in] newNbOfElem - the number of tuples in the result array.
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- *
- * \if ENABLE_EXAMPLES
- * \ref cpp_mcdataarrayint_invertarrayn2o2o2n "Here is a C++ example".
- *
- * \ref py_mcdataarrayint_invertarrayn2o2o2n "Here is a Python example".
- * \endif
- */
-DataArrayInt *DataArrayInt::invertArrayN2O2O2N(int oldNbOfElem) const
-{
- checkAllocated();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(oldNbOfElem,1);
- const int *new2Old=getConstPointer();
- int *pt=ret->getPointer();
- std::fill(pt,pt+oldNbOfElem,-1);
- int nbOfNewElems=getNumberOfTuples();
- for(int i=0;i<nbOfNewElems;i++)
- {
- int v(new2Old[i]);
- if(v>=0 && v<oldNbOfElem)
- pt[v]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::invertArrayN2O2O2N : in new id #" << i << " old value is " << v << " expected to be in [0," << oldNbOfElem << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return ret.retn();
-}
-
-/*!
- * Equivalent to DataArrayInt::isEqual except that if false the reason of
- * mismatch is given.
- *
- * \param [in] other the instance to be compared with \a this
- * \param [out] reason In case of inequality returns the reason.
- * \sa DataArrayInt::isEqual
- */
-bool DataArrayInt::isEqualIfNotWhy(const DataArrayInt& other, std::string& reason) const
-{
- if(!areInfoEqualsIfNotWhy(other,reason))
- return false;
- return _mem.isEqual(other._mem,0,reason);
-}
-
-/*!
- * Checks if \a this and another DataArrayInt are fully equal. For more info see
- * \ref MEDCouplingArrayBasicsCompare.
- * \param [in] other - an instance of DataArrayInt to compare with \a this one.
- * \return bool - \a true if the two arrays are equal, \a false else.
- */
-bool DataArrayInt::isEqual(const DataArrayInt& other) const
-{
- std::string tmp;
- return isEqualIfNotWhy(other,tmp);
-}
-
-/*!
- * Checks if values of \a this and another DataArrayInt are equal. For more info see
- * \ref MEDCouplingArrayBasicsCompare.
- * \param [in] other - an instance of DataArrayInt to compare with \a this one.
- * \return bool - \a true if the values of two arrays are equal, \a false else.
- */
-bool DataArrayInt::isEqualWithoutConsideringStr(const DataArrayInt& other) const
-{
- std::string tmp;
- return _mem.isEqual(other._mem,0,tmp);
-}
-
-/*!
- * Checks if values of \a this and another DataArrayInt are equal. Comparison is
- * performed on sorted value sequences.
- * For more info see\ref MEDCouplingArrayBasicsCompare.
- * \param [in] other - an instance of DataArrayInt to compare with \a this one.
- * \return bool - \a true if the sorted values of two arrays are equal, \a false else.
- */
-bool DataArrayInt::isEqualWithoutConsideringStrAndOrder(const DataArrayInt& other) const
-{
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> a=deepCpy();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> b=other.deepCpy();
- a->sort();
- b->sort();
- return a->isEqualWithoutConsideringStr(*b);
-}
-
-/*!
- * This method compares content of input vector \a v and \a this.
- * 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.
- * For performance reasons \a this is expected to be sorted ascendingly. If not an exception will be thrown.
- *
- * \param [in] v - the vector of 'flags' to be compared with \a this.
- *
- * \throw If \a this is not sorted ascendingly.
- * \throw If \a this has not exactly one component.
- * \throw If \a this is not allocated.
- */
-bool DataArrayInt::isFittingWith(const std::vector<bool>& v) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::isFittingWith : number of components of this should be equal to one !");
- const int *w(begin()),*end2(end());
- int refVal=-std::numeric_limits<int>::max();
- int i=0;
- std::vector<bool>::const_iterator it(v.begin());
- for(;it!=v.end();it++,i++)
- {
- if(*it)
- {
- if(w!=end2)
- {
- if(*w++==i)
- {
- if(i>refVal)
- refVal=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::isFittingWith : At pos #" << std::distance(begin(),w-1) << " this is not sorted ascendingly !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- else
- return false;
- }
- else
- return false;
- }
- }
- return w==end2;
-}
-
-/*!
- * Sorts values of the array.
- * \param [in] asc - \a true means ascending order, \a false, descending.
- * \throw If \a this is not allocated.
- * \throw If \a this->getNumberOfComponents() != 1.
- */
-void DataArrayInt::sort(bool asc)
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::sort : only supported with 'this' array with ONE component !");
- _mem.sort(asc);
- declareAsNew();
-}
-
-/*!
- * Computes for each tuple the sum of number of components values in the tuple and return it.
- *
- * \return DataArrayInt * - the new instance of DataArrayInt containing the
- * same number of tuples as \a this array and one component.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this is not allocated.
- */
-DataArrayInt *DataArrayInt::sumPerTuple() const
-{
- checkAllocated();
- int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
- ret->alloc(nbOfTuple,1);
- const int *src(getConstPointer());
- int *dest(ret->getPointer());
- for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
- *dest=std::accumulate(src,src+nbOfComp,0);
- return ret.retn();
-}
-
-/*!
- * Reverse the array values.
- * \throw If \a this->getNumberOfComponents() < 1.
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::reverse()
-{
- checkAllocated();
- _mem.reverse(getNumberOfComponents());
- declareAsNew();
-}
-
-/*!
- * Checks that \a this array is consistently **increasing** or **decreasing** in value.
- * If not an exception is thrown.
- * \param [in] increasing - if \a true, the array values should be increasing.
- * \throw If sequence of values is not strictly monotonic in agreement with \a
- * increasing arg.
- * \throw If \a this->getNumberOfComponents() != 1.
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::checkMonotonic(bool increasing) const
-{
- if(!isMonotonic(increasing))
- {
- if (increasing)
- throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not INCREASING monotonic !");
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::checkMonotonic : 'this' is not DECREASING monotonic !");
- }
-}
-
-/*!
- * Checks that \a this array is consistently **increasing** or **decreasing** in value.
- * \param [in] increasing - if \a true, array values should be increasing.
- * \return bool - \a true if values change in accordance with \a increasing arg.
- * \throw If \a this->getNumberOfComponents() != 1.
- * \throw If \a this is not allocated.
- */
-bool DataArrayInt::isMonotonic(bool increasing) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::isMonotonic : only supported with 'this' array with ONE component !");
- int nbOfElements=getNumberOfTuples();
- const int *ptr=getConstPointer();
- if(nbOfElements==0)
- return true;
- int ref=ptr[0];
- if(increasing)
- {
- for(int i=1;i<nbOfElements;i++)
- {
- if(ptr[i]>=ref)
- ref=ptr[i];
- else
- return false;
- }
- }
- else
- {
- for(int i=1;i<nbOfElements;i++)
- {
- if(ptr[i]<=ref)
- ref=ptr[i];
- else
- return false;
- }
- }
- return true;
-}
-
-/*!
- * This method check that array consistently INCREASING or DECREASING in value.
- */
-bool DataArrayInt::isStrictlyMonotonic(bool increasing) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::isStrictlyMonotonic : only supported with 'this' array with ONE component !");
- int nbOfElements=getNumberOfTuples();
- const int *ptr=getConstPointer();
- if(nbOfElements==0)
- return true;
- int ref=ptr[0];
- if(increasing)
- {
- for(int i=1;i<nbOfElements;i++)
- {
- if(ptr[i]>ref)
- ref=ptr[i];
- else
- return false;
- }
- }
- else
- {
- for(int i=1;i<nbOfElements;i++)
- {
- if(ptr[i]<ref)
- ref=ptr[i];
- else
- return false;
- }
- }
- return true;
-}
-
-/*!
- * This method check that array consistently INCREASING or DECREASING in value.
- */
-void DataArrayInt::checkStrictlyMonotonic(bool increasing) const
-{
- if(!isStrictlyMonotonic(increasing))
- {
- if (increasing)
- throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly INCREASING monotonic !");
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::checkStrictlyMonotonic : 'this' is not strictly DECREASING monotonic !");
- }
-}
-
-/*!
- * Creates a new one-dimensional DataArrayInt of the same size as \a this and a given
- * one-dimensional arrays that must be of the same length. The result array describes
- * correspondence between \a this and \a other arrays, so that
- * <em> other.getIJ(i,0) == this->getIJ(ret->getIJ(i),0)</em>. If such a permutation is
- * not possible because some element in \a other is not in \a this, an exception is thrown.
- * \param [in] other - an array to compute permutation to.
- * \return DataArrayInt * - a new instance of DataArrayInt, which is a permutation array
- * from \a this to \a other. The caller is to delete this array using decrRef() as it is
- * no more needed.
- * \throw If \a this->getNumberOfComponents() != 1.
- * \throw If \a other->getNumberOfComponents() != 1.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples().
- * \throw If \a other includes a value which is not in \a this array.
- *
- * \if ENABLE_EXAMPLES
- * \ref cpp_mcdataarrayint_buildpermutationarr "Here is a C++ example".
- *
- * \ref py_mcdataarrayint_buildpermutationarr "Here is a Python example".
- * \endif
- */
-DataArrayInt *DataArrayInt::buildPermutationArr(const DataArrayInt& other) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1 || other.getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' have to have exactly ONE component !");
- int nbTuple=getNumberOfTuples();
- other.checkAllocated();
- if(nbTuple!=other.getNumberOfTuples())
- throw INTERP_KERNEL::Exception("DataArrayInt::buildPermutationArr : 'this' and 'other' must have the same number of tuple !");
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbTuple,1);
- ret->fillWithValue(-1);
- const int *pt=getConstPointer();
- std::map<int,int> mm;
- for(int i=0;i<nbTuple;i++)
- mm[pt[i]]=i;
- pt=other.getConstPointer();
- int *retToFill=ret->getPointer();
- for(int i=0;i<nbTuple;i++)
- {
- std::map<int,int>::const_iterator it=mm.find(pt[i]);
- if(it==mm.end())
- {
- std::ostringstream oss; oss << "DataArrayInt::buildPermutationArr : Arrays mismatch : element (" << pt[i] << ") in 'other' not findable in 'this' !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- retToFill[i]=(*it).second;
- }
- return ret.retn();
-}
-
-/*!
- * Sets a C array to be used as raw data of \a this. The previously set info
- * of components is retained and re-sized.
- * For more info see \ref MEDCouplingArraySteps1.
- * \param [in] array - the C array to be used as raw data of \a this.
- * \param [in] ownership - if \a true, \a array will be deallocated at destruction of \a this.
- * \param [in] type - specifies how to deallocate \a array. If \a type == ParaMEDMEM::CPP_DEALLOC,
- * \c delete [] \c array; will be called. If \a type == ParaMEDMEM::C_DEALLOC,
- * \c free(\c array ) will be called.
- * \param [in] nbOfTuple - new number of tuples in \a this.
- * \param [in] nbOfCompo - new number of components in \a this.
- */
-void DataArrayInt::useArray(const int *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo)
-{
- _info_on_compo.resize(nbOfCompo);
- _mem.useArray(array,ownership,type,nbOfTuple*nbOfCompo);
- declareAsNew();
-}
-
-void DataArrayInt::useExternalArrayWithRWAccess(const int *array, int nbOfTuple, int nbOfCompo)
-{
- _info_on_compo.resize(nbOfCompo);
- _mem.useExternalArrayWithRWAccess(array,nbOfTuple*nbOfCompo);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayInt holding the same values as \a this array but differently
- * arranged in memory. If \a this array holds 2 components of 3 values:
- * \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$, then the result array holds these values arranged
- * as follows: \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$.
- * \warning Do not confuse this method with transpose()!
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- * \throw If \a this is not allocated.
- */
-DataArrayInt *DataArrayInt::fromNoInterlace() const
-{
- checkAllocated();
- if(_mem.isNull())
- throw INTERP_KERNEL::Exception("DataArrayInt::fromNoInterlace : Not defined array !");
- int *tab=_mem.fromNoInterlace(getNumberOfComponents());
- DataArrayInt *ret=DataArrayInt::New();
- ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
- return ret;
-}
-
-/*!
- * Returns a new DataArrayInt holding the same values as \a this array but differently
- * arranged in memory. If \a this array holds 2 components of 3 values:
- * \f$ x_0,y_0,x_1,y_1,x_2,y_2 \f$, then the result array holds these values arranged
- * as follows: \f$ x_0,x_1,x_2,y_0,y_1,y_2 \f$.
- * \warning Do not confuse this method with transpose()!
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- * \throw If \a this is not allocated.
- */
-DataArrayInt *DataArrayInt::toNoInterlace() const
-{
- checkAllocated();
- if(_mem.isNull())
- throw INTERP_KERNEL::Exception("DataArrayInt::toNoInterlace : Not defined array !");
- int *tab=_mem.toNoInterlace(getNumberOfComponents());
- DataArrayInt *ret=DataArrayInt::New();
- ret->useArray(tab,true,C_DEALLOC,getNumberOfTuples(),getNumberOfComponents());
- return ret;
-}
-
-/*!
- * Permutes values of \a this array as required by \a old2New array. The values are
- * permuted so that \c new[ \a old2New[ i ]] = \c old[ i ]. Number of tuples remains
- * the same as in \this one.
- * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
- * giving a new position for i-th old value.
- */
-void DataArrayInt::renumberInPlace(const int *old2New)
-{
- checkAllocated();
- int nbTuples=getNumberOfTuples();
- int nbOfCompo=getNumberOfComponents();
- int *tmp=new int[nbTuples*nbOfCompo];
- const int *iptr=getConstPointer();
- for(int i=0;i<nbTuples;i++)
- {
- int v=old2New[i];
- if(v>=0 && v<nbTuples)
- std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),tmp+nbOfCompo*v);
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::renumberInPlace : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
- delete [] tmp;
- declareAsNew();
-}
-
-/*!
- * Permutes values of \a this array as required by \a new2Old array. The values are
- * permuted so that \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of tuples remains
- * the same as in \this one.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
- * giving a previous position of i-th new value.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- */
-void DataArrayInt::renumberInPlaceR(const int *new2Old)
-{
- checkAllocated();
- int nbTuples=getNumberOfTuples();
- int nbOfCompo=getNumberOfComponents();
- int *tmp=new int[nbTuples*nbOfCompo];
- const int *iptr=getConstPointer();
- for(int i=0;i<nbTuples;i++)
- {
- int v=new2Old[i];
- if(v>=0 && v<nbTuples)
- std::copy(iptr+nbOfCompo*v,iptr+nbOfCompo*(v+1),tmp+nbOfCompo*i);
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::renumberInPlaceR : At place #" << i << " value is " << v << " ! Should be in [0," << nbTuples << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- std::copy(tmp,tmp+nbTuples*nbOfCompo,getPointer());
- delete [] tmp;
- declareAsNew();
-}
-
-/*!
- * Returns a copy of \a this array with values permuted as required by \a old2New array.
- * The values are permuted so that \c new[ \a old2New[ i ]] = \c old[ i ].
- * Number of tuples in the result array remains the same as in \this one.
- * If a permutation reduction is needed, renumberAndReduce() should be used.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
- * giving a new position for i-th old value.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- * \throw If \a this is not allocated.
- */
-DataArrayInt *DataArrayInt::renumber(const int *old2New) const
-{
- checkAllocated();
- int nbTuples=getNumberOfTuples();
- int nbOfCompo=getNumberOfComponents();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbTuples,nbOfCompo);
- ret->copyStringInfoFrom(*this);
- const int *iptr=getConstPointer();
- int *optr=ret->getPointer();
- for(int i=0;i<nbTuples;i++)
- std::copy(iptr+nbOfCompo*i,iptr+nbOfCompo*(i+1),optr+nbOfCompo*old2New[i]);
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Returns a copy of \a this array with values permuted as required by \a new2Old array.
- * The values are permuted so that \c new[ i ] = \c old[ \a new2Old[ i ]]. Number of
- * tuples in the result array remains the same as in \this one.
- * If a permutation reduction is needed, substr() or selectByTupleId() should be used.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] new2Old - C array of length equal to \a this->getNumberOfTuples()
- * giving a previous position of i-th new value.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- */
-DataArrayInt *DataArrayInt::renumberR(const int *new2Old) const
-{
- checkAllocated();
- int nbTuples=getNumberOfTuples();
- int nbOfCompo=getNumberOfComponents();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbTuples,nbOfCompo);
- ret->copyStringInfoFrom(*this);
- const int *iptr=getConstPointer();
- int *optr=ret->getPointer();
- for(int i=0;i<nbTuples;i++)
- std::copy(iptr+nbOfCompo*new2Old[i],iptr+nbOfCompo*(new2Old[i]+1),optr+nbOfCompo*i);
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
- * of size \a newNbOfTuple and it's values are permuted as required by \a old2New array.
- * The values are permuted so that \c new[ \a old2New[ i ]] = \c old[ i ] for all
- * \a old2New[ i ] >= 0. In other words every i-th tuple in \a this array, for which
- * \a old2New[ i ] is negative, is missing from the result array.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] old2New - C array of length equal to \a this->getNumberOfTuples()
- * giving a new position for i-th old tuple and giving negative position for
- * for i-th old tuple that should be omitted.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- */
-DataArrayInt *DataArrayInt::renumberAndReduce(const int *old2New, int newNbOfTuple) const
-{
- checkAllocated();
- int nbTuples=getNumberOfTuples();
- int nbOfCompo=getNumberOfComponents();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(newNbOfTuple,nbOfCompo);
- const int *iptr=getConstPointer();
- int *optr=ret->getPointer();
- for(int i=0;i<nbTuples;i++)
- {
- int w=old2New[i];
- if(w>=0)
- std::copy(iptr+i*nbOfCompo,iptr+(i+1)*nbOfCompo,optr+w*nbOfCompo);
- }
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
- * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
- * \a new2OldBg array.
- * The values are permuted so that \c new[ i ] = \c old[ \a new2OldBg[ i ]].
- * This method is equivalent to renumberAndReduce() except that convention in input is
- * \c new2old and \b not \c old2new.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
- * tuple index in \a this array to fill the i-th tuple in the new array.
- * \param [in] new2OldEnd - specifies the end of the permutation array that starts at
- * \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
- * \a new2OldBg <= \a pi < \a new2OldEnd.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- */
-DataArrayInt *DataArrayInt::selectByTupleId(const int *new2OldBg, const int *new2OldEnd) const
-{
- checkAllocated();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- int nbComp=getNumberOfComponents();
- ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
- ret->copyStringInfoFrom(*this);
- int *pt=ret->getPointer();
- const int *srcPt=getConstPointer();
- int i=0;
- for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
- std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Returns a shorten and permuted copy of \a this array. The new DataArrayInt is
- * of size \a new2OldEnd - \a new2OldBg and it's values are permuted as required by
- * \a new2OldBg array.
- * The values are permuted so that \c new[ i ] = \c old[ \a new2OldBg[ i ]].
- * This method is equivalent to renumberAndReduce() except that convention in input is
- * \c new2old and \b not \c old2new.
- * This method is equivalent to selectByTupleId() except that it prevents coping data
- * from behind the end of \a this array.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] new2OldBg - pointer to the beginning of a permutation array that gives a
- * tuple index in \a this array to fill the i-th tuple in the new array.
- * \param [in] new2OldEnd - specifies the end of the permutation array that starts at
- * \a new2OldBg, so that pointer to a tuple index (\a pi) varies as this:
- * \a new2OldBg <= \a pi < \a new2OldEnd.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- * \throw If \a new2OldEnd - \a new2OldBg > \a this->getNumberOfTuples().
- */
-DataArrayInt *DataArrayInt::selectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const
-{
- checkAllocated();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- int nbComp=getNumberOfComponents();
- int oldNbOfTuples=getNumberOfTuples();
- ret->alloc((int)std::distance(new2OldBg,new2OldEnd),nbComp);
- ret->copyStringInfoFrom(*this);
- int *pt=ret->getPointer();
- const int *srcPt=getConstPointer();
- int i=0;
- for(const int *w=new2OldBg;w!=new2OldEnd;w++,i++)
- if(*w>=0 && *w<oldNbOfTuples)
- std::copy(srcPt+(*w)*nbComp,srcPt+((*w)+1)*nbComp,pt+i*nbComp);
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::selectByTupleIdSafe : some ids has been detected to be out of [0,this->getNumberOfTuples) !");
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Returns a shorten copy of \a this array. The new DataArrayInt contains every
- * (\a bg + \c i * \a step)-th tuple of \a this array located before the \a end2-th
- * tuple. Indices of the selected tuples are the same as ones returned by the Python
- * command \c range( \a bg, \a end2, \a step ).
- * This method is equivalent to selectByTupleIdSafe() except that the input array is
- * not constructed explicitly.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] bg - index of the first tuple to copy from \a this array.
- * \param [in] end2 - index of the tuple before which the tuples to copy are located.
- * \param [in] step - index increment to get index of the next tuple to copy.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- * \sa DataArrayInt::substr.
- */
-DataArrayInt *DataArrayInt::selectByTupleId2(int bg, int end2, int step) const
-{
- checkAllocated();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- int nbComp=getNumberOfComponents();
- int newNbOfTuples=GetNumberOfItemGivenBESRelative(bg,end2,step,"DataArrayInt::selectByTupleId2 : ");
- ret->alloc(newNbOfTuples,nbComp);
- int *pt=ret->getPointer();
- const int *srcPt=getConstPointer()+bg*nbComp;
- for(int i=0;i<newNbOfTuples;i++,srcPt+=step*nbComp)
- std::copy(srcPt,srcPt+nbComp,pt+i*nbComp);
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Returns a shorten copy of \a this array. The new DataArrayInt contains ranges
- * of tuples specified by \a ranges parameter.
- * For more info on renumbering see \ref MEDCouplingArrayRenumbering.
- * \param [in] ranges - std::vector of std::pair's each of which defines a range
- * of tuples in [\c begin,\c end) format.
- * \return DataArrayInt * - the new instance of DataArrayInt that the caller
- * is to delete using decrRef() as it is no more needed.
- * \throw If \a end < \a begin.
- * \throw If \a end > \a this->getNumberOfTuples().
- * \throw If \a this is not allocated.
- */
-DataArray *DataArrayInt::selectByTupleRanges(const std::vector<std::pair<int,int> >& ranges) const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- int nbOfTuplesThis=getNumberOfTuples();
- if(ranges.empty())
- {
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(0,nbOfComp);
- ret->copyStringInfoFrom(*this);
- return ret.retn();
- }
- int ref=ranges.front().first;
- int nbOfTuples=0;
- bool isIncreasing=true;
- for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
- {
- if((*it).first<=(*it).second)
- {
- if((*it).first>=0 && (*it).second<=nbOfTuplesThis)
- {
- nbOfTuples+=(*it).second-(*it).first;
- if(isIncreasing)
- isIncreasing=ref<=(*it).first;
- ref=(*it).second;
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
- oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it);
- oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- if(isIncreasing && nbOfTuplesThis==nbOfTuples)
- return deepCpy();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbOfTuples,nbOfComp);
- ret->copyStringInfoFrom(*this);
- const int *src=getConstPointer();
- int *work=ret->getPointer();
- for(std::vector<std::pair<int,int> >::const_iterator it=ranges.begin();it!=ranges.end();it++)
- work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work);
- return ret.retn();
-}
-
-/*!
- * Returns a new DataArrayInt containing a renumbering map in "Old to New" mode.
- * This map, if applied to \a this array, would make it sorted. For example, if
- * \a this array contents are [9,10,0,6,4,11,3,7] then the contents of the result array
- * are [5,6,0,3,2,7,1,4]; if this result array (\a res) is used as an argument in call
- * \a this->renumber(\a res) then the returned array contains [0,3,4,6,7,9,10,11].
- * This method is useful for renumbering (in MED file for example). For more info
- * on renumbering see \ref MEDCouplingArrayRenumbering.
- * \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
- * array using decrRef() as it is no more needed.
- * \throw If \a this is not allocated.
- * \throw If \a this->getNumberOfComponents() != 1.
- * \throw If there are equal values in \a this array.
- */
-DataArrayInt *DataArrayInt::checkAndPreparePermutation() const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::checkAndPreparePermutation : number of components must == 1 !");
- int nbTuples=getNumberOfTuples();
- const int *pt=getConstPointer();
- int *pt2=CheckAndPreparePermutation(pt,pt+nbTuples);
- DataArrayInt *ret=DataArrayInt::New();
- ret->useArray(pt2,true,C_DEALLOC,nbTuples,1);
- return ret;