问题描述
VB.NET常用的哈希算法集.其中包括了著名的暴雪的哈希,T33哈希.......全部是网上的C/C++代码改的(VB.NET的资源真的很少).不同的哈希算法在分布式,布降过滤器,位图MAP等等应用得比较多...''</summary>PublicClassMyUnchecked#Region"UInt64"<StructLayout(LayoutKind.Explicit)>PublicStructureUncheckedUInt64<FieldOffset(0)>PrivatelongValueAsUInt64<FieldOffset(0)>PrivateintValueLoAsUInt32<FieldOffset(4)>PrivateintValueHiAsUInt32'//PrivateSubNew(newLongValueAsUInt64)longValue=newLongValueEndSubPublicOverloadsSharedWideningOperatorCType(valueAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(value)EndOperatorPublicOverloadsSharedWideningOperatorCType(valueAsUncheckedUInt64)AsUInt64Returnvalue.longValueEndOperatorPublicOverloadsSharedOperator*(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue*y)EndOperatorPublicOverloadsSharedOperator/(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue/y)EndOperatorPublicOverloadsSharedOperator^(xAsUncheckedUInt64,yAsDouble)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue^y)EndOperatorPublicOverloadsSharedOperator^(xAsUncheckedUInt64,yAsUInt32)AsUncheckedUInt64ReturnNewUncheckedUInt64(MyConvert.MyPower(x.longValue,y))EndOperatorPublicOverloadsSharedOperatorXor(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValueXory)EndOperatorPublicOverloadsSharedOperator+(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue+y)EndOperatorPublicOverloadsSharedOperator-(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue-y)EndOperatorPublicOverloadsSharedOperator<<(xAsUncheckedUInt64,yAsInt32)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue<<y)EndOperatorPublicOverloadsSharedOperator>>(xAsUncheckedUInt64,yAsInt32)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue>>y)EndOperatorPublicOverloadsSharedOperatorAnd(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValueAndy)EndOperatorPublicOverloadsSharedOperator=(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue)EndOperatorPublicOverloadsSharedOperator<>(xAsUncheckedUInt64,yAsUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(x.longValue<>y)EndOperatorPublicOverloadsSharedOperatorNot(xAsUncheckedUInt64)AsUncheckedUInt64ReturnNewUncheckedUInt64(Notx.longValue)EndOperatorEndStructure#EndRegionEndClass
解决方案
解决方案二:
#Region"哈希算法"'''<summary>'''和HashCMyMap基本一样.'''</summary>'''<paramname="Key"></param>'''<returns></returns>PublicSharedFunctionHashDJB(KeyAsString)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=5381DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayForiAsInt32=0ToLhash=((hash<<5)+hash)+Asc(KeyCharArr(i))+3NextReturnhashEndFunction'''<summary>'''和HashCMyMap基本一样.'''</summary>'''<paramname="KeyByte"></param>'''<returns></returns>PublicSharedFunctionHashDJB(ByValKeyByte()AsByte)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=5381DimLAsInt32=KeyByte.Length-1ForiAsInt32=0ToLhash=((hash<<5)+hash)+KeyByte(i)+3NextReturnhashEndFunction'''<summary>'''BKDR哈希'''</summary>'''<paramname="Key"></param>'''<paramname="seed">种子.最好是使用质数.</param>'''<returns></returns>PublicSharedFunctionHashBKDR(ByValKeyAsString,OptionalseedAsLong=131)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayForiAsInt32=0ToL-1hash=(hash*seed)+Asc(KeyCharArr(i))+3NextReturn(hashAnd&H7FFFFFFF)EndFunction'''<summary>'''BKDR哈希'''</summary>'''<paramname="KeyByte"></param>'''<paramname="seed">种子数</param>'''<returns></returns>PublicSharedFunctionHashBKDR(ByValKeyByte()AsByte,OptionalseedAsLong=131)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=KeyByte.Length-1ForiAsInt32=0ToL-1hash=(hash*seed)+KeyByte(i)+3NextReturn(hashAnd&H7FFFFFFF)EndFunctionPublicSharedFunctionHashRS(KeyAsString,OptionalseedAsLong=131)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=0DimbAsUInt64=378551DimaAsUInt64=63689DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayForiAsInt32=0ToL-1hash=(hash*a)+Asc(KeyCharArr(i))a=a*bNextReturn(hashAnd&H7FFFFFFF)EndFunctionPublicSharedFunctionHashRS(KeyByte()AsByte,OptionalseedAsLong=131)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=0DimbAsUInt64=378551DimaAsUInt64=63689DimLAsInt32=KeyByte.Length-1ForiAsInt32=0ToL-1hash=(hash*a)+KeyByte(i)a=a*bNextReturn(hashAnd&H7FFFFFFF)EndFunction
解决方案三:
PublicSharedFunctionHashSDBM(KeyAsString)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayForiAsInt32=0ToL-1hash=Asc(KeyCharArr(i))+(hash<<6)+(hash<<16)-hashNextReturn(hashAnd&H7FFFFFFF)EndFunctionPublicSharedFunctionHashSDBM(KeyByte()AsByte)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=KeyByte.Length-1ForiAsInt32=0ToL-1hash=KeyByte(i)+(hash<<6)+(hash<<16)-hashNextReturn(hashAnd&H7FFFFFFF)EndFunctionPublicSharedFunctionHashJS(KeyAsString)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=1315423911DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayForiAsInt32=0ToL-1hash=hashXor(((hash<<5)+Asc(KeyCharArr(i))+(hash>>2)))NextReturnhashEndFunctionPublicSharedFunctionHashJS(KeyByte()AsByte)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=1315423911DimLAsInt32=KeyByte.Length-1ForiAsInt32=0ToL-1hash=hashXor(((hash<<5)+KeyByte(i)+(hash>>2)))NextReturnhashEndFunctionPublicSharedFunctionHashPJW(KeyAsString)AsUInt64DimBitsInUnsignedIntAsUInt64=CLng(4*8)DimThreeQuartersAsUInt64=CLng((BitsInUnsignedInt*3)/4)DimOneEighthAsUInt64=CLng(BitsInUnsignedInt/8)DimHighBitsAsUInt64=CLng(&HFFFFFFFF)<<(BitsInUnsignedInt-OneEighth)DimhashAsMyUnchecked.UncheckedUInt64=0DimtestAsUInt64=0DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayForIAsInt32=0ToLhash=(hash<<OneEighth)+Asc(KeyCharArr(I))If((test=hashAndHighBits)<>0)Thenhash=((hashXor(test>>ThreeQuarters))And(NotHighBits))EndIfNextReturnhashEndFunctionPublicSharedFunctionHashPJW(KeyByte()AsByte)AsUInt64DimBitsInUnsignedIntAsUInt64=CLng(4*8)DimThreeQuartersAsUInt64=CLng((BitsInUnsignedInt*3)/4)DimOneEighthAsUInt64=CLng(BitsInUnsignedInt/8)DimHighBitsAsUInt64=CLng(&HFFFFFFFF)<<(BitsInUnsignedInt-OneEighth)DimhashAsMyUnchecked.UncheckedUInt64=0DimtestAsUInt64=0DimLAsInt32=KeyByte.Length-1ForIAsInt32=0ToLhash=(hash<<OneEighth)+KeyByte(I)If((test=hashAndHighBits)<>0)Thenhash=((hashXor(test>>ThreeQuarters))And(NotHighBits))EndIfNextReturnhashEndFunctionPublicSharedFunctionHashAP(KeyAsString)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=&HAAAAAAAADimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayForiAsInt32=0ToLIf((iAnd1)=0)Thenhash=hashXor(((hash<<7)XorAsc(KeyCharArr(i))*(hash>>3)))Elsehash=hashXor((Not((hash<<11)+Asc(KeyCharArr(i))Xor(hash>>5))))EndIfNextReturnhashEndFunctionPublicSharedFunctionHashAP(KeyByte()AsByte)AsUInt64DimhashAsMyUnchecked.UncheckedUInt64=&HAAAAAAAADimLAsInt32=KeyByte.Length-1ForiAsInt32=0ToLIf((iAnd1)=0)Thenhash=hashXor(((hash<<7)XorKeyByte(i)*(hash>>3)))Elsehash=hashXor((Not((hash<<11)+KeyByte(i)Xor(hash>>5))))EndIfNextReturnhashEndFunctionPublicSharedFunctionHashDEK(KeyAsString)AsUInt64DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayDimhashAsMyUnchecked.UncheckedUInt64=L+1ForiAsInt32=0ToLhash=((hash<<5)Xor(hash>>27))XorAsc(KeyCharArr(i))NextReturnhashEndFunctionPublicSharedFunctionHashDEK(KeyByte()AsByte)AsUInt64DimLAsInt32=KeyByte.Length-1DimhashAsMyUnchecked.UncheckedUInt64=L+1ForiAsInt32=0ToLhash=((hash<<5)Xor(hash>>27))XorKeyByte(i)NextReturnhashEndFunctionPublicSharedFunctionHashELF(KeyAsString)AsUInt64DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayDimhashAsMyUnchecked.UncheckedUInt64=0DimxAsLong=0ForiAsInt32=0ToLhash=(hash<<4)+Asc(KeyCharArr(i))x=hashAnd&HF0000000LIfx<>0Thenhash=hashXor(x>>24)EndIfhash=hashAnd(Notx)NextReturnhashEndFunctionPublicSharedFunctionHashELF(KeyByte()AsByte)AsUInt64DimLAsInt32=KeyByte.Length-1DimhashAsMyUnchecked.UncheckedUInt64=0DimxAsLong=0ForiAsInt32=0ToLhash=(hash<<4)+KeyByte(i)x=hashAnd&HF0000000LIfx<>0Thenhash=hashXor(x>>24)EndIfhash=hashAnd(Notx)NextReturnhashEndFunctionPrivateSharedcryptTable(&H100*5-1)AsUInt64PrivateSharedIsInitcryptTableAsBoolean=FalsePublicSharedSubHashBlizzardInit()DimseedAsUInt64=&H100001Dimindex1AsUInt64=0Dimindex2AsUInt64=0DimIAsUInt64DimKKKAsUInt64=0Forindex1=0To&H100-1index2=index1ForI=0To4Dimtemp1,temp2AsUInt64seed=(seed*125+3)Mod&H2AAAABtemp1=(seedAnd&HFFFF)<<&H10seed=(seed*125+3)Mod&H2AAAABtemp2=(seedAnd&HFFFF)cryptTable(index2)=(temp1Ortemp2)'//|index2+=&H100NextNextIsInitcryptTable=TrueEndSub'''<summary>'''暴雪公司出名的哈希码.'''测试了二千万GUID,没有重复.但运算量比较大。'''</summary>'''<paramname="key"></param>'''<paramname="HasType">HasType=0,1,2</param>'''<returns></returns>PublicSharedFunctionHashBlizzard(ByValKeyAsString,OptionalHasTypeAsLong=0)AsUInt64IfIsInitcryptTable=FalseThenHashBlizzardInit()DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayDimseed1AsMyUnchecked.UncheckedUInt64=&H7FED7FEDDimseed2AsMyUnchecked.UncheckedUInt64=&HEEEEEEEEDimLoopIDAsInt32=0While(LoopID<L)DimascCodeAsInt32=Asc(KeyCharArr(LoopID))seed1=cryptTable((HasType<<8)+ascCode)Xor(seed1+seed2)seed2=ascCode+seed1+seed2+(seed2<<5)+3LoopID+=1EndWhileReturnseed1EndFunction'''<summary>'''暴雪公司著名的HashMap.'''测试了二千万GUID,没有重复.但运算量比较大。'''</summary>'''<paramname="KeyByte"></param>'''<paramname="HasType">HasType=[0,1,2]</param>'''<returns></returns>PublicSharedFunctionHashBlizzard(KeyByte()AsByte,OptionalHasTypeAsLong=0)AsUInt64IfIsInitcryptTable=FalseThenHashBlizzardInit()DimLAsInt32=KeyByte.Length-1Dimseed1AsMyUnchecked.UncheckedUInt64=&H7FED7FEDDimseed2AsMyUnchecked.UncheckedUInt64=&HEEEEEEEEDimLoopIDAsInt32=0While(LoopID<L)DimascCodeAsInt32=KeyByte(LoopID)seed1=cryptTable((HasType<<8)+ascCode)Xor(seed1+seed2)seed2=ascCode+seed1+seed2+(seed2<<5)+3LoopID+=1EndWhileReturnseed1EndFunction
解决方案四:
'''<summary>'''经典times33算法。简单高效。[这个使用移位代替*33]'''测试一千万。没有重复哈希值。'''</summary>'''<paramname="Key"></param>'''<returns></returns>PublicSharedFunctionHashCMyMap(KeyAsString)AsUInt64DimnHashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayDimIAsInt32=0While(I<L)nHash=(nHash<<5)+nHash+Asc(KeyCharArr(I))+3I+=1EndWhileReturnnHashEndFunction'''<summary>'''经典times33算法。简单高效。[这个使用移位代替*33]'''测试一千万。没有重复哈希值。'''</summary>'''<paramname="KeyByte"></param>'''<returns></returns>PublicSharedFunctionHashCMyMap(KeyByte()AsByte)AsUInt64DimnHashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=KeyByte.Length-1DimIAsInt32=0While(I<L)nHash=(nHash<<5)+nHash+KeyByte(I)+3I+=1EndWhileReturnnHashEndFunction'''<summary>'''经典的Time算法。简单,高效。'''Ngix使用的是time31,TokyoCabinet使用的是time37'''小写英文词汇适合33,大小写混合使用65。time33比较适合的是英文词汇的hash.'''</summary>'''<paramname="Key"></param>'''<paramname="seed">种子数。31,33,37。。。</param>'''<returns></returns>PublicSharedFunctionHashTimeMap(KeyAsString,seedAsInt16)AsUInt64DimnHashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=Key.Length-1DimKeyCharArr()AsChar=Key.ToArrayDimIAsInt32=0While(I<L)nHash=seed*nHash+nHash+Asc(KeyCharArr(I))+3I+=1EndWhileReturnnHashEndFunction'''<summary>'''经典的Time算法。简单,高效。'''Ngix使用的是time31,TokyoCabinet使用的是time37'''小写英文词汇适合33,大小写混合使用65。time33比较适合的是英文词汇的hash.'''</summary>'''<paramname="KeyByte"></param>'''<paramname="seed">种子质数。31,33,37。。。</param>'''<returns></returns>PublicSharedFunctionHashTimeMap(KeyByte()AsByte,seedAsUInt32)AsUInt64DimnHashAsMyUnchecked.UncheckedUInt64=0DimLAsInt32=KeyByte.Length-1DimIAsInt32=0While(I<L)nHash=seed*nHash+nHash+KeyByte(I)+3I+=1EndWhileReturnnHashEndFunction#EndRegion
解决方案五:
Mark一下