Skip to content

Commit 220bbcb

Browse files
authored
Merge pull request #115 from dtpfl/main
Changes dtpfl + locale baseline delphi
2 parents 3705e59 + a061702 commit 220bbcb

2 files changed

Lines changed: 40 additions & 45 deletions

File tree

baseline/Common/baseline.delphi.pas

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ interface
55
uses
66
System.Classes
77
, System.Generics.Collections
8+
, System.SysUtils
89
;
910

1011
type
@@ -28,6 +29,7 @@ TBaseline = class
2829
private
2930
FInputFile: String;
3031
FStationNames: TStringList;
32+
FFormatSettings: TFormatSettings;
3133
FHashStationList: TDictionary<string, PWeatherStation>;
3234
procedure AddToHashList (AStation: String; ATemp: TAmount);
3335
procedure BuildHashList;
@@ -43,8 +45,7 @@ TBaseline = class
4345
implementation
4446

4547
uses
46-
System.SysUtils
47-
, System.StrUtils
48+
System.StrUtils
4849
, System.Math
4950
, Baseline.Common
5051
;
@@ -72,6 +73,8 @@ function Compare (AList: TStringList; AIndex1, AIndex2: Integer): Integer;
7273

7374
constructor TBaseline.Create (AInputFile: String);
7475
begin
76+
FFormatSettings := TFormatSettings.Create;
77+
FFormatSettings.DecimalSeparator := '.';
7578
FInputFile := AInputFile;
7679

7780
FHashStationList := TDictionary<string, PWeatherStation>.Create;
@@ -182,7 +185,7 @@ procedure TBaseline.Generate;
182185
Min := weatherStation^.FMin/10;
183186
Max := weatherStation^.FMax/10;
184187
Mean := RoundExDouble(weatherStation^.FTot/weatherStation^.FCnt/10);
185-
strTemp := weatherStation^.FStation + '=' + FormatFloat('0.0', Min) + '/' + FormatFloat('0.0', Mean) + '/' + FormatFloat('0.0', Max) + ',';
188+
strTemp := weatherStation^.FStation + '=' + FormatFloat('0.0', Min, FFormatSettings) + '/' + FormatFloat('0.0', Mean, FFormatSettings) + '/' + FormatFloat('0.0', Max, FFormatSettings) + ',';
186189
FStationNames.Add(strTemp);
187190
end;
188191
FStationNames.EndUpdate;

entries/dtoepfl/src/dtoepfl.dpr

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ uses
1111
System.TimeSpan,
1212
System.Generics.Collections,
1313
System.Diagnostics,
14+
Math,
1415
generate.console in '..\..\..\generator\Common\generate.console.pas',
1516
Baseline.Common in '..\..\..\Baseline\Common\Baseline.Common.pas';
1617

@@ -23,7 +24,7 @@ const paramPrefix = '--';
2324
{$REGION 'v1'}
2425

2526
type TStationEntry = record
26-
min, max, sum: Double;
27+
min, max, sum: Int64;
2728
count: Integer;
2829
end;
2930

@@ -33,9 +34,10 @@ var FileStream: TFileStream;
3334
StreamReader: TStreamReader;
3435
CityTemperatures: TDictionary<String, TStationEntry>; //Station, min, avrg, max
3536
Line: String;
36-
Parts: TArray<String>;
37+
Station: String;
3738
entry: TStationEntry;
38-
value: Double;
39+
valInt: Int64;
40+
position: Int16;
3941
begin
4042
CityTemperatures := TDictionary<String, TStationEntry>.Create;
4143

@@ -44,45 +46,39 @@ begin
4446
try
4547
StreamReader := TStreamReader.Create(FileStream, TEncoding.UTF8);
4648

