From: geay Date: Wed, 5 Mar 2014 11:03:57 +0000 (+0100) Subject: Behavior modification for slices in medcoupling and medloader. start==stop==length... X-Git-Tag: V7_4_0a1~24 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=539265beb92d5f8460e161b4630e2518bdff03ef;p=tools%2Fmedcoupling.git Behavior modification for slices in medcoupling and medloader. start==stop==length is now supported. Negative integers in __getitem__ are also supported. --- diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 2af096bf8..026fb199e 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -6020,13 +6020,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): else: self.assertTrue(False) pass - try: - da2=da[5:8,-2] - except InterpKernelException as e: - self.assertTrue(True) - else: - self.assertTrue(False) - pass + self.assertTrue(da[5:8,-2].isEqualWithoutConsideringStr(DataArrayInt([23,26,29]))) da2=da[5:8,:-2] self.assertEqual([22, 25, 28],da2.getValues()) try: @@ -6075,13 +6069,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): else: self.assertTrue(False) pass - try: - da2=da[5:8,-2] - except InterpKernelException as e: - self.assertTrue(True) - else: - self.assertTrue(False) - pass + self.assertTrue(da[5:8,-2].isEqualWithoutConsideringStr(DataArrayDouble([23.,26.,29.]),1e-12)) da2=da[5:8,:-2] self.assertEqual([22., 25., 28.],da2.getValues()) try: @@ -9583,13 +9571,13 @@ class MEDCouplingBasicsTest(unittest.TestCase): def testSwigGetItem3(self): da=DataArrayInt.New([4,5,6]) self.assertEqual(5,da[1]) - self.assertRaises(InterpKernelException,da.__getitem__,-1) + self.assertEqual(6,da[-1]) self.assertRaises(InterpKernelException,da.__getitem__,3) da=DataArrayInt.New([4,5,6,7,8,9],2,3) self.assertEqual(9,da[1,2]) da=DataArrayDouble.New([4.1,5.2,6.3]) self.assertAlmostEqual(5.2,da[1],12) - self.assertRaises(InterpKernelException,da.__getitem__,-1) + self.assertAlmostEqual(6.3,da[-1],12) self.assertRaises(InterpKernelException,da.__getitem__,3) da=DataArrayDouble.New([4.12,5.12,6.12,7.12,8.12,9.12],2,3) self.assertAlmostEqual(9.12,da[1,2],12) @@ -14379,6 +14367,78 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertTrue(m.getNodalConnectivityIndex().isEqual(DataArrayInt([0,13,20,27]))) pass + def testSwigExtendedSlice1(self): + d=DataArrayInt([5,6,7]) + self.assertTrue(d[2:].isEqual(DataArrayInt([7]))) + self.assertTrue(d[3:].isEqual(DataArrayInt([]))) + try: + d[4:] + except InterpKernelException as e: + self.assertTrue(True) + else: + self.assertTrue(False) + pass + d=DataArrayInt([5,6,7,8]) + self.assertEqual(d[-1],8) + self.assertEqual(d[-4],5) + try: + d[-5] + except InterpKernelException as e: + self.assertTrue(True) + else: + self.assertTrue(False) + pass + self.assertTrue(d[2::-1].isEqual(DataArrayInt([7,6,5]))) + self.assertTrue(d[0::-1].isEqual(DataArrayInt([5]))) + self.assertTrue(d[-1::-1].isEqual(DataArrayInt([8,7,6,5]))) + self.assertTrue(d[-3::-1].isEqual(DataArrayInt([6,5]))) + self.assertTrue(d[-5::-1].isEqual(DataArrayInt([]))) + try: + d[-6::-1] + except InterpKernelException as e: + self.assertTrue(True) + else: + self.assertTrue(False) + pass + d=DataArrayInt([]) + self.assertTrue(d[0:].isEqual(DataArrayInt([]))) + # + d=DataArrayDouble([5,6,7]) + self.assertTrue(d[2:].isEqual(DataArrayDouble([7]),1e-12)) + self.assertTrue(d[3:].isEqual(DataArrayDouble([]),1e-12)) + try: + d[4:] + except InterpKernelException as e: + self.assertTrue(True) + else: + self.assertTrue(False) + pass + d=DataArrayDouble([5,6,7,8]) + self.assertAlmostEqual(d[-1],8.,12) + self.assertAlmostEqual(d[-4],5.,12) + try: + d[-5] + except InterpKernelException as e: + self.assertTrue(True) + else: + self.assertTrue(False) + pass + self.assertTrue(d[2::-1].isEqual(DataArrayDouble([7,6,5]),1e-12)) + self.assertTrue(d[0::-1].isEqual(DataArrayDouble([5]),1e-12)) + self.assertTrue(d[-1::-1].isEqual(DataArrayDouble([8,7,6,5]),1e-12)) + self.assertTrue(d[-3::-1].isEqual(DataArrayDouble([6,5]),1e-12)) + self.assertTrue(d[-5::-1].isEqual(DataArrayDouble([]),1e-12)) + try: + d[-6::-1] + except InterpKernelException as e: + self.assertTrue(True) + else: + self.assertTrue(False) + pass + d=DataArrayDouble([]) + self.assertTrue(d[0:].isEqual(DataArrayDouble([]),1e-12)) + pass + def setUp(self): pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index b9d0854f2..eb4e14fcf 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -2080,8 +2080,7 @@ namespace ParaMEDMEM arrIndxIn->checkAllocated(); if(arrIndxIn->getNumberOfComponents()!=1) throw INTERP_KERNEL::Exception("ExtractFromIndexedArrays2 (wrap) : number of components of last argument must be equal to one !"); - if(PySlice_GetIndices(sliC,arrIndxIn->getNumberOfTuples(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("ExtractFromIndexedArrays2 (wrap) : Invalid slice regarding nb of elements !"); + GetIndicesOfSlice(sliC,arrIndxIn->getNumberOfTuples(),&strt,&stp,&step,"ExtractFromIndexedArrays2 (wrap) : Invalid slice regarding nb of elements !"); DataArrayInt *arrOut=0,*arrIndexOut=0; MEDCouplingUMesh::ExtractFromIndexedArrays2(strt,stp,step,arrIn,arrIndxIn,arrOut,arrIndexOut); PyObject *ret=PyTuple_New(2); diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i index 4b890dc50..46527b823 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i @@ -20,6 +20,53 @@ #include "InterpKernelAutoPtr.hxx" +/*! + * This method is an extention of PySlice_GetIndices but less + * open than PySlice_GetIndicesEx that accepts too many situations. + */ +void GetIndicesOfSlice(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, const char *msgInCaseOfFailure) +{ + int ret(PySlice_GetIndices(slice,length,start,stop,step)); + if(ret==0) + return ; + if(*step>0 && *start==*stop && length==*start) + return ; + throw INTERP_KERNEL::Exception(msgInCaseOfFailure); +} + +/*! + * This method allows to retrieve slice info from \a slice. + */ +void GetIndicesOfSliceExplicitely(PySliceObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, const char *msgInCaseOfFailure) +{ + int ret(PySlice_GetIndices(slice,std::numeric_limits::max(),start,stop,step)); + if(ret==0) + { + if(*start!=std::numeric_limits::max() && *stop!=std::numeric_limits::max()) + return ; + std::ostringstream oss; + oss << msgInCaseOfFailure << " The input slice contains some unknowns that can't be determined in static method ! The input slice must be explicit here !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + throw INTERP_KERNEL::Exception(msgInCaseOfFailure); +} + +int InterpreteNegativeInt(int val, int nbelem) +{ + if(val<0) + { + int newVal(nbelem+val); + if(newVal<0) + { + std::ostringstream oss; oss << "interpreteNegativeInt : request for negative int=" << val << " but number of elems is equal to " << nbelem << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return newVal; + } + else + return val; +} + #ifdef WITH_NUMPY #include @@ -1420,12 +1467,7 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& { Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(value); - if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0) - if(nbelem!=0 || strt!=0 || stp!=0) - { - std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } + GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); p.first=strt; p.second.first=stp; p.second.second=step; @@ -1462,6 +1504,18 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& throw INTERP_KERNEL::Exception(msg); } +/*! + * Idem than convertObjToPossibleCpp2 + */ +static void convertObjToPossibleCpp2WithNegIntInterp(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, ParaMEDMEM::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception) +{ + convertObjToPossibleCpp2(value,nbelem,sw,iTyypp,stdvecTyypp,p,daIntTyypp); + if(sw==1) + { + iTyypp=InterpreteNegativeInt(iTyypp,nbelem); + } +} + /*! * if python int -> cpp int sw=1 * if python tuple[int] -> cpp vector sw=2 @@ -1518,12 +1572,7 @@ static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& { Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(value); - if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0) - if(nbelem!=0 || strt!=0 || stp!=0) - { - std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } + GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); p.first=strt; p.second.first=stp; p.second.second=step; @@ -1653,7 +1702,7 @@ static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, { if(!PyTuple_Check(value)) { - convertObjToPossibleCpp2(value,nbTuple,sw,it,vt,pt,dt); + convertObjToPossibleCpp2WithNegIntInterp(value,nbTuple,sw,it,vt,pt,dt); return ; } else @@ -1663,9 +1712,9 @@ static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, throw INTERP_KERNEL::Exception("Unexpected nb of slice element : 1 or 2 expected !\n1st is for tuple selection, 2nd for component selection !"); PyObject *ob0=PyTuple_GetItem(value,0); int sw1,sw2; - convertObjToPossibleCpp2(ob0,nbTuple,sw1,it,vt,pt,dt); + convertObjToPossibleCpp2WithNegIntInterp(ob0,nbTuple,sw1,it,vt,pt,dt); PyObject *ob1=PyTuple_GetItem(value,1); - convertObjToPossibleCpp2(ob1,nbCompo,sw2,ic,vc,pc,dc); + convertObjToPossibleCpp2WithNegIntInterp(ob1,nbCompo,sw2,ic,vc,pc,dc); sw=4*sw2+sw1; } } diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index dbdc40bec..c17ed322b 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -408,10 +408,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("DataArray::GetSlice (wrap) : expecting a pyslice as second (first) parameter !"); Py_ssize_t strt=2,stp=2,step=2; PySliceObject *sly=reinterpret_cast(slic); - if(PySlice_GetIndices(sly,std::numeric_limits::max(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("DataArray::GetSlice (wrap) : the input slice is invalid !"); - if(strt==std::numeric_limits::max() || stp==std::numeric_limits::max()) - throw INTERP_KERNEL::Exception("DataArray::GetSlice (wrap) : the input slice contains some unknowns that can't be determined in static method ! Call DataArray::getSlice (non static) instead !"); + GetIndicesOfSliceExplicitely(sly,&strt,&stp,&step,"DataArray::GetSlice (wrap) : the input slice is invalid !"); int a,b; DataArray::GetSlice(strt,stp,step,sliceId,nbOfSlices,a,b); return PySlice_New(PyInt_FromLong(a),PyInt_FromLong(b),PyInt_FromLong(step)); @@ -423,8 +420,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("DataArray::getSlice (wrap) : expecting a pyslice as second (first) parameter !"); Py_ssize_t strt=2,stp=2,step=2; PySliceObject *sly=reinterpret_cast(slic); - if(PySlice_GetIndices(sly,self->getNumberOfTuples(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("DataArray::getSlice (wrap) : the input slice is invalid !"); + GetIndicesOfSlice(sly,self->getNumberOfTuples(),&strt,&stp,&step,"DataArray::getSlice (wrap) : the input slice is invalid !"); int a,b; DataArray::GetSlice(strt,stp,step,sliceId,nbOfSlices,a,b); return PySlice_New(PyInt_FromLong(a),PyInt_FromLong(b),PyInt_FromLong(step)); @@ -436,10 +432,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES (wrap) : expecting a pyslice as second (first) parameter !"); Py_ssize_t strt=2,stp=2,step=2; PySliceObject *sly=reinterpret_cast(slic); - if(PySlice_GetIndices(sly,std::numeric_limits::max(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES (wrap) : the input slice is invalid !"); - if(strt==std::numeric_limits::max() || stp==std::numeric_limits::max()) - throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES (wrap) : the input slice contains some unknowns that can't be determined in static method !"); + GetIndicesOfSliceExplicitely(sly,&strt,&stp,&step,"DataArray::GetNumberOfItemGivenBES (wrap) : the input slice is invalid !"); return DataArray::GetNumberOfItemGivenBES(strt,stp,step,""); } @@ -449,10 +442,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBESRelative (wrap) : expecting a pyslice as second (first) parameter !"); Py_ssize_t strt=2,stp=2,step=2; PySliceObject *sly=reinterpret_cast(slic); - if(PySlice_GetIndices(sly,std::numeric_limits::max(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBESRelative (wrap) : the input slice is invalid !"); - if(strt==std::numeric_limits::max() || stp==std::numeric_limits::max()) - throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBESRelative (wrap) : the input slice contains some unknowns that can't be determined in static method !"); + GetIndicesOfSliceExplicitely(sly,&strt,&stp,&step,"DataArray::GetNumberOfItemGivenBESRelative (wrap) : the input slice is invalid !"); return DataArray::GetNumberOfItemGivenBESRelative(strt,stp,step,""); } @@ -469,8 +459,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("DataArray::getNumberOfItemGivenBES (wrap) : expecting a pyslice as second (first) parameter !"); Py_ssize_t strt=2,stp=2,step=2; PySliceObject *sly=reinterpret_cast(slic); - if(PySlice_GetIndices(sly,self->getNumberOfTuples(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("DataArray::getNumberOfItemGivenBES (wrap) : the input slice is invalid !"); + GetIndicesOfSlice(sly,self->getNumberOfTuples(),&strt,&stp,&step,"DataArray::getNumberOfItemGivenBES (wrap) : the input slice is invalid !"); return DataArray::GetNumberOfItemGivenBES(strt,stp,step,""); } @@ -480,8 +469,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("DataArray::getNumberOfItemGivenBESRelative (wrap) : expecting a pyslice as second (first) parameter !"); Py_ssize_t strt=2,stp=2,step=2; PySliceObject *sly=reinterpret_cast(slic); - if(PySlice_GetIndices(sly,self->getNumberOfTuples(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("DataArray::getNumberOfItemGivenBESRelative (wrap) : the input slice is invalid !"); + GetIndicesOfSlice(sly,self->getNumberOfTuples(),&strt,&stp,&step,"DataArray::getNumberOfItemGivenBESRelative (wrap) : the input slice is invalid !"); return DataArray::GetNumberOfItemGivenBESRelative(strt,stp,step,""); } } @@ -2271,7 +2259,7 @@ namespace ParaMEDMEM ParaMEDMEM::DataArrayInt *daIntTyypp=0; const double *pt=self->getConstPointer(); int nbc=self->getNumberOfCompo(); - convertObjToPossibleCpp2(obj,nbc,sw,singleVal,multiVal,slic,daIntTyypp); + convertObjToPossibleCpp2WithNegIntInterp(obj,nbc,sw,singleVal,multiVal,slic,daIntTyypp); switch(sw) { case 1: @@ -2340,7 +2328,7 @@ namespace ParaMEDMEM std::pair > slic; ParaMEDMEM::DataArrayInt *daIntTyypp=0; double *pt=self->getPointer(); - convertObjToPossibleCpp2(obj,nbc,sw2,singleVal,multiVal,slic,daIntTyypp); + convertObjToPossibleCpp2WithNegIntInterp(obj,nbc,sw2,singleVal,multiVal,slic,daIntTyypp); switch(sw2) { case 1: @@ -2779,8 +2767,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr (wrap) : expecting a pyslice as second (first) parameter !"); Py_ssize_t strt=2,stp=2,step=2; PySliceObject *sly=reinterpret_cast(slic); - if(PySlice_GetIndices(sly,std::numeric_limits::max(),&strt,&stp,&step)!=0) - throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr (wrap) : the input slice is invalid !"); + GetIndicesOfSliceExplicitely(sly,&strt,&stp,&step,"DataArrayInt::buildExplicitArrOfSliceOnScaledArr (wrap) : the input slice is invalid !"); if(strt==std::numeric_limits::max() || stp==std::numeric_limits::max()) throw INTERP_KERNEL::Exception("DataArrayInt::buildExplicitArrOfSliceOnScaledArr (wrap) : the input slice contains some unknowns that can't be determined in static method ! Call DataArray::getSlice (non static) instead !"); return self->buildExplicitArrOfSliceOnScaledArr(strt,stp,step); @@ -4534,7 +4521,7 @@ namespace ParaMEDMEM ParaMEDMEM::DataArrayInt *daIntTyypp=0; const int *pt=self->getConstPointer(); int nbc=self->getNumberOfCompo(); - convertObjToPossibleCpp2(obj,nbc,sw,singleVal,multiVal,slic,daIntTyypp); + convertObjToPossibleCpp2WithNegIntInterp(obj,nbc,sw,singleVal,multiVal,slic,daIntTyypp); switch(sw) { case 1: @@ -4604,7 +4591,7 @@ namespace ParaMEDMEM std::pair > slic; ParaMEDMEM::DataArrayInt *daIntTyypp=0; int *pt=self->getPointer(); - convertObjToPossibleCpp2(obj,nbc,sw2,singleVal,multiVal,slic,daIntTyypp); + convertObjToPossibleCpp2WithNegIntInterp(obj,nbc,sw2,singleVal,multiVal,slic,daIntTyypp); switch(sw2) { case 1: @@ -5531,7 +5518,7 @@ namespace ParaMEDMEM std::vector stdvecTyyppArr; std::pair > sTyyppArr; ParaMEDMEM::DataArrayInt *daIntTyypp=0; - convertObjToPossibleCpp2(obj,self->getNumberOfTuples(),sw,iTypppArr,stdvecTyyppArr,sTyyppArr,daIntTyypp); + convertObjToPossibleCpp2WithNegIntInterp(obj,self->getNumberOfTuples(),sw,iTypppArr,stdvecTyyppArr,sTyyppArr,daIntTyypp); switch(sw) { case 1: @@ -5556,7 +5543,7 @@ namespace ParaMEDMEM ParaMEDMEM::DataArrayInt *daIntTyypp=0; int nbOfCompo=self->getNumberOfComponents(); int nbOfTuples=self->getNumberOfTuples(); - convertObjToPossibleCpp2(obj,nbOfTuples,sw1,iTypppArr,stdvecTyyppArr,sTyyppArr,daIntTyypp); + convertObjToPossibleCpp2WithNegIntInterp(obj,nbOfTuples,sw1,iTypppArr,stdvecTyyppArr,sTyyppArr,daIntTyypp); int sw2; char vc; std::string sc; std::vector vsc; DataArrayChar *dacc=0; convertObjToPossibleCpp6(value,sw2,vc,sc,vsc,dacc); diff --git a/src/MEDCoupling_Swig/MEDCouplingNumPyTest.py b/src/MEDCoupling_Swig/MEDCouplingNumPyTest.py index 9631ee00e..6a6221285 100644 --- a/src/MEDCoupling_Swig/MEDCouplingNumPyTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingNumPyTest.py @@ -31,7 +31,7 @@ import os,gc,weakref,unittest class MEDCouplingNumPyTest(unittest.TestCase): - @unittest.skipUnless(MEDCouplingHasNumPyBindings() and architecture()[0]=="64bit","requires numpy") + @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy") def test1(self): sz=20 a=array(0,dtype=int32) diff --git a/src/MEDLoader/MEDFileParameter.cxx b/src/MEDLoader/MEDFileParameter.cxx index d715f1b04..f2f6a3920 100644 --- a/src/MEDLoader/MEDFileParameter.cxx +++ b/src/MEDLoader/MEDFileParameter.cxx @@ -666,6 +666,11 @@ void MEDFileParameterMultiTS::eraseTimeStepIds(const int *startIds, const int *e _param_per_ts=paramPerTs; } +int MEDFileParameterMultiTS::getNumberOfTS() const +{ + return (int) getIterations().size(); +} + std::vector< std::pair > MEDFileParameterMultiTS::getIterations() const { std::vector< std::pair > ret; diff --git a/src/MEDLoader/MEDFileParameter.hxx b/src/MEDLoader/MEDFileParameter.hxx index f1035d588..fe3814000 100644 --- a/src/MEDLoader/MEDFileParameter.hxx +++ b/src/MEDLoader/MEDFileParameter.hxx @@ -138,6 +138,7 @@ namespace ParaMEDMEM MEDLOADER_EXPORT int getPosGivenTime(double time, double eps=1e-8) const; MEDLOADER_EXPORT MEDFileParameter1TS *getTimeStepAtPos(int posId) const; MEDLOADER_EXPORT void eraseTimeStepIds(const int *startIds, const int *endIds); + MEDLOADER_EXPORT int getNumberOfTS() const; MEDLOADER_EXPORT std::vector< std::pair > getIterations() const; MEDLOADER_EXPORT std::vector< std::pair > getTimeSteps(std::vector& ret1) const; MEDLOADER_EXPORT void simpleRepr2(int bkOffset, std::ostream& oss) const; diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 63acae414..c88e65e15 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -1026,7 +1026,7 @@ namespace ParaMEDMEM { if(PyInt_Check(obj)) { - MEDFileMesh *ret=self->getMeshAtPos((int)PyInt_AS_LONG(obj)); + MEDFileMesh *ret=self->getMeshAtPos(InterpreteNegativeInt((int)PyInt_AS_LONG(obj),self->getNumberOfMeshes())); if(ret) ret->incrRef(); return ret; @@ -1810,12 +1810,8 @@ namespace ParaMEDMEM { Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(elts); - if(PySlice_GetIndices(oC,self->getNumberOfTS(),&strt,&stp,&step)==0) - { - self->eraseTimeStepIds2(strt,stp,step); - } - else - throw INTERP_KERNEL::Exception("MEDFileAnyTypeFieldMultiTS.__delitem__ : error in input slice !"); + GetIndicesOfSlice(oC,self->getNumberOfTS(),&strt,&stp,&step,"MEDFileAnyTypeFieldMultiTS.__delitem__ : error in input slice !"); + self->eraseTimeStepIds2(strt,stp,step); } else { @@ -1880,10 +1876,8 @@ namespace ParaMEDMEM { Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(elt0); - if(PySlice_GetIndices(oC,self->getNumberOfTS(),&strt,&stp,&step)==0) - return convertMEDFileFieldMultiTS(self->buildSubPartSlice(strt,stp,step),SWIG_POINTER_OWN | 0); - else - throw INTERP_KERNEL::Exception("MEDFileAnyTypeFieldMultiTS.__getitem__ : error in input slice !"); + GetIndicesOfSlice(oC,self->getNumberOfTS(),&strt,&stp,&step,"MEDFileAnyTypeFieldMultiTS.__getitem__ : error in input slice !"); + return convertMEDFileFieldMultiTS(self->buildSubPartSlice(strt,stp,step),SWIG_POINTER_OWN | 0); } else return convertMEDFileField1TS(self->getTimeStepAtPos(MEDFileAnyTypeFieldMultiTSgetitemSingleTS__(self,elt0)),SWIG_POINTER_OWN | 0); @@ -2377,10 +2371,8 @@ namespace ParaMEDMEM { Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(elts); - if(PySlice_GetIndices(oC,self->getNumberOfFields(),&strt,&stp,&step)==0) - self->destroyFieldsAtPos2(strt,stp,step); - else - throw INTERP_KERNEL::Exception("MEDFileFields.__delitem__ : error in input slice !"); + GetIndicesOfSlice(oC,self->getNumberOfFields(),&strt,&stp,&step,"MEDFileFields.__delitem__ : error in input slice !"); + self->destroyFieldsAtPos2(strt,stp,step); } else { @@ -2497,6 +2489,7 @@ namespace ParaMEDMEM double getDoubleValue(int iteration, int order) const throw(INTERP_KERNEL::Exception); int getPosOfTimeStep(int iteration, int order) const throw(INTERP_KERNEL::Exception); int getPosGivenTime(double time, double eps=1e-8) const throw(INTERP_KERNEL::Exception); + int getNumberOfTS() const throw(INTERP_KERNEL::Exception); %extend { MEDFileParameterMultiTS() @@ -2567,7 +2560,7 @@ namespace ParaMEDMEM { if(elt0 && PyInt_Check(elt0)) {//fmts[3] - int pos=PyInt_AS_LONG(elt0); + int pos=InterpreteNegativeInt(PyInt_AS_LONG(elt0),self->getNumberOfTS()); return pos; } else if(elt0 && PyTuple_Check(elt0)) @@ -2711,7 +2704,7 @@ namespace ParaMEDMEM { if(PyInt_Check(obj)) { - MEDFileParameterMultiTS *ret=self->getParamAtPos((int)PyInt_AS_LONG(obj)); + MEDFileParameterMultiTS *ret=self->getParamAtPos(InterpreteNegativeInt((int)PyInt_AS_LONG(obj),self->getNumberOfParams())); if(ret) ret->incrRef(); return ret; diff --git a/src/MEDLoader/Swig/MEDLoaderTypemaps.i b/src/MEDLoader/Swig/MEDLoaderTypemaps.i index 96ad8994e..7e7fde217 100644 --- a/src/MEDLoader/Swig/MEDLoaderTypemaps.i +++ b/src/MEDLoader/Swig/MEDLoaderTypemaps.i @@ -310,7 +310,7 @@ int MEDFileAnyTypeFieldMultiTSgetitemSingleTS__(const MEDFileAnyTypeFieldMultiTS { if(elt0 && PyInt_Check(elt0)) {//fmts[3] - return PyInt_AS_LONG(elt0); + return InterpreteNegativeInt(PyInt_AS_LONG(elt0),self->getNumberOfTS()); } else if(elt0 && PyTuple_Check(elt0)) { @@ -347,7 +347,7 @@ int MEDFileFieldsgetitemSingleTS__(const MEDFileFields *self, PyObject *obj) thr { if(PyInt_Check(obj)) { - return (int)PyInt_AS_LONG(obj); + return InterpreteNegativeInt((int)PyInt_AS_LONG(obj),self->getNumberOfFields()); } else if(PyString_Check(obj)) {