This commit is contained in:
Michael Schimmel
2025-10-30 20:27:44 +01:00
parent 798aa08f02
commit 0526ec8a24
17 changed files with 193 additions and 137 deletions
+16 -54
View File
@@ -5,7 +5,8 @@ interface
uses
System.SysUtils,
Myc.Data.Decimal,
Myc.Data.Series;
Myc.Data.Series,
Myc.Data.Keyword;
{$SCOPEDENUMS ON}
@@ -91,20 +92,8 @@ type
TScalarTuple = TArray<TScalar>;
// A field definition for a scalar record.
TScalarRecordField = record
Name: String;
Kind: TScalar.TKind;
constructor Create(const AName: String; AKind: TScalar.TKind);
end;
TScalarRecordDefinition = record
private
FFields: TArray<TScalarRecordField>;
public
constructor Create(const AFields: TArray<TScalarRecordField>);
function IndexOf(const Name: String): Integer;
property Fields: TArray<TScalarRecordField> read FFields;
end;
TScalarRecordField = TKeywordMapping<TScalar.TKind>.TField;
TScalarRecordDefinition = TKeywordMapping<TScalar.TKind>;
// A record of scalar values, based on a definition.
TScalarRecord = record
@@ -112,10 +101,10 @@ type
constructor Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
function GetDef: TScalarRecordDefinition; inline;
function GetFields: TArray<TScalar.TValue>; inline;
function GetItems(const Name: String): TScalar;
function GetKeys(const Key: IKeyword): TScalar;
property Def: TScalarRecordDefinition read GetDef;
property Fields: TArray<TScalar.TValue> read GetFields;
property Items[const Name: String]: TScalar read GetItems; default;
property Keys[const Key: IKeyword]: TScalar read GetKeys; default;
strict private
FDef: TScalarRecordDefinition;
FFields: TArray<TScalar.TValue>;
@@ -158,7 +147,7 @@ type
function GetTotalCount: Int64;
{$endregion}
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
function CreateMemberSeries(const Field: String): ISeries;
function CreateMemberSeries(const Key: IKeyword): ISeries;
property Count: Int64 read GetCount;
property Def: TScalarRecordDefinition read GetDef;
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
@@ -192,7 +181,7 @@ type
public
constructor Create(const ADef: TScalarRecordDefinition);
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
function CreateMemberSeries(const Field: String): ISeries;
function CreateMemberSeries(const Key: IKeyword): ISeries;
property Count: Int64 read GetCount;
property Def: TScalarRecordDefinition read GetDef;
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
@@ -528,14 +517,6 @@ begin
Items := AItems;
end;
{ TScalarRecordField }
constructor TScalarRecordField.Create(const AName: String; AKind: TScalar.TKind);
begin
Name := AName;
Kind := AKind;
end;
{ TScalarRecord }
constructor TScalarRecord.Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
@@ -555,34 +536,15 @@ begin
Result := FFields;
end;
function TScalarRecord.GetItems(const Name: String): TScalar;
function TScalarRecord.GetKeys(const Key: IKeyword): TScalar;
var
idx: Integer;
begin
idx := FDef.IndexOf(Name);
idx := FDef.IndexOf(Key);
if idx < 0 then
raise EArgumentException.CreateFmt('Field "%s" not found in record definition.', [Name]);
raise EArgumentException.CreateFmt('Field ":%s" not found in record definition.', [Key.Name]);
Result.Create(FDef.Fields[idx].Kind, FFields[idx]);
end;
{ TScalarRecordDefinition }
constructor TScalarRecordDefinition.Create(const AFields: TArray<TScalarRecordField>);
begin
FFields := AFields;
end;
function TScalarRecordDefinition.IndexOf(const Name: String): Integer;
var
i: Integer;
begin
for i := 0 to High(FFields) do
begin
if (CompareText(FFields[i].Name, Name) = 0) then
exit(i);
end;
Result := -1;
Result.Create(FDef.Fields[idx].Value, FFields[idx]);
end;
{ TScalarRecordSeries }
@@ -600,11 +562,11 @@ begin
inc(FTotalCount);
end;
function TScalarRecordSeries.CreateMemberSeries(const Field: String): ISeries;
function TScalarRecordSeries.CreateMemberSeries(const Key: IKeyword): ISeries;
begin
var elem := FDef.IndexOf(Field);
var elem := FDef.IndexOf(Key);
if elem < 0 then
raise EArgumentException.CreateFmt('Field "%s" not found in record definition.', [Field]);
raise EArgumentException.CreateFmt('Field ":%s" not found in record definition.', [Key.Name]);
Result := TMemberSeries.Create(Self, elem);
end;
@@ -686,7 +648,7 @@ begin
inherited Create;
FRecordSeries := ARecordSeries;
FRecordSeries._AddRef;
FKind := FRecordSeries.FDef.FFields[AElementIdx].Kind;
FKind := FRecordSeries.FDef.Fields[AElementIdx].Value;
FOffset := sizeof(TScalar.TValue) * AElementIdx;
end;