From 8b53e7bdc5480b1b397787132722ad149780f60a Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 12 Jan 2017 07:43:44 +0100 Subject: [PATCH] DataArrayInt.indicesOfSubPart useful method --- src/MEDCoupling/MEDCouplingMemArray.cxx | 42 +++++++++++++++++++ src/MEDCoupling/MEDCouplingMemArray.hxx | 1 + .../MEDCouplingBasicsTest5.py | 12 ++++++ src/MEDCoupling_Swig/MEDCouplingMemArray.i | 2 + 4 files changed, 57 insertions(+) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 743e7dd7a..dc9c49992 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -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 ret(DataArrayInt::New()); + ret->alloc(nbTuples,1); + int *retPt(ret->getPointer()); + std::map m; + for(int i=0;i::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) diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 061e18d75..ee2e6fcf9 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -487,6 +487,7 @@ namespace MEDCoupling MEDCOUPLING_EXPORT void switchOnTupleEqualTo(int val, std::vector& vec) const; MEDCOUPLING_EXPORT void switchOnTupleNotEqualTo(int val, std::vector& 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; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py index 31b0c9cdf..480e586f2 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py @@ -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 diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index 87b46c260..2851ccb7d 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -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); -- 2.39.2