Salome HOME
DataArrayInt::getIdsEqualTuple
authorageay <ageay>
Mon, 16 Dec 2013 12:05:20 +0000 (12:05 +0000)
committerageay <ageay>
Mon, 16 Dec 2013 12:05:20 +0000 (12:05 +0000)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index 824cd21d4b498aeb7c5c369079a3ce4f6febe215..ff071afa6900e094d3a01acf462312d08df7ede6 100644 (file)
@@ -8395,13 +8395,14 @@ DataArrayIntIterator *DataArrayInt::iterator()
  *          array using decrRef() as it is no more needed.
  *  \throw If \a this is not allocated.
  *  \throw If \a this->getNumberOfComponents() != 1.
+ *  \sa DataArrayInt::getIdsEqualTuple
  */
 DataArrayInt *DataArrayInt::getIdsEqual(int val) const
 {
   checkAllocated();
   if(getNumberOfComponents()!=1)
     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqual : the array must have only one component, you can call 'rearrange' method before !");
-  const int *cptr=getConstPointer();
+  const int *cptr(getConstPointer());
   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
   int nbOfTuples=getNumberOfTuples();
   for(int i=0;i<nbOfTuples;i++,cptr++)
@@ -8424,7 +8425,7 @@ DataArrayInt *DataArrayInt::getIdsNotEqual(int val) const
   checkAllocated();
   if(getNumberOfComponents()!=1)
     throw INTERP_KERNEL::Exception("DataArrayInt::getIdsNotEqual : the array must have only one component, you can call 'rearrange' method before !");
-  const int *cptr=getConstPointer();
+  const int *cptr(getConstPointer());
   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
   int nbOfTuples=getNumberOfTuples();
   for(int i=0;i<nbOfTuples;i++,cptr++)
@@ -8433,6 +8434,45 @@ DataArrayInt *DataArrayInt::getIdsNotEqual(int val) const
   return ret.retn();
 }
 
