Skip to content
Merged
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
30 changes: 15 additions & 15 deletions entries/ghatem-fpc/src/onebrc.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ function RoundExDouble(const ATemp: Double): Double; inline;
TMyDictionary = class
private
FHashes: TKeys;
FData : TValues;
FValues: TValues;
FRecords: array of TStationData;
procedure InternalFind(const aKey: Cardinal; out aFound: Boolean; out aIndex: Integer);
public
constructor Create;
property Keys: TKeys read FHashes;
property Values: TValues read FData;
property Values: TValues read FValues;
function TryGetValue (const aKey: Cardinal; out aValue: PStationData): Boolean; inline;
procedure Add (const aKey: Cardinal; const aValue: PStationData); inline;
end;
Expand Down Expand Up @@ -94,9 +95,6 @@ TBRCThread = class (TThread)

implementation

uses
CRC;


const
c0ascii: ShortInt = 48;
Expand Down Expand Up @@ -169,21 +167,24 @@ procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean;
end;

constructor TMyDictionary.Create;
var
I: Integer;
begin
SetLength (FHashes, cDictSize);
SetLength (FData, cDictSize);
SetLength (FValues, cDictSize);
SetLength (FRecords, cDictSize);

for I := 0 to cDictSize - 1 do begin
FValues[I] := @FRecords[I];
end;
end;

function TMyDictionary.TryGetValue(const aKey: Cardinal; out aValue: PStationData): Boolean;
var
vIdx: Integer;
begin
InternalFind (aKey, Result, vIdx);

if Result then
aValue := FData[vIdx]
else
aValue := nil;
aValue := FValues[vIdx];
end;

procedure TMyDictionary.Add(const aKey: Cardinal; const aValue: PStationData);
Expand All @@ -194,7 +195,7 @@ procedure TMyDictionary.Add(const aKey: Cardinal; const aValue: PStationData);
InternalFind (aKey, vFound, vIdx);
if not vFound then begin
FHashes[vIdx] := aKey;
FData[vIdx] := aValue;
FValues[vIdx] := aValue;
end
else
raise Exception.Create ('TMyDict: cannot add, duplicate key');
Expand Down Expand Up @@ -351,7 +352,6 @@ procedure TOneBRC.ProcessData (aThreadNb: UInt16; aStartIdx: Int64; aEndIdx: Int
SetString(vStation, pAnsiChar(@FData[vLineStart]), vLenStationName);

// pre-allocated array of records instead of on-the-go allocation
new(vData);
vData^.Min := vTemp;
vData^.Max := vTemp;
vData^.Sum := vTemp;
Expand Down Expand Up @@ -420,8 +420,8 @@ procedure TOneBRC.GenerateOutput;
try
vStations.BeginUpdate;
for vData in FStationsDicts[0].Values do begin
// nil value means empty slot: skip
if vData <> nil then
// count = 0 means empty slot: skip
if vData^.Count <> 0 then
vStations.Add(vData^.Name);
end;
vStations.EndUpdate;
Expand Down