Refactoring Keyword mapping, introducing tuples
This commit is contained in:
@@ -6,7 +6,8 @@ uses
|
||||
System.SysUtils,
|
||||
System.Generics.Collections,
|
||||
System.Generics.Defaults,
|
||||
System.SyncObjs;
|
||||
System.SyncObjs,
|
||||
Myc.Data.Tuple;
|
||||
|
||||
type
|
||||
// The runtime representation of an interned keyword
|
||||
@@ -49,17 +50,18 @@ type
|
||||
// Defines a mapping from Keywords to a generic value T
|
||||
IKeywordMapping<T> = interface
|
||||
{$region 'private'}
|
||||
function GetItems(Idx: Integer): TPair<IKeyword, T>;
|
||||
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.
|
||||
function IndexOf(const Key: IKeyword): Integer;
|
||||
|
||||
// Access value by Keyword (raises exception if not found)
|
||||
property Keywords[Idx: Integer]: IKeyword read GetKeywords;
|
||||
property Fields[const Key: IKeyword]: T read GetFields;
|
||||
// Access Key-Value pair by Index (0..Count-1)
|
||||
property Items[Idx: Integer]: TPair<IKeyword, T> read GetItems; default;
|
||||
property Items[Idx: Integer]: T read GetItems; default;
|
||||
|
||||
property Count: Integer read GetCount;
|
||||
end;
|
||||
|
||||
@@ -74,8 +76,9 @@ type
|
||||
FFields: TArray<TPair<IKeyword, T>>;
|
||||
FFirst, FLast: Integer;
|
||||
|
||||
function GetItems(Idx: Integer): TPair<IKeyword, T>;
|
||||
function GetItems(Idx: Integer): T;
|
||||
function GetFields(const Key: IKeyword): T;
|
||||
function GetKeywords(Idx: Integer): IKeyword;
|
||||
function GetCount: Integer;
|
||||
public
|
||||
// Expects AFields to be in the desired (canonical) order
|
||||
@@ -100,7 +103,8 @@ type
|
||||
FFields: TArray<TPair<IKeyword, T>>;
|
||||
|
||||
// IKeywordMapping implementation
|
||||
function GetItems(Idx: Integer): TPair<IKeyword, T>;
|
||||
function GetItems(Idx: Integer): T;
|
||||
function GetKeywords(Idx: Integer): IKeyword;
|
||||
function GetFields(const Key: IKeyword): T;
|
||||
function GetCount: Integer;
|
||||
public
|
||||
@@ -224,9 +228,9 @@ begin
|
||||
end;
|
||||
|
||||
// Interface method for Items property
|
||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetItems(Idx: Integer): TPair<IKeyword, T>;
|
||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetItems(Idx: Integer): T;
|
||||
begin
|
||||
Result := FFields[Idx];
|
||||
Result := FFields[Idx].Value;
|
||||
end;
|
||||
|
||||
// Interface method for Fields property
|
||||
@@ -240,6 +244,11 @@ begin
|
||||
Result := FFields[idx].Value;
|
||||
end;
|
||||
|
||||
function TKeywordMappingRegistry<T>.TKeywordMapping.GetKeywords(Idx: Integer): IKeyword;
|
||||
begin
|
||||
Result := FFields[Idx].Key;
|
||||
end;
|
||||
|
||||
function TKeywordMappingRegistry<T>.TKeywordMapping.IndexOf(const Key: IKeyword): Integer;
|
||||
var
|
||||
keyIdx, mapIdx: Integer;
|
||||
@@ -312,9 +321,14 @@ begin
|
||||
Result := FFields[idx].Value;
|
||||
end;
|
||||
|
||||
function TKeywordMapping<T>.GetItems(Idx: Integer): TPair<IKeyword, T>;
|
||||
function TKeywordMapping<T>.GetItems(Idx: Integer): T;
|
||||
begin
|
||||
Result := FFields[Idx];
|
||||
Result := FFields[Idx].Value;
|
||||
end;
|
||||
|
||||
function TKeywordMapping<T>.GetKeywords(Idx: Integer): IKeyword;
|
||||
begin
|
||||
Result := FFields[Idx].Key;
|
||||
end;
|
||||
|
||||
function TKeywordMapping<T>.IndexOf(const Key: IKeyword): Integer;
|
||||
|
||||
Reference in New Issue
Block a user