Browse Source

* Patch from Silvio Clecio to add a TryGetValue method to TFPGMap (bug ID 29397)

git-svn-id: trunk@32942 -
michael 9 years ago
parent
commit
64c324caaf
1 changed files with 12 additions and 0 deletions
  1. 12 0
      rtl/objpas/fgl.pp

+ 12 - 0
rtl/objpas/fgl.pp

@@ -305,6 +305,7 @@ type
     function Add(const AKey: TKey; const AData: TData): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
     function Add(const AKey: TKey): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
     function Find(const AKey: TKey; out Index: Integer): Boolean; {$ifdef CLASSESINLINE} inline; {$endif}
+    function TryGetValue(const AKey: TKey; out AData: TData): Boolean; {$ifdef CLASSESINLINE} inline; {$endif}
     function IndexOf(const AKey: TKey): Integer; {$ifdef CLASSESINLINE} inline; {$endif}
     function IndexOfData(const AData: TData): Integer;
     procedure InsertKey(Index: Integer; const AKey: TKey);
@@ -1481,6 +1482,17 @@ begin
   Result := inherited Find(@AKey, Index);
 end;
 
+function TFPGMap.TryGetValue(const AKey: TKey; out AData: TData): Boolean;
+var
+  I: Integer;
+begin
+  Result := inherited Find(@AKey, I);
+  if Result then
+    AData := TData(inherited GetData(I)^)
+  else
+    AData := Default(TData);
+end;
+
 function TFPGMap.IndexOf(const AKey: TKey): Integer;
 begin
   Result := inherited IndexOf(@AKey);