11unit WeatherStation;
22
33{ $mode objfpc}{ $H+}{ $J-}{ $modeSwitch advancedRecords}
4+
45interface
56
67uses
78 Classes,
89 SysUtils,
9- Generics.Collections,
1010 Math,
1111 streamex,
1212 lgHashMap
@@ -26,7 +26,7 @@ TParsedData = record
2626 TStat = record
2727 var
2828 min: int64; // Borrowed the concept from go's approach to improve
29- // performance, save floats as int64.
29+ // performance, save floats as int64.
3030 max: int64; // This saved ~2 mins processing time.
3131 sum: int64;
3232 cnt: int64;
@@ -48,8 +48,9 @@ TWeatherStation = class
4848 weatherDictionary: TWeatherDictionaryLG;
4949 weatherStationList: TStringList;
5050 procedure ReadMeasurementsInChunk ;
51- procedure ProcessChunk (const chunkData: pansichar; const dataSize: integer; const chunkIndex: integer);
52- procedure ParseStationAndTempFromLine (line: string);
51+ procedure ProcessChunk (const chunkData: pansichar; const dataSize: int64;
52+ const chunkIndex: int64);
53+ procedure ParseStationAndTempFromLine (const line: string);
5354 procedure AddCityTemperatureLG (const cityName: string; const newTemp: int64);
5455 procedure SortWeatherStationAndStats ;
5556 procedure PrintSortedWeatherStationAndStats ;
@@ -101,24 +102,24 @@ function RemoveDots(const line: string): string;
101102 end ;
102103end ;
103104
104- function RoundEx (x: Currency ): Double ; inline;
105+ function RoundEx (x: currency ): double ; inline;
105106begin
106107 Result := Ceil(x * 10 ) / 10 ;
107108end ;
108109
109- function RoundExInteger (x: Currency ): Integer ; inline;
110+ function RoundExInteger (x: currency ): integer ; inline;
110111begin
111112 Result := Ceil(x * 10 );
112113end ;
113114
114115{ Neater version by @bytebites from Lazarus forum }
115- function RoundExString (x: Currency ): String ; inline;
116+ function RoundExString (x: currency ): string ; inline;
116117var
117- V, Q, R: Integer ;
118+ V, Q, R: integer ;
118119begin
119120 V := RoundExInteger(x);
120121 divmod(V, 10 , Q, R);
121- Result := IntToStr(Q) + ' .' + chr(48 + Abs(R))
122+ Result := IntToStr(Q) + ' .' + chr(48 + Abs(R));
122123end ;
123124
124125constructor TStat.Create(const newMin: int64; const newMax: int64;
@@ -187,7 +188,7 @@ procedure TWeatherStation.SortWeatherStationAndStats;
187188begin
188189 wsKey := ' ' ;
189190
190- if self.weatherDictionary.Count = 0 then
191+ if self.weatherDictionary.GetCapacity = 0 then
191192 begin
192193 WriteLn(' Nothing to Sort.' );
193194 Exit;
@@ -242,12 +243,13 @@ procedure TWeatherStation.AddCityTemperatureLG(const cityName: string;
242243 end ;
243244end ;
244245
245- procedure TWeatherStation.ParseStationAndTempFromLine (line: string);
246+ procedure TWeatherStation.ParseStationAndTempFromLine (const line: string);
246247var
247248 delimiterPos: integer;
248249 parsedStation, strTemp: string;
249250 parsedTemp, valCode: int64;
250251begin
252+
251253 // Get position of the delimiter
252254 delimiterPos := Pos(' ;' , line);
253255 if delimiterPos > 0 then
@@ -266,6 +268,7 @@ procedure TWeatherStation.ParseStationAndTempFromLine(line: string);
266268 // in each iteration. Saved approx 20-30 seconds for 1 billion row.
267269 // Remove dots turns a float into an int.
268270 strTemp := RemoveDots(strTemp);
271+ strTemp := StringReplace(strTemp, ' \n' , ' ' , [rfReplaceAll]);
269272
270273 // Add the weather station and the recorded temp (as int64) in the TDictionary
271274 Val(strTemp, parsedTemp, valCode);
@@ -276,8 +279,7 @@ procedure TWeatherStation.ParseStationAndTempFromLine(line: string);
276279 end ;
277280end ;
278281
279- procedure TWeatherStation.ProcessChunk (const chunkData: pansichar;
280- const dataSize: integer; const chunkIndex: integer);
282+ procedure TWeatherStation.ProcessChunk (const chunkData: pansichar;const dataSize: int64; const chunkIndex: int64);
281283var
282284 bufferStream: TMemoryStream;
283285 streamReader: TStreamReader;
@@ -326,7 +328,7 @@ procedure TWeatherStation.ReadMeasurementsInChunk;
326328begin
327329
328330 // Set buffer size here, not too big.
329- chunkSize:= chunkSize * 1 ;
331+ chunkSize := chunkSize * 1 ;
330332
331333 // Open the file for reading
332334 fileStream := TFileStream.Create(self.fname, fmOpenRead);
0 commit comments