mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-17 22:59:15 +02:00
TAChart: Optimize reverse-order access to TCalculatedChartSource
git-svn-id: trunk@28802 -
This commit is contained in:
parent
ca330915bc
commit
7abae0cdec
@ -96,6 +96,7 @@ type
|
|||||||
FCount: Cardinal;
|
FCount: Cardinal;
|
||||||
FStart: Cardinal;
|
FStart: Cardinal;
|
||||||
FSum: TChartDataItem;
|
FSum: TChartDataItem;
|
||||||
|
function EndIndex: Cardinal; inline;
|
||||||
function GetCapacity: Cardinal; inline;
|
function GetCapacity: Cardinal; inline;
|
||||||
procedure Put(AIndex: Integer; const AItem: TChartDataItem);
|
procedure Put(AIndex: Integer; const AItem: TChartDataItem);
|
||||||
procedure Remove(AIndex: Integer);
|
procedure Remove(AIndex: Integer);
|
||||||
@ -104,7 +105,9 @@ type
|
|||||||
procedure AddFirst(const AItem: TChartDataItem);
|
procedure AddFirst(const AItem: TChartDataItem);
|
||||||
procedure AddLast(const AItem: TChartDataItem);
|
procedure AddLast(const AItem: TChartDataItem);
|
||||||
procedure Clear; inline;
|
procedure Clear; inline;
|
||||||
|
function GetPLast: PChartDataItem;
|
||||||
procedure GetSum(var AItem: TChartDataItem);
|
procedure GetSum(var AItem: TChartDataItem);
|
||||||
|
procedure RemoveLast;
|
||||||
property Capacity: Cardinal read GetCapacity write SetCapacity;
|
property Capacity: Cardinal read GetCapacity write SetCapacity;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -151,8 +154,8 @@ begin
|
|||||||
FStart += 1;
|
FStart += 1;
|
||||||
end
|
end
|
||||||
else begin
|
else begin
|
||||||
Put((FStart + FCount) mod Capacity, AItem);
|
|
||||||
FCount += 1;
|
FCount += 1;
|
||||||
|
Put(EndIndex, AItem);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -163,11 +166,21 @@ begin
|
|||||||
FSum.YList := nil;
|
FSum.YList := nil;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TChartSourceBuffer.EndIndex: Cardinal;
|
||||||
|
begin
|
||||||
|
Result := (FStart + Cardinal(FCount - 1)) mod Capacity;
|
||||||
|
end;
|
||||||
|
|
||||||
function TChartSourceBuffer.GetCapacity: Cardinal;
|
function TChartSourceBuffer.GetCapacity: Cardinal;
|
||||||
begin
|
begin
|
||||||
Result := Length(FBuf);
|
Result := Length(FBuf);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TChartSourceBuffer.GetPLast: PChartDataItem;
|
||||||
|
begin
|
||||||
|
Result := @FBuf[EndIndex];
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TChartSourceBuffer.GetSum(var AItem: TChartDataItem);
|
procedure TChartSourceBuffer.GetSum(var AItem: TChartDataItem);
|
||||||
begin
|
begin
|
||||||
if FCount = 0 then
|
if FCount = 0 then
|
||||||
@ -203,6 +216,14 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TChartSourceBuffer.RemoveLast;
|
||||||
|
begin
|
||||||
|
if FCount = 0 then
|
||||||
|
raise EBufferError.Create('Empty');
|
||||||
|
Remove(EndIndex);
|
||||||
|
FCount -= 1;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TChartSourceBuffer.SetCapacity(AValue: Cardinal);
|
procedure TChartSourceBuffer.SetCapacity(AValue: Cardinal);
|
||||||
begin
|
begin
|
||||||
if AValue = Capacity then exit;
|
if AValue = Capacity then exit;
|
||||||
|
@ -1006,6 +1006,16 @@ begin
|
|||||||
ExtractItem(FItem, AIndex);
|
ExtractItem(FItem, AIndex);
|
||||||
FHistory.AddLast(FItem);
|
FHistory.AddLast(FItem);
|
||||||
end
|
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
|
else begin
|
||||||
FHistory.Clear;
|
FHistory.Clear;
|
||||||
for i := Max(AIndex - AccumulationRange + 1, 0) to AIndex do begin
|
for i := Max(AIndex - AccumulationRange + 1, 0) to AIndex do begin
|
||||||
|
@ -70,6 +70,9 @@ uses
|
|||||||
{ TCalculatedSourceTest }
|
{ TCalculatedSourceTest }
|
||||||
|
|
||||||
procedure TCalculatedSourceTest.Accumulate;
|
procedure TCalculatedSourceTest.Accumulate;
|
||||||
|
var
|
||||||
|
i, j: Integer;
|
||||||
|
rng: TMWCRandomGenerator;
|
||||||
begin
|
begin
|
||||||
FSource.AccumulationMethod := camSum;
|
FSource.AccumulationMethod := camSum;
|
||||||
FSource.AccumulationRange := 2;
|
FSource.AccumulationRange := 2;
|
||||||
@ -84,6 +87,16 @@ begin
|
|||||||
AssertEquals(1, FSource[0]^.X);
|
AssertEquals(1, FSource[0]^.X);
|
||||||
AssertEquals(102, FSource[0]^.Y);
|
AssertEquals(102, FSource[0]^.Y);
|
||||||
AssertEquals((102 + 202) / 2, FSource[1]^.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;
|
end;
|
||||||
|
|
||||||
procedure TCalculatedSourceTest.Percentage;
|
procedure TCalculatedSourceTest.Percentage;
|
||||||
|
Loading…
Reference in New Issue
Block a user