Salome HOME
Enable unpacking of tuples in DataArrays.
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingMemArray.i
index cbca5915eccefdc761a5a4bd9614078d47b31e24..7d583d4c8599038e25bab3262e4e12f06be9a464 100644 (file)
 %newobject ParaMEDMEM::DataArrayInt::buildPermArrPerLevel;
 %newobject ParaMEDMEM::DataArrayInt::getDifferentValues;
 %newobject ParaMEDMEM::DataArrayInt::FindPermutationFromFirstToSecond;
+%newobject ParaMEDMEM::DataArrayInt::CheckAndPreparePermutation;
 %newobject ParaMEDMEM::DataArrayInt::__neg__;
 %newobject ParaMEDMEM::DataArrayInt::__add__;
 %newobject ParaMEDMEM::DataArrayInt::__radd__;
@@ -408,10 +409,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<PySliceObject *>(slic);
-        if(PySlice_GetIndices(sly,std::numeric_limits<int>::max(),&strt,&stp,&step)!=0)
-          throw INTERP_KERNEL::Exception("DataArray::GetSlice (wrap) : the input slice is invalid !");
-        if(strt==std::numeric_limits<int>::max() || stp==std::numeric_limits<int>::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 +421,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<PySliceObject *>(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 +433,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<PySliceObject *>(slic);
-        if(PySlice_GetIndices(sly,std::numeric_limits<int>::max(),&strt,&stp,&step)!=0)
-          throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBES (wrap) : the input slice is invalid !");
-        if(strt==std::numeric_limits<int>::max() || stp==std::numeric_limits<int>::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 +443,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<PySliceObject *>(slic);
-        if(PySlice_GetIndices(sly,std::numeric_limits<int>::max(),&strt,&stp,&step)!=0)
-          throw INTERP_KERNEL::Exception("DataArray::GetNumberOfItemGivenBESRelative (wrap) : the input slice is invalid !");
-        if(strt==std::numeric_limits<int>::max() || stp==std::numeric_limits<int>::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 +460,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<PySliceObject *>(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 +470,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<PySliceObject *>(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,"");
       }
     }
@@ -697,6 +686,7 @@ namespace ParaMEDMEM
 #endif
         else
           throw INTERP_KERNEL::Exception(msg.c_str());
+        throw INTERP_KERNEL::Exception(msg.c_str());//to make g++ happy
       }
    
       DataArrayDouble(PyObject *elt0, PyObject *nbOfTuples=0, PyObject *elt2=0) throw(INTERP_KERNEL::Exception)
@@ -2260,6 +2250,11 @@ namespace ParaMEDMEM
         return trueSelf;
       }
 
+      PyObject *__len__() throw(INTERP_KERNEL::Exception)
+      {
+        return PyInt_FromLong(self->getNumberOfCompo());
+      }
+
       PyObject *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception)
       {
         const char msg2[]="DataArrayDoubleTuple::__getitem__ : Mismatch of slice values in 2nd parameter (components) !";
@@ -2270,7 +2265,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:
@@ -2279,7 +2274,8 @@ namespace ParaMEDMEM
                 {
                   std::ostringstream oss;
                   oss << "Requesting for id " << singleVal << " having only " << nbc << " components !";
-                  throw INTERP_KERNEL::Exception(oss.str().c_str());
+                  PyErr_SetString(PyExc_StopIteration,oss.str().c_str());
+                  return 0;
                 }
               if(singleVal>=0)
                 return PyFloat_FromDouble(pt[singleVal]);
@@ -2339,7 +2335,7 @@ namespace ParaMEDMEM
         std::pair<int, std::pair<int,int> > 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:
@@ -2704,6 +2700,7 @@ namespace ParaMEDMEM
 #endif
         else
           throw INTERP_KERNEL::Exception(msg.c_str());
+        throw INTERP_KERNEL::Exception(msg.c_str());//to make g++ happy
       }
 
       DataArrayInt(PyObject *elt0, PyObject *nbOfTuples=0, PyObject *nbOfComp=0) throw(INTERP_KERNEL::Exception)
@@ -2777,8 +2774,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<PySliceObject *>(slic);
-        if(PySlice_GetIndices(sly,std::numeric_limits<int>::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<int>::max() || stp==std::numeric_limits<int>::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);
@@ -2798,6 +2794,17 @@ namespace ParaMEDMEM
         return ret;
       }
 
+      static DataArrayInt *CheckAndPreparePermutation(PyObject *arr) throw(INTERP_KERNEL::Exception)
+      {
+        MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
+        int szArr,sw,iTypppArr;
+        std::vector<int> stdvecTyyppArr;
+        const int *arrPtr(convertObjToPossibleCpp1_Safe(arr,sw,szArr,iTypppArr,stdvecTyyppArr));
+        int *pt(ParaMEDMEM::DataArrayInt::CheckAndPreparePermutation(arrPtr,arrPtr+szArr));
+        ret->useArray(pt,true,ParaMEDMEM::C_DEALLOC,szArr,1);
+        return ret.retn();
+      }
+
       void setValues(PyObject *li, PyObject *nbOfTuples=0, PyObject *nbOfComp=0) throw(INTERP_KERNEL::Exception)
       {
         const char *msg="ParaMEDMEM::DataArrayInt::setValues : Available API are : \n-DataArrayInt.setValues([1,3,4])\n-DataArrayInt.setValues([1,3,4],3)\n-DataArrayInt.setValues([1,3,4,5],2,2)\n-DataArrayInt.New(5)\n !";
@@ -4521,6 +4528,11 @@ namespace ParaMEDMEM
         Py_XINCREF(trueSelf);
         return trueSelf;
       }
+
+      PyObject *__len__() throw(INTERP_KERNEL::Exception)
+      {
+        return PyInt_FromLong(self->getNumberOfCompo());
+      }
   
       PyObject *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception)
       {
@@ -4532,7 +4544,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:
@@ -4541,7 +4553,8 @@ namespace ParaMEDMEM
                 {
                   std::ostringstream oss;
                   oss << "Requesting for id " << singleVal << " having only " << nbc << " components !";
-                  throw INTERP_KERNEL::Exception(oss.str().c_str());
+                  PyErr_SetString(PyExc_StopIteration,oss.str().c_str());
+                  return 0;
                 }
               if(singleVal>=0)
                 return PyInt_FromLong(pt[singleVal]);
@@ -4602,7 +4615,7 @@ namespace ParaMEDMEM
         std::pair<int, std::pair<int,int> > 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:
@@ -5529,7 +5542,7 @@ namespace ParaMEDMEM
         std::vector<int> stdvecTyyppArr;
         std::pair<int, std::pair<int,int> > 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:
@@ -5554,7 +5567,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<std::string> vsc; DataArrayChar *dacc=0;
         convertObjToPossibleCpp6(value,sw2,vc,sc,vsc,dacc);