#include <set>
#include <cmath>
+#include <limits>
#include <numeric>
#include <functional>
* @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<DataArrayInt *>& groups, int newNb, std::vector< std::vector<int> >& fidsOfGroups)
+DataArrayInt *DataArrayInt::MakePartition(const std::vector<const DataArrayInt *>& groups, int newNb, std::vector< std::vector<int> >& 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<DataArrayInt *>::const_iterator iter=groups.begin();iter!=groups.end();iter++)
+ for(std::vector<const DataArrayInt *>::const_iterator iter=groups.begin();iter!=groups.end();iter++)
{
const int *ptr=(*iter)->getConstPointer();
int nbOfElem=(*iter)->getNbOfElems();
fidsOfGroups.clear();
fidsOfGroups.resize(groups.size());
int grId=0;
- for(std::vector<DataArrayInt *>::const_iterator iter=groups.begin();iter!=groups.end();iter++,grId++)
+ for(std::vector<const DataArrayInt *>::const_iterator iter=groups.begin();iter!=groups.end();iter++,grId++)
{
std::set<int> tmp;
const int *ptr=(*iter)->getConstPointer();
return ret;
}
+DataArrayInt *DataArrayInt::BuildUnion(const std::vector<const DataArrayInt *>& a) throw(INTERP_KERNEL::Exception)
+{
+ int valm=std::numeric_limits<int>::max();
+ for(std::vector<const DataArrayInt *>::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<int> r;
+ for(std::vector<const DataArrayInt *>::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<const DataArrayInt *>& a) throw(INTERP_KERNEL::Exception)
+{
+ int valm=std::numeric_limits<int>::max();
+ for(std::vector<const DataArrayInt *>::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<int> r;
+ for(std::vector<const DataArrayInt *>::const_iterator it=a.begin();it!=a.end();it++)
+ {
+ const int *pt=(*it)->getConstPointer();
+ int nbOfTuples=(*it)->getNumberOfTuples();
+ std::set<int> s1(pt,pt+nbOfTuples);
+ if(it!=a.begin())
+ {
+ std::set<int> 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();
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<int> s1(pt,pt+nbOfTuples);
- pt=other->getConstPointer();
- nbOfTuples=other->getNumberOfTuples();
- std::set<int> s2(pt,pt+nbOfTuples);
- std::vector<int> r;
- std::set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),std::back_insert_iterator< std::vector<int> >(r));
- DataArrayInt *ret=DataArrayInt::New();
- ret->alloc(r.size(),1);
- std::copy(r.begin(),r.end(),ret->getPointer());
- return ret;
+ std::vector<const DataArrayInt *>arrs(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<int> s1(pt,pt+nbOfTuples);
- pt=other->getConstPointer();
- nbOfTuples=other->getNumberOfTuples();
- std::set<int> s2(pt,pt+nbOfTuples);
- std::vector<int> r;
- std::set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),std::back_insert_iterator< std::vector<int> >(r));
- DataArrayInt *ret=DataArrayInt::New();
- ret->alloc(r.size(),1);
- std::copy(r.begin(),r.end(),ret->getPointer());
- return ret;
+ std::vector<const DataArrayInt *>arrs(2);
+ arrs[0]=this; arrs[1]=other;
+ return BuildIntersection(arrs);
}
/*!
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<const DataArrayInt *>& a) throw(INTERP_KERNEL::Exception);
- MEDCOUPLING_EXPORT static DataArrayInt *MakePartition(const std::vector<DataArrayInt *>& groups, int newNb, std::vector< std::vector<int> >& fidsOfGroups);
+ MEDCOUPLING_EXPORT static DataArrayInt *MakePartition(const std::vector<const DataArrayInt *>& groups, int newNb, std::vector< std::vector<int> >& fidsOfGroups);
+ MEDCOUPLING_EXPORT static DataArrayInt *BuildUnion(const std::vector<const DataArrayInt *>& a) throw(INTERP_KERNEL::Exception);
+ MEDCOUPLING_EXPORT static DataArrayInt *BuildIntersection(const std::vector<const DataArrayInt *>& 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);
CPPUNIT_ASSERT_EQUAL(expectedVals2[i][j],vals[j]);
}
std::vector< std::vector<int> > fidsOfGroups;
- DataArrayInt *arr2=DataArrayInt::MakePartition(corr,m7->getNumberOfCells(),fidsOfGroups);
+ std::vector<const DataArrayInt *> 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());
%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;
static PyObject *MakePartition(PyObject *gps, int newNb) throw(INTERP_KERNEL::Exception)
{
- std::vector<DataArrayInt *> groups;
+ std::vector<const DataArrayInt *> groups;
std::vector< std::vector<int> > 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 ));
return DataArrayInt::Meld(tmp);
}
+ static DataArrayInt *BuildUnion(PyObject *li) throw(INTERP_KERNEL::Exception)
+ {
+ std::vector<const DataArrayInt *> tmp;
+ convertPyObjToVecDataArrayIntCst(li,tmp);
+ return DataArrayInt::BuildUnion(tmp);
+ }
+
+ static DataArrayInt *BuildIntersection(PyObject *li) throw(INTERP_KERNEL::Exception)
+ {
+ std::vector<const DataArrayInt *> tmp;
+ convertPyObjToVecDataArrayIntCst(li,tmp);
+ return DataArrayInt::BuildIntersection(tmp);
+ }
+
PyObject *getMaxValue() const throw(INTERP_KERNEL::Exception)
{
int tmp;
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
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
}
}
-void convertPyObjToVecDataArrayInt(PyObject *ms, std::vector<ParaMEDMEM::DataArrayInt *>& v) throw(INTERP_KERNEL::Exception)
-{
- if(PyList_Check(ms))
- {
- int size=PyList_Size(ms);
- v.resize(size);
- for(int i=0;i<size;i++)
- {
- PyObject *obj=PyList_GetItem(ms,i);
- void *argp;
- int status=SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
- if(!SWIG_IsOK(status))
- {
- const char msg[]="list must contain only DataArrayInt";
- PyErr_SetString(PyExc_TypeError,msg);
- throw INTERP_KERNEL::Exception(msg);
- }
- ParaMEDMEM::DataArrayInt *arg=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(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<const ParaMEDMEM::DataArrayInt *>& v) throw(INTERP_KERNEL::Exception)
{
if(PyList_Check(ms))
_fam_coords->fillWithZero();
}
-void MEDFileUMesh::setGroupsAtLevel(int meshDimRelToMaxExt, const std::vector<DataArrayInt *>& grps, bool renum) throw(INTERP_KERNEL::Exception)
+void MEDFileUMesh::setGroupsAtLevel(int meshDimRelToMaxExt, const std::vector<const DataArrayInt *>& grps, bool renum) throw(INTERP_KERNEL::Exception)
{
if(grps.empty())
return ;
std::set<std::string> grpsName;
std::vector<std::string> grpsName2(grps.size());
int i=0;
- for(std::vector<DataArrayInt *>::const_iterator it=grps.begin();it!=grps.end();it++,i++)
+ for(std::vector<const DataArrayInt *>::const_iterator it=grps.begin();it!=grps.end();it++,i++)
{
grpsName.insert((*it)->getName());
grpsName2[i]=(*it)->getName();
//
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<DataArrayInt *>& grps, bool renum=true) throw(INTERP_KERNEL::Exception);
+ void setGroupsAtLevel(int meshDimRelToMaxExt, const std::vector<const DataArrayInt *>& 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<std::string>& grpNames) throw(INTERP_KERNEL::Exception);
void addNodeGroup(const std::string& name, const std::vector<int>& ids) throw(INTERP_KERNEL::Exception);
std::vector< DataArrayInt * > corr;
_m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
std::vector< std::vector<int> > 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<int,std::string> newfams;
std::map<int,int> famIdTrad;
MEDCouplingUMesh *m=ParaMEDMEM::MEDCouplingUMesh::FuseUMeshesOnSameCoords(meshes,0,corr);
m->setName(meshName);
std::vector< std::vector<int> > 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;