From 58a90278ff499ced062f437585b3b6b645c404ab Mon Sep 17 00:00:00 2001 From: emv Date: Fri, 8 Nov 2019 14:19:03 +0300 Subject: [PATCH] LOT3 Update: Make the static methods GetSlice, GetNumberOfItemGivenBES, GetNumberOfItemGivenBESRelative, GetPosOfItemGivenBESRelativeNoThrow of DataArray class template. --- src/MEDCoupling/MEDCouplingMemArray.cxx | 87 ++------------------ src/MEDCoupling/MEDCouplingMemArray.hxx | 17 +++- src/MEDCoupling/MEDCouplingMemArray.txx | 105 +++++++++++++++++++++++- 3 files changed, 123 insertions(+), 86 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index db6cea670..04ba0b18c 100755 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -704,95 +704,24 @@ void DataArray::CheckClosingParInRange(int ref, int value, const std::string& ms * \throw If \a nbOfSlices not > 0 * \throw If \a sliceId not in [0,nbOfSlices) */ -void DataArray::GetSlice(int start, int stop, int step, int sliceId, int nbOfSlices, int& startSlice, int& stopSlice) +void DataArray::GetSlice(mcIdType start, mcIdType stop, mcIdType step, mcIdType sliceId, mcIdType nbOfSlices, mcIdType& startSlice, mcIdType& stopSlice) { - if(nbOfSlices<=0) - { - std::ostringstream oss; oss << "DataArray::GetSlice : nbOfSlices (" << nbOfSlices << ") must be > 0 !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - if(sliceId<0 || sliceId>=nbOfSlices) - { - std::ostringstream oss; oss << "DataArray::GetSlice : sliceId (" << nbOfSlices << ") must be in [0 , nbOfSlices (" << nbOfSlices << ") ) !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - int nbElems=GetNumberOfItemGivenBESRelative(start,stop,step,"DataArray::GetSlice"); - int minNbOfElemsPerSlice=nbElems/nbOfSlices; - startSlice=start+minNbOfElemsPerSlice*step*sliceId; - if(sliceId::GetSlice(start, stop, step, sliceId, nbOfSlices, startSlice, stopSlice); } -int DataArray::GetNumberOfItemGivenBES(int begin, int end, int step, const std::string& msg) +mcIdType DataArray::GetNumberOfItemGivenBES(mcIdType begin, mcIdType end, mcIdType step, const std::string& msg) { - if(end 0 !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - return (end-1-begin)/step+1; + return DataArrayTools::GetNumberOfItemGivenBES(begin, end, step, msg); } -int DataArray::GetNumberOfItemGivenBESRelative(int begin, int end, int step, const std::string& msg) +mcIdType DataArray::GetNumberOfItemGivenBESRelative(mcIdType begin, mcIdType end, mcIdType step, const std::string& msg) { - if(step==0) - throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES : step=0 is not allowed !"); - if(end0) - { - std::ostringstream oss; oss << msg << " : end before begin whereas step is positive !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - if(begin 0 !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - if(begin!=end) - return (std::max(begin,end)-1-std::min(begin,end))/std::abs(step)+1; - else - return 0; + return DataArrayTools::GetNumberOfItemGivenBESRelative(begin, end, step, msg); } -int DataArray::GetPosOfItemGivenBESRelativeNoThrow(int value, int begin, int end, int step) +mcIdType DataArray::GetPosOfItemGivenBESRelativeNoThrow(mcIdType value, mcIdType begin, mcIdType end, mcIdType step) { - if(step!=0) - { - if(step>0) - { - if(begin<=value && value=value && value>end) - { - if((begin-value)%(-step)==0) - return (begin-value)/(-step); - else - return -1; - } - else - return -1; - } - } - else - return -1; + return DataArrayTools::GetPosOfItemGivenBESRelativeNoThrow(value, begin, end, step); } /*! diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 93a5c1f12..b945e15a0 100755 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -125,6 +125,15 @@ namespace MEDCoupling void *_param_for_deallocator; }; + template class DataArrayTools + { + public: + MEDCOUPLING_EXPORT static void GetSlice(T start, T stop, T step, mcIdType sliceId, mcIdType nbOfSlices, T& startSlice, T& stopSlice); + MEDCOUPLING_EXPORT static mcIdType GetNumberOfItemGivenBES(T begin, T end, T step, const std::string& msg); + MEDCOUPLING_EXPORT static mcIdType GetNumberOfItemGivenBESRelative(T begin, T end, T step, const std::string& msg); + MEDCOUPLING_EXPORT static mcIdType GetPosOfItemGivenBESRelativeNoThrow(T value, T begin, T end, T step); + }; + class DataArrayInt32; class DataArrayByte; @@ -185,10 +194,10 @@ namespace MEDCoupling MEDCOUPLING_EXPORT void checkNbOfTuplesAndComp(const DataArray& other, const std::string& msg) const; MEDCOUPLING_EXPORT void checkNbOfTuplesAndComp(mcIdType nbOfTuples, std::size_t nbOfCompo, const std::string& msg) const; MEDCOUPLING_EXPORT void checkNbOfElems(std::size_t nbOfElems, const std::string& msg) const; - MEDCOUPLING_EXPORT static void GetSlice(int start, int stop, int step, int sliceId, int nbOfSlices, int& startSlice, int& stopSlice); - MEDCOUPLING_EXPORT static int GetNumberOfItemGivenBES(int begin, int end, int step, const std::string& msg); - MEDCOUPLING_EXPORT static int GetNumberOfItemGivenBESRelative(int begin, int end, int step, const std::string& msg); - MEDCOUPLING_EXPORT static int GetPosOfItemGivenBESRelativeNoThrow(int value, int begin, int end, int step); + MEDCOUPLING_EXPORT static void GetSlice(mcIdType start, mcIdType stop, mcIdType step, mcIdType sliceId, mcIdType nbOfSlices, mcIdType& startSlice, mcIdType& stopSlice); + MEDCOUPLING_EXPORT static int GetNumberOfItemGivenBES(mcIdType begin, mcIdType end, mcIdType step, const std::string& msg); + MEDCOUPLING_EXPORT static int GetNumberOfItemGivenBESRelative(mcIdType begin, mcIdType end, mcIdType step, const std::string& msg); + MEDCOUPLING_EXPORT static int GetPosOfItemGivenBESRelativeNoThrow(mcIdType value, mcIdType begin, mcIdType end, mcIdType step); MEDCOUPLING_EXPORT static std::string GetVarNameFromInfo(const std::string& info); MEDCOUPLING_EXPORT static std::string GetUnitFromInfo(const std::string& info); MEDCOUPLING_EXPORT static std::string BuildInfoFromVarAndUnit(const std::string& var, const std::string& unit); diff --git a/src/MEDCoupling/MEDCouplingMemArray.txx b/src/MEDCoupling/MEDCouplingMemArray.txx index 4f0001f46..74101b12d 100755 --- a/src/MEDCoupling/MEDCouplingMemArray.txx +++ b/src/MEDCoupling/MEDCouplingMemArray.txx @@ -562,6 +562,106 @@ namespace MEDCoupling ////////////////////////////////// + /*! + * This method is useful to slice work among a pool of threads or processes. \a begin, \a end \a step is the input whole slice of work to perform, + * typically it is a whole slice of tuples of DataArray or cells, nodes of a mesh... + * + * The input \a sliceId should be an id in [0, \a nbOfSlices) that specifies the slice of work. + * + * \param [in] start - the start of the input slice of the whole work to perform split into slices. + * \param [in] stop - the stop of the input slice of the whole work to perform split into slices. + * \param [in] step - the step (that can be <0) of the input slice of the whole work to perform split into slices. + * \param [in] sliceId - the slice id considered + * \param [in] nbOfSlices - the number of slices (typically the number of cores on which the work is expected to be sliced) + * \param [out] startSlice - the start of the slice considered + * \param [out] stopSlice - the stop of the slice consided + * + * \throw If \a step == 0 + * \throw If \a nbOfSlices not > 0 + * \throw If \a sliceId not in [0,nbOfSlices) + */ + template + void DataArrayTools::GetSlice(T start, T stop, T step, mcIdType sliceId, mcIdType nbOfSlices, T& startSlice, T& stopSlice) + { + if(nbOfSlices<=0) + { + std::ostringstream oss; oss << "DataArray::GetSlice : nbOfSlices (" << nbOfSlices << ") must be > 0 !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(sliceId<0 || sliceId>=nbOfSlices) + { + std::ostringstream oss; oss << "DataArray::GetSlice : sliceId (" << nbOfSlices << ") must be in [0 , nbOfSlices (" << nbOfSlices << ") ) !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + mcIdType nbElems=DataArrayTemplate::GetNumberOfItemGivenBESRelative(start,stop,step,"DataArray::GetSlice"); + mcIdType minNbOfElemsPerSlice=nbElems/nbOfSlices; + startSlice=start+minNbOfElemsPerSlice*step*sliceId; + if(sliceId + mcIdType DataArrayTools::GetNumberOfItemGivenBES(T begin, T end, T step, const std::string& msg) + { + if(end 0 !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return ToIdType ((end-1-begin)/step+1); + } + + template + mcIdType DataArrayTools::GetNumberOfItemGivenBESRelative(T begin, T end, T step, const std::string& msg) + { + if(step==0) + throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES : step=0 is not allowed !"); + if(end0) + { + std::ostringstream oss; oss << msg << " : end before begin whereas step is positive !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(begin 0 !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(begin!=end) + return ToIdType((std::max(begin,end)-1-std::min(begin,end))/std::abs(step)+1); + else + return 0; + } + + template + mcIdType DataArrayTools::GetPosOfItemGivenBESRelativeNoThrow(T value, T begin, T end, T step) + { + if (step == 0) + return -1; + + if(step>0 && begin<=value && value=value && value>end) + { + mcIdType id = ToIdType((value-begin)/step); + if (begin + step * id == value) + return id; + else + return -1; + } + else + return -1; + } + + ////////////////////////////////// + template MCAuto< typename Traits::ArrayTypeCh > DataArrayTemplate::NewFromStdVector(const typename std::vector& v) { @@ -5899,8 +5999,7 @@ struct NotInRange if(nbOfTuples==0) throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr : number of tuples must be != 0 !"); const T *ids(this->begin()); - // TODO: the method GetNumberOfItemGivenBESRelative should be moved to DataArrayTemplate and conversion must be removed - mcIdType nbOfEltsInSlc=DataArray::GetNumberOfItemGivenBESRelative(ToIdType(bg),ToIdType(stop),ToIdType(step),"DataArrayInt::buildExplicitArrOfSliceOnScaledArr"); + mcIdType nbOfEltsInSlc=DataArrayTools::GetNumberOfItemGivenBESRelative(bg,stop,step,"DataArrayInt::buildExplicitArrOfSliceOnScaledArr"); T sz(0),pos(bg); for(mcIdType i=0;i typename Traits::ArrayType *DataArrayDiscrete::Range(T begin, T end, T step) { - mcIdType nbOfTuples=DataArray::GetNumberOfItemGivenBESRelative(ToIdType(begin),ToIdType(end),ToIdType(step),"DataArrayInt::Range"); + mcIdType nbOfTuples=DataArrayTools::GetNumberOfItemGivenBESRelative(begin,end,step,"DataArrayInt::Range"); MCAuto ret=DataArrayType::New(); ret->alloc(nbOfTuples,1); T *ptr=ret->getPointer(); -- 2.39.2