I have a C++ library which currently has some methods inside which return a std::vector
defined like
public:
const std::vector<uint32_t>& getValues() const;
I'm currently working on wrapping the whole library for Python using SWIG and this is working well so far.
SWIG wraps this getValues()
function fine such that it returns a Python tuple. The issue is in my Python-side code I want to convert this to a NumPy array. Of course I can do this by:
my_array = np.array(my_object.getValues(), dtype='uint32')
but this causes all the entries in the original vector to be first copied into a Python tuple by SWIG and then again into a numpy array by me. Since this vector could be very large, I'd rather avoid making these two copies and would like for a way to have SWIG create a numpy.array wrapper around the original vector data in memory.
I've read the documentation for numpy.i but that explicitly mentions that output arrays are not supported since they seem to be working under the assumption of C-style arrays rather than C++ vectors.
numpy.array's underlying data structure is just a C-style array as is a C++ std::vector so I would hope that it is feasible to have then access the same data in memory.
Is there any way to make SWIG return a numpy.array which doesn't copy the original data?