Murmurhash 哈希算法
Murmurhash 哈希算法
MurmurHash 是一种非加密型哈希函数,适用于一般的哈希检索操作。 由Austin Appleby在2008年发明, 并出现了多个变种,都已经发布到了公有领域(public domain)。与其它流行的哈希函数相比,对于规律性较强的key,MurmurHash的随机分布特征表现更良好。
Redis在实现字典时用到了两种不同的哈希算法,MurmurHash便是其中一种(另一种是djb),在Redis中应用十分广泛,包括数据库、集群、哈希键、阻塞操作等功能都用到了这个算法。发明算法的作者被邀到google工作,该算法最新版本是MurmurHash3,基于MurmurHash2改进了一些小瑕疵,使得速度更快,实现了32位(低延时)、128位HashKey,尤其对大块的数据,具有较高的平衡性与低碰撞率。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | unit Grijjy.Hash; {$INCLUDE 'Grijjy.inc' } {$OVERFLOWCHECKS OFF} // required since overflow checks will fail (code works ok w/o checking on) interface type { Incremental Murmur-2 hash. See https: //sites.google.com/site/murmurhash/ Uses the CMurmurHash2A variant, which can be used incrementally. The results are *not* the same as for goMurmurHash2 in Grijjy.SysUtils } TgoHashMurmur2 = record {$REGION 'Internal Declarations' } private const M = $5bd1e995; R = 24; private FHash: Cardinal; FTail: Cardinal; FCount: Cardinal; FSize: Cardinal; private class procedure Mix( var H, K: Cardinal); static ; inline; private procedure MixTail( var AData: PByte; var ALength: Integer); {$ENDREGION 'Internal Declarations' } public { Starts a new hash. Parameters: ASeed: (optional) seed value for the hash. This is identical to calling Reset. } class function Create( const ASeed: Integer = 0): TgoHashMurmur2; static ; inline; { Restarts the hash Parameters: ASeed: (optional) seed value for the hash. This is identical to using Create. } procedure Reset( const ASeed: Integer = 0); inline; { Updates the hash with new data. Parameters: AData: the data to hash ALength: the size of the data in bytes. } procedure Update( const AData; ALength: Integer); { Finishes the hash and returns the hash code. Returns: The hash code } function Finish: Cardinal; end; implementation { TgoHashMurmur2 } class function TgoHashMurmur2.Create( const ASeed: Integer): TgoHashMurmur2; begin Result.Reset(ASeed); end; function TgoHashMurmur2.Finish: Cardinal; begin Mix(FHash, FTail); Mix(FHash, FSize); FHash := FHash xor (FHash shr 13); FHash := FHash * M; FHash := FHash xor (FHash shr 15); Result := FHash; end; class procedure TgoHashMurmur2.Mix( var H, K: Cardinal); begin K := K * M; K := K xor (K shr R); K := K * M; H := H * M; H := H xor K; end; procedure TgoHashMurmur2.MixTail( var AData: PByte; var ALength: Integer); begin while (ALength <> 0) and ((ALength < 4) or (FCount <> 0)) do begin FTail := FTail or (AData^ shl (FCount * 8)); Inc(AData); Inc(FCount); Dec(ALength); if (FCount = 4) then begin Mix(FHash, FTail); FTail := 0; FCount := 0; end; end; end; procedure TgoHashMurmur2.Reset( const ASeed: Integer); begin FHash := ASeed; FTail := 0; FCount := 0; FSize := 0; end; procedure TgoHashMurmur2.Update( const AData; ALength: Integer); var Data: PByte; K: Cardinal; begin Inc(FSize, ALength); Data := @AData; MixTail(Data, ALength); while (ALength >= 4) do begin K := PCardinal(Data)^; Mix(FHash, K); Inc(Data, 4); Dec(ALength, 4); end; MixTail(Data, ALength); end; end. |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/14227995.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2017-01-04 mormot 数据集转换为JSON字串
2017-01-04 mormot orm rest注意事项
2017-01-04 mORMot 数据库操作
2017-01-04 mORMot使用基础