Salome HOME
PointLocator implementation for P1P1 for 1D and 2D curve.
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingDataArrayTypemaps.i
index 01df00d9c92d0844e51f7511e74e98eab22231b5..21f462ba01bb3545ef0dfef7d64f182813dac29b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -82,7 +82,7 @@ void numarrdeal(void *pt, void *wron)
     {
       Py_XINCREF(obj);
       PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
-      objC->flags|=NPY_OWNDATA;
+      objC->flags|=NPY_ARRAY_OWNDATA;
       Py_XDECREF(weakRefOnOwner);
       Py_XDECREF(obj);
     }
@@ -282,24 +282,36 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype
   if(PyArray_ISBEHAVED(elt0))//aligned and writeable and in machine byte-order
     {
       PyArrayObject *elt0C=reinterpret_cast<PyArrayObject *>(elt0);
-      PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & NPY_OWNDATA)?elt0C:NULL;
-      int mask=NPY_OWNDATA; mask=~mask;
+      PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & NPY_ARRAY_OWNDATA)?elt0C:NULL;
+      int mask=NPY_ARRAY_OWNDATA; mask=~mask;
       elt0C->flags&=mask;
       PyObject *deepestObj=elt0;
       PyObject *base=elt0C->base;
       if(base) deepestObj=base;
+      bool isSpetialCase(false);
       while(base)
         {
           if(PyArray_Check(base))
             {
               PyArrayObject *baseC=reinterpret_cast<PyArrayObject *>(base);
-              eltOwning=(PyArray_FLAGS(baseC) & NPY_OWNDATA)?baseC:eltOwning;
+              eltOwning=(PyArray_FLAGS(baseC) & NPY_ARRAY_OWNDATA)?baseC:eltOwning;
               baseC->flags&=mask;
               base=baseC->base;
               if(base) deepestObj=base;
             }
           else
-            break;
+            {
+              isSpetialCase=true;
+              break;
+            }
+        }
+      if(isSpetialCase)
+        {// this case is present for numpy arrayint coming from load of pickelized string. The owner of elt0 is not an array -> A copy is requested.
+          std::size_t nbOfElems(sz0*sz1);
+          T *dataCpy=(T*)malloc(sizeof(T)*nbOfElems);
+          std::copy(reinterpret_cast<const T*>(data),reinterpret_cast<const T*>(data)+nbOfElems,dataCpy);
+          ret->useArray(dataCpy,true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
+          return ret.retn();
         }
       typename ParaMEDMEM::MemArray<T>& mma=ret->accessToMemArray();
       if(eltOwning==NULL)
@@ -363,7 +375,7 @@ int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj)
  
 
         /* If this array owns its own data, stop collapsing */
-        if (PyArray_CHKFLAGS(obj_arr, NPY_OWNDATA)) {
+        if (PyArray_CHKFLAGS(obj_arr, NPY_ARRAY_OWNDATA )) { 
             break;
         }   
 
@@ -399,7 +411,7 @@ int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj)
 }
 
 template<class MCData, class T>
-PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
+PyObject *ToNumPyArrayUnderground(MCData *self, int npyObjectType, const char *MCDataStr, int nbTuples, int nbComp)
 {
   if(!self->isAllocated())
     {
@@ -407,7 +419,6 @@ PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
       throw INTERP_KERNEL::Exception(oss.str().c_str());
     }
   ParaMEDMEM::MemArray<T>& mem=self->accessToMemArray();
-  int nbComp=self->getNumberOfComponents();
   if(nbComp==0)
     {
       std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !"; 
@@ -415,7 +426,7 @@ PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
     }
   int nbDims=nbComp==1?1:2;
   npy_intp dim[2];
-  dim[0]=(npy_intp)self->getNumberOfTuples(); dim[1]=nbComp;
+  dim[0]=(npy_intp)nbTuples; dim[1]=nbComp;
   const T *bg=self->getConstPointer();
   PyObject *ret(PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast<T *>(bg)));
   if(mem.isDeallocatorCalled())
