From a8fae04b2820f7e66a5b58521f396ac765113008 Mon Sep 17 00:00:00 2001 From: ageay Date: Tue, 23 Aug 2011 06:29:16 +0000 Subject: [PATCH] *** empty log message *** --- src/MEDCoupling/MEDCouplingMemArray.cxx | 56 ++++ src/MEDCoupling/MEDCouplingMemArray.hxx | 31 +++ src/MEDCoupling_Swig/MEDCoupling.i | 251 +++++++++++++++++- src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 59 ++++ src/MEDCoupling_Swig/MEDCouplingTypemaps.i | 74 ++++++ 5 files changed, 469 insertions(+), 2 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 09e9d7ff5..2933769e5 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -1614,6 +1614,11 @@ void DataArrayDouble::applyFuncFast64(const char *func) throw(INTERP_KERNEL::Exc declareAsNew(); } +DataArrayDoubleIterator *DataArrayDouble::iterator() +{ + return new DataArrayDoubleIterator(this); +} + DataArrayInt *DataArrayDouble::getIdsInRange(double vmin, double vmax) const throw(INTERP_KERNEL::Exception) { if(getNumberOfComponents()!=1) @@ -2043,6 +2048,57 @@ void DataArrayDouble::finishUnserialization(const std::vector& tinyInfoI, c } } +DataArrayDoubleIterator::DataArrayDoubleIterator(DataArrayDouble *da):_da(da),_tuple(new DataArrayDoubleTuple(da)),_tuple_id(0),_nb_tuple(0) +{ + if(_da) + { + _da->incrRef(); + _nb_tuple=da->getNumberOfTuples(); + } +} + +DataArrayDoubleIterator::~DataArrayDoubleIterator() +{ + if(_da) + _da->decrRef(); + delete _tuple; +} + +DataArrayDoubleTuple *DataArrayDoubleIterator::nextt() +{ + if(_tuple_id<_nb_tuple) + { + _tuple_id++; + _tuple->next(); + return _tuple; + } + else + return 0; +} + +DataArrayDoubleTuple::DataArrayDoubleTuple(DataArrayDouble *da):_pt(0),_nb_of_compo(0) +{ + if(da) + { + _nb_of_compo=da->getNumberOfComponents(); + _pt=da->getPointer()-_nb_of_compo; + } +} + +void DataArrayDoubleTuple::next() +{ + _pt+=_nb_of_compo; +} + +std::string DataArrayDoubleTuple::repr() const +{ + std::ostringstream oss; oss.precision(15); oss << "("; + for(int i=0;i<_nb_of_compo-1;i++) + oss << _pt[i] << ", "; + oss << _pt[_nb_of_compo-1] << ")"; + return oss.str(); +} + DataArrayInt *DataArrayInt::New() { return new DataArrayInt; diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 202245484..2873a04c6 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -125,6 +125,7 @@ namespace ParaMEDMEM namespace ParaMEDMEM { class DataArrayInt; + class DataArrayDoubleIterator; class DataArrayDouble : public DataArray { public: @@ -183,6 +184,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT double *getPointer() { return _mem.getPointer(); } MEDCOUPLING_EXPORT static void SetArrayIn(DataArrayDouble *newArray, DataArrayDouble* &arrayToSet); MEDCOUPLING_EXPORT const double *getConstPointer() const { return _mem.getConstPointer(); } + MEDCOUPLING_EXPORT DataArrayDoubleIterator *iterator(); MEDCOUPLING_EXPORT const double *begin() const { return getConstPointer(); } MEDCOUPLING_EXPORT const double *end() const { return getConstPointer()+getNbOfElems(); } MEDCOUPLING_EXPORT void useArray(const double *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo); @@ -250,6 +252,35 @@ namespace ParaMEDMEM MemArray _mem; }; + class DataArrayDoubleTuple; + + class DataArrayDoubleIterator + { + public: + DataArrayDoubleIterator(DataArrayDouble *da); + ~DataArrayDoubleIterator(); + DataArrayDoubleTuple *nextt(); + private: + DataArrayDouble *_da; + DataArrayDoubleTuple *_tuple; + int _tuple_id; + int _nb_tuple; + }; + + class DataArrayDoubleTuple + { + public: + DataArrayDoubleTuple(DataArrayDouble *da); + void next(); + std::string repr() const; + int getNumberOfCompo() const { return _nb_of_compo; } + const double *getConstPointer() const { return _pt; } + double *getPointer() { return _pt; } + private: + double *_pt; + int _nb_of_compo; + }; + class DataArrayIntIterator; class DataArrayInt : public DataArray diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index ca14dcfab..1c3aee835 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -165,6 +165,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayInt::__mod__; %newobject ParaMEDMEM::DataArrayInt::__rmod__; %newobject ParaMEDMEM::DataArrayDouble::New; +%newobject ParaMEDMEM::DataArrayDouble::__iter__; %newobject ParaMEDMEM::DataArrayDouble::convertToIntArr; %newobject ParaMEDMEM::DataArrayDouble::deepCpy; %newobject ParaMEDMEM::DataArrayDouble::performCpy; @@ -288,6 +289,9 @@ using namespace INTERP_KERNEL; %ignore ParaMEDMEM::DataArrayIntIterator::nextt; %ignore ParaMEDMEM::DataArrayIntTuple::next; %ignore ParaMEDMEM::DataArrayIntTuple::repr; +%ignore ParaMEDMEM::DataArrayDoubleIterator::nextt; +%ignore ParaMEDMEM::DataArrayDoubleTuple::next; +%ignore ParaMEDMEM::DataArrayDoubleTuple::repr; %nodefaultctor; @@ -1331,6 +1335,244 @@ namespace ParaMEDMEM } } +%extend ParaMEDMEM::DataArrayDoubleIterator +{ + PyObject *next() + { + DataArrayDoubleTuple *ret=self->nextt(); + if(ret) + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0); + else + { + PyErr_SetString(PyExc_StopIteration,"No more data."); + return 0; + } + } +} + +%extend ParaMEDMEM::DataArrayDoubleTuple +{ + std::string __str__() const + { + return self->repr(); + } + + PyObject *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception) + { + int sw; + int singleVal; + std::vector multiVal; + std::pair > slic; + ParaMEDMEM::DataArrayInt *daIntTyypp=0; + const double *pt=self->getConstPointer(); + int nbc=self->getNumberOfCompo(); + convertObjToPossibleCpp2(obj,nbc,sw,singleVal,multiVal,slic,daIntTyypp); + switch(sw) + { + case 1: + { + if(singleVal>=nbc) + { + std::ostringstream oss; + oss << "Requesting for id " << singleVal << " having only " << nbc << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(singleVal>=0) + return PyFloat_FromDouble(pt[singleVal]); + else + { + if(nbc+singleVal>0) + return PyFloat_FromDouble(pt[nbc+singleVal]); + else + { + std::ostringstream oss; + oss << "Requesting for id " << singleVal << " having only " << nbc << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + } + case 2: + { + PyObject *t=PyTuple_New(multiVal.size()); + for(int j=0;j<(int)multiVal.size();j++) + { + int cid=multiVal[j]; + if(cid>=nbc) + { + std::ostringstream oss; + oss << "Requesting for id #" << cid << " having only " << nbc << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + PyTuple_SetItem(t,j,PyFloat_FromDouble(pt[cid])); + } + return t; + } + case 3: + { + int sz=DataArray::GetNumberOfItemGivenBES(slic.first,slic.second.first,slic.second.second,""); + PyObject *t=PyTuple_New(sz); + for(int j=0;j multiValV; + ParaMEDMEM::DataArrayDoubleTuple *daIntTyyppV=0; + int nbc=self->getNumberOfCompo(); + convertObjToPossibleCpp44(value,sw1,singleValV,multiValV,daIntTyyppV); + int singleVal; + std::vector multiVal; + std::pair > slic; + ParaMEDMEM::DataArrayInt *daIntTyypp=0; + double *pt=self->getPointer(); + convertObjToPossibleCpp2(obj,nbc,sw2,singleVal,multiVal,slic,daIntTyypp); + switch(sw2) + { + case 1: + { + if(singleVal>=nbc) + { + std::ostringstream oss; + oss << "Requesting for setting id # " << singleVal << " having only " << nbc << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + switch(sw1) + { + case 1: + { + pt[singleVal]=singleValV; + return self; + } + case 2: + { + if(multiValV.size()!=1) + { + std::ostringstream oss; + oss << "Requesting for setting id # " << singleVal << " with a list or tuple with size != 1 ! "; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + pt[singleVal]=multiValV[0]; + return self; + } + case 3: + { + pt[singleVal]=daIntTyyppV->getConstPointer()[0]; + return self; + } + default: + throw INTERP_KERNEL::Exception(msg); + } + } + case 2: + { + switch(sw1) + { + case 1: + { + for(std::vector::const_iterator it=multiVal.begin();it!=multiVal.end();it++) + { + if(*it>=nbc) + { + std::ostringstream oss; + oss << "Requesting for setting id # " << *it << " having only " << nbc << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + pt[*it]=singleValV; + } + return self; + } + case 2: + { + if(multiVal.size()!=multiValV.size()) + { + std::ostringstream oss; + oss << "Mismatch length of during assignment : " << multiValV.size() << " != " << multiVal.size() << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + for(int i=0;i<(int)multiVal.size();i++) + { + int pos=multiVal[i]; + if(pos>=nbc) + { + std::ostringstream oss; + oss << "Requesting for setting id # " << pos << " having only " << nbc << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + pt[multiVal[i]]=multiValV[i]; + } + return self; + } + case 3: + { + const double *ptV=daIntTyyppV->getConstPointer(); + if(nbc>daIntTyyppV->getNumberOfCompo()) + { + std::ostringstream oss; + oss << "Mismatch length of during assignment : " << nbc << " != " << daIntTyyppV->getNumberOfCompo() << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + std::copy(ptV,ptV+nbc,pt); + return self; + } + default: + throw INTERP_KERNEL::Exception(msg); + } + } + case 3: + { + int sz=DataArray::GetNumberOfItemGivenBES(slic.first,slic.second.first,slic.second.second,""); + switch(sw1) + { + case 1: + { + for(int j=0;jgetConstPointer(); + if(sz>daIntTyyppV->getNumberOfCompo()) + { + std::ostringstream oss; + oss << "Mismatch length of during assignment : " << nbc << " != " << daIntTyyppV->getNumberOfCompo() << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + for(int j=0;jrepr(); } + DataArrayDoubleIterator *__iter__() + { + return self->iterator(); + } + void setValues(PyObject *li, int nbOfTuples, int nbOfElsPerTuple) throw(INTERP_KERNEL::Exception) { double *tmp=new double[nbOfTuples*nbOfElsPerTuple]; @@ -2542,7 +2789,7 @@ namespace ParaMEDMEM oss << "Mismatch length of during assignment : " << multiValV.size() << " != " << multiVal.size() << " !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - for(int i=0;i=nbc) @@ -2584,7 +2831,7 @@ namespace ParaMEDMEM } case 2: { - if(sz!=multiValV.size()) + if(sz!=(int)multiValV.size()) { std::ostringstream oss; oss << "Mismatch length of during assignment : " << multiValV.size() << " != " << sz << " !"; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 2e9758664..cd37332c0 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -6338,6 +6338,65 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual([2, 3, 7, 5, 6, 7, 8, 9, 7, 11, 12, 7],da.getValues()) pass + def testSwigDataArrayDoubleIterator1(self): + da=DataArrayDouble.New() + da.alloc(12,1) + da.iota(2) + da.rearrange(3) + # __getitem__ testing + li=[] + for it in da: + li+=it[1:] + pass + self.assertEqual([3, 4, 6, 7, 9, 10, 12, 13],li) + li=[] + for it in da: + li+=[it[-1]] + pass + self.assertEqual([4, 7, 10, 13],li) + li=[] + for it in da: + li+=it[[2,1,0]] + pass + self.assertEqual([4, 3, 2, 7, 6, 5, 10, 9, 8, 13, 12, 11],li) + # __setitem__ testing + da3=da.deepCpy() + da2=DataArrayDouble.New() + da2.alloc(12,1) + da2.iota(2002) + da2.rearrange(3) + it2=da2.__iter__() + i=0 + for it in da: + pt=it2.next() + it[:]=pt + pass + self.assertTrue(da.isEqual(da2,1e-12)) + da=da3 + da3=da.deepCpy() + # + for it in da: + it[:]=5 + pass + da.rearrange(1) + self.assertTrue(da.isUniform(5,1e-12)) + da=da3 + da3=da.deepCpy() + # + for it in da: + it[:]=[8,9,12] + pass + self.assertEqual([8, 9, 12, 8, 9, 12, 8, 9, 12, 8, 9, 12],da.getValues()) + da=da3 + da3=da.deepCpy() + # + for it in da: + it[2]=[7] + pass + self.assertEqual([2, 3, 7, 5, 6, 7, 8, 9, 7, 11, 12, 7],da.getValues()) + pass + pass + def testDAIAggregateMulti1(self): a=DataArrayInt.New() a.setValues(range(4),2,2) diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index cb8c39340..b077fdda4 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -712,6 +712,80 @@ static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, s sw=3; } +/*! + * if python double -> cpp double sw=1 + * if python int -> cpp double sw=1 + * if python list[double] -> cpp vector sw=2 + * if python list[int] -> cpp vector sw=2 + * if python tuple[double] -> cpp vector sw=2 + * if python tuple[int] -> cpp vector sw=2 + * if python DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3 + * + * switch between (int,vector,DataArrayInt) + */ +static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, std::vector& stdvecTyypp, ParaMEDMEM::DataArrayDoubleTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception) +{ + sw=-1; + if(PyFloat_Check(value)) + { + iTyypp=PyFloat_AS_DOUBLE(value); + sw=1; + return; + } + if(PyInt_Check(value)) + { + iTyypp=(double)PyInt_AS_LONG(value); + sw=1; + return; + } + if(PyTuple_Check(value)) + { + int size=PyTuple_Size(value); + stdvecTyypp.resize(size); + for(int i=0;i(argp); + sw=3; +} + /*! * if python int -> cpp int sw=1 * if python list[int] -> cpp vector sw=2 -- 2.39.2