diff options
author | marco <marco@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2019-09-03 12:22:58 +0000 |
---|---|---|
committer | marco <marco@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2019-09-03 12:22:58 +0000 |
commit | 4c6b9330b37b2a8636f03b37a7074a8d92aa0045 (patch) | |
tree | 25aa7566408de694b4767e91682bcc567065181b /packages/fcl-stl | |
parent | ac16d1e5ac6145040ce428a78e547a26c8b16632 (diff) | |
download | fpc-4c6b9330b37b2a8636f03b37a7074a8d92aa0045.tar.gz |
* and some iterator work for gmap
git-svn-id: https://svn.freepascal.org/svn/fpc/trunk@42910 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'packages/fcl-stl')
-rw-r--r-- | packages/fcl-stl/doc/mapexample.pp | 8 | ||||
-rw-r--r-- | packages/fcl-stl/src/gmap.pp | 38 |
2 files changed, 44 insertions, 2 deletions
diff --git a/packages/fcl-stl/doc/mapexample.pp b/packages/fcl-stl/doc/mapexample.pp index 271582663d..49bfff6636 100644 --- a/packages/fcl-stl/doc/mapexample.pp +++ b/packages/fcl-stl/doc/mapexample.pp @@ -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; diff --git a/packages/fcl-stl/src/gmap.pp b/packages/fcl-stl/src/gmap.pp index 1e42a4b297..2d6e1a9510 100644 --- a/packages/fcl-stl/src/gmap.pp +++ b/packages/fcl-stl/src/gmap.pp @@ -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. |