1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| unit UnitFloatList;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TFloatList = class(TObject)
private
FList: TList;
FSum: Extended;
FMeanValue: Extended;
FIsUpdating:boolean;
FOnStatChanged: TNotifyEvent;
function GetItems(Item: integer): extended;
procedure SetItems(Item: integer; Value: extended);
function GetCount: integer;
procedure ComputeSum;
procedure ComputeMeanValue;
procedure UpDateStats;
procedure SetOnStatChanged(const Value: TNotifyEvent);
public
constructor Create;
destructor Destroy; override;
function Add(Value: extended): integer;
function First: extended;
function Last: extended;
procedure Clear;
procedure Delete(Item: integer);
procedure BeginUpdate;
procedure EndupDate;
property Items[Item: integer]: extended read GetItems
write SetItems; default;
property Count: integer read GetCount;
property Sum:Extended read FSum;
property MeanValue:Extended read FMeanValue;
property OnStatChanged:TNotifyEvent read FOnStatChanged write SetOnStatChanged;
end;
implementation
function TFloatList.GetItems(Item: integer): extended;
begin
Result:=PExtended(FList[Item])^;
end;
procedure TFloatList.SetItems(Item: integer; Value: extended);
begin
PExtended(FList[Item])^:=Value;
UpDateStats;
end;
function TFloatList.GetCount: integer;
begin
Result:=FList.Count;
end;
constructor TFloatList.Create;
begin
inherited Create;
FList:=TList.Create;
FIsUpdating:=false;
end;
destructor TFloatList.Destroy;
begin
Clear;
FList.Free;
inherited Destroy;
end;
function TFloatList.Add(Value: extended): integer;
var
FloatPtr: PExtended;
begin
GetMem(FloatPtr, SizeOf(extended));
FloatPtr^:=Value;
Result:=FList.Add(FloatPtr);
UpDateStats;
end;
function TFloatList.First: extended;
begin
Result:=PExtended(FList[0])^
end;
function TFloatList.Last: extended;
begin
Result:=PExtended(FList[FList.Count - 1])^;
end;
procedure TFloatList.Clear;
var
Index: integer;
begin
for Index:=0 to FList.Count - 1 do
FreeMem(FList.Items[Index], SizeOf(extended));
FList.Clear;
end;
procedure TFloatList.Delete(Item: integer);
begin
FreeMem(FList.Items[Item], SizeOf(extended));
FList.Delete(Item);
UpDateStats;
end;
procedure TFloatList.ComputeMeanValue;
begin
FMeanValue:=0;
if Count<>0
then FMeanValue:=FSum/Count;
end;
procedure TFloatList.ComputeSum;
var i:integer;
begin
FSum:=0;
for i:=0 to Count-1 do
FSum:=FSum+Items[i];
end;
procedure TFloatList.UpDateStats;
begin
if FIsUpdating then exit;
ComputeSum;
ComputeMeanValue;
if Assigned(FOnStatChanged) then FOnStatChanged(self);
end;
procedure TFloatList.BeginUpdate;
begin
FIsUpdating:=true;
end;
procedure TFloatList.EndupDate;
begin
FIsUpdating:=False;
UpDateStats;
end;
procedure TFloatList.SetOnStatChanged(const Value: TNotifyEvent);
begin
FOnStatChanged := Value;
end;
end. |
Partager