简体   繁体   English

快速字符串数组 - Cython

[英]Fast string array - Cython

Having following hypothetical code: 有以下假设代码:

cdef extern from "string.h":
    int strcmp(char* str1, char* str2)

def foo(list_str1, list_str2):
    cdef unsigned int i, j
    c_arr1 = ??
    c_arr2 = ??
    for i in xrange(len(list_str1)):
        for j in xrange(len(list_str2)):
            if not strcmp(c_arr1[i], c_arr2[j]):
                do some funny stuff

is there some way how to convert the lists to c arrays? 有没有办法将列表转换为c数组?

I have read and tried Cython - converting list of strings to char ** but that only throws errors. 我已经阅读并尝试过Cython - 将字符串列表转换为char **,但这只会引发错误。

Try following code. 请尝试以下代码。 to_cstring_array function in the following code is what you want. to_cstring_array函数在下面的代码中就是你想要的。

from libc.stdlib cimport malloc, free
from libc.string cimport strcmp
from cpython.string cimport PyString_AsString

cdef char ** to_cstring_array(list_str):
    cdef char **ret = <char **>malloc(len(list_str) * sizeof(char *))
    for i in xrange(len(list_str)):
        ret[i] = PyString_AsString(list_str[i])
    return ret

def foo(list_str1, list_str2):
    cdef unsigned int i, j
    cdef char **c_arr1 = to_cstring_array(list_str1)
    cdef char **c_arr2 = to_cstring_array(list_str2)

    for i in xrange(len(list_str1)):
        for j in xrange(len(list_str2)):
            if i != j and strcmp(c_arr1[i], c_arr2[j]) == 0:
                print i, j, list_str1[i]
    free(c_arr1)
    free(c_arr2)

foo(['hello', 'python', 'world'], ['python', 'rules'])

If you're on Python 3, here's an update to @falsetru's answer (untested on Python 2). 如果你使用的是Python 3,这里是对@fattru的答案的更新(在Python 2上未经测试)。

cdef extern from "Python.h":
    char* PyUnicode_AsUTF8(object unicode)

from libc.stdlib cimport malloc, free
from libc.string cimport strcmp

cdef char ** to_cstring_array(list_str):
    cdef char **ret = <char **>malloc(len(list_str) * sizeof(char *))
    for i in xrange(len(list_str)):
        ret[i] = PyUnicode_AsUTF8(list_str[i])
    return ret

def foo(list_str1, list_str2):
    cdef unsigned int i, j
    cdef char **c_arr1 = to_cstring_array(list_str1)
    cdef char **c_arr2 = to_cstring_array(list_str2)

    for i in range(len(list_str1)):
        for j in range(len(list_str2)):
            if i != j and strcmp(c_arr1[i], c_arr2[j]) == 0:
                print(i, j, list_str1[i])
    free(c_arr1)
    free(c_arr2)

foo(['hello', 'python', 'world'], ['python', 'rules'])

Warning: The pointer returned by PyUnicode_AsUTF8 is cached in the parent unicode-object. 警告: PyUnicode_AsUTF8返回的指针缓存在父unicode-object中。 Which has two consequences: 这有两个后果:

  1. this pointer is only valid as long as the parent unicode-object is alive. 只要父unicode-object处于活动状态,此指针才有效。 Accessing it afterwards leads to undefined behavior (eg possible segmentation fault). 之后访问它会导致未定义的行为(例如可能的分段错误)。
  2. The caller of the PyUnicode_AsUTF8 isn't responsible for the freeing the memory. PyUnicode_AsUTF8的调用者负责释放内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM