mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-12 11:06:19 +02:00
* and some iterator work for gmap
git-svn-id: trunk@42910 -
This commit is contained in:
parent
534d7b0943
commit
a5aa058693
@ -4,7 +4,7 @@ type lesslli=specialize TLess<longint>;
|
|||||||
maplli=specialize TMap<longint, longint, lesslli>;
|
maplli=specialize TMap<longint, longint, lesslli>;
|
||||||
|
|
||||||
var data:maplli; i:longint; iterator:maplli.TIterator;
|
var data:maplli; i:longint; iterator:maplli.TIterator;
|
||||||
|
pair : maplli.TPair;
|
||||||
begin
|
begin
|
||||||
data:=maplli.Create;
|
data:=maplli.Create;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ begin
|
|||||||
writeln(data[7]);
|
writeln(data[7]);
|
||||||
data[7] := 42;
|
data[7] := 42;
|
||||||
|
|
||||||
{Iteration through elements}
|
{Iteration through elements with write access}
|
||||||
iterator:=data.Min;
|
iterator:=data.Min;
|
||||||
repeat
|
repeat
|
||||||
writeln(iterator.Key, ' ', iterator.Value);
|
writeln(iterator.Key, ' ', iterator.Value);
|
||||||
@ -22,6 +22,10 @@ begin
|
|||||||
until not iterator.next;
|
until not iterator.next;
|
||||||
iterator.Destroy;
|
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);
|
iterator := data.FindLess(7);
|
||||||
writeln(iterator.Value);
|
writeln(iterator.Value);
|
||||||
iterator.Destroy;
|
iterator.Destroy;
|
||||||
|
@ -23,9 +23,12 @@ type
|
|||||||
class function c(a,b :TPair):boolean;
|
class function c(a,b :TPair):boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TMapIterator }
|
||||||
|
|
||||||
generic TMapIterator<TKey, TValue, TPair, TNode>=class
|
generic TMapIterator<TKey, TValue, TPair, TNode>=class
|
||||||
public
|
public
|
||||||
type PNode=^TNode;
|
type PNode=^TNode;
|
||||||
|
TLMapIterator = specialize TMapIterator<TKey, TValue, TPair, TNode>;
|
||||||
var FNode:PNode;
|
var FNode:PNode;
|
||||||
type PValue=^TValue;
|
type PValue=^TValue;
|
||||||
function GetData:TPair;inline;
|
function GetData:TPair;inline;
|
||||||
@ -33,12 +36,15 @@ type
|
|||||||
function GetValue:TValue;inline;
|
function GetValue:TValue;inline;
|
||||||
function GetMutable:PValue;inline;
|
function GetMutable:PValue;inline;
|
||||||
procedure SetValue(value:TValue);inline;
|
procedure SetValue(value:TValue);inline;
|
||||||
|
function MoveNext:boolean;inline;
|
||||||
function Next:boolean;inline;
|
function Next:boolean;inline;
|
||||||
function Prev:boolean;inline;
|
function Prev:boolean;inline;
|
||||||
|
function GetEnumerator: TLMapIterator; 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;
|
property MutableValue:PValue read GetMutable;
|
||||||
|
property Current : TPair read GetData;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
generic TMap<TKey, TValue, TCompare>=class
|
generic TMap<TKey, TValue, TCompare>=class
|
||||||
@ -71,6 +77,7 @@ type
|
|||||||
procedure Delete(key:TKey);inline;
|
procedure Delete(key:TKey);inline;
|
||||||
function Size:SizeUInt;inline;
|
function Size:SizeUInt;inline;
|
||||||
function IsEmpty:boolean;inline;
|
function IsEmpty:boolean;inline;
|
||||||
|
function GetEnumerator: TIterator; inline;
|
||||||
constructor Create;
|
constructor Create;
|
||||||
destructor Destroy;override;
|
destructor Destroy;override;
|
||||||
property Items[i : TKey]: TValue read GetValue write Insert; default;
|
property Items[i : TKey]: TValue read GetValue write Insert; default;
|
||||||
@ -227,6 +234,11 @@ begin
|
|||||||
IsEmpty:=FSet.IsEmpty;
|
IsEmpty:=FSet.IsEmpty;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TMap.GetEnumerator: TIterator;
|
||||||
|
begin
|
||||||
|
result:=titerator.create;
|
||||||
|
end;
|
||||||
|
|
||||||
function TMapIterator.GetData:TPair;inline;
|
function TMapIterator.GetData:TPair;inline;
|
||||||
begin
|
begin
|
||||||
GetData:=FNode^.Data;
|
GetData:=FNode^.Data;
|
||||||
@ -252,6 +264,27 @@ begin
|
|||||||
FNode^.Data.Value := value;
|
FNode^.Data.Value := value;
|
||||||
end;
|
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;
|
function TMapIterator.Next:boolean;inline;
|
||||||
var temp:PNode;
|
var temp:PNode;
|
||||||
begin
|
begin
|
||||||
@ -294,4 +327,9 @@ begin
|
|||||||
Prev:=true;
|
Prev:=true;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TMapIterator.GetEnumerator: TLMapIterator;
|
||||||
|
begin
|
||||||
|
result:=Self;
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
Loading…
Reference in New Issue
Block a user