forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uCnvDictionary.pas
763 lines (665 loc) · 20 KB
/
uCnvDictionary.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
unit uCnvDictionary;
interface
{$i DelphiVersion_defines.inc}
{$IFDEF DELPHIXE}
{$DEFINE HAS_GENERICS}
{$ENDIF}
uses
{$IFDEF HAS_GENERICS}System.Generics.Collections{$ELSE}HashTrie{$ENDIF},
Windows;
type
(* AValue can be primitive wrapper(use if (AValue is TPrimitiveWrapper) then to check)
then you can access value:
TStringWrapper(AValue).Value
TIntegerWrapper(AValue).Value
etc.
*)
TCnvStringDictionaryEnumerateCallback = procedure(const AKey: string; const AValue: TObject; AUserData: Pointer) of object;
TCnvIntegerDictionaryEnumerateCallback = procedure(const AKey: Integer; const AValue: TObject; AUserData: Pointer) of object;
(* Associative array with string key implementation *)
TCnvStringDictionary = class
private
FDic: {$IFDEF HAS_GENERICS}TDictionary<string, TObject>{$ELSE}TStringHashTrie{$ENDIF};
FUserEnumerateCallback: TCnvStringDictionaryEnumerateCallback;
{$IFDEF HAS_GENERICS}
FCaseSensitive: Boolean;
function PrepareKey(const AKey: string): string; inline;
{$ENDIF}
{$IFNDEF HAS_GENERICS}
procedure EnumerateCallback(UserData: Pointer; Value: PChar; Data: TObject; var Done: Boolean);
{$ENDIF}
public
constructor Create(AAutoFreeObjects: Boolean = false; ACaseSensitive: Boolean = False);
destructor Destroy; override;
procedure AddOrSetValue(const AKey: string; const AValue: TObject); overload;
procedure AddOrSetValue(const AKey: string; const AValue: string); overload;
procedure AddOrSetValue(const AKey: string; const AValue: Integer); overload;
procedure AddOrSetValue(const AKey: string; const AValue: Int64); overload;
procedure AddOrSetValue(const AKey: string; const AValue: Double); overload;
procedure AddOrSetValue(const AKey: string; const AValue: Boolean); overload;
procedure AddOrSetValueDate(const AKey: string; const AValue: TDateTime);
procedure Remove(const AKey: string);
function TryGetValue(const AKey: string; out AValue: TObject): Boolean;
overload;
function TryGetValue(const AKey: string; out AValue: string): Boolean; overload;
function TryGetValue(const AKey: string; out AValue: Integer): Boolean; overload;
function TryGetValue(const AKey: string; out AValue: Int64): Boolean; overload;
function TryGetValue(const AKey: string; out AValue: Double): Boolean; overload;
function TryGetValue(const AKey: string; out AValue: Boolean): Boolean; overload;
function TryGetValueDate(const AKey: string; out AValue: TDateTime): Boolean;
function ContainsKey(const AKey: string): Boolean;
procedure Clear;
procedure Foreach(ACallback: TCnvStringDictionaryEnumerateCallback; AUserData: Pointer = nil);
end;
TStrHashTraverseProc = procedure(UserData: Pointer; Value: PChar;
Data: TObject; var Done: Boolean);
TStrHashTraverseMeth = procedure(UserData: Pointer; Value: PChar;
Data: TObject; var Done: Boolean) of object;
_TStringHashTrie = class(TCnvStringDictionary)
private
FStrHashTraverseProc : TStrHashTraverseProc;
FStrHashTraverseMeth : TStrHashTraverseMeth;
procedure ForEachCallback(const AKey: string; const AValue: TObject; AUserData: Pointer);
public
procedure Add(const S: string; Data: TObject); overload;
procedure Add(const s : string); overload;
procedure Delete(const S: string);
function Find(const S: string; var Data: TObject): Boolean; overload;
function Find(const s : string): Boolean; overload;
procedure Traverse(UserData: Pointer; UserProc: TStrHashTraverseProc); overload;
procedure Traverse(UserData: Pointer; UserProc: TStrHashTraverseMeth); overload;
end;
(* Associative array with integer key implementation *)
TCnvIntegerDictionary = class
private
FDic: {$IFDEF HAS_GENERICS}TDictionary<Integer, TObject>{$ELSE}TIntegerHashTrie{$ENDIF};
FUserEnumerateCallback: TCnvIntegerDictionaryEnumerateCallback;
{$IFNDEF HAS_GENERICS}
procedure EnumerateCallback(UserData: Pointer; Value: Integer; Data: TObject; var Done: Boolean);
{$ENDIF}
public
constructor Create(AAutoFreeObjects: Boolean = false);
destructor Destroy; override;
procedure AddOrSetValue(const AKey: Integer; const AValue: TObject); overload;
procedure AddOrSetValue(const AKey: Integer; const AValue: string); overload;
procedure AddOrSetValue(const AKey: Integer; const AValue: Integer); overload;
procedure AddOrSetValue(const AKey: Integer; const AValue: Int64); overload;
procedure AddOrSetValue(const AKey: Integer; const AValue: Double); overload;
procedure AddOrSetValue(const AKey: Integer; const AValue: Boolean); overload;
procedure AddOrSetValueDate(const AKey: Integer; const AValue: TDateTime);
procedure Remove(const AKey: Integer);
function TryGetValue(const AKey: Integer; out AValue: TObject): Boolean; overload;
function TryGetValue(const AKey: Integer; out AValue: string): Boolean; overload;
function TryGetValue(const AKey: Integer; out AValue: Integer): Boolean; overload;
function TryGetValue(const AKey: Integer; out AValue: Int64): Boolean; overload;
function TryGetValue(const AKey: Integer; out AValue: Double): Boolean; overload;
function TryGetValue(const AKey: Integer; out AValue: Boolean): Boolean; overload;
function TryGetValueDate(const AKey: Integer; out AValue: TDateTime): Boolean;
function ContainsKey(const AKey: Integer): Boolean;
procedure Clear;
procedure Foreach(ACallback: TCnvIntegerDictionaryEnumerateCallback; AUserData: Pointer = nil);
end;
TIntHashTraverseProc = procedure(UserData: Pointer; Value: integer;
Data: TObject; var Done: Boolean);
TIntHashTraverseMeth = procedure(UserData: Pointer; Value: integer;
Data: TObject; var Done: Boolean) of object;
_TIntegerHashTrie = class(TCnvIntegerDictionary)
private
FIntHashTraverseProc : TIntHashTraverseProc;
FIntHashTraverseMeth : TIntHashTraverseMeth;
procedure ForEachCallback(const AKey: Integer; const AValue: TObject;
AUserData: Pointer);
public
procedure Add(const n: integer; Data: TObject); overload;
procedure Delete(n: integer);
function Find(n: integer; var Data: TObject): Boolean; overload;
procedure Traverse(UserData: Pointer; UserProc: TIntHashTraverseProc); overload;
procedure Traverse(UserData: Pointer; UserProc: TIntHashTraverseMeth); overload;
function Find(n : integer): Boolean; overload;
procedure Add(n : integer); overload;
end;
(* wrapper classes to support primitives(assigned to TObject) *)
TPrimitiveWrapper = class
end;
TStringWrapper = class(TPrimitiveWrapper)
private
FValue: string;
public
constructor Create(AValue: string);
property Value: string read FValue write FValue;
end;
TIntegerWrapper = class(TPrimitiveWrapper)
private
FValue: Integer;
public
constructor Create(AValue: Integer);
property Value: Integer read FValue write FValue;
end;
TInt64Wrapper = class(TPrimitiveWrapper)
private
FValue: Int64;
public
constructor Create(AValue: Int64);
property Value: Int64 read FValue write FValue;
end;
TDoubleWrapper = class(TPrimitiveWrapper)
private
FValue: Extended;
public
constructor Create(AValue: Extended);
property Value: Extended read FValue write FValue;
end;
TBooleanWrapper = class(TPrimitiveWrapper)
private
FValue: Boolean;
public
constructor Create(AValue: Boolean);
property Value: Boolean read FValue write FValue;
end;
TDateTimeWrapper = class(TPrimitiveWrapper)
private
FValue: TDateTime;
public
constructor Create(AValue: TDateTime);
property Value: TDateTime read FValue write FValue;
end;
implementation
uses
SysUtils;
{ TCnvStringDictionary }
constructor TCnvStringDictionary.Create(AAutoFreeObjects: Boolean; ACaseSensitive: Boolean);
{$IFDEF HAS_GENERICS}
var
ownerships: TDictionaryOwnerships;
{$ENDIF}
begin
{$IFDEF HAS_GENERICS}
if AAutoFreeObjects then
ownerships := [doOwnsValues]
else
ownerships := [];
FDic := TObjectDictionary<string, TObject>.Create(ownerships);
FCaseSensitive := ACaseSensitive;
{$ELSE}
FDic := TStringHashTrie.Create;
FDic.AutoFreeObjects := AAutoFreeObjects;
FDic.CaseSensitive := ACaseSensitive;
{$ENDIF}
end;
destructor TCnvStringDictionary.Destroy;
begin
FDic.Free;
inherited;
end;
{$IFNDEF HAS_GENERICS}
procedure TCnvStringDictionary.EnumerateCallback(UserData: Pointer;
Value: PChar; Data: TObject; var Done: Boolean);
begin
FUserEnumerateCallback(Value, Data, UserData);
end;
{$ENDIF}
{$IFDEF HAS_GENERICS}
function TCnvStringDictionary.PrepareKey(const AKey: string): string;
begin
if not FCaseSensitive then
Result := UpperCase(AKey)
else
Result := AKey;
end;
{$ENDIF}
procedure TCnvStringDictionary.Foreach(ACallback: TCnvStringDictionaryEnumerateCallback;
AUserData: Pointer);
{$IFDEF HAS_GENERICS}
var
key: string;
{$ENDIF}
begin
FUserEnumerateCallback := ACallback;
{$IFDEF HAS_GENERICS}
for key in FDic.Keys do
FUserEnumerateCallback(key, FDic.Items[key], AUserData);
{$ELSE}
FDic.Traverse(AUserData, EnumerateCallback);
{$ENDIF}
end;
procedure TCnvStringDictionary.AddOrSetValue(const AKey: string;
const AValue: TObject);
begin
{$IFDEF HAS_GENERICS}
FDic.AddOrSetValue(PrepareKey(AKey), AValue);
{$ELSE}
FDic.Add(AKey, AValue);
{$ENDIF}
end;
procedure TCnvStringDictionary.AddOrSetValue(const AKey: string;
const AValue: Integer);
begin
AddOrSetValue(AKey, TIntegerWrapper.Create(AValue));
end;
procedure TCnvStringDictionary.AddOrSetValue(const AKey, AValue: string);
begin
AddOrSetValue(AKey, TStringWrapper.Create(AValue));
end;
procedure TCnvStringDictionary.AddOrSetValue(const AKey: string;
const AValue: Boolean);
begin
AddOrSetValue(AKey, TBooleanWrapper.Create(AValue));
end;
procedure TCnvStringDictionary.AddOrSetValueDate(const AKey: string;
const AValue: TDateTime);
begin
AddOrSetValue(AKey, TDateTimeWrapper.Create(AValue));
end;
procedure TCnvStringDictionary.AddOrSetValue(const AKey: string;
const AValue: Int64);
begin
AddOrSetValue(AKey, TInt64Wrapper.Create(AValue));
end;
procedure TCnvStringDictionary.AddOrSetValue(const AKey: string;
const AValue: Double);
begin
AddOrSetValue(AKey, TDoubleWrapper.Create(AValue));
end;
procedure TCnvStringDictionary.Clear;
begin
FDic.Clear;
end;
function TCnvStringDictionary.ContainsKey(const AKey: string): Boolean;
{$IFNDEF HAS_GENERICS}
var
stub: TObject;
{$ENDIF}
begin
{$IFDEF HAS_GENERICS}
Result := FDic.ContainsKey(AKey);
{$ELSE}
Result := FDic.Find(AKey, stub);
{$ENDIF}
end;
procedure TCnvStringDictionary.Remove(const AKey: string);
begin
{$IFDEF HAS_GENERICS}
FDic.Remove(PrepareKey(AKey));
{$ELSE}
FDic.Delete(AKey);
{$ENDIF}
end;
function TCnvStringDictionary.TryGetValue(const AKey: string; out AValue:
TObject): Boolean;
begin
{$IFDEF HAS_GENERICS}
Result := FDic.TryGetValue(PrepareKey(AKey), AValue);
{$ELSE}
Result := FDic.Find(AKey, AValue);
{$ENDIF}
end;
function TCnvStringDictionary.TryGetValue(const AKey: string;
out AValue: Integer): Boolean;
var
wrap: TIntegerWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvStringDictionary.TryGetValue(const AKey: string;
out AValue: string): Boolean;
var
wrap: TStringWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvStringDictionary.TryGetValue(const AKey: string;
out AValue: Int64): Boolean;
var
wrap: TInt64Wrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvStringDictionary.TryGetValue(const AKey: string;
out AValue: Double): Boolean;
var
wrap: TDoubleWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvStringDictionary.TryGetValue(const AKey: string;
out AValue: Boolean): Boolean;
var
wrap: TBooleanWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvStringDictionary.TryGetValueDate(const AKey: string;
out AValue: TDateTime): Boolean;
var
wrap: TDateTimeWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
{ TCnvIntegerDictionary }
procedure TCnvIntegerDictionary.AddOrSetValue(const AKey: Integer;
const AValue: TObject);
begin
{$IFDEF HAS_GENERICS}
FDic.AddOrSetValue(AKey, AValue);
{$ELSE}
FDic.Add(AKey, AValue);
{$ENDIF}
end;
procedure TCnvIntegerDictionary.AddOrSetValue(const AKey, AValue: Integer);
begin
AddOrSetValue(AKey, TIntegerWrapper.Create(AValue));
end;
procedure TCnvIntegerDictionary.AddOrSetValue(const AKey: Integer;
const AValue: string);
begin
AddOrSetValue(AKey, TStringWrapper.Create(AValue));
end;
procedure TCnvIntegerDictionary.AddOrSetValue(const AKey: Integer;
const AValue: Boolean);
begin
AddOrSetValue(AKey, TBooleanWrapper.Create(AValue));
end;
procedure TCnvIntegerDictionary.AddOrSetValueDate(const AKey: Integer;
const AValue: TDateTime);
begin
AddOrSetValue(AKey, TDateTimeWrapper.Create(AValue));
end;
procedure TCnvIntegerDictionary.AddOrSetValue(const AKey: Integer;
const AValue: Int64);
begin
AddOrSetValue(AKey, TInt64Wrapper.Create(AValue));
end;
procedure TCnvIntegerDictionary.AddOrSetValue(const AKey: Integer;
const AValue: Double);
begin
AddOrSetValue(AKey, TDoubleWrapper.Create(AValue));
end;
procedure TCnvIntegerDictionary.Clear;
begin
FDic.Clear;
end;
function TCnvIntegerDictionary.ContainsKey(const AKey: Integer): Boolean;
{$IFNDEF HAS_GENERICS}
var
stub: TObject;
{$ENDIF}
begin
{$IFDEF HAS_GENERICS}
Result := FDic.ContainsKey(AKey);
{$ELSE}
Result := FDic.Find(AKey, stub);
{$ENDIF}
end;
constructor TCnvIntegerDictionary.Create(AAutoFreeObjects: Boolean);
{$IFDEF HAS_GENERICS}
var
ownerships: TDictionaryOwnerships;
{$ENDIF}
begin
{$IFDEF HAS_GENERICS}
if AAutoFreeObjects then
ownerships := [doOwnsValues]
else
ownerships := [];
FDic := TObjectDictionary<Integer, TObject>.Create(ownerships);
{$ELSE}
FDic := TIntegerHashTrie.Create;
FDic.AutoFreeObjects := AAutoFreeObjects;
{$ENDIF}
end;
destructor TCnvIntegerDictionary.Destroy;
begin
FDic.Free;
inherited;
end;
{$IFNDEF HAS_GENERICS}
procedure TCnvIntegerDictionary.EnumerateCallback(UserData: Pointer;
Value: Integer; Data: TObject; var Done: Boolean);
begin
FUserEnumerateCallback(Value, Data, UserData);
end;
{$ENDIF}
procedure TCnvIntegerDictionary.Foreach(
ACallback: TCnvIntegerDictionaryEnumerateCallback; AUserData: Pointer);
{$IFDEF HAS_GENERICS}
var
key: Integer;
{$ENDIF}
begin
FUserEnumerateCallback := ACallback;
{$IFDEF HAS_GENERICS}
for key in FDic.Keys do
FUserEnumerateCallback(key, FDic.Items[key], AUserData);
{$ELSE}
FDic.Traverse(AUserData, EnumerateCallback);
{$ENDIF}
end;
procedure TCnvIntegerDictionary.Remove(const AKey: Integer);
begin
{$IFDEF HAS_GENERICS}
FDic.Remove(AKey);
{$ELSE}
FDic.Delete(AKey);
{$ENDIF}
end;
function TCnvIntegerDictionary.TryGetValue(const AKey: Integer;
out AValue: TObject): Boolean;
begin
{$IFDEF HAS_GENERICS}
Result := FDic.TryGetValue(AKey, AValue);
{$ELSE}
Result := FDic.Find(AKey, AValue);
{$ENDIF}
end;
function TCnvIntegerDictionary.TryGetValue(const AKey: Integer;
out AValue: Integer): Boolean;
var
wrap: TIntegerWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvIntegerDictionary.TryGetValue(const AKey: Integer;
out AValue: string): Boolean;
var
wrap: TStringWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvIntegerDictionary.TryGetValue(const AKey: Integer;
out AValue: Int64): Boolean;
var
wrap: TInt64Wrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvIntegerDictionary.TryGetValue(const AKey: Integer;
out AValue: Double): Boolean;
var
wrap: TDoubleWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvIntegerDictionary.TryGetValue(const AKey: Integer;
out AValue: Boolean): Boolean;
var
wrap: TBooleanWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
function TCnvIntegerDictionary.TryGetValueDate(const AKey: Integer;
out AValue: TDateTime): Boolean;
var
wrap: TDateTimeWrapper;
begin
Result := TryGetValue(AKey, TObject(wrap)) and (wrap <> nil);
if Result then
AValue := wrap.Value;
end;
{ TStringWrapper }
constructor TStringWrapper.Create(AValue: string);
begin
FValue := AValue;
end;
{ TIntegerWrapper }
constructor TIntegerWrapper.Create(AValue: Integer);
begin
FValue := AValue;
end;
{ TInt64Wrapper }
constructor TInt64Wrapper.Create(AValue: Int64);
begin
FValue := AValue;
end;
{ TDoubleWrapper }
constructor TDoubleWrapper.Create(AValue: Extended);
begin
FValue := AValue;
end;
{ TBooleanWrapper }
constructor TBooleanWrapper.Create(AValue: Boolean);
begin
FValue := AValue;
end;
{ TDateTimeWrapper }
constructor TDateTimeWrapper.Create(AValue: TDateTime);
begin
FValue := AValue;
end;
{ _TStringHashTrie }
procedure _TStringHashTrie.Add(const S: string; Data: TObject);
begin
AddOrSetValue(S, Data);
end;
procedure _TStringHashTrie.Add(const s : string);
begin
AddOrSetValue(S, nil);
end;
procedure _TStringHashTrie.Delete(const S: string);
begin
Remove(S);
end;
function _TStringHashTrie.Find(const S: string; var Data: TObject):
Boolean;
begin
Result := TryGetValue(S, Data);
end;
function _TStringHashTrie.Find(const s : string): Boolean;
var
Dummy : TObject;
begin
Result := TryGetValue(S, Dummy);
end;
procedure _TStringHashTrie.ForEachCallback(const AKey: string; const
AValue: TObject; AUserData: Pointer);
var
ADone : Boolean;
begin
ADone := False;
if assigned(FStrHashTraverseMeth) then
FStrHashTraverseMeth(AUserData, PChar(AKey), AValue, ADone)
else FStrHashTraverseProc(AUserData, PChar(AKey), AValue, ADone);
if ADone then
Abort;
end;
procedure _TStringHashTrie.Traverse(UserData: Pointer; UserProc:
TStrHashTraverseProc);
begin
FStrHashTraverseProc := UserProc;
FStrHashTraverseMeth := nil;
try
ForEach(ForEachCallback, UserData);
except
on EAbort do { Ignore }
end;
end;
procedure _TStringHashTrie.Traverse(UserData: Pointer; UserProc:
TStrHashTraverseMeth);
begin
FStrHashTraverseMeth := UserProc;
FStrHashTraverseProc := nil;
try
ForEach(ForEachCallback, UserData);
except
on EAbort do { Ignore }
end;
end;
{ _TIntegerHashTrie }
procedure _TIntegerHashTrie.Add(const n: integer; Data: TObject);
begin
AddOrSetValue(n, Data);
end;
procedure _TIntegerHashTrie.Add(n : integer);
begin
AddOrSetValue(n, nil);
end;
procedure _TIntegerHashTrie.Delete(n: integer);
begin
Remove(n);
end;
function _TIntegerHashTrie.Find(n: integer; var Data: TObject): Boolean;
begin
Result := TryGetValue(n, Data);
end;
function _TIntegerHashTrie.Find(n : integer): Boolean;
var
Data : TObject;
begin
Result := TryGetValue(n, Data);
end;
procedure _TIntegerHashTrie.ForEachCallback(const AKey: Integer; const AValue:
TObject; AUserData: Pointer);
var
ADone : Boolean;
begin
ADone := False;
if assigned(FIntHashTraverseMeth) then
FIntHashTraverseMeth(AUserData, AKey, AValue, ADone)
else FIntHashTraverseProc(AUserData, AKey, AValue, ADone);
if ADone then
Abort;
end;
procedure _TIntegerHashTrie.Traverse(UserData: Pointer; UserProc:
TIntHashTraverseProc);
begin
FIntHashTraverseProc := UserProc;
FIntHashTraverseMeth := nil;
try
Foreach(ForEachCallback, UserData);
except
on EAbort do { ignore }
end;
end;
procedure _TIntegerHashTrie.Traverse(UserData: Pointer; UserProc:
TIntHashTraverseMeth);
begin
FIntHashTraverseProc := nil;
FIntHashTraverseMeth := UserProc;
try
Foreach(ForEachCallback, UserData);
except
on EAbort do { ignore }
end;
end;
end.