summaryrefslogtreecommitdiff
path: root/packages/fcl-stl/src/gmap.pp
diff options
context:
space:
mode:
authorflorian <florian@3ad0048d-3df7-0310-abae-a5850022a9f2>2011-04-10 19:20:48 +0000
committerflorian <florian@3ad0048d-3df7-0310-abae-a5850022a9f2>2011-04-10 19:20:48 +0000
commit160cc1e115eeb75638dce6effdd16b2bc810ddb4 (patch)
treeb791a95695a7cf674e61a6153139c6f9c6c491fa /packages/fcl-stl/src/gmap.pp
parent3843727e74b31bbf2a34e7e3b89ee422269f770e (diff)
parent413a6aa6469e6c297780217a27ca91363c637944 (diff)
downloadfpc-avr.tar.gz
* rebase to trunk@17295avr
git-svn-id: http://svn.freepascal.org/svn/fpc/branches/avr@17296 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'packages/fcl-stl/src/gmap.pp')
-rw-r--r--packages/fcl-stl/src/gmap.pp163
1 files changed, 163 insertions, 0 deletions
diff --git a/packages/fcl-stl/src/gmap.pp b/packages/fcl-stl/src/gmap.pp
new file mode 100644
index 0000000000..fb64db26b5
--- /dev/null
+++ b/packages/fcl-stl/src/gmap.pp
@@ -0,0 +1,163 @@
+{
+ This file is part of the Free Pascal FCL library.
+ BSD parts (c) 2011 Vlado Boza
+
+ See the file COPYING.FPC, included in this distribution,
+ for details about the copyright.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY;without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+**********************************************************************}
+{$mode objfpc}
+
+unit gmap;
+
+interface
+
+uses gset;
+
+type
+ generic TMapCompare<TPair, TKeyCompare>=class
+ class function c(a,b :TPair):boolean;
+ end;
+
+ generic TMap<TKey, TValue, TCompare>=class
+ public
+ type
+ TPair=record
+ Value:TValue;
+ Key:TKey;
+ end;
+ TMCompare = specialize TMapCompare<TPair, TCompare>;
+ TMSet = specialize TSet<TPair, TMCompare>;
+ PTValue = ^TValue;
+ PTPair = ^TPair;
+ var
+ private
+ FSet:TMSet;
+ public
+ function Find(key:TKey):TMSet.PNode;inline;
+ function FindLess(key:TKey):TMSet.PNode;inline;
+ function FindLessEqual(key:TKey):TMSet.PNode;inline;
+ function FindGreater(key:TKey):TMSet.PNode;inline;
+ function FindGreaterEqual(key:TKey):TMSet.PNode;inline;
+ function GetValue(key:TKey):TValue;inline;
+ procedure Insert(key:TKey; value:TValue);inline;
+ function Min:TMSet.PNode;inline;
+ function Max:TMSet.PNode;inline;
+ function Next(x:TMSet.PNode):TMSet.PNode;inline;
+ function Prev(x:TMSet.PNode):TMSet.PNode;inline;
+ procedure Delete(key:TKey);inline;
+ function Size:SizeUInt;inline;
+ function IsEmpty:boolean;inline;
+ constructor Create;
+ destructor Destroy;override;
+ property Items[i : TKey]: TValue read GetValue write Insert; default;
+ end;
+
+implementation
+
+class function TMapCompare.c(a,b: TPair):boolean;
+begin
+ c:= TKeyCompare.c(a.Key, b.Key);
+end;
+
+constructor TMap.Create;
+begin
+ FSet:=TMSet.Create;
+end;
+
+destructor TMap.Destroy;
+begin
+ FSet.Destroy;
+end;
+
+procedure TMap.Delete(key:TKey);inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ FSet.Delete(Pair);
+end;
+
+function TMap.Find(key:TKey):TMSet.PNode;inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ Find:=FSet.Find(Pair);
+end;
+
+function TMap.FindLess(key:TKey):TMSet.PNode;inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ FindLess:=FSet.FindLess(Pair);
+end;
+
+function TMap.FindLessEqual(key:TKey):TMSet.PNode;inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ FindLessEqual:=FSet.FindLessEqual(Pair);
+end;
+
+function TMap.FindGreater(key:TKey):TMSet.PNode;inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ FindGreater:=FSet.FindGreater(Pair);
+end;
+
+function TMap.FindGreaterEqual(key:TKey):TMSet.PNode;inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ FindGreaterEqual:=FSet.FindGreaterEqual(Pair);
+end;
+
+function TMap.GetValue(key:TKey):TValue;inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ GetValue:=FSet.Find(Pair)^.Data.Value;
+end;
+
+procedure TMap.Insert(key:TKey; value:TValue);inline;
+var Pair:TPair;
+begin
+ Pair.Key:=key;
+ FSet.Insert(Pair)^.Data.Value := value;
+end;
+
+function TMap.Min:TMSet.PNode;inline;
+begin
+ Min:=FSet.Min;
+end;
+
+function TMap.Max:TMSet.PNode;inline;
+begin
+ Max:=FSet.Max;
+end;
+
+function TMap.Next(x:TMSet.PNode):TMSet.PNode;inline;
+begin
+ Next:=FSet.Next(x);
+end;
+
+function TMap.Prev(x:TMSet.PNode):TMSet.PNode;inline;
+begin
+ Prev:=FSet.Prev(x);
+end;
+
+function TMap.Size:SizeUInt;inline;
+begin
+ Size:=FSet.Size;
+end;
+
+function TMap.IsEmpty:boolean;inline;
+begin
+ IsEmpty:=FSet.IsEmpty;
+end;
+
+end.