-
Notifications
You must be signed in to change notification settings - Fork 1
/
adthashfunct.pas.mcp
496 lines (414 loc) · 12 KB
/
adthashfunct.pas.mcp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
(* This file is a part of the PascalAdt library, which provides
commonly used algorithms and data structures for the FPC and Delphi
compilers.
Copyright (C) 2004, 2005 by Lukasz Czajka
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA *)
unit adthashfunct;
{ This unit provides some fairly good hashing functions for a number
of data types. You should use these functions as far as possible
instead of your own, because they are mostly widely-used,
well-tested, high-quality functions. If you use some non-standard
data type try to hash it using FNVHash on its every byte instead of
devising some new function. }
interface
uses
SysUtils, adtfunct;
&include adtdefs.inc
type
THashFunction = function(ptr : Pointer; len : SizeType) : UnsignedType;
{ ------ hasher functors for adaptating specific hash functions ------- }
THashAdaptor = class (TFunctor)
protected
hashFunc : THashFunction;
public
{ assings hf to hashFunc }
constructor Create(hf : THashFunction); overload;
{ sets hashFunc to nil; derived classes use FNVHash if this is
set to nil }
constructor Create; overload;
end;
&ifdef MCP_POINTER
TPCharHasher = class (THashAdaptor, IPointerHasher)
public
{ interprets ptr as a PChar; }
function Hash(ptr : Pointer) : UnsignedType;
end;
&endif
TAnsiStringHasher = class (THashAdaptor, IStringHasher)
public
{ interprets ptr as a wrapper around an AnsiString used by the
TAnsiString- containers; }
function Hash(aitem : String) : UnsignedType;
end;
&ifndef MCP_NO_INTEGER
TIntegerHasher = class (THashAdaptor, IIntegerHasher)
public
{ hashes an Integer }
function Hash(aitem : Integer) : UnsignedType;
end;
&endif
&ifdef MCP_REAL
TRealHasher = class (THashAdaptor, IRealHasher)
public
function Hash(aitem : Real) : UnsignedType;
end;
&endif
&ifdef MCP_POINTER
TPointerValueHasher = class (THashAdaptor, IPointerHasher)
public
{ interprets ptr as a plain number, i.e. uses the value of the
pointer to compute the result }
function Hash(ptr : Pointer) : UnsignedType;
end;
TMemoryHasher = class (THashAdaptor, IPointerHasher)
private
len : SizeType;
public
{ uses chunkLength as a length of each memory chunk hashed (in
bytes); always use these constructors to create TMemoryHasher }
constructor Create(chunkLength : SizeType; hf : THashFunction); overload;
constructor Create(chunkLength : SizeType); overload;
{ interprets ptr as a pointer to memory location chunkLength
bytes long (@see constructor) }
function Hash(ptr : Pointer) : UnsignedType;
end;
&endif
{ ----------------- functions returning hashers --------------------- }
{ note: it is more efficient to use one of these functions than to
create a new hasher every time; calling any of those functions
does not create a new hasher but reuses one already created }
function AnsiStringHasher : IStringHasher;
&ifdef MCP_POINTER
function PCharHasher : IPointerHasher;
&endif
&ifndef MCP_NO_INTEGER
function IntegerHasher : IIntegerHasher;
&endif
&ifdef MCP_REAL
function RealHasher : IRealHasher;
&endif
&ifdef MCP_POINTER
function PointerValueHasher : IPointerHasher;
&endif
{ ----------------- specific hashing algorithms --------------------- }
{ this uses a very good, widely applicable FNV-1a algorithm; it has
a decent distribution and is considerably simple and fast; this
is the default hash recommended for all sorts of data; for more
information see www.isthe.com/chongo/tech/comp/fnv/ or just do a
net search should this link be dead }
function FNVHash(ptr : Pointer; len : SizeType) : UnsignedType;
{ this is the One-at-a-Time hash; very good distribution, but probably
slower than FNV }
function OneAtATimeHash(ptr : Pointer; len : SizeType) : UnsignedType;
{ this is the Bob Jenkins' hash; good distribution, may be faster
than FNV on some machines with slow multiplication (not Intel);
see http://burtleburtle.net/bob/hash/evahash.html }
function BobJenkinsHash(ptr : Pointer; len : SizeType) : UnsignedType;
implementation
uses
{$ifdef STRINGS_UNIT }
Strings;
{$endif }
var
varAnsiStringHasher : IStringHasher;
&ifndef MCP_NO_INTEGER
varIntegerHasher : IIntegerHasher;
&endif
&ifdef MCP_REAL
varRealHasher : IRealHasher;
&endif
&ifdef MCP_POINTER
varPCharHasher : IPointerHasher;
varPointerValueHasher : IPointerHasher;
&endif
{$Q-}
{$R-}
{ ------ hasher functors for adaptating specific hash functions ------- }
constructor THashAdaptor.Create(hf : THashFunction);
begin
hashFunc := hf;
end;
constructor THashAdaptor.Create;
begin
hashFunc := nil;
end;
&ifdef MCP_POINTER
function TPCharHasher.Hash(ptr : Pointer) : UnsignedType;
begin
if not Assigned(hashFunc) then
Result := FNVHash(ptr, StrLen(PChar(ptr)))
else
Result := hashFunc(ptr, StrLen(PChar(ptr)));
end;
&endif
function TAnsiStringHasher.Hash(aitem : String) : UnsignedType;
begin
if not Assigned(hashFunc) then
Result := FNVHash(PChar(aitem), Length(aitem))
else
Result := hashFunc(PChar(aitem), Length(aitem));
end;
&ifndef MCP_NO_INTEGER
function TIntegerHasher.Hash(aitem : Integer) : UnsignedType;
begin
if not Assigned(hashFunc) then
Result := FNVHash(@aitem, SizeOf(Integer))
else
Result := hashFunc(@aitem, SizeOf(Integer));
end;
&endif
&ifdef MCP_REAL
function TRealHasher.Hash(aitem : Real) : UnsignedType;
begin
if not Assigned(hashFunc) then
Result := FNVHash(@aitem, SizeOf(Real))
else
Result := hashFunc(@aitem, SizeOf(Real));
end;
&endif
&ifdef MCP_POINTER
function TPointerValueHasher.Hash(ptr : Pointer) : UnsignedType;
begin
if not Assigned(hashFunc) then
Result := FNVHash(@ptr, SizeOf(Pointer))
else
Result := hashFunc(@ptr, SizeOf(Pointer));
end;
constructor TMemoryHasher.Create(chunkLength : SizeType; hf : THashFunction);
begin
inherited Create(hf);
len := chunkLength;
end;
constructor TMemoryHasher.Create(chunkLength : SizeType);
begin
inherited Create;
len := chunkLength;
end;
function TMemoryHasher.Hash(ptr : Pointer) : UnsignedType;
begin
if Assigned(hashFunc) then
Result := FNVHash(ptr, len)
else
Result := hashFunc(ptr, len);
end;
&endif &# MCP_POINTER
{ ----------------- functions returning hashers --------------------- }
function AnsiStringHasher : IStringHasher;
begin
Result := varAnsiStringHasher;
end;
&ifdef MCP_POINTER
function PCharHasher : IPointerHasher;
begin
Result := varPCharHasher;
end;
&endif
&ifndef MCP_NO_INTEGER
function IntegerHasher : IIntegerHasher;
begin
Result := varIntegerHasher;
end;
&endif
&ifdef MCP_REAL
function RealHasher : IRealHasher;
begin
Result := varRealHasher;
end;
&endif
&ifdef MCP_POINTER
function PointerValueHasher : IPointerHasher;
begin
Result := varPointerValueHasher;
end;
&endif
{ ----------------- specific hashing algorithms --------------------- }
function FNVHash(ptr : Pointer; len : SizeType) : UnsignedType;
const
FNV_PRIME = 16777619;
FNV_OFFSET_BASIS = $811c9dc5;
var
pch : PChar;
begin
pch := PChar(ptr);
Result := UnsignedType(FNV_OFFSET_BASIS);
while len <> 0 do
begin
Result := (Result xor Ord(pch^)) * FNV_PRIME;
Inc(pch);
Dec(len);
end;
end;
function OneAtATimeHash(ptr : Pointer; len : SizeType) : UnsignedType;
var
pch : PChar;
begin
pch := PChar(ptr);
Result := 0;
while len <> 0 do
begin
Inc(Result, Ord(pch^));
Inc(Result, Result shl 10);
Result := Result xor (Result shr 6);
Inc(pch);
Dec(len);
end;
Inc(Result, Result shl 3);
Result := Result xor (Result shr 11);
Inc(Result, Result shl 15);
end;
function BobJenkinsHash(ptr : Pointer; len : SizeType) : UnsignedType;
const
GOLDEN_RATIO = $9e3779b9;
var
a, b, c, originalLength : UnsignedType;
pch : PChar;
procedure Mix;
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif }
begin
Dec(a, b);
Dec(a, c);
a := a xor (c shr 13);
Dec(b, c);
Dec(b, a);
b := b xor (a shl 8);
Dec(c, a);
Dec(c, b);
c := c xor (b shr 13);
Dec(a, b);
Dec(a, c);
a := a xor (c shr 12);
Dec(b, c);
Dec(b, a);
b := b xor (a shl 16);
Dec(c, a);
Dec(c, b);
c := c xor (b shr 5);
Dec(a, b);
Dec(a, c);
a := a xor (c shr 3);
Dec(b, c);
Dec(b, a);
b := b xor (a shl 10);
Dec(c, a);
Dec(c, b);
c := c xor (b shr 15);
end;
begin
originalLength := len;
pch := PChar(ptr);
a := GOLDEN_RATIO;
b := GOLDEN_RATIO;
c := 0;
while len >= 12 do
begin
Inc(a, Ord(pch^));
Inc(pch);
Inc(a, Ord(pch^) shl 8);
Inc(pch);
Inc(a, Ord(pch^) shl 16);
Inc(pch);
Inc(a, Ord(pch^) shl 24);
Inc(pch);
Inc(b, Ord(pch^));
Inc(pch);
Inc(b, Ord(pch^) shl 8);
Inc(pch);
Inc(b, Ord(pch^) shl 16);
Inc(pch);
Inc(b, Ord(pch^) shl 24);
Inc(pch);
Inc(c, Ord(pch^));
Inc(pch);
Inc(c, Ord(pch^) shl 8);
Inc(pch);
Inc(c, Ord(pch^) shl 16);
Inc(pch);
Inc(c, Ord(pch^) shl 24);
Inc(pch);
Mix;
Dec(len, 12);
end;
Inc(c, originalLength);
if len >= 1 then
begin
Inc(a, Ord(pch^));
Inc(pch);
if len >= 2 then
begin
Inc(a, Ord(pch^) shl 8);
Inc(pch);
if len >= 3 then
begin
Inc(a, Ord(pch^) shl 16);
Inc(pch);
if len >= 4 then
begin
Inc(a, Ord(pch^) shl 24);
Inc(pch);
if len >= 5 then
begin
Inc(b, Ord(pch^));
Inc(pch);
if len >= 6 then
begin
Inc(b, Ord(pch^) shl 8);
Inc(pch);
if len >= 7 then
begin
Inc(b, Ord(pch^) shl 16);
Inc(pch);
if len >= 8 then
begin
Inc(b, Ord(pch^) shl 24);
Inc(pch);
if len >= 9 then
begin
Inc(c, Ord(pch^) shl 8);
Inc(pch);
if len >= 10 then
begin
Inc(c, Ord(pch^) shl 16);
Inc(pch);
if len >= 11 then
begin
Inc(c, Ord(pch^) shl 24);
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
Mix;
Result := c;
end;
initialization
varAnsiStringHasher := TAnsiStringHasher.Create;;
&ifndef MCP_NO_INTEGER
varIntegerHasher := TIntegerHasher.Create;
&endif
&ifdef MCP_REAL
varRealHasher := TRealHasher.Create;
&endif
&ifdef MCP_POINTER
varPointerValueHasher := TPointerValueHasher.Create;
varPCharHasher := TPCharHasher.Create;
&endif
end.