Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions generator/Common/generate.common.pas
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface
{ TGenerator }
TGenerator = class(TObject)
private
FOnly400Stations: Boolean;
rndState: Array [0..1] of Cardinal;
FInputFile: String;
FOutPutFile: String;
Expand All @@ -27,7 +28,7 @@ TGenerator = class(TObject)
AFileSize: Int64; ATimeElapsed: TDateTime): String;
function Rng1brc(Range: Integer): Integer;
public
constructor Create(AInputFile, AOutputFile: String; ALineCount: Int64);
constructor Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean = False);
destructor Destroy; override;

procedure Generate;
Expand Down Expand Up @@ -61,11 +62,12 @@ implementation

{ TGenerator }

constructor TGenerator.Create(AInputFile, AOutputFile: String; ALineCount: Int64);
constructor TGenerator.Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean);
begin
FInputFile := AInputFile;
FOutPutFile := AOutputFile;
FLineCount := ALineCount;
FOnly400Stations := AOnly400Stations;

FStationNames := TStringList.Create;
FStationNames.Capacity := stationsCapacity;
Expand Down Expand Up @@ -196,6 +198,13 @@ procedure TGenerator.Generate;
progressBatch := floor(FLineCount * (linesPercent / 100));
start := Now;

if FOnly400Stations then
begin
WriteLn('Only 400 weather stations in output file.');
while FStationNames.Count > 400 do
FStationNames.Delete(Rng1brc(FStationNames.Count));
end;

// This is all paweld magic:
// From here
// based on code @domasz from lazarus forum, github: PascalVault
Expand Down
30 changes: 17 additions & 13 deletions generator/Common/generate.console.pas
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ interface
;

const
cShortOptHelp: Char = 'h';
cLongOptHelp = 'help';
cShortOptVersion: Char = 'v';
cLongOptVersion = 'version';
cShortOptInput: Char = 'i';
cLongOptInput = 'input-file';
cShortOptOutput: Char = 'o';
cLongOptOutput = 'output-file';
cShortOptNumber: Char = 'n';
cLongOptNumber = 'line-count';
cShortOptHelp: Char = 'h';
cLongOptHelp = 'help';
cShortOptVersion: Char = 'v';
cLongOptVersion = 'version';
cShortOptInput: Char = 'i';
cLongOptInput = 'input-file';
cShortOptOutput: Char = 'o';
cLongOptOutput = 'output-file';
cShortOptNumber: Char = 'n';
cLongOptNumber = 'line-count';
cShortOptStations: Char = '4';
cLongOptStations = '400stations';
{$IFNDEF FPC}
cShortOptions: array of char = ['h', 'v', 'i', 'o', 'n'];
cShortOptions: array of char = ['h', 'v', 'i', 'o', 'n', '4'];
cLongOptions: array of string = ['help', 'version', 'input-file', 'output-file',
'line-count'];
'line-count', '400stations'];
{$ENDIF}

resourcestring
Expand All @@ -44,7 +46,8 @@ interface
var
inputFilename: String = '';
outputFilename: String = '';
lineCount: Integer = 0;
lineCount: Integer = 0;
only400Stations: Boolean = False;

procedure WriteHelp;

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

end.
Expand Down
11 changes: 9 additions & 2 deletions generator/Delphi/src/generator.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ begin
WriteLn(Format(rsLineCount, [Double(lineCount)]));
WriteLn;

FGenerator := TGenerator.Create(inputFilename, outputFilename, lineCount);
FGenerator := TGenerator.Create(inputFilename, outputFilename, lineCount, only400Stations);
try
try
FGenerator.generate;
Expand Down Expand Up @@ -146,8 +146,10 @@ begin

if (Length(FParams[I]) = 1) or (FParams[I][2] = '=') then
ParamOK := CheckShortParams(FParams[I][1])
else if Pos('=', FParams[I]) > 0 then
ParamOK := CheckLongParams(Copy(FParams[I], 1, Pos('=', FParams[I]) - 1))
else
ParamOK := CheckLongParams(Copy(FParams[I], 1, Pos('=', FParams[I]) - 1));
ParamOK := CheckLongParams(FParams[I]);

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

only400Stations := (FParams.IndexOf(cShortOptStations) >= 0) or (FParams.IndexOf(cLongOptStations) >= 0);

writeln(only400Stations);
writeln(fparams.Text);

// check if everything was provided
Result := (valid = 3) and (invalid = 0);
end;
Expand Down
12 changes: 8 additions & 4 deletions generator/Lazarus/src/generator.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ procedure TOneBRCGenerator.DoRun;
tmpLineCount: String;
begin
// quick check parameters
ErrorMsg:= CheckOptions(Format('%s%s%s:%s:%s:',[
ErrorMsg:= CheckOptions(Format('%s%s%s:%s:%s:%s',[
cShortOptHelp,
cShortOptVersion,
cShortOptInput,
cShortOptOutput,
cShortOptNumber
cShortOptNumber,
cShortOptStations
]),
[
cLongOptHelp,
cLongOptVersion,
cLongOptInput+':',
cLongOptOutput+':',
cLongOptNumber+':'
cLongOptNumber+':',
cLongOptStations+':'
]
);
if ErrorMsg<>'' then
Expand Down Expand Up @@ -130,6 +132,8 @@ procedure TOneBRCGenerator.DoRun;
Exit;
end;

only400stations := HasOption(cShortOptStations, cLongOptStations);

inputFilename:= ExpandFileName(inputFilename);
outputFilename:= ExpandFileName(outputFilename);

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

FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount);
FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount, only400stations);
try
try
FGenerator.Generate;
Expand Down