Skip to content

Commit 464a5ee

Browse files
committed
feat: First draft of the baseline
1 parent c6a18bb commit 464a5ee

11 files changed

Lines changed: 739 additions & 4 deletions

File tree

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
unit Baseline.Common;
2+
3+
{$IFDEF FPC}
4+
{$mode ObjFPC}{$H+}
5+
{$ENDIF}
6+
7+
interface
8+
9+
uses
10+
Classes
11+
, SysUtils
12+
{$IFDEF FPC}
13+
, Contnrs
14+
{$ELSE}
15+
{$ENDIF}
16+
17+
;
18+
19+
type
20+
{ TWeatherStation }
21+
PWeatherStation = ^TWeatherStation;
22+
TWeatherStation = record
23+
FStation: String[100];
24+
FMin: Single;
25+
FMax: Single;
26+
FTot: Single;
27+
FCnt: Integer;
28+
29+
end;
30+
{ TBaseline }
31+
TBaseline = class(TObject)
32+
private
33+
FInputFile: String;
34+
FStationNames: TStringList;
35+
FHashStationList: TFPHashList;
36+
37+
procedure AddToHashList(AStation: String; ATemp: Single);
38+
procedure BuildHashList;
39+
protected
40+
public
41+
constructor Create(AInputFile: String);
42+
destructor Destroy; override;
43+
44+
procedure Generate;
45+
published
46+
end;
47+
48+
{$IFNDEF FPC}
49+
TStringArray = array of Utf8String;
50+
TWriteBufStream = TFileStream;
51+
{$ENDIF}
52+
53+
implementation
54+
55+
uses
56+
Math
57+
{$IFDEF FPC}
58+
, streamex
59+
{$ELSE}
60+
, System.Diagnostics
61+
{$IF defined(MSWINDOWS)}, Winapi.Windows{$ENDIF}
62+
{$ENDIF}
63+
;
64+
65+
const
66+
//lineEnding = #13#10;
67+
stationsCapacity = 50000;
68+
69+
70+
function Compare(AList: TStringList; AIndex1, AIndex2: Integer): Integer;
71+
var
72+
Pos1, Pos2: Integer;
73+
Str1, Str2: String;
74+
begin
75+
Result := 0;
76+
Str1 := AList.Strings[AIndex1];
77+
Str2 := AList.Strings[AIndex2];
78+
Pos1 := Pos('=', Str1);
79+
Pos2 := Pos('=', Str2);
80+
if (Pos1 > 0) and (Pos2 > 0) then
81+
begin
82+
Str1 := Copy(Str1, 1, Pos1 - 1);
83+
Str2 := Copy(Str2, 1, Pos2 - 1);
84+
Result := CompareStr(Str1, Str2);
85+
end;
86+
end;
87+
88+
{ TBaseline }
89+
90+
constructor TBaseline.Create(AInputFile: String);
91+
begin
92+
FInputFile := AInputFile;
93+
94+
FHashStationList:= TFPHashList.Create;
95+
FHashStationList.Capacity:= stationsCapacity;
96+
97+
FStationNames := TStringList.Create;
98+
FStationNames.Capacity := stationsCapacity;
99+
FStationNames.UseLocale := False;
100+
end;
101+
102+
destructor TBaseline.Destroy;
103+
var
104+
index: Integer;
105+
begin
106+
FStationNames.Free;
107+
for index:= 0 to FHashStationList.Count - 1 do
108+
begin
109+
Dispose(PWeatherStation(FHashStationList.Items[index]));
110+
end;
111+
FHashStationList.Free;
112+
inherited Destroy;
113+
end;
114+
115+
procedure TBaseline.AddToHashList(AStation: String; ATemp: Single);
116+
var
117+
weatherStation: PWeatherStation;
118+
Index: Integer;
119+
begin
120+
Index := FHashStationList.FindIndexOf(AStation);
121+
if Index = -1 then
122+
begin
123+
New(weatherStation);
124+
weatherStation^.FStation := AStation;
125+
weatherStation^.FMin := ATemp;
126+
weatherStation^.FMax := ATemp;
127+
weatherStation^.FTot := ATemp;
128+
weatherStation^.FCnt := 1;
129+
FHashStationList.Add(AStation, weatherStation);
130+
end
131+
else
132+
begin
133+
weatherStation := FHashStationList.Items[Index];
134+
weatherStation^.FMin := Min(weatherStation^.FMin, ATemp);
135+
weatherStation^.FMax := Max(weatherStation^.FMax, ATemp);
136+
weatherStation^.FTot := weatherStation^.FTot + ATemp;
137+
weatherStation^.FCnt := weatherStation^.FCnt + 1;
138+
end;
139+
end;
140+
141+
procedure TBaseline.BuildHashList;
142+
var
143+
inputFileStream: TFileStream;
144+
streamReader: TStreamReader;
145+
position, Code: Integer;
146+
strLine: String;
147+
strStation: String;
148+
strTemp: String;
149+
temparature: Single;
150+
begin
151+
if FileExists(FInputFile) then
152+
begin
153+
inputFileStream:= TFileStream.Create(FInputFile, fmOpenRead);
154+
try
155+
streamReader:= TStreamReader.Create(inputFileStream);
156+
try
157+
while not streamReader.Eof do
158+
begin
159+
strLine:= streamReader.ReadLine;
160+
position := Pos(';', strLine);
161+
if position > 0 then
162+
begin
163+
strStation := Copy(strLine, 1, position - 1);
164+
strTemp := Copy(strLine, position + 1, Length(strLine));
165+
Val(strTemp, temparature, Code);
166+
if Code <> 0 then
167+
Continue;
168+
AddToHashList(strStation, temparature);
169+
end;
170+
end;
171+
finally
172+
streamReader.Free;
173+
end;
174+
finally
175+
inputFileStream.Free;
176+
end;
177+
end
178+
else
179+
begin
180+
raise Exception.Create(Format('File "%s" not found.', [FInputFile]));
181+
end;
182+
end;
183+
184+
procedure TBaseline.Generate;
185+
var
186+
index: Integer;
187+
strTemp: String;
188+
mean: Single;
189+
weatherStation: PWeatherStation;
190+
begin
191+
192+
BuildHashList;
193+
194+
//FStationNames.DefaultEncoding := TEncoding.UTF8;
195+
FStationNames.BeginUpdate;
196+
for index := 0 to FHashStationList.Count - 1 do
197+
begin
198+
weatherStation := FHashStationList.Items[index];
199+
weatherStation^.FTot := Round(weatherStation^.FTot*10)/10;
200+
mean := weatherStation^.FTot/weatherStation^.FCnt;
201+
strTemp := weatherStation^.FStation + '=' + FormatFloat('0.0', weatherStation^.FMin) + '/' + FormatFloat('0.0', mean) + '/' + FormatFloat('0.0', weatherStation^.FMax) + ',';
202+
FStationNames.Add(strTemp);
203+
end;
204+
FStationNames.EndUpdate;
205+
FStationNames.CustomSort(@Compare);
206+
207+
strTemp:= '';
208+
for index:= 0 to FStationNames.Count - 1 do
209+
begin
210+
strTemp:= strTemp + FStationNames[index] + ' ';
211+
end;
212+
SetLength(strTemp, Length(strTemp) - 2);
213+
WriteLn('{', strTemp, '}');
214+
215+
end;
216+
217+
end.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
unit Baseline.Console;
2+
3+
{$IFDEF FPC}
4+
{$mode ObjFPC}{$H+}
5+
{$ENDIF}
6+
7+
interface
8+
9+
uses
10+
Classes
11+
, SysUtils
12+
;
13+
14+
const
15+
cShortOptHelp: Char = 'h';
16+
cLongOptHelp = 'help';
17+
cShortOptVersion: Char = 'v';
18+
cLongOptVersion = 'version';
19+
cShortOptInput: Char = 'i';
20+
cLongOptInput = 'input-file';
21+
{$IFNDEF FPC}
22+
cShortOptions: array of char = ['h', 'v'{, 'i', 'o', 'n'}];
23+
cLongOptions: array of string = ['help', 'version'{, 'input-file', 'output-file',
24+
'line-count'}];
25+
{$ENDIF}
26+
27+
resourcestring
28+
rsAppTitle = 'One Billion Row Challenge Baseline';
29+
rsGeneratorVersion = 'baseline v%s';
30+
rsErrorMessage = 'ERROR: %s';
31+
rsMissingInputFlag = 'Missing input file flag.';
32+
33+
var
34+
inputFilename: String = '';
35+
{outputFilename: String = '';
36+
lineCount: Integer = 0;}
37+
38+
procedure WriteHelp;
39+
40+
41+
implementation
42+
43+
procedure WriteHelp;
44+
begin
45+
WriteLn('Generates the output for the challenge');
46+
WriteLn;
47+
WriteLn('USAGE');
48+
WriteLn(' baseline <flags>');
49+
WriteLn;
50+
WriteLn('FLAGS');
51+
WriteLn(' -h|--help Writes this help message and exits');
52+
WriteLn(' -v|--version Writes the version and exits');
53+
WriteLn(' -i|--input-file <filename> The file containing the Weather Stations');
54+
end;
55+
56+
end.
57+

baseline/Common/version.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const
2+
cVersion = '0.1';

0 commit comments

Comments
 (0)