-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashtable.pas
463 lines (418 loc) · 12.8 KB
/
hashtable.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
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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit hashtable;
// warning: if you edit the implementation section of this unit but
// not its interface, dependent units won't be recompiled, so force it
// with -B
interface
uses
hashfunctions;
(* How to use THashTable ******************************************************************
* This generates a TFoo to TBar hash table with name TFooHashTable.
* Replace FooHash32 with one of the functions in hashfunctions.pas, based on TFoo's type
* If TFoo is a type that doesn't have built-in =/</> operators, you'll need to define
* your own utility record type instead of using DefaultUtils; see genericutils.pas
type
TFooUtils = specialize DefaultUtils <TFoo>;
TFooHashTable = class(specialize THashTable <TFoo, TBar, TFooUtils>)
public
constructor Create(PredictedCount: THashTableSizeInt = 8);
end;
constructor TFooHashTable.Create(PredictedCount: THashTableSizeInt = 8);
begin
inherited Create(@FooHash32, PredictedCount);
end;
* If you just have a one-off instance, you can skip defining a convenience constructor
* and just do it like this instead:
type
TFooUtils = specialize DefaultUtils <TFoo>;
TFooHashTable = specialize THashTable <TFoo, TBar, TFooUtils>;
var
Hash: TFooHashTable;
Hash := TFooHashTable.Create(@FooHash32, PredictedCount);
*****************************************************************************************)
type
generic THashTable <TKey, TValue, Utils> = class
strict protected
type
PPHashTableEntry = ^PHashTableEntry;
PHashTableEntry = ^THashTableEntry;
THashTableEntry = record
Key: TKey;
Value: TValue;
Next: PHashTableEntry;
end;
THashFunction = function (const Key: TKey): DWord;
const
kMaxLoadFactor = 0.7; // Wikipedia: "With a good hash function, the average lookup cost is nearly constant as the load factor increases from 0 up to 0.7 or so";
var
FTable: array of PHashTableEntry;
FCount: THashTableSizeInt;
FHashFunction: THashFunction;
procedure DoubleSize();
procedure Resize(const NewSize: THashTableSizeInt);
procedure PrepareForSize(PredictedCount: THashTableSizeInt);
procedure InternalAdd(var Table: array of PHashTableEntry; const Key: TKey; const Value: TValue);
procedure Update(const Key: TKey; const Value: TValue); // will call Add() if the key isn't already present
function Get(const Key: TKey): TValue;
function GetKeyForEntry(const Entry: Pointer): TKey;
function GetValueForEntry(const Entry: Pointer): TValue;
procedure AdvanceEnumerator(var Current: Pointer; var Index: THashTableSizeInt);
public
constructor Create(const AHashFunction: THashFunction; const PredictedCount: THashTableSizeInt = 8);
destructor Destroy(); override;
procedure Empty();
procedure Remove(const Key: TKey);
function Has(const Key: TKey): Boolean;
procedure Add(const Key: TKey; const Value: TValue);
function Clone(): THashTable;
property Items[Key: TKey]: TValue read Get write Update; default;
{$IFDEF DEBUG} procedure Histogram(var F: Text); {$ENDIF}
property Count: THashTableSizeInt read FCount;
public
type
TKeyEnumerator = class
strict private
FOwner: THashTable;
FIndex: THashTableSizeInt;
FCurrent: Pointer;
function GetCurrent(): TKey;
public
constructor Create(const Owner: THashTable);
function MoveNext(): Boolean;
property Current: TKey read GetCurrent;
function GetEnumerator(): TKeyEnumerator;
end;
function GetEnumerator(): TKeyEnumerator;
public
type
TValueEnumerator = class
strict private
FOwner: THashTable;
FIndex: THashTableSizeInt;
FCurrent: Pointer;
function GetCurrent(): TValue;
public
constructor Create(const Owner: THashTable);
function MoveNext(): Boolean;
property Current: TValue read GetCurrent;
function GetEnumerator(): TValueEnumerator;
end;
function Values(): TValueEnumerator;
end;
// XXX would be good to see if we can cache the enumerators mentioned above
// e.g. by tracking if it's still in use, and having a "master" enumerator (cached the first time it's created) which
// we only free when it's done, and whose .Free doesn't do anything if the instance is a master, or something
// (assuming for..in implicitly calls .Free)
implementation
uses
sysutils;
constructor THashTable.Create(const AHashFunction: THashFunction; const PredictedCount: THashTableSizeInt = 8);
begin
inherited Create();
Assert(Assigned(AHashFunction));
FHashFunction := AHashFunction;
Assert(PredictedCount > 0);
PrepareForSize(PredictedCount);
end;
destructor THashTable.Destroy();
begin
Empty();
inherited;
end;
procedure THashTable.Empty();
var
Index: THashTableSizeInt;
Item, LastItem: PHashTableEntry;
begin
if (Length(FTable) > 0) then
for Index := Low(FTable) to High(FTable) do
begin
Item := FTable[Index];
while (Assigned(Item)) do
begin
LastItem := Item;
Item := Item^.Next;
Dispose(LastItem);
end;
FTable[Index] := nil;
end;
FCount := 0;
end;
procedure THashTable.DoubleSize();
begin
Assert(Length(FTable) > 0);
if (Length(FTable)*2 < High(THashTableSizeInt)) then
Resize(Length(FTable) * 2) // $R-
else
if (Length(FTable) < High(THashTableSizeInt)) then
Resize(High(THashTableSizeInt));
end;
procedure THashTable.PrepareForSize(PredictedCount: THashTableSizeInt);
const
LoadFactorLimit = 1/kMaxLoadFactor;
begin
Assert(PredictedCount > 0);
if (PredictedCount * LoadFactorLimit < High(THashTableSizeInt)) then
PredictedCount := Trunc(PredictedCount * LoadFactorLimit) // $R-
else
PredictedCount := High(THashTableSizeInt);
if (FCount > 0) then
Resize(PredictedCount)
else
SetLength(FTable, PredictedCount);
end;
procedure THashTable.Resize(const NewSize: THashTableSizeInt);
var
NewTable: array of PHashTableEntry;
Index: THashTableSizeInt;
Item, LastItem: PHashTableEntry;
begin
Assert(NewSize > 0);
if (NewSize <> Length(FTable)) then
begin
SetLength(NewTable, NewSize);
Assert(Length(FTable) > 0);
for Index := Low(FTable) to High(FTable) do // $R-
begin
Item := FTable[Index];
while (Assigned(Item)) do
begin
InternalAdd(NewTable, Item^.Key, Item^.Value);
LastItem := Item;
Item := Item^.Next;
Dispose(LastItem);
end;
end;
FTable := NewTable;
end;
end;
procedure THashTable.InternalAdd(var Table: array of PHashTableEntry; const Key: TKey; const Value: TValue);
var
Hash: DWord;
Entry: PHashTableEntry;
begin
{ This is safe because Length(table) is positive and 'mod' will only ever return a smaller value }
Hash := FHashFunction(Key) mod Length(Table); // $R-
New(Entry);
Entry^.Key := Key;
Entry^.Value := Value;
Entry^.Next := Table[Hash];
Table[Hash] := Entry;
end;
procedure THashTable.Add(const Key: TKey; const Value: TValue);
begin
Inc(FCount);
if (FCount/Length(FTable) > kMaxLoadFactor) then
begin
{ Wikipedia: "With a good hash function, the average lookup cost is nearly constant as the load factor increases from 0 up to 0.7 or so" }
DoubleSize();
end;
InternalAdd(FTable, Key, Value);
end;
procedure THashTable.Remove(const Key: TKey);
var
Hash: DWord;
Entry: PHashTableEntry;
LastEntry: PPHashTableEntry;
begin
{ This is safe because Length(table) is positive and 'mod' will only ever return a smaller value }
Hash := FHashFunction(Key) mod Length(FTable); // $R-
Entry := FTable[Hash];
LastEntry := @FTable[Hash];
while (Assigned(Entry)) do
begin
if (Utils.Equals(Entry^.Key, Key)) then
begin
LastEntry^ := Entry^.Next;
Dispose(Entry);
Dec(FCount);
exit;
end;
LastEntry := @Entry^.Next;
Entry := Entry^.Next;
end;
end;
function THashTable.Get(const Key: TKey): TValue;
var
Entry: PHashTableEntry;
begin
{ This is safe because Length(table) is positive and 'mod' will only ever return a smaller value }
Entry := FTable[FHashFunction(Key) mod Length(FTable)];
while (Assigned(Entry)) do
begin
if (Utils.Equals(Entry^.Key, Key)) then
begin
Result := Entry^.Value;
exit;
end;
Entry := Entry^.Next;
end;
Result := Default(TValue);
end;
function THashTable.Has(const Key: TKey): Boolean;
var
Entry: PHashTableEntry;
begin
{ This is safe because Length(table) is positive and 'mod' will only ever return a smaller value }
Entry := FTable[FHashFunction(Key) mod Length(FTable)];
while (Assigned(Entry)) do
begin
if (Utils.Equals(Entry^.Key, Key)) then
begin
Result := True;
exit;
end;
Entry := Entry^.Next;
end;
Result := False;
end;
procedure THashTable.Update(const Key: TKey; const Value: TValue);
var
Entry: PHashTableEntry;
begin
{ This is safe because Length(table) is positive and 'mod' will only ever return a smaller value }
Entry := FTable[FHashFunction(Key) mod Length(FTable)];
while (Assigned(Entry)) do
begin
if (Utils.Equals(Entry^.Key, Key)) then
begin
Entry^.Value := Value;
exit;
end;
Entry := Entry^.Next;
end;
Add(Key, Value);
end;
{$IFDEF DEBUG}
procedure THashTable.Histogram(var F: Text);
var
Index: THashTableSizeInt;
Item: PHashTableEntry;
begin
Assert(Length(FTable) > 0);
Writeln(F, 'THashTable histogram:'); // $DFA- for F
for Index := Low(FTable) to High(FTable) do // $R-
begin
System.Write(F, Index: 5, ': ');
Item := FTable[Index];
while (Assigned(Item)) do
begin
System.Write(F, '#');
Item := Item^.Next;
end;
Writeln(F);
end;
Writeln(F, 'Size: ' + IntToStr(Length(FTable)) + '; Count: ' + IntToStr(FCount));
end;
{$ENDIF}
function THashTable.GetKeyForEntry(const Entry: Pointer): TKey;
begin
if (Assigned(Entry)) then
begin
Result := PHashTableEntry(Entry)^.Key;
end
else
begin
Result := Default(TKey);
end;
end;
function THashTable.GetValueForEntry(const Entry: Pointer): TValue;
begin
if (Assigned(Entry)) then
begin
Result := PHashTableEntry(Entry)^.Value;
end
else
begin
Result := Default(TValue);
end;
end;
procedure THashTable.AdvanceEnumerator(var Current: Pointer; var Index: THashTableSizeInt);
begin
if (Assigned(Current)) then
begin // advance
Current := PHashTableEntry(Current)^.Next
end
else
begin // just started
Assert(Index = 0);
Current := FTable[Index];
end;
while ((not Assigned(Current)) and (Index < High(FTable))) do
begin
Inc(Index);
Current := FTable[Index];
end;
end;
function THashTable.Clone(): THashTable;
var
Index: Cardinal;
Current: PHashTableEntry;
begin
Assert(Assigned(Self));
Result := ClassType.Create() as THashTable;
Result.FHashFunction := FHashFunction;
Result.PrepareForSize(FCount);
if (FCount > 0) then
begin
Assert(Length(FTable) > 0);
for Index := Low(FTable) to High(FTable) do // $R-
begin
Current := FTable[Index];
while (Assigned(Current)) do
begin
Result.Add(Current^.Key, Current^.Value);
Current := Current^.Next;
end;
end;
end;
Assert(Result.Count = FCount);
end;
constructor THashTable.TKeyEnumerator.Create(const Owner: THashTable);
begin
FOwner := Owner;
FIndex := 0;
FCurrent := nil;
end;
function THashTable.TKeyEnumerator.GetCurrent(): TKey;
begin
Result := FOwner.GetKeyForEntry(FCurrent);
end;
function THashTable.TKeyEnumerator.MoveNext(): Boolean;
begin
FOwner.AdvanceEnumerator(FCurrent, FIndex);
Result := Assigned(FCurrent);
end;
function THashTable.TKeyEnumerator.GetEnumerator(): TKeyEnumerator;
begin
Result := Self;
end;
function THashTable.GetEnumerator(): TKeyEnumerator;
begin
Result := TKeyEnumerator.Create(Self);
end;
constructor THashTable.TValueEnumerator.Create(const Owner: THashTable);
begin
FOwner := Owner;
FIndex := 0;
FCurrent := nil;
end;
function THashTable.TValueEnumerator.GetCurrent(): TValue;
begin
Result := FOwner.GetValueForEntry(FCurrent);
end;
function THashTable.TValueEnumerator.MoveNext(): Boolean;
begin
FOwner.AdvanceEnumerator(FCurrent, FIndex);
Result := Assigned(FCurrent);
end;
function THashTable.TValueEnumerator.GetEnumerator(): TValueEnumerator;
begin
Result := Self;
end;
function THashTable.Values(): TValueEnumerator;
begin
Result := TValueEnumerator.Create(Self);
end;
end.