|
| 1 | +program dtoepfl; |
| 2 | + |
| 3 | +{$APPTYPE CONSOLE} |
| 4 | +{$R *.res} |
| 5 | + |
| 6 | +uses |
| 7 | + System.SysUtils, |
| 8 | + System.StrUtils, |
| 9 | + System.IOUtils, |
| 10 | + System.Classes, |
| 11 | + System.TimeSpan, |
| 12 | + System.Generics.Collections, |
| 13 | + System.Diagnostics, |
| 14 | + generate.console in '..\..\..\generator\Common\generate.console.pas', |
| 15 | + Baseline.Common in '..\..\..\Baseline\Common\Baseline.Common.pas'; |
| 16 | + |
| 17 | +type TAlgorithm = (v1, v2, Count); |
| 18 | +var algorithm: TAlgorithm; |
| 19 | + FormatSettings: TFormatSettings; |
| 20 | +const paramPrefix = '--'; |
| 21 | + paramPrefixShort = '-'; |
| 22 | + |
| 23 | +{$REGION 'v1'} |
| 24 | + |
| 25 | +type TStationEntry = record |
| 26 | + min, max, sum: Double; |
| 27 | + count: Integer; |
| 28 | +end; |
| 29 | + |
| 30 | +/// <summary>Simple algorithm, single-thread</summary> |
| 31 | +procedure runV1; |
| 32 | +var FileStream: TFileStream; |
| 33 | + StreamReader: TStreamReader; |
| 34 | + CityTemperatures: TDictionary<String, TStationEntry>; //Station, min, avrg, max |
| 35 | + Line: String; |
| 36 | + Parts: TArray<String>; |
| 37 | + entry: TStationEntry; |
| 38 | + value: Double; |
| 39 | +begin |
| 40 | + CityTemperatures := TDictionary<String, TStationEntry>.Create; |
| 41 | + |
| 42 | + try |
| 43 | + FileStream := TFileStream.Create(inputFilename, fmOpenRead or fmShareDenyWrite); |
| 44 | + try |
| 45 | + StreamReader := TStreamReader.Create(FileStream, TEncoding.UTF8); |
| 46 | + |
| 47 | + // Read the first three bytes to check for UTF-8 BOM |
| 48 | +// var NumRead := FileStream.Read(BOM, Length(BOM)); |
| 49 | + // If the file starts with the UTF-8 BOM, continue as is, otherwise reset the stream position. |
| 50 | +// if (NumRead <> Length(BOM)) or (BOM[0] <> $EF) or (BOM[1] <> $BB) or (BOM[2] <> $BF) then |
| 51 | +// begin |
| 52 | +// FileStream.Position := 0; // Reset the position if no BOM or not a UTF-8 BOM |
| 53 | +// end; |
| 54 | + |
| 55 | + try |
| 56 | + while not StreamReader.EndOfStream do |
| 57 | + begin |
| 58 | + Line := StreamReader.ReadLine; |
| 59 | + Parts := SplitString(Line, ';'); |
| 60 | + if (TryStrToFloat(Parts[1], value, FormatSettings)) then |
| 61 | + begin |
| 62 | + if (CityTemperatures.ContainsKey(Parts[0])) then |
| 63 | + begin |
| 64 | + entry := CityTemperatures[Parts[0]]; |
| 65 | + |
| 66 | + if (value < entry.min) then //min |
| 67 | + entry.min := value; |
| 68 | + |
| 69 | + entry.sum := (entry.sum + value); //average |
| 70 | + entry.count := entry.count + 1; |
| 71 | + |
| 72 | + if (value > entry.max) then //max |
| 73 | + entry.max := value; |
| 74 | + |
| 75 | + CityTemperatures[Parts[0]] := entry; |
| 76 | + end |
| 77 | + else |
| 78 | + begin |
| 79 | + entry.min := value; |
| 80 | + entry.count := 1; |
| 81 | + entry.sum := value; |
| 82 | + entry.max := value; |
| 83 | + CityTemperatures.Add(Parts[0], entry); |
| 84 | + end; |
| 85 | + end; |
| 86 | + end; |
| 87 | + |
| 88 | + |
| 89 | + //Sorted output |
| 90 | + var SortedKeys := TList<String>.Create(CityTemperatures.Keys); |
| 91 | + SortedKeys.Sort; |
| 92 | + var i := 0; |
| 93 | + {$IFDEF DEBUG} |
| 94 | + var vStream := TStringStream.Create('', TEncoding.UTF8); |
| 95 | + {$ENDIF} |
| 96 | + try |
| 97 | + for var Key in SortedKeys do |
| 98 | + begin |
| 99 | + |
| 100 | + // Windows will mess up the characters when outputting to STDOUT. |
| 101 | + // for debug purposes, we'll output it to a file instead. |
| 102 | + //https://github.com/gcarreno/1brc-ObjectPascal/blob/main/baseline/Common/baseline.delphi.pas @https://github.com/georges-hatem |
| 103 | + {$IFDEF DEBUG} |
| 104 | + if (i = 0) then |
| 105 | + vStream.WriteString('{'); |
| 106 | + |
| 107 | + vStream.WriteString(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min, RoundExDouble(CityTemperatures[key].sum / CityTemperatures[key].count), CityTemperatures[key].max], FormatSettings)); |
| 108 | + |
| 109 | + if (i = CityTemperatures.Count-1) then |
| 110 | + vStream.WriteString('}' + #10) |
| 111 | + else |
| 112 | + vStream.WriteString(', '); |
| 113 | + |
| 114 | + {$ELSE} |
| 115 | + if (i = 0) then |
| 116 | + Write('{'); |
| 117 | + |
| 118 | + Write(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min, RoundExDouble(CityTemperatures[key].sum / CityTemperatures[key].count), CityTemperatures[key].max], FormatSettings)); |
| 119 | + |
| 120 | + if (i = CityTemperatures.Count-1) then |
| 121 | + Write('}' + #10) |
| 122 | + else |
| 123 | + Write(', '); |
| 124 | + {$ENDIF} |
| 125 | + |
| 126 | + Inc(i); |
| 127 | + end; |
| 128 | + |
| 129 | + {$IFDEF DEBUG} |
| 130 | + vStream.SaveToFile('output.txt'); |
| 131 | + {$ENDIF} |
| 132 | + |
| 133 | + finally |
| 134 | + {$IFDEF DEBUG} |
| 135 | + vStream.Free; |
| 136 | + {$ENDIF} |
| 137 | + end; |
| 138 | + |
| 139 | + finally |
| 140 | + StreamReader.Free; |
| 141 | + end; |
| 142 | + finally |
| 143 | + FileStream.Free; |
| 144 | + end; |
| 145 | + finally |
| 146 | + CityTemperatures.Free; |
| 147 | + end; |
| 148 | +end; |
| 149 | +{$ENDREGION} |
| 150 | + |
| 151 | +{$REGION 'v2'} |
| 152 | +procedure runV2; |
| 153 | +begin |
| 154 | + raise Exception.Create('v2 not implemented yet!'); |
| 155 | +end; |
| 156 | +{$ENDREGION} |
| 157 | + |
| 158 | +function TryGetParamValue(index: Int8): String; |
| 159 | +begin |
| 160 | + try |
| 161 | + Result := ParamStr(index + 1).ToLower |
| 162 | + except on e:exception do |
| 163 | + raise Exception.Create('Invalid parameter for ' + ParamStr(index) + sLineBreak + e.Message); |
| 164 | + end; |
| 165 | +end; |
| 166 | + |
| 167 | +begin |
| 168 | + try |
| 169 | + FormatSettings := TFormatSettings.Create; |
| 170 | + FormatSettings.DecimalSeparator := '.'; |
| 171 | + |
| 172 | + {$REGION 'Application Params'} |
| 173 | + var Arg: String; |
| 174 | + for var i := 1 to ParamCount do |
| 175 | + begin |
| 176 | + Arg := ParamStr(i).ToLower; |
| 177 | + |
| 178 | + if (Arg = paramPrefixShort + cShortOptions[2]) or |
| 179 | + (Arg = paramPrefix + cLongOptions[2]) then //input-file |
| 180 | + inputFilename := TryGetParamValue(i); |
| 181 | + |
| 182 | + if (Arg = paramPrefixShort + 'a') or |
| 183 | + (Arg = paramPrefix + 'algorithm') then //algorithm |
| 184 | + begin |
| 185 | + const v = StringReplace(TryGetParamValue(i), 'v', '', [rfReplaceAll]); |
| 186 | + |
| 187 | + var version: Integer; |
| 188 | + if (TryStrToInt(v, version)) then |
| 189 | + begin |
| 190 | + |
| 191 | + if (Ord(TAlgorithm.Count) = version) then |
| 192 | + begin |
| 193 | + case version of |
| 194 | + 1: algorithm := TAlgorithm.v1; |
| 195 | + 2: algorithm := TAlgorithm.v2; |
| 196 | + end; |
| 197 | + end |
| 198 | + else |
| 199 | + raise Exception.Create('Invalid algorithm version!'); |
| 200 | + |
| 201 | + end; |
| 202 | + end; |
| 203 | + end; |
| 204 | + |
| 205 | + //Check if values are valid |
| 206 | + if (inputFilename.IsEmpty) then |
| 207 | + raise Exception.Create(Format(rsErrorMessage, [ rsMissingInputFlag ])); |
| 208 | + |
| 209 | + inputFilename := ExpandFileName(inputFilename); |
| 210 | + {$ENDREGION} |
| 211 | + |
| 212 | + {$IFDEF DEBUG} |
| 213 | + WriteLn('The One Billion Row Challenge in Object Pascal'); |
| 214 | + WriteLn('Source: https://github.com/dtpfl, https://github.com/gcarreno/1brc-ObjectPascal'); |
| 215 | + WriteLn; |
| 216 | + WriteLn(Format(rsInputFile, [ inputFilename ])); |
| 217 | + WriteLn(Format('Algorithm: v%d', [Ord(algorithm)+1])); |
| 218 | + WriteLn; |
| 219 | + |
| 220 | + var Stopwatch := TStopwatch.StartNew; |
| 221 | + {$ENDIF} |
| 222 | + |
| 223 | + if (algorithm = TAlgorithm.v1) then |
| 224 | + runV1 |
| 225 | + else if (algorithm = TAlgorithm.v2) then |
| 226 | + runv2; |
| 227 | + |
| 228 | + {$IFDEF DEBUG} |
| 229 | + var Elapsed: TTimeSpan := Stopwatch.Elapsed; |
| 230 | + Writeln; |
| 231 | + Writeln('Elapsed time: ' + FormatFloat('0.000', Elapsed.TotalSeconds) + ' seconds'); |
| 232 | + {$ENDIF} |
| 233 | + except |
| 234 | + on E: Exception do |
| 235 | + Writeln(E.ClassName, ': ', E.Message); |
| 236 | + end; |
| 237 | +end. |
0 commit comments