forked from grijjy/GrijjyFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grijjy.Hash.pas
134 lines (109 loc) · 2.8 KB
/
Grijjy.Hash.pas
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.