Mercurial > pytave
changeset 45:3eb653452a38
avoid useless instances in octave_to_python.cc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 26 May 2009 08:51:39 +0200 |
parents | f237eb38e9c3 |
children | 095e26d93935 |
files | ChangeLog octave_to_python.cc |
diffstat | 2 files changed, 38 insertions(+), 66 deletions(-) [+] |
line wrap: on
line diff
--- 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 <highegg@gmail.com> + + * 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 <highegg@gmail.com> * python_to_octave.cc (pydict_to_octmap): Save key and val in an
--- 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 <boost/python.hpp> #include <boost/python/numeric.hpp> +#include <boost/type_traits/integral_constant.hpp> #undef HAVE_STAT /* both boost::python and octave define HAVE_STAT... */ #include <octave/oct.h> #include <octave/Matrix.h> @@ -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<double, NDArray>( - 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<double, NDArray>( - 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<float, FloatNDArray>( - 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<float, FloatNDArray>( - 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 <class PythonPrimitive, class OctaveBase> + static PyArrayObject *create_array(const OctaveBase &octarr, + int pyarraytype, boost::true_type) { + return create_array<PythonPrimitive, OctaveBase> (octarr, pyarraytype); + } + + template <class PythonPrimitive, class OctaveBase> + static PyArrayObject *create_array(const OctaveBase &octarr, + int pyarraytype, boost::false_type) { + assert(0); + return 0; + } + template <class CLASS, size_t bytes> inline static PyArrayObject *create_uint_array(CLASS value) { if (bytes == sizeof(int)) { - return create_array<unsigned int, CLASS>(value, PyArray_UINT); + boost::integral_constant<bool, bytes==sizeof(int)> inst; + return create_array<unsigned int, CLASS>(value, PyArray_UINT, inst); } else if (bytes == sizeof(char)) { - return create_array<unsigned char, CLASS>(value, PyArray_UBYTE); + boost::integral_constant<bool, bytes==sizeof(char)> inst; + return create_array<unsigned char, CLASS>(value, PyArray_UBYTE, inst); } else if (bytes == sizeof(short)) { - return create_array<unsigned short, CLASS>(value, PyArray_USHORT); + boost::integral_constant<bool, bytes==sizeof(short)> inst; + return create_array<unsigned short, CLASS>(value, PyArray_USHORT, inst); } else { ostringstream os; os << "Numeric arrays doesn't support unsigned " << (bytes*8) @@ -176,13 +135,17 @@ template <class CLASS, size_t bytes> inline static PyArrayObject *create_sint_array(CLASS value) { if (bytes == sizeof(long)) { - return create_array<long, CLASS>(value, PyArray_LONG); + boost::integral_constant<bool, bytes==sizeof(long)> inst; + return create_array<long, CLASS>(value, PyArray_LONG, inst); } else if (bytes == sizeof(int)) { - return create_array<signed int, CLASS>(value, PyArray_INT); + boost::integral_constant<bool, bytes==sizeof(int)> inst; + return create_array<signed int, CLASS>(value, PyArray_INT, inst); } else if (bytes == sizeof(char)) { - return create_array<signed char, CLASS>(value, PyArray_SBYTE); + boost::integral_constant<bool, bytes==sizeof(char)> inst; + return create_array<signed char, CLASS>(value, PyArray_SBYTE, inst); } else if (bytes == sizeof(short)) { - return create_array<signed short, CLASS>(value, PyArray_SHORT); + boost::integral_constant<bool, bytes==sizeof(short)> inst; + return create_array<signed short, CLASS>(value, PyArray_SHORT, inst); } else { ostringstream os; os << "Numeric arrays doesn't support signed " << (bytes*8)