Skip to content

Commit 7d0be71

Browse files
committed
added only 400 weather stations parameter
1 parent c5c8022 commit 7d0be71

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

generator/Common/generate.common.pas

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface
1616
{ TGenerator }
1717
TGenerator = class(TObject)
1818
private
19+
FOnly400Stations: Boolean;
1920
rndState: Array [0..1] of Cardinal;
2021
FInputFile: String;
2122
FOutPutFile: String;
@@ -27,7 +28,7 @@ TGenerator = class(TObject)
2728
AFileSize: Int64; ATimeElapsed: TDateTime): String;
2829
function Rng1brc(Range: Integer): Integer;
2930
public
30-
constructor Create(AInputFile, AOutputFile: String; ALineCount: Int64);
31+
constructor Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean = False);
3132
destructor Destroy; override;
3233

3334
procedure Generate;
@@ -61,11 +62,12 @@ implementation
6162

6263
{ TGenerator }
6364

64-
constructor TGenerator.Create(AInputFile, AOutputFile: String; ALineCount: Int64);
65+
constructor TGenerator.Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean);
6566
begin
6667
FInputFile := AInputFile;
6768
FOutPutFile := AOutputFile;
6869
FLineCount := ALineCount;
70+
FOnly400Stations := AOnly400Stations;
6971

7072
FStationNames := TStringList.Create;
7173
FStationNames.Capacity := stationsCapacity;
@@ -196,6 +198,13 @@ procedure TGenerator.Generate;
196198
progressBatch := floor(FLineCount * (linesPercent / 100));
197199
start := Now;
198200

201+
if FOnly400Stations then
202+
begin
203+
WriteLn('Only 400 weather stations in output file.');
204+
while FStationNames.Count > 400 do
205+
FStationNames.Delete(Rng1brc(FStationNames.Count));
206+
end;
207+
199208
// This is all paweld magic:
200209
// From here
201210
// based on code @domasz from lazarus forum, github: PascalVault

generator/Common/generate.console.pas

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ interface
1212
;
1313

1414
const
15-
cShortOptHelp: Char = 'h';
16-
cLongOptHelp = 'help';
17-
cShortOptVersion: Char = 'v';
18-
cLongOptVersion = 'version';
19-
cShortOptInput: Char = 'i';
20-
cLongOptInput = 'input-file';
21-
cShortOptOutput: Char = 'o';
22-
cLongOptOutput = 'output-file';
23-
cShortOptNumber: Char = 'n';
24-
cLongOptNumber = 'line-count';
15+
cShortOptHelp: Char = 'h';
16+
cLongOptHelp = 'help';
17+
cShortOptVersion: Char = 'v';
18+
cLongOptVersion = 'version';
19+
cShortOptInput: Char = 'i';
20+
cLongOptInput = 'input-file';
21+
cShortOptOutput: Char = 'o';
22+
cLongOptOutput = 'output-file';
23+
cShortOptNumber: Char = 'n';
24+
cLongOptNumber = 'line-count';
25+
cShortOptStations: Char = '4';
26+
cLongOptStations = '400stations';
2527
{$IFNDEF FPC}
26-
cShortOptions: array of char = ['h', 'v', 'i', 'o', 'n'];
28+
cShortOptions: array of char = ['h', 'v', 'i', 'o', 'n', '4'];
2729
cLongOptions: array of string = ['help', 'version', 'input-file', 'output-file',
28-
'line-count'];
30+
'line-count', '400stations'];
2931
{$ENDIF}
3032

3133
resourcestring
@@ -44,7 +46,8 @@ interface
4446
var
4547
inputFilename: String = '';
4648
outputFilename: String = '';
47-
lineCount: Integer = 0;
49+
lineCount: Integer = 0;
50+
only400Stations: Boolean = False;
4851

4952
procedure WriteHelp;
5053

@@ -64,6 +67,7 @@ procedure WriteHelp;
6467
WriteLn(' -i|--input-file <filename> The file containing the Weather Stations');
6568
WriteLn(' -o|--output-file <filename> The file that will contain the generated lines');
6669
WriteLn(' -n|--line-count <number> The amount of lines to be generated ( Can use 1_000_000_000 )');
70+
WriteLn(' -4|--400stations Only 400 weather stations in output file');
6771
end;
6872

6973
end.

generator/Delphi/src/generator.dpr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ begin
4242
WriteLn(Format(rsLineCount, [Double(lineCount)]));
4343
WriteLn;
4444

45-
FGenerator := TGenerator.Create(inputFilename, outputFilename, lineCount);
45+
FGenerator := TGenerator.Create(inputFilename, outputFilename, lineCount, only400Stations);
4646
try
4747
try
4848
FGenerator.generate;
@@ -146,8 +146,10 @@ begin
146146

147147
if (Length(FParams[I]) = 1) or (FParams[I][2] = '=') then
148148
ParamOK := CheckShortParams(FParams[I][1])
149+
else if Pos('=', FParams[I]) > 0 then
150+
ParamOK := CheckLongParams(Copy(FParams[I], 1, Pos('=', FParams[I]) - 1))
149151
else
150-
ParamOK := CheckLongParams(Copy(FParams[I], 1, Pos('=', FParams[I]) - 1));
152+
ParamOK := CheckLongParams(FParams[I]);
151153

152154
// if we found a bad parameter, don't need to check the rest of them
153155
if not ParamOK then
@@ -238,6 +240,11 @@ begin
238240
inc(valid);
239241
end;
240242

243+
only400Stations := (FParams.IndexOf(cShortOptStations) >= 0) or (FParams.IndexOf(cLongOptStations) >= 0);
244+
245+
writeln(only400Stations);
246+
writeln(fparams.Text);
247+
241248
// check if everything was provided
242249
Result := (valid = 3) and (invalid = 0);
243250
end;

generator/Lazarus/src/generator.lpr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ procedure TOneBRCGenerator.DoRun;
3737
tmpLineCount: String;
3838
begin
3939
// quick check parameters
40-
ErrorMsg:= CheckOptions(Format('%s%s%s:%s:%s:',[
40+
ErrorMsg:= CheckOptions(Format('%s%s%s:%s:%s:%s',[
4141
cShortOptHelp,
4242
cShortOptVersion,
4343
cShortOptInput,
4444
cShortOptOutput,
45-
cShortOptNumber
45+
cShortOptNumber,
46+
cShortOptStations
4647
]),
4748
[
4849
cLongOptHelp,
4950
cLongOptVersion,
5051
cLongOptInput+':',
5152
cLongOptOutput+':',
52-
cLongOptNumber+':'
53+
cLongOptNumber+':',
54+
cLongOptStations+':'
5355
]
5456
);
5557
if ErrorMsg<>'' then
@@ -130,6 +132,8 @@ procedure TOneBRCGenerator.DoRun;
130132
Exit;
131133
end;
132134

135+
only400stations := HasOption(cShortOptStations, cLongOptStations);
136+
133137
inputFilename:= ExpandFileName(inputFilename);
134138
outputFilename:= ExpandFileName(outputFilename);
135139

@@ -138,7 +142,7 @@ procedure TOneBRCGenerator.DoRun;
138142
WriteLn(Format(rsLineCount, [ Double(lineCount) ]));
139143
WriteLn;
140144

141-
FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount);
145+
FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount, only400stations);
142146
try
143147
try
144148
FGenerator.Generate;

0 commit comments

Comments
 (0)