[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Discuss-gnuradio] vectors of vectors in python
From: |
Eric Blossom |
Subject: |
Re: [Discuss-gnuradio] vectors of vectors in python |
Date: |
Wed, 7 Feb 2007 08:26:29 -0800 |
User-agent: |
Mutt/1.5.9i |
On Wed, Feb 07, 2007 at 10:31:05AM -0500, Michael Dickens wrote:
> On Feb 1, 2007, at 11:47 PM, Achilleas Anastasopoulos wrote:
> >private:
> > std::vector< std::vector<int> > d_PS;
> >public:
> > const std::vector< std::vector<int> > & PS () const { return d_PS; }
>
> Here's a quick, partial answer. According to the SWIG docs &
> tutorials, what one needs to do is something like (these could be in
> struct's of class'es instead):
>
> typedef std::vector<int> VecInt;
> typedef std::vector<VecInt> VecInt2;
>
> Then in Python (after compilation via SWIG -> C++), something like:
>
> >>> import VecStuff
> >>> a = VecInt ([1 2 3])
> >>> b = VecInt ([4 5 6])
> >>> c = VecInt2 (a, b)
>
> Please note that 'a' and 'b' above do -not- need to be the same
> length, not for C++ nor SWIG nor Python, since this is not a "matrix"
> but just a vector of vectors.
>
> The exact details remain to be figured out. The basic idea is that,
> in order for SWIG (and thus Python) to know what type of structure is
> being dealt with, one has to name all of the relevant parts of the
> structure. Thus
>
> std::vector< std::vector<int> > V2Int;
>
> won't work because SWIG can't name the internal "std::vector<int>"
> data. SWIG takes the approach of "separate and conquer". Hope this
> helps! - MLD
Note that std::vector<int>, <gr_complex>, <float> and more are already
handled in gnuradio.i
You may be missing only a template instantiate in the .i file to have
const std::vector< std::vector<int> >
work.
You may want to call it
const std::vector< const std::vector<int> >
In that case, swig may just "do the right thing" and automatically
generate the correct typemap since it's free to create new copies of
the nested vectors. I would expect (but haven't tested) that this
would allow you to call from python using syntax like this:
foo(((1,2,3), (4,5,6), (7,8,9,10)))
Eric