blob: 80acc57cafbba96dc1f7b5abb9dac0731ff35e62 (
plain) (
blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
//
//
// The Nimrod Compiler
// (c) Copyright 2008 Andreas Rumpf
//
// See the file "copying.txt", included in this
// distribution, for details about the copyright.
//
unit hashes;
interface
uses
charsets, nsystem, strutils;
const
SmallestSize = (1 shl 3) - 1;
DefaultSize = (1 shl 11) - 1;
BiggestSize = (1 shl 28) - 1;
type
THash = type int;
PHash = ^THash;
THashFunc = function (str: PChar): THash;
function GetHash(str: PChar): THash;
function GetHashCI(str: PChar): THash;
function GetDataHash(Data: Pointer; Size: int): THash;
function hashPtr(p: Pointer): THash;
function GetHashStr(const s: string): THash;
function GetHashStrCI(const s: string): THash;
function getNormalizedHash(const s: string): THash;
//function nextPowerOfTwo(x: int): int;
function concHash(h: THash; val: int): THash;
function finishHash(h: THash): THash;
implementation
type
TUnsignedHash = cardinal;
{@ignore}
{$ifopt Q+} { we need Q- here! }
{$define Q_on}
{$Q-}
{$endif}
{$ifopt R+}
{$define R_on}
{$R-}
{$endif}
{@emit}
function nextPowerOfTwo(x: int): int;
begin
result := x -{%} 1;
result := result or (result shr 32);
result := result or (result shr 16);
result := result or (result shr 8);
result := result or (result shr 4);
result := result or (result shr 2);
result := result or (result shr 1);
Inc(result)
end;
function concHash(h: THash; val: int): THash;
begin
result := h +{%} val;
result := result +{%} result shl 10;
result := result xor (result shr 6);
end;
function finishHash(h: THash): THash;
begin
result := h +{%} h shl 3;
result := result xor (result shr 11);
result := result +{%} result shl 15;
end;
function GetDataHash(Data: Pointer; Size: int): THash;
var
h: TUnsignedHash;
p: PChar;
i, s: int;
begin
h := 0;
p := {@cast}pchar(Data);
i := 0;
s := size;
while s > 0 do begin
h := h +{%} ord(p[i]);
h := h +{%} h shl 10;
h := h xor (h shr 6);
Inc(i); Dec(s)
end;
h := h +{%} h shl 3;
h := h xor (h shr 11);
h := h +{%} h shl 15;
result := THash(h)
end;
function hashPtr(p: Pointer): THash;
begin
result := ({@cast}THash(p)) shr 3; // skip the alignment
end;
function GetHash(str: PChar): THash;
var
h: TUnsignedHash;
i: int;
begin
h := 0;
i := 0;
while str[i] <> #0 do begin
h := h +{%} ord(str[i]);
h := h +{%} h shl 10;
h := h xor (h shr 6);
Inc(i)
end;
h := h +{%} h shl 3;
h := h xor (h shr 11);
h := h +{%} h shl 15;
result := THash(h)
end;
function GetHashStr(const s: string): THash;
var
h: TUnsignedHash;
i: int;
begin
h := 0;
for i := 1 to Length(s) do begin
h := h +{%} ord(s[i]);
h := h +{%} h shl 10;
h := h xor (h shr 6);
end;
h := h +{%} h shl 3;
h := h xor (h shr 11);
h := h +{%} h shl 15;
result := THash(h)
end;
function getNormalizedHash(const s: string): THash;
var
h: TUnsignedHash;
c: Char;
i: int;
begin
h := 0;
for i := strStart to length(s)+strStart-1 do begin
c := s[i];
if c = '_' then continue; // skip _
if c in ['A'..'Z'] then c := chr(ord(c) + (ord('a')-ord('A'))); // toLower()
h := h +{%} ord(c);
h := h +{%} h shl 10;
h := h xor (h shr 6);
end;
h := h +{%} h shl 3;
h := h xor (h shr 11);
h := h +{%} h shl 15;
result := THash(h)
end;
function GetHashStrCI(const s: string): THash;
var
h: TUnsignedHash;
c: Char;
i: int;
begin
h := 0;
for i := strStart to length(s)+strStart-1 do begin
c := s[i];
if c in ['A'..'Z'] then c := chr(ord(c) + (ord('a')-ord('A'))); // toLower()
h := h +{%} ord(c);
h := h +{%} h shl 10;
h := h xor (h shr 6);
end;
h := h +{%} h shl 3;
h := h xor (h shr 11);
h := h +{%} h shl 15;
result := THash(h)
end;
function GetHashCI(str: PChar): THash;
var
h: TUnsignedHash;
c: Char;
i: int;
begin
h := 0;
i := 0;
while str[i] <> #0 do begin
c := str[i];
if c in ['A'..'Z'] then c := chr(ord(c) + (ord('a')-ord('A'))); // toLower()
h := h +{%} ord(c);
h := h +{%} h shl 10;
h := h xor (h shr 6);
Inc(i)
end;
h := h +{%} h shl 3;
h := h xor (h shr 11);
h := h +{%} h shl 15;
result := THash(h)
end;
{@ignore}
{$ifdef Q_on}
{$undef Q_on}
{$Q+}
{$endif}
{$ifdef R_on}
{$undef R_on}
{$R+}
{$endif}
{@emit}
end.
|