* \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<nbOfSlices-1)
- stopSlice=start+minNbOfElemsPerSlice*step*(sliceId+1);
- else
- stopSlice=stop;
+ DataArrayTools<mcIdType>::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<begin)
- {
- std::ostringstream oss; oss << msg << " : end before begin !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- if(end==begin)
- return 0;
- if(step<=0)
- {
- std::ostringstream oss; oss << msg << " : invalid step should be > 0 !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- return (end-1-begin)/step+1;
+ return DataArrayTools<mcIdType>::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(end<begin && step>0)
- {
- std::ostringstream oss; oss << msg << " : end before begin whereas step is positive !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- if(begin<end && step<0)
- {
- std::ostringstream oss; oss << msg << " : invalid step should be > 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<mcIdType>::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<end)
- {
- if((value-begin)%step==0)
- return (value-begin)/step;
- else
- return -1;
- }
- else
- return -1;
- }
- else
- {
- if(begin>=value && value>end)
- {
- if((begin-value)%(-step)==0)
- return (begin-value)/(-step);
- else
- return -1;
- }
- else
- return -1;
- }
- }
- else
- return -1;
+ return DataArrayTools<mcIdType>::GetPosOfItemGivenBESRelativeNoThrow(value, begin, end, step);
}
/*!
void *_param_for_deallocator;
};
+ template <class T> 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;
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);
//////////////////////////////////
+ /*!
+ * 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 <class T>
+ void DataArrayTools<T>::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<T>::GetNumberOfItemGivenBESRelative(start,stop,step,"DataArray::GetSlice");
+ mcIdType minNbOfElemsPerSlice=nbElems/nbOfSlices;
+ startSlice=start+minNbOfElemsPerSlice*step*sliceId;
+ if(sliceId<nbOfSlices-1)
+ stopSlice=start+minNbOfElemsPerSlice*step*(sliceId+1);
+ else
+ stopSlice=stop;
+ }
+
+ template <class T>
+ mcIdType DataArrayTools<T>::GetNumberOfItemGivenBES(T begin, T end, T step, const std::string& msg)
+ {
+ if(end<begin)
+ {
+ std::ostringstream oss; oss << msg << " : end before begin !";
+ throw INTERP_KERNEL::Exception(oss.str().c_str());
+ }
+ if(end==begin)
+ return 0;
+ if(step<=0)
+ {
+ std::ostringstream oss; oss << msg << " : invalid step should be > 0 !";
+ throw INTERP_KERNEL::Exception(oss.str().c_str());
+ }
+ return ToIdType ((end-1-begin)/step+1);
+ }
+
+ template <class T>
+ mcIdType DataArrayTools<T>::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(end<begin && step>0)
+ {
+ std::ostringstream oss; oss << msg << " : end before begin whereas step is positive !";
+ throw INTERP_KERNEL::Exception(oss.str().c_str());
+ }
+ if(begin<end && step<0)
+ {
+ std::ostringstream oss; oss << msg << " : invalid step should be > 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 <class T>
+ mcIdType DataArrayTools<T>::GetPosOfItemGivenBESRelativeNoThrow(T value, T begin, T end, T step)
+ {
+ if (step == 0)
+ return -1;
+
+ if(step>0 && begin<=value && value<end ||
+ step<0 && begin>=value && value>end)
+ {
+ mcIdType id = ToIdType((value-begin)/step);
+ if (begin + step * id == value)
+ return id;
+ else
+ return -1;
+ }
+ else
+ return -1;
+ }
+
+ //////////////////////////////////
+
template<class T>
MCAuto< typename Traits<T>::ArrayTypeCh > DataArrayTemplate<T>::NewFromStdVector(const typename std::vector<T>& v)
{
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<T>::GetNumberOfItemGivenBESRelative(bg,stop,step,"DataArrayInt::buildExplicitArrOfSliceOnScaledArr");
T sz(0),pos(bg);
for(mcIdType i=0;i<nbOfEltsInSlc;i++,pos+=step)
{
template <class T>
typename Traits<T>::ArrayType *DataArrayDiscrete<T>::Range(T begin, T end, T step)
{
- mcIdType nbOfTuples=DataArray::GetNumberOfItemGivenBESRelative(ToIdType(begin),ToIdType(end),ToIdType(step),"DataArrayInt::Range");
+ mcIdType nbOfTuples=DataArrayTools<T>::GetNumberOfItemGivenBESRelative(begin,end,step,"DataArrayInt::Range");
MCAuto<DataArrayType> ret=DataArrayType::New();
ret->alloc(nbOfTuples,1);
T *ptr=ret->getPointer();