From: ageay Date: Mon, 16 Dec 2013 12:05:20 +0000 (+0000) Subject: DataArrayInt::getIdsEqualTuple X-Git-Tag: V7_3_0~2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=89d12fef9ec565d9160d84f2bf3eb93d3be0f7fc;p=modules%2Fmed.git DataArrayInt::getIdsEqualTuple --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 824cd21d4..ff071afa6 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -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 ret(DataArrayInt::New()); ret->alloc(0,1); int nbOfTuples=getNumberOfTuples(); for(int i=0;i ret(DataArrayInt::New()); ret->alloc(0,1); int nbOfTuples=getNumberOfTuples(); for(int i=0;igetNumberOfComponents() != 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 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 diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index af0e9b255..3f6e38a89 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -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& tupl) const; MEDCOUPLING_EXPORT int locateValue(int value) const; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 60d22ed44..f4e4e111b 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -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 diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index 248cd2028..d664d0894 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -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 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 > slcs(self->splitInBalancedSlices(nbOfSlices));