-
Notifications
You must be signed in to change notification settings - Fork 4
/
lptypes.pas
2251 lines (1933 loc) · 64.7 KB
/
lptypes.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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Author: Niels A.D
Project: Lape (http://code.google.com/p/la-pe/)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
General basetypes and objects.
}
unit lptypes;
{$I lape.inc}
interface
uses
Classes, SysUtils;
const
LapeCaseSensitive = {$IFDEF Lape_CaseSensitive}True{$ELSE}False{$ENDIF};
LapeSystemCaseSensitive = {$IFDEF Unix}True{$ELSE}False{$ENDIF};
type
UInt8 = Byte;
Int8 = ShortInt;
UInt16 = Word;
Int16 = SmallInt;
UInt32 = LongWord;
Int32 = LongInt;
//UInt64 = QWord; Already defined
//Int64 = Int64; Already defined
PUInt8 = ^UInt8;
PInt8 = ^Int8;
PUInt16 = ^UInt16;
PInt16 = ^Int16;
PUInt32 = ^UInt32;
PInt32 = ^Int32;
PUInt64 = ^UInt64;
//PInt64 = ^Int64; Already defined
PByteBool = ^ByteBool;
{$IFDEF FPC}
NativeInt = PtrInt;
NativeUInt = PtrUInt;
PNativeInt = ^NativeInt;
PNativeUInt = ^NativeUInt;
{$ELSE}
PtrInt = NativeInt;
PtrUInt = NativeUInt;
PPtrInt = ^PtrInt;
PPtrUInt = ^PtrUInt;
SizeInt = {$IFDEF CPUX64}Int64{$ELSE}Int32{$ENDIF};
SizeUInt = {$IFDEF CPUX64}UInt64{$ELSE}UInt32{$ENDIF};
PSizeInt = ^SizeInt;
PSizeUInt = ^SizeUInt;
PLongBool = ^LongBool;
{$ENDIF}
{$IFDEF Lape_Unicode}
lpString = UnicodeString;
lpChar = WideChar;
lpCharInt = UInt16;
{$ELSE}
lpString = AnsiString;
lpChar = AnsiChar;
lpCharInt = UInt8;
{$ENDIF}
PlpString = ^lpString;
PlpChar = ^lpChar;
PlpCharInt = ^lpCharInt;
TVarRecArray = array of TVarRec;
TVarRecContainer = {$IFDEF Lape_SmallCode}packed{$ENDIF} record
CVar: Variant;
case SizeInt of
vtExtended: (CExtended: Extended);
vtString : (CShortString: shortstring);
vtCurrency: (CCurrency: Currency);
vtInt64 : (CInt64: Int64);
{$IFDEF FPC}
vtQWord : (CQWord: QWord);
{$ENDIF}
end;
TVarRecContainerArray = array of TVarRecContainer;
TVarRecList = {$IFDEF Lape_SmallCode}packed{$ENDIF} record
Containers: TVarRecContainerArray;
VarRecs: TVarRecArray;
end;
TStringArray = array of lpString;
TByteArray = array of Byte;
TIntegerArray = array of Integer;
TInitBool = (bUnknown, bFalse, bTrue);
TCodeArray = TByteArray;
PCodeArray = ^TCodeArray;
PParamArray = ^TParamArray;
TParamArray = array[Word] of Pointer;
PDocPos = ^TDocPos;
TDocPos = record
Line, Col: {$IFDEF Lape_SmallCode}UInt16{$ELSE}UInt32{$ENDIF};
FileName: lpString;
end;
TLapeRange = record
Lo, Hi: Int64;
end;
TCodePos = SizeUInt;
PCodePos = ^TCodePos;
TCodeOffset = SizeInt;
PCodeOffset = ^TCodeOffset;
{$IFDEF Lape_SmallCode}
//Means Lape can only locate up to 65kb of local variables (per stackframe)
TStackInc = Int16;
TStackOffset = UInt16;
TVarStackOffset = UInt16;
TPointerOffset = Int16;
TParamSize = UInt16;
EvalBool = Boolean;
{$ELSE}
TStackInc = NativeInt;
TStackOffset = NativeUInt;
TVarStackOffset = NativeUInt;
TPointerOffset = NativeInt;
TParamSize = NativeUInt;
EvalBool = LongBool;
{$ENDIF}
PStackInc = ^TStackInc;
PStackOffset = ^TStackOffset;
PVarStackOffset = ^TVarStackOffset;
PPointerOffset = ^TPointerOffset;
PParamSize = ^TParamSize;
PEvalBool = ^EvalBool;
EMemoryPos = (mpNone, mpStack, mpMem, mpVar);
TLapeEvalProc = procedure(const Dest, Left, Right: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
TLapeImportedProc = procedure(const Params: PParamArray); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
TLapeImportedFunc = procedure(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
ELoopType = (loopUp, loopDown, loopOver);
ELapeBaseType = (
ltUnknown,
ltUInt8, ltInt8, ltUInt16, ltInt16, ltUInt32, ltInt32, ltUInt64, ltInt64, //Integer
ltCurrency, ltSingle, ltDouble, ltExtended, //Real
ltBoolean, ltByteBool, ltWordBool, ltLongBool, //Boolean
ltAnsiChar, ltWideChar, //Char
ltShortString, ltAnsiString, ltWideString, ltUnicodeString, //String
ltVariant, //Variant
ltSmallEnum, ltLargeEnum, ltSmallSet, ltLargeSet, //Set
ltPointer, //Pointer
ltRecord, ltUnion, //Struct
ltDynArray, ltStaticArray, //Array
ltScriptMethod, ltImportedMethod //Methods
);
LapeIntegerTypeRange = ltUInt8..ltInt64;
EOperatorAssociative = (assocNone, assocLeft, assocRight);
EOperator = (
op_Unknown,
//Same order as lpparser.EParserToken
op_cmp_Equal,
op_cmp_GreaterThan,
op_cmp_GreaterThanOrEqual,
op_cmp_LessThan,
op_cmp_LessThanOrEqual,
op_cmp_NotEqual,
op_Addr,
op_AND,
op_Assign,
op_AssignDiv,
op_AssignMinus,
op_AssignMul,
op_AssignPlus,
op_Deref,
op_DIV,
op_Divide,
op_Dot,
op_IN,
op_IS,
op_Index,
op_Minus,
op_MOD,
op_Multiply,
op_NOT,
op_OR,
op_Plus,
op_Power,
op_SHL,
op_SHR,
op_XOR,
//Extra
op_UnaryMinus,
op_UnaryPlus
);
ELapeSmallEnum = (__LapeSmallEnum1,__LapeSmallEnum2,__LapeSmallEnum3,__LapeSmallEnum4,__LapeSmallEnum5,__LapeSmallEnum6,__LapeSmallEnum7,__LapeSmallEnum8,__LapeSmallEnum9,__LapeSmallEnum10,__LapeSmallEnum11,__LapeSmallEnum12,__LapeSmallEnum13,__LapeSmallEnum14,__LapeSmallEnum15,__LapeSmallEnum16,__LapeSmallEnum17,__LapeSmallEnum18,__LapeSmallEnum19,__LapeSmallEnum20,__LapeSmallEnum21,__LapeSmallEnum22,__LapeSmallEnum23,__LapeSmallEnum24,__LapeSmallEnum25,__LapeSmallEnum26,__LapeSmallEnum27,__LapeSmallEnum28,__LapeSmallEnum29,__LapeSmallEnum30,__LapeSmallEnum31,__LapeSmallEnum32);
ELapeLargeEnum = (__LapeLargeEnum1,__LapeLargeEnum2,__LapeLargeEnum3,__LapeLargeEnum4,__LapeLargeEnum5,__LapeLargeEnum6,__LapeLargeEnum7,__LapeLargeEnum8,__LapeLargeEnum9,__LapeLargeEnum10,__LapeLargeEnum11,__LapeLargeEnum12,__LapeLargeEnum13,__LapeLargeEnum14,__LapeLargeEnum15,__LapeLargeEnum16,__LapeLargeEnum17,__LapeLargeEnum18,__LapeLargeEnum19,__LapeLargeEnum20,__LapeLargeEnum21,__LapeLargeEnum22,__LapeLargeEnum23,__LapeLargeEnum24,__LapeLargeEnum25,__LapeLargeEnum26,__LapeLargeEnum27,__LapeLargeEnum28,__LapeLargeEnum29,__LapeLargeEnum30,__LapeLargeEnum31,__LapeLargeEnum32,__LapeLargeEnum33,__LapeLargeEnum34,__LapeLargeEnum35,__LapeLargeEnum36,__LapeLargeEnum37,__LapeLargeEnum38,__LapeLargeEnum39,__LapeLargeEnum40,__LapeLargeEnum41,__LapeLargeEnum42,__LapeLargeEnum43,__LapeLargeEnum44,__LapeLargeEnum45,__LapeLargeEnum46,__LapeLargeEnum47,__LapeLargeEnum48,__LapeLargeEnum49,__LapeLargeEnum50,
__LapeLargeEnum51,__LapeLargeEnum52,__LapeLargeEnum53,__LapeLargeEnum54,__LapeLargeEnum55,__LapeLargeEnum56,__LapeLargeEnum57,__LapeLargeEnum58,__LapeLargeEnum59,__LapeLargeEnum60,__LapeLargeEnum61,__LapeLargeEnum62,__LapeLargeEnum63,__LapeLargeEnum64,__LapeLargeEnum65,__LapeLargeEnum66,__LapeLargeEnum67,__LapeLargeEnum68,__LapeLargeEnum69,__LapeLargeEnum70,__LapeLargeEnum71,__LapeLargeEnum72,__LapeLargeEnum73,__LapeLargeEnum74,__LapeLargeEnum75,__LapeLargeEnum76,__LapeLargeEnum77,__LapeLargeEnum78,__LapeLargeEnum79,__LapeLargeEnum80,__LapeLargeEnum81,__LapeLargeEnum82,__LapeLargeEnum83,__LapeLargeEnum84,__LapeLargeEnum85,__LapeLargeEnum86,__LapeLargeEnum87,__LapeLargeEnum88,__LapeLargeEnum89,__LapeLargeEnum90,__LapeLargeEnum91,__LapeLargeEnum92,__LapeLargeEnum93,__LapeLargeEnum94,__LapeLargeEnum95,__LapeLargeEnum96,__LapeLargeEnum97,__LapeLargeEnum98,__LapeLargeEnum99,__LapeLargeEnum100,
__LapeLargeEnum101,__LapeLargeEnum102,__LapeLargeEnum103,__LapeLargeEnum104,__LapeLargeEnum105,__LapeLargeEnum106,__LapeLargeEnum107,__LapeLargeEnum108,__LapeLargeEnum109,__LapeLargeEnum110,__LapeLargeEnum111,__LapeLargeEnum112,__LapeLargeEnum113,__LapeLargeEnum114,__LapeLargeEnum115,__LapeLargeEnum116,__LapeLargeEnum117,__LapeLargeEnum118,__LapeLargeEnum119,__LapeLargeEnum120,__LapeLargeEnum121,__LapeLargeEnum122,__LapeLargeEnum123,__LapeLargeEnum124,__LapeLargeEnum125,__LapeLargeEnum126,__LapeLargeEnum127,__LapeLargeEnum128,__LapeLargeEnum129,__LapeLargeEnum130,__LapeLargeEnum131,__LapeLargeEnum132,__LapeLargeEnum133,__LapeLargeEnum134,__LapeLargeEnum135,__LapeLargeEnum136,__LapeLargeEnum137,__LapeLargeEnum138,__LapeLargeEnum139,__LapeLargeEnum140,__LapeLargeEnum141,__LapeLargeEnum142,__LapeLargeEnum143,__LapeLargeEnum144,__LapeLargeEnum145,__LapeLargeEnum146,__LapeLargeEnum147,__LapeLargeEnum148,__LapeLargeEnum149,__LapeLargeEnum150,
__LapeLargeEnum151,__LapeLargeEnum152,__LapeLargeEnum153,__LapeLargeEnum154,__LapeLargeEnum155,__LapeLargeEnum156,__LapeLargeEnum157,__LapeLargeEnum158,__LapeLargeEnum159,__LapeLargeEnum160,__LapeLargeEnum161,__LapeLargeEnum162,__LapeLargeEnum163,__LapeLargeEnum164,__LapeLargeEnum165,__LapeLargeEnum166,__LapeLargeEnum167,__LapeLargeEnum168,__LapeLargeEnum169,__LapeLargeEnum170,__LapeLargeEnum171,__LapeLargeEnum172,__LapeLargeEnum173,__LapeLargeEnum174,__LapeLargeEnum175,__LapeLargeEnum176,__LapeLargeEnum177,__LapeLargeEnum178,__LapeLargeEnum179,__LapeLargeEnum180,__LapeLargeEnum181,__LapeLargeEnum182,__LapeLargeEnum183,__LapeLargeEnum184,__LapeLargeEnum185,__LapeLargeEnum186,__LapeLargeEnum187,__LapeLargeEnum188,__LapeLargeEnum189,__LapeLargeEnum190,__LapeLargeEnum191,__LapeLargeEnum192,__LapeLargeEnum193,__LapeLargeEnum194,__LapeLargeEnum195,__LapeLargeEnum196,__LapeLargeEnum197,__LapeLargeEnum198,__LapeLargeEnum199,__LapeLargeEnum200,
__LapeLargeEnum201,__LapeLargeEnum202,__LapeLargeEnum203,__LapeLargeEnum204,__LapeLargeEnum205,__LapeLargeEnum206,__LapeLargeEnum207,__LapeLargeEnum208,__LapeLargeEnum209,__LapeLargeEnum210,__LapeLargeEnum211,__LapeLargeEnum212,__LapeLargeEnum213,__LapeLargeEnum214,__LapeLargeEnum215,__LapeLargeEnum216,__LapeLargeEnum217,__LapeLargeEnum218,__LapeLargeEnum219,__LapeLargeEnum220,__LapeLargeEnum221,__LapeLargeEnum222,__LapeLargeEnum223,__LapeLargeEnum224,__LapeLargeEnum225,__LapeLargeEnum226,__LapeLargeEnum227,__LapeLargeEnum228,__LapeLargeEnum229,__LapeLargeEnum230,__LapeLargeEnum231,__LapeLargeEnum232,__LapeLargeEnum233,__LapeLargeEnum234,__LapeLargeEnum235,__LapeLargeEnum236,__LapeLargeEnum237,__LapeLargeEnum238,__LapeLargeEnum239,__LapeLargeEnum240,__LapeLargeEnum241,__LapeLargeEnum242,__LapeLargeEnum243,__LapeLargeEnum244,__LapeLargeEnum245,__LapeLargeEnum246,__LapeLargeEnum247,__LapeLargeEnum248,__LapeLargeEnum249,__LapeLargeEnum250,
__LapeLargeEnum251,__LapeLargeEnum252,__LapeLargeEnum253,__LapeLargeEnum254,__LapeLargeEnum255,__LapeLargeEnum256);
TLapeSmallSet = set of ELapeSmallEnum;
TLapeLargeSet = set of ELapeLargeEnum;
PLapeSmallEnum = ^ELapeSmallEnum;
PLapeLargeEnum = ^ELapeLargeEnum;
PLapeSmallSet = ^TLapeSmallSet;
PLapeLargeSet = ^TLapeLargeSet;
{$IF DEFINED(FPC) AND (NOT DEFINED(MSWINDOWS)) AND (FPC_FULLVERSION >= 20501)}
{$DEFINE Interface_CDecl}
{$IFEND}
TLapeBaseClass = class(TObject, IUnknown)
protected
function _AddRef: Integer; {$IFDEF Interface_CDecl}cdecl{$ELSE}stdcall{$ENDIF};
function _Release: Integer; {$IFDEF Interface_CDecl}cdecl{$ELSE}stdcall{$ENDIF};
public
constructor Create; virtual;
{$IFDEF Lape_TrackObjects}
destructor Destroy; override;
{$ENDIF}
function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult; {$IFDEF Interface_CDecl}cdecl{$ELSE}stdcall{$ENDIF};
function GetSelf: TLapeBaseClass; inline;
end;
TLapeBaseDeclClass = class(TLapeBaseClass)
protected
function getDocPos: TDocPos; virtual; abstract;
public
Tag: Int64;
property DocPos: TDocPos read getDocPos;
end;
{$IFDEF FPC}generic{$ENDIF} TLapeStack<_T> = class(TLapeBaseClass)
public type
TTArray = array of _T;
var protected
FArr: TTArray;
FLen: Integer;
FCur: Integer;
procedure Grow(AGrowSize: Integer); virtual;
procedure CheckIndex(Index: Integer; GrowIfNeeded: Boolean = False); virtual;
function getItem(Index: Integer): _T; virtual;
function getCurItem: _T; virtual;
procedure setCurItem(Item: _T); virtual;
function getCount: Integer; virtual;
public
GrowSize: Word;
constructor Create(StartBufLen: Cardinal = 32); reintroduce; virtual;
procedure Reset; virtual;
function Pop: _T; virtual;
function Push(Item: _T): Integer; virtual;
procedure ImportFromArray(Arr: TTArray); virtual;
function ExportToArray: TTArray; virtual;
property Items[Index: Integer]: _T read getItem; default;
property Top: _T read getCurItem write setCurItem;
property Size: Integer read FLen;
property Count: Integer read getCount;
property Cur: Integer read FCur;
end;
{$IFDEF FPC}generic{$ENDIF} TLapeList<_T> = class(TLapeBaseClass)
public type
TTArray = {$IFDEF Delphi}TArray<_T>{$ELSE}array of _T{$ENDIF};
var protected
FDuplicates: TDuplicates;
FSorted: Boolean;
FItems: TTArray;
FCount: Integer;
function getSorted: Boolean; virtual;
procedure setSorted(Sort: Boolean; DoUpdate: Boolean); overload; virtual;
procedure setSorted(Sort: Boolean); overload; virtual;
function getItem(Index: Integer): _T; virtual;
procedure setItem(Index: Integer; Item: _T); virtual;
public
InvalidVal: _T;
constructor Create(InvalidValue: _T; ADuplicates: TDuplicates; ASort: Boolean); reintroduce; virtual;
procedure Clear; virtual;
function Add(Item: _T): Integer; virtual;
function Insert(Item: _T; Index: Integer): Integer; virtual;
function Delete(Index: Integer): _T; virtual;
function DeleteItem(Item: _T): _T; virtual;
procedure MoveItem(AFrom, ATo: Integer); virtual;
procedure SwapItem(AFrom, ATo: Integer); virtual;
function IndicesOf(Item: _T): TIntegerArray; virtual;
function IndexOf(Item: _T; Lo, Hi: Integer): Integer; overload; virtual;
function IndexOf(Item: _T): Integer; overload; virtual;
function ExistsItem(Item: _T): Boolean;
procedure ImportFromArray(Arr: TTArray); virtual;
function ExportToArray: TTArray; virtual;
property Items[Index: Integer]: _T read getItem write setItem; default;
property Count: Integer read FCount;
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
property Sorted: Boolean read getSorted write setSorted;
end;
TLapeList_String = {$IFDEF FPC}specialize{$ENDIF} TLapeList<lpString>;
TLapeList_UInt32 = {$IFDEF FPC}specialize{$ENDIF} TLapeList<UInt32>;
TLapeStringList = class(TLapeList_String)
protected
FHashList: TLapeList_UInt32;
FCaseSensitive: Boolean;
function getSorted: Boolean; override;
procedure setSorted(Sort: Boolean; DoUpdate: Boolean = True); override;
procedure setItem(Index: Integer; Item: lpString); override;
public
constructor Create(InvalidValue: lpString; ADuplicates: TDuplicates; ACaseSensitive, ASort: Boolean); reintroduce; virtual;
destructor Destroy; override;
procedure Clear; override;
function CaseSens(const Item: lpString): lpString; {$IFDEF Lape_Inline}inline;{$ENDIF}
function Add(Item: lpString): Integer; override;
function Delete(Index: Integer): lpString; override;
procedure MoveItem(AFrom, ATo: Integer); override;
procedure SwapItem(AFrom, ATo: Integer); override;
function IndexOf(Item: lpString; Lo, Hi: Integer): Integer; override;
procedure ImportFromArray(Arr: TLapeList_String.TTArray); override;
function Implode(ASep: lpString): lpString; virtual;
property CaseSensitive: Boolean read FCaseSensitive;
end;
{$IFDEF FPC}generic{$ENDIF} TLapeStringMap<_T> = class(TLapeBaseClass)
public type
TTItems = {$IFDEF FPC}specialize{$ENDIF} TLapeList<_T>;
TTArrays = record
Keys: {$IFDEF Delphi}TArray<lpString>{$ELSE}TLapeStringList.TTArray{$ENDIF};
Items: {$IFDEF Delphi}TTItems.TTArray{$ELSE}array of _T{$ENDIF};
end;
var protected
FStringList: TLapeStringList;
FItems: TTItems;
FCount: Integer;
function getItem(Key: lpString): _T; virtual;
procedure setItem(Key: lpString; Item: _T); virtual;
function getItemI(Index: Integer): _T; virtual;
procedure setItemI(Index: Integer; Item: _T); virtual;
function getKey(Index: Integer): lpString; virtual;
function getSorted: Boolean; virtual;
procedure setSorted(Sort: Boolean); virtual;
public
constructor Create(InvalidValue: _T; Duplicates: TDuplicates; Sort: Boolean; InvalidKey: lpString = ''; ACaseSensitive: Boolean = LapeCaseSensitive); reintroduce; virtual;
destructor Destroy; override;
procedure Clear; virtual;
function Insert(Key: lpString; Item: _T; Index: Integer): Integer; virtual;
function Add(Key: lpString; Item: _T): Integer; virtual;
function Delete(Key: lpString): _T; overload; virtual;
function Delete(Index: Integer): _T; overload; virtual;
function DeleteItem(Item: _T): _T; virtual;
function IndicesOfItemI(Item: _T): TIntegerArray; virtual;
function IndicesOfItem(Item: _T): TStringArray; virtual;
function IndicesOfKey(Key: lpString): TIntegerArray; virtual;
function IndexOfItemI(Item: _T): Integer; virtual;
function IndexOfItem(Item: _T): lpString; virtual;
function IndexOfKey(Key: lpString): Integer; virtual;
function ExistsItem(Item: _T): Boolean;
function ExistsKey(Key: lpString): Boolean;
procedure setKeyI(Index: Integer; NewKey: lpString); virtual;
procedure setKey(Item: _T; NewKey: lpString); virtual;
procedure ImportFromArrays(Arr: TTArrays); virtual;
function ExportToArrays: TTArrays; virtual;
property Items[Index: lpString]: _T read getItem write setItem; default;
property ItemsI[Index: Integer]: _T read getItemI write setItemI;
property Key[Index: Integer]: lpString read getKey write setKeyI;
property Count: Integer read FCount;
property Sorted: Boolean read getSorted write setSorted;
end;
{$IFDEF FPC}generic{$ENDIF} TLapeNotifier<_T> = class(TLapeBaseClass)
public type
TNotifyProc = procedure(Sender: _T);
TNotifiers = {$IFDEF FPC}specialize{$ENDIF} TLapeList<TNotifyProc>;
var protected
FNotifiers: TNotifiers;
public
constructor Create; reintroduce; virtual;
destructor Destroy; override;
procedure AddProc(const Proc: TNotifyProc); virtual;
procedure DeleteProc(const Proc: TNotifyProc); virtual;
procedure Notify(Sender: _T); virtual;
end;
TLapeDeclaration = class;
TLapeDeclarationClass = class of TLapeDeclaration;
TLapeDeclArray = array of TLapeDeclaration;
TLapeDeclCollection = class({$IFDEF FPC}specialize{$ENDIF} TLapeList<TLapeDeclaration>); //Needs class() for Delphi support!!
TLapeDeclarationList = class(TLapeBaseClass)
protected
FList: TLapeDeclCollection;
function getItem(Index: Integer): TLapeDeclaration; virtual;
function getItemCount: Integer; virtual;
function getCount: Integer; virtual;
function getSorted: Boolean; virtual;
procedure setSorted(ASorted: Boolean); virtual;
public
Parent: TLapeDeclarationList;
FreeDecls: Boolean;
constructor Create(AList: TLapeDeclCollection; ManageDeclarations: Boolean = True); reintroduce; virtual;
destructor Destroy; override;
function HasParent: Boolean;
procedure Clear; virtual;
procedure ClearSubDeclarations; virtual;
function addDeclaration(d: TLapeDeclaration): Integer; virtual;
function HasSubDeclaration(AName: lpString; CheckParent: TInitBool): Boolean; overload; virtual;
function HasSubDeclaration(ADecl: TLapeDeclaration; CheckParent: TInitBool): Boolean; overload; virtual;
function IndexOf(ADecl: TLapeDeclaration): Integer; virtual;
procedure Delete(i: Integer; DoFree: Boolean = False); overload; virtual;
procedure Delete(d: TLapeDeclaration; DoFree: Boolean = False); overload; virtual;
procedure Delete(AClass: TLapeDeclarationClass; DoFree: Boolean = False); overload; virtual;
function getByName(AName: lpString; CheckParent: TInitBool): TLapeDeclArray; virtual;
function getByClass(AClass: TLapeDeclarationClass; CheckParent: TInitBool; FullClassMatch: Boolean = False): TLapeDeclArray; virtual;
function getByClassAndName(AName: lpString; AClass: TLapeDeclarationClass; CheckParent: TInitBool; FullClassMatch: Boolean = False): TLapeDeclArray; virtual;
property Items[Index: Integer]: TLapeDeclaration read getItem; default;
property ItemCount: Integer read getItemCount;
property Count: Integer read getCount;
property Sorted: Boolean read getSorted write setSorted;
end;
TLapeDeclaration = class(TLapeBaseDeclClass)
protected
FList: TLapeDeclarationList;
FName: lpString;
FNameHash: UInt32;
function getDocPos: TDocPos; override;
procedure setList(AList: TLapeDeclarationList); virtual;
procedure setName(AName: lpString); virtual;
public
_DocPos: TDocPos;
Used: Boolean;
constructor Create(AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); reintroduce; virtual;
destructor Destroy; override;
property DeclarationList: TLapeDeclarationList read FList write setList;
property Name: lpString read FName write setName;
property NameHash: UInt32 read FNameHash;
end;
TLapeManagingDeclaration = class(TLapeDeclaration)
protected
FManagedDecls: TLapeDeclarationList;
public
FreeDecls: Boolean;
constructor Create(AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); override;
destructor Destroy; override;
procedure setManagedDecls(ADecls: TLapeDeclarationList; DoManage: Boolean); overload; virtual;
procedure setManagedDecls(ADecls: TLapeDeclarationList); overload; virtual;
procedure inheritManagedDecls(ADecls: TLapeManagingDeclaration; AReplace: Boolean = False); virtual;
function addSubDeclaration(ADecl: TLapeDeclaration): Integer; virtual;
function HasSubDeclaration(AName: lpString; CheckParent: TInitBool): Boolean; overload; virtual;
function HasSubDeclaration(ADecl: TLapeDeclaration; CheckParent: TInitBool): Boolean; overload; virtual;
procedure ClearSubDeclarations; virtual;
property ManagedDeclarations: TLapeDeclarationList read FManagedDecls write setManagedDecls;
end;
const
op_Invoke = op_Index;
op_Label = op_Addr;
{$IFNDEF FPC}
LineEnding = #13#10;
{$ENDIF}
{$IFDEF Lape_Unicode}
ltString = ltUnicodeString;
ltChar = ltWideChar;
ltCharInt = ltUInt16;
{$ELSE}
ltString = ltAnsiString;
ltChar = ltAnsiChar;
ltCharInt = ltUInt8;
{$ENDIF}
{$IF SizeOf(NativeInt) = SizeOf(Int64)}
ltNativeInt = ltInt64;
ltNativeUInt = ltUInt64;
{$ELSE}
ltNativeInt = ltInt32;
ltNativeUInt = ltUInt32;
{$IFEND}
ltSizeInt = ltNativeInt;
ltSizeUInt = ltNativeUInt;
{$IFDEF Lape_SmallCode}
ltEvalBool = ltBoolean;
{$ELSE}
ltEvalBool = ltLongBool;
{$ENDIF}
LapeTypeSize: array[ELapeBaseType] of Integer = (
-1,
SizeOf(UInt8), SizeOf(Int8), SizeOf(UInt16), SizeOf(Int16), SizeOf(UInt32),
SizeOf(Int32), SizeOf(UInt64), SizeOf(Int64),
SizeOf(Currency), SizeOf(Single), SizeOf(Double), SizeOf(Extended),
SizeOf(Boolean), SizeOf(ByteBool), SizeOf(WordBool), SizeOf(LongBool),
SizeOf(AnsiChar), SizeOf(WideChar),
SizeOf(ShortString), SizeOf(AnsiString), SizeOf(WideString), SizeOf(UnicodeString),
SizeOf(Variant),
SizeOf(ELapeSmallEnum), SizeOf(ELapeLargeEnum), SizeOf(TLapeSmallSet), SizeOf(TLapeLargeSet),
SizeOf(Pointer),
-1, -1,
SizeOf(Pointer), -1,
SizeOf(TCodePos), SizeOf(Pointer)
);
{$IF SizeOf(TCodePos) > SizeOf(Pointer)}
{$MESSAGE Fatal 'TCodePos should be <= Pointer for universal methods'}
{$IFEND}
LapeIntegerTypes = [Low(LapeIntegerTypeRange)..High(LapeIntegerTypeRange)];
LapeSignedIntegerTypes = [ltInt8, ltInt16, ltInt32, ltInt64];
LapeUnsignedIntegerTypes = [ltUInt8, ltUInt16, ltUInt32, ltUInt64];
LapeRealTypes = [ltCurrency..ltExtended];
LapeBoolTypes = [ltBoolean..ltLongBool];
LapeStringTypes = [ltShortString..ltUnicodeString];
LapeCharTypes = [ltAnsiChar..ltWideChar];
LapeEnumTypes = [ltSmallEnum..ltLargeEnum] + LapeBoolTypes;
LapeSetTypes = [ltSmallSet..ltLargeSet];
LapeArrayTypes = [ltDynArray..ltStaticArray] + LapeStringTypes;
LapeStructTypes = [ltRecord..ltUnion];
LapeProcTypes = [ltScriptMethod..ltImportedMethod];
LapeOrdinalTypes = LapeIntegerTypes + LapeBoolTypes + LapeCharTypes + LapeEnumTypes;
LapeRefTypes = LapeArrayTypes - [ltShortString, ltStaticArray];
LapePointerTypes = [ltPointer] + LapeProcTypes + LapeRefTypes;
LapeStackTypes = LapeOrdinalTypes + LapeRealTypes + LapeSetTypes;
LapeIfTypes = LapeOrdinalTypes + LapeStringTypes + LapePointerTypes + LapeRealTypes + [ltVariant];
LapeNoInitTypes = LapeOrdinalTypes + LapeRealTypes + [ltUnknown, ltPointer, ltScriptMethod, ltImportedMethod, ltShortString];
NullDocPos: TDocPos = (Line: 0; Col: 0; FileName: '');
NullRange: TLapeRange = (Lo: 0; Hi: -1);
UnaryOperators = [op_Addr, op_Deref, op_NOT, op_UnaryMinus, op_UnaryPlus];
BinaryOperators = [op_AND, op_OR, op_XOR];
CompareOperators = [op_cmp_Equal, op_cmp_GreaterThan, op_cmp_GreaterThanOrEqual, op_cmp_LessThan, op_cmp_LessThanOrEqual, op_cmp_NotEqual];
LabelOperators = CompareOperators;
CompoundOperators = [op_AssignPlus, op_AssignMinus, op_AssignDiv, op_AssignMul];
AssignOperators = [op_Assign] + CompoundOperators;
OverloadableOperators = [op_Assign, op_Plus, op_Minus, op_Multiply, op_Divide, op_DIV, op_Power, op_MOD, op_IN, op_IS, op_SHL, op_SHR] + CompareOperators + BinaryOperators + CompoundOperators;
op_str: array[EOperator] of lpString = ('',
'=', '>', '>=', '<', '<=', '<>', '@', 'and', ':=', '/=', '-=', '*=', '+=',
'^', 'div', '/', '.' , 'in', 'is', '[', '-', 'mod', '*', 'not', 'or', '+',
'**', 'shl', 'shr', 'xor', '-', '+'
);
op_name: array[EOperator] of lpString = ('',
'EQ', 'GT', 'GTEQ', 'LT', 'LTEQ', 'NEQ', 'ADDR', 'AND', 'ASGN', 'DIVASGN', 'SUBASGN', 'MULASGN', 'ADDASGN',
'DEREF', 'IDIV', 'DIV', 'DOT', 'IN', 'IS', 'IDX', 'SUB', 'MOD', 'MUL', 'NOT', 'OR', 'ADD',
'POW', 'SHL', 'SHR', 'XOR', 'UMIN', 'UPOS'
);
var
lowUInt8: UInt8 = Low(UInt8); highUInt8: UInt8 = High(UInt8);
lowInt8: Int8 = Low(Int8); highInt8: Int8 = High(Int8);
lowUInt16: UInt16 = Low(UInt16); highUInt16: UInt16 = High(UInt16);
lowInt16: Int16 = Low(Int16); highInt16: Int16 = High(Int16);
lowUInt32: UInt32 = Low(UInt32); highUInt32: UInt32 = High(UInt32);
lowInt32: Int32 = Low(Int32); highInt32: Int32 = High(Int32);
lowUInt64: UInt64 = Low(UInt64); highUInt64: UInt64 = High(UInt64);
lowInt64: Int64 = Low(Int64); highInt64: Int64 = High(Int64);
LapeTypeLow: array[LapeIntegerTypeRange] of Pointer = (
@lowUInt8, @lowInt8, @lowUInt16, @lowInt16, @lowUInt32, @lowInt32, @lowUInt64, @lowInt64
);
LapeTypeHigh: array[LapeIntegerTypeRange] of Pointer = (
@highUInt8, @highInt8, @highUInt16, @highInt16, @highUInt32, @highInt32, @highUInt64, @highInt64
);
function LapeCase(const Str: lpString): lpString; {$IFDEF Lape_Inline}inline;{$ENDIF}
function LapeHash(const Value: lpString): UInt32;
function LapeTypeToString(Token: ELapeBaseType): lpString; {$IFDEF Lape_Inline}inline;{$ENDIF}
function LapeOperatorToString(Token: EOperator): lpString; {$IFDEF Lape_Inline}inline;{$ENDIF}
function PointerToString(const p: Pointer): lpString;
{$IF NOT(DECLARED(UIntToStr))}
function UIntToStr(i: UInt32): lpString; inline; overload;
function UIntToStr(i: UInt64): lpString; inline; overload;
{$DEFINE DoUIntToStr}
{$IFEND}
function VarTypeToVType(v: TVarType): SizeInt;
function VariantToVarRec(const v: Variant): TVarRec; overload;
function VariantToVarRec(const v: Variant; out Container: TVarRecContainer): TVarRec; overload;
function VariantArrToConstArr(v: array of Variant): TVarRecList;
procedure Swap(var A, B: Pointer); overload; {$IFDEF Lape_Inline}inline;{$ENDIF}
procedure Swap(var A, B: Boolean); overload; {$IFDEF Lape_Inline}inline;{$ENDIF}
function _Compare8(Arr: PUInt8; Item: UInt8; Lo, Hi: Integer): Integer;
function _Compare16(Arr: PUInt16; Item: UInt16; Lo, Hi: Integer): Integer;
function _Compare32(Arr: PUInt32; Item: UInt32; Lo, Hi: Integer): Integer;
function _Compare64(Arr: PUInt64; Item: UInt64; Lo, Hi: Integer): Integer;
function _BSearch8(Arr: PUInt8; Item: UInt8; Lo, Hi: Integer): Integer;
function _BSearch16(Arr: PUInt16; Item: UInt16; Lo, Hi: Integer): Integer;
function _BSearch32(Arr: PUInt32; Item: UInt32; Lo, Hi: Integer): Integer;
function _BSearch64(Arr: PUInt64; Item: UInt64; Lo, Hi: Integer): Integer;
procedure _Insert8(Arr: PUInt8; var Index: Integer);
procedure _Insert16(Arr: PUInt16; var Index: Integer);
procedure _Insert32(Arr: PUInt32; var Index: Integer);
procedure _Insert64(Arr: PUInt64; var Index: Integer);
{$IFDEF Lape_TrackObjects}
var
lpgCounter: Integer;
lpgList: TList;
{$ENDIF}
implementation
uses
typinfo, variants,
{$IFDEF Lape_NeedAnsiStringsUnit}AnsiStrings,{$ENDIF}
lpexceptions;
function LapeCase(const Str: lpString): lpString;
begin
{$IFDEF Lape_CaseSensitive}
Result := Str;
{$ELSE}
Result := LowerCase(Str);
{$ENDIF}
end;
//MurMurHas2 by Tommi Prami & optimizations by Patrick van Logchem
function LapeHash(const Value: lpString): UInt32;
const
Seed: UInt32 = $c58f1a7b;
cM: UInt32 = $5bd1e995;
cR: UInt32 = 24;
var
Data: Pointer;
i, k, Len: UInt32;
begin
{$UNDEF REDO_Q}{$IFOPT Q+}{$Q-}{$DEFINE REDO_Q}{$ENDIF}
{$UNDEF REDO_R}{$IFOPT R+}{$R-}{$DEFINE REDO_R}{$ENDIF}
Data := @Value[1];
Len := Length(Value) * SizeOf(lpChar);
Result := Seed xor Len;
for i := 1 to Len div SizeOf(UInt32) do
begin
k := PUInt32(Data)^ * cM;
Result := (Result * cM) xor ((k xor (k shr cR)) * cM);
Inc(PtrUInt(Data), SizeOf(UInt32));
end;
Len := Len and 3;
if (Len > 0) then
begin
if (Len >= SizeOf(UInt16)) then
begin
k := PUInt16(Data)^;
if (Len > SizeOf(UInt16)) then
k := k + (UInt32(PUInt8(PtrUInt(Data) + SizeOf(UInt16))^) shl 16);
end
else
k := PUInt8(Data)^;
Result := (Result xor k) * cM;
end;
Result := (Result xor (Result shr 13)) * cM;
Result := (Result xor (Result shr 15));
{$IFDEF REDO_Q}{$Q+}{$ENDIF}
{$IFDEF REDO_R}{$R+}{$ENDIF}
end;
function LapeTypeToString(Token: ELapeBaseType): lpString;
begin
Result := lpString(getEnumName(TypeInfo(ELapeBaseType), Ord(Token)));
Delete(Result, 1, 2);
end;
function LapeOperatorToString(Token: EOperator): lpString;
begin
Result := lpString(getEnumName(TypeInfo(EOperator), Ord(Token)));
Delete(Result, 1, 3);
end;
function PointerToString(const p: Pointer): lpString;
begin
if ((p = nil) or (PPointer(p)^ = nil)) then
Result := 'nil'
else
Result := lpString('0x'+IntToHex(PtrUInt(p^), 1));
end;
{$IFDEF DoUIntToStr}
function UIntToStr(i: UInt32): lpString; inline; overload;
begin
Result := lpString(IntToStr(i));
end;
function UIntToStr(i: UInt64): lpString; inline; overload;
begin
Result := lpString(IntToStr(i));
end;
{$ENDIF}
function VarTypeToVType(v: TVarType): SizeInt;
begin
Result := vtVariant;
v := v and VarTypeMask;
case v of
varSingle,
varDouble: Result := vtExtended;
varCurrency: Result := vtCurrency;
varOleStr: Result := vtWideString;
varDispatch: Result := vtInterface;
varBoolean: Result := vtBoolean;
varVariant: Result := vtVariant;
varSmallint,
varShortInt,
varByte,
varWord,
varLongWord,
varInteger: Result := vtInteger;
varInt64: Result := vtInt64;
varString: Result := vtString;
{$IFDEF FPC}
varDecimal: Result := vtInteger;
varQWord: Result := vtQWord;
{$ENDIF}
end;
end;
function VariantToVarRec(const v: Variant): TVarRec;
begin
Result.VType := VarTypeToVType(VarType(v));
case Result.VType of
vtInteger: Result.VInteger := v;
vtBoolean: Result.VBoolean := v;
vtAnsiString: Result.VAnsiString := TVarData(v).VString;
vtCurrency: Result.VCurrency := @TVarData(v).VCurrency;
vtVariant: Result.VVariant := @v;
vtInterface: Result.VInterface := TVarData(v).VDispatch;
vtWideString: Result.VWideString := TVarData(v).VOleStr;
vtInt64: Result.VInt64 := @TVarData(v).VInt64;
{$IFDEF FPC}
vtChar: Result.VChar := v;
vtWideChar: Result.VWideChar := v;
vtQWord: Result.VQWord := @TVarData(v).VQWord;
{$ENDIF}
else VarCastError();
end;
end;
function VariantToVarRec(const v: Variant; out Container: TVarRecContainer): TVarRec;
begin
Container.CVar := v;
Result.VType := VarTypeToVType(VarType(v));
case Result.VType of
vtExtended:
begin
Container.CExtended := Extended(Container.CVar);
Result.VExtended := @Container.CExtended;
end;
vtString:
begin
Container.CShortString := ShortString(Container.CVar);
Result.VString := @Container.CShortString;
end;
vtCurrency:
begin
Container.CCurrency := Currency(Container.CVar);
Result.VCurrency := @Container.CCurrency;
end;
vtInt64:
begin
Container.CInt64 := Int64(Container.CVar);
Result.VInt64 := @Container.CInt64;
end;
{$IFDEF FPC}
vtQWord:
begin
Container.CQWord := QWord(Container.CVar);
Result.VQWord := @Container.CQWord;
end;
{$ENDIF}
else Result := VariantToVarRec(Container.CVar);
end;
end;
function VariantArrToConstArr(v: array of Variant): TVarRecList;
var
i: Integer;
begin
SetLength(Result.VarRecs, Length(v));
SetLength(Result.Containers, Length(v));
for i := 0 to High(v) do
Result.VarRecs[i] := VariantToVarRec(v[i], Result.Containers[i]);
end;
procedure Swap(var A, B: Pointer);
var
C: Pointer;
begin
C := A;
A := B;
B := C;
end;
procedure Swap(var A, B: Boolean);
var
C: Boolean;
begin
C := A;
A := B;
B := C;
end;
function _Compare8(Arr: PUInt8; Item: UInt8; Lo, Hi: Integer): Integer;
var
i: Integer;
begin
for i := Lo to Hi do
if (Arr^ = Item) then
Exit(i)
else
Inc(Arr);
Result := -1;
end;
function _Compare16(Arr: PUInt16; Item: UInt16; Lo, Hi: Integer): Integer;
var
i: Integer;
begin
for i := Lo to Hi do
if (Arr^ = Item) then
Exit(i)
else
Inc(Arr);
Result := -1;
end;
function _Compare32(Arr: PUInt32; Item: UInt32; Lo, Hi: Integer): Integer;
var
i: Integer;
begin
for i := Lo to Hi do
if (Arr^ = Item) then
Exit(i)
else
Inc(Arr);
Result := -1;
end;
function _Compare64(Arr: PUInt64; Item: UInt64; Lo, Hi: Integer): Integer;
var
i: Integer;
begin
for i := Lo to Hi do
if (Arr^ = Item) then
Exit(i)
else
Inc(Arr);
Result := -1;
end;
function _BSearch8(Arr: PUInt8; Item: UInt8; Lo, Hi: Integer): Integer;
var
mVal: UInt8;
mIndex: Integer;
begin
while (Lo <= Hi) do
begin
mIndex := (Lo + Hi) div 2;
mVal := Arr[mIndex];
if (Item = mVal) then
begin
while (mIndex > Lo) and (Arr[mIndex - 1] = Item) do
Dec(mIndex);
Exit(mIndex);
end
else if (Item < mVal) then
Hi := mIndex - 1
else
Lo := mIndex + 1;
end;
Result := -(Lo + 1);
end;
function _BSearch16(Arr: PUInt16; Item: UInt16; Lo, Hi: Integer): Integer;
var
mVal: UInt16;
mIndex: Integer;
begin
while (Lo <= Hi) do
begin
mIndex := (Lo + Hi) div 2;
mVal := Arr[mIndex];
if (Item = mVal) then
begin
while (mIndex > Lo) and (Arr[mIndex - 1] = Item) do
Dec(mIndex);
Exit(mIndex);
end
else if (Item < mVal) then
Hi := mIndex - 1
else
Lo := mIndex + 1;
end;
Result := -(Lo + 1);
end;
function _BSearch32(Arr: PUInt32; Item: UInt32; Lo, Hi: Integer): Integer;
var
mVal: UInt32;
mIndex: Integer;
begin
while (Lo <= Hi) do
begin
mIndex := (Lo + Hi) div 2;
mVal := Arr[mIndex];
if (Item = mVal) then
begin
while (mIndex > Lo) and (Arr[mIndex - 1] = Item) do
Dec(mIndex);
Exit(mIndex);
end
else if (Item < mVal) then
Hi := mIndex - 1
else
Lo := mIndex + 1;
end;
Result := -(Lo + 1);
end;
function _BSearch64(Arr: PUInt64; Item: UInt64; Lo, Hi: Integer): Integer;
var
mVal: UInt64;
mIndex: Integer;
begin
while (Lo <= Hi) do
begin
mIndex := (Lo + Hi) div 2;
mVal := Arr[mIndex];
if (Item = mVal) then
begin
while (mIndex > Lo) and (Arr[mIndex - 1] = Item) do
Dec(mIndex);