Refactoring Keyword mapping, introducing tuples
This commit is contained in:
@@ -385,7 +385,7 @@ begin
|
|||||||
SetLength(fields, N.Fields.Count);
|
SetLength(fields, N.Fields.Count);
|
||||||
for i := 0 to N.Fields.Count - 1 do
|
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));
|
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;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
@@ -482,7 +482,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Result := TDataValue.FromGenericRecord(TKeywordMapping<TDataValue>.Create(recFields.ToArray));
|
Result := TDataValue.FromGenericRecord(TGenericRecord<TDataValue>.Create(recFields.ToArray));
|
||||||
finally
|
finally
|
||||||
recFields.Free;
|
recFields.Free;
|
||||||
end;
|
end;
|
||||||
|
|||||||
@@ -48,11 +48,9 @@ type
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// Defines a mapping from Keywords to a generic value T
|
// Defines a mapping from Keywords to a generic value T
|
||||||
IKeywordMapping<T> = interface
|
IKeywordMapping<T> = interface(ITuple<T>)
|
||||||
{$region 'private'}
|
{$region 'private'}
|
||||||
function GetItems(Idx: Integer): T;
|
|
||||||
function GetFields(const Key: IKeyword): T;
|
function GetFields(const Key: IKeyword): T;
|
||||||
function GetCount: Integer;
|
|
||||||
function GetKeywords(Idx: Integer): IKeyword;
|
function GetKeywords(Idx: Integer): IKeyword;
|
||||||
{$endregion}
|
{$endregion}
|
||||||
// Finds the 0-based index for a given keyword. Returns -1 if not found.
|
// 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 Keywords[Idx: Integer]: IKeyword read GetKeywords;
|
||||||
property Fields[const Key: IKeyword]: T read GetFields;
|
property Fields[const Key: IKeyword]: T read GetFields;
|
||||||
property Items[Idx: Integer]: T read GetItems; default;
|
|
||||||
|
|
||||||
property Count: Integer read GetCount;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Factory for creating IKeywordMapping instances
|
// Factory for creating IKeywordMapping instances
|
||||||
TKeywordMappingRegistry<T> = record
|
TKeywordMappingRegistry<T> = record
|
||||||
strict private
|
strict private
|
||||||
// Implementation class for IKeywordMapping
|
|
||||||
type
|
type
|
||||||
TKeywordMapping = class(TInterfacedObject, IKeywordMapping<T>)
|
TMapping = class(TInterfacedObject, IKeywordMapping<T>)
|
||||||
private
|
private
|
||||||
FMap: TArray<Integer>;
|
FMap: TArray<Integer>;
|
||||||
FFields: TArray<TPair<IKeyword, T>>;
|
FFields: TArray<TPair<IKeyword, T>>;
|
||||||
@@ -98,11 +92,10 @@ type
|
|||||||
class function Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>; static;
|
class function Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>; static;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TKeywordMapping<T> = class(TInterfacedObject, IKeywordMapping<T>)
|
TGenericRecord<T> = class(TInterfacedObject, IKeywordMapping<T>)
|
||||||
private
|
private
|
||||||
FFields: TArray<TPair<IKeyword, T>>;
|
FFields: TArray<TPair<IKeyword, T>>;
|
||||||
|
|
||||||
// IKeywordMapping implementation
|
|
||||||
function GetItems(Idx: Integer): T;
|
function GetItems(Idx: Integer): T;
|
||||||
function GetKeywords(Idx: Integer): IKeyword;
|
function GetKeywords(Idx: Integer): IKeyword;
|
||||||
function GetFields(const Key: IKeyword): T;
|
function GetFields(const Key: IKeyword): T;
|
||||||
@@ -181,9 +174,9 @@ begin
|
|||||||
Result := '';
|
Result := '';
|
||||||
end;
|
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
|
var
|
||||||
i: Integer;
|
i: Integer;
|
||||||
keyIdx: Integer;
|
keyIdx: Integer;
|
||||||
@@ -222,19 +215,19 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetCount: Integer;
|
function TKeywordMappingRegistry<T>.TMapping.GetCount: Integer;
|
||||||
begin
|
begin
|
||||||
Result := Length(FFields);
|
Result := Length(FFields);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Interface method for Items property
|
// Interface method for Items property
|
||||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetItems(Idx: Integer): T;
|
function TKeywordMappingRegistry<T>.TMapping.GetItems(Idx: Integer): T;
|
||||||
begin
|
begin
|
||||||
Result := FFields[Idx].Value;
|
Result := FFields[Idx].Value;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Interface method for Fields property
|
// Interface method for Fields property
|
||||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetFields(const Key: IKeyword): T;
|
function TKeywordMappingRegistry<T>.TMapping.GetFields(const Key: IKeyword): T;
|
||||||
var
|
var
|
||||||
idx: Integer;
|
idx: Integer;
|
||||||
begin
|
begin
|
||||||
@@ -244,12 +237,12 @@ begin
|
|||||||
Result := FFields[idx].Value;
|
Result := FFields[idx].Value;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetKeywords(Idx: Integer): IKeyword;
|
function TKeywordMappingRegistry<T>.TMapping.GetKeywords(Idx: Integer): IKeyword;
|
||||||
begin
|
begin
|
||||||
Result := FFields[Idx].Key;
|
Result := FFields[Idx].Key;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMappingRegistry<T>.TKeywordMapping.IndexOf(const Key: IKeyword): Integer;
|
function TKeywordMappingRegistry<T>.TMapping.IndexOf(const Key: IKeyword): Integer;
|
||||||
var
|
var
|
||||||
keyIdx, mapIdx: Integer;
|
keyIdx, mapIdx: Integer;
|
||||||
begin
|
begin
|
||||||
@@ -290,7 +283,7 @@ begin
|
|||||||
try
|
try
|
||||||
if not FRegistry.TryGetValue(signature, Result) then
|
if not FRegistry.TryGetValue(signature, Result) then
|
||||||
begin
|
begin
|
||||||
Result := TKeywordMapping.Create(AFields);
|
Result := TMapping.Create(AFields);
|
||||||
FRegistry.Add(signature, Result);
|
FRegistry.Add(signature, Result);
|
||||||
end;
|
end;
|
||||||
finally
|
finally
|
||||||
@@ -298,20 +291,20 @@ begin
|
|||||||
end;
|
end;
|
||||||
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
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
FFields := AFields;
|
FFields := AFields;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMapping<T>.GetCount: Integer;
|
function TGenericRecord<T>.GetCount: Integer;
|
||||||
begin
|
begin
|
||||||
Result := Length(FFields);
|
Result := Length(FFields);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMapping<T>.GetFields(const Key: IKeyword): T;
|
function TGenericRecord<T>.GetFields(const Key: IKeyword): T;
|
||||||
var
|
var
|
||||||
idx: Integer;
|
idx: Integer;
|
||||||
begin
|
begin
|
||||||
@@ -321,17 +314,17 @@ begin
|
|||||||
Result := FFields[idx].Value;
|
Result := FFields[idx].Value;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMapping<T>.GetItems(Idx: Integer): T;
|
function TGenericRecord<T>.GetItems(Idx: Integer): T;
|
||||||
begin
|
begin
|
||||||
Result := FFields[Idx].Value;
|
Result := FFields[Idx].Value;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMapping<T>.GetKeywords(Idx: Integer): IKeyword;
|
function TGenericRecord<T>.GetKeywords(Idx: Integer): IKeyword;
|
||||||
begin
|
begin
|
||||||
Result := FFields[Idx].Key;
|
Result := FFields[Idx].Key;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeywordMapping<T>.IndexOf(const Key: IKeyword): Integer;
|
function TGenericRecord<T>.IndexOf(const Key: IKeyword): Integer;
|
||||||
var
|
var
|
||||||
i: Integer;
|
i: Integer;
|
||||||
begin
|
begin
|
||||||
|
|||||||
Reference in New Issue
Block a user