-
Notifications
You must be signed in to change notification settings - Fork 0
/
EuLibMin.pas
1481 lines (1356 loc) · 33.7 KB
/
EuLibMin.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
unit EuLibMin;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
{$IFnDEF FPC}
windows,
{$ELSE}
LCLIntf, LCLType, LMessages,
{$ENDIF}
classes, graphics, forms, comctrls, stdctrls, inifiles, grids;
type
TEuMemIniFile=class(TMemIniFile)
function ReadFloat(const Section, Name: string; Default: Double): Double; override;
end;
{TLongIntList class from mwPCharList.pas edited}
PLongIntArray=^TLongIntArray;
TLongIntArray=array[0..MaxInt div sizeof(LongInt)-1] of LongInt;
TFloatList=class;
TLongIntList=class(TObject)
private
FCapacity: Integer;
FCount: Integer;
FLongIntList: PLongIntArray;
protected
function GetItems(Index: Integer): LongInt;
procedure SetCapacity(NewCapacity: Integer);
procedure SetCount(NewCount: Integer);
procedure SetItems(Index: Integer; Item: LongInt);
public
constructor Create;
destructor Destroy; override;
procedure Order(const InCrease: Boolean=True);
function Add(Item: LongInt): Integer;
function AddUniq(Item: LongInt): Integer;
procedure Assign(const Src: TLongIntList);
procedure Clear;
procedure Delete(Index: Integer);
procedure Exchange(Index1, Index2: Integer);
function First: LongInt;
function IndicesOf(anItem: LongInt; FoundPositions: TLongIntList): Boolean;
function IndexOf(Item: LongInt): Integer;
procedure Insert(Index: Integer; Item: LongInt);
function Last: LongInt;
procedure Move(CurIndex, NewIndex: Integer);
function Remove(Item: LongInt): Integer;
procedure OrderByFloatList(const Floats: TFloatList);
procedure OrderSimilar(const anItem: LongInt);
property Capacity: Integer read FCapacity write SetCapacity;
property Count: Integer read FCount write SetCount;
property Items[Index: Integer]: LongInt read GetItems write SetItems; default;
property LongIntList: PLongIntArray read FLongIntList;
end;
PFloatArray=^TFloatArray;
TFloatArray=array[0..MaxInt div sizeof(double)-1] of double;
TFloatList=class(TObject)
private
FCapacity: Integer;
FCount: Integer;
FFloatList: PFloatArray;
protected
function GetItems(Index: Integer): double;
procedure SetCapacity(NewCapacity: Integer);
procedure SetCount(NewCount: Integer);
procedure SetItems(Index: Integer; Item: double);
public
constructor Create;
destructor Destroy; override;
procedure Order(const InCrease: Boolean=True);
function Add(Item: double): Integer;
procedure Assign(const Src: TFloatList);
procedure Clear;
procedure Delete(Index: Integer);
procedure Exchange(Index1, Index2: Integer);
function First: double;
function IndexOf(Item: double): Integer;
procedure Insert(Index: Integer; Item: double);
function Last: double;
procedure Move(CurIndex, NewIndex: Integer);
function Remove(Item: double): Integer;
property Capacity: Integer read FCapacity write SetCapacity;
property Count: Integer read FCount write SetCount;
property Items[Index: Integer]: double read GetItems write SetItems; default;
property FloatList: PFloatArray read FFloatList;
end;
{TEditList}
TEditList=class(TList)
function GetEdit(Index: integer): TEdit;
procedure SetEdit(Index: integer; aEdit: TEdit);
procedure Add(aEdit: TEdit);
destructor Destroy; override;
constructor Create;
function FindInList(const Edit: TEdit): integer;
property
Edits[Index: integer]: TEdit read GetEdit write SetEdit; default;
end;
{TButtonList}
TButtonList=class(TList)
function GetButton(Index: integer): TButton;
procedure SetButton(Index: integer; aButton: TButton);
procedure Add(aButton: TButton);
destructor Destroy; override;
constructor Create;
function FindInList(const Button: TButton): integer;
property
Buttons[Index: integer]: TButton read GetButton write SetButton; default;
end;
{TComboBoxList}
TComboBoxList=class(TList)
function GetComboBox(Index: integer): TComboBox;
procedure SetComboBox(Index: integer; aComboBox: TComboBox);
procedure Add(aComboBox: TComboBox);
destructor Destroy; override;
constructor Create;
function FindInList(const ComboBox: TComboBox): integer;
property
ComboBoxs[Index: integer]: TComboBox read GetComboBox write SetComboBox; default;
end;
//LIB
type
TListManFunction=function(const List1, List2: TLongIntList; var VarList: TLongIntList): boolean;
function ListIntersect(const List1, List2: TLongIntList; var VarList: TLongIntList): boolean;
function ListUnion(const List1, List2: TLongIntList; var VarList: TLongIntList): boolean;
function ListEqual(const List1, List2: TLongIntList): boolean;
function ValEu(const InStr: string; var aFloat: double; const MsgStr: string=''): Boolean;
function ValEuSilent(const InStr: string; var aFloat: double): Boolean;
function IsSmallFonts: boolean; {Returns TRUE if small font}
function IsDelphiLoaded: Boolean;
function DuplicateComponents(
AComponent: TComponent// original component
): TComponent; // returns created new component
function AreFloatsEqualAbs(const Float1, Float2: double; const Eps: double=1.E-5): boolean;
function AreFloatsEqualRel(const Float1, Float2: double; const Eps: double=1.E-5): boolean;
function PrepareToParse(const InStr: ShortString; var OutStr: ShortString): Boolean;
function AddTwoStrings(const Str1, Str2: string): string;
function ShellSortI(var v: array of integer): Boolean; // From Kernigan & Richi
// NOT tested
function ShellSortF(var v: array of double): Boolean; // 3.5 p.66 - InCrease
// NOT tested
function AddUniqToStrList(var aList: TStringList; const Str2Add: string): integer;
function SaveStringGridToStream(const aGrid: TStringGrid; Stream: TStream): boolean;
function LoadStringGridFromStream(var TheGrid: TStringGrid; Stream: TStream; OverwriteFixed: Boolean=True): boolean;
function IsWritableDir(const aDirIn: string): Boolean;
//procedure EmptyMouseQueue;
function CopyToClipboardFromStringGrid(StringGrid: TStringGrid): Boolean;
function PasteFromClipboardToStringGrid(StringGrid: TStringGrid): Boolean;
function DeleteInStringGrid(StringGrid: TStringGrid): Boolean;
function CompareFloatStr(const Str1, Str2: string): Boolean;
function DecayPercentFormat(const aDecayPercent: double): ShortString; // 0-100
function EuMin(const aFloat1, aFloat2: double): double; overload;
function EuMin(const anInt1, anInt2: LongInt): LongInt; overload;
const
Ln2: double=0.6931471805599453;
UndefinedVal: double = 1.0e-32; // double defined in Here
var
InLongOperation: Boolean;
StopLongOperation: Boolean;
implementation
uses
Dialogs, SysUtils, Messages, Controls,
Parsing,
Clipbrd;
type
TUniqueReader=class(TReader)
LastRead: TComponent;
procedure ComponentRead(Component: TComponent);
procedure SetNameUnique(
Reader: TReader;
Component: TComponent;
var Name: string
);
end;
procedure TUniqueReader.ComponentRead(
Component: TComponent
);
begin
LastRead:= Component;
end;
procedure TUniqueReader.SetNameUnique(// sets the name of the read
//component to something like "Panel2" if "Panel1" already exists
Reader: TReader;
Component: TComponent; // component being read
var Name: string// Name to use and modify
);
var
i: Integer;
tempname: string;
begin
i:= 0;
tempname:= Name;
while Component.Owner.FindComponent(Name)<>nil do begin
Inc(i);
Name:= Format('%s%d', [tempname, i]);
end;
end;
function DuplicateComponents(
AComponent: TComponent// original component
): TComponent; // returns created new component
procedure RegisterComponentClasses(
AComponent: TComponent);
var
i: integer;
begin
RegisterClass(TPersistentClass(AComponent.ClassType));
if AComponent is TWinControl then
if TWinControl(AComponent).ControlCount>0 then
for i:= 0 to(TWinControl(AComponent).ControlCount-1) do
RegisterComponentClasses(TWinControl(AComponent).Controls[i]);
end;
var
Stream: TMemoryStream;
UniqueReader: TUniqueReader;
Writer: TWriter;
begin
// result:=nil;
UniqueReader:= nil;
Writer:= nil;
Stream:= TMemoryStream.Create;
try
RegisterComponentClasses(AComponent);
try
Writer:= TWriter.Create(Stream, 4096);
Writer.Root:= AComponent.Owner;
Writer.WriteSignature;
Writer.WriteComponent(AComponent);
Writer.WriteListEnd;
finally
Writer.Free;
end;
Stream.Position:= 0;
try
UniqueReader:= TUniqueReader.Create(Stream, 4096); // create reader
// should probably move these routines into theconstructor
UniqueReader.OnSetName:= UniqueReader.SetNameUnique;
UniqueReader.LastRead:= nil;
if AComponent is TWinControl then
UniqueReader.ReadComponents(
// read in components and sub-components
TWinControl(AComponent).Owner,
TWinControl(AComponent).Parent,
UniqueReader.ComponentRead
)
else
UniqueReader.ReadComponents(
// read in components
AComponent.Owner,
nil,
UniqueReader.ComponentRead
);
result:= UniqueReader.LastRead;
finally
UniqueReader.Free;
end;
finally
Stream.Free;
end;
end;
{TEditList}
function TEditList.GetEdit(Index: integer): TEdit;
begin
Result:= TEdit(Items[Index]);
end;
function TEditList.FindInList(const Edit: TEdit): integer;
var
I: integer;
Found: Boolean;
begin
Result:= -1;
Found:= False;
for I:= 0 to(Count-1) do
if (Self[I]=Edit) then begin
Found:= True;
break;
end;
if Found then Result:= I;
end;
procedure TEditList.SetEdit(Index: integer; aEdit: TEdit);
begin
TEdit(Items[Index]).Free;
Items[Index]:= Pointer(aEdit);
end;
procedure TEditList.Add(aEdit: TEdit);
begin
inherited Add(aEdit);
end;
destructor TEditList.Destroy;
var
I: integer;
begin
for I:= 0 to(Count-1) do begin
Self[I].Free;
Self[I]:= nil;
end;
inherited Destroy;
end;
constructor TEditList.Create;
begin
inherited Create;
end;
{TButtonList}
function TButtonList.GetButton(Index: integer): TButton;
begin
Result:= TButton(Items[Index]);
end;
function TButtonList.FindInList(const Button: TButton): integer;
var
I: integer;
Found: Boolean;
begin
Result:= -1;
Found:= False;
for I:= 0 to(Count-1) do
if (Self[I]=Button) then begin
Found:= True;
break;
end;
if Found then Result:= I;
end;
procedure TButtonList.SetButton(Index: integer; aButton: TButton);
begin
TButton(Items[Index]).Free;
Items[Index]:= Pointer(aButton);
end;
procedure TButtonList.Add(aButton: TButton);
begin
inherited Add(aButton);
end;
destructor TButtonList.Destroy;
var
I: integer;
begin
for I:= 0 to(Count-1) do begin
Self[I].Free;
Self[I]:= nil;
end;
inherited Destroy;
end;
constructor TButtonList.Create;
begin
inherited Create;
end;
{TComboBoxList}
function TComboBoxList.GetComboBox(Index: integer): TComboBox;
begin
Result:= TComboBox(Items[Index]);
end;
function TComboBoxList.FindInList(const ComboBox: TComboBox): integer;
var
I: integer;
Found: Boolean;
begin
Result:= -1;
Found:= False;
for I:= 0 to(Count-1) do
if (Self[I]=ComboBox) then begin
Found:= True;
break;
end;
if Found then
Result:= I;
end;
procedure TComboBoxList.SetComboBox(Index: integer; aComboBox: TComboBox);
begin
TComboBox(Items[Index]).Free;
Items[Index]:= Pointer(aComboBox);
end;
procedure TComboBoxList.Add(aComboBox: TComboBox);
begin
inherited Add(aComboBox);
end;
destructor TComboBoxList.Destroy;
var
I: integer;
begin
for I:= 0 to(Count-1) do begin
Self[I].Free;
Self[I]:= nil;
end;
inherited Destroy;
end;
constructor TComboBoxList.Create;
begin
inherited Create;
end;
//LIB
function IsSmallFonts: boolean;
{ TRUE if small font}
var
DC: HDC;
begin
DC:= GetDC(0);
Result:= (GetDeviceCaps(DC, LOGPIXELSX)=96);
{ In the case of big font 120}
ReleaseDC(0, DC);
end;
function ValEu(const InStr: string; var aFloat: double; const MsgStr: string=''): Boolean;
var
I: integer;
NumStr, aStr: string;
begin
NumStr:= Trim(Copy(InStr, 1, Length(InStr)));
if Length(NumStr)>0 then
try
for I:= 1 to Length(NumStr) do
if NumStr[I]=',' then
NumStr[I]:= '.'
else if (NumStr[I]='Е')or(NumStr[I]='е') then
NumStr[I]:= 'E';
Val(NumStr, aFloat, I);
if I<>0 then begin
if MsgStr='' then
aStr:= 'ValEu error in position'
else
aStr:= MsgStr+' in position ';
MessageDlg(aStr+IntToStr(I), mtWarning, [mbOk], 0);
Result:= False;
end
else
Result:= True;
except
Result:= False;
end
else
Result:= False;
end; {ValEu}
function ValEuSilent(const InStr: string; var aFloat: double): Boolean;
var
I: integer;
NumStr: string;
begin
NumStr:= Trim(Copy(InStr, 1, Length(InStr)));
if Length(NumStr)>0 then
try
for I:= 1 to Length(NumStr) do
if NumStr[I]=',' then
NumStr[I]:= '.'
else if (NumStr[I]='Е')or(NumStr[I]='е') then
NumStr[I]:= 'E';
Val(NumStr, aFloat, I);
if I<>0 then begin
Result:= False;
end
else
Result:= True;
except
Result:= False;
end
else
Result:= False;
end; {ValEuSilent}
function IsDelphiLoaded: Boolean;
begin
// Result:= FileExists('DEBUG.DEL');
// Result:= FindWindow('TAppBuilder', nil)>0;
Result:= False;
end;
{TLongIntList }
procedure TLongIntList.OrderSimilar(const anItem: LongInt);
var
I, J: integer;
begin
if Self.Count<2 then
Exit;
for J:= 1 to(Self.Count-1) do begin
I:= J;
while (Abs(Self[I]-anItem)<Abs(Self[I-1]-anItem)) do begin
Self.Exchange(I, I-1);
if I=1 then break;
Dec(I);
end;
end;
end;
procedure TLongIntList.OrderByFloatList(const Floats: TFloatList);
var
I, J: integer;
begin
if Self.Count<2 then
Exit;
if Self.Count=Floats.Count then
for J:= 1 to(Self.Count-1) do begin
I:= J;
while (Floats[I]>Floats[I-1]) do begin
Self.Exchange(I, I-1);
Floats.Exchange(I, I-1);
if I=1 then break;
Dec(I);
end;
end;
end;
{TLongIntList constructor and destructor +++++++++++++++++++++++++++++++++++++}
constructor TLongIntList.Create;
begin
inherited Create;
end; { Create }
destructor TLongIntList.Destroy;
begin
Clear;
inherited Destroy;
end; { Destroy }
{ TLongIntList protected ++++++++++++++++++++++++++++++++++++++++++++++++++++++}
function TLongIntList.GetItems(Index: Integer): LongInt;
begin
Result:= FLongIntList[Index];
end; { GetItems }
procedure TLongIntList.SetCapacity(NewCapacity: Integer);
begin
if NewCapacity<FCount then FCount:= NewCapacity;
if NewCapacity<>FCapacity then
begin
ReallocMem(FLongIntList, NewCapacity*SizeOf(LongInt));
FCapacity:= NewCapacity;
end;
end; { SetCapacity }
procedure TLongIntList.SetCount(NewCount: Integer);
begin
if NewCount>FCapacity then SetCapacity(NewCount);
FCount:= NewCount;
end; { SetCount }
procedure TLongIntList.SetItems(Index: Integer; Item: LongInt);
begin
FLongIntList[Index]:= Item;
end; { SetItems }
{ TLongIntList public +++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
function TLongIntList.Add(Item: LongInt): Integer;
begin
Result:= FCount;
if Result+1>=FCapacity then SetCapacity(FCapacity+1024);
FLongIntList[Result]:= Item;
Inc(FCount);
end; { Add }
procedure TLongIntList.Assign(const Src: TLongIntList);
var
I: integer;
begin
if ((Self<>Src)and(Src<>nil)) then begin
Self.Clear;
for I:= 0 to Src.Count-1 do
Self.Add(Src[I]);
end;
end;
function TLongIntList.AddUniq(Item: Integer): Integer;
var
I: integer;
InList: Boolean;
begin
Result:= -1;
InList:= False;
for I:= FCount-1 downto 0 do
if Self[I]=Item then begin
InList:= True;
break;
end;
if not(InList) then
Result:= Add(Item);
end;
procedure TLongIntList.Clear;
begin
SetCount(0);
SetCapacity(0);
end; { Clear }
procedure TLongIntList.Delete(Index: Integer);
begin
Dec(FCount);
if Index<FCount then
System.Move(FLongIntList[Index+1], FLongIntList[Index],
(FCount-Index)*SizeOf(LongInt));
end; { Delete }
procedure TLongIntList.Exchange(Index1, Index2: Integer);
var
Item: LongInt;
begin
Item:= FLongIntList[Index1];
FLongIntList[Index1]:= FLongIntList[Index2];
FLongIntList[Index2]:= Item;
end; { Exchange }
function TLongIntList.First: LongInt;
begin
Result:= GetItems(0);
end; { First }
function TLongIntList.IndicesOf(anItem: LongInt; FoundPositions: TLongIntList): Boolean;
var
I: integer;
begin
try
for I:= 0 to FCount do
if FLongIntList[I]=anItem then
FoundPositions.Add(I);
Result:= True;
except
Result:= False;
end;
end; { IndicesOf }
function TLongIntList.IndexOf(Item: LongInt): Integer;
begin
Result:= 0;
while (Result<FCount)and(FLongIntList[Result]<>Item) do Inc(Result);
if Result=FCount then Result:= -1;
end; { IndexOf }
procedure TLongIntList.Insert(Index: Integer; Item: LongInt);
begin
if FCount=FCapacity then SetCapacity(FCapacity+1024);
if Index<FCount then
System.Move(FLongIntList[Index], FLongIntList[Index+1],
(FCount-Index)*SizeOf(LongInt));
FLongIntList[Index]:= Item;
Inc(FCount);
end; { Insert }
function TLongIntList.Last: LongInt;
begin
Result:= GetItems(FCount-1);
end; { Last }
procedure TLongIntList.Move(CurIndex, NewIndex: Integer);
var
Item: LongInt;
begin
if CurIndex<>NewIndex then
begin
Item:= GetItems(CurIndex);
Delete(CurIndex);
Insert(NewIndex, Item);
end;
end; { Move }
function TLongIntList.Remove(Item: LongInt): Integer;
begin
Result:= IndexOf(Item);
if Result<>-1 then Delete(Result);
end; { Remove }
procedure TLongIntList.Order(const InCrease: Boolean=True);
var
I, J: integer;
begin
if Count<2 then
Exit;
if InCrease then
for J:= 1 to(Count-1) do begin
I:= J;
while (Items[I]<Items[I-1]) do begin
Exchange(I, I-1);
if I=1 then break;
Dec(I);
end;
end
else //Decrease
for J:= 1 to(Count-1) do begin
I:= J;
while (Items[I]>Items[I-1]) do begin
Exchange(I, I-1);
if I=1 then break;
Dec(I);
end;
end;
end;
{TFloatList }
{TFloatList constructor and destructor +++++++++++++++++++++++++++++++++++++}
constructor TFloatList.Create;
begin
inherited Create;
end; { Create }
destructor TFloatList.Destroy;
begin
Clear;
inherited Destroy;
end; { Destroy }
{ TFloatList protected ++++++++++++++++++++++++++++++++++++++++++++++++++++++}
function TFloatList.GetItems(Index: Integer): double;
begin
Result:= FFloatList[Index];
end; { GetItems }
procedure TFloatList.SetCapacity(NewCapacity: Integer);
begin
if NewCapacity<FCount then FCount:= NewCapacity;
if NewCapacity<>FCapacity then
begin
ReallocMem(FFloatList, NewCapacity*SizeOf(double));
FCapacity:= NewCapacity;
end;
end; { SetCapacity }
procedure TFloatList.SetCount(NewCount: Integer);
begin
if NewCount>FCapacity then SetCapacity(NewCount);
FCount:= NewCount;
end; { SetCount }
procedure TFloatList.SetItems(Index: Integer; Item: double);
begin
FFloatList[Index]:= Item;
end; { SetItems }
{ TFloatList public +++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
function TFloatList.Add(Item: double): Integer;
begin
Result:= FCount;
if Result+1>=FCapacity then SetCapacity(FCapacity+1024);
FFloatList[Result]:= Item;
Inc(FCount);
end; { Add }
procedure TFloatList.Assign(const Src: TFloatList);
var
I: integer;
begin
if ((Self<>Src)and(Src<>nil)) then begin
Self.Clear;
for I:= 0 to Src.Count-1 do
Self.Add(Src[I]);
end;
end;
procedure TFloatList.Clear;
begin
SetCount(0);
SetCapacity(0);
end; { Clear }
procedure TFloatList.Delete(Index: Integer);
begin
Dec(FCount);
if Index<FCount then
System.Move(FFloatList[Index+1], FFloatList[Index],
(FCount-Index)*SizeOf(double));
end; { Delete }
procedure TFloatList.Exchange(Index1, Index2: Integer);
var
Item: double;
begin
Item:= FFloatList[Index1];
FFloatList[Index1]:= FFloatList[Index2];
FFloatList[Index2]:= Item;
end; { Exchange }
function TFloatList.First: double;
begin
Result:= GetItems(0);
end; { First }
function TFloatList.IndexOf(Item: double): Integer;
begin
Result:= 0;
while (Result<FCount)and(FFloatList[Result]<>Item) do Inc(Result);
if Result=FCount then Result:= -1;
end; { IndexOf }
procedure TFloatList.Insert(Index: Integer; Item: double);
begin
if FCount=FCapacity then SetCapacity(FCapacity+1024);
if Index<FCount then
System.Move(FFloatList[Index], FFloatList[Index+1],
(FCount-Index)*SizeOf(double));
FFloatList[Index]:= Item;
Inc(FCount);
end; { Insert }
function TFloatList.Last: double;
begin
Result:= GetItems(FCount-1);
end; { Last }
procedure TFloatList.Move(CurIndex, NewIndex: Integer);
var
Item: double;
begin
if CurIndex<>NewIndex then
begin
Item:= GetItems(CurIndex);
Delete(CurIndex);
Insert(NewIndex, Item);
end;
end; { Move }
function TFloatList.Remove(Item: double): Integer;
begin
Result:= IndexOf(Item);
if Result<>-1 then Delete(Result);
end; { Remove }
procedure TFloatList.Order(const InCrease: Boolean=True);
var
I, J: integer;
begin
if Count<2 then
Exit;
if InCrease then
for J:= 1 to(Count-1) do begin
I:= J;
while (Items[I]<Items[I-1]) do begin
Exchange(I, I-1);
if I=1 then break;
Dec(I);
end;
end
else //Decrease
for J:= 1 to(Count-1) do begin
I:= J;
while (Items[I]>Items[I-1]) do begin
Exchange(I, I-1);
if I=1 then break;
Dec(I);
end;
end;
end;
function TEuMemIniFile.ReadFloat(const Section, Name: string; Default: Double): Double;
var
FloatStr: string;
I: integer;
begin
FloatStr:= ReadString(Section, Name, '');
Result:= Default;
if FloatStr<>'' then
try
for I:= 1 to Length(FloatStr) do
if (FloatStr[I]=',') then
FloatStr[I]:= '.';
Val(FloatStr, Result, I);
if (I>0) then begin
MessageDlg('Error TEuMemIniFile.ReadFloat converting to double: '+FloatStr, mtWarning, [mbOK], 0);
Result:= -1;
end;
except
on EConvertError do
else raise;
end;
end;
function ListIntersect(const List1, List2: TLongIntList; var VarList: TLongIntList): boolean;
var
I: integer;
begin
if (List1<>nil)and(List2<>nil)and(VarList<>nil) then begin
try
VarList.Clear;
for I:= 0 to List1.Count-1 do
if List2.IndexOf(List1[I])>=0 then begin
VarList.AddUniq(List1[I]);
continue;
end;
Result:= true;
except
Result:= False;
end;
end
else
Result:= False;
end;
function ListUnion(const List1, List2: TLongIntList; var VarList: TLongIntList): boolean;
var
I: integer;
begin
if (List1<>nil)and(List2<>nil)and(VarList<>nil) then begin
try
VarList.Clear;
for I:= 0 to List1.Count-1 do
VarList.AddUniq(List1[I]);
for I:= 0 to List2.Count-1 do
VarList.AddUniq(List2[I]);
Result:= true;
except
Result:= False;
end;
end
else
Result:= False;
end;
function ListEqual(const List1, List2: TLongIntList): boolean;
var
I: integer;
begin
if List1.Count<>List2.Count then begin
Result:= False;
exit
end
else begin
Result:= True;
for I:= 0 to List1.Count-1 do
if List1[I]<>List2[I] then begin
Result:= False;
exit
end;
end;
end;
function AreFloatsEqualAbs(const Float1, Float2: double; const Eps: double=1.E-5): boolean;
begin
if Abs(Float1-Float2)<Eps then
Result:= True
else
Result:= False;
end;
function AreFloatsEqualRel(const Float1, Float2: double; const Eps: double=1.E-5): boolean;
begin
if (Abs(Float1)<Eps)or(Abs(Float2)<Eps) then begin
if Abs(Float1-Float2)<Eps then
Result:= True
else
Result:= False;
end
else begin
if (Abs((Float1-Float2)/Float1)<Eps) then
Result:= True
else
Result:= False
end;
end;
function PrepareToParse(const InStr: ShortString; var OutStr: ShortString): Boolean;
var
I: integer;
aStr: string;
DefaultValue: double;
begin