-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbaseline.delphi.pas
217 lines (194 loc) · 5.32 KB
/
baseline.delphi.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
unit Baseline.Delphi;
interface
uses
System.Classes
, System.Generics.Collections
, System.SysUtils
;
type
// alias, to easily try various types (Int64, Currency, Double)
// due to rounding problems during the average calculation,
// and the mismatch between Delphi and Lazarus.
TAmount = Int64;
{ TWeatherStation }
PWeatherStation = ^TWeatherStation;
TWeatherStation = record
FStation: string;
FMin: TAmount;
FMax: TAmount;
FTot: TAmount;
FCnt: Integer;
end;
{ TBaseline }
TBaseline = class
private
FInputFile: String;
FStationNames: TStringList;
FFormatSettings: TFormatSettings;
FHashStationList: TDictionary<string, PWeatherStation>;
procedure AddToHashList (AStation: String; ATemp: TAmount);
procedure BuildHashList;
protected
public
constructor Create (AInputFile: String);
destructor Destroy; override;
procedure Generate;
end;
implementation
uses
System.StrUtils
, System.Math
, Baseline.Common
;
function Compare (AList: TStringList; AIndex1, AIndex2: Integer): Integer;
var
Pos1, Pos2: Integer;
Str1, Str2: String;
begin
Result := 0;
Str1 := AList.Strings[AIndex1];
Str2 := AList.Strings[AIndex2];
Pos1 := Pos('=', Str1);
Pos2 := Pos('=', Str2);
if (Pos1 > 0) and (Pos2 > 0) then
begin
Str1 := Copy(Str1, 1, Pos1 - 1);
Str2 := Copy(Str2, 1, Pos2 - 1);
Result := CompareStr(Str1, Str2);
end;
end;
{ TBaseline }
constructor TBaseline.Create (AInputFile: String);
begin
FFormatSettings := TFormatSettings.Create;
FFormatSettings.DecimalSeparator := '.';
FInputFile := AInputFile;
FHashStationList := TDictionary<string, PWeatherStation>.Create;
FStationNames := TStringList.Create;
FStationNames.DefaultEncoding := TEncoding.UTF8;
FStationNames.UseLocale := True;
end;
destructor TBaseline.Destroy;
var
iStation: PWeatherStation;
begin
for iStation in FHashStationList.Values do
begin
Dispose(iStation);
end;
FStationNames.Free;
FHashStationList.Free;
inherited Destroy;
end;
procedure TBaseline.AddToHashList(AStation: String; ATemp: TAmount);
var
vWeatherStation: PWeatherStation;
begin
// on Delphi, the below station returns a different average from the baseline.output provided
// this issue was resolved after
// if aStation = 'Danau Kändimarg' then begin
// FProblematicSum := FProblematicSum + aTemp;
// Inc (FProblematicCount);
// end;
if not FHashStationList.TryGetValue(AStation, vWeatherStation) then
begin
New(vWeatherStation);
vWeatherStation^.FStation := AStation;
vWeatherStation^.FMin := ATemp;
vWeatherStation^.FMax := ATemp;
vWeatherStation^.FTot := ATemp;
vWeatherStation^.FCnt := 1;
FHashStationList.Add(AStation, vWeatherStation);
end
else
begin
vWeatherStation^.FMin := Min(vWeatherStation^.FMin, ATemp);
vWeatherStation^.FMax := Max(vWeatherStation^.FMax, ATemp);
vWeatherStation^.FTot := vWeatherStation^.FTot + ATemp;
vWeatherStation^.FCnt := vWeatherStation^.FCnt + 1;
end;
end;
procedure TBaseline.BuildHashList;
var
inputFileStream: TFileStream;
streamReader: TStreamReader;
position, Code: Integer;
strLine: String;
strStation: String;
strTemp: String;
temperature: Int64;
begin
inputFileStream := TFileStream.Create(FInputFile, fmOpenRead);
try
streamReader := TStreamReader.Create(inputFileStream);
try
while not streamReader.EndOfStream do
begin
strLine:= streamReader.ReadLine;
position := Pos(';', strLine);
if position > 0 then
begin
strStation := Copy(strLine, 1, position - 1);
strTemp := Copy(strLine, position + 1, Length(strLine));
strTemp := StringReplace(strTemp, '.', '', [rfReplaceAll]);
Val(strTemp, temperature, Code);
if Code <> 0 then
Continue;
AddToHashList(strStation, temperature);
end;
end;
finally
streamReader.Free;
end;
finally
inputFileStream.Free;
end;
end;
procedure TBaseline.Generate;
var
index: Integer;
strTemp: string;
min: Double;
max: Double;
mean: Double;
weatherStation: PWeatherStation;
iStation: string;
{$IFDEF DEBUG}
vStream: TStringStream;
{$ENDIF}
begin
BuildHashList;
FStationNames.BeginUpdate;
for iStation in FHashStationList.Keys do
begin
FHashStationList.TryGetValue(iStation, weatherStation);
Min := weatherStation^.FMin/10;
Max := weatherStation^.FMax/10;
Mean := RoundExDouble(weatherStation^.FTot/weatherStation^.FCnt/10);
strTemp := weatherStation^.FStation + '=' + FormatFloat('0.0', Min, FFormatSettings) + '/' + FormatFloat('0.0', Mean, FFormatSettings) + '/' + FormatFloat('0.0', Max, FFormatSettings) + ',';
FStationNames.Add(strTemp);
end;
FStationNames.EndUpdate;
FStationNames.CustomSort(Compare);
strTemp:= '';
for index:= 0 to FStationNames.Count - 1 do
begin
strTemp:= strTemp + FStationNames[index] + ' ';
end;
SetLength(strTemp, Length(strTemp) - 2);
// Windows will mess up the characters when outputting to STDOUT.
// for debug purposes, we'll output it to a file instead.
{$IFDEF DEBUG}
vStream := TStringStream.Create('', TEncoding.UTF8);
try
vStream.WriteString('{' + strTemp + '}' + #10);
vStream.SaveToFile('output.txt');
finally
vStream.Free;
end;
{$ENDIF}
{$IFDEF RELEASE}
Write ('{' + strTemp + '}' + #10);
{$ENDIF}
end;
end.