一些字符串哈希算法

unsigned int hash_BPHash(unsigned char *str){
    unsigned int hash = 0;
    while(*str){
        hash = (hash << 7) ^ (*str++);
    }
    return hash;
}

unsigned int hash_FNVHash(unsigned char *str){
    unsigned int hash = 0;
    while(*str){
        hash = (hash * 0x811C9DC5) ^ (*str++);
    }
    return hash;
}

unsigned int hash_BKDRHash(unsigned char *str){
    unsigned int hash = 0;
    while(*str){
        hash = (hash * 131) + (*str++); // 31 131 1313 13131 131313 etc...
    }
    return hash;
}

unsigned int hash_DJBHash(unsigned char *str){
    unsigned int hash = 5381;
    while(*str){
        hash += ((hash << 5) + (*str++));
    }
    return hash;
}

unsigned int hash_JSHash(unsigned char *str){
    unsigned int hash = 1315423911;
    while(*str){
        hash ^= ((hash << 5) + (hash >> 2) + (*str++));
    }
    return hash;
}

unsigned int hash_DEKHash(unsigned char *str){
    unsigned int hash = strlen(str);
    while(*str){
        hash = ((hash << 5) ^ (hash >> 27)) ^ (*str++);
    }
    return hash;
}

unsigned int hash_SDBMHash(unsigned char *str){
    unsigned int hash = 0;
    while(*str){
        hash = (hash << 6) + (hash << 16) + (*str++) - hash;
    }
    return hash;
}

unsigned int hash_RSHash(unsigned char *str){
    unsigned int a = 63689;
    unsigned int hash = 0;
    while(*str){
        hash = hash * a + (*str++);
        a *= 378551;
    }
    return hash;
}

unsigned int hash_APHash(unsigned char *str){
    unsigned int hash = 0;   //0x00000000, 0xAAAAAAAA
    unsigned int flag = 0;
    while(*str){
        if(flag&1){
            hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
        }
        else{
            hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
        }
        flag++;
    }
    return hash;
}

unsigned int hash_PJWHash(unsigned char *str){
    unsigned int hash = 0;
    unsigned int test = 0;
    while(*str){
        hash = (hash << 4) + (*str++);
        if((test = hash & 0xF0000000) != 0){
            hash = ((hash ^ (test >> 24)) & 0x0FFFFFFF);
        }
    }
    return hash;
}

unsigned int hash_ELFHash(unsigned char *str){
    unsigned int hash = 0;
    unsigned int test = 0;
    while(*str){
        hash = (hash << 4) + (*str++);
        if((test = hash & 0xF0000000) != 0){
            hash = ((hash ^ (test >> 24)) & (~test));
        }
    }
    return hash;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值