mass 幸运哈希彩竞猜游戏娱乐平台开发技术详解
多种哈希算法代码,用于文件校验、简单加密等场合。
哈希表也称作散列表,叫法不同,是一个意思。这种数据结构提供了键值对的映射关系,给出键就可以快速得到对应的值,比如上面提到的"50 号"就是键,游戏机就是键得到的值。时间复杂度接近 O(1)。哈希表是如何根据键来得到值的呢?我们来揭秘这个过程。
本文由小编 hkkf5566 整理编辑发布,哈希娱乐游戏平台项目开发对接薇 hkkf5566
内容介绍
Copyright 2008-2015, www.xxx.comFile name : HashFunction.hAuthor :
Version : V00R001Date :
Description : Hash 函数集合,包含主流的 hash 函数 Struct List :Function list:History :
Date : 2008-10-25Create :
Modification : create
*/#include "HashFunction.h"#define ngx_hash(key, c) ((u_int) key * 31 + c)
unsigned int ngx_hash_key(const std::string& data){size_t len = data.size();unsigned int i, key;key = 0; for (i = 0; i < len; i++) {key = ngx_hash(key, data[i]);} return key;}//经典字符串 Hash 函数 unsigned int pub_inthash(const char *str){register unsigned int h;register unsigned char *p;
for(h=0, p = (unsigned char *)str; *p ; p++)h = 31 * h + *p;return h;}
//PHP 中出现的字符串 Hash 函数 unsigned long php_longhashpjw(const char* str){unsigned long h = 0, g;char *p;for(h=0, p = (char *)str; *p ; p++){h = (h << 4) + *p++;if ((g = (h & 0xF0000000))){h = h ^ (g >> 24);h = h ^ g;}} return h;}
//OpenSSL 中出现的字符串 Hash 函数 unsigned int OpenSSL_strhash1(const char *str){int i,l;unsigned int ret=0;unsigned short *s;
}
//MySql 中出现的字符串 Hash 函数 unsigned int mysql_hashnr1(const char key,unsigned int length){register unsigned int nr=1, nr2=4;while (length--){nr^= (((nr & 63)+nr2)((unsigned int) (unsigned char) *key++))+ (nr << 8);nr2+=3;}return((unsigned int) nr);}
unsigned int mysql_hashnr_caseup1(const char key,unsigned int length){register unsigned int nr=1, nr2=4;while (length--){nr^= (((nr & 63)+nr2)((unsigned int) (unsigned char) toupper(*key++)))+ (nr << 8);nr2+=3;}return((unsigned int) nr);}
unsigned int mysql_hashnr2(const char *key, unsigned int len){const char end=key+len;unsigned int hash;for (hash= 0; key < end; key++){hash= 16777619;hash^= (unsigned int) (unsigned char) key;}return (hash);}
unsigned int mysql_hashnr_caseup2(const char *key, unsigned int len){const char end=key+len;unsigned int hash;for (hash= 0; key < end; key++){hash= 16777619;hash^= (unsigned int) (unsigned char) toupper(*key);}return (hash);}
/**
加法 hash
@param key 字符串
@param prime 一个质数
@return hash 结果/int additiveHash(const std::string& key, int prime){unsigned int hash, i; for (hash = key.size(), i = 0; i < key.size(); i++)hash += key[i]; return (hash % prime);}/*
旋转 hash
@param key 输入字符串
@param prime 质数
@return hash 值/int rotatingHash(const std::string& key, int prime){unsigned int hash, i; for (hash=key.size(), i=0; i<key.size(); ++i)hash = (hash<<4)^(hash>>28)^key[i]; return (hash % prime);// return (hash ^ (hash>>10) ^ (hash>>20));}/*
一次一个 hash
@param key 输入字符串
@return 输出 hash 值/int oneByOneHash(const std::string& key){unsigned int hash, i; for (hash=0, i=0; i<key.size(); ++i){hash += key[i];hash += (hash << 10);hash ^= (hash >> 6);}hash += (hash << 3);hash ^= (hash >> 11);hash += (hash << 15);// return (hash & M_MASK);return hash;}/*
Bernstein's hash
@param key 输入字节数组
@param level 初始 hash 常量
@return 结果 hash/int bernstein(const std::string& key){unsigned int hash = 0;unsigned int i; for (i=0; i<key.size(); ++i) hash = 33hash + key[i]; return hash;}/**
RS 算法 hash
@param str 字符串/unsigned int RSHash(const std::string& str){unsigned int b = 378551;unsigned int a = 63689;unsigned int hash = 0; for(unsigned int i = 0; i < str.size(); i++){hash = hash * a + str[i];a = a * b;} return (hash & 0x7FFFFFFF);}/*
JS 算法/int JSHash(const std::string& str){ int hash = 1315423911; for(unsigned int i = 0; i < str.size(); i++){hash ^= ((hash << 5) + str[i] + (hash >> 2));} return (hash & 0x7FFFFFFF);}/*
PJW 算法/int PJWHash(const std::string& str){ int BitsInUnsignedInt = 32; int ThreeQuarters = (BitsInUnsignedInt * 3) / 4; int OneEighth = BitsInUnsignedInt / 8; int HighBits = 0xFFFFFFFF << (BitsInUnsignedInt - OneEighth); int hash = 0; int test = 0; for(unsigned int i = 0; i < str.size();i++){hash = (hash << OneEighth) + str[i]; if((test = hash & HighBits) != 0){hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));}} return (hash & 0x7FFFFFFF);}/ End Of P. J. Weinberger Hash Function //*
ELF 算法/int ELFHash(const std::string& str){ int hash = 0; int x = 0; for(unsigned int i = 0; i < str.size(); i++){hash = (hash << 4) + str[i]; if((x = (int)(hash & 0xF0000000L)) != 0){hash ^= (x >> 24);hash &= ~x;}} return (hash & 0x7FFFFFFF);}/ End Of ELF Hash Function //*
BKDR 算法/uint32_t BKDRHash32(const std::string& str){ uint32_t seed = 131; // 31 131 1313 13131 131313 etc..uint32_t hash = 0; for(unsigned int i = 0; i < str.size(); ++i){hash = (hash * seed) + str[i];} return (uint32_t)hash;}uint64_t BKDRHash64(const std::string& str){ uint64_t seed = 131; // 31 131 1313 13131 131313 etc..uint64_t hash = 0; for(unsigned int i = 0; i < str.size(); ++i){hash = (hash * seed) + str[i];} return (uint64_t)hash;}/ End Of BKDR Hash Function //*
SDBM 算法/int SDBMHash(const std::string& str){ int hash = 0; for(unsigned int i = 0; i < str.size(); i++){hash = str[i] + (hash << 6) + (hash << 16) - hash;} return (hash & 0x7FFFFFFF);}/ End Of SDBM Hash Function //*
DJB 算法/unsigned int DJBHash(const std::string& str){unsigned int hash = 5381; for(unsigned int i = 0; i < str.size(); i++){hash = ((hash << 5) + hash) + str[i];} return (hash & 0x7FFFFFFF);}/ End Of DJB Hash Function //*
DEK 算法/int DEKHash(const std::string& str){ int hash = str.size(); for(unsigned int i = 0; i < str.size(); i++){hash = ((hash << 5) ^ (hash >> 27)) ^ str[i];} return (hash & 0x7FFFFFFF);}/ End Of DEK Hash Function //*
AP 算法/int APHash(const std::string& str){ int hash = 0; for(unsigned int i = 0; i < str.size(); i++){hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str[i] ^ (hash >> 3)) :(~((hash << 11) ^ str[i] ^ (hash >> 5)));}// return (hash & 0x7FFFFFFF);return hash;}/ End Of AP Hash Function //*
JAVA 自己带的算法*/int java(const std::string& str){ int h = 0; int off = 0;unsigned int len = str.size(); for (unsigned int i = 0; i < len; i++){h = 31 * h + str[off++];} return h;}复制
评论