dieter h wrote:
> Hi all,
> 
> What would be an 'elegant' way to handle dynamic casting?
> 
> For instance, I have a numpy.recarray.data buffer (char*) that I want
> to iterate (pointer arithmetic) and manipulate in cython. What would
> be an elegant way to dynamically cast the char* to the type of the
> heterogeneous elements? Obvious to say, these are unknown at compile
> time.

I would say that is highly unobvious, in most cases you know the type?

Make sure to chech whether you have a contiguous array:

if not arr.flags['C_CONTIGUOUS']: raise ...

> Utilizing tokenization in a C macro is all that I've thought up so
> far, considering I'm a self-taught, still studying student of CS. :)

Not sure if I know what you mean...

All together I know too little about the problem you have to give a good 
answer (is speed or clarity most important? And so on), and also this is 
a generic programming question and not at all Cython-related.

Assuming what you want to do is converting the recarray to a Python 
representation (to make an example -- what do you really do if you do 
not know the types?), what I'd do is create classes for reading each type:

cdef class ReadNext:
     cdef object read_next(char** pptr): pass

cdef class ReadInt:
     cdef object read_next(char** pptr):
         int** typed = pptr
         result = **pptr
         *pptr += 1 # Increment the "reading-head" you use

... repeat for all types ...

cdef class ReadStruct:
     cdef __init__(self, list_of_sub_readers): ...
     cdef object read_next(char** pptr):
         ...iterate subreaders to do your stuff and return a tuple ...


Then, "parse" the dtype of the recarray to construct a tree of these 
readers (that's only done once per array, so consider it O(1)), and then 
you simply feed your buffer to the root reader.

Lots of polymorphic dispatches though, but unless you know the type at 
compile-time... I suppose using if-tests/switches on the type *might* be 
faster, but I'm no C/performance expert on that level, and this looks a 
lot cleaner.

-- 
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to