Refactoring Keyword mapping, introducing tuples

This commit is contained in:
Michael Schimmel
2026-01-04 15:21:46 +01:00
parent 0a7a6ea2d0
commit 700088b5c5
3 changed files with 20 additions and 27 deletions
+1 -1
View File
@@ -385,7 +385,7 @@ begin
SetLength(fields, N.Fields.Count);
for i := 0 to N.Fields.Count - 1 do
fields[i] := TPair<IKeyword, TDataValue>.Create(N.Fields[i].Key.Value, Visit(N.Fields[i].Value));
Result := TDataValue.FromGenericRecord(TKeywordMapping<TDataValue>.Create(fields));
Result := TDataValue.FromGenericRecord(TGenericRecord<TDataValue>.Create(fields));
end;
end;
+1 -1
View File
@@ -482,7 +482,7 @@ begin
end;
end;
Result := TDataValue.FromGenericRecord(TKeywordMapping<TDataValue>.Create(recFields.ToArray));
Result := TDataValue.FromGenericRecord(TGenericRecord<TDataValue>.Create(recFields.ToArray));
finally
recFields.Free;
end;
+18 -25
View File
@@ -48,11 +48,9 @@ type
end;
// Defines a mapping from Keywords to a generic value T
IKeywordMapping<T> = interface
IKeywordMapping<T> = interface(ITuple<T>)
{$region 'private'}
function GetItems(Idx: Integer): T;
function GetFields(const Key: IKeyword): T;
function GetCount: Integer;
function GetKeywords(Idx: Integer): IKeyword;
{$endregion}
// Finds the 0-based index for a given keyword. Returns -1 if not found.
@@ -60,17 +58,13 @@ type
property Keywords[Idx: Integer]: IKeyword read GetKeywords;
property Fields[const Key: IKeyword]: T read GetFields;
property Items[Idx: Integer]: T read GetItems; default;
property Count: Integer read GetCount;
end;
// Factory for creating IKeywordMapping instances
TKeywordMappingRegistry<T> = record
strict private
// Implementation class for IKeywordMapping
type
TKeywordMapping = class(TInterfacedObject, IKeywordMapping<T>)
TMapping = class(TInterfacedObject, IKeywordMapping<T>)
private
FMap: TArray<Integer>;
FFields: TArray<TPair<IKeyword, T>>;
@@ -98,11 +92,10 @@ type
class function Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>; static;
end;
TKeywordMapping<T> = class(TInterfacedObject, IKeywordMapping<T>)
TGenericRecord<T> = class(TInterfacedObject, IKeywordMapping<T>)
private
FFields: TArray<TPair<IKeyword, T>>;
// IKeywordMapping implementation
function GetItems(Idx: Integer): T;
function GetKeywords(Idx: Integer): IKeyword;
function GetFields(const Key: IKeyword): T;
@@ -181,9 +174,9 @@ begin
Result := '';
end;
{ TKeywordMappingRegistry<T>.TKeywordMapping }
{ TKeywordMappingRegistry<T>.TMapping }
constructor TKeywordMappingRegistry<T>.TKeywordMapping.Create(const AFields: TArray<TPair<IKeyword, T>>);
constructor TKeywordMappingRegistry<T>.TMapping.Create(const AFields: TArray<TPair<IKeyword, T>>);
var
i: Integer;
keyIdx: Integer;
@@ -222,19 +215,19 @@ begin
end;
end;
function TKeywordMappingRegistry<T>.TKeywordMapping.GetCount: Integer;
function TKeywordMappingRegistry<T>.TMapping.GetCount: Integer;
begin
Result := Length(FFields);
end;
// Interface method for Items property
function TKeywordMappingRegistry<T>.TKeywordMapping.GetItems(Idx: Integer): T;
function TKeywordMappingRegistry<T>.TMapping.GetItems(Idx: Integer): T;
begin
Result := FFields[Idx].Value;
end;
// Interface method for Fields property
function TKeywordMappingRegistry<T>.TKeywordMapping.GetFields(const Key: IKeyword): T;
function TKeywordMappingRegistry<T>.TMapping.GetFields(const Key: IKeyword): T;
var
idx: Integer;
begin
@@ -244,12 +237,12 @@ begin
Result := FFields[idx].Value;
end;
function TKeywordMappingRegistry<T>.TKeywordMapping.GetKeywords(Idx: Integer): IKeyword;
function TKeywordMappingRegistry<T>.TMapping.GetKeywords(Idx: Integer): IKeyword;
begin
Result := FFields[Idx].Key;
end;
function TKeywordMappingRegistry<T>.TKeywordMapping.IndexOf(const Key: IKeyword): Integer;
function TKeywordMappingRegistry<T>.TMapping.IndexOf(const Key: IKeyword): Integer;
var
keyIdx, mapIdx: Integer;
begin
@@ -290,7 +283,7 @@ begin
try
if not FRegistry.TryGetValue(signature, Result) then
begin
Result := TKeywordMapping.Create(AFields);
Result := TMapping.Create(AFields);
FRegistry.Add(signature, Result);
end;
finally
@@ -298,20 +291,20 @@ begin
end;
end;
{ TKeywordMapping }
{ TGenericRecord }
constructor TKeywordMapping<T>.Create(const AFields: TArray<TPair<IKeyword, T>>);
constructor TGenericRecord<T>.Create(const AFields: TArray<TPair<IKeyword, T>>);
begin
inherited Create;
FFields := AFields;
end;
function TKeywordMapping<T>.GetCount: Integer;
function TGenericRecord<T>.GetCount: Integer;
begin
Result := Length(FFields);
end;
function TKeywordMapping<T>.GetFields(const Key: IKeyword): T;
function TGenericRecord<T>.GetFields(const Key: IKeyword): T;
var
idx: Integer;
begin
@@ -321,17 +314,17 @@ begin
Result := FFields[idx].Value;
end;
function TKeywordMapping<T>.GetItems(Idx: Integer): T;
function TGenericRecord<T>.GetItems(Idx: Integer): T;
begin
Result := FFields[Idx].Value;
end;
function TKeywordMapping<T>.GetKeywords(Idx: Integer): IKeyword;
function TGenericRecord<T>.GetKeywords(Idx: Integer): IKeyword;
begin
Result := FFields[Idx].Key;
end;
function TKeywordMapping<T>.IndexOf(const Key: IKeyword): Integer;
function TGenericRecord<T>.IndexOf(const Key: IKeyword): Integer;
var
i: Integer;
begin