Created
July 29, 2014 23:20
-
-
Save lebedov/e51c2db540469065a98e to your computer and use it in GitHub Desktop.
How to use function pointers with fused types in Cython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Demo of how to use function pointers with fused types in Cython: | |
| cimport cython | |
| ctypedef fused fused_type: | |
| cython.double | |
| cython.longlong | |
| cdef double func0(double x, double y): | |
| return x+y | |
| cdef long long func1(long long x, long long y): | |
| return x+y | |
| ctypedef fused_type (*FUNC)(fused_type x, fused_type y) | |
| def foo(fused_type x, fused_type y): | |
| cdef FUNC f | |
| if fused_type == cython.double: | |
| print 'double' | |
| f = func0 | |
| else: | |
| print 'long' | |
| f = func1 | |
| return f(x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment