On Wed, 2012-07-11 at 09:23 -0700, Robert Bradshaw wrote:
>
> On Tue, Jul 10, 2012 at 9:18 AM, Henry Gomersall <
he...@cantab.net>
> wrote:
> > On Tue, 2012-07-10 at 17:07 +0100, Henry Gomersall wrote:
> >>
> >> Is there some way in which I can create a numpy dtype that has the
> >> correct integer type in order to put an enum value into it.
> >
> > So, the solution I have come up with is as follows:
> >
> > enum_dtype = np.dtype('u'+sizeof(MY_ENUM_TYPE))
> >
> > Does this seem reasonable?
>
> It's possible that your enum type is signed, so you might want to use
> 'i' (though this could have the same issues for the top of an unsigned
> enum's range (but this might not actually matter because the cast to
> and from the wrong signed type should preserve the value)). It looks
> like the type of int used for an enum is implementation dependent, and
> may even be a function of the enumerated enum values.
>
The signed/unsigned issue became apparent to me in that half sleep state
just before one drifts off yesterday evening. What you say makes sense.
> If N is not large, you could just use i8, or even int with an assert
> in
> your module that sizeof(MY_ENUM_TYPE) <= sizeof(int) (which should
> typically be
> true unless your enum has very large values in it). This may be less
> memory-efficient, but would have the advantage of being able to
> specify the type at compile time.
It's actually the case that I'm wrapping a lib in which the enums are
already defined. The purpose of using a numpy dtype is to create an
array of structures, in which the memory management is handled for me,
and in which I get the niceties of numpy (getting the size of the array
and so on). I then just cast the pointer to the buffer to be a struct
pointer and use that as needed.
It strikes me this must be a pretty common problem? To the point that
there could/should be a way in which C arrays of any type (including
structs) can be created trivially and presented as a python object
(which is just a presentation of the underlying buffer). I'm not saying
Numpy is the way to do this, but it does seem to possess the necessary
machinery. (If something like this is already implemented, I'd really
appreciate knowing how to use it :)
Cheers,
Henry