47-
// Read the first three bytes to check for UTF-8 BOM
48-
// var NumRead := FileStream.Read(BOM, Length(BOM));
49-
// If the file starts with the UTF-8 BOM, continue as is, otherwise reset the stream position.
50-
// if (NumRead <> Length(BOM)) or (BOM[0] <> $EF) or (BOM[1] <> $BB) or (BOM[2] <> $BF) then
51-
// begin
52-
// FileStream.Position := 0; // Reset the position if no BOM or not a UTF-8 BOM
53-
// end;
54-
5549
try
5650
while not StreamReader.EndOfStream do
5751
begin
5852
Line := StreamReader.ReadLine;
59-
Parts := SplitString(Line, ';');
60-
if (TryStrToFloat(Parts[1], value, FormatSettings)) then
53+
position := Pos(';', Line);
54+
Station := Copy(Line, 1, position - 1);
55+
56+
valInt := StringReplace(Copy(Line, position + 1, Length(Line)), '.', '', [rfReplaceAll]).ToInt64;
57+
58+
if (CityTemperatures.ContainsKey(Station)) then
6159
begin
62-
if (CityTemperatures.ContainsKey(Parts[0])) then
63-
begin
64-
entry := CityTemperatures[Parts[0]];
65-
66-
if (value < entry.min) then //min
67-
entry.min := value;
68-
69-
entry.sum := (entry.sum + value); //average
70-
entry.count := entry.count + 1;
71-
72-
if (value > entry.max) then //max
73-
entry.max := value;
74-
75-
CityTemperatures[Parts[0]] := entry;
76-
end
77-
else
78-
begin
79-
entry.min := value;
80-
entry.count := 1;
81-
entry.sum := value;
82-
entry.max := value;
83-
CityTemperatures.Add(Parts[0], entry);
84-
end;
60+
entry := CityTemperatures[Station];
61+
62+
if (valInt < entry.min) then //min
63+
entry.min := valInt;
64+
65+
entry.sum := (entry.sum + valInt); //average
66+
Inc(entry.count);
67+
68+
if (valInt > entry.max) then //max
69+
entry.max := valInt;
70+
71+
CityTemperatures[Station] := entry;
72+
end
73+
else
74+
begin
75+
entry.min := valInt;
76+
entry.count := 1;
77+
entry.sum := valInt;
78+
entry.max := valInt;
79+
CityTemperatures.Add(Station, entry);
8580
end;
81+
8682
end;
8783

8884

@@ -96,15 +92,11 @@ begin
9692
try
9793
for var Key in SortedKeys do
9894
begin
99-
100-
// Windows will mess up the characters when outputting to STDOUT.
101-
// for debug purposes, we'll output it to a file instead.
102-
//https://github.com/gcarreno/1brc-ObjectPascal/blob/main/baseline/Common/baseline.delphi.pas @https://github.com/georges-hatem
10395
{$IFDEF DEBUG}
10496
if (i = 0) then
10597
vStream.WriteString('{');
10698

107-
vStream.WriteString(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min, RoundExDouble(CityTemperatures[key].sum / CityTemperatures[key].count), CityTemperatures[key].max], FormatSettings));
99+
vStream.WriteString(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min / 10, RoundExDouble( (CityTemperatures[key].sum / 10) / CityTemperatures[key].count) , CityTemperatures[key].max / 10], FormatSettings));
108100

109101
if (i = CityTemperatures.Count-1) then
110102
vStream.WriteString('}' + #10)
@@ -115,7 +107,7 @@ begin
115107
if (i = 0) then
116108
Write('{');
117109

118-
Write(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min, RoundExDouble(CityTemperatures[key].sum / CityTemperatures[key].count), CityTemperatures[key].max], FormatSettings));
110+
Write(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min / 10, RoundExDouble( (CityTemperatures[key].sum / 10) / CityTemperatures[key].count), CityTemperatures[key].max / 10], FormatSettings));
119111

120112
if (i = CityTemperatures.Count-1) then
121113
Write('}' + #10)

0 commit comments

Comments
 (0)