240 lines
6.3 KiB
ObjectPascal
240 lines
6.3 KiB
ObjectPascal
unit Myc.Data.Types.Arrays;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.Generics.Collections,
|
|
System.SysUtils,
|
|
Myc.Data.Types;
|
|
|
|
type
|
|
// Implements the array data type.
|
|
TDataArrayType = class(TInterfacedObject, IDataType, IDataArrayType)
|
|
private
|
|
FElementType: IDataType;
|
|
FEmptyValue: IDataArrayValue; // Cached empty array value for this type
|
|
function GetElementType: IDataType;
|
|
function GetName: String;
|
|
function GetKind: TDataKind;
|
|
public
|
|
constructor Create(const AElementType: IDataType);
|
|
function CreateValue(const AItems: array of IDataValue): IDataArrayValue;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.SyncObjs;
|
|
|
|
type
|
|
// Implements the array data value.
|
|
TDataArrayValue = class(TInterfacedObject, IDataValue, IDataArrayValue)
|
|
private
|
|
FArrayType: IDataArrayType;
|
|
FItems: TArray<IDataValue>;
|
|
function GetDataType: IDataType;
|
|
function GetAsString: string;
|
|
function GetElementCount: Integer;
|
|
function GetItem(Idx: Integer): IDataValue;
|
|
public
|
|
constructor Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
|
|
end;
|
|
|
|
// Optimized implementation for an array with 0 elements.
|
|
TDataArrayValue0 = class(TInterfacedObject, IDataValue, IDataArrayValue)
|
|
private
|
|
FArrayType: IDataArrayType;
|
|
function GetDataType: IDataType;
|
|
function GetAsString: string;
|
|
function GetElementCount: Integer;
|
|
function GetItem(Idx: Integer): IDataValue;
|
|
public
|
|
constructor Create(const AArrayType: IDataArrayType);
|
|
end;
|
|
|
|
// Optimized implementation for an array with 1 element.
|
|
TDataArrayValue1 = class(TInterfacedObject, IDataValue, IDataArrayValue)
|
|
private
|
|
FArrayType: IDataArrayType;
|
|
FItem0: IDataValue;
|
|
function GetDataType: IDataType;
|
|
function GetAsString: string;
|
|
function GetElementCount: Integer;
|
|
function GetItem(Idx: Integer): IDataValue;
|
|
public
|
|
constructor Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
|
|
end;
|
|
|
|
{ TDataArrayValue (generic implementation for N > 1 elements) }
|
|
|
|
constructor TDataArrayValue.Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
|
|
var
|
|
i: Integer;
|
|
begin
|
|
inherited Create;
|
|
FArrayType := AArrayType;
|
|
SetLength(FItems, Length(AItems));
|
|
for i := 0 to High(AItems) do
|
|
FItems[i] := AItems[i];
|
|
end;
|
|
|
|
function TDataArrayValue.GetDataType: IDataType;
|
|
begin
|
|
Result := FArrayType;
|
|
end;
|
|
|
|
function TDataArrayValue.GetAsString: string;
|
|
var
|
|
sb: TStringBuilder;
|
|
i: Integer;
|
|
begin
|
|
sb := TStringBuilder.Create;
|
|
try
|
|
sb.Append('[');
|
|
for i := 0 to High(FItems) do
|
|
begin
|
|
sb.Append(FItems[i].AsString);
|
|
if (i < High(FItems)) then
|
|
sb.Append(', ');
|
|
end;
|
|
sb.Append(']');
|
|
Result := sb.ToString;
|
|
finally
|
|
sb.Free;
|
|
end;
|
|
end;
|
|
|
|
function TDataArrayValue.GetElementCount: Integer;
|
|
begin
|
|
Result := Length(FItems);
|
|
end;
|
|
|
|
function TDataArrayValue.GetItem(Idx: Integer): IDataValue;
|
|
begin
|
|
Result := FItems[Idx];
|
|
end;
|
|
|
|
{ TDataArrayValue0 }
|
|
|
|
constructor TDataArrayValue0.Create(const AArrayType: IDataArrayType);
|
|
begin
|
|
inherited Create;
|
|
FArrayType := AArrayType;
|
|
end;
|
|
|
|
function TDataArrayValue0.GetAsString: string;
|
|
begin
|
|
Result := '[]';
|
|
end;
|
|
|
|
function TDataArrayValue0.GetDataType: IDataType;
|
|
begin
|
|
Result := FArrayType;
|
|
end;
|
|
|
|
function TDataArrayValue0.GetElementCount: Integer;
|
|
begin
|
|
Result := 0;
|
|
end;
|
|
|
|
function TDataArrayValue0.GetItem(Idx: Integer): IDataValue;
|
|
begin
|
|
raise EArgumentException.Create('Index out of bounds');
|
|
end;
|
|
|
|
{ TDataArrayValue1 }
|
|
|
|
constructor TDataArrayValue1.Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
|
|
begin
|
|
inherited Create;
|
|
FArrayType := AArrayType;
|
|
FItem0 := AItems[0];
|
|
end;
|
|
|
|
function TDataArrayValue1.GetAsString: string;
|
|
begin
|
|
Result := '[' + FItem0.AsString + ']';
|
|
end;
|
|
|
|
function TDataArrayValue1.GetDataType: IDataType;
|
|
begin
|
|
Result := FArrayType;
|
|
end;
|
|
|
|
function TDataArrayValue1.GetElementCount: Integer;
|
|
begin
|
|
Result := 1;
|
|
end;
|
|
|
|
function TDataArrayValue1.GetItem(Idx: Integer): IDataValue;
|
|
begin
|
|
case Idx of
|
|
0: Result := FItem0;
|
|
else
|
|
raise EArgumentException.Create('Index out of bounds');
|
|
end;
|
|
end;
|
|
|
|
{ TDataArrayType }
|
|
|
|
constructor TDataArrayType.Create(const AElementType: IDataType);
|
|
begin
|
|
inherited Create;
|
|
FElementType := AElementType;
|
|
FEmptyValue := nil;
|
|
end;
|
|
|
|
function TDataArrayType.CreateValue(const AItems: array of IDataValue): IDataArrayValue;
|
|
var
|
|
item: IDataValue;
|
|
i: Integer;
|
|
newValue: IDataArrayValue;
|
|
begin
|
|
// Validate that all items match the element type of this array type.
|
|
i := 0;
|
|
for item in AItems do
|
|
begin
|
|
if (item.DataType <> FElementType) then
|
|
raise EArgumentException.CreateFmt(
|
|
'Invalid data type for item at index %d. Expected ''%s'', but got ''%s''.',
|
|
[i, FElementType.Name, item.DataType.Name]);
|
|
inc(i);
|
|
end;
|
|
|
|
// Use optimized implementations for arrays with 0 or 1 elements.
|
|
case Length(AItems) of
|
|
0:
|
|
begin
|
|
// Thread-safe lazy initialization for the cached empty array value.
|
|
if FEmptyValue = nil then
|
|
begin
|
|
newValue := TDataArrayValue0.Create(Self);
|
|
if TInterlocked.CompareExchange(Pointer(FEmptyValue), Pointer(newValue), nil) <> nil then
|
|
newValue := nil; // Another thread won, discard our instance.
|
|
end;
|
|
Result := FEmptyValue;
|
|
end;
|
|
1: Result := TDataArrayValue1.Create(Self, AItems);
|
|
else
|
|
// Use the generic implementation for arrays with more than 1 element.
|
|
Result := TDataArrayValue.Create(Self, AItems);
|
|
end;
|
|
end;
|
|
|
|
function TDataArrayType.GetElementType: IDataType;
|
|
begin
|
|
Result := FElementType;
|
|
end;
|
|
|
|
function TDataArrayType.GetName: String;
|
|
begin
|
|
Result := Format('Array<%s>', [FElementType.Name]);
|
|
end;
|
|
|
|
function TDataArrayType.GetKind: TDataKind;
|
|
begin
|
|
Result := dkArray;
|
|
end;
|
|
|
|
end.
|