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
2526type TStationEntry = record
26- min, max, sum: Double ;
27+ min, max, sum: Int64 ;
2728 count: Integer;
2829end ;
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;
3941begin
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