* and some iterator work for gmap

git-svn-id: trunk@42910 -
This commit is contained in:
marco 2019-09-03 12:22:58 +00:00
parent 534d7b0943
commit a5aa058693
2 changed files with 44 additions and 2 deletions
packages/fcl-stl

View File

@ -4,7 +4,7 @@ type lesslli=specialize TLess<longint>;
maplli=specialize TMap<longint, longint, lesslli>;
var data:maplli; i:longint; iterator:maplli.TIterator;
pair : maplli.TPair;
begin
data:=maplli.Create;
@ -14,7 +14,7 @@ begin
writeln(data[7]);
data[7] := 42;
{Iteration through elements}
{Iteration through elements with write access}
iterator:=data.Min;
repeat
writeln(iterator.Key, ' ', iterator.Value);
@ -22,6 +22,10 @@ begin
until not iterator.next;
iterator.Destroy;
// using for..in to check everything changed to 47. For in is shorter and autoallocated, but can't write to cells via iterator.
for pair in data.min do
writeln('Min: ',pair.Key, ' ', pair.Value);
iterator := data.FindLess(7);
writeln(iterator.Value);
iterator.Destroy;

View File

@ -23,9 +23,12 @@ type
class function c(a,b :TPair):boolean;
end;
{ TMapIterator }
generic TMapIterator<TKey, TValue, TPair, TNode>=class
public
type PNode=^TNode;
TLMapIterator = specialize TMapIterator<TKey, TValue, TPair, TNode>;
var FNode:PNode;
type PValue=^TValue;
function GetData:TPair;inline;
@ -33,12 +36,15 @@ type
function GetValue:TValue;inline;
function GetMutable:PValue;inline;
procedure SetValue(value:TValue);inline;
function MoveNext:boolean;inline;
function Next:boolean;inline;
function Prev:boolean;inline;
function GetEnumerator: TLMapIterator; inline;
property Data:TPair read GetData;
property Key:TKey read GetKey;
property Value:TValue read GetValue write SetValue;
property MutableValue:PValue read GetMutable;
property Current : TPair read GetData;
end;
generic TMap<TKey, TValue, TCompare>=class
@ -71,6 +77,7 @@ type
procedure Delete(key:TKey);inline;
function Size:SizeUInt;inline;
function IsEmpty:boolean;inline;
function GetEnumerator: TIterator; inline;
constructor Create;
destructor Destroy;override;
property Items[i : TKey]: TValue read GetValue write Insert; default;
@ -227,6 +234,11 @@ begin
IsEmpty:=FSet.IsEmpty;
end;
function TMap.GetEnumerator: TIterator;
begin
result:=titerator.create;
end;
function TMapIterator.GetData:TPair;inline;
begin
GetData:=FNode^.Data;
@ -252,6 +264,27 @@ begin
FNode^.Data.Value := value;
end;
function TMapIterator.MoveNext: boolean;
var temp:PNode;
begin
if(FNode=nil) then exit(false);
if(FNode^.Right<>nil) then begin
temp:=FNode^.Right;
while(temp^.Left<>nil) do temp:=temp^.Left;
end
else begin
temp:=FNode;
while(true) do begin
if(temp^.Parent=nil) then begin temp:=temp^.Parent; break; end;
if(temp^.Parent^.Left=temp) then begin temp:=temp^.Parent; break; end;
temp:=temp^.Parent;
end;
end;
if (temp = nil) then exit(false);
FNode:=temp;
MoveNext:=true;
end;
function TMapIterator.Next:boolean;inline;
var temp:PNode;
begin
@ -294,4 +327,9 @@ begin
Prev:=true;
end;
function TMapIterator.GetEnumerator: TLMapIterator;
begin
result:=Self;
end;
end.