From d1699222684b48fc6850179dabb6c44eec3339c7 Mon Sep 17 00:00:00 2001 From: ageay Date: Mon, 23 Apr 2012 07:47:50 +0000 Subject: [PATCH] Some new functionnalities with arrays in python. --- src/MEDCoupling/MEDCouplingMemArray.cxx | 42 ++++++++++++++- src/MEDCoupling/MEDCouplingMemArray.hxx | 10 ++-- src/MEDCoupling/MEDCouplingMemArray.txx | 8 ++- src/MEDCoupling_Swig/MEDCoupling.i | 70 +++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 8 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index f40d2d4ef..4d433ca42 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -423,8 +423,10 @@ void DataArrayDouble::allocIfNecessary(int nbOfTuple, int nbOfCompo) alloc(nbOfTuple,nbOfCompo); } -void DataArrayDouble::alloc(int nbOfTuple, int nbOfCompo) +void DataArrayDouble::alloc(int nbOfTuple, int nbOfCompo) throw(INTERP_KERNEL::Exception) { + if(nbOfTuple<0 || nbOfCompo<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::alloc : request for negative length of data !"); _nb_of_tuples=nbOfTuple; _info_on_compo.resize(nbOfCompo); _mem.alloc(nbOfCompo*_nb_of_tuples); @@ -1901,6 +1903,23 @@ void DataArrayDouble::applyInv(double numerator) throw(INTERP_KERNEL::Exception) declareAsNew(); } +/*! + * This method returns a newly allocated array containing the application of negate on \b this. + * This method throws an INTERP_KERNEL::Exception if \b this is not allocated. + */ +DataArrayDouble *DataArrayDouble::negate() const throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + DataArrayDouble *newArr=DataArrayDouble::New(); + int nbOfTuples=getNumberOfTuples(); + int nbOfComp=getNumberOfComponents(); + newArr->alloc(nbOfTuples,nbOfComp); + const double *cptr=getConstPointer(); + std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate()); + newArr->copyStringInfoFrom(*this); + return newArr; +} + DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, FunctionToEvaluate func) const throw(INTERP_KERNEL::Exception) { checkAllocated(); @@ -2810,8 +2829,10 @@ void DataArrayInt::allocIfNecessary(int nbOfTuple, int nbOfCompo) alloc(nbOfTuple,nbOfCompo); } -void DataArrayInt::alloc(int nbOfTuple, int nbOfCompo) +void DataArrayInt::alloc(int nbOfTuple, int nbOfCompo) throw(INTERP_KERNEL::Exception) { + if(nbOfTuple<0 || nbOfCompo<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::alloc : request for negative length of data !"); _nb_of_tuples=nbOfTuple; _info_on_compo.resize(nbOfCompo); _mem.alloc(nbOfCompo*_nb_of_tuples); @@ -4284,6 +4305,23 @@ void DataArrayInt::applyLin(int a, int b) throw(INTERP_KERNEL::Exception) declareAsNew(); } +/*! + * This method returns a newly allocated array containing the application of negate on \b this. + * This method throws an INTERP_KERNEL::Exception if \b this is not allocated. + */ +DataArrayInt *DataArrayInt::negate() const throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + DataArrayInt *newArr=DataArrayInt::New(); + int nbOfTuples=getNumberOfTuples(); + int nbOfComp=getNumberOfComponents(); + newArr->alloc(nbOfTuples,nbOfComp); + const int *cptr=getConstPointer(); + std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate()); + newArr->copyStringInfoFrom(*this); + return newArr; +} + /*! * This method applies the operation 'numerator/x' for each element 'x' in 'this'. * If there is a value in 'this' exactly equal to 0. an exception is thrown. diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 4693f2d65..ac6a4be40 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -70,8 +70,8 @@ namespace ParaMEDMEM T *toNoInterlace(int nbOfComp) const; void sort(); void reverse(); - void alloc(int nbOfElements); - void reAlloc(int newNbOfElements); + void alloc(int nbOfElements) throw(INTERP_KERNEL::Exception); + void reAlloc(int newNbOfElements) throw(INTERP_KERNEL::Exception); void useArray(const T *array, bool ownership, DeallocType type, int nbOfElem); void writeOnPlace(int id, T element0, const T *others, int sizeOfOthers); ~MemArray() { destroy(); } @@ -143,7 +143,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayDouble *deepCpy() const; MEDCOUPLING_EXPORT DataArrayDouble *performCpy(bool deepCpy) const; MEDCOUPLING_EXPORT void cpyFrom(const DataArrayDouble& other) throw(INTERP_KERNEL::Exception); - MEDCOUPLING_EXPORT void alloc(int nbOfTuple, int nbOfCompo); + MEDCOUPLING_EXPORT void alloc(int nbOfTuple, int nbOfCompo) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void allocIfNecessary(int nbOfTuple, int nbOfCompo); MEDCOUPLING_EXPORT void fillWithZero() throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void fillWithValue(double val) throw(INTERP_KERNEL::Exception); @@ -236,6 +236,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void applyLin(double a, double b, int compoId) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyLin(double a, double b) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyInv(double numerator) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayDouble *negate() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *applyFunc(int nbOfComp, FunctionToEvaluate func) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *applyFunc(int nbOfComp, const char *func) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *applyFunc(const char *func) const throw(INTERP_KERNEL::Exception); @@ -322,7 +323,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayInt *deepCpy() const; MEDCOUPLING_EXPORT DataArrayInt *performCpy(bool deepCpy) const; MEDCOUPLING_EXPORT void cpyFrom(const DataArrayInt& other) throw(INTERP_KERNEL::Exception); - MEDCOUPLING_EXPORT void alloc(int nbOfTuple, int nbOfCompo); + MEDCOUPLING_EXPORT void alloc(int nbOfTuple, int nbOfCompo) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void allocIfNecessary(int nbOfTuple, int nbOfCompo); MEDCOUPLING_EXPORT bool isEqual(const DataArrayInt& other) const; MEDCOUPLING_EXPORT bool isEqualWithoutConsideringStr(const DataArrayInt& other) const; @@ -407,6 +408,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void applyLin(int a, int b, int compoId) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyLin(int a, int b) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyInv(int numerator) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayInt *negate() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyDivideBy(int val) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyModulus(int val) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyRModulus(int val) throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling/MEDCouplingMemArray.txx b/src/MEDCoupling/MEDCouplingMemArray.txx index 7dd57679f..0040f95b1 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.txx +++ b/src/MEDCoupling/MEDCouplingMemArray.txx @@ -224,9 +224,11 @@ namespace ParaMEDMEM } template - void MemArray::alloc(int nbOfElements) + void MemArray::alloc(int nbOfElements) throw(INTERP_KERNEL::Exception) { destroy(); + if(nbOfElements<0) + throw INTERP_KERNEL::Exception("MemArray::alloc : request for negative length of data !"); _nb_of_elem=nbOfElements; _pointer.setInternal(new T[_nb_of_elem]); _ownership=true; @@ -234,8 +236,10 @@ namespace ParaMEDMEM } template - void MemArray::reAlloc(int newNbOfElements) + void MemArray::reAlloc(int newNbOfElements) throw(INTERP_KERNEL::Exception) { + if(newNbOfElements<0) + throw INTERP_KERNEL::Exception("MemArray::reAlloc : request for negative length of data !"); T *pointer=new T[newNbOfElements]; std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min(_nb_of_elem,newNbOfElements),pointer); if(_ownership) diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index f5db9b5d7..29e624b8a 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -137,6 +137,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayInt::getIdsNotEqual; %newobject ParaMEDMEM::DataArrayInt::getIdsEqualList; %newobject ParaMEDMEM::DataArrayInt::getIdsNotEqualList; +%newobject ParaMEDMEM::DataArrayInt::negate; %newobject ParaMEDMEM::DataArrayInt::Aggregate; %newobject ParaMEDMEM::DataArrayInt::Meld; %newobject ParaMEDMEM::DataArrayInt::Add; @@ -156,6 +157,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayInt::buildPermutationArr; %newobject ParaMEDMEM::DataArrayInt::buildPermArrPerLevel; %newobject ParaMEDMEM::DataArrayInt::__getitem__; +%newobject ParaMEDMEM::DataArrayInt::__neg__; %newobject ParaMEDMEM::DataArrayInt::__add__; %newobject ParaMEDMEM::DataArrayInt::__radd__; %newobject ParaMEDMEM::DataArrayInt::__sub__; @@ -188,6 +190,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayDouble::selectByTupleIdSafe; %newobject ParaMEDMEM::DataArrayDouble::selectByTupleId2; %newobject ParaMEDMEM::DataArrayDouble::selectByTupleRanges; +%newobject ParaMEDMEM::DataArrayDouble::negate; %newobject ParaMEDMEM::DataArrayDouble::applyFunc; %newobject ParaMEDMEM::DataArrayDouble::applyFunc2; %newobject ParaMEDMEM::DataArrayDouble::applyFunc3; @@ -210,6 +213,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayDouble::fromSpherToCart; %newobject ParaMEDMEM::DataArrayDouble::getDifferentValues; %newobject ParaMEDMEM::DataArrayDouble::__getitem__; +%newobject ParaMEDMEM::DataArrayDouble::__neg__; %newobject ParaMEDMEM::DataArrayDouble::__add__; %newobject ParaMEDMEM::DataArrayDouble::__radd__; %newobject ParaMEDMEM::DataArrayDouble::__sub__; @@ -1895,6 +1899,34 @@ namespace ParaMEDMEM %extend ParaMEDMEM::DataArrayDouble { + static DataArrayDouble *New(int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + ret->alloc(nbOfTuples,nbOfComp); + ret->incrRef(); + return ret; + } + + static DataArrayDouble *New(PyObject *li, int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception) + { + if(nbOfTuples<0 || nbOfComp<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::New(PyList,nbOfTuples,nbOfComponents) : should be a positive set of allocated memory !"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + double *tmp=new double[nbOfTuples*nbOfComp]; + try + { + fillArrayWithPyListDbl(li,tmp,nbOfTuples*nbOfComp,0.); + } + catch(INTERP_KERNEL::Exception& e) + { + delete [] tmp; + throw e; + } + ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfComp); + ret->incrRef(); + return ret; + } + std::string __str__() const { return self->repr(); @@ -2709,6 +2741,11 @@ namespace ParaMEDMEM return self; } + DataArrayDouble *__neg__() const throw(INTERP_KERNEL::Exception) + { + return self->negate(); + } + DataArrayDouble *__add__(PyObject *obj) throw(INTERP_KERNEL::Exception) { const char msg[]="Unexpected situation in __add__ !"; @@ -3387,6 +3424,34 @@ namespace ParaMEDMEM %extend ParaMEDMEM::DataArrayInt { + static DataArrayInt *New(int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuples,nbOfComp); + ret->incrRef(); + return ret; + } + + static DataArrayInt *New(PyObject *li, int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception) + { + if(nbOfTuples<0 || nbOfComp<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::New(PyList,nbOfTuples,nbOfComponents) : should be a positive set of allocated memory !"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + int *tmp=new int[nbOfTuples*nbOfComp]; + try + { + fillArrayWithPyListInt(li,tmp,nbOfTuples*nbOfComp,0); + } + catch(INTERP_KERNEL::Exception& e) + { + delete [] tmp; + throw e; + } + ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfComp); + ret->incrRef(); + return ret; + } + std::string __str__() const { return self->repr(); @@ -4336,6 +4401,11 @@ namespace ParaMEDMEM return self; } + DataArrayInt *__neg__() const throw(INTERP_KERNEL::Exception) + { + return self->negate(); + } + DataArrayInt *__add__(PyObject *obj) throw(INTERP_KERNEL::Exception) { const char msg[]="Unexpected situation in __add__ !"; -- 2.39.2