From cd525755ec2ee6e505ceb1e24112d0d0e16772f5 Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 23 Feb 2012 14:48:08 +0000 Subject: [PATCH] Some new methods in DataArrayInt --- src/MEDCoupling/MEDCouplingMemArray.cxx | 143 ++++++++++++- src/MEDCoupling/MEDCouplingMemArray.hxx | 3 + .../Test/MEDCouplingBasicsTest5.cxx | 188 ++++++++++++++++++ .../Test/MEDCouplingBasicsTest5.hxx | 4 + src/MEDCoupling_Swig/MEDCoupling.i | 15 ++ src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 166 ++++++++++++++++ src/MEDCoupling_Swig/MEDCouplingTypemaps.i | 57 ++++++ 7 files changed, 569 insertions(+), 7 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 45b07fb92..850c40a8e 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -813,7 +813,7 @@ DataArrayDouble *DataArrayDouble::selectByTupleRanges(const std::vector=0 && (*it).second<=nbOfTuplesThis) { nbOfTuples+=(*it).second-(*it).first; if(isIncreasing) @@ -1305,12 +1305,14 @@ void DataArrayDouble::setContigPartOfSelectedValues2(int tupleIdStart, const Dat const char msg[]="DataArrayDouble::setContigPartOfSelectedValues2"; int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg); if(nbOfComp!=a->getNumberOfComponents()) - throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : This and a do not have the same number of components !"); + throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : This and a do not have the same number of components !"); int thisNt=getNumberOfTuples(); int aNt=a->getNumberOfTuples(); double *valsToSet=getPointer()+tupleIdStart*nbOfComp; if(tupleIdStart+nbOfTupleToWrite>thisNt) - throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues : invalid number range of values to write !"); + throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid number range of values to write !"); + if(end2>aNt) + throw INTERP_KERNEL::Exception("DataArrayDouble::setContigPartOfSelectedValues2 : invalid range of values to read !"); const double *valsSrc=a->getConstPointer()+bg*nbOfComp; for(int i=0;i >& ranges) const throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + int nbOfComp=getNumberOfComponents(); + int nbOfTuplesThis=getNumberOfTuples(); + if(ranges.empty()) + { + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc(0,nbOfComp); + ret->copyStringInfoFrom(*this); + return ret; + } + int st=ranges.front().first; + int endd=ranges.back().second; + int ref=st; + int nbOfTuples=0; + bool isIncreasing=true; + for(std::vector >::const_iterator it=ranges.begin();it!=ranges.end();it++) + { + if((*it).first<=(*it).second) + { + if((*it).first>=0 && (*it).second<=nbOfTuplesThis) + { + nbOfTuples+=(*it).second-(*it).first; + if(isIncreasing) + isIncreasing=ref<=(*it).first; + ref=(*it).second; + } + else + { + std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it); + oss << " (" << (*it).first << "," << (*it).second << ") is greater than number of tuples of this :" << nbOfTuples << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + else + { + std::ostringstream oss; oss << "DataArrayInt::selectByTupleRanges : on range #" << std::distance(ranges.begin(),it); + oss << " (" << (*it).first << "," << (*it).second << ") end is before begin !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + if(isIncreasing && nbOfTuplesThis==nbOfTuples) + return deepCpy(); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuples,nbOfComp); + ret->copyStringInfoFrom(*this); + const int *src=getConstPointer(); + int *work=ret->getPointer(); + for(std::vector >::const_iterator it=ranges.begin();it!=ranges.end();it++) + work=std::copy(src+(*it).first*nbOfComp,src+(*it).second*nbOfComp,work); + ret->incrRef(); + return ret; +} + /*! * This method works only for arrays having single component. * If this contains the array a1 containing [9,10,0,6,4,11,3,7] this method returns an array a2 [5,6,0,3,2,7,1,4]. @@ -3644,6 +3706,73 @@ void DataArrayInt::setPartOfValuesAdv(const DataArrayInt *a, const DataArrayInt } } +/*! + * 'this', 'a' and 'tuplesSelec' are expected to be defined. If not an exception will be thrown. + * This is a method that is a specialization to DataArrayInt::setPartOfValuesAdv method, except that here the tuple selection of 'a' is given by a range ('bg','end2' and 'step') + * rather than an explicite array of tuple ids (given by the 2nd component) and the feeding is done in 'this' contiguously starting from 'tupleIdStart'. + * @param a is an array having exactly the same number of components than 'this' + */ +void DataArrayInt::setContigPartOfSelectedValues(int tupleIdStart, const DataArrayInt*a, const DataArrayInt *tuplesSelec) throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + a->checkAllocated(); + tuplesSelec->checkAllocated(); + int nbOfComp=getNumberOfComponents(); + if(nbOfComp!=a->getNumberOfComponents()) + throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : This and a do not have the same number of components !"); + if(tuplesSelec->getNumberOfComponents()!=1) + throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : Expecting to have a tuple selector DataArrayInt instance with exactly 1 component !"); + int thisNt=getNumberOfTuples(); + int aNt=a->getNumberOfTuples(); + int nbOfTupleToWrite=tuplesSelec->getNumberOfTuples(); + int *valsToSet=getPointer()+tupleIdStart*nbOfComp; + if(tupleIdStart+nbOfTupleToWrite>thisNt) + throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues : invalid number range of values to write !"); + const int *valsSrc=a->getConstPointer(); + for(const int *tuple=tuplesSelec->begin();tuple!=tuplesSelec->end();tuple++,valsToSet+=nbOfComp) + { + if(*tuple>=0 && *tuplebegin(),tuple); + oss << " of 'tuplesSelec' request of tuple id #" << *tuple << " in 'a' ! It should be in [0," << aNt << ") !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } +} + +/*! + * 'this' and 'a' are expected to be defined. If not an exception will be thrown. + * This is a method that is a specialization to DataArrayInt::setContigPartOfSelectedValues method, except that here the tuple selection is givenin a is done by a range ('bg','end2' and 'step') + * rather than an explicite array of tuple ids. + * @param a is an array having exactly the same number of components than 'this' + */ +void DataArrayInt::setContigPartOfSelectedValues2(int tupleIdStart, const DataArrayInt *a, int bg, int end2, int step) throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + a->checkAllocated(); + int nbOfComp=getNumberOfComponents(); + const char msg[]="DataArrayInt::setContigPartOfSelectedValues2"; + int nbOfTupleToWrite=DataArray::GetNumberOfItemGivenBES(bg,end2,step,msg); + if(nbOfComp!=a->getNumberOfComponents()) + throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : This and a do not have the same number of components !"); + int thisNt=getNumberOfTuples(); + int aNt=a->getNumberOfTuples(); + int *valsToSet=getPointer()+tupleIdStart*nbOfComp; + if(tupleIdStart+nbOfTupleToWrite>thisNt) + throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid number range of values to write !"); + if(end2>aNt) + throw INTERP_KERNEL::Exception("DataArrayInt::setContigPartOfSelectedValues2 : invalid range of values to read !"); + const int *valsSrc=a->getConstPointer()+bg*nbOfComp; + for(int i=0;i& a int DataArrayInt::getMaxValue(int& tupleId) const throw(INTERP_KERNEL::Exception) { if(getNumberOfComponents()!=1) - throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : must be applied on DataArrayDouble with only one component !"); + throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !"); int nbOfTuples=getNumberOfTuples(); if(nbOfTuples<=0) - throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : array exists but number of tuples must be > 0 !"); + throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !"); const int *vals=getConstPointer(); const int *loc=std::max_element(vals,vals+nbOfTuples); tupleId=(int)std::distance(vals,loc); @@ -3849,10 +3978,10 @@ int DataArrayInt::getMaxValueInArray() const throw(INTERP_KERNEL::Exception) int DataArrayInt::getMinValue(int& tupleId) const throw(INTERP_KERNEL::Exception) { if(getNumberOfComponents()!=1) - throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : must be applied on DataArrayDouble with only one component !"); + throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : must be applied on DataArrayInt with only one component !"); int nbOfTuples=getNumberOfTuples(); if(nbOfTuples<=0) - throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxValue : array exists but number of tuples must be > 0 !"); + throw INTERP_KERNEL::Exception("DataArrayInt::getMaxValue : array exists but number of tuples must be > 0 !"); const int *vals=getConstPointer(); const int *loc=std::min_element(vals,vals+nbOfTuples); tupleId=(int)std::distance(vals,loc); diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 1ad5ef8aa..22ee1a151 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -353,6 +353,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayInt *selectByTupleId(const int *new2OldBg, const int *new2OldEnd) const; MEDCOUPLING_EXPORT DataArrayInt *selectByTupleIdSafe(const int *new2OldBg, const int *new2OldEnd) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *selectByTupleId2(int bg, int end, int step) const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayInt *selectByTupleRanges(const std::vector >& ranges) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *checkAndPreparePermutation() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void changeSurjectiveFormat(int targetNb, DataArrayInt *&arr, DataArrayInt *&arrI) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT static DataArrayInt *BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTuples, const DataArrayInt *arr, const DataArrayInt *arrI, int &newNbOfTuples) throw(INTERP_KERNEL::Exception); @@ -372,6 +373,8 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void setPartOfValues3(const DataArrayInt *a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp, bool strictCompoCompare=true) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void setPartOfValuesSimple3(int a, const int *bgTuples, const int *endTuples, int bgComp, int endComp, int stepComp) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void setPartOfValuesAdv(const DataArrayInt *a, const DataArrayInt *tuplesSelec) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT void setContigPartOfSelectedValues(int tupleIdStart, const DataArrayInt*a, const DataArrayInt *tuplesSelec) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT void setContigPartOfSelectedValues2(int tupleIdStart, const DataArrayInt *a, int bg, int end2, int step) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void getTuple(int tupleId, int *res) const { std::copy(_mem.getConstPointerLoc(tupleId*((int)_info_on_compo.size())),_mem.getConstPointerLoc((tupleId+1)*((int)_info_on_compo.size())),res); } MEDCOUPLING_EXPORT int getIJ(int tupleId, int compoId) const { return _mem[tupleId*((int)_info_on_compo.size())+compoId]; } MEDCOUPLING_EXPORT int back() const throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx index c49d78d8f..3ea07c2a2 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx @@ -304,3 +304,191 @@ void MEDCouplingBasicsTest5::testBuildSlice3DSurf1() // mesh2D->decrRef(); } + +void MEDCouplingBasicsTest5::testDataArrayDoubleAdvSetting1() +{ + const double data1[14]={1.,11.,2.,12.,3.,13.,4.,14.,5.,15.,6.,16.,7.,17.}; + const double data2[10]={8.,38.,9.,39.,0.,30.,11.,41.,12.,42.}; + const char *comps[2]={"comp1","comp2"}; + std::vector compsCpp(comps,comps+2); + DataArrayDouble *da=DataArrayDouble::New(); + DataArrayDouble *tmp=0; + da->setInfoAndChangeNbOfCompo(compsCpp); + da->setName("da"); + da->alloc(7,2); + compsCpp.pop_back(); + CPPUNIT_ASSERT_THROW(da->setInfoAndChangeNbOfCompo(compsCpp),INTERP_KERNEL::Exception); + std::copy(data1,data1+14,da->getPointer()); + // + std::vector > p(3); + p[0].first=0; p[0].second=3; p[1].first=3; p[1].second=5; p[2].first=5; p[2].second=7; + tmp=da->selectByTupleRanges(p); + CPPUNIT_ASSERT(tmp->isEqual(*da,1e-14)); + tmp->decrRef(); + p[0].first=0; p[0].second=2; p[1].first=3; p[1].second=4; p[2].first=5; p[2].second=7; + tmp=da->selectByTupleRanges(p); + const double expected1[10]={1.,11.,2.,12.,4.,14.,6.,16.,7.,17.}; + CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents()); + for(int i=0;i<10;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected1[i],tmp->getIJ(0,i),1e-14); + tmp->decrRef(); + p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=6; + tmp=da->selectByTupleRanges(p); + const double expected2[10]={1.,11.,2.,12.,1.,11.,2.,12.,6.,16.}; + CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents()); + for(int i=0;i<10;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected2[i],tmp->getIJ(0,i),1e-14); + tmp->decrRef(); + p[0].first=0; p[0].second=2; p[1].first=-1; p[1].second=2; p[2].first=5; p[2].second=6; + CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception); + p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=8; + CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception); + // + DataArrayDouble *da2=DataArrayDouble::New(); + da2->alloc(5,2); + std::copy(data2,data2+10,da2->getPointer()); + // + DataArrayDouble *dac=da->deepCpy(); + dac->setContigPartOfSelectedValues2(1,da2,2,4,1); + const double expected3[14]={1.,11.,0.,30.,11.,41.,4.,14.,5.,15.,6.,16.,7.,17.}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected3[i],dac->getIJ(0,i),1e-14); + dac->decrRef(); + // + dac=da->deepCpy(); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,0,5,1),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(0,da2,4,6,1),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,5,0,1),INTERP_KERNEL::Exception); + dac->setContigPartOfSelectedValues2(3,da2,1,5,1); + const double expected4[14]={1.,11.,2.,12.,3.,13.,9.,39.,0.,30.,11.,41.,12.,42.}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected4[i],dac->getIJ(0,i),1e-14); + dac->decrRef(); + // + DataArrayInt *ids=DataArrayInt::New(); + ids->alloc(3,1); + dac=da->deepCpy(); + ids->setIJ(0,0,2); ids->setIJ(1,0,0); ids->setIJ(2,0,4); + dac->setContigPartOfSelectedValues(2,da2,ids); + const double expected5[14]={1.,11.,2.,12.,0.,30.,8.,38.,12.,42.,6.,16.,7.,17.}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected5[i],dac->getIJ(0,i),1e-14); + dac->decrRef(); + // + dac=da->deepCpy(); + ids->setIJ(0,0,2); ids->setIJ(1,0,5); ids->setIJ(2,0,4); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception); + ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,-1); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception); + ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(5,da2,ids),INTERP_KERNEL::Exception); + dac->decrRef(); + // + ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1); + dac=da->deepCpy(); + dac->setContigPartOfSelectedValues(4,da2,ids); + const double expected6[14]={1.,11.,2.,12.,3.,13.,4.,14.,0.,30.,0.,30.,9.,39.}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected6[i],dac->getIJ(0,i),1e-14); + dac->decrRef(); + ids->decrRef(); + // + da2->decrRef(); + da->decrRef(); +} + +void MEDCouplingBasicsTest5::testDataArrayIntAdvSetting1() +{ + const int data1[14]={1,11,2,12,3,13,4,14,5,15,6,16,7,17}; + const int data2[10]={8,38,9,39,0,30,11,41,12,42}; + const char *comps[2]={"comp1","comp2"}; + std::vector compsCpp(comps,comps+2); + DataArrayInt *da=DataArrayInt::New(); + DataArrayInt *tmp=0; + da->setInfoAndChangeNbOfCompo(compsCpp); + da->setName("da"); + da->alloc(7,2); + compsCpp.pop_back(); + CPPUNIT_ASSERT_THROW(da->setInfoAndChangeNbOfCompo(compsCpp),INTERP_KERNEL::Exception); + std::copy(data1,data1+14,da->getPointer()); + // + std::vector > p(3); + p[0].first=0; p[0].second=3; p[1].first=3; p[1].second=5; p[2].first=5; p[2].second=7; + tmp=da->selectByTupleRanges(p); + CPPUNIT_ASSERT(tmp->isEqual(*da)); + tmp->decrRef(); + p[0].first=0; p[0].second=2; p[1].first=3; p[1].second=4; p[2].first=5; p[2].second=7; + tmp=da->selectByTupleRanges(p); + const int expected1[10]={1,11,2,12,4,14,6,16,7,17}; + CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents()); + for(int i=0;i<10;i++) + CPPUNIT_ASSERT_EQUAL(expected1[i],tmp->getIJ(0,i)); + tmp->decrRef(); + p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=6; + tmp=da->selectByTupleRanges(p); + const int expected2[10]={1,11,2,12,1,11,2,12,6,16}; + CPPUNIT_ASSERT_EQUAL(5,tmp->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(2,tmp->getNumberOfComponents()); + for(int i=0;i<10;i++) + CPPUNIT_ASSERT_EQUAL(expected2[i],tmp->getIJ(0,i)); + tmp->decrRef(); + p[0].first=0; p[0].second=2; p[1].first=-1; p[1].second=2; p[2].first=5; p[2].second=6; + CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception); + p[0].first=0; p[0].second=2; p[1].first=0; p[1].second=2; p[2].first=5; p[2].second=8; + CPPUNIT_ASSERT_THROW(da->selectByTupleRanges(p),INTERP_KERNEL::Exception); + // + DataArrayInt *da2=DataArrayInt::New(); + da2->alloc(5,2); + std::copy(data2,data2+10,da2->getPointer()); + // + DataArrayInt *dac=da->deepCpy(); + dac->setContigPartOfSelectedValues2(1,da2,2,4,1); + const int expected3[14]={1,11,0,30,11,41,4,14,5,15,6,16,7,17}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_EQUAL(expected3[i],dac->getIJ(0,i)); + dac->decrRef(); + // + dac=da->deepCpy(); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,0,5,1),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(0,da2,4,6,1),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues2(3,da2,5,0,1),INTERP_KERNEL::Exception); + dac->setContigPartOfSelectedValues2(3,da2,1,5,1); + const int expected4[14]={1,11,2,12,3,13,9,39,0,30,11,41,12,42}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_EQUAL(expected4[i],dac->getIJ(0,i)); + dac->decrRef(); + // + DataArrayInt *ids=DataArrayInt::New(); + ids->alloc(3,1); + dac=da->deepCpy(); + ids->setIJ(0,0,2); ids->setIJ(1,0,0); ids->setIJ(2,0,4); + dac->setContigPartOfSelectedValues(2,da2,ids); + const int expected5[14]={1,11,2,12,0,30,8,38,12,42,6,16,7,17}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_EQUAL(expected5[i],dac->getIJ(0,i)); + dac->decrRef(); + // + dac=da->deepCpy(); + ids->setIJ(0,0,2); ids->setIJ(1,0,5); ids->setIJ(2,0,4); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception); + ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,-1); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(1,da2,ids),INTERP_KERNEL::Exception); + ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1); + CPPUNIT_ASSERT_THROW(dac->setContigPartOfSelectedValues(5,da2,ids),INTERP_KERNEL::Exception); + dac->decrRef(); + // + ids->setIJ(0,0,2); ids->setIJ(1,0,2); ids->setIJ(2,0,1); + dac=da->deepCpy(); + dac->setContigPartOfSelectedValues(4,da2,ids); + const int expected6[14]={1,11,2,12,3,13,4,14,0,30,0,30,9,39}; + for(int i=0;i<14;i++) + CPPUNIT_ASSERT_EQUAL(expected6[i],dac->getIJ(0,i)); + dac->decrRef(); + ids->decrRef(); + // + da2->decrRef(); + da->decrRef(); +} diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx index 6d8a20610..a20c1da19 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx @@ -40,6 +40,8 @@ namespace ParaMEDMEM CPPUNIT_TEST( testGetCellIdsCrossingPlane1 ); CPPUNIT_TEST( testBuildSlice3D1 ); CPPUNIT_TEST( testBuildSlice3DSurf1 ); + CPPUNIT_TEST( testDataArrayDoubleAdvSetting1 ); + CPPUNIT_TEST( testDataArrayIntAdvSetting1 ); CPPUNIT_TEST_SUITE_END(); public: void testUMeshTessellate2D1(); @@ -47,6 +49,8 @@ namespace ParaMEDMEM void testGetCellIdsCrossingPlane1(); void testBuildSlice3D1(); void testBuildSlice3DSurf1(); + void testDataArrayDoubleAdvSetting1(); + void testDataArrayIntAdvSetting1(); }; } diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index ebfd04bef..74c752b46 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -125,6 +125,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayInt::selectByTupleId; %newobject ParaMEDMEM::DataArrayInt::selectByTupleIdSafe; %newobject ParaMEDMEM::DataArrayInt::selectByTupleId2; +%newobject ParaMEDMEM::DataArrayInt::selectByTupleRanges; %newobject ParaMEDMEM::DataArrayInt::checkAndPreparePermutation; %newobject ParaMEDMEM::DataArrayInt::transformWithIndArrR; %newobject ParaMEDMEM::DataArrayInt::renumber; @@ -2126,6 +2127,13 @@ namespace ParaMEDMEM return DataArrayDouble::Meld(tmp); } + DataArrayDouble *selectByTupleRanges(PyObject *li) const throw(INTERP_KERNEL::Exception) + { + std::vector > ranges; + convertPyToVectorPairInt(li,ranges); + return self->selectByTupleRanges(ranges); + } + DataArrayDouble *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception) { const char msg[]="Unexpected situation in __getitem__ !"; @@ -3464,6 +3472,13 @@ namespace ParaMEDMEM return res; } + DataArrayInt *selectByTupleRanges(PyObject *li) const throw(INTERP_KERNEL::Exception) + { + std::vector > ranges; + convertPyToVectorPairInt(li,ranges); + return self->selectByTupleRanges(ranges); + } + static DataArrayInt *Meld(PyObject *li) throw(INTERP_KERNEL::Exception) { std::vector tmp; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index bb038c49b..77ffeed3a 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -8715,6 +8715,172 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertAlmostEqual(expected8[i],slice1.getCoords().getIJ(0,i),12); pass pass + + def testDataArrayDoubleAdvSetting1(self): + data1=[1.,11.,2.,12.,3.,13.,4.,14.,5.,15.,6.,16.,7.,17.] + data2=[8.,38.,9.,39.,0.,30.,11.,41.,12.,42.] + compsCpp=["comp1","comp2"] + da=DataArrayDouble.New(); + da.setInfoAndChangeNbOfCompo(compsCpp); + da.setName("da"); + da.alloc(7,2); + compsCpp=compsCpp[:-1] + self.assertRaises(InterpKernelException,da.setInfoAndChangeNbOfCompo,compsCpp); + da.setValues(data1,7,2) + # + p=[(0,3),(3,5),(5,7)] + tmp=da.selectByTupleRanges(p); + self.assertTrue(tmp.isEqual(da,1e-14)); + p=[(0,2),(3,4),(5,7)] + tmp=da.selectByTupleRanges(p); + expected1=[1.,11.,2.,12.,4.,14.,6.,16.,7.,17.] + self.assertEqual(5,tmp.getNumberOfTuples()); + self.assertEqual(2,tmp.getNumberOfComponents()); + for i in xrange(10): + self.assertAlmostEqual(expected1[i],tmp.getIJ(0,i),14); + pass + p=[(0,2),(0,2),(5,6)] + tmp=da.selectByTupleRanges(p); + expected2=[1.,11.,2.,12.,1.,11.,2.,12.,6.,16.] + self.assertEqual(5,tmp.getNumberOfTuples()); + self.assertEqual(2,tmp.getNumberOfComponents()); + for i in xrange(10): + self.assertAlmostEqual(expected2[i],tmp.getIJ(0,i),14); + pass + p=[(0,2),(-1,2),(5,6)] + self.assertRaises(InterpKernelException,da.selectByTupleRanges,p); + p=[(0,2),(0,2),(5,8)] + self.assertRaises(InterpKernelException,da.selectByTupleRanges,p); + # + da2=DataArrayDouble.New(); + da2.setValues(data2,5,2); + # + dac=da.deepCpy(); + dac.setContigPartOfSelectedValues2(1,da2,2,4,1); + expected3=[1.,11.,0.,30.,11.,41.,4.,14.,5.,15.,6.,16.,7.,17.] + for i in xrange(14): + self.assertAlmostEqual(expected3[i],dac.getIJ(0,i),14); + pass + # + dac=da.deepCpy(); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,0,5,1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,0,da2,4,6,1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,5,0,1); + dac.setContigPartOfSelectedValues2(3,da2,1,5,1); + expected4=[1.,11.,2.,12.,3.,13.,9.,39.,0.,30.,11.,41.,12.,42.] + for i in xrange(14): + self.assertAlmostEqual(expected4[i],dac.getIJ(0,i),14); + pass + # + ids=DataArrayInt.New(); + ids.alloc(3,1); + dac=da.deepCpy(); + ids.setIJ(0,0,2); ids.setIJ(1,0,0); ids.setIJ(2,0,4); + dac.setContigPartOfSelectedValues(2,da2,ids); + expected5=[1.,11.,2.,12.,0.,30.,8.,38.,12.,42.,6.,16.,7.,17.] + for i in xrange(14): + self.assertAlmostEqual(expected5[i],dac.getIJ(0,i),14); + pass + # + dac=da.deepCpy(); + ids.setIJ(0,0,2); ids.setIJ(1,0,5); ids.setIJ(2,0,4); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids); + ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,-1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids); + ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,5,da2,ids); + # + ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1); + dac=da.deepCpy(); + dac.setContigPartOfSelectedValues(4,da2,ids); + expected6=[1.,11.,2.,12.,3.,13.,4.,14.,0.,30.,0.,30.,9.,39.] + for i in xrange(14): + self.assertAlmostEqual(expected6[i],dac.getIJ(0,i),14); + pass + pass + + def testDataArrayIntAdvSetting1(self): + data1=[1,11,2,12,3,13,4,14,5,15,6,16,7,17] + data2=[8,38,9,39,0,30,11,41,12,42] + compsCpp=["comp1","comp2"] + da=DataArrayInt.New(); + da.setInfoAndChangeNbOfCompo(compsCpp); + da.setName("da"); + da.alloc(7,2); + compsCpp=compsCpp[:-1] + self.assertRaises(InterpKernelException,da.setInfoAndChangeNbOfCompo,compsCpp); + da.setValues(data1,7,2) + # + p=[(0,3),(3,5),(5,7)] + tmp=da.selectByTupleRanges(p); + self.assertTrue(tmp.isEqual(da)); + p=[(0,2),(3,4),(5,7)] + tmp=da.selectByTupleRanges(p); + expected1=[1,11,2,12,4,14,6,16,7,17] + self.assertEqual(5,tmp.getNumberOfTuples()); + self.assertEqual(2,tmp.getNumberOfComponents()); + for i in xrange(10): + self.assertEqual(expected1[i],tmp.getIJ(0,i)); + pass + p=[(0,2),(0,2),(5,6)] + tmp=da.selectByTupleRanges(p); + expected2=[1,11,2,12,1,11,2,12,6,16] + self.assertEqual(5,tmp.getNumberOfTuples()); + self.assertEqual(2,tmp.getNumberOfComponents()); + for i in xrange(10): + self.assertEqual(expected2[i],tmp.getIJ(0,i)); + pass + p=[(0,2),(-1,2),(5,6)] + self.assertRaises(InterpKernelException,da.selectByTupleRanges,p); + p=[(0,2),(0,2),(5,8)] + self.assertRaises(InterpKernelException,da.selectByTupleRanges,p); + # + da2=DataArrayInt.New(); + da2.setValues(data2,5,2); + # + dac=da.deepCpy(); + dac.setContigPartOfSelectedValues2(1,da2,2,4,1); + expected3=[1,11,0,30,11,41,4,14,5,15,6,16,7,17] + for i in xrange(14): + self.assertEqual(expected3[i],dac.getIJ(0,i)); + pass + # + dac=da.deepCpy(); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,0,5,1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,0,da2,4,6,1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues2,3,da2,5,0,1); + dac.setContigPartOfSelectedValues2(3,da2,1,5,1); + expected4=[1,11,2,12,3,13,9,39,0,30,11,41,12,42] + for i in xrange(14): + self.assertEqual(expected4[i],dac.getIJ(0,i)); + pass + # + ids=DataArrayInt.New(); + ids.alloc(3,1); + dac=da.deepCpy(); + ids.setIJ(0,0,2); ids.setIJ(1,0,0); ids.setIJ(2,0,4); + dac.setContigPartOfSelectedValues(2,da2,ids); + expected5=[1,11,2,12,0,30,8,38,12,42,6,16,7,17] + for i in xrange(14): + self.assertEqual(expected5[i],dac.getIJ(0,i)); + pass + # + dac=da.deepCpy(); + ids.setIJ(0,0,2); ids.setIJ(1,0,5); ids.setIJ(2,0,4); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids); + ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,-1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,1,da2,ids); + ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1); + self.assertRaises(InterpKernelException,dac.setContigPartOfSelectedValues,5,da2,ids); + # + ids.setIJ(0,0,2); ids.setIJ(1,0,2); ids.setIJ(2,0,1); + dac=da.deepCpy(); + dac.setContigPartOfSelectedValues(4,da2,ids); + expected6=[1,11,2,12,3,13,4,14,0,30,0,30,9,39] + for i in xrange(14): + self.assertEqual(expected6[i],dac.getIJ(0,i)); + pass + pass def setUp(self): pass diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index b077fdda4..f116a9945 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -173,6 +173,63 @@ static int *convertPyToNewIntArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL } } +static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair >& arr) throw(INTERP_KERNEL::Exception) +{ + const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !"; + if(PyList_Check(pyLi)) + { + int size=PyList_Size(pyLi); + arr.resize(size); + for(int i=0;i& arr) throw(INTERP_KERNEL::Exception) { if(PyList_Check(pyLi)) -- 2.39.2