From: Anthony Geay Date: Wed, 10 May 2017 06:21:08 +0000 (+0200) Subject: Small but useful DataArrayInt::checkUniformAndGuess X-Git-Tag: V8_4_0a1~52 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=69fd460430f4507abe451a07ff684688c3ae15e8;p=tools%2Fmedcoupling.git Small but useful DataArrayInt::checkUniformAndGuess --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index b5559feaa..a9e68fdfd 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -5028,21 +5028,45 @@ bool DataArrayInt::isIota(int sizeExpected) const * \return bool - \a true if all values are \a val. * \throw If \a this is not allocated. * \throw If \a this->getNumberOfComponents() != 1 + * \sa DataArrayInt::checkUniformAndGuess */ bool DataArrayInt::isUniform(int val) const { checkAllocated(); if(getNumberOfComponents()!=1) throw INTERP_KERNEL::Exception("DataArrayInt::isUniform : must be applied on DataArrayInt with only one component, you can call 'rearrange' method before !"); - int nbOfTuples=getNumberOfTuples(); - const int *w=getConstPointer(); - const int *end2=w+nbOfTuples; + const int *w(begin()),*end2(end()); for(;w!=end2;w++) if(*w!=val) return false; return true; } +/*! + * This method checks that \a this is uniform. If not and exception will be thrown. + * In case of uniformity the corresponding value is returned. + * + * \return int - the unique value contained in this + * \throw If \a this is not allocated. + * \throw If \a this->getNumberOfComponents() != 1 + * \throw If \a this is not uniform. + * \sa DataArrayInt::isUniform + */ +int DataArrayInt::checkUniformAndGuess() const +{ + checkAllocated(); + if(getNumberOfComponents()!=1) + throw INTERP_KERNEL::Exception("DataArrayInt::checkUniformAndGuess : must be applied on DataArrayInt with only one component, you can call 'rearrange' method before !"); + if(empty()) + throw INTERP_KERNEL::Exception("DataArrayInt::checkUniformAndGuess : this is empty !"); + const int *w(begin()),*end2(end()); + int ret(*w); + for(;w!=end2;w++) + if(*w!=ret) + throw INTERP_KERNEL::Exception("DataArrayInt::checkUniformAndGuess : this is not uniform !"); + return ret; +} + /*! * Checks if all values in \a this array are unique. * \return bool - \a true if condition above is true diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index f22c61d43..1aab9c787 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -583,6 +583,7 @@ namespace MEDCoupling MEDCOUPLING_EXPORT DataArrayInt *buildPermArrPerLevel() const; MEDCOUPLING_EXPORT bool isIota(int sizeExpected) const; MEDCOUPLING_EXPORT bool isUniform(int val) const; + MEDCOUPLING_EXPORT int checkUniformAndGuess() const; MEDCOUPLING_EXPORT bool hasUniqueValues() const; MEDCOUPLING_EXPORT void setSelectedComponents(const DataArrayInt *a, const std::vector& compoIds); MEDCOUPLING_EXPORT DataArrayIntIterator *iterator(); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py index b1ed46f08..83df022da 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py @@ -4649,6 +4649,21 @@ class MEDCouplingBasicsTest5(unittest.TestCase): d2.transformWithIndArr(m) self.assertTrue(d2.isEqual(DataArrayInt([0,0,1,0,1]))) pass + + def testDAICheckUniformAndGuess1(self): + d=DataArrayInt([3,3],1,2) + self.assertRaises(InterpKernelException,d.checkUniformAndGuess)# non single compo + d=DataArrayInt([]) + self.assertRaises(InterpKernelException,d.checkUniformAndGuess)# empty + d=DataArrayInt() + self.assertRaises(InterpKernelException,d.checkUniformAndGuess)# non allocated + d=DataArrayInt([3,3,3]) + self.assertEqual(3,d.checkUniformAndGuess()) + d=DataArrayInt([7]) + self.assertEqual(7,d.checkUniformAndGuess()) + d=DataArrayInt([3,4,3]) + self.assertRaises(InterpKernelException,d.checkUniformAndGuess)# non uniform + pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index 2bff77e43..bc0240c5d 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -2331,6 +2331,7 @@ namespace MEDCoupling DataArrayInt *buildPermArrPerLevel() const throw(INTERP_KERNEL::Exception); bool isIota(int sizeExpected) const throw(INTERP_KERNEL::Exception); bool isUniform(int val) const throw(INTERP_KERNEL::Exception); + int checkUniformAndGuess() const throw(INTERP_KERNEL::Exception); bool hasUniqueValues() const throw(INTERP_KERNEL::Exception); DataArrayInt *subArray(int tupleIdBg, int tupleIdEnd=-1) const throw(INTERP_KERNEL::Exception); void transpose() throw(INTERP_KERNEL::Exception);