Salome HOME
DataArrayInt.indicesOfSubPart useful method
authorAnthony Geay <anthony.geay@edf.fr>
Thu, 12 Jan 2017 06:43:44 +0000 (07:43 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Thu, 12 Jan 2017 06:43:44 +0000 (07:43 +0100)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index 743e7dd7a9aec2e61036f5e2c3a6111101d6d817..dc9c49992b63a604d912b162154e6abeb22675ec 100644 (file)
@@ -5620,6 +5620,48 @@ DataArrayInt *DataArrayInt::buildPermutationArr(const DataArrayInt& other) const
   return ret.retn();
 }
 
+/*!
+ * Elements of \a partOfThis are expected to be included in \a this.
+ * The returned array \a ret is so that this[ret]==partOfThis
+ *
+ * For example, if \a this array contents are [9,10,0,6,4,11,3,8] and if \a partOfThis contains [6,0,11,8]
+ * the return array will contain [3,2,5,7].
+ *
+ * \a this is expected to be a 1 compo allocated array.
+ * \param [in] partOfThis - A 1 compo allocated array
+ * \return - A newly allocated array to be dealed by caller having the same number of tuples than \a partOfThis.
+ * \throw if two same element is present twice in \a this
+ * \throw if an element in \a partOfThis is \b NOT in \a this.
+ */
+DataArrayInt *DataArrayInt::indicesOfSubPart(const DataArrayInt& partOfThis) const
+{
+  if(getNumberOfComponents()!=1 || partOfThis.getNumberOfComponents()!=1)
+    throw INTERP_KERNEL::Exception("DataArrayInt::indicesOfSubPart : this and input array must be one component array !");
+  checkAllocated(); partOfThis.checkAllocated();
+  int thisNbTuples(getNumberOfTuples()),nbTuples(partOfThis.getNumberOfTuples());
+  const int *thisPt(begin()),*pt(partOfThis.begin());
+  MCAuto<DataArrayInt> ret(DataArrayInt::New());
+  ret->alloc(nbTuples,1);
+  int *retPt(ret->getPointer());
+  std::map<int,int> m;
+  for(int i=0;i<thisNbTuples;i++,thisPt++)
+    m[*thisPt]=i;
+  if(m.size()!=thisNbTuples)
+    throw INTERP_KERNEL::Exception("DataArrayInt::indicesOfSubPart : some elements appears more than once !");
+  for(int i=0;i<nbTuples;i++,retPt++,pt++)
+    {
+      std::map<int,int>::const_iterator it(m.find(*pt));
+      if(it!=m.end())
+        *retPt=(*it).second;
+      else
+        {
+          std::ostringstream oss; oss << "DataArrayInt::indicesOfSubPart : At pos #" << i << " of input array value is " << *pt << " not in this !";
+          throw INTERP_KERNEL::Exception(oss.str());
+        }
+    }
+  return ret.retn();
+}
+
 void DataArrayInt::aggregate(const DataArrayInt *other)
 {
   if(!other)
index 061e18d754b84503ff504459ed6864c3f6b132c4..ee2e6fcf9a6bebf37ada69d3a620aa4608283427 100644 (file)
@@ -487,6 +487,7 @@ namespace MEDCoupling
     MEDCOUPLING_EXPORT void switchOnTupleEqualTo(int val, std::vector<bool>& vec) const;
     MEDCOUPLING_EXPORT void switchOnTupleNotEqualTo(int val, std::vector<bool>& vec) const;
     MEDCOUPLING_EXPORT DataArrayInt *buildPermutationArr(const DataArrayInt& other) const;
+    MEDCOUPLING_EXPORT DataArrayInt *indicesOfSubPart(const DataArrayInt& partOfThis) const;
     MEDCOUPLING_EXPORT DataArrayInt *sumPerTuple() const;
     MEDCOUPLING_EXPORT void checkMonotonic(bool increasing) const;
     MEDCOUPLING_EXPORT bool isMonotonic(bool increasing) const;
index 31b0c9cdf6e48482e434d36deb68b33ebd12b0aa..480e586f22b74693eeac95a439ed3e7c6b6da522 100644 (file)
@@ -4456,6 +4456,18 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         self.assertEqual(f.getMesh().getHiddenCppPointer(),m.getHiddenCppPointer())
         self.assertTrue(f.getArray().isEqual(expected,1e-12))
         pass
+
+    def testDAIIndicesOfSubPart(self):
+        a=DataArrayInt([9,10,0,6,4,11,3,8])
+        b=DataArrayInt([6,0,11,8])
+        c=a.indicesOfSubPart(b)
+        self.assertTrue(c.isEqual(DataArrayInt([3,2,5,7])))
+        #
+        d=DataArrayInt([9,10,0,6,4,11,0,8])
+        self.assertRaises(InterpKernelException,d.indicesOfSubPart,b) # 0 appears twice in the d array
+        f=DataArrayInt([6,0,11,8,12])
+        self.assertRaises(InterpKernelException,a.indicesOfSubPart,f) # 12 in f does not exist in a
+        pass
     
     pass
 
index 87b46c260c96219817920d83a3009ee6af1ea67f..2851ccb7d395ce775eea26f002e97963ebb0fb7e 100644 (file)
@@ -86,6 +86,7 @@
 %newobject MEDCoupling::DataArrayInt::BuildUnion;
 %newobject MEDCoupling::DataArrayInt::BuildIntersection;
 %newobject MEDCoupling::DataArrayInt::Range;
+%newobject MEDCoupling::DataArrayInt::indicesOfSubPart;
 %newobject MEDCoupling::DataArrayInt::fromNoInterlace;
 %newobject MEDCoupling::DataArrayInt::toNoInterlace;
 %newobject MEDCoupling::DataArrayInt::buildComplement;
@@ -2640,6 +2641,7 @@ namespace MEDCoupling
     DataArrayInt *invertArrayN2O2O2N(int oldNbOfElem) const throw(INTERP_KERNEL::Exception);
     DataArrayInt *invertArrayO2N2N2OBis(int newNbOfElem) const throw(INTERP_KERNEL::Exception);
     DataArrayDouble *convertToDblArr() const throw(INTERP_KERNEL::Exception);
+    DataArrayInt *indicesOfSubPart(const DataArrayInt& partOfThis) const throw(INTERP_KERNEL::Exception);
     DataArrayInt *fromNoInterlace() const throw(INTERP_KERNEL::Exception);
     DataArrayInt *toNoInterlace() const throw(INTERP_KERNEL::Exception);
     DataArrayInt *selectByTupleIdSafeSlice(int bg, int end, int step) const throw(INTERP_KERNEL::Exception);