فهرست منبع

GetMutableValue and TryGetMutableValue for fcl-stl.TMap and rtl-generics.generics.TDictionary

zamtmn 1 سال پیش
والد
کامیت
b3e8d88f68

+ 22 - 0
packages/fcl-stl/src/gmap.pp

@@ -75,7 +75,9 @@ type
     function FindGreater(key:TKey):TIterator;inline;
     function FindGreaterEqual(key:TKey):TIterator;inline;
     function GetValue(key:TKey):TValue;inline;
+    function GetMutableValue(key:TKey):PTValue;inline;
     function TryGetValue(key:TKey; out Value: TValue): boolean;inline;
+    function TryGetMutableValue(key:TKey;out pvalue:PTValue):boolean;inline;
     procedure Insert(key:TKey; value:TValue);inline;
     function InsertAndGetIterator(key:TKey; value:TValue):TIterator;inline;
     function Min:TIterator;inline;
@@ -180,6 +182,18 @@ begin
   GetValue:=FSet.NFind(Pair)^.Data.Value;
 end;
 
+function TMap.GetMutableValue(key:TKey):PTValue;inline;
+var Pair:TPair;
+    Node:TMSet.PNode;
+begin
+ Pair.Key:=key;
+ Node:=FSet.NFind(Pair);
+ if Node=nil then
+   result:=nil
+ else
+   result:=@Node^.Data.Value;
+end;
+
 function TMap.TryGetValue(key: TKey; out Value: TValue): boolean;
 var Pair:TPair;
     Node: TMSet.PNode;
@@ -191,6 +205,14 @@ begin
     Value := Node^.Data.Value;
 end;
 
+function TMap.TryGetMutableValue(key:TKey;out pvalue:PTValue):boolean;
+var Pair:TPair;
+    Node:TMSet.PNode;
+begin
+  pvalue:=GetMutableValue(key);
+  Result:=pvalue<>nil;
+end;
+
 procedure TMap.Insert(key:TKey; value:TValue);inline;
 var Pair:TPair;
 begin

+ 18 - 0
packages/rtl-generics/src/inc/generics.dictionaries.inc

@@ -607,6 +607,24 @@ begin
   SetValue(FItems[LIndex].Pair.Value, AValue);
 end;
 
+function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetMutableValue(const AKey: TKey): PValue;
+var
+  LIndex: SizeInt;
+  LHash: UInt32;
+begin
+  LIndex := FindBucketIndex(FItems, AKey, LHash);
+  if LIndex < 0 then
+    Result := Nil
+  else
+    Result := @FItems[LIndex].Pair.Value;
+end;
+
+function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TryGetMutableValue(const AKey: TKey; out APValue: PValue): Boolean;
+begin
+  APValue := GetMutableValue(AKey);
+  Result := APValue <>Nil;
+end;
+
 function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TryGetValue(const AKey: TKey; out AValue: TValue): Boolean;
 var
   LIndex: SizeInt;

+ 2 - 0
packages/rtl-generics/src/inc/generics.dictionariesh.inc

@@ -261,6 +261,8 @@ type
     function ExtractPair(const AKey: TKey): TPair<TKey, TValue>;
     procedure Clear; override;
     procedure TrimExcess;
+    function GetMutableValue(const AKey: TKey): PValue; inline;
+    function TryGetMutableValue(const AKey: TKey; out APValue: PValue): Boolean;
     function TryGetValue(const AKey: TKey; out AValue: TValue): Boolean;
     function TryAdd(const AKey: TKey; const AValue: TValue): Boolean;
     procedure AddOrSetValue(const AKey: TKey; const AValue: TValue);