Explorar el Código

* sets enumerators

git-svn-id: trunk@42915 -
marco hace 5 años
padre
commit
11203bcdeb
Se han modificado 2 ficheros con 67 adiciones y 6 borrados
  1. 47 4
      packages/fcl-stl/src/ghashset.pp
  2. 20 2
      packages/fcl-stl/src/gset.pp

+ 47 - 4
packages/fcl-stl/src/ghashset.pp

@@ -23,14 +23,22 @@ const baseFDataSize = 8;
 value in range <0,n-1> base only on arguments, n will be always power of 2}
 
 type
+
+    { THashSetIterator }
+
     generic THashSetIterator<T, TTable>=class
     public
+    Type
+      TLHashSetIterator = specialize THashSetIterator<T, TTable>;
     var
       Fh,Fp:SizeUInt;
       FData:TTable;
       function Next:boolean;
+      function MoveNext:boolean; inline;
       function GetData:T;
+      function GetEnumerator: TLHashSetIterator; inline;
       property Data:T read GetData;
+      property Current:T read GetData;
  end;
 
   generic THashSet<T, Thash>=class
@@ -52,18 +60,18 @@ type
       function size:SizeUInt;inline;
       procedure delete(value:T);inline;
       function IsEmpty:boolean;inline;
-
+      function GetEnumerator: TIterator; inline;
       function Iterator:TIterator;
   end;
 
 implementation
 
-function THashSet.Size:SizeUInt;inline;
+function THashSet.size: SizeUInt;
 begin
   Size:=FDataSize;
 end;
 
-destructor THashSet.Destroy;
+destructor THashSet.destroy;
 var i:SizeUInt;
 begin
   for i:=0 to FData.size-1 do
@@ -71,7 +79,7 @@ begin
   FData.Destroy;
 end;
 
-function THashSet.IsEmpty():boolean;inline;
+function THashSet.IsEmpty: boolean;
 begin
   if Size()=0 then 
     IsEmpty:=true
@@ -79,6 +87,22 @@ begin
     IsEmpty:=false;
 end;
 
+function THashSet.GetEnumerator: TIterator;
+  var h,p:SizeUInt;
+begin
+  h:=0;
+  p:=0;
+  while h < FData.size do begin
+    if ((FData[h]).size > 0) then break;
+    inc(h);
+  end;
+  if (h = FData.size) then exit(nil);
+  result := TIterator.create;
+  result.Fh := h;
+  result.Fp := p;
+  result.FData := FData;
+end;
+
 procedure THashSet.EnlargeTable;
 var i,j,h,oldDataSize:SizeUInt; 
     value:T;
@@ -163,11 +187,30 @@ begin
   Next := true;
 end;
 
+function THashSetIterator.MoveNext: boolean;
+begin
+  inc(Fp);
+  if (Fp = (FData[Fh]).size) then begin
+    Fp:=0; inc(Fh);
+    while Fh < FData.size do begin
+      if ((FData[Fh]).size > 0) then break;
+      inc(Fh);
+    end;
+    if (Fh = FData.size) then exit(false);
+  end;
+  MoveNext := true;
+end;
+
 function THashSetIterator.GetData:T;
 begin
   GetData:=(FData[Fh])[Fp];
 end;
 
+function THashSetIterator.GetEnumerator: TLHashSetIterator;
+begin
+  result:=self;
+end;
+
 function THashSet.Iterator:TIterator;
 var h,p:SizeUInt;
 begin

+ 20 - 2
packages/fcl-stl/src/gset.pp

@@ -20,14 +20,22 @@ const RED=true;
 const BLACK=false;
 
 type
+
+  { TSetIterator }
+
   generic TSetIterator<T, TNode>=class
     public
     type PNode=^TNode;
+         TLSetIterator = specialize TSetIterator<T, TNode>;
+
     var FNode:PNode;
-    function GetData:T;
+    function GetData:T; Inline;
     function Next:boolean;
+    function MoveNext:boolean; Inline;
+    function GetEnumerator : TLSetIterator; Inline;
     function Prev:boolean;
     property Data:T read GetData;
+    property Current:T read GetData;
   end;
 
   generic TSet<T, TCompare>=class
@@ -502,6 +510,11 @@ begin
 end;
 
 function TSetIterator.Next:boolean;
+begin
+  Result:=MoveNext;
+end;
+
+function TSetIterator.MoveNext: boolean;
 var temp:PNode;
 begin
   if(FNode=nil) then exit(false);
@@ -519,7 +532,12 @@ begin
   end;
   if (temp = nil) then exit(false);
   FNode:=temp;
-  Next:=true;
+  Result:=true;
+end;
+
+function TSetIterator.GetEnumerator: TLSetIterator;
+begin
+  result:=self;
 end;
 
 function TSetIterator.Prev:boolean;