* sets enumerators

git-svn-id: trunk@42915 -
This commit is contained in:
marco 2019-09-03 18:10:53 +00:00
parent e32aeb86c0
commit 11203bcdeb
2 changed files with 67 additions and 6 deletions

View File

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

View File

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