Salome HOME
Small but useful DataArrayInt::checkUniformAndGuess
authorAnthony Geay <anthony.geay@edf.fr>
Wed, 10 May 2017 06:21:08 +0000 (08:21 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Wed, 10 May 2017 06:21:08 +0000 (08:21 +0200)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index b5559feaa5da9e9e1d5c4acc11ca9b129a3077c9..a9e68fdfd442bad3bd5afa96cdfcccf12e66fdc3 100644 (file)
@@ -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
index f22c61d439dd6061038fc66388d8da12e227fa55..1aab9c787c3f9a8ac8b8adcc88e34a6d0b2a6b43 100644 (file)
@@ -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<int>& compoIds);
     MEDCOUPLING_EXPORT DataArrayIntIterator *iterator();
index b1ed46f0806c83d8c95a790231ee04f82a9f9916..83df022daefc16ca320081325c0aefc309246819 100644 (file)
@@ -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
 
index 2bff77e433dc5f5baa3f859b7700edbb58efe720..bc0240c5d9cc88eb8f7465b498aa874a517d7b8c 100644 (file)
@@ -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);