DataSeries.Copy

This commit is contained in:
Michael Schimmel
2025-06-24 12:42:28 +02:00
parent 6077d094f7
commit 3048c28fe3
4 changed files with 211 additions and 26 deletions
+18 -14
View File
@@ -23,6 +23,7 @@ type
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetTotalCount: Int64;
function Copy: IDataSeries<T>;
property Count: Int64 read GetCount;
// Accesses data points by their logical index.
// Index 0 is the newest element, Index (Count - 1) is the oldest.
@@ -40,18 +41,15 @@ type
// Interface Helper for IDataSeries<T>.
// Provides a safe, value-type-like wrapper around the interface.
TDataSeries<T> = record
strict private
class var
FNull: IDataSeries<T>;
private
FDataSeries: IDataSeries<T>;
FMaxLookback: Int64;
class constructor CreateClass;
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetData(Idx: Int64): T;
function GetTime(Idx: Int64): TDateTime;
function GetMaxLookback: Int64;
class function GetNull: IDataSeries<T>; static;
function GetTotalCount: Int64;
public
constructor Create(ADataSeries: IDataSeries<T>);
@@ -64,13 +62,16 @@ type
class function CreateArray(MaxLookback: Int64): IDataArray<T>; static;
// Create a guaranteed immutable version of the given series.
function Copy: TDataSeries<T>;
// Searches for a data point by its timestamp.
// Returns the logical index of the matching item.
// If no exact match, returns the index of the item immediately preceding the timestamp.
// Returns -1 if the timestamp is before the oldest item in the series.
function IndexOf(TimeStamp: TDateTime): Int64;
class property Null: IDataSeries<T> read FNull;
class property Null: IDataSeries<T> read GetNull;
property Count: Int64 read GetCount;
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
property Data[Idx: Int64]: T read GetData;
@@ -100,19 +101,17 @@ begin
Data := AData;
end;
{ TDataSeries<T> Interface Helper }
class constructor TDataSeries<T>.CreateClass;
begin
FNull := TNullDataSeries<T>.Create;
end;
constructor TDataSeries<T>.Create(ADataSeries: IDataSeries<T>);
begin
if Assigned(ADataSeries) then
FDataSeries := ADataSeries
else
FDataSeries := FNull;
FDataSeries := Null;
end;
function TDataSeries<T>.Copy: TDataSeries<T>;
begin
Result := FDataSeries.Copy;
end;
class function TDataSeries<T>.CreateArray(MaxLookback: Int64): IDataArray<T>;
@@ -127,7 +126,7 @@ end;
class operator TDataSeries<T>.Initialize(out Dest: TDataSeries<T>);
begin
Dest.FDataSeries := FNull;
Dest.FDataSeries := Null;
end;
class operator TDataSeries<T>.Implicit(const A: TDataSeries<T>): IDataSeries<T>;
@@ -165,6 +164,11 @@ begin
Result := FMaxLookback;
end;
class function TDataSeries<T>.GetNull: IDataSeries<T>;
begin
Result := TNullDataSeries<T>.Null;
end;
function TDataSeries<T>.GetTotalCount: Int64;
begin
Result := FDataSeries.GetTotalCount;