Keywords as basic scalar type
This commit is contained in:
@@ -35,11 +35,15 @@ type
|
||||
FLock: TSpinLock;
|
||||
class var
|
||||
FRegistry: TDictionary<string, IKeyword>;
|
||||
class var
|
||||
FReverseMap: TList<IKeyword>; // Use TList
|
||||
class constructor Create;
|
||||
class destructor Destroy;
|
||||
public
|
||||
// Gets or creates the interned keyword for the given name.
|
||||
class function Intern(const AName: string): IKeyword; static;
|
||||
// Gets the name for a given keyword index.
|
||||
class function GetName(AIdx: Integer): string; static;
|
||||
end;
|
||||
|
||||
// Defines a mapping from Keywords to a generic value T
|
||||
@@ -113,11 +117,30 @@ class constructor TKeywordRegistry.Create;
|
||||
begin
|
||||
FLock := TSpinLock.Create(false);
|
||||
FRegistry := TDictionary<string, IKeyword>.Create;
|
||||
FReverseMap := TList<IKeyword>.Create;
|
||||
|
||||
Intern('false');
|
||||
Intern('true');
|
||||
end;
|
||||
|
||||
class destructor TKeywordRegistry.Destroy;
|
||||
begin
|
||||
FRegistry.Free;
|
||||
FReverseMap.Free;
|
||||
end;
|
||||
|
||||
class function TKeywordRegistry.GetName(AIdx: Integer): string;
|
||||
begin
|
||||
FLock.Enter;
|
||||
try
|
||||
// Check bounds
|
||||
if (AIdx >= 0) and (AIdx < FReverseMap.Count) then
|
||||
Result := FReverseMap[AIdx].Name
|
||||
else
|
||||
Result := ''; // Return empty for safety
|
||||
finally
|
||||
FLock.Exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
class function TKeywordRegistry.Intern(const AName: string): IKeyword;
|
||||
@@ -127,7 +150,8 @@ begin
|
||||
if not FRegistry.TryGetValue(AName, Result) then
|
||||
begin
|
||||
Result := TKeyword.Create(AName, FRegistry.Count);
|
||||
FRegistry.Add(AName, Result);
|
||||
FReverseMap.Add(Result); // Add to reverse map (Idx -> Interface)
|
||||
FRegistry.Add(AName, Result); // Add to forward map (Name -> Interface)
|
||||
end;
|
||||
finally
|
||||
FLock.Exit;
|
||||
@@ -200,6 +224,7 @@ end;
|
||||
class function TKeywordMappingRegistry<T>.Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>;
|
||||
var
|
||||
key: TArray<Integer>;
|
||||
newMapping: IKeywordMapping<T>;
|
||||
begin
|
||||
SetLength(key, Length(AFields));
|
||||
for var i := 0 to High(key) do
|
||||
@@ -216,7 +241,7 @@ begin
|
||||
end;
|
||||
|
||||
// 4. Not in cache. Create (O(N+M)) - OUTSIDE lock
|
||||
var newMapping := TKeywordMapping.Create(AFields) as IKeywordMapping<T>;
|
||||
newMapping := TKeywordMapping.Create(AFields) as IKeywordMapping<T>;
|
||||
|
||||
// 5. Lock again to add
|
||||
FLock.Enter;
|
||||
|
||||
Reference in New Issue
Block a user