Skip to content

Commit 96ca4c0

Browse files
committed
refactor: Delphi baseline using Common
1 parent 9f83b70 commit 96ca4c0

8 files changed

Lines changed: 272 additions & 142 deletions

File tree

baseline/Common/baseline.console.pas

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface
2525
cOptionInput: array of string = ['-i', '--input-file'];
2626
{$ENDIF}
2727

28+
{$I version.inc}
29+
2830
resourcestring
2931
rsAppTitle = 'One Billion Row Challenge Baseline';
3032
rsGeneratorVersion = 'baseline v%s';
@@ -69,8 +71,10 @@ function ArrayContains (const aArray: array of string; const aValue: string): Bo
6971
iValue: string;
7072
begin
7173
Result := False;
72-
for iValue in aArray do begin
73-
if aValue.ToLower = iValue then begin
74+
for iValue in aArray do
75+
begin
76+
if aValue.ToLower = iValue then
77+
begin
7478
Result := True;
7579
break;
7680
end;
@@ -85,22 +89,27 @@ function ParseCmdLineParams (out aInputFile: string): Boolean;
8589
aInputFile := '';
8690

8791
// 0 is the exe path, so we start at 1
88-
for I := 1 to ParamCount do begin
89-
if ArrayContains (cOptionHelp, ParamStr(I)) then begin
92+
for I := 1 to ParamCount do
93+
begin
94+
if ArrayContains (cOptionHelp, ParamStr(I)) then
95+
begin
9096
WriteHelp;
9197
exit;
9298
end
93-
else if ArrayContains (cOptionVersion, ParamStr(I)) then begin
99+
else if ArrayContains (cOptionVersion, ParamStr(I)) then
100+
begin
94101
WriteLn(Format(rsGeneratorVersion, [ cVersion ]));
95102
exit;
96103
end
97-
else if ArrayContains (cOptionInput, ParamStr(I)) then begin
104+
else if ArrayContains (cOptionInput, ParamStr(I)) then
105+
begin
98106
// must be followed by the user's specified input file
99107
if (I+1) <= ParamCount then
100108
aInputFile := ExpandFileName (ParamStr (I+1));
101109
if not TFile.Exists (aInputFile) then
102110
WriteLn(Format(rsErrorMessage, [ Format(rsNoInputFile, [aInputFile]) ]))
103-
else begin
111+
else
112+
begin
104113
Result := True;
105114
exit;
106115
end;
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
unit OneBRC;
1+
unit Baseline.Delphi;
22

33
interface
44

55
uses
6-
System.Classes, System.Generics.Collections;
6+
System.Classes
7+
, System.Generics.Collections
8+
;
79

810
type
9-
// alias, to easily try various types (Int64, Currency, Double)
10-
// due to rounding problems during the average calculation,
11-
// and the mismatch between Delphi and Lazarus.
11+
// alias, to easily try various types (Int64, Currency, Double)
12+
// due to rounding problems during the average calculation,
13+
// and the mismatch between Delphi and Lazarus.
1214
TAmount = Int64;
1315

14-
{ TWeatherStation }
16+
{ TWeatherStation }
1517
PWeatherStation = ^TWeatherStation;
1618
TWeatherStation = record
1719
FStation: string;
@@ -21,28 +23,31 @@ TWeatherStation = record
2123
FCnt: Integer;
2224
end;
2325

24-
{ TBaseline }
26+
{ TBaseline }
2527
TBaseline = class
2628
private
2729
FInputFile: String;
2830
FStationNames: TStringList;
2931
FHashStationList: TDictionary<string, PWeatherStation>;
3032
procedure AddToHashList (AStation: String; ATemp: TAmount);
3133
procedure BuildHashList;
32-
function RoundEx (x: Currency): Double;
3334
protected
3435
public
3536
constructor Create (AInputFile: String);
3637
destructor Destroy; override;
3738

3839
procedure Generate;
39-
end;
40+
end;
4041

4142

4243
implementation
4344

4445
uses
45-
System.SysUtils, System.StrUtils, System.Math;
46+
System.SysUtils
47+
, System.StrUtils
48+
, System.Math
49+
, Baseline.Common
50+
;
4651

4752

4853
function Compare (AList: TStringList; AIndex1, AIndex2: Integer): Integer;
@@ -63,7 +68,6 @@ function Compare (AList: TStringList; AIndex1, AIndex2: Integer): Integer;
6368
end;
6469
end;
6570

66-
//---------------------------------------------------
6771
{ TBaseline }
6872

