From e9967e47d2217fbdf2197ae627f56cb24f602fc7 Mon Sep 17 00:00:00 2001 From: ageay Date: Mon, 3 Jan 2011 16:37:52 +0000 Subject: [PATCH] *** empty log message *** --- src/MEDCoupling/MEDCouplingMemArray.cxx | 123 ++++++++++-------- src/MEDCoupling/MEDCouplingMemArray.hxx | 4 +- .../Test/MEDCouplingBasicsTest1.cxx | 3 +- src/MEDCoupling_Swig/MEDCoupling.i | 20 ++- src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 14 ++ src/MEDCoupling_Swig/MEDCouplingTypemaps.i | 29 ----- src/MEDLoader/MEDFileMesh.cxx | 4 +- src/MEDLoader/MEDFileMesh.hxx | 2 +- src/MEDLoader/MEDFileMeshLL.cxx | 3 +- src/MEDLoader/MEDLoader.cxx | 3 +- 10 files changed, 116 insertions(+), 89 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 0761e66da..efd539ac4 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -2096,14 +2097,14 @@ DataArrayInt *DataArrayInt::Meld(const std::vector& a) thr * @param newNb specifies size of whole set. Must be at least equal to max eltid in 'groups'. * @return an array of size newNb specifying fid of each item. */ -DataArrayInt *DataArrayInt::MakePartition(const std::vector& groups, int newNb, std::vector< std::vector >& fidsOfGroups) +DataArrayInt *DataArrayInt::MakePartition(const std::vector& groups, int newNb, std::vector< std::vector >& fidsOfGroups) { DataArrayInt *ret=DataArrayInt::New(); ret->alloc(newNb,1); int *retPtr=ret->getPointer(); std::fill(retPtr,retPtr+newNb,0); int fid=1; - for(std::vector::const_iterator iter=groups.begin();iter!=groups.end();iter++) + for(std::vector::const_iterator iter=groups.begin();iter!=groups.end();iter++) { const int *ptr=(*iter)->getConstPointer(); int nbOfElem=(*iter)->getNbOfElems(); @@ -2126,7 +2127,7 @@ DataArrayInt *DataArrayInt::MakePartition(const std::vector& gro fidsOfGroups.clear(); fidsOfGroups.resize(groups.size()); int grId=0; - for(std::vector::const_iterator iter=groups.begin();iter!=groups.end();iter++,grId++) + for(std::vector::const_iterator iter=groups.begin();iter!=groups.end();iter++,grId++) { std::set tmp; const int *ptr=(*iter)->getConstPointer(); @@ -2138,6 +2139,68 @@ DataArrayInt *DataArrayInt::MakePartition(const std::vector& gro return ret; } +DataArrayInt *DataArrayInt::BuildUnion(const std::vector& a) throw(INTERP_KERNEL::Exception) +{ + int valm=std::numeric_limits::max(); + for(std::vector::const_iterator it=a.begin();it!=a.end();it++) + { + (*it)->checkAllocated(); + if((*it)->getNumberOfComponents()!=1) + throw INTERP_KERNEL::Exception("DataArrayInt::BuildUnion : only single component allowed !"); + int tmp1; + valm=std::min((*it)->getMinValue(tmp1),valm); + } + if(valm<0) + throw INTERP_KERNEL::Exception("DataArrayInt::BuildUnion : a negative value has been detected !"); + // + std::set r; + for(std::vector::const_iterator it=a.begin();it!=a.end();it++) + { + const int *pt=(*it)->getConstPointer(); + int nbOfTuples=(*it)->getNumberOfTuples(); + r.insert(pt,pt+nbOfTuples); + } + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc(r.size(),1); + std::copy(r.begin(),r.end(),ret->getPointer()); + return ret; +} + +DataArrayInt *DataArrayInt::BuildIntersection(const std::vector& a) throw(INTERP_KERNEL::Exception) +{ + int valm=std::numeric_limits::max(); + for(std::vector::const_iterator it=a.begin();it!=a.end();it++) + { + (*it)->checkAllocated(); + if((*it)->getNumberOfComponents()!=1) + throw INTERP_KERNEL::Exception("DataArrayInt::BuildUnion : only single component allowed !"); + int tmp1; + valm=std::min((*it)->getMinValue(tmp1),valm); + } + if(valm<0) + throw INTERP_KERNEL::Exception("DataArrayInt::BuildUnion : a negative value has been detected !"); + // + std::set r; + for(std::vector::const_iterator it=a.begin();it!=a.end();it++) + { + const int *pt=(*it)->getConstPointer(); + int nbOfTuples=(*it)->getNumberOfTuples(); + std::set s1(pt,pt+nbOfTuples); + if(it!=a.begin()) + { + std::set r2; + std::set_intersection(r.begin(),r.end(),s1.begin(),s1.end(),inserter(r2,r2.end())); + r=r2; + } + else + r=s1; + } + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc(r.size(),1); + std::copy(r.begin(),r.end(),ret->getPointer()); + return ret; +} + DataArrayInt *DataArrayInt::buildComplement(int nbOfElement) const throw(INTERP_KERNEL::Exception) { checkAllocated(); @@ -2186,58 +2249,16 @@ DataArrayInt *DataArrayInt::buildSubstraction(const DataArrayInt *other) const t DataArrayInt *DataArrayInt::buildUnion(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception) { - checkAllocated(); - other->checkAllocated(); - if(getNumberOfComponents()!=1) - throw INTERP_KERNEL::Exception("DataArrayInt::buildUnion : only single component allowed !"); - if(other->getNumberOfComponents()!=1) - throw INTERP_KERNEL::Exception("DataArrayInt::buildUnion : only single component allowed for other type !"); - int tmp1; - int valm=getMinValue(tmp1); - valm=std::min(other->getMinValue(tmp1),valm); - if(valm<0) - throw INTERP_KERNEL::Exception("DataArrayInt::buildUnion : a negative value has been detected !"); - // - const int *pt=getConstPointer(); - int nbOfTuples=getNumberOfTuples(); - std::set s1(pt,pt+nbOfTuples); - pt=other->getConstPointer(); - nbOfTuples=other->getNumberOfTuples(); - std::set s2(pt,pt+nbOfTuples); - std::vector r; - std::set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),std::back_insert_iterator< std::vector >(r)); - DataArrayInt *ret=DataArrayInt::New(); - ret->alloc(r.size(),1); - std::copy(r.begin(),r.end(),ret->getPointer()); - return ret; + std::vectorarrs(2); + arrs[0]=this; arrs[1]=other; + return BuildUnion(arrs); } DataArrayInt *DataArrayInt::buildIntersection(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception) { - checkAllocated(); - other->checkAllocated(); - if(getNumberOfComponents()!=1) - throw INTERP_KERNEL::Exception("DataArrayInt::buildIntersection : only single component allowed !"); - if(other->getNumberOfComponents()!=1) - throw INTERP_KERNEL::Exception("DataArrayInt::buildIntersection : only single component allowed for other type !"); - int tmp1; - int valm=getMinValue(tmp1); - valm=std::min(other->getMinValue(tmp1),valm); - if(valm<0) - throw INTERP_KERNEL::Exception("DataArrayInt::buildIntersection : a negative value has been detected !"); - // - const int *pt=getConstPointer(); - int nbOfTuples=getNumberOfTuples(); - std::set s1(pt,pt+nbOfTuples); - pt=other->getConstPointer(); - nbOfTuples=other->getNumberOfTuples(); - std::set s2(pt,pt+nbOfTuples); - std::vector r; - std::set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),std::back_insert_iterator< std::vector >(r)); - DataArrayInt *ret=DataArrayInt::New(); - ret->alloc(r.size(),1); - std::copy(r.begin(),r.end(),ret->getPointer()); - return ret; + std::vectorarrs(2); + arrs[0]=this; arrs[1]=other; + return BuildIntersection(arrs); } /*! diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 9194cc88e..da5331829 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -270,7 +270,9 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT static DataArrayInt *Aggregate(const DataArrayInt *a1, const DataArrayInt *a2, int offsetA2); MEDCOUPLING_EXPORT static DataArrayInt *Meld(const DataArrayInt *a1, const DataArrayInt *a2) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT static DataArrayInt *Meld(const std::vector& a) throw(INTERP_KERNEL::Exception); - MEDCOUPLING_EXPORT static DataArrayInt *MakePartition(const std::vector& groups, int newNb, std::vector< std::vector >& fidsOfGroups); + MEDCOUPLING_EXPORT static DataArrayInt *MakePartition(const std::vector& groups, int newNb, std::vector< std::vector >& fidsOfGroups); + MEDCOUPLING_EXPORT static DataArrayInt *BuildUnion(const std::vector& a) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT static DataArrayInt *BuildIntersection(const std::vector& a) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *buildComplement(int nbOfElement) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *buildSubstraction(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *buildUnion(const DataArrayInt *other) const throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest1.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest1.cxx index ad2baa704..12c726f3b 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest1.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest1.cxx @@ -2026,7 +2026,8 @@ void MEDCouplingBasicsTest::testFuseUMeshesOnSameCoords() CPPUNIT_ASSERT_EQUAL(expectedVals2[i][j],vals[j]); } std::vector< std::vector > fidsOfGroups; - DataArrayInt *arr2=DataArrayInt::MakePartition(corr,m7->getNumberOfCells(),fidsOfGroups); + std::vector corr2(corr.begin(),corr.end()); + DataArrayInt *arr2=DataArrayInt::MakePartition(corr2,m7->getNumberOfCells(),fidsOfGroups); const int fidExp[4]={5,1,3,4}; const int fidsGrp[3][3]={{1,3,5},{3,4,5},{4,5,23344}}; CPPUNIT_ASSERT_EQUAL(3,(int)fidsOfGroups.size()); diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 1cd5b1737..4ec419da3 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -118,6 +118,8 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayInt::getIdsEqualList; %newobject ParaMEDMEM::DataArrayInt::Aggregate; %newobject ParaMEDMEM::DataArrayInt::Meld; +%newobject ParaMEDMEM::DataArrayInt::BuildUnion; +%newobject ParaMEDMEM::DataArrayInt::BuildIntersection; %newobject ParaMEDMEM::DataArrayInt::fromNoInterlace; %newobject ParaMEDMEM::DataArrayInt::toNoInterlace; %newobject ParaMEDMEM::DataArrayInt::buildComplement; @@ -1193,9 +1195,9 @@ namespace ParaMEDMEM static PyObject *MakePartition(PyObject *gps, int newNb) throw(INTERP_KERNEL::Exception) { - std::vector groups; + std::vector groups; std::vector< std::vector > fidsOfGroups; - convertPyObjToVecDataArrayInt(gps,groups); + convertPyObjToVecDataArrayIntCst(gps,groups); ParaMEDMEM::DataArrayInt *ret0=ParaMEDMEM::DataArrayInt::MakePartition(groups,newNb,fidsOfGroups); PyObject *ret = PyList_New(2); PyList_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(ret0),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 )); @@ -1310,6 +1312,20 @@ namespace ParaMEDMEM return DataArrayInt::Meld(tmp); } + static DataArrayInt *BuildUnion(PyObject *li) throw(INTERP_KERNEL::Exception) + { + std::vector tmp; + convertPyObjToVecDataArrayIntCst(li,tmp); + return DataArrayInt::BuildUnion(tmp); + } + + static DataArrayInt *BuildIntersection(PyObject *li) throw(INTERP_KERNEL::Exception) + { + std::vector tmp; + convertPyObjToVecDataArrayIntCst(li,tmp); + return DataArrayInt::BuildIntersection(tmp); + } + PyObject *getMaxValue() const throw(INTERP_KERNEL::Exception) { int tmp; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index a989f582d..a5f449cc3 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -5369,6 +5369,13 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(7,b.getNumberOfTuples()); self.assertEqual(1,b.getNumberOfComponents()); expected1=[0,1,3,5,7,8,18] + for i in xrange(7): + self.assertEqual(expected1[i],b.getIJ(0,i)); + pass + b=DataArrayInt.BuildUnion([a,c]); + self.assertEqual(7,b.getNumberOfTuples()); + self.assertEqual(1,b.getNumberOfComponents()); + expected1=[0,1,3,5,7,8,18] for i in xrange(7): self.assertEqual(expected1[i],b.getIJ(0,i)); pass @@ -5385,6 +5392,13 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(2,b.getNumberOfTuples()); self.assertEqual(1,b.getNumberOfComponents()); expected1=[3,8] + for i in xrange(2): + self.assertEqual(expected1[i],b.getIJ(0,i)); + pass + b=DataArrayInt.BuildIntersection([a,c]); + self.assertEqual(2,b.getNumberOfTuples()); + self.assertEqual(1,b.getNumberOfComponents()); + expected1=[3,8] for i in xrange(2): self.assertEqual(expected1[i],b.getIJ(0,i)); pass diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index af41bf45e..a42063aec 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -538,35 +538,6 @@ void convertPyObjToVecFieldDblCst(PyObject *ms, std::vector& v) throw(INTERP_KERNEL::Exception) -{ - if(PyList_Check(ms)) - { - int size=PyList_Size(ms); - v.resize(size); - for(int i=0;i(argp); - v[i]=arg; - } - } - else - { - const char msg[]="convertPyObjToVecDataArrayInt : not a list"; - PyErr_SetString(PyExc_TypeError,msg); - throw INTERP_KERNEL::Exception(msg); - } -} - void convertPyObjToVecDataArrayIntCst(PyObject *ms, std::vector& v) throw(INTERP_KERNEL::Exception) { if(PyList_Check(ms)) diff --git a/src/MEDLoader/MEDFileMesh.cxx b/src/MEDLoader/MEDFileMesh.cxx index ad71693d6..8c9e19a32 100644 --- a/src/MEDLoader/MEDFileMesh.cxx +++ b/src/MEDLoader/MEDFileMesh.cxx @@ -541,14 +541,14 @@ void MEDFileUMesh::setCoords(DataArrayDouble *coords) throw(INTERP_KERNEL::Excep _fam_coords->fillWithZero(); } -void MEDFileUMesh::setGroupsAtLevel(int meshDimRelToMaxExt, const std::vector& grps, bool renum) throw(INTERP_KERNEL::Exception) +void MEDFileUMesh::setGroupsAtLevel(int meshDimRelToMaxExt, const std::vector& grps, bool renum) throw(INTERP_KERNEL::Exception) { if(grps.empty()) return ; std::set grpsName; std::vector grpsName2(grps.size()); int i=0; - for(std::vector::const_iterator it=grps.begin();it!=grps.end();it++,i++) + for(std::vector::const_iterator it=grps.begin();it!=grps.end();it++,i++) { grpsName.insert((*it)->getName()); grpsName2[i]=(*it)->getName(); diff --git a/src/MEDLoader/MEDFileMesh.hxx b/src/MEDLoader/MEDFileMesh.hxx index 14d812bbe..d6f585462 100644 --- a/src/MEDLoader/MEDFileMesh.hxx +++ b/src/MEDLoader/MEDFileMesh.hxx @@ -90,7 +90,7 @@ namespace ParaMEDMEM // void setFamilyNameAttachedOnId(int id, const std::string& newFamName) throw(INTERP_KERNEL::Exception); void setCoords(DataArrayDouble *coords) throw(INTERP_KERNEL::Exception); - void setGroupsAtLevel(int meshDimRelToMaxExt, const std::vector& grps, bool renum=true) throw(INTERP_KERNEL::Exception); + void setGroupsAtLevel(int meshDimRelToMaxExt, const std::vector& grps, bool renum=true) throw(INTERP_KERNEL::Exception); void eraseGroupsAtLevel(int meshDimRelToMaxExt) throw(INTERP_KERNEL::Exception); void setFamilyField(DataArrayInt *arr, const std::vector< std::vector< int > > &userfids, const std::vector& grpNames) throw(INTERP_KERNEL::Exception); void addNodeGroup(const std::string& name, const std::vector& ids) throw(INTERP_KERNEL::Exception); diff --git a/src/MEDLoader/MEDFileMeshLL.cxx b/src/MEDLoader/MEDFileMeshLL.cxx index 08e369c06..2bfdb7c7f 100644 --- a/src/MEDLoader/MEDFileMeshLL.cxx +++ b/src/MEDLoader/MEDFileMeshLL.cxx @@ -346,7 +346,8 @@ void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector corr; _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr); std::vector< std::vector > fidsOfGroups; - _fam=DataArrayInt::MakePartition(corr,_m->getNumberOfCells(),fidsOfGroups); + std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end()); + _fam=DataArrayInt::MakePartition(corr2,_m->getNumberOfCells(),fidsOfGroups); int nbOfCells=_m->getNumberOfCells(); std::map newfams; std::map famIdTrad; diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index 1e85c07bb..de5eb3fb5 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -2225,7 +2225,8 @@ void MEDLoaderNS::writeUMeshesPartitionDirectly(const char *fileName, const char MEDCouplingUMesh *m=ParaMEDMEM::MEDCouplingUMesh::FuseUMeshesOnSameCoords(meshes,0,corr); m->setName(meshName); std::vector< std::vector > fidsOfGroups; - DataArrayInt *arr2=DataArrayInt::MakePartition(corr,m->getNumberOfCells(),fidsOfGroups); + std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end()); + DataArrayInt *arr2=DataArrayInt::MakePartition(corr2,m->getNumberOfCells(),fidsOfGroups); for(std::vector< DataArrayInt * >::iterator it=corr.begin();it!=corr.end();it++) (*it)->decrRef(); bool isRenumbering; -- 2.39.2