-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashtables.pas
394 lines (310 loc) · 8.53 KB
/
Hashtables.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
Unit Hashtables;
Interface
Uses
StringSupport,
AdvObjects, AdvExceptions;
Type
THashTable = Class;
THashTableEnumerator = Class;
THashKey = Int64;
THashValue = Int64;
THashEntry = Record
HashCode : Integer;
Next : Integer;
Key : THashKey;
Value : THashValue;
End;
TIntegerArray = Array Of Integer;
THashEntryArray = Array Of THashEntry;
THashTable = Class(TAdvObject)
Private
FVersion : Integer;
FCount : Integer;
FFreeCount : Integer;
FFreeList : Integer;
FBuckets : TIntegerArray;
FEntries : THashEntryArray;
Function GetCount : Integer;
Protected
Procedure Initialize(iCapacity : Integer);
Procedure Resize; Overload;
Procedure Resize(iNewSize : Integer; bRehash : Boolean); Overload;
Procedure Insert(Const aKey : THashKey; Const aValue : THashValue; bAdd : Boolean);
Function FindEntry(Const aKey : THashKey) : Integer;
Public
Constructor Create; Overload; Override;
Constructor Create(iCapacity : Integer); Overload;
Procedure Add(Const aKey : THashKey; Const aValue : THashValue);
Function TryGetValue(Const aKey : THashKey; Out aValue : THashValue) : Boolean;
Function Remove(Const aKey : THashKey) : Boolean;
Function GetEnumerator : THashTableEnumerator;
Property Count : Integer Read GetCount;
End;
THashTableEnumerator = Class(TAdvObject)
Private
FCurrentKey : THashKey;
FCurrentValue : THashValue;
FHashTable : THashTable;
FVersion : Integer;
FIndex : Integer;
Public
Constructor Create; Override;
Function MoveNext : Boolean;
Property CurrentKey : THashKey Read FCurrentKey;
Property CurrentValue : THashValue Read FCurrentValue;
End;
TCacheTableLocker = Class(TAdvObject)
End;
TCacheEntry = Class(TAdvObject)
End;
TCacheTable = Class(TAdvObject)
End;
Implementation
Const
KNOWN_PRIMES : Array[0..71] Of Integer = (
3, 7, 11, $11, $17, $1d, $25, $2f, $3b, $47, $59, $6b, $83, $a3, $c5, $ef,
$125, $161, $1af, $209, $277, $2f9, $397, $44f, $52f, $63d, $78b, $91d, $af1, $d2b, $fd1, $12fd,
$16cf, $1b65, $20e3, $2777, $2f6f, $38ff, $446f, $521f, $628d, $7655, $8e01, $aa6b, $cc89, $f583, $126a7, $1619b,
$1a857, $1fd3b, $26315, $2dd67, $3701b, $42023, $4f361, $5f0ed, $72125, $88e31, $a443b, $c51eb, $ec8c1, $11bdbf, $154a3f, $198c4f,
$1ea867, $24ca19, $2c25c1, $34fa1b, $3f928f, $4c4987, $5b8b6f, $6dda89
);
Function GetPrime(iMin : Integer) : Integer;
Var
iIndex : Integer;
Begin
iIndex := 0;
While KNOWN_PRIMES[iIndex] < iMin Do
Inc(iIndex);
If iIndex < Length(KNOWN_PRIMES) Then
Result := KNOWN_PRIMES[iIndex]
Else
Begin
// TODO: Calculate larger primes
Raise EAdvException.Create('Not implemented');
End;
End;
Function GetHashCode(Const aValue : THashKey) : Integer;
Begin
Result := (Integer(aValue Shr 32)) Xor (Integer(aValue));
End;
Function KeyEquals(Const aValue, bValue : THashKey) : Boolean;
Begin
Result := aValue = bValue;
End;
Function KeyToString(Const aValue : THashKey) : String;
Begin
Result := StringFormat('%d', [aValue]);
End;
Function THashTable.GetCount : Integer;
Begin
Result := FCount - FFreeCount;
End;
Procedure THashTable.Initialize(iCapacity : Integer);
Var
iPrime, iLoop : Integer;
Begin
iPrime := GetPrime(iCapacity);
SetLength(FBuckets, iPrime);
For iLoop := 0 To Length(FBuckets) - 1 Do
FBuckets[iLoop] := -1;
SetLength(FEntries, iPrime);
FFreeList := -1;
End;
Procedure THashTable.Resize;
Begin
Resize(GetPrime(FCount), False);
End;
Procedure THashTable.Resize(iNewSize : Integer; bRehash : Boolean);
Var
aNumArray : TIntegerArray;
aDestinationArray : THashEntryArray;
i, j, k, iIndex : Integer;
Begin
aNumArray := Nil;
aDestinationArray := Nil;
SetLength(aNumArray, iNewSize);
For i := 0 To Length(aNumArray) - 1 Do
aNumArray[i] := -1;
SetLength(aDestinationArray, iNewSize);
Move(FEntries[0], aDestinationArray[0], FCount * SizeOf(THashEntry));
If bRehash Then
Begin
For k := 0 To FCount - 1 Do
Begin
If aDestinationArray[k].HashCode <> -1 Then
aDestinationArray[k].HashCode := GetHashCode(aDestinationArray[k].Key) And MaxInt;
End;
End;
For j := 0 To FCount - 1 Do
Begin
If aDestinationArray[j].HashCode >= 0 Then
Begin
iIndex := aDestinationArray[j].HashCode Mod iNewSize;
aDestinationArray[j].Next := aNumArray[iIndex];
aNumArray[iIndex] := j;
End;
End;
// FBuckets := aNumArray;
// FEntries := aDestinationArray;
End;
Procedure THashTable.Insert(Const aKey : THashValue; Const aValue : THashValue; bAdd : Boolean);
Var
iNum, iIndex, i, iFreeList : Integer;
// iNum3 : Integer;
Begin
If Not Assigned(FBuckets) Then
Initialize(0);
iNum := GetHashCode(aKey) And MaxInt;
iIndex := iNum Mod Length(FBuckets);
// iNum3 := 0;
i := FBuckets[iIndex];
While i >= 0 Do
Begin
If (FEntries[i].HashCode = iNum) And KeyEquals(FEntries[i].Key, aKey) Then
Begin
If bAdd Then
Error('Insert', StringFormat('Key %s already present', [KeyToString(aKey)]));
FEntries[i].Value := aValue;
Inc(FVersion);
Exit;
End;
// Inc(iNum3);
i := FEntries[i].Next;
End;
If FFreeCount > 0 Then
Begin
iFreeList := FFreeList;
FFreeList := FEntries[iFreeList].Next;
Dec(FFreeCount);
End
Else
Begin
If FCount = Length(FEntries) Then
Begin
Resize;
iIndex := iNum Mod Length(FBuckets);
End;
iFreeList := FCount;
Inc(FCount);
End;
FEntries[iFreeList].HashCode := iNum;
FEntries[iFreeList].Next := FBuckets[iIndex];
FEntries[iFreeList].Key := aKey;
FEntries[iFreeList].Value := aValue;
FBuckets[iIndex] := iFreeList;
Inc(FVersion);
{If iNum3 > 100 Then
Begin
RandomizeComparer;
Resize(Length(FEentries), True);
End;}
End;
Function THashTable.FindEntry(Const aKey : THashKey) : Integer;
Var
iNum, i : Integer;
Begin
If Assigned(FBuckets) Then
Begin
iNum := GetHashCode(aKey) And MaxInt;
i := FBuckets[iNum Mod Length(FBuckets)];
While i >= 0 Do
Begin
If (FEntries[i].HashCode = iNum) And KeyEquals(FEntries[i].Key, aKey) Then
Begin
Result := i;
Exit;
End;
i := FEntries[i].Next;
End;
End;
Result := -1;
End;
Constructor THashTable.Create;
Begin
Create(0);
End;
Constructor THashTable.Create(iCapacity : Integer);
Begin
Initialize(iCapacity);
End;
Procedure THashTable.Add(Const aKey : THashKey; Const aValue : THashValue);
Begin
Insert(aKey, aValue, True);
End;
Function THashTable.TryGetValue(Const aKey : THashKey; Out aValue : THashValue) : Boolean;
Var
iIndex : Integer;
Begin
iIndex := FindEntry(aKey);
Result := iIndex >= 0;
If Result Then
aValue := FEntries[iIndex].Value
Else
FillChar(aValue, SizeOf(THashValue), 0);
End;
Function THashTable.Remove(Const aKey : THashKey) : Boolean;
Var
iNum, iIndex, iNum3, i : Integer;
Begin
If Assigned(FBuckets) Then
Begin
iNum := GetHashCode(aKey) And MaxInt;
iIndex := iNum Mod Length(FBuckets);
iNum3 := -1;
i := FBuckets[iIndex];
While i >= 0 Do
Begin
If (FEntries[i].HashCode = iNum) And KeyEquals(FEntries[i].Key, aKey) Then
Begin
If iNum3 < 0 Then
FBuckets[iIndex] := FEntries[i].Next
Else
FEntries[iNum3].Next := FEntries[i].Next;
FEntries[i].HashCode := -1;
FEntries[i].Next := FFreeList;
FillChar(FEntries[i].Key, SizeOf(THashKey), 0);
FillChar(FEntries[i].Value, SizeOf(THashValue), 0);
FFreeList := i;
Inc(FFreeCount);
Inc(FVersion);
Result := True;
Exit;
End;
iNum3 := i;
i := FEntries[i].Next;
End;
End;
Result := False;
End;
Function THashTable.GetEnumerator : THashTableEnumerator;
Begin
Result := THashTableEnumerator.Create;
Result.FHashTable := Self;
Result.FVersion := FVersion;
End;
Constructor THashTableEnumerator.Create;
Begin
Inherited;
End;
Function THashTableEnumerator.MoveNext : Boolean;
Begin
If FVersion <> FHashTable.FVersion Then
Error('MoveNext', 'Underlying hashtable was changed during enumeration');
While FIndex < FHashTable.FCount Do
Begin
If FHashTable.FEntries[FIndex].HashCode >= 0 Then
Begin
FCurrentKey := FHashTable.FEntries[FIndex].Key;
FCurrentValue := FHashTable.FEntries[FIndex].Value;
Inc(FIndex);
Result := True;
Exit;
End;
Inc(FIndex);
End;
FIndex := FHashTable.FCount + 1;
FillChar(FCurrentKey, SizeOf(THashKey), 0);
FillChar(FCurrentValue, SizeOf(THashValue), 0);
Result := False;
End;
End.