6973
constructor TBaseline.Create (AInputFile: String);
@@ -81,7 +85,8 @@ destructor TBaseline.Destroy;
8185
var
8286
iStation: PWeatherStation;
8387
begin
84-
for iStation in FHashStationList.Values do begin
88+
for iStation in FHashStationList.Values do
89+
begin
8590
Dispose(iStation);
8691
end;
8792
FStationNames.Free;
@@ -93,8 +98,8 @@ procedure TBaseline.AddToHashList(AStation: String; ATemp: TAmount);
9398
var
9499
vWeatherStation: PWeatherStation;
95100
begin
96-
// on Delphi, the below station returns a different average from the baseline.output provided
97-
// this issue was resolved after
101+
// on Delphi, the below station returns a different average from the baseline.output provided
102+
// this issue was resolved after
98103
// if aStation = 'Danau Kändimarg' then begin
99104
// FProblematicSum := FProblematicSum + aTemp;
100105
// Inc (FProblematicCount);
@@ -156,11 +161,6 @@ procedure TBaseline.BuildHashList;
156161
end;
157162
end;
158163

159-
function TBaseline.RoundEx(x: Currency): Double;
160-
begin
161-
Result := Ceil(x * 10) / 10;
162-
end;
163-
164164
procedure TBaseline.Generate;
165165
var
166166
index: Integer;
@@ -170,23 +170,27 @@ procedure TBaseline.Generate;
170170
mean: Double;
171171
weatherStation: PWeatherStation;
172172
iStation: string;
173+
{$IFDEF DEBUG}
173174
vStream: TStringStream;
175+
{$ENDIF}
174176
begin
175177
BuildHashList;
176178
FStationNames.BeginUpdate;
177-
for iStation in FHashStationList.Keys do begin
179+
for iStation in FHashStationList.Keys do
180+
begin
178181
FHashStationList.TryGetValue(iStation, weatherStation);
179182
Min := weatherStation^.FMin/10;
180183
Max := weatherStation^.FMax/10;
181-
Mean := RoundEx(weatherStation^.FTot/weatherStation^.FCnt/10);
184+
Mean := RoundExDouble(weatherStation^.FTot/weatherStation^.FCnt/10);
182185
strTemp := weatherStation^.FStation + '=' + FormatFloat('0.0', Min) + '/' + FormatFloat('0.0', Mean) + '/' + FormatFloat('0.0', Max) + ',';
183186
FStationNames.Add(strTemp);
184187
end;
185188
FStationNames.EndUpdate;
186189
FStationNames.CustomSort(Compare);
187190

188191
strTemp:= '';
189-
for index:= 0 to FStationNames.Count - 1 do begin
192+
for index:= 0 to FStationNames.Count - 1 do
193+
begin
190194
strTemp:= strTemp + FStationNames[index] + ' ';
191195
end;
192196
SetLength(strTemp, Length(strTemp) - 2);

baseline/Common/baseline.lazarus.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ TWeatherStation = record
2020
FMax: Int64;
2121
FTot: Int64;
2222
FCnt: Integer;
23-
2423
end;
24+
2525
{ TBaseline }
2626
TBaseline = class(TObject)
2727
private

baseline/Delphi/src/Baseline.Console.pas

Lines changed: 0 additions & 92 deletions
This file was deleted.

baseline/Delphi/src/Baseline.dpr

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
{$R *.res}
66

77
uses
8-
System.SysUtils,
9-
Baseline.Console in 'Baseline.Console.pas',
10-
OneBRC in 'OneBRC.pas';
8+
System.SysUtils
9+
, Baseline.Console
10+
, Baseline.Delphi
11+
;
1112

1213
var
13-
vInputFilePath: string;
14+
//vInputFilePath: string;
1415
vBaseline: TBaseline;
1516
begin
1617
try
17-
if ParseCmdLineParams (vInputFilePath) then begin
18-
vBaseline := TBaseline.Create (vInputFilePath);
18+
if ParseCmdLineParams (inputFilename) then
19+
begin
20+
vBaseline := TBaseline.Create (inputFilename);
1921
try
2022
vBaseline.Generate;
2123
finally
@@ -25,6 +27,6 @@ begin
2527

2628
except
2729
on E: Exception do
28-
Writeln(E.ClassName, ': ', E.Message);
30+
Writeln(Format(rsErrorMessage, [ E.ClassName, ': ', E.Message ]));
2931
end;
3032
end.

0 commit comments

Comments
 (0)