TAChart: Optimize reverse-order access to TCalculatedChartSource

git-svn-id: trunk@28802 -
This commit is contained in:
ask 2010-12-22 11:06:10 +00:00
parent ca330915bc
commit 7abae0cdec
3 changed files with 45 additions and 1 deletions

View File

@ -96,6 +96,7 @@ type
FCount: Cardinal;
FStart: Cardinal;
FSum: TChartDataItem;
function EndIndex: Cardinal; inline;
function GetCapacity: Cardinal; inline;
procedure Put(AIndex: Integer; const AItem: TChartDataItem);
procedure Remove(AIndex: Integer);
@ -104,7 +105,9 @@ type
procedure AddFirst(const AItem: TChartDataItem);
procedure AddLast(const AItem: TChartDataItem);
procedure Clear; inline;
function GetPLast: PChartDataItem;
procedure GetSum(var AItem: TChartDataItem);
procedure RemoveLast;
property Capacity: Cardinal read GetCapacity write SetCapacity;
end;
@ -151,8 +154,8 @@ begin
FStart += 1;
end
else begin
Put((FStart + FCount) mod Capacity, AItem);
FCount += 1;
Put(EndIndex, AItem);
end;
end;
@ -163,11 +166,21 @@ begin
FSum.YList := nil;
end;
function TChartSourceBuffer.EndIndex: Cardinal;
begin
Result := (FStart + Cardinal(FCount - 1)) mod Capacity;
end;
function TChartSourceBuffer.GetCapacity: Cardinal;
begin
Result := Length(FBuf);
end;
function TChartSourceBuffer.GetPLast: PChartDataItem;
begin
Result := @FBuf[EndIndex];
end;
procedure TChartSourceBuffer.GetSum(var AItem: TChartDataItem);
begin
if FCount = 0 then
@ -203,6 +216,14 @@ begin
end;
end;
procedure TChartSourceBuffer.RemoveLast;
begin
if FCount = 0 then
raise EBufferError.Create('Empty');
Remove(EndIndex);
FCount -= 1;
end;
procedure TChartSourceBuffer.SetCapacity(AValue: Cardinal);
begin
if AValue = Capacity then exit;

View File

@ -1006,6 +1006,16 @@ begin
ExtractItem(FItem, AIndex);
FHistory.AddLast(FItem);
end
else if FIndex = AIndex + 1 then begin
i := AIndex - AccumulationRange + 1;
if i < 0 then
FHistory.RemoveLast
else begin
ExtractItem(FItem, i);
FHistory.AddFirst(FItem);
end;
FItem := FHistory.GetPLast^;
end
else begin
FHistory.Clear;
for i := Max(AIndex - AccumulationRange + 1, 0) to AIndex do begin

View File

@ -70,6 +70,9 @@ uses
{ TCalculatedSourceTest }
procedure TCalculatedSourceTest.Accumulate;
var
i, j: Integer;
rng: TMWCRandomGenerator;
begin
FSource.AccumulationMethod := camSum;
FSource.AccumulationRange := 2;
@ -84,6 +87,16 @@ begin
AssertEquals(1, FSource[0]^.X);
AssertEquals(102, FSource[0]^.Y);
AssertEquals((102 + 202) / 2, FSource[1]^.Y);
AssertEquals(102, FSource[0]^.Y);
rng := TMWCRandomGenerator.Create;
rng.Seed := 89237634;
FSource.AccumulationRange := 5;
for i := 1 to 100 do begin
j := rng.GetInRange(5, FSource.Count - 1);
AssertEquals(IntToStr(j), (j - 1) * 100 + 2, FSource[j]^.Y);
end;
rng.Free;
end;
procedure TCalculatedSourceTest.Percentage;