From 291c9fb90a83f5f0fd90fb2f6543efaf31395869 Mon Sep 17 00:00:00 2001 From: ageay Date: Wed, 1 Dec 2010 11:25:31 +0000 Subject: [PATCH] *** empty log message *** --- src/MEDCoupling/MEDCouplingMemArray.cxx | 64 ++++++++++++++++++---- src/MEDCoupling/MEDCouplingMemArray.hxx | 24 ++++---- src/MEDCoupling_Swig/libMEDCoupling_Swig.i | 18 ++++++ 3 files changed, 85 insertions(+), 21 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index cf6aa2a1d..a825b9aa8 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -120,6 +120,17 @@ DataArrayDouble *DataArrayDouble::New() return new DataArrayDouble; } +bool DataArrayDouble::isAllocated() const +{ + return getConstPointer()!=0; +} + +void DataArrayDouble::checkAllocated() const throw(INTERP_KERNEL::Exception) +{ + if(!isAllocated()) + throw INTERP_KERNEL::Exception("DataArrayDouble::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !"); +} + DataArrayDouble *DataArrayDouble::deepCopy() const { return new DataArrayDouble(*this); @@ -237,8 +248,9 @@ bool DataArrayDouble::isEqualWithoutConsideringStr(const DataArrayDouble& other, return _mem.isEqual(other._mem,prec); } -void DataArrayDouble::reAlloc(int nbOfTuples) +void DataArrayDouble::reAlloc(int nbOfTuples) throw(INTERP_KERNEL::Exception) { + checkAllocated(); _mem.reAlloc(_info_on_compo.size()*nbOfTuples); _nb_of_tuples=nbOfTuples; declareAsNew(); @@ -426,6 +438,7 @@ DataArrayDouble *DataArrayDouble::substr(int tupleIdBg, int tupleIdEnd) const th */ DataArrayDouble *DataArrayDouble::changeNbOfComponents(int newNbOfComp, double dftValue) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); DataArrayDouble *ret=DataArrayDouble::New(); ret->alloc(getNumberOfTuples(),newNbOfComp); const double *oldc=getConstPointer(); @@ -450,6 +463,7 @@ DataArrayDouble *DataArrayDouble::changeNbOfComponents(int newNbOfComp, double d DataArrayDouble *DataArrayDouble::keepSelectedComponents(const std::vector& compoIds) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); MEDCouplingAutoRefCountObjectPtr ret(DataArrayDouble::New()); int newNbOfCompo=compoIds.size(); int oldNbOfCompo=getNumberOfComponents(); @@ -563,8 +577,9 @@ double DataArrayDouble::getAverageValue() const throw(INTERP_KERNEL::Exception) return ret/nbOfTuples; } -void DataArrayDouble::accumulate(double *res) const +void DataArrayDouble::accumulate(double *res) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); const double *ptr=getConstPointer(); int nbTuple=getNumberOfTuples(); int nbComps=getNumberOfComponents(); @@ -573,8 +588,9 @@ void DataArrayDouble::accumulate(double *res) const std::transform(ptr+i*nbComps,ptr+(i+1)*nbComps,res,res,std::plus()); } -double DataArrayDouble::accumulate(int compId) const +double DataArrayDouble::accumulate(int compId) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); const double *ptr=getConstPointer(); int nbTuple=getNumberOfTuples(); int nbComps=getNumberOfComponents(); @@ -586,7 +602,7 @@ double DataArrayDouble::accumulate(int compId) const return ret; } -DataArrayDouble *DataArrayDouble::fromPolarToCart() const +DataArrayDouble *DataArrayDouble::fromPolarToCart() const throw(INTERP_KERNEL::Exception) { int nbOfComp=getNumberOfComponents(); if(nbOfComp!=2) @@ -604,7 +620,7 @@ DataArrayDouble *DataArrayDouble::fromPolarToCart() const return ret; } -DataArrayDouble *DataArrayDouble::fromCylToCart() const +DataArrayDouble *DataArrayDouble::fromCylToCart() const throw(INTERP_KERNEL::Exception) { int nbOfComp=getNumberOfComponents(); if(nbOfComp!=3) @@ -624,7 +640,7 @@ DataArrayDouble *DataArrayDouble::fromCylToCart() const return ret; } -DataArrayDouble *DataArrayDouble::fromSpherToCart() const +DataArrayDouble *DataArrayDouble::fromSpherToCart() const throw(INTERP_KERNEL::Exception) { int nbOfComp=getNumberOfComponents(); if(nbOfComp!=3) @@ -660,6 +676,7 @@ DataArrayDouble *DataArrayDouble::doublyContractedProduct() const throw(INTERP_K DataArrayDouble *DataArrayDouble::determinant() const throw(INTERP_KERNEL::Exception) { + checkAllocated(); DataArrayDouble *ret=DataArrayDouble::New(); int nbOfTuple=getNumberOfTuples(); ret->alloc(nbOfTuple,1); @@ -814,6 +831,7 @@ DataArrayDouble *DataArrayDouble::deviator() const throw(INTERP_KERNEL::Exceptio DataArrayDouble *DataArrayDouble::magnitude() const throw(INTERP_KERNEL::Exception) { + checkAllocated(); int nbOfComp=getNumberOfComponents(); DataArrayDouble *ret=DataArrayDouble::New(); int nbOfTuple=getNumberOfTuples(); @@ -832,6 +850,7 @@ DataArrayDouble *DataArrayDouble::magnitude() const throw(INTERP_KERNEL::Excepti DataArrayDouble *DataArrayDouble::maxPerTuple() const throw(INTERP_KERNEL::Exception) { + checkAllocated(); int nbOfComp=getNumberOfComponents(); DataArrayDouble *ret=DataArrayDouble::New(); int nbOfTuple=getNumberOfTuples(); @@ -845,6 +864,7 @@ DataArrayDouble *DataArrayDouble::maxPerTuple() const throw(INTERP_KERNEL::Excep void DataArrayDouble::sortPerTuple(bool asc) throw(INTERP_KERNEL::Exception) { + checkAllocated(); double *pt=getPointer(); int nbOfTuple=getNumberOfTuples(); int nbOfComp=getNumberOfComponents(); @@ -857,8 +877,9 @@ void DataArrayDouble::sortPerTuple(bool asc) throw(INTERP_KERNEL::Exception) declareAsNew(); } -void DataArrayDouble::applyLin(double a, double b, int compoId) +void DataArrayDouble::applyLin(double a, double b, int compoId) throw(INTERP_KERNEL::Exception) { + checkAllocated(); double *ptr=getPointer()+compoId; int nbOfComp=getNumberOfComponents(); int nbOfTuple=getNumberOfTuples(); @@ -869,6 +890,7 @@ void DataArrayDouble::applyLin(double a, double b, int compoId) DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, FunctionToEvaluate func) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); DataArrayDouble *newArr=DataArrayDouble::New(); int nbOfTuples=getNumberOfTuples(); int oldNbOfComp=getNumberOfComponents(); @@ -891,6 +913,7 @@ DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, FunctionToEvaluate fun DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, const char *func) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); INTERP_KERNEL::ExprParser expr(func); expr.parse(); std::set vars; @@ -931,6 +954,7 @@ DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, const char *func) cons DataArrayDouble *DataArrayDouble::applyFunc(const char *func) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); INTERP_KERNEL::ExprParser expr(func); expr.parse(); expr.prepareExprEvaluationVec(); @@ -959,8 +983,9 @@ DataArrayDouble *DataArrayDouble::applyFunc(const char *func) const throw(INTERP return newArr; } -void DataArrayDouble::applyFuncFast32(const char *func) +void DataArrayDouble::applyFuncFast32(const char *func) throw(INTERP_KERNEL::Exception) { + checkAllocated(); INTERP_KERNEL::ExprParser expr(func); expr.parse(); char *funcStr=expr.compileX86(); @@ -975,8 +1000,9 @@ void DataArrayDouble::applyFuncFast32(const char *func) declareAsNew(); } -void DataArrayDouble::applyFuncFast64(const char *func) +void DataArrayDouble::applyFuncFast64(const char *func) throw(INTERP_KERNEL::Exception) { + checkAllocated(); INTERP_KERNEL::ExprParser expr(func); expr.parse(); char *funcStr=expr.compileX86_64(); @@ -994,7 +1020,7 @@ void DataArrayDouble::applyFuncFast64(const char *func) DataArrayInt *DataArrayDouble::getIdsInRange(double vmin, double vmax) const throw(INTERP_KERNEL::Exception) { if(getNumberOfComponents()!=1) - throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsInRange : the default array must have only one component !"); + throw INTERP_KERNEL::Exception("DataArrayDouble::getIdsInRange : this must have exactly one component !"); const double *cptr=getConstPointer(); std::vector res; int nbOfTuples=getNumberOfTuples(); @@ -1024,6 +1050,8 @@ DataArrayDouble *DataArrayDouble::aggregate(const DataArrayDouble *a1, const Dat DataArrayDouble *DataArrayDouble::dot(const DataArrayDouble *a1, const DataArrayDouble *a2) throw(INTERP_KERNEL::Exception) { + a1->checkAllocated(); + a2->checkAllocated(); int nbOfComp=a1->getNumberOfComponents(); if(nbOfComp!=a2->getNumberOfComponents()) throw INTERP_KERNEL::Exception("Nb of components mismatch for array dot !"); @@ -1272,6 +1300,17 @@ DataArrayInt *DataArrayInt::New() return new DataArrayInt; } +bool DataArrayInt::isAllocated() const +{ + return getConstPointer()!=0; +} + +void DataArrayInt::checkAllocated() const throw(INTERP_KERNEL::Exception) +{ + if(!isAllocated()) + throw INTERP_KERNEL::Exception("DataArrayInt::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !"); +} + DataArrayInt *DataArrayInt::deepCopy() const { return new DataArrayInt(*this); @@ -1604,6 +1643,7 @@ DataArrayInt *DataArrayInt::substr(int tupleIdBg, int tupleIdEnd) const throw(IN */ DataArrayInt *DataArrayInt::changeNbOfComponents(int newNbOfComp, int dftValue) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); DataArrayInt *ret=DataArrayInt::New(); ret->alloc(getNumberOfTuples(),newNbOfComp); const int *oldc=getConstPointer(); @@ -1626,8 +1666,9 @@ DataArrayInt *DataArrayInt::changeNbOfComponents(int newNbOfComp, int dftValue) return ret; } -void DataArrayInt::reAlloc(int nbOfTuples) +void DataArrayInt::reAlloc(int nbOfTuples) throw(INTERP_KERNEL::Exception) { + checkAllocated(); _mem.reAlloc(_info_on_compo.size()*nbOfTuples); _nb_of_tuples=nbOfTuples; declareAsNew(); @@ -1635,6 +1676,7 @@ void DataArrayInt::reAlloc(int nbOfTuples) DataArrayInt *DataArrayInt::keepSelectedComponents(const std::vector& compoIds) const throw(INTERP_KERNEL::Exception) { + checkAllocated(); MEDCouplingAutoRefCountObjectPtr ret(DataArrayInt::New()); int newNbOfCompo=compoIds.size(); int oldNbOfCompo=getNumberOfComponents(); diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index f38ea86c2..06c4091db 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -116,6 +116,8 @@ namespace ParaMEDMEM { public: MEDCOUPLING_EXPORT static DataArrayDouble *New(); + MEDCOUPLING_EXPORT bool isAllocated() const; + MEDCOUPLING_EXPORT void checkAllocated() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *deepCopy() const; MEDCOUPLING_EXPORT DataArrayDouble *performCpy(bool deepCpy) const; MEDCOUPLING_EXPORT void alloc(int nbOfTuple, int nbOfCompo); @@ -132,7 +134,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT bool isEqual(const DataArrayDouble& other, double prec) const; MEDCOUPLING_EXPORT bool isEqualWithoutConsideringStr(const DataArrayDouble& other, double prec) const; //!alloc or useArray should have been called before. - MEDCOUPLING_EXPORT void reAlloc(int nbOfTuples); + MEDCOUPLING_EXPORT void reAlloc(int nbOfTuples) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *convertToIntArr() const; MEDCOUPLING_EXPORT DataArrayDouble *fromNoInterlace() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *toNoInterlace() const throw(INTERP_KERNEL::Exception); @@ -160,11 +162,11 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT double getMaxValue2(DataArrayInt*& tupleIds) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT double getMinValue2(DataArrayInt*& tupleIds) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT double getAverageValue() const throw(INTERP_KERNEL::Exception); - MEDCOUPLING_EXPORT void accumulate(double *res) const; - MEDCOUPLING_EXPORT double accumulate(int compId) const; - MEDCOUPLING_EXPORT DataArrayDouble *fromPolarToCart() const; - MEDCOUPLING_EXPORT DataArrayDouble *fromCylToCart() const; - MEDCOUPLING_EXPORT DataArrayDouble *fromSpherToCart() const; + MEDCOUPLING_EXPORT void accumulate(double *res) const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT double accumulate(int compId) const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayDouble *fromPolarToCart() const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayDouble *fromCylToCart() const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayDouble *fromSpherToCart() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *doublyContractedProduct() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *determinant() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *eigenValues() const throw(INTERP_KERNEL::Exception); @@ -175,12 +177,12 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayDouble *magnitude() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *maxPerTuple() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void sortPerTuple(bool asc) throw(INTERP_KERNEL::Exception); - MEDCOUPLING_EXPORT void applyLin(double a, double b, int compoId); + MEDCOUPLING_EXPORT void applyLin(double a, double b, int compoId) 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); - MEDCOUPLING_EXPORT void applyFuncFast32(const char *func); - MEDCOUPLING_EXPORT void applyFuncFast64(const char *func); + MEDCOUPLING_EXPORT void applyFuncFast32(const char *func) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT void applyFuncFast64(const char *func) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *getIdsInRange(double vmin, double vmax) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT static DataArrayDouble *aggregate(const DataArrayDouble *a1, const DataArrayDouble *a2) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT static DataArrayDouble *dot(const DataArrayDouble *a1, const DataArrayDouble *a2) throw(INTERP_KERNEL::Exception); @@ -207,6 +209,8 @@ namespace ParaMEDMEM { public: MEDCOUPLING_EXPORT static DataArrayInt *New(); + MEDCOUPLING_EXPORT bool isAllocated() const; + MEDCOUPLING_EXPORT void checkAllocated() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *deepCopy() const; MEDCOUPLING_EXPORT DataArrayInt *performCpy(bool deepCpy) const; MEDCOUPLING_EXPORT void alloc(int nbOfTuple, int nbOfCompo); @@ -224,7 +228,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayInt *invertArrayO2N2N2O(int newNbOfElem) const; MEDCOUPLING_EXPORT DataArrayInt *invertArrayN2O2O2N(int oldNbOfElem) const; //!alloc or useArray should have been called before. - MEDCOUPLING_EXPORT void reAlloc(int nbOfTuples); + MEDCOUPLING_EXPORT void reAlloc(int nbOfTuples) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *convertToDblArr() const; MEDCOUPLING_EXPORT DataArrayInt *fromNoInterlace() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayInt *toNoInterlace() const throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling_Swig/libMEDCoupling_Swig.i b/src/MEDCoupling_Swig/libMEDCoupling_Swig.i index 13d52eb7b..5d7dc1866 100644 --- a/src/MEDCoupling_Swig/libMEDCoupling_Swig.i +++ b/src/MEDCoupling_Swig/libMEDCoupling_Swig.i @@ -978,6 +978,24 @@ namespace ParaMEDMEM PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(tmp),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 )); return ret; } + + PyObject *accumulate() const throw(INTERP_KERNEL::Exception) + { + int sz=self->getNumberOfComponents(); + double *tmp=new double[sz]; + try + { + self->accumulate(tmp); + } + catch(INTERP_KERNEL::Exception& e) + { + delete [] tmp; + throw e; + } + PyObject *ret=convertDblArrToPyList(tmp,sz); + delete [] tmp; + return ret; + } DataArrayDouble *keepSelectedComponents(PyObject *li) const throw(INTERP_KERNEL::Exception) { -- 2.39.2