+/*!
+ * Creates a new DataArrayInt containing IDs (indices) of tuples holding tuple equal to those defined by [ \a tupleBg , \a tupleEnd )
+ * This method is an extension of  DataArrayInt::getIdsEqual method.
+ *
+ *  \param [in] tupleBg - the begin (included) of the input tuple to find within \a this.
+ *  \param [in] tupleEnd - the end (excluded) of the input tuple to find within \a this.
+ *  \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() != std::distance(tupleBg,tupleEnd).
+ * \throw If \a this->getNumberOfComponents() is equal to 0.
+ * \sa DataArrayInt::getIdsEqual
+ */
+DataArrayInt *DataArrayInt::getIdsEqualTuple(const int *tupleBg, const int *tupleEnd) const
+{
+  std::size_t nbOfCompoExp(std::distance(tupleBg,tupleEnd));
+  checkAllocated();
+  if(getNumberOfComponents()!=(int)nbOfCompoExp)
+    {
+      std::ostringstream oss; oss << "DataArrayInt::getIdsEqualTuple : mismatch of number of components. Input tuple has " << nbOfCompoExp << " whereas this array has " << getNumberOfComponents() << " components !";
+      throw INTERP_KERNEL::Exception(oss.str().c_str());
+    }
+  if(nbOfCompoExp==0)
+    throw INTERP_KERNEL::Exception("DataArrayInt::getIdsEqualTuple : number of components should be > 0 !");
+  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
+  const int *bg(begin()),*end2(end()),*work(begin());
+  while(work!=end2)
+    {
+      work=std::search(work,end2,tupleBg,tupleEnd);
+      if(work!=end2)
+        {
+          std::size_t pos(std::distance(bg,work));
+          if(pos%nbOfCompoExp==0)
+            ret->pushBackSilent(pos/nbOfCompoExp);
+          work++;
+        }
+    }
+  return ret.retn();
+}
 
 /*!
  * Assigns \a newValue to all elements holding \a oldValue within \a this
index af0e9b255144a1456b1ecc90cd67e7d8d4d4a6d9..3f6e38a893276ab311b8c314b1dfca25c12358bd 100644 (file)
@@ -534,6 +534,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT DataArrayInt *getIdsNotEqual(int val) const;
     MEDCOUPLING_EXPORT DataArrayInt *getIdsEqualList(const int *valsBg, const int *valsEnd) const;
     MEDCOUPLING_EXPORT DataArrayInt *getIdsNotEqualList(const int *valsBg, const int *valsEnd) const;
+    MEDCOUPLING_EXPORT DataArrayInt *getIdsEqualTuple(const int *tupleBg, const int *tupleEnd) const;
     MEDCOUPLING_EXPORT int changeValue(int oldValue, int newValue);
     MEDCOUPLING_EXPORT int locateTuple(const std::vector<int>& tupl) const;
     MEDCOUPLING_EXPORT int locateValue(int value) const;
index 60d22ed44f48d9fe461bbfc22fff3a62073df67b..f4e4e111bdfc65eeaca106e1f3014cb9e31251ce 100644 (file)
@@ -14025,6 +14025,20 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertTrue(m.getCellsContainingPoint([0.,0.27],1e-12).isEqual(DataArrayInt([2])))
         pass
 
+    def testSwig2DAIGetIdsEqualTuple1(self):
+        da=DataArrayInt([0,7,1,2,4,1,2,1,1,2,0,1,2,1,5,1,1,2],9,2)
+        self.assertTrue(da.getIdsEqualTuple([1,2]).isEqual(DataArrayInt([1,4,8])))
+        self.assertTrue(da.getIdsEqualTuple((1,2)).isEqual(DataArrayInt([1,4,8])))
+        self.assertTrue(da.getIdsEqualTuple(DataArrayInt([1,2])).isEqual(DataArrayInt([1,4,8])))
+        da.rearrange(3)
+        self.assertRaises(InterpKernelException,da.getIdsEqualTuple,[1,2])# mismatch nb of compo (3) and nb of elts in input tuple (2)
+        self.assertTrue(da.getIdsEqualTuple([2,0,1]).isEqual(DataArrayInt([3])))
+        self.assertTrue(da.getIdsEqualTuple([2,0,7]).isEqual(DataArrayInt([])))
+        da.rearrange(1)
+        self.assertTrue(da.getIdsEqualTuple(2).isEqual(DataArrayInt([3,6,9,12,17])))
+        self.assertTrue(da.getIdsEqualTuple(2).isEqual(da.getIdsEqual(2)))
+        pass
+
     def setUp(self):
         pass
     pass
index 248cd2028a8f30c6144be5c9af2d4e04634d1e23..d664d08944f865fd35caa312b85337ce675987e0 100644 (file)
@@ -67,6 +67,7 @@
 %newobject ParaMEDMEM::DataArrayInt::getIdsNotEqual;
 %newobject ParaMEDMEM::DataArrayInt::getIdsEqualList;
 %newobject ParaMEDMEM::DataArrayInt::getIdsNotEqualList;
+%newobject ParaMEDMEM::DataArrayInt::getIdsEqualTuple;
 %newobject ParaMEDMEM::DataArrayInt::sumPerTuple;
 %newobject ParaMEDMEM::DataArrayInt::negate;
 %newobject ParaMEDMEM::DataArrayInt::computeAbs;
@@ -2753,6 +2754,14 @@ namespace ParaMEDMEM
         return self->accumulatePerChunck(bg,bg+sz);
       }
 
+      DataArrayInt *getIdsEqualTuple(PyObject *inputTuple) const throw(INTERP_KERNEL::Exception)
+      {
+        int sw,sz,val;
+        std::vector<int> val2;
+        const int *bg(convertObjToPossibleCpp1_Safe(inputTuple,sw,sz,val,val2));
+        return self->getIdsEqualTuple(bg,bg+sz);
+      }
+
       PyObject *splitInBalancedSlices(int nbOfSlices) const throw(INTERP_KERNEL::Exception)
       {
         std::vector< std::pair<int,int> > slcs(self->splitInBalancedSlices(nbOfSlices));