Keyword mapping refactoring
This commit is contained in:
@@ -3,6 +3,7 @@ unit Myc.Data.Records;
|
||||
interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Generics.Collections,
|
||||
Myc.Data.Keyword,
|
||||
Myc.Data.Value;
|
||||
@@ -12,7 +13,11 @@ type
|
||||
TDynamicRecord = class(TInterfacedObject, IKeywordMapping<TDataValue>)
|
||||
private
|
||||
FFields: TArray<TPair<IKeyword, TDataValue>>;
|
||||
function GetFields: TArray<TPair<IKeyword, TDataValue>>;
|
||||
|
||||
// IKeywordMapping implementation
|
||||
function GetItems(Idx: Integer): TPair<IKeyword, TDataValue>;
|
||||
function GetFields(const Key: IKeyword): TDataValue;
|
||||
function GetCount: Integer;
|
||||
public
|
||||
constructor Create(const AFields: TArray<TPair<IKeyword, TDataValue>>);
|
||||
function IndexOf(const Key: IKeyword): Integer;
|
||||
@@ -28,19 +33,39 @@ begin
|
||||
FFields := AFields;
|
||||
end;
|
||||
|
||||
function TDynamicRecord.GetFields: TArray<TPair<IKeyword, TDataValue>>;
|
||||
function TDynamicRecord.GetCount: Integer;
|
||||
begin
|
||||
Result := FFields;
|
||||
Result := Length(FFields);
|
||||
end;
|
||||
|
||||
function TDynamicRecord.GetFields(const Key: IKeyword): TDataValue;
|
||||
var
|
||||
idx: Integer;
|
||||
begin
|
||||
idx := IndexOf(Key);
|
||||
if idx < 0 then
|
||||
raise EArgumentException.CreateFmt('Field "%s" not found in dynamic record.', [Key.Name]);
|
||||
Result := FFields[idx].Value;
|
||||
end;
|
||||
|
||||
function TDynamicRecord.GetItems(Idx: Integer): TPair<IKeyword, TDataValue>;
|
||||
begin
|
||||
Result := FFields[Idx];
|
||||
end;
|
||||
|
||||
function TDynamicRecord.IndexOf(const Key: IKeyword): Integer;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
// Linear search (O(n)). Fast enough for typical records.
|
||||
// Keyword comparison is integer-based (Idx), so this is efficient.
|
||||
for Result := 0 to High(FFields) do
|
||||
for i := 0 to High(FFields) do
|
||||
begin
|
||||
if FFields[Result].Key.Idx = Key.Idx then
|
||||
if FFields[i].Key.Idx = Key.Idx then
|
||||
begin
|
||||
Result := i;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
Result := -1;
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user