@@ -35,12 +35,13 @@ function RoundExDouble(const ATemp: Double): Double; inline;
3535 TMyDictionary = class
3636 private
3737 FHashes: TKeys;
38- FData : TValues;
38+ FValues: TValues;
39+ FRecords: array of TStationData;
3940 procedure InternalFind (const aKey: Cardinal; out aFound: Boolean; out aIndex: Integer);
4041 public
4142 constructor Create;
4243 property Keys: TKeys read FHashes;
43- property Values: TValues read FData ;
44+ property Values: TValues read FValues ;
4445 function TryGetValue (const aKey: Cardinal; out aValue: PStationData): Boolean; inline;
4546 procedure Add (const aKey: Cardinal; const aValue: PStationData); inline;
4647 end ;
@@ -94,9 +95,6 @@ TBRCThread = class (TThread)
9495
9596implementation
9697
97- uses
98- CRC;
99-
10098
10199const
102100 c0ascii: ShortInt = 48 ;
@@ -169,21 +167,24 @@ procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean;
169167end ;
170168
171169constructor TMyDictionary.Create;
170+ var
171+ I: Integer;
172172begin
173173 SetLength (FHashes, cDictSize);
174- SetLength (FData, cDictSize);
174+ SetLength (FValues, cDictSize);
175+ SetLength (FRecords, cDictSize);
176+
177+ for I := 0 to cDictSize - 1 do begin
178+ FValues[I] := @FRecords[I];
179+ end ;
175180end ;
176181
177182function TMyDictionary.TryGetValue (const aKey: Cardinal; out aValue: PStationData): Boolean;
178183var
179184 vIdx: Integer;
180185begin
181186 InternalFind (aKey, Result, vIdx);
182-
183- if Result then
184- aValue := FData[vIdx]
185- else
186- aValue := nil ;
187+ aValue := FValues[vIdx];
187188end ;
188189
189190procedure TMyDictionary.Add (const aKey: Cardinal; const aValue: PStationData);
@@ -194,7 +195,7 @@ procedure TMyDictionary.Add(const aKey: Cardinal; const aValue: PStationData);
194195 InternalFind (aKey, vFound, vIdx);
195196 if not vFound then begin
196197 FHashes[vIdx] := aKey;
197- FData [vIdx] := aValue;
198+ FValues [vIdx] := aValue;
198199 end
199200 else
200201 raise Exception.Create (' TMyDict: cannot add, duplicate key' );
@@ -351,7 +352,6 @@ procedure TOneBRC.ProcessData (aThreadNb: UInt16; aStartIdx: Int64; aEndIdx: Int
351352 SetString(vStation, pAnsiChar(@FData[vLineStart]), vLenStationName);
352353
353354 // pre-allocated array of records instead of on-the-go allocation
354- new(vData);
355355 vData^.Min := vTemp;
356356 vData^.Max := vTemp;
357357 vData^.Sum := vTemp;
@@ -420,8 +420,8 @@ procedure TOneBRC.GenerateOutput;
420420 try
421421 vStations.BeginUpdate;
422422 for vData in FStationsDicts[0 ].Values do begin
423- // nil value means empty slot: skip
424- if vData <> nil then
423+ // count = 0 means empty slot: skip
424+ if vData^.Count <> 0 then
425425 vStations.Add(vData^.Name );
426426 end ;
427427 vStations.EndUpdate;
0 commit comments