I am trying to write a piece of code in C++ which can produce an array and return it as as a Python list. I understand that I can return the list as a NumPy array using the typemaps in numpy.i instead, but NumPy is not installed on some of the clusters I am using. The main difference between this question and some similar ones I've seen on here is that I want to return a C array of variable length as a Python list.
The stripped down C++ routine is below:
/* File get_rand_list.cpp */
#include "get_rand_list.h"
/* Define function implementation */
double* get_rand_list(int length) {
output_list = new double[length];
/* Populate input NumPy array with random numbers */
for (int i=0; i < length; i++)
output_list[i] = ((double) rand()) / RAND_MAX;
return output_list;
}
I would like to be able to use this in Python as so:
from get_rand_list import *
list = get_rand_list(10)
Which would return a Python list of 10 random floats. How can I use SWIG typemaps to wrap the C++ routine to do this? If there is a simpler way which requires some small modifications to the C++ routine that is most likely fine.