Travis E. Oliphant wrote:
Lane Brooks wrote:
Travis E. Oliphant wrote:
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
Would this require implementing a unique function for each of the possible types, though? That is mostly what I want to avoid. I do not want to have to implement 10 to 15 different functions that all do the same exact thing but to different types of data. I guess with your proposal I can still use templates to have a single function definition.
You could have a default function which does type coercion or uses type-indifferent code. It really depends on what you are doing.

But generally if you want to support multiple types you have to repeat the code for that type, I don't know of anyway around that.

Also, you don't have to fill in all the functions (some could be NULL and you could use coercion to another type or some other default implementation as needed).

-Travis

Is such an approach preferred to templates? I think duplicating the code for different types sound tedious and error-prone.

What is type-indifferent code? Is there a way, for example, to add or multiple two variables in a type-indifferent way?

Thanks,
Lane
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to