X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDCoupling_Swig%2FMEDCouplingDataArrayTypemaps.i;h=6fd671e1be40c9ea81ced51d3e08a54e10be02f8;hb=a019ec6e72f540d3378f3e869c2b19bf4886459c;hp=1971f90fbe4157d0c913e1c5efb48e233b7402f2;hpb=3b1d77efdd048ef4aad858e96138bf79318119df;p=tools%2Fmedcoupling.git diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i index 1971f90fb..6fd671e1b 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2015 CEA/DEN, EDF R&D +// Copyright (C) 2007-2017 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 @@ -16,17 +16,52 @@ // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -// Author : Anthony Geay (CEA/DEN) +// Author : Anthony Geay (EDF R&D) + +#ifndef __MEDCOUPLINGDATAARRAYTYPEMAPS_I__ +#define __MEDCOUPLINGDATAARRAYTYPEMAPS_I__ + +#if PY_VERSION_HEX >= 0x03000000 +#define PyInt_AS_LONG PyLong_AS_LONG +#endif #include "InterpKernelAutoPtr.hxx" +#include "MEDCouplingDataArrayTraits.hxx" + +#include + +static PyObject *convertArray(MEDCoupling::DataArray *array, int owner) +{ + PyObject *ret(NULL); + if(!array) + { + Py_XINCREF(Py_None); + return Py_None; + } + if(dynamic_cast(array)) + ret=SWIG_NewPointerObj((void*)array,SWIGTYPE_p_MEDCoupling__DataArrayDouble,owner); + if(dynamic_cast(array)) + ret=SWIG_NewPointerObj((void*)array,SWIGTYPE_p_MEDCoupling__DataArrayInt,owner); + if(dynamic_cast(array)) + ret=SWIG_NewPointerObj((void*)array,SWIGTYPE_p_MEDCoupling__DataArrayFloat,owner); + if(!ret) + throw INTERP_KERNEL::Exception("Not recognized type of array on downcast !"); + return ret; +} /*! - * This method is an extention of PySlice_GetIndices but less + * This method is an extension 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) +void GetIndicesOfSlice(PyObject *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)); + int ret(PySlice_GetIndices( +#if PY_VERSION_HEX >= 0x03000000 + slice, +#else + reinterpret_cast(slice), +#endif + length,start,stop,step)); if(ret==0) return ; if(*step>0 && *start==*stop && length==*start) @@ -37,9 +72,15 @@ void GetIndicesOfSlice(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *star /*! * 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) +void GetIndicesOfSliceExplicitely(PyObject *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)); + int ret(PySlice_GetIndices( +#if PY_VERSION_HEX >= 0x03000000 + slice, +#else + reinterpret_cast(slice), +#endif + std::numeric_limits::max(),start,stop,step)); if(ret==0) { if(*start!=std::numeric_limits::max() && *stop!=std::numeric_limits::max()) @@ -68,180 +109,6 @@ int InterpreteNegativeInt(int val, int nbelem) } #ifdef WITH_NUMPY -#include -#if NPY_API_VERSION <= 0x00000006 -# define MED_NUMPY_OWNDATA NPY_OWNDATA -#else -# define MED_NUMPY_OWNDATA NPY_ARRAY_OWNDATA -#endif - -// specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr -// method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it. -// if no more alive the "standart" DataArray deallocator is called. -void numarrdeal(void *pt, void *wron) -{ - void **wronc=(void **)wron; - PyObject *weakRefOnOwner=reinterpret_cast(wronc[0]); - PyObject *obj=PyWeakref_GetObject(weakRefOnOwner); - if(obj!=Py_None) - { - Py_XINCREF(obj); - PyArrayObject *objC=reinterpret_cast(obj); - objC->flags|=MED_NUMPY_OWNDATA; - Py_XDECREF(weakRefOnOwner); - Py_XDECREF(obj); - } - else - { - typedef void (*MyDeallocator)(void *,void *); - MyDeallocator deall=(MyDeallocator)wronc[1]; - deall(pt,NULL); - Py_XDECREF(weakRefOnOwner); - } - delete [] wronc; -} - -template -struct PyCallBackDataArraySt { - PyObject_HEAD - MCData *_pt_mc; -}; - -typedef struct PyCallBackDataArraySt PyCallBackDataArrayInt; -typedef struct PyCallBackDataArraySt PyCallBackDataArrayDouble; - -extern "C" -{ - static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; } - - static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) - { - PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) ); - return (PyObject *)self; - } - - static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) - { - PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) ); - return (PyObject *)self; - } - - static void callbackmcdataarray_dealloc(PyObject *self) - { - Py_TYPE(self)->tp_free(self); - } - - // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed. - // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients. - static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw) - { - if(self->_pt_mc) - { - MEDCoupling::MemArray& mma=self->_pt_mc->accessToMemArray(); - mma.destroy(); - } - Py_XINCREF(Py_None); - return Py_None; - } - - // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed. - // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients. - static PyObject *callbackmcdataarraydouble_call(PyCallBackDataArrayDouble *self, PyObject *args, PyObject *kw) - { - if(self->_pt_mc) - { - MEDCoupling::MemArray& mma=self->_pt_mc->accessToMemArray(); - mma.destroy(); - } - Py_XINCREF(Py_None); - return Py_None; - } -} - -PyTypeObject PyCallBackDataArrayInt_RefType = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "callbackmcdataarrayint", - sizeof(PyCallBackDataArrayInt), - 0, - callbackmcdataarray_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - (ternaryfunc)callbackmcdataarrayint_call, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - 0, /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - 0, /*tp_methods*/ - 0, /*tp_members*/ - 0, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - 0, /*tp_dictoffset*/ - callbackmcdataarray___init__, /*tp_init*/ - PyType_GenericAlloc, /*tp_alloc*/ - callbackmcdataarrayint___new__, /*tp_new*/ - PyObject_GC_Del, /*tp_free*/ -}; - -PyTypeObject PyCallBackDataArrayDouble_RefType = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "callbackmcdataarraydouble", - sizeof(PyCallBackDataArrayDouble), - 0, - callbackmcdataarray_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - (ternaryfunc)callbackmcdataarraydouble_call, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - 0, /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - 0, /*tp_methods*/ - 0, /*tp_members*/ - 0, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - 0, /*tp_dictoffset*/ - callbackmcdataarray___init__, /*tp_init*/ - PyType_GenericAlloc, /*tp_alloc*/ - callbackmcdataarraydouble___new__, /*tp_new*/ - PyObject_GC_Del, /*tp_free*/ -}; - // this is the second type of specific deallocator, only valid for the constructor of DataArrays taking numpy array // in input when an another DataArray is already client of this. template @@ -369,27 +236,27 @@ int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj) /* * Don't allow infinite chains of views, always set the base - * to the first owner of the data. - * That is, either the first object which isn't an array, + * to the first owner of the data. + * That is, either the first object which isn't an array, * or the first object which owns its own data. */ while (PyArray_Check(obj) && (PyObject *)arr != obj) { PyArrayObject *obj_arr = (PyArrayObject *)obj; PyObject *tmp; - + /* If this array owns its own data, stop collapsing */ - if (PyArray_CHKFLAGS(obj_arr, MED_NUMPY_OWNDATA )) { + if (PyArray_CHKFLAGS(obj_arr, MED_NUMPY_OWNDATA )) { break; - } + } tmp = PyArray_BASE(obj_arr); /* If there's no base, stop collapsing */ if (tmp == NULL) { break; } - /* Stop the collapse new base when the would not be of the same + /* Stop the collapse new base when the would not be of the same * type (i.e. different subclass). */ if (Py_TYPE(tmp) != Py_TYPE(arr)) { @@ -426,7 +293,7 @@ PyObject *ToNumPyArrayUnderground(MCData *self, int npyObjectType, const char *M MEDCoupling::MemArray& mem=self->accessToMemArray(); if(nbComp==0) { - std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !"; + std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } int nbDims=nbComp==1?1:2; @@ -476,7 +343,10 @@ PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr) SWIGINTERN PyObject *MEDCoupling_DataArrayInt_toNumPyArray(MEDCoupling::DataArrayInt *self); SWIGINTERN PyObject *MEDCoupling_DataArrayDouble_toNumPyArray(MEDCoupling::DataArrayDouble *self); -PyObject *ToCSRMatrix(const std::vector >& m, int nbCols) throw(INTERP_KERNEL::Exception) +#endif + +#ifdef WITH_SCIPY +PyObject *ToCSRMatrix(const std::vector >& m, int nbCols) { int nbRows((int)m.size()); MEDCoupling::MCAuto indPtr(MEDCoupling::DataArrayInt::New()),indices(MEDCoupling::DataArrayInt::New()); @@ -520,7 +390,7 @@ PyObject *ToCSRMatrix(const std::vector >& m, int nbCols) t #endif -static PyObject *convertDataArrayChar(MEDCoupling::DataArrayChar *dac, int owner) throw(INTERP_KERNEL::Exception) +static PyObject *convertDataArrayChar(MEDCoupling::DataArrayChar *dac, int owner) { PyObject *ret=0; if(!dac) @@ -537,7 +407,7 @@ static PyObject *convertDataArrayChar(MEDCoupling::DataArrayChar *dac, int owner return ret; } -static PyObject *convertDataArray(MEDCoupling::DataArray *dac, int owner) throw(INTERP_KERNEL::Exception) +static PyObject *convertDataArray(MEDCoupling::DataArray *dac, int owner) { PyObject *ret=0; if(!dac) @@ -549,6 +419,8 @@ static PyObject *convertDataArray(MEDCoupling::DataArray *dac, int owner) throw( ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayDouble,owner); if(dynamic_cast(dac)) ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayInt,owner); + if(dynamic_cast(dac)) + ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayFloat,owner); if(dynamic_cast(dac)) ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayByte,owner); if(dynamic_cast(dac)) @@ -558,7 +430,7 @@ static PyObject *convertDataArray(MEDCoupling::DataArray *dac, int owner) throw( return ret; } -static PyObject *convertIntArrToPyList(const int *ptr, int size) throw(INTERP_KERNEL::Exception) +static PyObject *convertIntArrToPyList(const int *ptr, int size) { PyObject *ret=PyList_New(size); for(int i=0;i& v) throw(INTERP_KERNEL::Exception) +static PyObject *convertIntArrToPyList2(const std::vector& v) { int size=v.size(); PyObject *ret=PyList_New(size); @@ -575,7 +447,7 @@ static PyObject *convertIntArrToPyList2(const std::vector& v) throw(INTERP_ return ret; } -static PyObject *convertIntArrToPyList3(const std::set& v) throw(INTERP_KERNEL::Exception) +static PyObject *convertIntArrToPyList3(const std::set& v) { int size=v.size(); PyObject *ret=PyList_New(size); @@ -585,7 +457,45 @@ static PyObject *convertIntArrToPyList3(const std::set& v) throw(INTERP_KER return ret; } -static PyObject *convertIntArrToPyListOfTuple(const int *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception) +static bool convertPyObjectToStrNT(PyObject *obj, std::string& ret) +{ + if(PyString_Check(obj)) + { + ret=PyString_AsString(obj); + return true; + } +#if PY_VERSION_HEX >= 0x03000000 + else if(PyUnicode_Check(obj)) + { + ret=PyUnicode_AsUTF8(obj); + return true; + } +#endif + return false; +} + +static std::string convertPyObjectToStr(PyObject *obj, const char *msg=NULL) +{ + std::string ret; + if(PyString_Check(obj)) + ret=PyString_AsString(obj); +#if PY_VERSION_HEX >= 0x03000000 + else if(PyUnicode_Check(obj)) + ret=PyUnicode_AsUTF8(obj); +#endif + else + { + std::ostringstream oss; + if(msg) + oss << msg; + else + oss << "PyWrap convertPyObjectToStr : expect a string like py object !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + return ret; +} + +static PyObject *convertIntArrToPyListOfTuple(const int *vals, int nbOfComp, int nbOfTuples) { PyObject *ret=PyList_New(nbOfTuples); for(int i=0;i >& arr) throw(INTERP_KERNEL::Exception) +static PyObject *convertFromVectorPairInt(const std::vector< std::pair >& arr) { PyObject *ret=PyList_New(arr.size()); for(std::size_t i=0;i return ret; } -static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair >& arr) throw(INTERP_KERNEL::Exception) +static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair >& arr) { const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !"; if(PyList_Check(pyLi)) @@ -716,7 +626,7 @@ static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair >& arr) throw(INTERP_KERNEL::Exception) +static void convertPyToVectorPairStringInt(PyObject *pyLi, std::vector< std::pair >& arr) { 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)) @@ -732,12 +642,10 @@ static void convertPyToVectorPairStringInt(PyObject *pyLi, std::vector< std::pai 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); + arr[i].first=convertPyObjectToStr(o_0,msg); 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 @@ -757,12 +665,10 @@ static void convertPyToVectorPairStringInt(PyObject *pyLi, std::vector< std::pai 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); + arr[i].first=convertPyObjectToStr(o_0,msg); 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 @@ -773,7 +679,7 @@ static void convertPyToVectorPairStringInt(PyObject *pyLi, std::vector< std::pai throw INTERP_KERNEL::Exception(msg); } -static void convertPyToNewIntArr3(PyObject *pyLi, std::vector& arr) throw(INTERP_KERNEL::Exception) +static void convertPyToNewIntArr3(PyObject *pyLi, std::vector& arr) { if(PyList_Check(pyLi)) { @@ -813,7 +719,7 @@ static void convertPyToNewIntArr3(PyObject *pyLi, std::vector& arr) throw(I } } -static void convertPyToNewIntArr4(PyObject *pyLi, int recurseLev, int nbOfSubPart, std::vector& arr) throw(INTERP_KERNEL::Exception) +static void convertPyToNewIntArr4(PyObject *pyLi, int recurseLev, int nbOfSubPart, std::vector& arr) { if(recurseLev<0) throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : invalid list of integers level of recursion !"); @@ -870,7 +776,7 @@ static void convertPyToNewIntArr4(PyObject *pyLi, int recurseLev, int nbOfSubPar throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : not a list nor a tuple recursively !"); } -static void checkFillArrayWithPyList(int size1, int size2, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception) +static void checkFillArrayWithPyList(int size1, int size2, int& nbOfTuples, int& nbOfComp) { if(nbOfTuples==-1) { @@ -965,7 +871,7 @@ static void fillArrayWithPyListInt3(PyObject *pyLi, int& nbOfElt, std::vector fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception) +static std::vector fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) { std::vector ret; int size1=-1,size2=-1; @@ -998,7 +904,7 @@ static std::vector fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, return ret; } -static bool fillStringVector(PyObject *pyLi, std::vector& vec) throw(INTERP_KERNEL::Exception) +static bool fillStringVector(PyObject *pyLi, std::vector& vec) { if(PyList_Check(pyLi)) { @@ -1007,9 +913,7 @@ static bool fillStringVector(PyObject *pyLi, std::vector& vec) thro for(int i=0;i& vec) thro for(int i=0;i& vec) thro else return false; } -static void convertPyToVectorOfVectorOfString(PyObject *pyLi, std::vector< std::vector >& arr) throw(INTERP_KERNEL::Exception) +static void convertPyToVectorOfVectorOfString(PyObject *pyLi, std::vector< std::vector >& arr) { const char msg[]="convertPyToVectorOfVectorOfString : expecting list of list of strings !"; if(PyList_Check(pyLi)) @@ -1060,7 +962,7 @@ static void convertPyToVectorOfVectorOfString(PyObject *pyLi, std::vector< std:: throw INTERP_KERNEL::Exception(msg); } -static bool fillIntVector(PyObject *pyLi, std::vector& vec) throw(INTERP_KERNEL::Exception) +static bool fillIntVector(PyObject *pyLi, std::vector& vec) { if(PyList_Check(pyLi)) { @@ -1094,7 +996,7 @@ static bool fillIntVector(PyObject *pyLi, std::vector& vec) throw(INTERP_KE return false; } -static void convertPyToVectorOfVectorOfInt(PyObject *pyLi, std::vector< std::vector >& arr) throw(INTERP_KERNEL::Exception) +static void convertPyToVectorOfVectorOfInt(PyObject *pyLi, std::vector< std::vector >& arr) { const char msg[]="convertPyToVectorOfVectorOfInt : expecting list of list of strings !"; if(PyList_Check(pyLi)) @@ -1123,7 +1025,7 @@ static void convertPyToVectorOfVectorOfInt(PyObject *pyLi, std::vector< std::vec throw INTERP_KERNEL::Exception(msg); } -static void convertPyToVectorPairStringVecString(PyObject *pyLi, std::vector< std::pair > >& arr) throw(INTERP_KERNEL::Exception) +static void convertPyToVectorPairStringVecString(PyObject *pyLi, std::vector< std::pair > >& arr) { const char msg[]="convertPyToVectorPairStringVecString : expecting list of tuples containing each exactly 2 items : one string and one vector of string !"; if(PyList_Check(pyLi)) @@ -1140,9 +1042,7 @@ static void convertPyToVectorPairStringVecString(PyObject *pyLi, std::vector< st throw INTERP_KERNEL::Exception(msg); std::pair > item; PyObject *o_0=PyTuple_GetItem(o,0); - if(!PyString_Check(o_0)) - throw INTERP_KERNEL::Exception(msg); - item.first=PyString_AsString(o_0); + item.first=convertPyObjectToStr(o_0,msg); PyObject *o_1=PyTuple_GetItem(o,1); if(!fillStringVector(o_1,item.second)) throw INTERP_KERNEL::Exception(msg); @@ -1166,9 +1066,7 @@ static void convertPyToVectorPairStringVecString(PyObject *pyLi, std::vector< st throw INTERP_KERNEL::Exception(msg); std::pair > item; PyObject *o_0=PyTuple_GetItem(o,0); - if(!PyString_Check(o_0)) - throw INTERP_KERNEL::Exception(msg); - item.first=PyString_AsString(o_0); + item.first=convertPyObjectToStr(o_0,msg); PyObject *o_1=PyTuple_GetItem(o,1); if(!fillStringVector(o_1,item.second)) throw INTERP_KERNEL::Exception(msg); @@ -1182,26 +1080,28 @@ static void convertPyToVectorPairStringVecString(PyObject *pyLi, std::vector< st throw INTERP_KERNEL::Exception(msg); } -static PyObject *convertDblArrToPyList(const double *ptr, int size) throw(INTERP_KERNEL::Exception) +template +PyObject *convertDblArrToPyList(const T *ptr, int size) { - PyObject *ret=PyList_New(size); + PyObject *ret(PyList_New(size)); for(int i=0;i& v) throw(INTERP_KERNEL::Exception) +static PyObject *convertDblArrToPyList2(const std::vector& v) { - int size=v.size(); - PyObject *ret=PyList_New(size); + int size(v.size()); + PyObject *ret(PyList_New(size)); for(int i=0;i +PyObject *convertDblArrToPyListOfTuple(const T *vals, int nbOfComp, int nbOfTuples) { - PyObject *ret=PyList_New(nbOfTuples); + PyObject *ret(PyList_New(nbOfTuples)); for(int i=0;i tmp=new char[nbOfComp+1]; tmp[nbOfComp]='\0'; @@ -1224,7 +1124,7 @@ static PyObject *convertCharArrToPyListOfTuple(const char *vals, int nbOfComp, i return ret; } -static double *convertPyToNewDblArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception) +static double *convertPyToNewDblArr2(PyObject *pyLi, int *size) { if(PyList_Check(pyLi)) { @@ -1348,7 +1248,7 @@ static void fillArrayWithPyListDbl3(PyObject *pyLi, int& nbOfElt, std::vector fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception) +static std::vector fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) { std::vector ret; int size1=-1,size2=-1; @@ -1439,7 +1339,7 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons * * switch between (int,vector,DataArrayInt) */ -static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std::vector& stdvecTyypp, MEDCoupling::DataArrayInt *& daIntTyypp, MEDCoupling::DataArrayIntTuple *&daIntTuple) throw(INTERP_KERNEL::Exception) +static void convertIntStarLikePyObjToCpp(PyObject *value, int& sw, int& iTyypp, std::vector& stdvecTyypp, MEDCoupling::DataArrayInt *& daIntTyypp, MEDCoupling::DataArrayIntTuple *&daIntTuple) { sw=-1; if(PyInt_Check(value)) @@ -1494,7 +1394,7 @@ static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std: } status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0); if(SWIG_IsOK(status)) - { + { daIntTuple=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp); sw=4; return ; @@ -1502,6 +1402,86 @@ static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std: throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple"); } +/*! + * if python int -> cpp int sw=1 + * if python list[int] -> cpp vector sw=2 + * if python tuple[int] -> cpp vector sw=2 + * if python DataArrayInt -> cpp DataArrayInt sw=3 + * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4 + * + * switch between (int,vector,DataArrayInt) + */ +static const int *convertIntStarLikePyObjToCppIntStar(PyObject *value, int& sw, int& sz, int& iTyypp, std::vector& stdvecTyypp) +{ + sw=-1; + if(PyInt_Check(value)) + { + iTyypp=(int)PyInt_AS_LONG(value); + sw=1; sz=1; + return &iTyypp; + } + if(PyTuple_Check(value)) + { + int size=PyTuple_Size(value); + stdvecTyypp.resize(size); + for(int i=0;i(argp); + if(daIntTyypp) + { + sw=3; sz=daIntTyypp->getNbOfElems(); + return daIntTyypp->begin(); + } + else + { + sz=0; + return 0; + } + } + status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0); + if(SWIG_IsOK(status)) + { + MEDCoupling::DataArrayIntTuple *daIntTuple=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp); + sw=4; sz=daIntTuple->getNumberOfCompo(); + return daIntTuple->getConstPointer(); + } + throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple"); +} + /*! * if python double -> cpp double sw=1 * if python int -> cpp double sw=1 @@ -1513,18 +1493,19 @@ static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std: * * switch between (int,vector,DataArrayInt) */ -static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, std::vector& stdvecTyypp, MEDCoupling::DataArrayDouble *& daIntTyypp) throw(INTERP_KERNEL::Exception) +template +void considerPyObjAsATStarLikeObject(PyObject *value, int& sw, T& iTyypp, std::vector& stdvecTyypp, typename MEDCoupling::Traits::ArrayType *& daIntTyypp, swig_type_info *ti) { sw=-1; if(PyFloat_Check(value)) { - iTyypp=PyFloat_AS_DOUBLE(value); + iTyypp=(T)PyFloat_AS_DOUBLE(value); sw=1; return; } if(PyInt_Check(value)) { - iTyypp=(double)PyInt_AS_LONG(value); + iTyypp=(T)PyInt_AS_LONG(value); sw=1; return; } @@ -1536,9 +1517,9 @@ static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, s { PyObject *o=PyTuple_GetItem(value,i); if(PyFloat_Check(o)) - stdvecTyypp[i]=PyFloat_AS_DOUBLE(o); + stdvecTyypp[i]=(T)PyFloat_AS_DOUBLE(o); else if(PyInt_Check(o)) - stdvecTyypp[i]=(double)PyInt_AS_LONG(o); + stdvecTyypp[i]=(T)PyInt_AS_LONG(o); else { std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !"; @@ -1556,9 +1537,9 @@ static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, s { PyObject *o=PyList_GetItem(value,i); if(PyFloat_Check(o)) - stdvecTyypp[i]=PyFloat_AS_DOUBLE(o); + stdvecTyypp[i]=(T)PyFloat_AS_DOUBLE(o); else if(PyInt_Check(o)) - stdvecTyypp[i]=(double)PyInt_AS_LONG(o); + stdvecTyypp[i]=(T)PyInt_AS_LONG(o); else { std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !"; @@ -1569,10 +1550,10 @@ static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, s return; } void *argp; - int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0); + int status=SWIG_ConvertPtr(value,&argp,ti,0|0); if(!SWIG_IsOK(status)) throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDouble"); - daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp); + daIntTyypp=reinterpret_cast< typename MEDCoupling::Traits::ArrayType * >(argp); sw=3; } @@ -1587,7 +1568,7 @@ static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, s * * switch between (int,vector,DataArrayInt) */ -static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, std::vector& stdvecTyypp, MEDCoupling::DataArrayDoubleTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception) +static void convertDoubleStarLikePyObjToCpp(PyObject *value, int& sw, double& iTyypp, std::vector& stdvecTyypp, MEDCoupling::DataArrayDoubleTuple *& daIntTyypp) { sw=-1; if(PyFloat_Check(value)) @@ -1650,6 +1631,106 @@ static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, sw=3; } +template +void convertFPStarLikePyObjToCpp_2(PyObject *value, int& sw, T& val, typename MEDCoupling::Traits::ArrayType *&d, typename MEDCoupling::Traits::ArrayTuple *&e, std::vector& f, swig_type_info *ti_da, swig_type_info *ti_tuple) +{ + sw=-1; + if(PyFloat_Check(value)) + { + val=PyFloat_AS_DOUBLE(value); + sw=1; + return; + } + if(PyInt_Check(value)) + { + val=(T)PyInt_AS_LONG(value); + sw=1; + return; + } + if(PyTuple_Check(value)) + { + int size=PyTuple_Size(value); + f.resize(size); + for(int i=0;i::ArrayType * >(argp); + sw=2; + return ; + } + status=SWIG_ConvertPtr(value,&argp,ti_tuple,0|0); + if(SWIG_IsOK(status)) + { + e=reinterpret_cast< typename MEDCoupling::Traits::ArrayTuple * >(argp); + sw=3; + return ; + } + throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple"); +} + +/*! + * if value int -> cpp val sw=1 + * if value double -> cpp val sw=1 + * if value DataArrayDouble -> cpp DataArrayDouble sw=2 + * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3 + * if value list[int,double] -> cpp std::vector sw=4 + * if value tuple[int,double] -> cpp std::vector sw=4 + */ +static void convertDoubleStarLikePyObjToCpp_2(PyObject *value, int& sw, double& val, MEDCoupling::DataArrayDouble *&d, MEDCoupling::DataArrayDoubleTuple *&e, std::vector& f) +{ + convertFPStarLikePyObjToCpp_2(value,sw,val,d,e,f,SWIGTYPE_p_MEDCoupling__DataArrayDouble,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple); +} + +/*! + * if value int -> cpp val sw=1 + * if value double -> cpp val sw=1 + * if value DataArrayDouble -> cpp DataArrayDouble sw=2 + * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3 + * if value list[int,double] -> cpp std::vector sw=4 + * if value tuple[int,double] -> cpp std::vector sw=4 + */ +static void convertFloatStarLikePyObjToCpp_2(PyObject *value, int& sw, float& val, MEDCoupling::DataArrayFloat *&d, MEDCoupling::DataArrayFloatTuple *&e, std::vector& f) +{ + convertFPStarLikePyObjToCpp_2(value,sw,val,d,e,f,SWIGTYPE_p_MEDCoupling__DataArrayFloat,SWIGTYPE_p_MEDCoupling__DataArrayFloatTuple); +} + /*! * if python int -> cpp int sw=1 * if python list[int] -> cpp vector sw=2 @@ -1659,7 +1740,7 @@ static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, * * switch between (int,vector,DataArrayInt) */ -static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, MEDCoupling::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception) +static void convertIntStarOrSliceLikePyObjToCpp(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, MEDCoupling::DataArrayInt *& daIntTyypp) { const char *msg="5 types accepted : integer, tuple of integer, list of integer, slice, DataArrayInt, DataArrayIntTuple"; sw=-1; @@ -1708,8 +1789,7 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& if(PySlice_Check(value)) { Py_ssize_t strt=2,stp=2,step=2; - PySliceObject *oC=reinterpret_cast(value); - GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); + GetIndicesOfSlice(value,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); p.first=strt; p.second.first=stp; p.second.second=step; @@ -1747,11 +1827,11 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& } /*! - * Idem than convertObjToPossibleCpp2 + * Idem than convertIntStarOrSliceLikePyObjToCpp */ -static void convertObjToPossibleCpp2WithNegIntInterp(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, MEDCoupling::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception) +static void convertIntStarOrSliceLikePyObjToCppWithNegIntInterp(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, MEDCoupling::DataArrayInt *& daIntTyypp) { - convertObjToPossibleCpp2(value,nbelem,sw,iTyypp,stdvecTyypp,p,daIntTyypp); + convertIntStarOrSliceLikePyObjToCpp(value,nbelem,sw,iTyypp,stdvecTyypp,p,daIntTyypp); if(sw==1) { iTyypp=InterpreteNegativeInt(iTyypp,nbelem); @@ -1765,7 +1845,7 @@ static void convertObjToPossibleCpp2WithNegIntInterp(PyObject *value, int nbelem * if python slice -> cpp pair sw=3 * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4 . WARNING The returned pointer can be the null pointer ! */ -static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, MEDCoupling::DataArrayIntTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception) +static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, MEDCoupling::DataArrayIntTuple *& daIntTyypp) { sw=-1; if(PyInt_Check(value)) @@ -1813,8 +1893,7 @@ static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& if(PySlice_Check(value)) { Py_ssize_t strt=2,stp=2,step=2; - PySliceObject *oC=reinterpret_cast(value); - GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); + GetIndicesOfSlice(value,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); p.first=strt; p.second.first=stp; p.second.second=step; @@ -1836,7 +1915,7 @@ static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& * if python not null pointer of DataArrayChar -> cpp DataArrayChar sw=4 * switch between (int,string,vector,DataArrayChar) */ -static void convertObjToPossibleCpp6(PyObject *value, int& sw, char& cTyp, std::string& sType, std::vector& vsType, MEDCoupling::DataArrayChar *& dacType) throw(INTERP_KERNEL::Exception) +static void convertObjToPossibleCpp6(PyObject *value, int& sw, char& cTyp, std::string& sType, std::vector& vsType, MEDCoupling::DataArrayChar *& dacType) { const char *msg="4 types accepted : string, list or tuple of strings having same size, not null DataArrayChar instance."; sw=-1; @@ -1857,19 +1936,40 @@ static void convertObjToPossibleCpp6(PyObject *value, int& sw, char& cTyp, std:: return; } } - if(PyTuple_Check(value)) +#if PY_VERSION_HEX >= 0x03000000 + if(PyUnicode_Check(value)) { - int size=PyTuple_Size(value); - vsType.resize(size); - for(int i=0;i& vt, std::vector& vc, std::pair >& pt, std::pair >& pc, - MEDCoupling::DataArrayInt *&dt, MEDCoupling::DataArrayInt *&dc) throw(INTERP_KERNEL::Exception) + MEDCoupling::DataArrayInt *&dt, MEDCoupling::DataArrayInt *&dc) { if(!PyTuple_Check(value)) { - convertObjToPossibleCpp2WithNegIntInterp(value,nbTuple,sw,it,vt,pt,dt); + convertIntStarOrSliceLikePyObjToCppWithNegIntInterp(value,nbTuple,sw,it,vt,pt,dt); return ; } else @@ -1954,94 +2056,13 @@ 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; - convertObjToPossibleCpp2WithNegIntInterp(ob0,nbTuple,sw1,it,vt,pt,dt); + convertIntStarOrSliceLikePyObjToCppWithNegIntInterp(ob0,nbTuple,sw1,it,vt,pt,dt); PyObject *ob1=PyTuple_GetItem(value,1); - convertObjToPossibleCpp2WithNegIntInterp(ob1,nbCompo,sw2,ic,vc,pc,dc); + convertIntStarOrSliceLikePyObjToCppWithNegIntInterp(ob1,nbCompo,sw2,ic,vc,pc,dc); sw=4*sw2+sw1; } } -/*! - * if value int -> cpp val sw=1 - * if value double -> cpp val sw=1 - * if value DataArrayDouble -> cpp DataArrayDouble sw=2 - * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3 - * if value list[int,double] -> cpp std::vector sw=4 - * if value tuple[int,double] -> cpp std::vector sw=4 - */ -static void convertObjToPossibleCpp5(PyObject *value, int& sw, double& val, MEDCoupling::DataArrayDouble *&d, MEDCoupling::DataArrayDoubleTuple *&e, std::vector& f) -{ - sw=-1; - if(PyFloat_Check(value)) - { - val=PyFloat_AS_DOUBLE(value); - sw=1; - return; - } - if(PyInt_Check(value)) - { - val=(double)PyInt_AS_LONG(value); - sw=1; - return; - } - if(PyTuple_Check(value)) - { - int size=PyTuple_Size(value); - f.resize(size); - for(int i=0;i(argp); - sw=2; - return ; - } - status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0); - if(SWIG_IsOK(status)) - { - e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp); - sw=3; - return ; - } - throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple"); -} - /*! * if value int -> cpp val sw=1 * if value double -> cpp val sw=1 @@ -2051,7 +2072,7 @@ static void convertObjToPossibleCpp5(PyObject *value, int& sw, double& val, MEDC * if value tuple[int,double] -> cpp std::vector sw=4 */ static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, double& val, MEDCoupling::DataArrayDouble *&d, MEDCoupling::DataArrayDoubleTuple *&e, std::vector& f, - const char *msg, int nbTuplesExpected, int nbCompExpected, bool throwIfNullPt) throw(INTERP_KERNEL::Exception) + const char *msg, int nbTuplesExpected, int nbCompExpected, bool throwIfNullPt) { sw=-1; if(PyFloat_Check(value)) @@ -2060,7 +2081,7 @@ static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, dou sw=1; if(nbTuplesExpected*nbCompExpected!=1) { - std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; + std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } return &val; @@ -2071,7 +2092,7 @@ static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, dou sw=1; if(nbTuplesExpected*nbCompExpected!=1) { - std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; + std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } return &val; @@ -2091,7 +2112,7 @@ static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, dou void *argp; int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0); if(SWIG_IsOK(status)) - { + { d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp); sw=2; if(d) @@ -2127,7 +2148,7 @@ static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, dou } status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0); if(SWIG_IsOK(status)) - { + { e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp); sw=3; if(e->getNumberOfCompo()==nbCompExpected) @@ -2136,7 +2157,7 @@ static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, dou return e->getConstPointer(); else { - std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by contruction !"; + std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by construction !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } } @@ -2158,7 +2179,7 @@ static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, dou * if value tuple[int,double] -> cpp std::vector sw=4 */ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, double& val, MEDCoupling::DataArrayDouble *&d, MEDCoupling::DataArrayDoubleTuple *&e, std::vector& f, - const char *msg, int nbCompExpected, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception) + const char *msg, int nbCompExpected, bool throwIfNullPt, int& nbTuples) { sw=-1; if(PyFloat_Check(value)) @@ -2167,7 +2188,7 @@ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, do sw=1; if(nbCompExpected!=1) { - std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; + std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } nbTuples=1; @@ -2179,7 +2200,7 @@ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, do sw=1; if(nbCompExpected!=1) { - std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; + std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } nbTuples=1; @@ -2205,7 +2226,7 @@ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, do sw=4; if(size%nbCompExpected!=0) { - std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; + std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } nbTuples=size/nbCompExpected; @@ -2231,7 +2252,7 @@ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, do sw=4; if(size%nbCompExpected!=0) { - std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; + std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } nbTuples=size/nbCompExpected; @@ -2240,7 +2261,7 @@ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, do void *argp; int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0); if(SWIG_IsOK(status)) - { + { d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp); sw=2; if(d) @@ -2269,7 +2290,7 @@ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, do } status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0); if(SWIG_IsOK(status)) - { + { e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp); sw=3; if(e) @@ -2308,7 +2329,7 @@ static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, do * if value tuple[int,double] -> cpp std::vector sw=4 */ static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& sw, double& val, std::vector& f, - const char *msg, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception) + const char *msg, bool throwIfNullPt, int& nbTuples) { MEDCoupling::DataArrayDouble *d=0; MEDCoupling::DataArrayDoubleTuple *e=0; @@ -2372,7 +2393,7 @@ static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& void *argp; int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0); if(SWIG_IsOK(status)) - { + { d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp); sw=2; if(d) @@ -2401,7 +2422,7 @@ static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& } status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0); if(SWIG_IsOK(status)) - { + { e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp); sw=3; if(e) @@ -2423,107 +2444,941 @@ static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple"); } -/*! - * if python int -> cpp int sw=1 - * if python list[int] -> cpp vector sw=2 - * if python tuple[int] -> cpp vector sw=2 - * if python DataArrayInt -> cpp DataArrayInt sw=3 - * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4 - * - * switch between (int,vector,DataArrayInt) - */ -static const int *convertObjToPossibleCpp1_Safe(PyObject *value, int& sw, int& sz, int& iTyypp, std::vector& stdvecTyypp) throw(INTERP_KERNEL::Exception) +static MEDCoupling::DataArray *CheckAndRetrieveDataArrayInstance(PyObject *obj, const char *msg) { - sw=-1; - if(PyInt_Check(value)) - { - iTyypp=(int)PyInt_AS_LONG(value); - sw=1; sz=1; - return &iTyypp; - } - if(PyTuple_Check(value)) + void *aBasePtrVS=0; + int status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArray,0|0); + if(!SWIG_IsOK(status)) { - int size=PyTuple_Size(value); - stdvecTyypp.resize(size); - for(int i=0;i(aBasePtrVS); +} + +static PyObject *convertPartDefinition(MEDCoupling::PartDefinition *pd, int owner) +{ + PyObject *ret=0; + if(!pd) { - int size=PyList_Size(value); - stdvecTyypp.resize(size); - for(int i=0;i(pd)) + ret=SWIG_NewPointerObj((void*)pd,SWIGTYPE_p_MEDCoupling__DataArrayPartDefinition,owner); + else + ret=SWIG_NewPointerObj((void*)pd,SWIGTYPE_p_MEDCoupling__SlicePartDefinition,owner); + return ret; +} + +template +static typename MEDCoupling::Traits::ArrayType *DataArrayT_New(PyObject *elt0, PyObject *nbOfTuples, PyObject *elt2) +{ + const char *msgBase="MEDCoupling::DataArrayDouble::New : Available API are : \n-DataArrayDouble.New()\n-DataArrayDouble.New([1.,3.,4.])\n-DataArrayDouble.New([1.,3.,4.],3)\n-DataArrayDouble.New([1.,3.,4.,5.],2,2)\n-DataArrayDouble.New([1.,3.,4.,5.,7,8.],3,2)\n-DataArrayDouble.New([(1.,3.),(4.,5.),(7,8.)])\n-DataArrayDouble.New(5)\n-DataArrayDouble.New(5,2)"; + std::string msg(msgBase); +#ifdef WITH_NUMPY + msg+="\n-DataArrayDouble.New(numpy array with dtype=float64)"; +#endif + msg+=" !"; + if(PyList_Check(elt0) || PyTuple_Check(elt0)) + { + if(nbOfTuples) { - PyObject *o=PyList_GetItem(value,i); - if(PyInt_Check(o)) - stdvecTyypp[i]=(int)PyInt_AS_LONG(o); - else + if(PyInt_Check(nbOfTuples)) { - std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); + int nbOfTuples1=PyInt_AS_LONG(nbOfTuples); + if(nbOfTuples1<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive set of allocated memory !"); + if(elt2) + { + if(PyInt_Check(elt2)) + {//DataArrayDouble.New([1.,3.,4.,5.],2,2) + int nbOfCompo=PyInt_AS_LONG(elt2); + if(nbOfCompo<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive number of components !"); + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > ret(MEDCoupling::Traits::ArrayType::New()); + std::vector tmp(fillArrayWithPyListDbl2(elt0,nbOfTuples1,nbOfCompo)); + ret->alloc(nbOfTuples1,nbOfCompo); std::copy(tmp.begin(),tmp.end(),ret->getPointer()); + return ret.retn(); + } + else + throw INTERP_KERNEL::Exception(msg.c_str()); + } + else + {//DataArrayDouble.New([1.,3.,4.],3) + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > ret(MEDCoupling::Traits::ArrayType::New()); + int tmpp1(-1); + std::vector tmp(fillArrayWithPyListDbl2(elt0,nbOfTuples1,tmpp1)); + ret->alloc(nbOfTuples1,tmpp1); std::copy(tmp.begin(),tmp.end(),ret->getPointer()); + return ret.retn(); + } } + else + throw INTERP_KERNEL::Exception(msg.c_str()); + } + else + {// DataArrayDouble.New([1.,3.,4.]) + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > ret(MEDCoupling::Traits::ArrayType::New()); + int tmpp1(-1),tmpp2(-1); + std::vector tmp=fillArrayWithPyListDbl2(elt0,tmpp1,tmpp2); + ret->alloc(tmpp1,tmpp2); std::copy(tmp.begin(),tmp.end(),ret->getPointer()); + return ret.retn(); } - sw=2; sz=size; - return &stdvecTyypp[0]; } - void *argp; - int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayInt,0|0); - if(SWIG_IsOK(status)) + else if(PyInt_Check(elt0)) { - MEDCoupling::DataArrayInt *daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayInt * >(argp); - if(daIntTyypp) + int nbOfTuples1(PyInt_AS_LONG(elt0)); + if(nbOfTuples1<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive set of allocated memory !"); + if(nbOfTuples) { - sw=3; sz=daIntTyypp->getNbOfElems(); - return daIntTyypp->begin(); + if(!elt2) + { + if(PyInt_Check(nbOfTuples)) + {//DataArrayDouble.New(5,2) + int nbOfCompo=PyInt_AS_LONG(nbOfTuples); + if(nbOfCompo<0) + throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive number of components !"); + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > ret(MEDCoupling::Traits::ArrayType::New()); + ret->alloc(nbOfTuples1,nbOfCompo); + return ret.retn(); + } + else + throw INTERP_KERNEL::Exception(msg.c_str()); + } + else + throw INTERP_KERNEL::Exception(msg.c_str()); } else - { - sz=0; - return 0; + {//DataArrayDouble.New(5) + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > ret(MEDCoupling::Traits::ArrayType::New()); + ret->alloc(nbOfTuples1,1); + return ret.retn(); } } - status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0); - if(SWIG_IsOK(status)) - { - MEDCoupling::DataArrayIntTuple *daIntTuple=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp); - sw=4; sz=daIntTuple->getNumberOfCompo(); - return daIntTuple->getConstPointer(); +#ifdef WITH_NUMPY + else if(PyArray_Check(elt0) && nbOfTuples==NULL && elt2==NULL) + {//DataArrayDouble.New(numpyArray) + return BuildNewInstance< typename MEDCoupling::Traits::ArrayType , T >(elt0,NPYTraits::NPYObjectType,NPYTraits::NPYFunc,MEDCoupling::Traits::NPYStr); } - throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple"); +#endif + else + throw INTERP_KERNEL::Exception(msg.c_str()); + throw INTERP_KERNEL::Exception(msg.c_str());//to make g++ happy } -static MEDCoupling::DataArray *CheckAndRetrieveDataArrayInstance(PyObject *obj, const char *msg) +template +typename MEDCoupling::Traits::ArrayType *DataArrayT__setitem__internal(typename MEDCoupling::Traits::ArrayType *self, PyObject *obj, PyObject *value, swig_type_info *ti) { - void *aBasePtrVS=0; - int status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArray,0|0); - if(!SWIG_IsOK(status)) + self->checkAllocated(); + const char msg[]="Unexpected situation in DataArrayDouble::__setitem__ !"; + int nbOfTuples(self->getNumberOfTuples()),nbOfComponents(self->getNumberOfComponents()); + int sw1,sw2; + T i1; + std::vector v1; + typename MEDCoupling::Traits::ArrayType *d1=0; + considerPyObjAsATStarLikeObject(value,sw1,i1,v1,d1,ti); + int it1,ic1; + std::vector vt1,vc1; + std::pair > pt1,pc1; + MEDCoupling::DataArrayInt *dt1=0,*dc1=0; + convertObjToPossibleCpp3(obj,nbOfTuples,nbOfComponents,sw2,it1,ic1,vt1,vc1,pt1,pc1,dt1,dc1); + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > tmp; + switch(sw2) + { + case 1: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple1(i1,it1,it1+1,1,0,nbOfComponents,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues1(tmp,it1,it1+1,1,0,nbOfComponents,1,false); + return self; + case 3: + self->setPartOfValues1(d1,it1,it1+1,1,0,nbOfComponents,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 2: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple3(i1,&vt1[0],&vt1[0]+vt1.size(),0,nbOfComponents,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),0,nbOfComponents,1,false); + return self; + case 3: + self->setPartOfValues3(d1,&vt1[0],&vt1[0]+vt1.size(),0,nbOfComponents,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 3: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple1(i1,pt1.first,pt1.second.first,pt1.second.second,0,nbOfComponents,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues1(tmp,pt1.first,pt1.second.first,pt1.second.second,0,nbOfComponents,1,false); + return self; + case 3: + self->setPartOfValues1(d1,pt1.first,pt1.second.first,pt1.second.second,0,nbOfComponents,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 4: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple3(i1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),0,nbOfComponents,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),0,nbOfComponents,1,false); + return self; + case 3: + self->setPartOfValues3(d1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),0,nbOfComponents,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 5: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple1(i1,it1,it1+1,1,ic1,ic1+1,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues1(tmp,it1,it1+1,1,ic1,ic1+1,1,false); + return self; + case 3: + self->setPartOfValues1(d1,it1,it1+1,1,ic1,ic1+1,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 6: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple3(i1,&vt1[0],&vt1[0]+vt1.size(),ic1,ic1+1,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),ic1,ic1+1,1,false); + return self; + case 3: + self->setPartOfValues3(d1,&vt1[0],&vt1[0]+vt1.size(),ic1,ic1+1,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 7: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple1(i1,pt1.first,pt1.second.first,pt1.second.second,ic1,ic1+1,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues1(tmp,pt1.first,pt1.second.first,pt1.second.second,ic1,ic1+1,1,false); + return self; + case 3: + self->setPartOfValues1(d1,pt1.first,pt1.second.first,pt1.second.second,ic1,ic1+1,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 8: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple3(i1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),ic1,ic1+1,1); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),ic1,ic1+1,1,false); + return self; + case 3: + self->setPartOfValues3(d1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),ic1,ic1+1,1); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 9: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple2(i1,&it1,&it1+1,&vc1[0],&vc1[0]+vc1.size()); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues2(tmp,&it1,&it1+1,&vc1[0],&vc1[0]+vc1.size(),false); + return self; + case 3: + self->setPartOfValues2(d1,&it1,&it1+1,&vc1[0],&vc1[0]+vc1.size()); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 10: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple2(i1,&vt1[0],&vt1[0]+vt1.size(),&vc1[0],&vc1[0]+vc1.size()); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues2(tmp,&vt1[0],&vt1[0]+vt1.size(),&vc1[0],&vc1[0]+vc1.size(),false); + return self; + case 3: + self->setPartOfValues2(d1,&vt1[0],&vt1[0]+vt1.size(),&vc1[0],&vc1[0]+vc1.size()); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 11: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple4(i1,pt1.first,pt1.second.first,pt1.second.second,&vc1[0],&vc1[0]+vc1.size()); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues4(tmp,pt1.first,pt1.second.first,pt1.second.second,&vc1[0],&vc1[0]+vc1.size(),false); + return self; + case 3: + self->setPartOfValues4(d1,pt1.first,pt1.second.first,pt1.second.second,&vc1[0],&vc1[0]+vc1.size()); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 12: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple2(i1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),&vc1[0],&vc1[0]+vc1.size()); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues2(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),&vc1[0],&vc1[0]+vc1.size(),false); + return self; + case 3: + self->setPartOfValues2(d1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),&vc1[0],&vc1[0]+vc1.size()); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 13: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple1(i1,it1,it1+1,1,pc1.first,pc1.second.first,pc1.second.second); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues1(tmp,it1,it1+1,1,pc1.first,pc1.second.first,pc1.second.second,false); + return self; + case 3: + self->setPartOfValues1(d1,it1,it1+1,1,pc1.first,pc1.second.first,pc1.second.second); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 14: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple3(i1,&vt1[0],&vt1[0]+vt1.size(),pc1.first,pc1.second.first,pc1.second.second); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues3(tmp,&vt1[0],&vt1[0]+vt1.size(),pc1.first,pc1.second.first,pc1.second.second,false); + return self; + case 3: + self->setPartOfValues3(d1,&vt1[0],&vt1[0]+vt1.size(),pc1.first,pc1.second.first,pc1.second.second); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 15: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple1(i1,pt1.first,pt1.second.first,pt1.second.second,pc1.first,pc1.second.first,pc1.second.second); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues1(tmp,pt1.first,pt1.second.first,pt1.second.second,pc1.first,pc1.second.first,pc1.second.second,false); + return self; + case 3: + self->setPartOfValues1(d1,pt1.first,pt1.second.first,pt1.second.second,pc1.first,pc1.second.first,pc1.second.second); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + case 16: + { + switch(sw1) + { + case 1: + self->setPartOfValuesSimple3(i1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),pc1.first,pc1.second.first,pc1.second.second); + return self; + case 2: + tmp=MEDCoupling::Traits::ArrayType::New(); + tmp->useArray(&v1[0],false,MEDCoupling::CPP_DEALLOC,1,v1.size()); + self->setPartOfValues3(tmp,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),pc1.first,pc1.second.first,pc1.second.second,false); + return self; + case 3: + self->setPartOfValues3(d1,dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems(),pc1.first,pc1.second.first,pc1.second.second); + return self; + default: + throw INTERP_KERNEL::Exception(msg); + } + break; + } + default: + throw INTERP_KERNEL::Exception(msg); + } + return self; +} + +template +PyObject *DataArrayT__getitem__internal(const typename MEDCoupling::Traits::ArrayType *self, PyObject *obj, swig_type_info *ti) +{ + const char msg[]="Unexpected situation in DataArrayDouble::__getitem__ !"; + const char msg2[]="DataArrayDouble::__getitem__ : Mismatch of slice values in 2nd parameter (components) !"; + self->checkAllocated(); + int nbOfTuples(self->getNumberOfTuples()),nbOfComponents(self->getNumberOfComponents()); + int it1,ic1; + std::vector vt1,vc1; + std::pair > pt1,pc1; + MEDCoupling::DataArrayInt *dt1=0,*dc1=0; + int sw; + convertObjToPossibleCpp3(obj,nbOfTuples,nbOfComponents,sw,it1,ic1,vt1,vc1,pt1,pc1,dt1,dc1); + MEDCoupling::MCAuto::ArrayType > ret; + switch(sw) + { + case 1: + if(nbOfComponents==1) + return PyFloat_FromDouble((T)self->getIJSafe(it1,0)); + return SWIG_NewPointerObj(SWIG_as_voidptr(self->selectByTupleIdSafe(&it1,&it1+1)),ti, SWIG_POINTER_OWN | 0 ); + case 2: + return SWIG_NewPointerObj(SWIG_as_voidptr(self->selectByTupleIdSafe(&vt1[0],&vt1[0]+vt1.size())),ti, SWIG_POINTER_OWN | 0 ); + case 3: + return SWIG_NewPointerObj(SWIG_as_voidptr(self->selectByTupleIdSafeSlice(pt1.first,pt1.second.first,pt1.second.second)),ti, SWIG_POINTER_OWN | 0 ); + case 4: + return SWIG_NewPointerObj(SWIG_as_voidptr(self->selectByTupleIdSafe(dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems())),ti, SWIG_POINTER_OWN | 0 ); + case 5: + return PyFloat_FromDouble((T)self->getIJSafe(it1,ic1)); + case 6: + { + ret=self->selectByTupleIdSafe(&vt1[0],&vt1[0]+vt1.size()); + std::vector v2(1,ic1); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret->keepSelectedComponents(v2)),ti, SWIG_POINTER_OWN | 0 ); + } + case 7: + { + ret=self->selectByTupleIdSafeSlice(pt1.first,pt1.second.first,pt1.second.second); + std::vector v2(1,ic1); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret->keepSelectedComponents(v2)),ti, SWIG_POINTER_OWN | 0 ); + } + case 8: + { + ret=self->selectByTupleIdSafe(dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems()); + std::vector v2(1,ic1); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret->keepSelectedComponents(v2)),ti, SWIG_POINTER_OWN | 0 ); + } + case 9: + { + ret=self->selectByTupleIdSafe(&it1,&it1+1); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret->keepSelectedComponents(vc1)),ti, SWIG_POINTER_OWN | 0 ); + } + case 10: + { + ret=self->selectByTupleIdSafe(&vt1[0],&vt1[0]+vt1.size()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret->keepSelectedComponents(vc1)),ti, SWIG_POINTER_OWN | 0 ); + } + case 11: + { + ret=self->selectByTupleIdSafeSlice(pt1.first,pt1.second.first,pt1.second.second); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret->keepSelectedComponents(vc1)),ti, SWIG_POINTER_OWN | 0 ); + } + case 12: + { + ret=self->selectByTupleIdSafe(dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret->keepSelectedComponents(vc1)),ti, SWIG_POINTER_OWN | 0 ); + } + case 13: + { + ret=self->selectByTupleIdSafe(&it1,&it1+1); + int nbOfComp(MEDCoupling::DataArray::GetNumberOfItemGivenBESRelative(pc1.first,pc1.second.first,pc1.second.second,msg2)); + std::vector v2(nbOfComp); + for(int i=0;ikeepSelectedComponents(v2)),ti, SWIG_POINTER_OWN | 0 ); + } + case 14: + { + ret=self->selectByTupleIdSafe(&vt1[0],&vt1[0]+vt1.size()); + int nbOfComp(MEDCoupling::DataArray::GetNumberOfItemGivenBESRelative(pc1.first,pc1.second.first,pc1.second.second,msg2)); + std::vector v2(nbOfComp); + for(int i=0;ikeepSelectedComponents(v2)),ti, SWIG_POINTER_OWN | 0 ); + } + case 15: + { + ret=self->selectByTupleIdSafeSlice(pt1.first,pt1.second.first,pt1.second.second); + int nbOfComp(MEDCoupling::DataArray::GetNumberOfItemGivenBESRelative(pc1.first,pc1.second.first,pc1.second.second,msg2)); + std::vector v2(nbOfComp); + for(int i=0;ikeepSelectedComponents(v2)),ti, SWIG_POINTER_OWN | 0 ); + } + case 16: + { + ret=self->selectByTupleIdSafe(dt1->getConstPointer(),dt1->getConstPointer()+dt1->getNbOfElems()); + int nbOfComp(MEDCoupling::DataArray::GetNumberOfItemGivenBESRelative(pc1.first,pc1.second.first,pc1.second.second,msg2)); + std::vector v2(nbOfComp); + for(int i=0;ikeepSelectedComponents(v2)),ti, SWIG_POINTER_OWN | 0 ); + } + default: + throw INTERP_KERNEL::Exception(msg); + } +} + +bool isCSRMatrix(PyObject *m) +{ +#if defined(WITH_NUMPY) && defined(WITH_SCIPY) + PyObject* pdict(PyDict_New()); + PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins()); + PyObject *tmp(PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict)); + if(!tmp) + throw INTERP_KERNEL::Exception("Problem during loading csr_matrix in scipy.sparse ! Is Scipy module available in present ?"); + PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix"); + if(!csrMatrixCls) + throw INTERP_KERNEL::Exception("csr_matrix not found in scipy.sparse ! Is Scipy module available in present ?"); + bool ret(PyObject_IsInstance(m,csrMatrixCls)); + Py_DECREF(pdict); Py_XDECREF(tmp); + return ret; +#else + return false; +#endif +} + +void convertCSR_MCDataToVectMapIntDouble(const MEDCoupling::DataArrayInt *indptrPtr, const MEDCoupling::DataArrayInt *indicesPtr, const MEDCoupling::DataArrayDouble *dataPtr, std::vector >& mCpp) +{ +#if __cplusplus >= 201103L + auto nbOfRows(indptrPtr->getNumberOfTuples()-1); + if(nbOfRows<0) + throw INTERP_KERNEL::Exception("pywrap of MEDCouplingRemapper::setMatrix : input CSR matrix looks bad regarding indptr array !"); + mCpp.resize(nbOfRows); + auto indPtrCPtr(indptrPtr->begin()); + auto indicesCPtr(indicesPtr->begin()); + auto dataCPtr(dataPtr->begin()); + for(auto i=0;i >& mCpp) +{ + if(!PyList_Check(pyobj)) + throw INTERP_KERNEL::Exception("convertToVectMapIntDouble : input is not a python list !"); + mCpp.clear(); + Py_ssize_t sz(PyList_Size(pyobj)); + mCpp.resize(sz); + for(Py_ssize_t i=0;i& mapCpp(mCpp[i]); + while(PyDict_Next(elt,&pos,&key,&value)) + { + if(!PyInt_Check(key)) { - status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArrayAsciiChar,0|0); - if(!SWIG_IsOK(status)) - { - status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArrayByte,0|0); - std::ostringstream oss; oss << msg << " ! Accepted instances are DataArrayDouble, DataArrayInt, DataArrayAsciiChar, DataArrayByte !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } + std::ostringstream oss; oss << "convertToVectMapIntDouble : at pos # " << i << " of pylist the dict contains at pos " << pos << " a key not mappable to pyint !"; + throw INTERP_KERNEL::Exception(oss.str()); } + if(!PyFloat_Check(value)) + { + std::ostringstream oss; oss << "convertToVectMapIntDouble : at pos # " << i << " of pylist the dict contains at pos " << pos << " the value not mappable to pyfloat !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + mapCpp[(int)PyInt_AS_LONG(key)]=PyFloat_AS_DOUBLE(value); } } - return reinterpret_cast< MEDCoupling::DataArray * >(aBasePtrVS); } + +template +PyObject *DataArrayT_imul__internal(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self, swig_type_info *ti_da, swig_type_info *ti_tuple) +{ + const char msg[]="Unexpected situation in __imul__ !"; + T val; + typename MEDCoupling::Traits::ArrayType *a; + typename MEDCoupling::Traits::ArrayTuple *aa; + std::vector bb; + int sw; + convertFPStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb,ti_da,ti_tuple); + switch(sw) + { + case 1: + { + self->applyLin(val,0.); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 2: + { + self->multiplyEqual(a); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 3: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(aa->buildDA(1,self->getNumberOfComponents())); + self->multiplyEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 4: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(MEDCoupling::Traits::ArrayType::New()); aaa->useArray(&bb[0],false,MEDCoupling::CPP_DEALLOC,1,(int)bb.size()); + self->multiplyEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + default: + throw INTERP_KERNEL::Exception(msg); + } +} + +template +PyObject *DataArrayT_idiv__internal(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self, swig_type_info *ti_da, swig_type_info *ti_tuple) +{ + const char msg[]="Unexpected situation in __idiv__ !"; + T val; + typename MEDCoupling::Traits::ArrayType *a; + typename MEDCoupling::Traits::ArrayTuple *aa; + std::vector bb; + int sw; + convertFPStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb,ti_da,ti_tuple); + switch(sw) + { + case 1: + { + if(val==0.) + throw INTERP_KERNEL::Exception("DataArrayDouble::__div__ : trying to divide by zero !"); + self->applyLin(1./val,0.); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 2: + { + self->divideEqual(a); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 3: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(aa->buildDA(1,self->getNumberOfComponents())); + self->divideEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 4: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(MEDCoupling::Traits::ArrayType::New()); aaa->useArray(&bb[0],false,MEDCoupling::CPP_DEALLOC,1,(int)bb.size()); + self->divideEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + default: + throw INTERP_KERNEL::Exception(msg); + } +} + +template +PyObject *DataArrayT_iadd__internal(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self, swig_type_info *ti_da, swig_type_info *ti_tuple) +{ + const char msg[]="Unexpected situation in __iadd__ !"; + T val; + typename MEDCoupling::Traits::ArrayType *a; + typename MEDCoupling::Traits::ArrayTuple *aa; + std::vector bb; + int sw; + convertFPStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb,ti_da,ti_tuple); + switch(sw) + { + case 1: + { + self->applyLin(1.,val); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 2: + { + self->addEqual(a); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 3: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(aa->buildDA(1,self->getNumberOfComponents())); + self->addEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 4: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(MEDCoupling::Traits::ArrayType::New()); aaa->useArray(&bb[0],false,MEDCoupling::CPP_DEALLOC,1,(int)bb.size()); + self->addEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + default: + throw INTERP_KERNEL::Exception(msg); + } +} + +template +PyObject *DataArrayT_isub__internal(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self, swig_type_info *ti_da, swig_type_info *ti_tuple) +{ + const char msg[]="Unexpected situation in __isub__ !"; + T val; + typename MEDCoupling::Traits::ArrayType *a; + typename MEDCoupling::Traits::ArrayTuple *aa; + std::vector bb; + int sw; + convertFPStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb,ti_da,ti_tuple); + switch(sw) + { + case 1: + { + self->applyLin(1.,-val); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 2: + { + self->substractEqual(a); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 3: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(aa->buildDA(1,self->getNumberOfComponents())); + self->substractEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 4: + { + MEDCoupling::MCAuto< typename MEDCoupling::Traits::ArrayType > aaa(MEDCoupling::Traits::ArrayType::New()); aaa->useArray(&bb[0],false,MEDCoupling::CPP_DEALLOC,1,(int)bb.size()); + self->substractEqual(aaa); + Py_XINCREF(trueSelf); + return trueSelf; + } + default: + throw INTERP_KERNEL::Exception(msg); + } +} + +template +struct SWIGTITraits +{ }; + +template<> +struct SWIGTITraits +{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; }; + +template<> +struct SWIGTITraits +{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; }; + +template<> +struct SWIGTITraits +{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; }; + +swig_type_info *SWIGTITraits::TI=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayDouble is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayDouble is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe() + +#ifdef WITH_NUMPY +PyTypeObject *NPYTraits::NPYFunc=&PyCallBackDataArrayDouble_RefType; + +PyTypeObject *NPYTraits::NPYFunc=&PyCallBackDataArrayFloat_RefType; +#endif + +template +typename MEDCoupling::Traits::ArrayType *DataArrayT__setitem__(typename MEDCoupling::Traits::ArrayType *self, PyObject *obj, PyObject *value) +{ + return DataArrayT__setitem__internal(self,obj,value,SWIGTITraits::TI); +} + +template +PyObject *DataArrayT__getitem(const typename MEDCoupling::Traits::ArrayType *self, PyObject *obj) +{ + return DataArrayT__getitem__internal(self,obj,SWIGTITraits::TI); +} + +template +PyObject *DataArrayT_imul(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self) +{ + return DataArrayT_imul__internal(trueSelf,obj,self,SWIGTITraits::TI,SWIGTITraits::TI_TUPLE); +} + +template +PyObject *DataArrayT_idiv(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self) +{ + return DataArrayT_idiv__internal(trueSelf,obj,self,SWIGTITraits::TI,SWIGTITraits::TI_TUPLE); +} + +template +PyObject *DataArrayT_iadd(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self) +{ + return DataArrayT_iadd__internal(trueSelf,obj,self,SWIGTITraits::TI,SWIGTITraits::TI_TUPLE); +} + +template +PyObject *DataArrayT_isub(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits::ArrayType *self) +{ + return DataArrayT_isub__internal(trueSelf,obj,self,SWIGTITraits::TI,SWIGTITraits::TI_TUPLE); +} + +template +typename MEDCoupling::Traits::ArrayType *DataArrayFPT_rmul(typename MEDCoupling::Traits::ArrayType *self, PyObject *obj) +{ + const char msg[]="Unexpected situation in __rmul__ !"; + T val; + typename MEDCoupling::Traits::ArrayType *a; + typename MEDCoupling::Traits::ArrayTuple *aa; + std::vector bb; + int sw; + convertFPStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb,SWIGTITraits::TI,SWIGTITraits::TI_TUPLE); + switch(sw) + { + case 1: + { + typename MEDCoupling::MCAuto::ArrayType> ret(self->deepCopy()); + ret->applyLin(val,0.); + return ret.retn(); + } + case 3: + { + typename MEDCoupling::MCAuto::ArrayType> aaa(aa->buildDA(1,self->getNumberOfComponents())); + return MEDCoupling::Traits::ArrayType::Multiply(self,aaa); + } + case 4: + { + typename MEDCoupling::MCAuto::ArrayType> aaa(MEDCoupling::Traits::ArrayType::New()); aaa->useArray(&bb[0],false,MEDCoupling::CPP_DEALLOC,1,(int)bb.size()); + return MEDCoupling::Traits::ArrayType::Multiply(self,aaa); + } + default: + throw INTERP_KERNEL::Exception(msg); + } +} + +#endif