From: Bob H. <bh...@co...> - 2011-07-29 00:50:01
|
On 7/27/2011 7:03 PM, Bob Hood wrote: > On 7/26/2011 2:33 PM, Stefan Zager wrote: >> The problem is that you haven't defined indexing operators for type double[]. > > Hi, Stefan. Thanks for the reply. > > Hmm, this isn't very clear to me. I thought SWIG generated that sort of thing > automatically when it found a <type>[<size>] declaration. Is there some > example to which you can point me in the SWIG distribution (or documentation) > that illustrates this? Or, if not, could you perhaps give me a brief example? > > I really appreciate your time. I have addressed this (I guess) with an "out" typemap, for example: %typemap(out) double [ANY] { $result = PyList_New($1_dim0); for(int i = 0; i < $1_dim0; i++) { PyObject *o = PyFloat_FromDouble((double)$1[i]); PyList_SetItem($result, i, o); } } But this seems like a really inefficient approach. It means that Python code like this: for i in range(3): ms.a1[i] = ms.a1[i] * (i * i) is going to be copying and releasing whole lists at a time (assuming it even works to begin with). I tried extending the class with my own "get" and "set" methods: %extend myStruct { double a1_get(int i) { return self->a1[i]; } void a1_set(int i, double v) { self->a1[i] = v; } } But that ended in disaster (they're already defined for the class). Is there a better way to do this that I'm missing, one that just accesses, and returns, the individual elements of the wrapped double*, instead of the entire array each time? Or is the whole-list-each-time approach as good as it gets? |