29 views
Skip to first unread message

Ian Bell

unread,
Nov 9, 2012, 6:56:22 AM11/9/12
to cython...@googlegroups.com
I posted this to sourceforge as well, any ideas?

I have written a function in cython that will search a STL vector of strings for a given string and return true if it is found, false otherwise. Performance is very important here! I would ideally like to have a templated function to do the same thing for vectors of integers or double values so I don't have to write a function for each data type. I am sure this is possible but I don't know the cython syntax for templated functions. (I know how to do it in c++)

from libcpp cimport bool
from libcpp.string cimport string
from libcpp.vector cimport vector
from cython.operator cimport dereference as deref, preincrement as inc

cpdef bool is_in_vector(string a, vector[string] v):
    cdef vector[string].iterator it = v.begin()
    while it != v.end():
        if deref(it) == a:
            return True
        #Increment iterator
        inc(it)
    return False

Thanks,
Ian

Robert Bradshaw

unread,
Nov 9, 2012, 11:57:35 AM11/9/12
to cython...@googlegroups.com
Check out http://docs.cython.org/src/userguide/fusedtypes.html

Note that vector[bool] is a very different specialization C++ than all
the other vector types.

- Robert
Reply all
Reply to author
Forward
0 new messages