@@ -451,6 +462,12 @@ PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
   return ret;
 }
 
+template<class MCData, class T>
+PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
+{
+  return ToNumPyArrayUnderground<MCData,T>(self,npyObjectType,MCDataStr,self->getNumberOfTuples(),self->getNumberOfComponents());
+}
+
 SWIGINTERN PyObject *ParaMEDMEM_DataArrayInt_toNumPyArray(ParaMEDMEM::DataArrayInt *self);
 SWIGINTERN PyObject *ParaMEDMEM_DataArrayDouble_toNumPyArray(ParaMEDMEM::DataArrayDouble *self);
 
@@ -624,6 +641,19 @@ static int *convertPyToNewIntArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL
     }
 }
 
+static PyObject *convertFromVectorPairInt(const std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
+{
+  PyObject *ret=PyList_New(arr.size());
+  for(std::size_t i=0;i<arr.size();i++)
+    {
+      PyObject *t=PyTuple_New(2);
+      PyTuple_SetItem(t,0,PyInt_FromLong(arr[i].first));
+      PyTuple_SetItem(t,1,PyInt_FromLong(arr[i].second));
+      PyList_SetItem(ret,i,t);
+    }
+  return ret;
+}
+
 static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
 {
   const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !";
@@ -681,6 +711,63 @@ static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair<int,
     throw INTERP_KERNEL::Exception(msg);
 }
 
+static void convertPyToVectorPairStringInt(PyObject *pyLi, std::vector< std::pair<std::string,int> >& arr) throw(INTERP_KERNEL::Exception)
+{
+  const char msg[]="convertPyToVectorPairStringInt : list must contain tuples of 2 integers only or tuple must contain tuples of 1 string and 1 integer only !";
+  if(PyList_Check(pyLi))
+    {
+      int size=PyList_Size(pyLi);
+      arr.resize(size);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(PyTuple_Check(o))
+            {
+              int sz2=PyTuple_Size(o);
+              if(sz2!=2)
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_0=PyTuple_GetItem(o,0);
+              if(!PyString_Check(o_0))
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_1=PyTuple_GetItem(o,1);
+              if(!PyInt_Check(o_1))
+                throw INTERP_KERNEL::Exception(msg);
+              arr[i].first=PyString_AsString(o_0);
+              arr[i].second=(int)PyInt_AS_LONG(o_1);
+            }
+          else
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      int size=PyTuple_Size(pyLi);
+      arr.resize(size);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(PyTuple_Check(o))
+            {
+              int sz2=PyTuple_Size(o);
+              if(sz2!=2)
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_0=PyTuple_GetItem(o,0);
+              if(!PyString_Check(o_0))
+                throw INTERP_KERNEL::Exception(msg);
+              PyObject *o_1=PyTuple_GetItem(o,1);
+              if(!PyInt_Check(o_1))
+                throw INTERP_KERNEL::Exception(msg);
+              arr[i].first=PyString_AsString(o_0);
+              arr[i].second=(int)PyInt_AS_LONG(o_1);
+            }
+          else
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else
+    throw INTERP_KERNEL::Exception(msg);
+}
+
 static void convertPyToNewIntArr3(PyObject *pyLi, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
 {
   if(PyList_Check(pyLi))
@@ -939,6 +1026,156 @@ static bool fillStringVector(PyObject *pyLi, std::vector<std::string>& vec) thro
   else
     return false;
 }
+static void convertPyToVectorOfVectorOfString(PyObject *pyLi, std::vector< std::vector<std::string> >& arr) throw(INTERP_KERNEL::Exception)
+{
+  const char msg[]="convertPyToVectorOfVectorOfString : expecting list of list of strings !";
+  if(PyList_Check(pyLi))
+    {
+      Py_ssize_t sz=PyList_Size(pyLi);
+      arr.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(!fillStringVector(o,arr[i]))
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      Py_ssize_t sz=PyTuple_Size(pyLi);
+      arr.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(!fillStringVector(o,arr[i]))
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else
+    throw INTERP_KERNEL::Exception(msg);
+}
+
+static bool fillIntVector(PyObject *pyLi, std::vector<int>& vec) throw(INTERP_KERNEL::Exception)
+{
+  if(PyList_Check(pyLi))
+    {
+      Py_ssize_t sz=PyList_Size(pyLi);
+      vec.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(PyInt_Check(o))
+            vec[i]=PyInt_AS_LONG(o);
+          else
+            return false;
+        }
+      return true;
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      Py_ssize_t sz=PyTuple_Size(pyLi);
+      vec.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(PyInt_Check(o))
+            vec[i]=PyInt_AS_LONG(o);
+          else
+            return false;
+        }
+      return true;
+    }
+  else
+    return false;
+}
+
+static void convertPyToVectorOfVectorOfInt(PyObject *pyLi, std::vector< std::vector<int> >& arr) throw(INTERP_KERNEL::Exception)
+{
+  const char msg[]="convertPyToVectorOfVectorOfInt : expecting list of list of strings !";
+  if(PyList_Check(pyLi))
+    {
+      Py_ssize_t sz=PyList_Size(pyLi);
+      arr.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(!fillIntVector(o,arr[i]))
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      Py_ssize_t sz=PyTuple_Size(pyLi);
+      arr.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(!fillIntVector(o,arr[i]))
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else
+    throw INTERP_KERNEL::Exception(msg);
+}
+
+static void convertPyToVectorPairStringVecString(PyObject *pyLi, std::vector< std::pair<std::string, std::vector<std::string> > >& arr) throw(INTERP_KERNEL::Exception)
+{
+  const char msg[]="convertPyToVectorPairStringVecString : expecting list of tuples containing each exactly 2 items : one string and one vector of string !";
+  if(PyList_Check(pyLi))
+    {
+      Py_ssize_t sz=PyList_Size(pyLi);
+      arr.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(PyTuple_Check(o))
+            {
+              int sz2=PyTuple_Size(o);
+              if(sz2!=2)
+                throw INTERP_KERNEL::Exception(msg);
+              std::pair<std::string, std::vector<std::string> > item;
+              PyObject *o_0=PyTuple_GetItem(o,0);
+              if(!PyString_Check(o_0))
+                throw INTERP_KERNEL::Exception(msg);
+              item.first=PyString_AsString(o_0);
+              PyObject *o_1=PyTuple_GetItem(o,1);
+              if(!fillStringVector(o_1,item.second))
+                throw INTERP_KERNEL::Exception(msg);
+              arr[i]=item;
+            }
+          else
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      Py_ssize_t sz=PyTuple_Size(pyLi);
+      arr.resize(sz);
+      for(int i=0;i<sz;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(PyTuple_Check(o))
+            {
+              int sz2=PyTuple_Size(o);
+              if(sz2!=2)
+                throw INTERP_KERNEL::Exception(msg);
+              std::pair<std::string, std::vector<std::string> > item;
+              PyObject *o_0=PyTuple_GetItem(o,0);
+              if(!PyString_Check(o_0))
+                throw INTERP_KERNEL::Exception(msg);
+              item.first=PyString_AsString(o_0);
+              PyObject *o_1=PyTuple_GetItem(o,1);
+              if(!fillStringVector(o_1,item.second))
+                throw INTERP_KERNEL::Exception(msg);
+              arr[i]=item;
+            }
+          else
+            throw INTERP_KERNEL::Exception(msg);
+        }
+    }
+  else
+    throw INTERP_KERNEL::Exception(msg);
+}
 
 static PyObject *convertDblArrToPyList(const double *ptr, int size) throw(INTERP_KERNEL::Exception)
 {