11
System.Generics.Collections,
30
// -------------------------------------------------------------------------
33
PPLongint = ^PLongint;
35
// -------------------------------------------------------------------------
48
TArray2<T> = array of TArray<T>;
49
TArray3<T> = array of TArray2<T>;
53
TConstProc<TA> = reference to procedure(const A: TA);
54
TConstProc<TA, TB> = reference to procedure(const A: TA; const B: TB);
55
TConstProc<TA, TB, TC> = reference to procedure(const A: TA; const B: TB;
57
TConstProc<TA, TB, TC, TD> = reference to procedure(const A: TA; const B: TB;
58
const C: TC; const D: TD);
60
TConstFunc<TA, TResult> = reference to function(const A: TA): TResult;
61
TConstFunc<TA, TB, TResult> = reference to function(const A: TA;
62
const B: TB): TResult;
63
TConstFunc<TA, TB, TC, TResult> = reference to function(const A: TA;
64
const B: TB; const C: TC): TResult;
65
TConstFunc<TA, TB, TC, TD, TResult> = reference to function(const A: TA;
66
const B: TB; const C: TC; const D: TD): TResult;
68
TConstProc1<T> = reference to procedure(const A: T);
69
TConstProc2<T, TResult> = reference to procedure(const A, B: T);
70
TConstProc3<T, TResult> = reference to procedure(const A, B, C: T);
71
TConstProc4<T, TResult> = reference to procedure(const A, B, C, D: T);
73
TConstFunc1<T, TResult> = reference to function(const A: T): TResult;
74
TConstFunc2<T, TResult> = reference to function(const A, B: T): TResult;
75
TConstFunc3<T, TResult> = reference to function(const A, B, C: T): TResult;
76
TConstFunc4<T, TResult> = reference to function(const A, B, C, D: T): TResult;
83
HHex4 = record helper for THex4
86
function ToString: String;
91
HMatrix3D = record helper for TMatrix3D
93
function GetTranslate: TPoint3D;
94
procedure SetTranslate(const Translate_: TPoint3D);
96
property Translate: TPoint3D read GetTranslate write SetTranslate;
97
class function Identity: TMatrix3D; static;
107
constructor Create(const Pos_, Vec_: TVector3D);
110
// TRangeArray<_TValue_>
112
TRangeArray<_TValue_> = record
114
_Values: TArray<_TValue_>;
117
function GetValues(I_: Integer): _TValue_;
118
procedure SetValues(I_: Integer; const Value_: _TValue_);
119
procedure SetMinI(const MinI_: Integer);
120
procedure SetMaxI(const MaxI_: Integer);
121
function GetCount: Integer;
124
constructor Create(const MinI_, MaxI_: Integer);
125
property Values[I_: Integer]: _TValue_ read GetValues
126
write SetValues; default;
127
property MinI: Integer read _MinI write SetMinI;
128
property MaxI: Integer read _MaxI write SetMaxI;
129
property Count: Integer read GetCount;
130
procedure SetRange(const I_: Integer); overload;
131
procedure SetRange(const MinI_, MaxI_: Integer); overload;
134
// TMarginArray<_TValue_>
136
TMarginArray<_TValue_> = record
138
_Values: TArray<_TValue_>;
142
function GetValues(I_: Integer): _TValue_;
143
procedure SetValues(I_: Integer; const Value_: _TValue_);
144
procedure SetLowerN(const LowerN_: Integer);
145
procedure SetCount(const Count_: Integer);
146
procedure SetUpperN(const UpperN_: Integer);
149
constructor Create(const LowerN_, Count_, UpperN_: Integer);
150
property Values[I_: Integer]: _TValue_ read GetValues
151
write SetValues; default;
152
property LowerN: Integer read _LowerN write SetLowerN;
153
property Count: Integer read _Count write SetCount;
154
property UpperN: Integer read _UpperN write SetUpperN;
159
TInterfacedBase = class(TObject, IInterface)
162
function QueryInterface(const IID_: TGUID; out Obj_): HResult; stdcall;
163
function _AddRef: Integer; stdcall;
164
function _Release: Integer; stdcall;
173
class var _Task: ITask;
175
class procedure Run(const Proc_: TThreadProcedure;
176
const Delay_: Integer = 500);
181
TIter<TValue_> = class
184
function GetValue: TValue_; virtual; abstract;
185
procedure SetValue(const Value_: TValue_); virtual; abstract;
187
property Value: TValue_ read GetValue write SetValue;
192
TFileReader = class(TBinaryReader)
195
_Encoding: TEncoding;
198
constructor Create(Stream_: TStream; Encoding_: TEncoding = nil;
199
OwnsStream_: Boolean = False); overload;
200
constructor Create(const Filename_: String;
201
Encoding_: TEncoding = nil); overload;
202
property OffsetBOM: Integer read _OffsetBOM;
203
function EndOfStream: Boolean;
204
function ReadLine: String;
205
function Read(var Buffer_; Count_: Longint): Longint;
210
TSearchBM<_TYPE_> = class
212
__TableBC: TDictionary<_TYPE_, Integer>;
216
function Get_TableBC(const Key_: _TYPE_): Integer;
217
procedure Set_TableBC(const Key_: _TYPE_; const Val_: Integer);
218
function Equal(const A_, B_: _TYPE_): Boolean;
220
_Pattern: TArray<_TYPE_>;
221
_TableSF: TArray<Integer>;
222
_TableGS: TArray<Integer>;
223
property _TableBC[const Key_: _TYPE_]: Integer read Get_TableBC
225
function GetPattern: TArray<_TYPE_>;
226
procedure SetPattern(const Pattern_: TArray<_TYPE_>);
227
procedure MakeTableBC;
228
procedure MakeTableSF;
229
procedure MakeTableGS;
231
TOnRead = reference to function(const I_: Integer): _TYPE_;
234
TOnReadBlock = reference to procedure(const HeadI_: Integer;
235
const Buffer_: TArray<_TYPE_>);
237
constructor Create; overload;
238
constructor Create(const Pattern_: TArray<_TYPE_>); overload;
239
destructor Destroy; override;
240
property Pattern: TArray<_TYPE_> read GetPattern write SetPattern;
241
function Match(const Source_: TArray<_TYPE_>;
242
const StartI_, StopI_: Integer): Integer; overload;
243
function Matches(const Source_: TArray<_TYPE_>;
244
const StartI_, StopI_: Integer): TArray<Integer>; overload;
245
function Match(const Source_: TArray<_TYPE_>; const StartI_: Integer = 0)
247
function Matches(const Source_: TArray<_TYPE_>; const StartI_: Integer = 0)
248
: TArray<Integer>; overload;
249
function Match(const StartI_, StopI_: Integer; const OnRead_: TOnRead)
251
function Matches(const StartI_, StopI_: Integer; const OnRead_: TOnRead)
252
: TArray<Integer>; overload;
253
function Match(const StartI_, StopI_: Integer;
254
const OnReadBlock_: TOnReadBlock): Integer; overload;
255
function Matches(const StartI_, StopI_: Integer;
256
const OnReadBlock_: TOnReadBlock): TArray<Integer>; overload;
260
SINGLE_EPS = 1.1920928955078125E-7;
261
DOUBLE_EPS = 2.220446049250313080847263336181640625E-16;
263
SINGLE_EPS1 = SINGLE_EPS * 1E1;
264
DOUBLE_EPS1 = DOUBLE_EPS * 1E1;
266
SINGLE_EPS2 = SINGLE_EPS * 1E2;
267
DOUBLE_EPS2 = DOUBLE_EPS * 1E2;
269
SINGLE_EPS3 = SINGLE_EPS * 1E3;
270
DOUBLE_EPS3 = DOUBLE_EPS * 1E3;
272
SINGLE_EPS4 = SINGLE_EPS * 1E4;
273
DOUBLE_EPS4 = DOUBLE_EPS * 1E4;
275
// ------------------------------------------------------------------------
287
// ------------------------------------------------------------------------
292
_ThreadPool_ :TThreadPool;
294
{$IF SizeOf( Extended ) = 10 }
295
function Int(const X_: Extended): Extended; inline; overload;
296
function Frac(const X_: Extended): Extended; inline; overload;
297
function Exp(const X_: Extended): Extended; inline; overload;
298
function Cos(const X_: Extended): Extended; inline; overload;
299
function Sin(const X_: Extended): Extended; inline; overload;
300
function Ln(const X_: Extended): Extended; inline; overload;
301
function ArcTan(const X_: Extended): Extended; inline; overload;
302
function Sqrt(const X_: Extended): Extended; inline; overload;
303
function Tangent(const X_: Extended): Extended; inline; overload;
304
procedure SineCosine(const X_: Extended; var Sin_, Cos_: Extended);
306
function ExpMinus1(const X_: Extended): Extended; inline; overload;
307
function LnXPlus1(const X_: Extended): Extended; inline; overload;
309
function Pow2(const X_: Int32u): Int32u; inline; overload;
310
function Pow2(const X_: Int32s): Int32s; inline; overload;
311
function Pow2(const X_: Int64u): Int64u; inline; overload;
312
function Pow2(const X_: Int64s): Int64s; inline; overload;
313
function Pow2(const X_: Single): Single; inline; overload;
314
function Pow2(const X_: Double): Double; inline; overload;
316
function Pow3(const X_: Int32u): Int32u; inline; overload;
317
function Pow3(const X_: Int32s): Int32s; inline; overload;
318
function Pow3(const X_: Int64u): Int64u; inline; overload;
319
function Pow3(const X_: Int64s): Int64s; inline; overload;
320
function Pow3(const X_: Single): Single; inline; overload;
321
function Pow3(const X_: Double): Double; inline; overload;
323
function Pow4(const X_: Int32u): Int32u; inline; overload;
324
function Pow4(const X_: Int32s): Int32s; inline; overload;
325
function Pow4(const X_: Int64u): Int64u; inline; overload;
326
function Pow4(const X_: Int64s): Int64s; inline; overload;
327
function Pow4(const X_: Single): Single; inline; overload;
328
function Pow4(const X_: Double): Double; inline; overload;
330
function Pow5(const X_: Int32u): Int32u; inline; overload;
331
function Pow5(const X_: Int32s): Int32s; inline; overload;
332
function Pow5(const X_: Int64u): Int64u; inline; overload;
333
function Pow5(const X_: Int64s): Int64s; inline; overload;
334
function Pow5(const X_: Single): Single; inline; overload;
335
function Pow5(const X_: Double): Double; inline; overload;
337
function Roo2(const X_: Single): Single; inline; overload;
338
function Roo2(const X_: Double): Double; inline; overload;
340
function Roo3(const X_: Single): Single; inline; overload;
341
function Roo3(const X_: Double): Double; inline; overload;
343
function Clamp(const X_, Min_, Max_: Integer): Integer; inline; overload;
344
function Clamp(const X_, Min_, Max_: Single): Single; inline; overload;
345
function Clamp(const X_, Min_, Max_: Double): Double; inline; overload;
347
function ClampMin(const X_, Min_: Integer): Integer; inline; overload;
348
function ClampMin(const X_, Min_: Single): Single; inline; overload;
349
function ClampMin(const X_, Min_: Double): Double; inline; overload;
351
function ClampMax(const X_, Max_: Integer): Integer; inline; overload;
352
function ClampMax(const X_, Max_: Single): Single; inline; overload;
353
function ClampMax(const X_, Max_: Double): Double; inline; overload;
355
function Min(const A_, B_, C_: Integer): Integer; overload;
356
function Min(const A_, B_, C_: Single): Single; overload;
357
function Min(const A_, B_, C_: Double): Double; overload;
359
function Max(const A_, B_, C_: Integer): Integer; overload;
360
function Max(const A_, B_, C_: Single): Single; overload;
361
function Max(const A_, B_, C_: Double): Double; overload;
363
function MinI(const A_, B_: Integer): Byte; inline; overload;
364
function MinI(const A_, B_: Single): Byte; inline; overload;
365
function MinI(const A_, B_: Double): Byte; inline; overload;
367
function MaxI(const A_, B_: Integer): Byte; inline; overload;
368
function MaxI(const A_, B_: Single): Byte; inline; overload;
369
function MaxI(const A_, B_: Double): Byte; inline; overload;
371
function MinI(const A_, B_, C_: Integer): Integer; inline; overload;
372
function MinI(const A_, B_, C_: Single): Integer; inline; overload;
373
function MinI(const A_, B_, C_: Double): Integer; inline; overload;
375
function MaxI(const A_, B_, C_: Integer): Integer; inline; overload;
376
function MaxI(const A_, B_, C_: Single): Integer; inline; overload;
377
function MaxI(const A_, B_, C_: Double): Integer; inline; overload;
379
function MinI(const Vs_: array of Integer): Integer; overload;
380
function MinI(const Vs_: array of Single): Integer; overload;
381
function MinI(const Vs_: array of Double): Integer; overload;
383
function MaxI(const Vs_: array of Integer): Integer; overload;
384
function MaxI(const Vs_: array of Single): Integer; overload;
385
function MaxI(const Vs_: array of Double): Integer; overload;
387
function PoMod(const X_, Range_: Integer): Integer; overload;
388
function PoMod(const X_, Range_: Int64): Int64; overload;
390
{$IF Defined( MACOS ) or Defined( MSWINDOWS ) }
391
function RevBytes(const Value_: Word): Word; overload;
392
function RevBytes(const Value_: Smallint): Smallint; overload;
394
function RevBytes(const Value_: Cardinal): Cardinal; overload;
395
function RevBytes(const Value_: Integer): Integer; overload;
396
function RevBytes(const Value_: Single): Single; overload;
398
function RevBytes(const Value_: UInt64): UInt64; overload;
399
function RevBytes(const Value_: Int64): Int64; overload;
400
function RevBytes(const Value_: Double): Double; overload;
402
{$IF Defined( MACOS ) or Defined( MSWINDOWS ) }
403
function CharsToStr(const Cs_: TArray<AnsiChar>): AnsiString;
405
function FileToBytes(const Filename_: string): TBytes;
407
function Comb(N_, K_: Cardinal): UInt64;
409
function BinPow(const N_: Integer): Integer; overload;
410
function BinPow(const N_: Cardinal): Cardinal; overload;
411
function BinPow(const N_: Int64): Int64; overload;
412
function BinPow(const N_: UInt64): UInt64; overload;
414
function UIntToStr(const Value_: UInt32; const N_: Integer;
415
const C_: Char = '0'): String; overload;
416
function UIntToStr(const Value_: UInt64; const N_: Integer;
417
const C_: Char = '0'): String; overload;
419
function IntToStr(const Value_: Integer; const N_: Integer;
420
const C_: Char = '0'): String; overload;
421
function IntToStr(const Value_: Int64; const N_: Integer; const C_: Char = '0')
423
function IntToStrP(const Value_: Integer; const N_: Integer;
424
const C_: Char = '0'): String; overload;
425
function IntToStrP(const Value_: Int64; const N_: Integer; const C_: Char = '0')
428
function FloatToStr(const Value_: Single; const N_: Integer;
429
out Man_, Exp_: String): Boolean; overload;
430
function FloatToStr(const Value_: Double; const N_: Integer;
431
out Man_, Exp_: String): Boolean; overload;
433
function _TestFloatToStr_Single(const Value_: String;
434
const N_: Integer): String;
435
function _TestFloatToStr_Double(const Value_: String;
436
const N_: Integer): String;
438
function FloatToStr(const Value_: Single; const N_: Integer;
439
out Man_, Exp_: String; out DecN_: Integer): Boolean; overload;
440
function FloatToStr(const Value_: Double; const N_: Integer;
441
out Man_, Exp_: String; out DecN_: Integer): Boolean; overload;
443
function FloatToStr(const Value_: Single; const N_: Integer): String; overload;
444
function FloatToStr(const Value_: Double; const N_: Integer): String; overload;
445
function FloatToStrP(const Value_: Single; const N_: Integer): String; overload;
446
function FloatToStrP(const Value_: Double; const N_: Integer): String; overload;
448
function Floor(const X_, D_: UInt32): UInt32; overload;
449
function Floor(const X_, D_: UInt64): UInt64; overload;
451
function Ceil(const X_, D_: UInt32): UInt32; overload;
452
function Ceil(const X_, D_: UInt64): UInt64; overload;
454
function Floor2N(const X_, D_: UInt32): UInt32; overload;
455
function Floor2N(const X_, D_: UInt64): UInt64; overload;
457
function Ceil2N(const X_, D_: UInt32): UInt32; overload;
458
function Ceil2N(const X_, D_: UInt64): UInt64; overload;
460
procedure GetMemAligned(out P_: Pointer; const Size_, Align2N_: UInt32);
461
procedure FreeMemAligned(const P_: Pointer);
463
function RealMod(const X_, Range_: Integer): Integer; overload;
464
function RealMod(const X_, Range_: Int64): Int64; overload;
467
// ----------------------------------------------------------------
469
// ----------------------------------------------------------------
471
function RealMod(const X_, Range_: Integer): Integer;
473
Result := X_ mod Range_;
478
function RealMod(const X_, Range_: Int64): Int64;
480
Result := X_ mod Range_;
488
function HHex4.ToString: String;
490
Result := IntToHex(Self, 4);
495
function HMatrix3D.GetTranslate: TPoint3D;
505
procedure HMatrix3D.SetTranslate(const Translate_: TPoint3D);
515
class function HMatrix3D.Identity: TMatrix3D;
540
constructor TRay3D.Create(const Pos_, Vec_: TVector3D);
546
// TRangeArray<_TValue_>
548
function TRangeArray<_TValue_>.GetValues(I_: Integer): _TValue_;
551
Result := _Values[I_];
554
procedure TRangeArray<_TValue_>.SetValues(I_: Integer; const Value_: _TValue_);
557
_Values[I_] := Value_;
560
procedure TRangeArray<_TValue_>.SetMinI(const MinI_: Integer);
567
procedure TRangeArray<_TValue_>.SetMaxI(const MaxI_: Integer);
574
function TRangeArray<_TValue_>.GetCount: Integer;
576
Result := _MaxI - _MinI + 1;
579
procedure TRangeArray<_TValue_>.InitArray;
581
SetLength(_Values, GetCount);
584
constructor TRangeArray<_TValue_>.Create(const MinI_, MaxI_: Integer);
586
SetRange(MinI_, MaxI_);
589
procedure TRangeArray<_TValue_>.SetRange(const I_: Integer);
594
procedure TRangeArray<_TValue_>.SetRange(const MinI_, MaxI_: Integer);
602
// TMarginArray<_TValue_>
604
function TMarginArray<_TValue_>.GetValues(I_: Integer): _TValue_;
607
Result := _Values[I_];
610
procedure TMarginArray<_TValue_>.SetValues(I_: Integer; const Value_: _TValue_);
613
_Values[I_] := Value_;
616
procedure TMarginArray<_TValue_>.SetLowerN(const LowerN_: Integer);
623
procedure TMarginArray<_TValue_>.SetCount(const Count_: Integer);
630
procedure TMarginArray<_TValue_>.SetUpperN(const UpperN_: Integer);
637
/// //////////////////////////////////////////////////////////////////// メソッド
639
procedure TMarginArray<_TValue_>.InitArray;
641
SetLength(_Values, _LowerN + _Count + _UpperN);
644
constructor TMarginArray<_TValue_>.Create(const LowerN_, Count_,
656
function TInterfacedBase.QueryInterface(const IID_: TGUID; out Obj_): HResult;
658
if GetInterface(IID_, Obj_) then
661
Result := E_NOINTERFACE;
664
function TInterfacedBase._AddRef: Integer;
669
function TInterfacedBase._Release: Integer;
676
class procedure TIdleTask.Run(const Proc_: TThreadProcedure;
677
const Delay_: Integer = 500);
679
if Assigned(_Task) then
687
if TTask.CurrentTask.Status = TTaskStatus.Running then
688
TThread.Queue(nil, Proc_);
694
constructor TFileReader.Create(Stream_: TStream; Encoding_: TEncoding = nil;
695
OwnsStream_: Boolean = False);
697
inherited Create(Stream_, TEncoding.ANSI, OwnsStream_);
699
_OffsetBOM := TEncoding.GetBufferEncoding(ReadBytes(8), _Encoding, Encoding_);
701
BaseStream.Position := _OffsetBOM;
704
constructor TFileReader.Create(const Filename_: String;
705
Encoding_: TEncoding = nil);
707
Create(TFileStream.Create(Filename_, fmOpenRead or fmShareDenyWrite),
711
function TFileReader.EndOfStream: Boolean;
713
Result := (PeekChar = -1);
716
function TFileReader.ReadLine: String;
723
while not EndOfStream do
732
if PeekChar = 10 then
742
Result := _Encoding.GetString(Bs);
745
function TFileReader.Read(var Buffer_; Count_: Longint): Longint;
747
Result := BaseStream.Read(Buffer_, Count_);
752
function TSearchBM<_TYPE_>.Get_TableBC(const Key_: _TYPE_): Integer;
754
if __TableBC.ContainsKey(Key_) then
755
Result := __TableBC[Key_]
761
procedure TSearchBM<_TYPE_>.Set_TableBC(const Key_: _TYPE_;
764
__TableBC.AddOrSetValue(Key_, Val_);
767
function TSearchBM<_TYPE_>.Equal(const A_, B_: _TYPE_): Boolean;
769
Result := CompareMem(@A_, @B_, SizeOf(_TYPE_));
772
function TSearchBM<_TYPE_>.GetPattern: TArray<_TYPE_>;
777
procedure TSearchBM<_TYPE_>.SetPattern(const Pattern_: TArray<_TYPE_>);
779
_Pattern := Pattern_;
781
_PN0 := Length(_Pattern);
790
procedure TSearchBM<_TYPE_>.MakeTableBC;
798
for I := 0 to _PN2 do
799
AddOrSetValue(_Pattern[I], _PN1 - I);
803
procedure TSearchBM<_TYPE_>.MakeTableSF;
807
SetLength(_TableSF, _PN0);
809
_TableSF[_PN1] := _PN0;
813
for I := _PN2 downto 0 do
815
if (I > G) and (_TableSF[I + _PN1 - F] < I - G) then
817
_TableSF[I] := _TableSF[I + _PN1 - F];
826
while (G >= 0) and Equal(_Pattern[G], _Pattern[G + _PN1 - F]) do
829
_TableSF[I] := F - G;
835
procedure TSearchBM<_TYPE_>.MakeTableGS;
839
SetLength( _TableGS, _PN0 );
841
for I := 0 to _PN1 do _TableGS[ I ] := _PN0;
845
for J := _PN1 downto 0 do
847
if _TableSF[ J ] = J + 1 then
860
for I := 0 to _PN2 do _TableGS[ _PN1 - _TableSF[ I ] ] := _PN1 - I;
863
procedure TSearchBM<_TYPE_>.MakeTableGS;
867
SetLength(_TableGS, _PN0);
874
for I := _PN2 downto 0 do
876
if _TableSF[J] = J + 1 then
884
for I := 0 to _PN2 do
885
_TableGS[_PN1 - _TableSF[I]] := _PN1 - I;
888
constructor TSearchBM<_TYPE_>.Create;
892
__TableBC := TDictionary<_TYPE_, Integer>.Create;
895
constructor TSearchBM<_TYPE_>.Create(const Pattern_: TArray<_TYPE_>);
899
SetPattern(Pattern_);
902
destructor TSearchBM<_TYPE_>.Destroy;
909
function TSearchBM<_TYPE_>.Match(const Source_: TArray<_TYPE_>;
910
const StartI_, StopI_: Integer): Integer;
919
while J <= StopI_ - _PN0 do
921
for I := _PN1 downto 0 do
925
if not Equal(_Pattern[I], A) then
927
Inc(J, Max(_TableGS[I], _TableBC[A] - _PN1 + I));
943
function TSearchBM<_TYPE_>.Matches(const Source_: TArray<_TYPE_>;
944
const StartI_, StopI_: Integer): TArray<Integer>;
955
while J <= StopI_ - _PN0 do
957
for I := _PN1 downto 0 do
961
if not Equal(_Pattern[I], A) then
963
Inc(J, Max(_TableGS[I], _TableBC[A] - _PN1 + I));
969
Result := Result + [J];
977
// ------------------------------------------------------------------------------
979
function TSearchBM<_TYPE_>.Match(const Source_: TArray<_TYPE_>;
980
const StartI_: Integer = 0): Integer;
982
Result := Match(Source_, StartI_, Length(Source_));
985
function TSearchBM<_TYPE_>.Matches(const Source_: TArray<_TYPE_>;
986
const StartI_: Integer = 0): TArray<Integer>;
988
Result := Matches(Source_, StartI_, Length(Source_));
991
// ------------------------------------------------------------------------------
993
function TSearchBM<_TYPE_>.Match(const StartI_, StopI_: Integer;
994
const OnRead_: TOnRead): Integer;
1003
while J <= StopI_ - _PN0 do
1005
for I := _PN1 downto 0 do
1007
A := OnRead_(J + I);
1009
if not Equal(_Pattern[I], A) then
1011
Inc(J, Max(_TableGS[I], _TableBC[A] - _PN1 + I));
1027
function TSearchBM<_TYPE_>.Matches(const StartI_, StopI_: Integer;
1028
const OnRead_: TOnRead): TArray<Integer>;
1039
while J <= StopI_ - _PN0 do
1041
for I := _PN1 downto 0 do
1043
A := OnRead_(J + I);
1045
if not Equal(_Pattern[I], A) then
1047
Inc(J, Max(_TableGS[I], _TableBC[A] - _PN1 + I));
1053
Result := Result + [J];
1055
Inc(J, _TableGS[0]);
1061
// ------------------------------------------------------------------------------
1063
function TSearchBM<_TYPE_>.Match(const StartI_, StopI_: Integer;
1064
const OnReadBlock_: TOnReadBlock): Integer;
1075
while J <= StopI_ - _PN0 do
1079
for I := _PN1 downto 0 do
1081
if not Equal(_Pattern[I], B[I]) then
1083
Inc(J, Max(_TableGS[I], _TableBC[B[I]] - _PN1 + I));
1099
function TSearchBM<_TYPE_>.Matches(const StartI_, StopI_: Integer;
1100
const OnReadBlock_: TOnReadBlock): TArray<Integer>;
1113
while J <= StopI_ - _PN0 do
1117
for I := _PN1 downto 0 do
1119
if not Equal(_Pattern[I], B[I]) then
1121
Inc(J, Max(_TableGS[I], _TableBC[B[I]] - _PN1 + I));
1127
Result := Result + [J];
1129
Inc(J, _TableGS[0]);
1135
{$IF SizeOf( Extended ) = 10 }
1137
function Int(const X_: Extended): Extended;
1139
Result := System.Int(X_);
1142
function Frac(const X_: Extended): Extended;
1144
Result := System.Frac(X_);
1147
function Exp(const X_: Extended): Extended;
1149
Result := System.Exp(X_);
1152
function Cos(const X_: Extended): Extended;
1154
Result := System.Cos(X_);
1157
function Sin(const X_: Extended): Extended;
1159
Result := System.Sin(X_);
1162
function Ln(const X_: Extended): Extended;
1164
Result := System.Ln(X_);
1167
function ArcTan(const X_: Extended): Extended;
1169
Result := System.ArcTan(X_);
1172
function Sqrt(const X_: Extended): Extended;
1174
Result := System.Sqrt(X_);
1177
function Tangent(const X_: Extended): Extended;
1179
Result := System.Tangent(X_);
1182
procedure SineCosine(const X_: Extended; var Sin_, Cos_: Extended);
1184
System.SineCosine(X_, Sin_, Cos_);
1187
function ExpMinus1(const X_: Extended): Extended;
1189
Result := System.ExpMinus1(X_);
1192
function LnXPlus1(const X_: Extended): Extended;
1194
Result := System.LnXPlus1(X_);
1199
// ------------------------------------------------------------------------------
1201
function Pow2(const X_: Int32u): Int32u;
1206
function Pow2(const X_: Int32s): Int32s;
1211
function Pow2(const X_: Int64u): Int64u;
1216
function Pow2(const X_: Int64s): Int64s;
1221
function Pow2(const X_: Single): Single;
1226
function Pow2(const X_: Double): Double;
1231
// ------------------------------------------------------------------------------
1233
function Pow3(const X_: Int32u): Int32u;
1235
Result := X_ * Pow2(X_);
1238
function Pow3(const X_: Int32s): Int32s;
1240
Result := X_ * Pow2(X_);
1243
function Pow3(const X_: Int64u): Int64u;
1245
Result := X_ * Pow2(X_);
1248
function Pow3(const X_: Int64s): Int64s;
1250
Result := X_ * Pow2(X_);
1253
function Pow3(const X_: Single): Single;
1255
Result := X_ * Pow2(X_);
1258
function Pow3(const X_: Double): Double;
1260
Result := X_ * Pow2(X_);
1263
// ------------------------------------------------------------------------------
1265
function Pow4(const X_: Int32u): Int32u;
1267
Result := Pow2(Pow2(X_));
1270
function Pow4(const X_: Int32s): Int32s;
1272
Result := Pow2(Pow2(X_));
1275
function Pow4(const X_: Int64u): Int64u;
1277
Result := Pow2(Pow2(X_));
1280
function Pow4(const X_: Int64s): Int64s;
1282
Result := Pow2(Pow2(X_));
1285
function Pow4(const X_: Single): Single;
1287
Result := Pow2(Pow2(X_));
1290
function Pow4(const X_: Double): Double;
1292
Result := Pow2(Pow2(X_));
1295
// ------------------------------------------------------------------------------
1297
function Pow5(const X_: Int32u): Int32u;
1299
Result := Pow4(X_) * X_;
1302
function Pow5(const X_: Int32s): Int32s;
1304
Result := Pow4(X_) * X_;
1307
function Pow5(const X_: Int64u): Int64u;
1309
Result := Pow4(X_) * X_;
1312
function Pow5(const X_: Int64s): Int64s;
1314
Result := Pow4(X_) * X_;
1317
function Pow5(const X_: Single): Single;
1319
Result := Pow4(X_) * X_;
1322
function Pow5(const X_: Double): Double;
1324
Result := Pow4(X_) * X_;
1327
// ------------------------------------------------------------------------------
1329
function Roo2(const X_: Single): Single;
1334
function Roo2(const X_: Double): Double;
1339
// ------------------------------------------------------------------------------
1341
function Roo3(const X_: Single): Single;
1343
Result := Sign(X_) * Power(Abs(X_), 1 / 3);
1346
function Roo3(const X_: Double): Double;
1348
Result := Sign(X_) * Power(Abs(X_), 1 / 3);
1351
// ------------------------------------------------------------------------------
1353
function Clamp(const X_, Min_, Max_: Integer): Integer;
1357
else if Max_ < X_ then
1363
function Clamp(const X_, Min_, Max_: Single): Single;
1367
else if Max_ < X_ then
1373
function Clamp(const X_, Min_, Max_: Double): Double;
1377
else if Max_ < X_ then
1383
// ------------------------------------------------------------------------------
1385
function ClampMin(const X_, Min_: Integer): Integer;
1393
function ClampMin(const X_, Min_: Single): Single;
1401
function ClampMin(const X_, Min_: Double): Double;
1409
// ------------------------------------------------------------------------------
1411
function ClampMax(const X_, Max_: Integer): Integer;
1419
function ClampMax(const X_, Max_: Single): Single;
1427
function ClampMax(const X_, Max_: Double): Double;
1435
// ------------------------------------------------------------------------------
1437
function Min(const A_, B_, C_: Integer): Integer;
1455
function Min(const A_, B_, C_: Single): Single;
1473
function Min(const A_, B_, C_: Double): Double;
1491
// ------------------------------------------------------------------------------
1493
function Max(const A_, B_, C_: Integer): Integer;
1512
function Max(const A_, B_, C_: Single): Single;
1531
function Max(const A_, B_, C_: Double): Double;
1550
// ------------------------------------------------------------------------------
1552
function MinI(const A_, B_: Integer): Byte;
1560
function MinI(const A_, B_: Single): Byte;
1568
function MinI(const A_, B_: Double): Byte;
1576
// ------------------------------------------------------------------------------
1578
function MaxI(const A_, B_: Integer): Byte;
1586
function MaxI(const A_, B_: Single): Byte;
1594
function MaxI(const A_, B_: Double): Byte;
1602
// ------------------------------------------------------------------------------
1604
function MinI(const A_, B_, C_: Integer): Integer;
1622
function MinI(const A_, B_, C_: Single): Integer;
1640
function MinI(const A_, B_, C_: Double): Integer;
1658
// ------------------------------------------------------------------------------
1660
function MaxI(const A_, B_, C_: Integer): Integer;
1678
function MaxI(const A_, B_, C_: Single): Integer;
1696
function MaxI(const A_, B_, C_: Double): Integer;
1714
// ------------------------------------------------------------------------------
1716
function MinI(const Vs_: array of Integer): Integer;
1723
for I := 1 to High(Vs_) do
1735
function MinI(const Vs_: array of Single): Integer;
1743
for I := 1 to High(Vs_) do
1755
function MinI(const Vs_: array of Double): Integer;
1763
for I := 1 to High(Vs_) do
1775
// ------------------------------------------------------------------------------
1777
function MaxI(const Vs_: array of Integer): Integer;
1784
for I := 1 to High(Vs_) do
1796
function MaxI(const Vs_: array of Single): Integer;
1804
for I := 1 to High(Vs_) do
1816
function MaxI(const Vs_: array of Double): Integer;
1824
for I := 1 to High(Vs_) do
1836
// ------------------------------------------------------------------------------
1838
function PoMod(const X_, Range_: Integer): Integer;
1840
Result := X_ - (X_ div Range_) * Range_;
1843
Inc(Result, Range_);
1846
function PoMod(const X_, Range_: Int64): Int64;
1848
Result := X_ - (X_ div Range_) * Range_;
1851
Inc(Result, Range_);
1854
// ------------------------------------------------------------------------------
1856
{$IF Defined( MACOS ) or Defined( MSWINDOWS ) }
1858
function RevBytes(const Value_: Word): Word;
1866
function RevBytes(const Value_: Smallint): Smallint;
1874
// ------------------------------------------------------------------------------
1876
function RevBytes(const Value_: Cardinal): Cardinal;
1884
function RevBytes(const Value_: Integer): Integer;
1892
function RevBytes(const Value_: Single): Single;
1896
V := RevBytes(PCardinal(@Value_)^);
1898
Result := PSingle(@V)^;
1901
// ------------------------------------------------------------------------------
1903
function RevBytes(const Value_: UInt64): UInt64;
1905
{$IF Defined( CPUX86 ) }
1906
mov edx, [ ebp + $08 ]
1907
mov eax, [ ebp + $0c ]
1910
{$ELSEIF Defined( CPUX64 ) }
1914
{$MESSAGE Fatal 'RevByte has not been implemented for this architecture.' }
1918
function RevBytes(const Value_: Int64): Int64;
1920
{$IF Defined( CPUX86 ) }
1921
mov edx, [ ebp + $08 ]
1922
mov eax, [ ebp + $0c ]
1925
{$ELSEIF Defined( CPUX64 ) }
1929
{$MESSAGE Fatal 'RevByte has not been implemented for this architecture.' }
1933
function RevBytes(const Value_: Double): Double;
1937
V := RevBytes(PUInt64(@Value_)^);
1939
Result := PDouble(@V)^;
1944
// ------------------------------------------------------------------------------
1946
{$IF Defined( MACOS ) or Defined( MSWINDOWS ) }
1948
function CharsToStr(const Cs_: TArray<AnsiChar>): AnsiString;
1954
for I := 0 to High(Cs_) do
1956
if Cs_[I] = Char(nil) then
1957
Result := Result + CRLF
1959
Result := Result + Cs_[I];
1965
// ------------------------------------------------------------------------------
1967
function FileToBytes(const Filename_: string): TBytes;
1969
with TMemoryStream.Create do
1972
LoadFromFile(Filename_);
1974
SetLength(Result, Size);
1984
// ------------------------------------------------------------------------------
1986
function Comb(N_, K_: Cardinal): UInt64;
1997
// Result := Result * ( N_ - K_ + I ) div I;
1999
Result := Result * N_ div I;
2004
// ------------------------------------------------------------------------------
2006
function BinPow(const N_: Integer): Integer;
2011
function BinPow(const N_: Cardinal): Cardinal;
2016
function BinPow(const N_: Int64): Int64;
2021
function BinPow(const N_: UInt64): UInt64;
2026
// ------------------------------------------------------------------------------
2028
function UIntToStr(const Value_: UInt32; const N_: Integer;
2029
const C_: Char = '0'): String;
2031
Result := UIntToStr(Value_);
2033
Result := Result.Insert(0, StringOfChar(C_, N_ - Length(Result)));
2036
function UIntToStr(const Value_: UInt64; const N_: Integer;
2037
const C_: Char = '0'): String;
2039
Result := UIntToStr(Value_);
2041
Result := Result.Insert(0, StringOfChar(C_, N_ - Length(Result)));
2044
// ------------------------------------------------------------------------------
2046
function IntToStr(const Value_: Integer; const N_: Integer;
2047
const C_: Char = '0'): String;
2051
Result := IntToStr(Value_);
2058
Result := Result.Insert(I, StringOfChar(C_, N_ + I - Length(Result)));
2061
function IntToStr(const Value_: Int64; const N_: Integer;
2062
const C_: Char = '0'): String;
2066
Result := IntToStr(Value_);
2073
Result := Result.Insert(I, StringOfChar(C_, N_ + I - Length(Result)));
2076
function IntToStrP(const Value_: Integer; const N_: Integer;
2077
const C_: Char = '0'): String;
2079
Result := IntToStr(Value_, N_, C_);
2082
Result := '+' + Result;
2085
function IntToStrP(const Value_: Int64; const N_: Integer;
2086
const C_: Char = '0'): String;
2088
Result := IntToStr(Value_, N_, C_);
2091
Result := '+' + Result;
2094
// ------------------------------------------------------------------------------
2096
procedure _SplitME(const Value_: String; out Man_, Exp_: String);
2100
I := Value_.IndexOf('E');
2102
Man_ := Value_.Substring(0, I).TrimRight(['0']);
2103
Exp_ := Value_.Substring(I + 1);
2106
function FloatToStr(const Value_: Single; const N_: Integer;
2107
out Man_, Exp_: String): Boolean;
2109
Result := not(Value_.IsNan or Value_.IsInfinity);
2112
_SplitME(FloatToStrF(Value_, TFloatFormat.ffExponent, N_, 0), Man_, Exp_);
2115
function FloatToStr(const Value_: Double; const N_: Integer;
2116
out Man_, Exp_: String): Boolean;
2118
Result := not(Value_.IsNan or Value_.IsInfinity);
2121
_SplitME(FloatToStrF(Value_, TFloatFormat.ffExponent, N_, 0), Man_, Exp_);
2124
// ------------------------------------------------------------------------------
2126
function _DecN(const Man_, Exp_: String): Integer;
2130
if Man_.Chars[0] = '-' then
2131
M := Man_.Length - 3
2133
M := Man_.Length - 2;
2135
E := Exp_.ToInteger;
2143
function FloatToStr(const Value_: Single; const N_: Integer;
2144
out Man_, Exp_: String; out DecN_: Integer): Boolean;
2146
Result := FloatToStr(Value_, N_, Man_, Exp_);
2149
DecN_ := _DecN(Man_, Exp_);
2152
function FloatToStr(const Value_: Double; const N_: Integer;
2153
out Man_, Exp_: String; out DecN_: Integer): Boolean;
2155
Result := FloatToStr(Value_, N_, Man_, Exp_);
2158
DecN_ := _DecN(Man_, Exp_);
2161
// ------------------------------------------------------------------------------
2163
function _TestFloatToStr_Single(const Value_: String;
2164
const N_: Integer): String;
2169
Zs := StringOfChar('0', N_ + 1);
2171
S0 := Zs + Value_ + Zs;
2173
for I := 1 to Length(S0) - 1 do
2178
Result := Result + S + ' ' + FloatToStr(S.ToSingle, N_) + CRLF;
2182
function _TestFloatToStr_Double(const Value_: String;
2183
const N_: Integer): String;
2188
Zs := StringOfChar('0', N_ + 1);
2190
S0 := Zs + Value_ + Zs;
2192
for I := 1 to Length(S0) - 1 do
2197
Result := Result + S + ' ' + FloatToStr(S.ToDouble, N_) + CRLF;
2201
// ------------------------------------------------------------------------------
2203
function FloatToStr(const Value_: Single; const N_: Integer): String;
2208
if FloatToStr(Value_, N_, M, E, D) then
2210
if Abs(D) <= N_ then
2211
Result := FloatToStrF(Value_, TFloatFormat.ffFixed, N_, ClampMin(D, 0))
2213
Result := M + 'e' + E;
2215
else if Value_.IsNan then
2217
else if Value_.IsNegativeInfinity then
2219
else if Value_.IsPositiveInfinity then
2223
function FloatToStr(const Value_: Double; const N_: Integer): String;
2228
if FloatToStr(Value_, N_, M, E, D) then
2230
if Abs(D) <= N_ then
2231
Result := FloatToStrF(Value_, TFloatFormat.ffFixed, N_, ClampMin(D, 0))
2233
Result := M + 'e' + E;
2235
else if Value_.IsNan then
2237
else if Value_.IsNegativeInfinity then
2239
else if Value_.IsPositiveInfinity then
2243
function FloatToStrP(const Value_: Single; const N_: Integer): String;
2245
Result := FloatToStr(Value_, N_);
2248
Result := '+' + Result;
2251
function FloatToStrP(const Value_: Double; const N_: Integer): String;
2253
Result := FloatToStr(Value_, N_);
2256
Result := '+' + Result;
2259
// ------------------------------------------------------------------------------
2261
function Floor(const X_, D_: UInt32): UInt32;
2263
Result := X_ div D_ * D_;
2266
function Floor(const X_, D_: UInt64): UInt64;
2268
Result := X_ div D_ * D_;
2271
// ------------------------------------------------------------------------------
2273
function Ceil(const X_, D_: UInt32): UInt32;
2275
Result := Floor(X_ + D_ - 1, D_);
2278
function Ceil(const X_, D_: UInt64): UInt64;
2280
Result := Floor(X_ + D_ - 1, D_);
2283
// ------------------------------------------------------------------------------
2285
function Floor2N(const X_, D_: UInt32): UInt32;
2287
Result := X_ and not(D_ - 1);
2290
function Floor2N(const X_, D_: UInt64): UInt64;
2292
Result := X_ and not(D_ - 1);
2295
// ------------------------------------------------------------------------------
2297
function Ceil2N(const X_, D_: UInt32): UInt32;
2299
Result := Floor(X_ + D_ - 1, D_);
2302
function Ceil2N(const X_, D_: UInt64): UInt64;
2304
Result := Floor(X_ + D_ - 1, D_);
2307
// ------------------------------------------------------------------------------
2309
procedure GetMemAligned(out P_: Pointer; const Size_, Align2N_: UInt32);
2311
H: UInt32 = SizeOf(Pointer);
2317
// ┠───A───╂────────S────────┤
2319
// ┃ │I0 ┃ ┃ ┃ │ │ ┃
2320
// ╂─├─┬─┬─┣━┯━┯━┯━╋━┯━┯━┯━╋━┥─┬─┤─╂
2321
// ┃ │×│×│I0┃◯│◯│◯│◯┃◯│◯│◯│◯┃◯│×│×│ ┃
2322
// ╂─├─┴─┴─┣━┷━┷━┷━╋━┷━┷━┷━╋━┥─┴─┤─╂
2323
// ┃ │ │ ┃I1 │ ┃ ┃ │ ┃
2325
// ├H┼───A───┼────────S────────┤
2327
GetMem(P0, H + Align2N_ + Size_);
2329
I0 := NativeUInt(P0);
2331
I1 := Ceil2N(H + I0, Align2N_);
2340
procedure FreeMemAligned(const P_: Pointer);
2349
initialization // ------------------------------------------------------------
2351
_ThreadPool_ := TThreadPool.Create;
2355
SetCurrentDir(ExtractFilePath(ParamStr(0)));