Ast Playground 1. visualization

This commit is contained in:
Michael Schimmel
2025-08-30 01:38:40 +02:00
parent d2c00c6033
commit c7a865d12e
11 changed files with 1298 additions and 330 deletions
+147 -266
View File
@@ -8,7 +8,7 @@ uses
Myc.Data.Series;
type
// POD: Plain old data (...that fits into 128 bits)
// POD: Plain old data (...that fits into 64 bits)
TScalarKind = (
skInteger,
@@ -26,9 +26,9 @@ type
skDecimal
);
TScalarBytes = array[0..15] of Byte;
TScalarPChar = array[0..7] of Char;
TScalarString = String[15];
TScalarBytes = array[0..7] of Byte;
TScalarPChar = array[0..3] of Char;
TScalarString = String[7];
TScalarValue = record
public
@@ -49,54 +49,29 @@ type
class function FromBytes(const AValue: TScalarBytes): TScalarValue; static; inline;
class function FromDecimal(AValue: TDecimal): TScalarValue; static; inline;
function GetAsInteger: Integer; inline;
function GetAsInt64: Int64; inline;
function GetAsUInt64: UInt64; inline;
function GetAsSingle: Single; inline;
function GetAsDouble: Double; inline;
function GetAsDateTime: TDateTime; inline;
function GetAsTimestamp: TTimestamp; inline;
function GetAsBoolean: Boolean; inline;
function GetAsChar: Char; inline;
function GetAsPChar: PChar; inline;
function GetAsString: ShortString; inline;
function GetAsBytes: TScalarBytes; inline;
function GetAsDecimal: TDecimal; inline;
property AsInteger: Integer read GetAsInteger;
property AsInt64: Int64 read GetAsInt64;
property AsUInt64: UInt64 read GetAsUInt64;
property AsSingle: Single read GetAsSingle;
property AsDouble: Double read GetAsDouble;
property AsDateTime: TDateTime read GetAsDateTime;
property AsTimestamp: TTimestamp read GetAsTimestamp;
property AsBoolean: Boolean read GetAsBoolean;
property AsChar: Char read GetAsChar;
property AsPChar: PChar read GetAsPChar;
property AsString: ShortString read GetAsString;
property AsBytes: TScalarBytes read GetAsBytes;
property AsDecimal: TDecimal read GetAsDecimal;
strict private
// Direct field access for performance.
case TScalarKind of
skInteger: (FInteger: Integer);
skInt64: (FInt64: Int64);
skUInt64: (FUInt64: UInt64);
skSingle: (FSingle: Single);
skDouble: (FDouble: Double);
skDateTime: (FDateTime: TDateTime);
skTimestamp: (FTimestamp: TTimestamp);
skBoolean: (FBoolean: Boolean);
skChar: (FChar: Char);
skPChar: (FPChar: TScalarPChar);
skString: (FString: String[15]);
skBytes: (FBytes: TScalarBytes);
skDecimal: (FDecimal: TDecimal);
skInteger: (AsInteger: Integer);
skInt64: (AsInt64: Int64);
skUInt64: (AsUInt64: UInt64);
skSingle: (AsSingle: Single);
skDouble: (AsDouble: Double);
skDateTime: (AsDateTime: TDateTime);
skTimestamp: (AsTimestamp: TTimestamp);
skBoolean: (AsBoolean: Boolean);
skChar: (AsChar: Char);
skPChar: (AsPChar: TScalarPChar);
skString: (AsString: String[7]);
skBytes: (AsBytes: TScalarBytes);
skDecimal: (AsDecimal: TDecimal);
end;
// A scalar value with a type identifier.
TScalar = record
public
Kind: TScalarKind;
Value: TScalarValue;
// Creates a new scalar from a kind and a value.
constructor Create(AKind: TScalarKind; const AValue: TScalarValue);
@@ -113,19 +88,9 @@ type
class function FromPChar(const AValue: TScalarPChar): TScalar; static; inline;
class function FromString(const AValue: String): TScalar; static; inline;
class function FromBytes(const AValue: TScalarBytes): TScalar; static; inline;
class function FromDecimal(AValue: TDecimal): TScalar; static; inline;
function GetKind: TScalarKind; inline;
function GetValue: TScalarValue; inline;
class function FromDecimal(const AValue: TDecimal): TScalar; static; inline;
function ToString: String;
property Kind: TScalarKind read GetKind;
property Value: TScalarValue read GetValue;
strict private
FKind: TScalarKind;
FValue: TScalarValue;
end;
// Basic data structures using the scalar type (these are not POD of course)
@@ -133,51 +98,23 @@ type
// An array of scalar values of the same kind.
TScalarArray = record
public
Kind: TScalarKind;
Items: TArray<TScalarValue>;
// Creates a new scalar array.
constructor Create(AKind: TScalarKind; const AItems: TArray<TScalarValue>);
function GetKind: TScalarKind; inline;
function GetItems: TArray<TScalarValue>; inline;
property Kind: TScalarKind read GetKind;
property Items: TArray<TScalarValue> read GetItems;
strict private
FKind: TScalarKind;
FItems: TArray<TScalarValue>;
end;
TScalarTuple = TArray<TScalar>;
// A field definition for a scalar record.
TScalarRecordField = record
public
// Creates a new record field definition.
Name: String;
Kind: TScalarKind;
constructor Create(const AName: String; AKind: TScalarKind);
function GetName: String; inline;
function GetKind: TScalarKind; inline;
property Name: String read GetName;
property Kind: TScalarKind read GetKind;
strict private
FName: String;
FKind: TScalarKind;
end;
// A collection of field definitions that describe a scalar record.
TScalarRecordDefinition = record
public
// Creates a new record definition.
constructor Create(const AFields: TArray<TScalarRecordField>);
function GetFields: TArray<TScalarRecordField>; inline;
property Fields: TArray<TScalarRecordField> read GetFields;
strict private
FFields: TArray<TScalarRecordField>;
end;
TScalarRecordDefinition = TArray<TScalarRecordField>;
// A record of scalar values, based on a definition.
TScalarRecord = record
@@ -215,272 +152,213 @@ type
FItems: TSeries<TScalarValue>;
end;
// A time series of scalar records, optimized for memory and access speed.
TScalarRecordSeries = record
private
FDef: TScalarRecordDefinition;
FArray: TChunkArray<TScalarValue>;
FTotalCount: Int64;
function GetDef: TScalarRecordDefinition; inline;
function GetItems(Idx: Integer): TScalarRecord;
public
constructor Create(const ADef: TScalarRecordDefinition);
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
property Def: TScalarRecordDefinition read GetDef;
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
property TotalCount: Int64 read FTotalCount;
end;
implementation
{ TScalarValue }
class function TScalarValue.FromBoolean(AValue: Boolean): TScalarValue;
begin
Result.FBoolean := AValue;
Result.AsBoolean := AValue;
end;
class function TScalarValue.FromBytes(const AValue: TScalarBytes): TScalarValue;
begin
Result.FBytes := AValue;
Result.AsBytes := AValue;
end;
class function TScalarValue.FromChar(AValue: Char): TScalarValue;
begin
Result.FChar := AValue;
Result.AsChar := AValue;
end;
class function TScalarValue.FromDateTime(AValue: TDateTime): TScalarValue;
begin
Result.FDateTime := AValue;
Result.AsDateTime := AValue;
end;
class function TScalarValue.FromDecimal(AValue: TDecimal): TScalarValue;
begin
Result.FDecimal := AValue;
Result.AsDecimal := AValue;
end;
class function TScalarValue.FromDouble(AValue: Double): TScalarValue;
begin
Result.FDouble := AValue;
Result.AsDouble := AValue;
end;
class function TScalarValue.FromInt64(AValue: Int64): TScalarValue;
begin
Result.FInt64 := AValue;
Result.AsInt64 := AValue;
end;
class function TScalarValue.FromInteger(AValue: Integer): TScalarValue;
begin
Result.FInteger := AValue;
Result.AsInteger := AValue;
end;
class function TScalarValue.FromPChar(const AValue: String): TScalarValue;
var
len, i: Integer;
begin
FillChar(Result.FPChar, SizeOf(Result.FPChar), #0);
FillChar(Result.AsPChar, SizeOf(Result.AsPChar), #0);
len := Length(AValue);
if (len > 0) then
begin
if (len > Length(Result.FPChar)) then
len := Length(Result.FPChar);
if (len > Length(Result.AsPChar)) then
len := Length(Result.AsPChar);
for i := 0 to len - 1 do
Result.FPChar[i] := AValue[i + 1];
Result.AsPChar[i] := AValue[i + 1];
end;
end;
class function TScalarValue.FromPChar(const AValue: TScalarPChar): TScalarValue;
begin
Result.FPChar := AValue;
Result.AsPChar := AValue;
end;
class function TScalarValue.FromSingle(AValue: Single): TScalarValue;
begin
Result.FSingle := AValue;
Result.AsSingle := AValue;
end;
class function TScalarValue.FromString(const AValue: String): TScalarValue;
begin
Result.FString := ShortString(AValue);
Result.AsString := ShortString(AValue);
end;
class function TScalarValue.FromString(const AValue: TScalarString): TScalarValue;
begin
Result.FString := AValue;
Result.AsString := AValue;
end;
class function TScalarValue.FromTimestamp(AValue: TTimestamp): TScalarValue;
begin
Result.FTimestamp := AValue;
Result.AsTimestamp := AValue;
end;
class function TScalarValue.FromUInt64(AValue: UInt64): TScalarValue;
begin
Result.FUInt64 := AValue;
end;
function TScalarValue.GetAsBoolean: Boolean;
begin
Result := FBoolean;
end;
function TScalarValue.GetAsBytes: TScalarBytes;
begin
Result := FBytes;
end;
function TScalarValue.GetAsChar: Char;
begin
Result := FChar;
end;
function TScalarValue.GetAsDateTime: TDateTime;
begin
Result := FDateTime;
end;
function TScalarValue.GetAsDecimal: TDecimal;
begin
Result := FDecimal;
end;
function TScalarValue.GetAsDouble: Double;
begin
Result := FDouble;
end;
function TScalarValue.GetAsInt64: Int64;
begin
Result := FInt64;
end;
function TScalarValue.GetAsInteger: Integer;
begin
Result := FInteger;
end;
function TScalarValue.GetAsPChar: PChar;
begin
Result := @FPChar;
end;
function TScalarValue.GetAsSingle: Single;
begin
Result := FSingle;
end;
function TScalarValue.GetAsString: ShortString;
begin
Result := FString;
end;
function TScalarValue.GetAsTimestamp: TTimestamp;
begin
Result := FTimestamp;
end;
function TScalarValue.GetAsUInt64: UInt64;
begin
Result := FUInt64;
Result.AsUInt64 := AValue;
end;
{ TScalar }
constructor TScalar.Create(AKind: TScalarKind; const AValue: TScalarValue);
begin
FKind := AKind;
FValue := AValue;
Kind := AKind;
Value := AValue;
end;
class function TScalar.FromBoolean(AValue: Boolean): TScalar;
begin
Result.FKind := skBoolean;
Result.FValue := TScalarValue.FromBoolean(AValue);
Result.Kind := skBoolean;
Result.Value := TScalarValue.FromBoolean(AValue);
end;
class function TScalar.FromBytes(const AValue: TScalarBytes): TScalar;
begin
Result.FKind := skBytes;
Result.FValue := TScalarValue.FromBytes(AValue);
Result.Kind := skBytes;
Result.Value := TScalarValue.FromBytes(AValue);
end;
class function TScalar.FromChar(AValue: Char): TScalar;
begin
Result.FKind := skChar;
Result.FValue := TScalarValue.FromChar(AValue);
Result.Kind := skChar;
Result.Value := TScalarValue.FromChar(AValue);
end;
class function TScalar.FromDateTime(AValue: TDateTime): TScalar;
begin
Result.FKind := skDateTime;
Result.FValue := TScalarValue.FromDateTime(AValue);
Result.Kind := skDateTime;
Result.Value := TScalarValue.FromDateTime(AValue);
end;
class function TScalar.FromDecimal(AValue: TDecimal): TScalar;
class function TScalar.FromDecimal(const AValue: TDecimal): TScalar;
begin
Result.FKind := skDecimal;
Result.FValue := TScalarValue.FromDecimal(AValue);
Result.Kind := skDecimal;
Result.Value := TScalarValue.FromDecimal(AValue);
end;
class function TScalar.FromDouble(AValue: Double): TScalar;
begin
Result.FKind := skDouble;
Result.FValue := TScalarValue.FromDouble(AValue);
Result.Kind := skDouble;
Result.Value := TScalarValue.FromDouble(AValue);
end;
class function TScalar.FromInt64(AValue: Int64): TScalar;
begin
Result.FKind := skInt64;
Result.FValue := TScalarValue.FromInt64(AValue);
Result.Kind := skInt64;
Result.Value := TScalarValue.FromInt64(AValue);
end;
class function TScalar.FromInteger(AValue: Integer): TScalar;
begin
Result.FKind := skInteger;
Result.FValue := TScalarValue.FromInteger(AValue);
Result.Kind := skInteger;
Result.Value := TScalarValue.FromInteger(AValue);
end;
class function TScalar.FromPChar(const AValue: TScalarPChar): TScalar;
begin
Result.FKind := skPChar;
Result.FValue := TScalarValue.FromPChar(AValue);
Result.Kind := skPChar;
Result.Value := TScalarValue.FromPChar(AValue);
end;
class function TScalar.FromSingle(AValue: Single): TScalar;
begin
Result.FKind := skSingle;
Result.FValue := TScalarValue.FromSingle(AValue);
Result.Kind := skSingle;
Result.Value := TScalarValue.FromSingle(AValue);
end;
class function TScalar.FromString(const AValue: String): TScalar;
begin
Result.FKind := skString;
Result.FValue := TScalarValue.FromString(AValue);
Result.Kind := skString;
Result.Value := TScalarValue.FromString(AValue);
end;
class function TScalar.FromTimestamp(AValue: TTimestamp): TScalar;
begin
Result.FKind := skTimestamp;
Result.FValue := TScalarValue.FromTimestamp(AValue);
Result.Kind := skTimestamp;
Result.Value := TScalarValue.FromTimestamp(AValue);
end;
class function TScalar.FromUInt64(AValue: UInt64): TScalar;
begin
Result.FKind := skUInt64;
Result.FValue := TScalarValue.FromUInt64(AValue);
end;
function TScalar.GetKind: TScalarKind;
begin
Result := FKind;
end;
function TScalar.GetValue: TScalarValue;
begin
Result := FValue;
Result.Kind := skUInt64;
Result.Value := TScalarValue.FromUInt64(AValue);
end;
function TScalar.ToString: String;
begin
case FKind of
skInteger: Result := IntToStr(FValue.AsInteger);
skInt64: Result := IntToStr(FValue.AsInt64);
skUInt64: Result := UIntToStr(FValue.AsUInt64);
skSingle: Result := FloatToStr(FValue.AsSingle);
skDouble: Result := FloatToStr(FValue.AsDouble);
skDateTime: Result := DateTimeToStr(FValue.AsDateTime);
skTimestamp: Result := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', TimeStampToDateTime(FValue.AsTimestamp));
skBoolean: Result := BoolToStr(FValue.AsBoolean, True);
skChar: Result := '''' + FValue.AsChar + '''';
skPChar: Result := FValue.AsPChar; // Already null-terminated
skString: Result := '''' + string(FValue.AsString) + '''';
case Kind of
skInteger: Result := IntToStr(Value.AsInteger);
skInt64: Result := IntToStr(Value.AsInt64);
skUInt64: Result := UIntToStr(Value.AsUInt64);
skSingle: Result := FloatToStr(Value.AsSingle);
skDouble: Result := FloatToStr(Value.AsDouble);
skDateTime: Result := DateTimeToStr(Value.AsDateTime);
skTimestamp: Result := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', TimeStampToDateTime(Value.AsTimestamp));
skBoolean: Result := BoolToStr(Value.AsBoolean, True);
skChar: Result := '''' + Value.AsChar + '''';
skPChar: Result := PChar(@Value.AsPChar); // Needs explicit cast/pointer
skString: Result := '''' + string(Value.AsString) + '''';
skBytes: Result := '(Bytes)';
skDecimal: Result := FloatToStr(Double(FValue.AsDecimal));
skDecimal: Result := FloatToStr(Double(Value.AsDecimal));
else
Result := '[Unknown Scalar]';
end;
@@ -490,55 +368,23 @@ end;
constructor TScalarArray.Create(AKind: TScalarKind; const AItems: TArray<TScalarValue>);
begin
FKind := AKind;
FItems := AItems;
end;
function TScalarArray.GetKind: TScalarKind;
begin
Result := FKind;
end;
function TScalarArray.GetItems: TArray<TScalarValue>;
begin
Result := FItems;
Kind := AKind;
Items := AItems;
end;
{ TScalarRecordField }
constructor TScalarRecordField.Create(const AName: String; AKind: TScalarKind);
begin
FName := AName;
FKind := AKind;
end;
function TScalarRecordField.GetName: String;
begin
Result := FName;
end;
function TScalarRecordField.GetKind: TScalarKind;
begin
Result := FKind;
end;
{ TScalarRecordDefinition }
constructor TScalarRecordDefinition.Create(const AFields: TArray<TScalarRecordField>);
begin
FFields := AFields;
end;
function TScalarRecordDefinition.GetFields: TArray<TScalarRecordField>;
begin
Result := FFields;
Name := AName;
Kind := AKind;
end;
{ TScalarRecord }
constructor TScalarRecord.Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalarValue>);
begin
Assert(Length(ADef.Fields) = Length(AFields), 'Field definition and value count must match.');
Assert(Length(ADef) = Length(AFields), 'Field definition and value count must match.');
FDef := ADef;
FFields := AFields;
end;
@@ -559,9 +405,9 @@ var
fieldDef: TScalarRecordField;
begin
// Find the index of the field by name (case-insensitive)
for i := 0 to Length(FDef.Fields) - 1 do
for i := 0 to Length(FDef) - 1 do
begin
fieldDef := FDef.Fields[i];
fieldDef := FDef[i];
if (CompareText(fieldDef.Name, Name) = 0) then
begin
// Found it. Construct the TScalar result from the kind and value.
@@ -592,7 +438,42 @@ begin
Result := FItems;
end;
{ TScalarRecordSeries }
// Implements a time series of scalar records using a chunk array for efficient storage.
// Each record's fields are stored sequentially in the TChunkArray.
constructor TScalarRecordSeries.Create(const ADef: TScalarRecordDefinition);
begin
Assert(Length(ADef) > 0);
FDef := ADef;
FArray := Default(TChunkArray<TScalarValue>);
FTotalCount := 0;
end;
procedure TScalarRecordSeries.Add(const Item: TScalarRecord; Lookback: Int64 = -1);
begin
FArray.Add(Item.Fields, Length(FDef) * Lookback);
inc(FTotalCount);
end;
function TScalarRecordSeries.GetDef: TScalarRecordDefinition;
begin
Result := FDef;
end;
function TScalarRecordSeries.GetItems(Idx: Integer): TScalarRecord;
var
values: TArray<TScalarValue>;
fieldCount: Integer;
begin
fieldCount := Length(FDef);
SetLength(values, fieldCount);
Move(FArray.ItemRef[(FArray.Count - fieldCount) - (Idx * fieldCount)]^, values[0], sizeof(TScalarValue) * fieldCount);
Result.Create(FDef, values);
end;
initialization
Assert(sizeof(TScalarValue) = 16);
Assert(sizeof(TScalarValue) = 8);
end.