[Numpy-discussion] Dealing with types in extension modules

Travis E. Oliphant oliphant at enthought.com
Wed Sep 10 22:21:20 EDT 2008


Lane Brooks wrote:
> When writing an numpy extension module, what is the preferred way to 
> deal with the all the possible types an ndarray can have?
>
> I have some data processing functions I need to implement and they need 
> to be generic and work for all the possible numerical dtypes.  I do not 
> want to have to re-implement the same C-code for all the possible types, 
> so the way I approached it was to use a C++ template function to 
> implement the processing.  Then I have a dispatching function that 
> checks the type of the input ndarray and calls the correct template.  Is 
> there a better way?
>   
You could store the functions in an array of function pointers and 
look-up the correct one using the typenum:

resize_funcs[PyArray_Type(buf1)](PyArray_DATA(bufi))

with resize_funcs filled appropriately.

-Travis




More information about the NumPy-Discussion mailing list