# HG changeset patch # User Jaroslav Hajek # Date 1243320699 -7200 # Node ID 3eb653452a380a7d3b64d0deb3b15c558b5a6de8 # Parent f237eb38e9c3d2f5987ec978f3e261b17353d73d avoid useless instances in octave_to_python.cc diff -r f237eb38e9c3 -r 3eb653452a38 ChangeLog --- a/ChangeLog Mon May 25 13:22:20 2009 +0200 +++ b/ChangeLog Tue May 26 08:51:39 2009 +0200 @@ -1,3 +1,12 @@ +2009-05-26 Jaroslav Hajek + + * octave_to_python.cc: New #include (boost/type_traits). + (copy_octarray_to_pyarrobj): Don't use .value(); don't specialize. + (create_array (..., boost::true_type)): New overload (forward). + (create_array (..., boost::false_type)): New overload (dummy). + (create_uint_array): Pick proper overload. + (create_sint_array): Pick proper overload. + 2009-05-25 Jaroslav Hajek * python_to_octave.cc (pydict_to_octmap): Save key and val in an diff -r f237eb38e9c3 -r 3eb653452a38 octave_to_python.cc --- a/octave_to_python.cc Mon May 25 13:22:20 2009 +0200 +++ b/octave_to_python.cc Tue May 26 08:51:39 2009 +0200 @@ -20,6 +20,7 @@ #include "arrayobjectdefs.h" #include #include +#include #undef HAVE_STAT /* both boost::python and octave define HAVE_STAT... */ #include #include @@ -59,7 +60,7 @@ // Last dimension, base case for (int i = 0; i < pyarr->dimensions[dimension]; i++) { *(PythonPrimitive *)&ptr[offset + i*pyarr->strides[dimension]] - = matrix.elem(matindex + i*matstride).value(); + = matrix.elem(matindex + i*matstride); } } else { for (int i = 0; i < pyarr->dimensions[dimension]; i++) { @@ -74,64 +75,6 @@ } } - template <> - void copy_octarray_to_pyarrobj( - PyArrayObject *pyarr, - const NDArray &matrix, - const unsigned int matindex, - const unsigned int matstride, - const int dimension, - const unsigned int offset) { - unsigned char *ptr = (unsigned char*) pyarr->data; - if (dimension == pyarr->nd - 1) { - // Last dimension, base case - for (int i = 0; i < pyarr->dimensions[dimension]; i++) { - *(double *)&ptr[offset + i*pyarr->strides[dimension]] - = matrix.elem(matindex + i*matstride); - } - } else { - for (int i = 0; i < pyarr->dimensions[dimension]; i++) { - copy_octarray_to_pyarrobj( - pyarr, - matrix, - matindex + i*matstride, - matstride * pyarr->dimensions[dimension], - dimension + 1, - offset + i*pyarr->strides[dimension]); - } - } - } - -#ifdef PYTAVE_USE_OCTAVE_FLOATS - template <> - void copy_octarray_to_pyarrobj( - PyArrayObject *pyarr, - const FloatNDArray &matrix, - const unsigned int matindex, - const unsigned int matstride, - const int dimension, - const unsigned int offset) { - unsigned char *ptr = (unsigned char*) pyarr->data; - if (dimension == pyarr->nd - 1) { - // Last dimension, base case - for (int i = 0; i < pyarr->dimensions[dimension]; i++) { - *(float *)&ptr[offset + i*pyarr->strides[dimension]] - = matrix.elem(matindex + i*matstride); - } - } else { - for (int i = 0; i < pyarr->dimensions[dimension]; i++) { - copy_octarray_to_pyarrobj( - pyarr, - matrix, - matindex + i*matstride, - matstride * pyarr->dimensions[dimension], - dimension + 1, - offset + i*pyarr->strides[dimension]); - } - } - } -#endif /* PYTAVE_USE_OCTAVE_FLOATS */ - static PyArrayObject *createPyArr(const dim_vector &dims, int pyarrtype) { int dimensions[dims.length()]; @@ -157,14 +100,30 @@ return pyarr; } + template + static PyArrayObject *create_array(const OctaveBase &octarr, + int pyarraytype, boost::true_type) { + return create_array (octarr, pyarraytype); + } + + template + static PyArrayObject *create_array(const OctaveBase &octarr, + int pyarraytype, boost::false_type) { + assert(0); + return 0; + } + template inline static PyArrayObject *create_uint_array(CLASS value) { if (bytes == sizeof(int)) { - return create_array(value, PyArray_UINT); + boost::integral_constant inst; + return create_array(value, PyArray_UINT, inst); } else if (bytes == sizeof(char)) { - return create_array(value, PyArray_UBYTE); + boost::integral_constant inst; + return create_array(value, PyArray_UBYTE, inst); } else if (bytes == sizeof(short)) { - return create_array(value, PyArray_USHORT); + boost::integral_constant inst; + return create_array(value, PyArray_USHORT, inst); } else { ostringstream os; os << "Numeric arrays doesn't support unsigned " << (bytes*8) @@ -176,13 +135,17 @@ template inline static PyArrayObject *create_sint_array(CLASS value) { if (bytes == sizeof(long)) { - return create_array(value, PyArray_LONG); + boost::integral_constant inst; + return create_array(value, PyArray_LONG, inst); } else if (bytes == sizeof(int)) { - return create_array(value, PyArray_INT); + boost::integral_constant inst; + return create_array(value, PyArray_INT, inst); } else if (bytes == sizeof(char)) { - return create_array(value, PyArray_SBYTE); + boost::integral_constant inst; + return create_array(value, PyArray_SBYTE, inst); } else if (bytes == sizeof(short)) { - return create_array(value, PyArray_SHORT); + boost::integral_constant inst; + return create_array(value, PyArray_SHORT, inst); } else { ostringstream os; os << "Numeric arrays doesn't support signed " << (bytes*8)