Keywords
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -769,7 +769,7 @@ var
|
||||
ast, callAst: IAstNode;
|
||||
resultValue: TDataValue;
|
||||
series: TScalarRecordSeries;
|
||||
recordDef: TScalarRecordDefinition;
|
||||
recordDef: IScalarRecordDefinition;
|
||||
i: Integer;
|
||||
scope: IExecutionScope;
|
||||
values: TArray<TScalar.TValue>;
|
||||
|
||||
@@ -155,10 +155,10 @@ type
|
||||
|
||||
TBoundRecordLiteralNode = class(TRecordLiteralNode)
|
||||
private
|
||||
FDefinition: TScalarRecordDefinition;
|
||||
FDefinition: IScalarRecordDefinition;
|
||||
public
|
||||
constructor Create(const AFields: TArray<TRecordFieldLiteral>; const ADef: TScalarRecordDefinition);
|
||||
property Definition: TScalarRecordDefinition read FDefinition;
|
||||
constructor Create(const AFields: TArray<TRecordFieldLiteral>; const ADef: IScalarRecordDefinition);
|
||||
property Definition: IScalarRecordDefinition read FDefinition;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -345,7 +345,7 @@ begin
|
||||
end;
|
||||
|
||||
{ TBoundRecordLiteralNode }
|
||||
constructor TBoundRecordLiteralNode.Create(const AFields: TArray<TRecordFieldLiteral>; const ADef: TScalarRecordDefinition);
|
||||
constructor TBoundRecordLiteralNode.Create(const AFields: TArray<TRecordFieldLiteral>; const ADef: IScalarRecordDefinition);
|
||||
begin
|
||||
inherited Create(AFields);
|
||||
FDefinition := ADef;
|
||||
@@ -960,8 +960,8 @@ function TAstBinder.VisitRecordLiteral(const Node: IRecordLiteralNode): TDataVal
|
||||
var
|
||||
i: Integer;
|
||||
boundFields: TArray<TRecordFieldLiteral>;
|
||||
defFields: TArray<TScalarRecordDefinition.TField>;
|
||||
def: TScalarRecordDefinition;
|
||||
defFields: TArray<TScalarRecordField>;
|
||||
def: IScalarRecordDefinition;
|
||||
staticType: IStaticType;
|
||||
boundNode: IRecordLiteralNode;
|
||||
valNode: IAstNode;
|
||||
@@ -997,7 +997,7 @@ begin
|
||||
defFields[i] := TScalarRecordField.Create(Node.Fields[i].Key.Value, scalarKind);
|
||||
end;
|
||||
|
||||
def := TScalarRecordDefinition.Create(defFields);
|
||||
def := TScalarRecordRegistry.Intern(defFields);
|
||||
staticType := TTypes.CreateRecord(def);
|
||||
boundNode := TBoundRecordLiteralNode.Create(boundFields, def);
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ end;
|
||||
function NativeCreateRecordSeries(const Args: TArray<TDataValue>): TDataValue;
|
||||
var
|
||||
jsonDef: string;
|
||||
recordDef: TScalarRecordDefinition;
|
||||
recordDef: IScalarRecordDefinition;
|
||||
series: IRecordSeries;
|
||||
begin
|
||||
if (Length(Args) <> 1) or (Args[0].Kind <> vkText) then
|
||||
@@ -421,14 +421,6 @@ begin
|
||||
Result := Items[Integer(index)];
|
||||
end;
|
||||
end;
|
||||
vkRecordSeries:
|
||||
begin
|
||||
var series := baseValue.AsRecordSeries;
|
||||
if (index < 0) or (index >= series.TotalCount) then
|
||||
raise EArgumentException.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, series.TotalCount]);
|
||||
var recordValue := series.Items[Integer(index)];
|
||||
Result := TDataValue.FromRecord(recordValue);
|
||||
end;
|
||||
else
|
||||
raise EArgumentException.Create('Indexer `[]` is not supported for this value type.');
|
||||
end;
|
||||
@@ -441,7 +433,7 @@ begin
|
||||
baseValue := Node.Base.Accept(Self);
|
||||
|
||||
case baseValue.Kind of
|
||||
vkRecordSeries: Result := TDataValue.FromSeries(baseValue.AsRecordSeries.CreateMemberSeries(Node.Member.Value));
|
||||
vkRecordSeries: Result := TDataValue.FromSeries(baseValue.AsRecordSeries[Node.Member.Value]);
|
||||
vkRecord: Result := baseValue.AsRecord[Node.Member.Value];
|
||||
else
|
||||
raise EArgumentException.Create('Member access operator `.` is not supported for this value type.');
|
||||
|
||||
+14
-14
@@ -46,7 +46,7 @@ type
|
||||
function GetKind: TStaticTypeKind;
|
||||
function GetElementType: IStaticType;
|
||||
function GetSignature: IMethodSignature;
|
||||
function GetDefinition: TScalarRecordDefinition;
|
||||
function GetDefinition: IScalarRecordDefinition;
|
||||
{$endregion}
|
||||
|
||||
// The kind of type (e.g., Ordinal, Series, etc.)
|
||||
@@ -56,7 +56,7 @@ type
|
||||
// The signature (if Kind = stMethod)
|
||||
property Signature: IMethodSignature read GetSignature;
|
||||
// The definition (if Kind = stRecord or stRecordSeries)
|
||||
property Definition: TScalarRecordDefinition read GetDefinition;
|
||||
property Definition: IScalarRecordDefinition read GetDefinition;
|
||||
|
||||
// Checks for type equality
|
||||
function IsEqual(const Other: IStaticType): Boolean;
|
||||
@@ -94,8 +94,8 @@ type
|
||||
// Factory functions for complex types
|
||||
class function CreateSeries(const AElementType: IStaticType): IStaticType; static;
|
||||
class function CreateMethod(const AParamTypes: TArray<IStaticType>; const AReturnType: IStaticType): IStaticType; static;
|
||||
class function CreateRecord(const ADef: TScalarRecordDefinition): IStaticType; static;
|
||||
class function CreateRecordSeries(const ADef: TScalarRecordDefinition): IStaticType; static;
|
||||
class function CreateRecord(const ADef: IScalarRecordDefinition): IStaticType; static;
|
||||
class function CreateRecordSeries(const ADef: IScalarRecordDefinition): IStaticType; static;
|
||||
|
||||
class function FromScalarKind(AKind: TScalar.TKind): IStaticType; static;
|
||||
end;
|
||||
@@ -148,7 +148,7 @@ type
|
||||
function GetKind: TStaticTypeKind; virtual; abstract;
|
||||
function GetElementType: IStaticType; virtual;
|
||||
function GetSignature: IMethodSignature; virtual;
|
||||
function GetDefinition: TScalarRecordDefinition; virtual;
|
||||
function GetDefinition: IScalarRecordDefinition; virtual;
|
||||
function IsEqual(const Other: IStaticType): Boolean; virtual;
|
||||
function ToString: string; override;
|
||||
end;
|
||||
@@ -163,10 +163,10 @@ begin
|
||||
Result := nil;
|
||||
end;
|
||||
|
||||
function TAbstractStaticType.GetDefinition: TScalarRecordDefinition;
|
||||
function TAbstractStaticType.GetDefinition: IScalarRecordDefinition;
|
||||
begin
|
||||
// Return an empty/invalid definition
|
||||
Result := Default(TScalarRecordDefinition);
|
||||
Result := Default(IScalarRecordDefinition);
|
||||
end;
|
||||
|
||||
function TAbstractStaticType.IsEqual(const Other: IStaticType): Boolean;
|
||||
@@ -346,16 +346,16 @@ type
|
||||
TRecordType = class(TAbstractStaticType)
|
||||
private
|
||||
FKind: TStaticTypeKind;
|
||||
FDefinition: TScalarRecordDefinition;
|
||||
FDefinition: IScalarRecordDefinition;
|
||||
public
|
||||
constructor Create(AKind: TStaticTypeKind; ADef: TScalarRecordDefinition);
|
||||
constructor Create(AKind: TStaticTypeKind; ADef: IScalarRecordDefinition);
|
||||
function GetKind: TStaticTypeKind; override;
|
||||
function GetDefinition: TScalarRecordDefinition; override;
|
||||
function GetDefinition: IScalarRecordDefinition; override;
|
||||
function IsEqual(const Other: IStaticType): Boolean; override;
|
||||
function ToString: string; override;
|
||||
end;
|
||||
|
||||
constructor TRecordType.Create(AKind: TStaticTypeKind; ADef: TScalarRecordDefinition);
|
||||
constructor TRecordType.Create(AKind: TStaticTypeKind; ADef: IScalarRecordDefinition);
|
||||
begin
|
||||
inherited Create;
|
||||
Assert(AKind in [stRecord, stRecordSeries]);
|
||||
@@ -368,7 +368,7 @@ begin
|
||||
Result := FKind;
|
||||
end;
|
||||
|
||||
function TRecordType.GetDefinition: TScalarRecordDefinition;
|
||||
function TRecordType.GetDefinition: IScalarRecordDefinition;
|
||||
begin
|
||||
Result := FDefinition;
|
||||
end;
|
||||
@@ -426,12 +426,12 @@ begin
|
||||
Result := TMethodType.Create(AParamTypes, AReturnType);
|
||||
end;
|
||||
|
||||
class function TTypes.CreateRecord(const ADef: TScalarRecordDefinition): IStaticType;
|
||||
class function TTypes.CreateRecord(const ADef: IScalarRecordDefinition): IStaticType;
|
||||
begin
|
||||
Result := TRecordType.Create(stRecord, ADef);
|
||||
end;
|
||||
|
||||
class function TTypes.CreateRecordSeries(const ADef: TScalarRecordDefinition): IStaticType;
|
||||
class function TTypes.CreateRecordSeries(const ADef: IScalarRecordDefinition): IStaticType;
|
||||
begin
|
||||
Result := TRecordType.Create(stRecordSeries, ADef);
|
||||
end;
|
||||
|
||||
+110
-28
@@ -10,7 +10,7 @@ uses
|
||||
|
||||
type
|
||||
// The runtime representation of an interned keyword
|
||||
IKeyword = interface(IInterface)
|
||||
IKeyword = interface
|
||||
function GetIdx: Integer;
|
||||
function GetName: string;
|
||||
property Idx: Integer read GetIdx;
|
||||
@@ -42,26 +42,52 @@ type
|
||||
class function Intern(const AName: string): IKeyword; static;
|
||||
end;
|
||||
|
||||
TKeywordMapping<T> = record
|
||||
type
|
||||
TField = record
|
||||
Key: IKeyword;
|
||||
Value: T;
|
||||
public
|
||||
constructor Create(const AKey: IKeyword; const AValue: T);
|
||||
end;
|
||||
private
|
||||
FMap: TArray<Integer>;
|
||||
FFields: TArray<TField>;
|
||||
FFirst, FLast: Integer;
|
||||
public
|
||||
constructor Create(const AFields: TArray<TField>);
|
||||
// Defines a mapping from Keywords to a generic value T
|
||||
IKeywordMapping<T> = interface
|
||||
{$region 'private'}
|
||||
function GetFields: TArray<TPair<IKeyword, T>>;
|
||||
{$endregion}
|
||||
// Finds the 0-based index for a given keyword. Returns -1 if not found.
|
||||
function IndexOf(const Key: IKeyword): Integer;
|
||||
property Fields: TArray<TField> read FFields;
|
||||
// Gets all fields in the mapping.
|
||||
property Fields: TArray<TPair<IKeyword, T>> read GetFields;
|
||||
end;
|
||||
|
||||
// Factory for creating IKeywordMapping instances
|
||||
TKeywordMappingRegistry<T> = record
|
||||
strict private
|
||||
// Implementation class for IKeywordMapping
|
||||
type
|
||||
TKeywordMapping = class(TInterfacedObject, IKeywordMapping<T>)
|
||||
private
|
||||
FMap: TArray<Integer>;
|
||||
FFields: TArray<TPair<IKeyword, T>>;
|
||||
FFirst, FLast: Integer;
|
||||
function GetFields: TArray<TPair<IKeyword, T>>;
|
||||
public
|
||||
// Expects AFields to be in the desired (canonical) order
|
||||
constructor Create(const AFields: TArray<TPair<IKeyword, T>>);
|
||||
function IndexOf(const Key: IKeyword): Integer;
|
||||
end;
|
||||
strict private
|
||||
// Cache implementation
|
||||
class var
|
||||
FLock: TSpinLock;
|
||||
class var
|
||||
FRegistry: TDictionary<TArray<Integer>, IKeywordMapping<T>>;
|
||||
class constructor Create;
|
||||
class destructor Destroy;
|
||||
|
||||
public
|
||||
// Creates or gets the cached keyword mapping.
|
||||
class function Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
System.Generics.Defaults; // For TComparer<Integer>
|
||||
|
||||
{ TKeywordRegistry.TKeyword }
|
||||
|
||||
constructor TKeywordRegistry.TKeyword.Create(const AName: string; AIdx: Integer);
|
||||
@@ -108,21 +134,25 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TKeywordMapping }
|
||||
{ TKeywordMappingRegistry<T>.TKeywordMapping<T> }
|
||||
|
||||
constructor TKeywordMapping<T>.Create(const AFields: TArray<TField>);
|
||||
constructor TKeywordMappingRegistry<T>.TKeywordMapping.Create(const AFields: TArray<TPair<IKeyword, T>>);
|
||||
var
|
||||
i, idx: Integer;
|
||||
begin
|
||||
inherited Create;
|
||||
FFields := AFields;
|
||||
FFirst := 0;
|
||||
FLast := -1;
|
||||
if Length(FFields) = 0 then
|
||||
exit;
|
||||
|
||||
// Find min/max Idx for the map. Fields are *not* assumed to be sorted.
|
||||
FFirst := FFields[0].Key.Idx;
|
||||
FLast := FFirst;
|
||||
for var i := 1 to High(FFields) do
|
||||
for i := 1 to High(FFields) do
|
||||
begin
|
||||
var idx := FFields[i].Key.Idx;
|
||||
idx := FFields[i].Key.Idx;
|
||||
if FFirst > idx then
|
||||
FFirst := idx;
|
||||
if FLast < idx then
|
||||
@@ -130,25 +160,77 @@ begin
|
||||
end;
|
||||
|
||||
SetLength(FMap, FLast - FFirst + 1);
|
||||
for var i := 0 to High(FMap) do
|
||||
for i := 0 to High(FMap) do
|
||||
FMap[i] := -1;
|
||||
for var i := 0 to High(FFields) do
|
||||
FMap[FFirst + FFields[i].Key.Idx] := i;
|
||||
|
||||
// Populate map based on the *original field order*
|
||||
for i := 0 to High(FFields) do
|
||||
FMap[FFields[i].Key.Idx - FFirst] := i;
|
||||
end;
|
||||
|
||||
function TKeywordMapping<T>.IndexOf(const Key: IKeyword): Integer;
|
||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetFields: TArray<TPair<IKeyword, T>>;
|
||||
begin
|
||||
var idx := Key.Idx - FFirst;
|
||||
Result := FFields;
|
||||
end;
|
||||
|
||||
function TKeywordMappingRegistry<T>.TKeywordMapping.IndexOf(const Key: IKeyword): Integer;
|
||||
var
|
||||
idx: Integer;
|
||||
begin
|
||||
idx := Key.Idx - FFirst;
|
||||
if (idx < 0) or (Idx > High(FMap)) then
|
||||
exit(-1);
|
||||
|
||||
Result := FMap[idx];
|
||||
end;
|
||||
|
||||
constructor TKeywordMapping<T>.TField.Create(const AKey: IKeyword; const AValue: T);
|
||||
{ TKeywordMappingRegistry<T> }
|
||||
|
||||
class constructor TKeywordMappingRegistry<T>.Create;
|
||||
begin
|
||||
Key := AKey;
|
||||
Value := AValue;
|
||||
FLock := TSpinLock.Create(false);
|
||||
FRegistry := TDictionary<TArray<Integer>, IKeywordMapping<T>>.Create;
|
||||
end;
|
||||
|
||||
class destructor TKeywordMappingRegistry<T>.Destroy;
|
||||
begin
|
||||
FRegistry.Free;
|
||||
end;
|
||||
|
||||
class function TKeywordMappingRegistry<T>.Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>;
|
||||
var
|
||||
key: TArray<Integer>;
|
||||
begin
|
||||
SetLength(key, Length(AFields));
|
||||
for var i := 0 to High(key) do
|
||||
key[i] := AFields[i].Key.Idx;
|
||||
|
||||
// 2. Lock
|
||||
FLock.Enter;
|
||||
try
|
||||
// 3. Check cache
|
||||
if FRegistry.TryGetValue(key, Result) then
|
||||
exit; // Found it, exit
|
||||
finally
|
||||
FLock.Exit;
|
||||
end;
|
||||
|
||||
// 4. Not in cache. Create (O(N+M)) - OUTSIDE lock
|
||||
var newMapping := TKeywordMapping.Create(AFields) as IKeywordMapping<T>;
|
||||
|
||||
// 5. Lock again to add
|
||||
FLock.Enter;
|
||||
try
|
||||
// 6. Check again (double-check)
|
||||
if FRegistry.TryGetValue(key, Result) then
|
||||
exit; // Another thread won the race
|
||||
|
||||
// 7. Add our new mapping.
|
||||
Result := newMapping;
|
||||
FRegistry.Add(key, Result);
|
||||
finally
|
||||
FLock.Exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
@@ -18,7 +18,7 @@ type
|
||||
// Creates a JSON definition string from a record type info.
|
||||
class function RecordDefinitionToJson<T: record>: string; static;
|
||||
// Creates a record definition from a JSON string.
|
||||
class function JsonToRecordDefinition(const AJson: string): TScalarRecordDefinition; static;
|
||||
class function JsonToRecordDefinition(const AJson: string): IScalarRecordDefinition; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -30,7 +30,7 @@ uses
|
||||
|
||||
{ TRttiAstHelper }
|
||||
|
||||
class function TRttiAstHelper.JsonToRecordDefinition(const AJson: string): TScalarRecordDefinition;
|
||||
class function TRttiAstHelper.JsonToRecordDefinition(const AJson: string): IScalarRecordDefinition;
|
||||
var
|
||||
jsonValue: TJSONValue;
|
||||
fieldsArray: TJSONArray;
|
||||
@@ -63,7 +63,7 @@ begin
|
||||
|
||||
fields[i] := TScalarRecordField.Create(fieldKey, TScalar.TKind(GetEnumValue(TypeInfo(TScalar.TKind), kindStr)));
|
||||
end;
|
||||
Result := TScalarRecordDefinition.Create(fields);
|
||||
Result := TScalarRecordRegistry.Intern(fields);
|
||||
finally
|
||||
jsonValue.Free;
|
||||
end;
|
||||
|
||||
@@ -4,6 +4,8 @@ interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Generics.Collections,
|
||||
System.Generics.Defaults,
|
||||
Myc.Data.Decimal,
|
||||
Myc.Data.Series,
|
||||
Myc.Data.Keyword;
|
||||
@@ -92,21 +94,22 @@ type
|
||||
TScalarTuple = TArray<TScalar>;
|
||||
|
||||
// A field definition for a scalar record.
|
||||
TScalarRecordField = TKeywordMapping<TScalar.TKind>.TField;
|
||||
TScalarRecordDefinition = TKeywordMapping<TScalar.TKind>;
|
||||
TScalarRecordField = TPair<IKeyword, TScalar.TKind>;
|
||||
IScalarRecordDefinition = IKeywordMapping<TScalar.TKind>;
|
||||
TScalarRecordRegistry = TKeywordMappingRegistry<TScalar.TKind>;
|
||||
|
||||
// A record of scalar values, based on a definition.
|
||||
TScalarRecord = record
|
||||
public
|
||||
constructor Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
|
||||
function GetDef: TScalarRecordDefinition; inline;
|
||||
constructor Create(const ADef: IScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
|
||||
function GetDef: IScalarRecordDefinition; inline;
|
||||
function GetFields: TArray<TScalar.TValue>; inline;
|
||||
function GetKeys(const Key: IKeyword): TScalar;
|
||||
property Def: TScalarRecordDefinition read GetDef;
|
||||
property Def: IScalarRecordDefinition read GetDef;
|
||||
property Fields: TArray<TScalar.TValue> read GetFields;
|
||||
property Keys[const Key: IKeyword]: TScalar read GetKeys; default;
|
||||
strict private
|
||||
FDef: TScalarRecordDefinition;
|
||||
FDef: IScalarRecordDefinition;
|
||||
FFields: TArray<TScalar.TValue>;
|
||||
end;
|
||||
|
||||
@@ -142,22 +145,21 @@ type
|
||||
IRecordSeries = interface
|
||||
{$region 'private'}
|
||||
function GetCount: Int64;
|
||||
function GetDef: TScalarRecordDefinition;
|
||||
function GetItems(Idx: Integer): TScalarRecord;
|
||||
function GetDef: IScalarRecordDefinition;
|
||||
function GetTotalCount: Int64;
|
||||
function GetFields(const Key: IKeyword): ISeries;
|
||||
{$endregion}
|
||||
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||
function CreateMemberSeries(const Key: IKeyword): ISeries;
|
||||
property Count: Int64 read GetCount;
|
||||
property Def: TScalarRecordDefinition read GetDef;
|
||||
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
|
||||
property Def: IScalarRecordDefinition read GetDef;
|
||||
property Fields[const Key: IKeyword]: ISeries read GetFields; default;
|
||||
property TotalCount: Int64 read GetTotalCount;
|
||||
end;
|
||||
|
||||
// A time series of scalar records, optimized for memory and access speed.
|
||||
TScalarRecordSeries = class(TInterfacedObject, IRecordSeries)
|
||||
type
|
||||
TMemberSeries = class(TInterfacedObject, ISeries)
|
||||
TMemberSeries = class(TObject, ISeries)
|
||||
private
|
||||
FRecordSeries: TScalarRecordSeries;
|
||||
FKind: TScalar.TKind;
|
||||
@@ -165,26 +167,29 @@ type
|
||||
function GetCount: Int64;
|
||||
function GetItems(Idx: Integer): TScalar;
|
||||
function GetTotalCount: Int64;
|
||||
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
|
||||
function _AddRef: Integer; stdcall;
|
||||
function _Release: Integer; stdcall;
|
||||
public
|
||||
constructor Create(ARecordSeries: TScalarRecordSeries; AElementIdx: Integer);
|
||||
destructor Destroy; override;
|
||||
end;
|
||||
private
|
||||
FDef: TScalarRecordDefinition;
|
||||
FDef: IScalarRecordDefinition;
|
||||
FArray: TChunkArray<TScalar.TValue>;
|
||||
FFields: TArray<TMemberSeries>;
|
||||
FTotalCount: Int64;
|
||||
function GetCount: Int64; inline;
|
||||
function GetDef: TScalarRecordDefinition; inline;
|
||||
function GetItems(Idx: Integer): TScalarRecord; inline;
|
||||
function GetFields(const Key: IKeyword): ISeries;
|
||||
function GetDef: IScalarRecordDefinition; inline;
|
||||
function GetTotalCount: Int64; inline;
|
||||
function GetItemRef(Idx: Integer): TChunkArray<TScalar.TValue>.PT; inline;
|
||||
public
|
||||
constructor Create(const ADef: TScalarRecordDefinition);
|
||||
constructor Create(const ADef: IScalarRecordDefinition);
|
||||
destructor Destroy; override;
|
||||
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||
function CreateMemberSeries(const Key: IKeyword): ISeries;
|
||||
property Count: Int64 read GetCount;
|
||||
property Def: TScalarRecordDefinition read GetDef;
|
||||
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
|
||||
property Fields[const Key: IKeyword]: ISeries read GetFields; default;
|
||||
property Def: IScalarRecordDefinition read GetDef;
|
||||
property TotalCount: Int64 read GetTotalCount;
|
||||
end;
|
||||
|
||||
@@ -519,14 +524,14 @@ end;
|
||||
|
||||
{ TScalarRecord }
|
||||
|
||||
constructor TScalarRecord.Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
|
||||
constructor TScalarRecord.Create(const ADef: IScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
|
||||
begin
|
||||
Assert(Length(ADef.Fields) = Length(AFields), 'Field definition and value count must match.');
|
||||
FDef := ADef;
|
||||
FFields := AFields;
|
||||
end;
|
||||
|
||||
function TScalarRecord.GetDef: TScalarRecordDefinition;
|
||||
function TScalarRecord.GetDef: IScalarRecordDefinition;
|
||||
begin
|
||||
Result := FDef;
|
||||
end;
|
||||
@@ -549,11 +554,21 @@ end;
|
||||
|
||||
{ TScalarRecordSeries }
|
||||
|
||||
constructor TScalarRecordSeries.Create(const ADef: TScalarRecordDefinition);
|
||||
constructor TScalarRecordSeries.Create(const ADef: IScalarRecordDefinition);
|
||||
begin
|
||||
Assert(Length(ADef.Fields) > 0);
|
||||
FDef := ADef;
|
||||
FTotalCount := 0;
|
||||
SetLength(FFields, Length(FDef.Fields));
|
||||
for var i := 0 to High(FFields) do
|
||||
FFields[i] := TMemberSeries.Create(Self, i);
|
||||
end;
|
||||
|
||||
destructor TScalarRecordSeries.Destroy;
|
||||
begin
|
||||
for var i := High(FFields) downto 0 do
|
||||
FFields[i].Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TScalarRecordSeries.Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||
@@ -562,13 +577,13 @@ begin
|
||||
inc(FTotalCount);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.CreateMemberSeries(const Key: IKeyword): ISeries;
|
||||
function TScalarRecordSeries.GetFields(const Key: IKeyword): ISeries;
|
||||
begin
|
||||
var elem := FDef.IndexOf(Key);
|
||||
if elem < 0 then
|
||||
raise EArgumentException.CreateFmt('Field ":%s" not found in record definition.', [Key.Name]);
|
||||
|
||||
Result := TMemberSeries.Create(Self, elem);
|
||||
Result := FFields[elem];
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetCount: Int64;
|
||||
@@ -576,7 +591,7 @@ begin
|
||||
Result := FArray.Count div Length(FDef.Fields);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetDef: TScalarRecordDefinition;
|
||||
function TScalarRecordSeries.GetDef: IScalarRecordDefinition;
|
||||
begin
|
||||
Result := FDef;
|
||||
end;
|
||||
@@ -587,15 +602,6 @@ begin
|
||||
Result := FArray.ItemRef[(FArray.Count - len) - (Idx * len)];
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetItems(Idx: Integer): TScalarRecord;
|
||||
var
|
||||
values: TArray<TScalar.TValue>;
|
||||
begin
|
||||
SetLength(values, Length(FDef.Fields));
|
||||
Move(GetItemRef(Idx)^, values[0], sizeof(TScalar.TValue) * Length(values));
|
||||
Result.Create(FDef, values);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetTotalCount: Int64;
|
||||
begin
|
||||
Result := FTotalCount;
|
||||
@@ -643,21 +649,16 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TScalarRecordSeries.TMemberSeries }
|
||||
|
||||
constructor TScalarRecordSeries.TMemberSeries.Create(ARecordSeries: TScalarRecordSeries; AElementIdx: Integer);
|
||||
begin
|
||||
inherited Create;
|
||||
FRecordSeries := ARecordSeries;
|
||||
FRecordSeries._AddRef;
|
||||
FKind := FRecordSeries.FDef.Fields[AElementIdx].Value;
|
||||
FOffset := sizeof(TScalar.TValue) * AElementIdx;
|
||||
end;
|
||||
|
||||
destructor TScalarRecordSeries.TMemberSeries.Destroy;
|
||||
begin
|
||||
FRecordSeries._Release;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.TMemberSeries.GetCount: Int64;
|
||||
begin
|
||||
Result := FRecordSeries.Count;
|
||||
@@ -677,6 +678,24 @@ begin
|
||||
Result := FRecordSeries.TotalCount;
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.TMemberSeries.QueryInterface(const IID: TGUID; out Obj): HResult;
|
||||
begin
|
||||
if GetInterface(IID, Obj) then
|
||||
Result := S_OK
|
||||
else
|
||||
Result := E_NOINTERFACE;
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.TMemberSeries._AddRef: Integer;
|
||||
begin
|
||||
Result := FRecordSeries._AddRef;
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.TMemberSeries._Release: Integer;
|
||||
begin
|
||||
Result := FRecordSeries._Release;
|
||||
end;
|
||||
|
||||
{ TScalarSeries }
|
||||
|
||||
constructor TScalarSeries.Create(AKind: TScalar.TKind);
|
||||
|
||||
Reference in New Issue
Block a user