• Python调用C的方法


    参考:http://www.cnblogs.com/fxjwind/archive/2011/07/05/2098636.html

             http://amazingjxq.com/2012/01/09/python%E8%B0%83%E7%94%A8c%E5%87%BD%E6%95%B0/

     >>>cd /home/jxq/code/

    gcc -fPIC -shared bob_hash.c -o bob_hash.so

    然后在python里面用ctypes加载刚才的动态库。argtypes和restype分别对应函数的参数和返回值的类型。这样就可以直接调用了。


    >>> from ctypes import CDLL, c_int, c_void_p
    >>> bob_hash = CDLL('/home/jxq/code/bob_hash.so')
    >>> hash_string = bob_hash.hash_string
    >>> hash_string.argtypes = [c_void_p]
    >>> hash_string.restype = c_int
    >>> hash_string('123')

    代码:

    #include <stdio.h>
    #include <string.h>
    #define mix(a,b,c)                              
        {                                          
            a -= b; a -= c; a ^= (c >> 13);        
            b -= c; b -= a; b ^= (a << 8);          
            c -= a; c -= b; c ^= (b >> 13);        
            a -= b; a -= c; a ^= (c >> 12);        
            b -= c; b -= a; b ^= (a << 16);        
            c -= a; c -= b; c ^= (b >> 5);          
            a -= b; a -= c; a ^= (c >> 3);          
            b -= c; b -= a; b ^= (a << 10);        
            c -= a; c -= b; c ^= (b >> 15);        
        }

    unsigned int bob_hash(void *val, unsigned int length)
    {
        char *k = (char *)val;
        unsigned long a,b,c,len;

        /* Set up the internal state */
        len = length;
        a = b = c = 0x9e3779b9;  /* the golden ratio; an arbitrary value */

        /* Handle most of the key */
        while (len >= 12)
        {
            a += (k[0] +((unsigned long)k[1] << 8) +((unsigned long)k[2] << 16) +((unsigned long)k[3] << 24));
            b += (k[4] +((unsigned long)k[5] << 8) +((unsigned long)k[6] << 16) +((unsigned long)k[7] << 24));
            c += (k[8] +((unsigned long)k[9] << 8) +((unsigned long)k[10]<< 16)+((unsigned long)k[11] << 24));
            mix(a,b,c);
            k += 12; len -= 12;
        }

        /* Handle the last 11 bytes */
        c += length;
        switch(len)              /* all the case statements fall through */
        {
        case 11: c+=((unsigned long)k[10] << 24);
        case 10: c+=((unsigned long)k[9]  << 16);
        case 9 : c+=((unsigned long)k[8]  << 8);
            /* the first byte of c is reserved for the length */
        case 8 : b+=((unsigned long)k[7] << 24);
        case 7 : b+=((unsigned long)k[6] << 16);
        case 6 : b+=((unsigned long)k[5] << 8);
        case 5 : b+=k[4];
        case 4 : a+=((unsigned long)k[3] << 24);
        case 3 : a+=((unsigned long)k[2] << 16);
        case 2 : a+=((unsigned long)k[1] << 8);
        case 1 : a+=k[0];
        }
        mix(a,b,c);

        return c;
    }

    int hash_string(void *tmpstr)
    {
        int hash = (int)bob_hash(tmpstr, strlen(tmpstr));
        if(hash < 0)
        {
            hash = -hash;
        }
        return (int)(hash % 200);
    }

  • 相关阅读:
    TensorFlow 2.0 笔记(三)神经网络搭建八股
    TensorFlow 2.0 笔记(四)功能扩展
    Zabbix监控告警系统
    TensorFlow 2.0 笔记(二)神经网络优化
    TensorFlow 2.0 笔记(一)神经网络计算过程及模型搭建
    TensorFlow 2.0 笔记(五)卷积神经网络
    js手机校验正则表达式
    【ASP.NET Core】自己编程来生成自签名的服务器证书
    golang之带指针的结构体格式化打印
    go使用logrus同时输出屏幕和文件日志
  • 原文地址:https://www.cnblogs.com/li-baibo/p/3178548.html
Copyright © 2020-2023  润新知