-
Notifications
You must be signed in to change notification settings - Fork 1
/
iardict.pas
452 lines (390 loc) · 10.5 KB
/
iardict.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
unit iardict;
{
TIarDict (Iaramaz Dictionary) - Simple & Bad separate chaining Hash Table.
Copyright (C) 2020, I. Kakoulidis, all right reserved.
}
{$ifdef FPC}
{$mode delphi}
{$macro on}
{$undef USE_MACRO}
{$endif}
{$inline on}
{$pointermath on}
interface
uses
Classes, SysUtils;
type
THash = function(key: PChar; keyLen: NativeUInt): UInt32;
PIarDict = ^TIarDict;
{ TIarDict }
TIarDict = record
private
FTable: PPChar;
FCapacity, FKeyNum: NativeUInt;
FHash: THash;
procedure Grow; inline;
procedure Shrink; inline;
procedure Resize(cap: NativeUInt);
function FindNode(key: PChar; keyLen: NativeUInt;
out block: NativeUInt; out prev: PChar): PChar;
public
procedure Init(hash: THash); overload;
procedure Init(hash: THash; cap: NativeUInt); overload;
procedure Clear;
function UsedBlocks: NativeUInt; inline;
function Find(key: PChar; keyLen: NativeUInt; out value: NativeUInt): boolean;
function Remove(key: PChar; keyLen: NativeUInt): boolean;
function Insert(key: PChar; keyLen: NativeUInt; value: NativeUInt = 0): integer;
function SetValues(value: NativeUInt): NativeUInt;
property keyNum: NativeUInt read FKeyNum;
property capacity: NativeUInt read FCapacity;
end;
function KN_Create(key: PChar; keyLen: NativeUInt): PChar; inline;
{$ifdef USE_MACRO}
{$define KN_NextKN := PPChar(kn) }
{$define KN_NextPN := PPChar(pn) }
{$define KN_KeyLen := PNativeUInt(kn + SizeOf(PChar)) }
{$define KN_Key := (kn + (SizeOf(PChar) + SizeOf(NativeUInt))) }
{$define KN_Value := PPointer(kn + (SizeOf(PChar) + SizeOf(NativeUInt)) + PNativeUInt(kn + SizeOf(PChar))^) }
{$else}
function KN_Next(kn: PChar): PPChar; inline;
function KN_KeyLen(kn: PChar): PNativeUInt; inline;
function KN_Key(kn: PChar): PChar; inline;
function KN_Value(kn: PChar): PNativeUInt; inline;
{$endif}
(* function CmpMem(a, b: PChar; len: NativeUInt): boolean; inline; *)
function MemEq(a, b: PChar; len: NativeUInt): boolean; inline;
procedure MemCpy(src, dst: PChar; len: NativeUInt); inline;
{ A fast alternative to the modulo reduction.
https://github.com/lemire/fastrange }
function FastRange32(x, r: UInt32): UInt32; inline;
implementation
const
BASE_CAPACITY = 1 shl 3; (* 8 *)
{ TIarDict }
procedure TIarDict.Grow;
begin
if FKeyNum > FCapacity then
Resize(7*(FCapacity shr 2));
end;
procedure TIarDict.Shrink;
begin
if 4*FKeyNum < FCapacity then
if FCapacity >= 2*BASE_CAPACITY then
Resize(FCapacity div 2);
end;
procedure TIarDict.Resize(cap: NativeUInt);
var
i, bn: NativeUInt;
tbl: PPChar;
kn, nn: PChar;
begin
tbl := AllocMem(SizeOf(PChar) * cap);
for i := 0 to FCapacity - 1 do
begin
kn := FTable[i];
while kn <> nil do
begin
(* Rehash and insert *)
{$ifdef USE_MACRO}
nn := KN_NextKN^;
(* bn := FHash(KN_Key(kn), KN_KeyLen(kn)^) and (cap - 1); *)
bn := FastRange32(FHash(KN_Key, KN_KeyLen^), cap);
KN_NextKN^ := tbl[bn];
{$else}
nn := KN_Next(kn)^;
bn := FastRange32(FHash(KN_Key(kn), KN_KeyLen(kn)^), cap);
KN_Next(kn)^ := tbl[bn];
{$endif}
tbl[bn] := kn;
kn := nn;
end;
end;
FreeMem(FTable);
FTable := tbl;
FCapacity := cap;
end;
function TIarDict.FindNode(key: PChar; keyLen: NativeUInt;
out block: NativeUInt; out prev: PChar): PChar;
var
bn: NativeUInt;
kn, pn: PChar;
begin
pn := nil;
//bn := FHash(key, keyLen) and (FCapacity - 1);
bn := FastRange32(FHash(key, keyLen), FCapacity);
kn := FTable[bn];
block := bn;
while kn <> nil do
begin
{$ifdef USE_MACRO}
if (KN_KeyLen^ = keylen) and (CompareMem(KN_Key, key, keyLen) = True) then
{$else}
if (KN_KeyLen(kn)^ = keylen) and (MemEq(KN_Key(kn), key, keyLen) = True) then
//if (KN_KeyLen(kn)^ = keylen) and (CompareMem(KN_Key(kn), key, keyLen) = True) then
{$endif}
begin
prev := pn;
Exit(kn);
end;
pn := kn;
{$ifdef USE_MACRO} kn := KN_NextKN^;
{$else} kn := KN_Next(kn)^; {$endif}
end;
prev := pn;
Exit(kn);
end;
procedure TIarDict.Init(hash: THash);
begin
FCapacity := BASE_CAPACITY;
FKeyNum := 0;
FTable := AllocMem(SizeOf(PChar) * FCapacity);
Fhash := hash;
end;
procedure TIarDict.Init(hash: THash; cap: NativeUInt);
begin
if cap < BASE_CAPACITY then
cap := BASE_CAPACITY;
FCapacity := cap;
FKeyNum := 0;
FTable := AllocMem(SizeOf(PChar) * cap);
Fhash := hash;
end;
procedure TIarDict.Clear;
var
i: NativeUInt;
kn, nn: PChar;
begin
for i := 0 to FCapacity - 1 do
begin
kn := FTable[i];
while kn <> nil do
begin
{$ifdef USE_MACRO} nn := KN_NextKN^;
{$else} nn := KN_Next(kn)^; {$endif}
FreeMem(kn);
kn := nn;
end;
end;
FreeMem(FTable);
end;
function TIarDict.UsedBlocks: NativeUInt;
var
i: NativeUInt;
begin
Result := 0;
for i := 0 to FCapacity - 1 do
if FTable[i] <> nil then
Inc(Result);
end;
function TIarDict.Find(key: PChar; keyLen: NativeUInt; out value: NativeUInt): boolean;
var
bn: NativeUInt;
kn, pn: PChar;
begin
value := 0;
if (key = nil) or (keyLen = 0) then
Exit(false);
kn := FindNode(key, keyLen, bn, pn);
if kn <> nil then
begin
{$ifdef USE_MACRO} value := KN_Value^;
{$else} value := KN_value(kn)^; {$endif}
Result := true;
end
else
Result := false;
end;
function TIarDict.Remove(key: PChar; keyLen: NativeUInt): boolean;
var
bn: NativeUInt;
kn, pn: PChar;
begin
if (key = nil) or (keyLen = 0) then
Exit(false);
kn := FindNode(key, keyLen, bn, pn);
if kn <> nil then
begin
{$ifdef USE_MACRO}
if pn <> nil then
KN_NextPN^ := KN_NextKN^
else
FTable[bn] := KN_NextKN^;
{$else}
if pn <> nil then
KN_Next(pn)^ := KN_Next(kn)^
else
FTable[bn] := KN_Next(kn)^;
{$endif}
Freemem(kn);
Dec(FKeyNum);
Shrink();
Result := true;
end
else
Result := false;
end;
function TIarDict.Insert(key: PChar; keyLen: NativeUInt; value: NativeUInt = 0): integer;
var
bn: NativeUInt;
kn, pn: PChar;
begin
if (key = nil) or (keyLen = 0) then
Exit(-1);
kn := FindNode(key, keyLen, bn, pn);
if kn <> nil then
begin
{$ifdef USE_MACRO} KN_Value^ := value;
{$else} KN_Value(kn)^ := value; {$endif}
Exit(0);
end
else
begin
Inc(FKeyNum);
kn := KN_Create(key, keyLen);
{$ifdef USE_MACRO}
KN_NextKN^ := FTable[bn];
KN_Value^ := value;
{$else}
KN_Next(kn)^ := FTable[bn];
KN_Value(kn)^ := value;
{$endif}
FTable[bn] := kn;
Grow();
Exit(1);
end;
end;
function TIarDict.SetValues(value: NativeUInt): NativeUInt;
var
i: NativeUInt;
kn, nn: PChar;
begin
Result := 0;
for i := 0 to FCapacity - 1 do
begin
kn := FTable[i];
while kn <> nil do
begin
{$ifdef USE_MACRO}
nn := KN_NextKN^;
KN_Value^ := value;
{$else}
nn := KN_Next(kn)^;
KN_Value(kn)^ := value;
{$endif}
Inc(Result);
kn := nn;
end;
end;
end;
{ KeyNode Layout
next - PChar offset - 0
value - NativeUInt offset - SizeOf(NativeUInt)
keeLen - NativeUInt offset - SizeOf(PChar) + SizeOf(NativeUInt)
key - char[] offset - SizeOf(PChar) + SizeOf(NativeUInt) + SizeOf(NativeUInt)
Total - SizeOf(PChar) + SizeOf(NativeUInt) + SizeOf(NativeUInt) + keyLen
}
function KN_Create(key: PChar; keyLen: NativeUInt): PChar;
var
kn: PChar;
begin
kn := GetMem((SizeOf(PChar) + SizeOf(NativeUInt) + SizeOf(NativeUInt)) + keyLen);
(* Copy key *)
{$ifndef USE_MACRO}
KN_Next(kn)^ := nil;
KN_Value(kn)^ := 0;
KN_KeyLen(kn)^ := keyLen;
{$else}
KN_NextKN^ := nil;
KN_KeyLen^ := keyLen;
KN_Value^ := nil;
{$endif}
Move(key^, (kn + (SizeOf(PChar) + SizeOf(NativeUInt) + SizeOf(NativeUInt)))^, keyLen);
//MemCpy(key, kn + (SizeOf(PChar) + SizeOf(NativeUInt) + SizeOf(NativeUInt)), keyLen);
Result := kn;
end;
{$ifndef USE_MACRO}
function KN_Next(kn: PChar): PPChar; inline;
begin
Result := PPChar(kn);
end;
function KN_Value(kn: PChar): PNativeUInt; inline;
begin
Result := PNativeUInt(kn + SizeOf(PChar));
end;
function KN_KeyLen(kn: PChar): PNativeUInt; inline;
begin
Result := PNativeUInt(kn + (SizeOf(PChar) + SizeOf(NativeUInt)));
end;
function KN_Key(kn: PChar): PChar; inline;
begin
Result := kn + (SizeOf(PChar) + SizeOf(NativeUInt) + SizeOf(NativeUInt));
end;
{$endif}
function FastRange32(x, r: UInt32): UInt32; inline;
begin
Result := UInt32((UInt64(x) * UInt64(r)) shr 32);
end;
(* Not secure memcmp() *)
function MemEq(a, b: PChar; len: NativeUInt): boolean; inline;
begin
while len >= 4*SizeOf(NativeUInt) do
begin
if PNativeUInt(a)^ <> PNativeUInt(b)^ then Exit(false);
Inc(a, SizeOf(NativeUInt)); Inc(b, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
if PNativeUInt(a)^ <> PNativeUInt(b)^ then Exit(false);
Inc(a, SizeOf(NativeUInt)); Inc(b, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
if PNativeUInt(a)^ <> PNativeUInt(b)^ then Exit(false);
Inc(a, SizeOf(NativeUInt)); Inc(b, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
if PNativeUInt(a)^ <> PNativeUInt(b)^ then Exit(false);
Inc(a, SizeOf(NativeUInt)); Inc(b, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
end;
while len >= SizeOf(NativeUInt) do
begin
if PNativeUInt(a)^ <> PNativeUInt(b)^ then Exit(false);
Inc(a, SizeOf(NativeUInt)); Inc(b, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
end;
while len > 0 do
begin
if a^ <> b^ then Exit(false);
Inc(a); Inc(b); Dec(len);
end;
Exit(true);
end;
procedure MemCpy(src, dst: PChar; len: NativeUInt); inline;
begin
while len >= 4*SizeOf(NativeUInt) do
begin
PNativeUInt(dst)^ := PNativeUInt(src)^;
Inc(src, SizeOf(NativeUInt)); Inc(dst, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
PNativeUInt(dst)^ := PNativeUInt(src)^;
Inc(src, SizeOf(NativeUInt)); Inc(dst, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
PNativeUInt(dst)^ := PNativeUInt(src)^;
Inc(src, SizeOf(NativeUInt)); Inc(dst, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
PNativeUInt(dst)^ := PNativeUInt(src)^;
Inc(src, SizeOf(NativeUInt)); Inc(dst, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
end;
while len >= SizeOf(NativeUInt) do
begin
PNativeUInt(dst)^ := PNativeUInt(src)^;
Inc(src, SizeOf(NativeUInt)); Inc(dst, SizeOf(NativeUInt));
Dec(len, SizeOf(NativeUInt));
end;
while len > 0 do
begin
dst^ := src^;
Inc(src); Inc(dst); Dec(len);
end;
end;
initialization
finalization
end.