Pārlūkot izejas kodu

mutable value iterators fcl-stl

git-svn-id: trunk@17355 -
vladob 14 gadi atpakaļ
vecāks
revīzija
983c3633cd

+ 9 - 1
packages/fcl-stl/src/ghashmap.pp

@@ -25,6 +25,7 @@
   type
   type
     generic THashmapIterator<TKey, TValue, T, TTable>=class
     generic THashmapIterator<TKey, TValue, T, TTable>=class
       public
       public
+      type PValue=^TValue;
       var
       var
         Fh,Fp:SizeUInt;
         Fh,Fp:SizeUInt;
         FData:TTable;
         FData:TTable;
@@ -32,10 +33,12 @@
         function GetData:T;inline;
         function GetData:T;inline;
         function GetKey:TKey;inline;
         function GetKey:TKey;inline;
         function GetValue:TValue;inline;
         function GetValue:TValue;inline;
+        function GetMutable:PValue;inline;
         procedure SetValue(value:TValue);inline;
         procedure SetValue(value:TValue);inline;
         property Data:T read GetData;
         property Data:T read GetData;
         property Key:TKey read GetKey;
         property Key:TKey read GetKey;
         property Value:TValue read GetValue write SetValue;
         property Value:TValue read GetValue write SetValue;
+        property MutableValue:PValue read GetMutable;
     end;
     end;
 
 
     generic THashmap<TKey, TValue, Thash>=class
     generic THashmap<TKey, TValue, Thash>=class
@@ -164,7 +167,7 @@ begin
   inc(FDataSize);
   inc(FDataSize);
   (FData[h]).pushback(pair);
   (FData[h]).pushback(pair);
 
 
-  if (FDataSize > 2*FData.size) then
+  if (FDataSize > 5*FData.size) then
     EnlargeTable;
     EnlargeTable;
 end;
 end;
 
 
@@ -229,6 +232,11 @@ begin
   GetValue:=((FData[Fh])[Fp]).Value;
   GetValue:=((FData[Fh])[Fp]).Value;
 end;
 end;
 
 
+function THashmapIterator.GetMutable:PValue;inline;
+begin
+  GetMutable:=@((FData[Fh]).Mutable[Fp]^.Value);
+end;
+
 procedure THashmapIterator.SetValue(value:TValue);inline;
 procedure THashmapIterator.SetValue(value:TValue);inline;
 begin
 begin
   ((FData[Fh]).mutable[Fp])^.Value := value;
   ((FData[Fh]).mutable[Fp])^.Value := value;

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

@@ -27,15 +27,18 @@ type
     public
     public
     type PNode=^TNode;
     type PNode=^TNode;
     var FNode:PNode;
     var FNode:PNode;
+    type PValue=^TValue;
     function GetData:TPair;inline;
     function GetData:TPair;inline;
     function GetKey:TKey;inline;
     function GetKey:TKey;inline;
     function GetValue:TValue;inline;
     function GetValue:TValue;inline;
+    function GetMutable:PValue;inline;
     procedure SetValue(value:TValue);inline;
     procedure SetValue(value:TValue);inline;
     function Next:boolean;inline;
     function Next:boolean;inline;
     function Prev:boolean;inline;
     function Prev:boolean;inline;
     property Data:TPair read GetData;
     property Data:TPair read GetData;
     property Key:TKey read GetKey;
     property Key:TKey read GetKey;
     property Value:TValue read GetValue write SetValue;
     property Value:TValue read GetValue write SetValue;
+    property MutableValue:PValue read GetMutable;
   end;
   end;
 
 
   generic TMap<TKey, TValue, TCompare>=class
   generic TMap<TKey, TValue, TCompare>=class
@@ -227,6 +230,11 @@ begin
   GetValue:=FNode^.Data.Value;
   GetValue:=FNode^.Data.Value;
 end;
 end;
 
 
+function TMapIterator.GetMutable:PValue;inline;
+begin
+  GetMutable:=@(FNode^.Data.Value);
+end;
+
 procedure TMapIterator.SetValue(value:TValue);inline;
 procedure TMapIterator.SetValue(value:TValue);inline;
 begin
 begin
   FNode^.Data.Value := value;
   FNode^.Data.Value := value;

+ 2 - 0
packages/fcl-stl/tests/ghashmaptest.pp

@@ -87,6 +87,8 @@ begin
     it.Value := it.Key+23;
     it.Value := it.Key+23;
     it.Value := it.Value*2;
     it.Value := it.Value*2;
     AssertEquals('bad value3', it.Key*2+46, it.Value);
     AssertEquals('bad value3', it.Key*2+46, it.Value);
+    it.MutableValue^ := 222;
+    AssertEquals('bad value4', 222, it.Value);
   until not it.next;
   until not it.next;
   for i:=0 to 1000 do begin
   for i:=0 to 1000 do begin
     AssertEquals('som not 1', 1, x[i]);
     AssertEquals('som not 1', 1, x[i]);

+ 2 - 0
packages/fcl-stl/tests/gmaptest.pp

@@ -56,6 +56,8 @@ begin
   AssertEquals('Wrong next value', 5, it.Value);
   AssertEquals('Wrong next value', 5, it.Value);
   it.Value := it.Value + 17;
   it.Value := it.Value + 17;
   AssertEquals('Wrong value update', 22, it.Value);
   AssertEquals('Wrong value update', 22, it.Value);
+  it.MutableValue^:= 444;
+  AssertEquals('Wrong mutable value update', 444, it.Value);
   AssertEquals('Next not true', true, it.Next);
   AssertEquals('Next not true', true, it.Next);
   AssertEquals('Wrong next', 7, it.GetData.key);
   AssertEquals('Wrong next', 7, it.GetData.key);
   AssertEquals('Next not true', true, it.Next);
   AssertEquals('Next not true', true, it.Next);