forked from dim-s/soulengine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dwsHashtables.pas
440 lines (377 loc) · 9.37 KB
/
dwsHashtables.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
unit dwsHashtables;
{$I PHP.inc}
interface
uses SysUtils, ZendTypes {$IFDEF PHP_UNICODE}, WideStrUtils{$ENDIF};
type
ValueType = zend_ustr;
THashItem = class
Twin: THashItem;
Value: ValueType;
function HashCode: integer; virtual; abstract;
function Equals(Item: THashItem): boolean; reintroduce; virtual; abstract;
end;
PHashItems = ^THashItems;
THashItems = array[0..MaxInt div Sizeof(THashItem) - 1] of THashItem;
THashTable = class
public
FCapacity: integer;
FSize: integer;
FThreshold: integer;
FLoadFactor: integer; // In percent
FItems: PHashItems;
procedure Rehash(NewCapacity: integer);
protected
function InternalGet(Item: THashItem): ValueType;
function InternalFind(Item: THashItem): THashItem;
procedure InternalPut(Item: THashItem);
function InternalHasKey(Item: THashItem): boolean;
function InternalRemoveKey(Item: THashItem): ValueType;
public
constructor Create(InitCapacity: integer = 256; LoadFactor: integer = 75);
destructor Destroy; override;
procedure Clear;
property Capacity: integer read FCapacity;
property Size: integer read FSize;
end;
TStringHashItem = class(THashItem)
private
Key: string;
public
function HashCode: integer; override;
function Equals(Item: THashItem): boolean; override;
end;
TStringHashTable = class(THashTable)
private
FTestItem: TStringHashItem;
public
constructor Create(InitCapacity: integer = 256; LoadFactor: integer = 75);
destructor Destroy; override;
function Get(const Key: string): ValueType;
procedure SetValue(const Key: string; Value: ValueType);
procedure Put(const Key: string; Value: ValueType);
function HasKey(const Key: string): boolean;
function RemoveKey(const Key: string): ValueType;
end;
TIntegerHashItem = class(THashItem)
private
Key: integer;
public
function HashCode: integer; override;
function Equals(Item: THashItem): boolean; override;
end;
TIntegerHashTable = class(THashTable)
private
FTestItem: TIntegerHashItem;
public
constructor Create(InitCapacity: integer = 256; LoadFactor: integer = 75);
destructor Destroy; override;
function Get(const Key: integer): ValueType;
procedure Put(const Key: integer; Value: ValueType);
function HasKey(const Key: integer): boolean;
function RemoveKey(const Key: integer): ValueType;
end;
implementation
var
HashTable: array[#0..#255] of byte;
InsensitiveHashTable: array[#0..#255] of byte;
procedure InitTables;
var
I, K: zend_uchar;
Temp: integer;
begin
for I := #0 to #255 do
begin
HashTable[I] := Ord(I);
InsensitiveHashTable[I] := Ord(
{$IFDEF PHP_UNICODE}
Utf8UpperCase(Utf8String(I))[1]
{$ELSE}
AnsiString(AnsiUpperCase(string(I)))[1]
{$ENDIF}
);
end;
RandSeed := 111;
for I := #1 to #255 do
begin
repeat
K := zend_uchar(Random(255));
until K <> #0;
Temp := HashTable[I];
HashTable[I] := HashTable[K];
HashTable[K] := Temp;
end;
end;
{ THashTable }
constructor THashTable.Create(InitCapacity, LoadFactor: integer);
begin
if (InitCapacity < 1) or (InitCapacity >= MaxInt div Sizeof(integer)) then
raise Exception.CreateFmt('Invalid InitCapacity: %d', [InitCapacity]);
if (LoadFactor < 0) or (LoadFactor > 100) then
raise Exception.CreateFmt('Invalid LoadFactor: %d', [LoadFactor]);
FLoadFactor := LoadFactor;
Rehash(InitCapacity);
end;
destructor THashTable.Destroy;
begin
Clear;
FreeMem(FItems);
inherited;
end;
procedure THashTable.Clear;
var
x: integer;
oldItem, hashItem: THashItem;
begin
for x := 0 to FCapacity - 1 do
begin
hashItem := FItems[x];
while Assigned(hashItem) do
begin
oldItem := hashItem;
hashItem := hashItem.Twin;
oldItem.Free;
end;
FItems[x] := nil;
end;
end;
function THashTable.InternalFind(Item: THashItem): THashItem;
var
hashItem: THashItem;
begin
hashItem := FItems[Item.HashCode mod FCapacity];
while hashItem <> nil do
begin
if hashItem.Equals(Item) then
begin
Result := hashItem;
Exit;
end;
hashItem := hashItem.Twin;
end;
Result := hashItem;
end;
function THashTable.InternalGet(Item: THashItem): ValueType;
var
hashItem: THashItem;
begin
hashItem := FItems[Item.HashCode mod FCapacity];
while hashItem <> nil do
begin
if hashItem.Equals(Item) then
begin
Result := hashItem.Value;
Exit;
end;
hashItem := hashItem.Twin;
end;
Result := '';
end;
function THashTable.InternalHasKey(Item: THashItem): boolean;
var
hashItem: THashItem;
begin
Result := False;
hashItem := FItems[Item.HashCode mod FCapacity];
while hashItem <> nil do
begin
if hashItem.Equals(Item) then
begin
Result := True;
Exit;
end;
hashItem := hashItem.Twin;
end;
end;
procedure THashTable.InternalPut(Item: THashItem);
var
hash: integer;
begin
Inc(FSize);
if FSize > FThreshold then
// Double the size of the hashtable
Rehash(FCapacity * 2);
// Find item with same hash-key
// Insert new item to the existing (if any)
hash := Item.HashCode mod FCapacity;
Item.Twin := FItems[hash];
FItems[hash] := Item;
end;
function THashTable.InternalRemoveKey(Item: THashItem): ValueType;
var
hashItem, lastItem: THashItem;
hash: integer;
begin
hash := Item.HashCode mod FCapacity;
hashItem := FItems[hash];
lastItem := nil;
while hashItem <> nil do
begin
if hashItem.Equals(Item) then
begin
// Remove item from pointer chain
if lastItem = nil then
FItems[hash] := hashItem.Twin
else
lastItem.Twin := hashItem.Twin;
// Dispose item
Result := hashItem.Value;
hashItem.Free;
Dec(FSize);
Exit;
end;
lastItem := hashItem;
hashItem := hashItem.Twin;
end;
Result := '';
end;
procedure THashTable.Rehash(NewCapacity: integer);
var
x, hash: integer;
newItems: PHashItems;
itm, Twin: THashItem;
begin
// Enlarge the size of the hashtable
GetMem(newItems, Sizeof(THashItem) * NewCapacity);
// Clear new space
for x := 0 to NewCapacity - 1 do
newItems[x] := nil;
// Transfer items to the new hashtable
for x := 0 to FCapacity - 1 do
begin
itm := FItems[x];
while itm <> nil do
begin
Twin := itm.Twin;
hash := itm.HashCode mod NewCapacity;
itm.Twin := newItems[hash];
newItems[hash] := itm;
itm := Twin;
end;
end;
FreeMem(FItems);
FItems := newItems;
FThreshold := (NewCapacity div 100) * FLoadFactor;
FCapacity := NewCapacity;
end;
{ TStringHashItem }
function TStringHashItem.Equals(Item: THashItem): boolean;
begin
Result := SameText(Key, TStringHashItem(Item).Key);
end;
function TStringHashItem.HashCode: integer;
var
I: integer;
begin
Result := 0;
for i := 1 to length(Key) do
begin
Result := (Result shr 4) xor
(((Result xor InsensitiveHashTable[Key[I]]) and $F) * $80);
Result := (Result shr 4) xor
(((Result xor (Ord(InsensitiveHashTable[Key[I]]) shr 4)) and $F) * $80);
if I = 3 then
break;
end;
if Result = 0 then
Result := Length(Key) mod 8 + 1;
end;
{ TStringHashTable }
constructor TStringHashTable.Create(InitCapacity, LoadFactor: integer);
begin
inherited;
FTestItem := TStringHashItem.Create;
end;
destructor TStringHashTable.Destroy;
begin
inherited;
FTestItem.Free;
end;
function TStringHashTable.Get(const Key: string): ValueType;
begin
FTestItem.Key := Key;
Result := InternalGet(FTestItem);
end;
function TStringHashTable.HasKey(const Key: string): boolean;
begin
FTestItem.Key := Key;
Result := InternalHasKey(FTestItem);
end;
procedure TStringHashTable.Put(const Key: string; Value: ValueType);
var
item: TStringHashItem;
begin
item := TStringHashItem.Create;
item.Key := Key;
item.Value := Value;
InternalPut(item);
end;
function TStringHashTable.RemoveKey(const Key: string): ValueType;
begin
FTestItem.Key := Key;
Result := InternalRemoveKey(FTestItem);
end;
procedure TStringHashTable.SetValue(const Key: string; Value: ValueType);
var
item, ret: TStringHashItem;
begin
item := TStringHashItem.Create;
item.Key := Key;
ret := TStringHashItem(InternalFind(item));
if ret <> nil then
begin
ret.Value := Value;
item.Free;
end
else
begin
item.Value := Value;
InternalPut(item);
end;
end;
{ TIntegerHashItem }
function TIntegerHashItem.Equals(Item: THashItem): boolean;
begin
Result := Key = TIntegerHashItem(Item).Key;
end;
function TIntegerHashItem.HashCode: integer;
begin
Result := Key;
end;
{ TIntegerHashTable }
constructor TIntegerHashTable.Create(InitCapacity, LoadFactor: integer);
begin
inherited;
FTestItem := TIntegerHashItem.Create;
end;
destructor TIntegerHashTable.Destroy;
begin
FTestItem.Free;
inherited;
end;
function TIntegerHashTable.Get(const Key: integer): ValueType;
begin
FTestItem.Key := Key;
Result := InternalGet(FTestItem);
end;
function TIntegerHashTable.HasKey(const Key: integer): boolean;
begin
FTestItem.Key := Key;
Result := InternalHasKey(FTestItem);
end;
procedure TIntegerHashTable.Put(const Key: integer; Value: ValueType);
var
item: TIntegerHashItem;
begin
item := TIntegerHashItem.Create;
item.Key := Key;
item.Value := Value;
InternalPut(item);
end;
function TIntegerHashTable.RemoveKey(const Key: integer): ValueType;
begin
FTestItem.Key := Key;
Result := InternalRemoveKey(FTestItem);
end;
initialization
InitTables
end.