|
| 1 | +program OneBRC; |
| 2 | + |
| 3 | +{ |
| 4 | + ==Credits== |
| 5 | +
|
| 6 | + 1. The FPC team, Lazarus team, fpcupdeluxe team, and other contributors. |
| 7 | + - For providing a usable programming language and a usable ecosystem. |
| 8 | + 2. Gustavo 'Gus' Carreno. |
| 9 | + - For making this happen. |
| 10 | + - Borrowed Gus' approach to use `TCustomApplication` and using `unit`s properly |
| 11 | + to make main code more readable. |
| 12 | + - Borrowed and modified Gus' `WriteHelp` from the `baseline.lpr`. |
| 13 | + 3. Székely Balázs. |
| 14 | + - Now I know what `Single` data type is! |
| 15 | + - I borrowed the custom `TStringList` comparer from the `baseline` program. |
| 16 | + 4. Shraddha Agrawal - https://www.bytesizego.com/blog/one-billion-row-challenge-go. |
| 17 | + - The advice for not storing measurements for each station in a data structure. |
| 18 | + 5. Arman Hajisafi - https://arman-hs.github.io |
| 19 | + - Encouragements and inspirations. |
| 20 | + } |
| 21 | + |
| 22 | +{$mode objfpc}{$H+}{$J-}{$modeSwitch advancedRecords} |
| 23 | +{$codepage utf8} |
| 24 | +//{$DEFINE DEBUG} |
| 25 | + |
| 26 | +uses |
| 27 | + {$IFDEF UNIX} |
| 28 | + cthreads, |
| 29 | + {$ENDIF} |
| 30 | + Classes, |
| 31 | + SysUtils, |
| 32 | + CustApp, |
| 33 | + WeatherStation; |
| 34 | + |
| 35 | +const |
| 36 | + version = '1.0'; |
| 37 | + |
| 38 | +type |
| 39 | + |
| 40 | + { TOneBRC } |
| 41 | + |
| 42 | + TOneBRC = class(TCustomApplication) |
| 43 | + protected |
| 44 | + procedure DoRun; override; |
| 45 | + public |
| 46 | + constructor Create(TheOwner: TComponent); override; |
| 47 | + destructor Destroy; override; |
| 48 | + procedure WriteHelp; virtual; |
| 49 | + end; |
| 50 | + |
| 51 | + { TOneBRC } |
| 52 | + |
| 53 | + procedure TOneBRC.DoRun; |
| 54 | + var |
| 55 | + ErrorMsg: string; |
| 56 | + filename: string = ''; |
| 57 | + begin |
| 58 | + // quick check parameters |
| 59 | + ErrorMsg := CheckOptions('hvi:', ['help', 'version', 'input:']); |
| 60 | + if ErrorMsg <> '' then |
| 61 | + begin |
| 62 | + // Commented out the default ShowException as the generated text is not user friendly. |
| 63 | + // ShowException(Exception.Create(ErrorMsg)); |
| 64 | + WriteLn('Error: ', ErrorMsg); |
| 65 | + WriteHelp; |
| 66 | + Terminate; |
| 67 | + Exit; |
| 68 | + end; |
| 69 | + |
| 70 | + // Parse h |
| 71 | + if HasOption('h', 'help') then |
| 72 | + begin |
| 73 | + WriteHelp; |
| 74 | + Terminate; |
| 75 | + Exit; |
| 76 | + end; |
| 77 | + |
| 78 | + // Parse v |
| 79 | + if HasOption('v', 'version') then |
| 80 | + begin |
| 81 | + WriteLn('OneBRC version ', version); |
| 82 | + Terminate; |
| 83 | + Exit; |
| 84 | + end; |
| 85 | + |
| 86 | + // Parse i |
| 87 | + if HasOption('i', 'input') then |
| 88 | + begin |
| 89 | + filename := GetOptionValue('i', 'input'); |
| 90 | + end; |
| 91 | + |
| 92 | + if (length(filename) < 4) then |
| 93 | + begin |
| 94 | + WriteLn('Input file seems invalid.'); |
| 95 | + WriteHelp; |
| 96 | + Terminate; |
| 97 | + Exit; |
| 98 | + end; |
| 99 | + |
| 100 | + // Start the main algorithm |
| 101 | + WeatherStation.ProcessTempMeasurements(filename); |
| 102 | + |
| 103 | + // stop program loop |
| 104 | + Terminate; |
| 105 | + end; |
| 106 | + |
| 107 | + constructor TOneBRC.Create(TheOwner: TComponent); |
| 108 | + begin |
| 109 | + inherited Create(TheOwner); |
| 110 | + StopOnException := True; |
| 111 | + end; |
| 112 | + |
| 113 | + destructor TOneBRC.Destroy; |
| 114 | + begin |
| 115 | + inherited Destroy; |
| 116 | + end; |
| 117 | + |
| 118 | + procedure TOneBRC.WriteHelp; |
| 119 | + begin |
| 120 | + WriteLn('OneBRC -- An entry to the One Billion Row Challenge for Object Pascal'); |
| 121 | + WriteLn; |
| 122 | + WriteLn('Usage: OneBRC [-h] [-v] [-i input_file]'); |
| 123 | + WriteLn; |
| 124 | + WriteLn(' -h | --help Show this help screen'); |
| 125 | + WriteLn(' -v | --version Show the version number'); |
| 126 | + WriteLn(' -i | --input-file <filename> Input text file to process.'); |
| 127 | + WriteLn(' Each row is one temperature measurement in the format <string: station name>;<double: measurement>'); |
| 128 | + end; |
| 129 | + |
| 130 | +var |
| 131 | + Application: TOneBRC; |
| 132 | +begin |
| 133 | + Application := TOneBRC.Create(nil); |
| 134 | + Application.Title := 'OneBRC'; |
| 135 | + Application.Run; |
| 136 | + Application.Free; |
| 137 | +end. |
0 commit comments