From 742aad82b06f772d09c0410004e7f1e25c4b47ca Mon Sep 17 00:00:00 2001 From: ageay Date: Tue, 28 Feb 2012 07:44:44 +0000 Subject: [PATCH] Implementation of subtuple assignation in python. --- src/MEDCoupling/MEDCouplingMemArray.cxx | 168 ++++++++++++++---- src/MEDCoupling_Swig/MEDCoupling.i | 40 ++--- src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 24 +++ 3 files changed, 176 insertions(+), 56 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 393c5b4a3..55c685872 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -1131,17 +1131,42 @@ void DataArrayDouble::setPartOfValues2(const DataArrayDouble *a, const int *bgTu DataArray::CheckValueInRange(nbComp,*z,"invalid component id"); int newNbOfTuples=(int)std::distance(bgTuples,endTuples); int newNbOfComp=(int)std::distance(bgComp,endComp); - a->checkNbOfElems(newNbOfTuples*newNbOfComp,msg); - if(strictCompoCompare) - a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + bool assignTech=true; + if(a->getNbOfElems()==newNbOfTuples*newNbOfComp) + { + if(strictCompoCompare) + a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + } + else + { + a->checkNbOfTuplesAndComp(1,newNbOfComp,msg); + assignTech=false; + } double *pt=getPointer(); const double *srcPt=a->getConstPointer(); - for(const int *w=bgTuples;w!=endTuples;w++) - for(const int *z=bgComp;z!=endComp;z++,srcPt++) - { - DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id"); - pt[(*w)*nbComp+(*z)]=*srcPt; - } + if(assignTech) + { + for(const int *w=bgTuples;w!=endTuples;w++) + { + DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id"); + for(const int *z=bgComp;z!=endComp;z++,srcPt++) + { + pt[(*w)*nbComp+(*z)]=*srcPt; + } + } + } + else + { + for(const int *w=bgTuples;w!=endTuples;w++) + { + const double *srcPt2=srcPt; + DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id"); + for(const int *z=bgComp;z!=endComp;z++,srcPt2++) + { + pt[(*w)*nbComp+(*z)]=*srcPt2; + } + } + } } /*! @@ -1178,17 +1203,40 @@ void DataArrayDouble::setPartOfValues3(const DataArrayDouble *a, const int *bgTu DataArray::CheckValueInRange(nbComp,bgComp,"invalid begin component value"); DataArray::CheckClosingParInRange(nbComp,endComp,"invalid end component value"); int newNbOfTuples=(int)std::distance(bgTuples,endTuples); - a->checkNbOfElems(newNbOfTuples*newNbOfComp,msg); - if(strictCompoCompare) - a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + bool assignTech=true; + if(a->getNbOfElems()==newNbOfTuples*newNbOfComp) + { + if(strictCompoCompare) + a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + } + else + { + a->checkNbOfTuplesAndComp(1,newNbOfComp,msg); + assignTech=false; + } double *pt=getPointer()+bgComp; const double *srcPt=a->getConstPointer(); - for(const int *w=bgTuples;w!=endTuples;w++) - for(int j=0;jcheckNbOfElems(newNbOfTuples*newNbOfComp,msg); - if(strictCompoCompare) - a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + bool assignTech=true; + if(a->getNbOfElems()==newNbOfTuples*newNbOfComp) + { + if(strictCompoCompare) + a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + } + else + { + a->checkNbOfTuplesAndComp(1,newNbOfComp,msg); + assignTech=false; + } int *pt=getPointer(); const int *srcPt=a->getConstPointer(); - for(const int *w=bgTuples;w!=endTuples;w++) - for(const int *z=bgComp;z!=endComp;z++,srcPt++) - { - DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id"); - pt[(*w)*nbComp+(*z)]=*srcPt; - } + if(assignTech) + { + for(const int *w=bgTuples;w!=endTuples;w++) + { + DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id"); + for(const int *z=bgComp;z!=endComp;z++,srcPt++) + { + pt[(*w)*nbComp+(*z)]=*srcPt; + } + } + } + else + { + for(const int *w=bgTuples;w!=endTuples;w++) + { + const int *srcPt2=srcPt; + DataArray::CheckValueInRange(nbOfTuples,*w,"invalid tuple id"); + for(const int *z=bgComp;z!=endComp;z++,srcPt2++) + { + pt[(*w)*nbComp+(*z)]=*srcPt2; + } + } + } } /*! @@ -3629,17 +3702,40 @@ void DataArrayInt::setPartOfValues3(const DataArrayInt *a, const int *bgTuples, DataArray::CheckValueInRange(nbComp,bgComp,"invalid begin component value"); DataArray::CheckClosingParInRange(nbComp,endComp,"invalid end component value"); int newNbOfTuples=(int)std::distance(bgTuples,endTuples); - a->checkNbOfElems(newNbOfTuples*newNbOfComp,msg); - if(strictCompoCompare) - a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + bool assignTech=true; + if(a->getNbOfElems()==newNbOfTuples*newNbOfComp) + { + if(strictCompoCompare) + a->checkNbOfTuplesAndComp(newNbOfTuples,newNbOfComp,msg); + } + else + { + a->checkNbOfTuplesAndComp(1,newNbOfComp,msg); + assignTech=false; + } int *pt=getPointer()+bgComp; const int *srcPt=a->getConstPointer(); - for(const int *w=bgTuples;w!=endTuples;w++) - for(int j=0;juseArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),0,nbOfComponents,1,false); return self; case 3: @@ -2330,7 +2330,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),0,nbOfComponents,1,false); return self; case 3: @@ -2370,7 +2370,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),ic1,ic1+1,1,false); return self; case 3: @@ -2410,7 +2410,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),ic1,ic1+1,1,false); return self; case 3: @@ -2430,7 +2430,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,&it1,&it1+1,&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -2450,7 +2450,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,&vt1[0],&vt1[0]+vt1.size(),&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -2479,7 +2479,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,&nv[0],&nv[0]+nv.size(),&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -2499,7 +2499,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -2539,7 +2539,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),pc1.first,pc1.second.first,pc1.second.second,false); return self; case 3: @@ -2579,7 +2579,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayDouble::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),pc1.first,pc1.second.first,pc1.second.second,false); return self; case 3: @@ -3683,7 +3683,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),0,nbOfComponents,1,false); return self; case 3: @@ -3723,7 +3723,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),0,nbOfComponents,1,false); return self; case 3: @@ -3763,7 +3763,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),ic1,ic1+1,1,false); return self; case 3: @@ -3803,7 +3803,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),ic1,ic1+1,1,false); return self; case 3: @@ -3823,7 +3823,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,&it1,&it1+1,&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -3843,7 +3843,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,&vt1[0],&vt1[0]+vt1.size(),&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -3872,7 +3872,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,&nv[0],&nv[0]+nv.size(),&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -3892,7 +3892,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues2(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),&vc1[0],&vc1[0]+vc1.size(),false); return self; case 3: @@ -3932,7 +3932,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),pc1.first,pc1.second.first,pc1.second.second,false); return self; case 3: @@ -3972,7 +3972,7 @@ namespace ParaMEDMEM return self; case 2: tmp=DataArrayInt::New(); - tmp->useArray(&v1[0],false,CPP_DEALLOC,v1.size(),1); + tmp->useArray(&v1[0],false,CPP_DEALLOC,1,v1.size()); self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),pc1.first,pc1.second.first,pc1.second.second,false); return self; case 3: diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 539c57775..5f0fd796a 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -6419,6 +6419,18 @@ class MEDCouplingBasicsTest(unittest.TestCase): st2=da.getHiddenCppPointer() self.assertEqual(st1,st2) self.assertEqual([10.0, 12.5, 15.0, 14.0, 16.0, 18.0, 15.0, 16.5, 18.0, 13.0, 14.0, 15.0],da.getValues()) + # + da.alloc(30,1) + da.iota(7.) + da.rearrange(3) + ids=DataArrayInt.New() + ids.setValues([3,4,7],3,1) + da[ids,:]=[5.,8.,9.] + self.assertEqual([7.,8.,9.,10.,11.,12.,13.,14.,15.,5.,8.,9.,5.,8.,9.,22.,23.,24.,25.,26.,27.,5.,8.,9.,31.,32.,33.,34.,35.,36.0],da.getValues()) + # + da.rearrange(1) ; da.iota(7) ; da.rearrange(3) + da[ids,[1,2]]=[5,8] + self.assertEqual([7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,5.,8.,19.,5.,8.,22.,23.,24.,25.,26.,27.,28.,5.,8.,31.,32.,33.,34.,35.,36.],da.getValues()) pass def testSwigDAIOp(self): @@ -6487,6 +6499,18 @@ class MEDCouplingBasicsTest(unittest.TestCase): st2=da.getHiddenCppPointer() self.assertEqual(st1,st2) self.assertEqual([4,4,3,0,4,4,3,3,0,0,2,2],da.getValues()) + # + da.alloc(30,1) + da.iota(7) + da.rearrange(3) + ids=DataArrayInt.New() + ids.setValues([3,4,7],3,1) + da[ids,:]=[5,8,9] + self.assertEqual([7,8,9,10,11,12,13,14,15,5,8,9,5,8,9,22,23,24,25,26,27,5,8,9,31,32,33,34,35,36],da.getValues()) + # + da.rearrange(1) ; da.iota(7) ; da.rearrange(3) + da[ids,[1,2]]=[5,8] + self.assertEqual([7,8,9,10,11,12,13,14,15,16,5,8,19,5,8,22,23,24,25,26,27,28,5,8,31,32,33,34,35,36],da.getValues()) pass def testSwigDAIOp2(self): -- 2.39.2