diff --git a/.gitattributes b/.gitattributes index cbb6516fd8..44282d5103 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2785,6 +2785,7 @@ components/lazutils/laz_xmlwrite.pas svneol=native#text/pascal components/lazutils/lazclasses.pas svneol=native#text/pascal components/lazutils/lazconfigstorage.pas svneol=native#text/pascal components/lazutils/lazdbglog.pas svneol=native#text/pascal +components/lazutils/lazfglhash.pas svneol=native#text/plain components/lazutils/lazfilecache.pas svneol=native#text/pascal components/lazutils/lazfileutils.inc svneol=native#text/plain components/lazutils/lazfileutils.pas svneol=native#text/pascal diff --git a/components/lazutils/lazfglhash.pas b/components/lazutils/lazfglhash.pas new file mode 100644 index 0000000000..48c9c6e725 --- /dev/null +++ b/components/lazutils/lazfglhash.pas @@ -0,0 +1,120 @@ +unit lazfglhash; + +{$mode objfpc}{$H+} + +interface + +uses + contnrs; + +type + generic TLazHTGNode = Class(THTCustomNode) + Private + FData : T; + public + property Data: T read FData write FData; + end; + + { TLazFPGHashTable } + + generic TLazFPGHashTable = Class(TFPCustomHashTable) + Protected + type + THTGNode = Class(THTCustomNode) + Private + FData : T; + public + property Data: T read FData write FData; + end; + TGIteratorMethod = Procedure(Item: T; const Key: string; var Continue: Boolean) of object; + Function CreateNewNode(const aKey : String) : THTCustomNode; override; + Procedure AddNode(ANode : THTCustomNode); override; + Procedure SetData(const Index: string; AValue: T); virtual; + Function GetData(const index: string): T; virtual; + {$if not(defined(ver2) or defined(ver3_0))} + Function ForEachCall(aMethod: TGIteratorMethod): THTGNode; virtual; + {$endif} + Public + {$if not(defined(ver2) or defined(ver3_0))} + Function Iterate(aMethod: TGIteratorMethod): T; virtual; + {$endif} + Procedure Add(const aKey: string; const aItem: T); virtual; + property Items[const index: string]: T read GetData write SetData; default; + end; + + +implementation + +{ TFPGHashTable } + +function TLazFPGHashTable.CreateNewNode(const aKey: String): THTCustomNode; +begin + Result:=THTGNode.CreateWith(aKey); +end; + +procedure TLazFPGHashTable.AddNode(ANode: THTCustomNode); +begin + with THTGNode(ANode) do + Add(Key,Data); +end; + +procedure TLazFPGHashTable.SetData(const Index: string; AValue: T); +begin + THTGNode(FindOrCreateNew(index)).Data:=AValue; +end; + +function TLazFPGHashTable.GetData(const index: string): T; +var + node: THTGNode; +begin + node:=THTGNode(Find(Index)); + if Assigned(node) then + Result:=node.Data; +end; + +{$if not(defined(ver2) or defined(ver3_0))} +function TLazFPGHashTable.ForEachCall(aMethod: TGIteratorMethod): THTGNode; +var + i, j: Longword; + continue: boolean; +begin + Result:=nil; + continue:=True; + if FHashTableSize>0 then + for i:=0 to FHashTableSize-1 do + if Assigned(Chain(i)) then + if chain(i).Count>0 then + for j:=0 to Chain(i).Count-1 do + begin + aMethod(THTGNode(Chain(i)[j]).Data, THTGNode(Chain(i)[j]).Key, continue); + if not continue then + begin + Result:=THTGNode(Chain(i)[j]); + Exit; + end; + end; +end; + +function TLazFPGHashTable.Iterate(aMethod: TGIteratorMethod): T; +var + N : THTGNode; +begin + N:=ForEachCall(AMethod); + if Assigned(N) then + Result:=N.Data +end; +{$endif} + +procedure TLazFPGHashTable.Add(const aKey: string; const aItem: T); +var + chn: TFPObjectList; + NewNode: THTGNode; +begin + chn:=FindChainForAdd(akey); + NewNode:=THTGNode(CreateNewNode(aKey)); + NewNode.Data:=aItem; + chn.Add(NewNode); +end; + +end. + diff --git a/components/lazutils/lazutils.lpk b/components/lazutils/lazutils.lpk index 3efada4ab4..11414c7079 100644 --- a/components/lazutils/lazutils.lpk +++ b/components/lazutils/lazutils.lpk @@ -16,7 +16,7 @@ - + @@ -316,12 +316,16 @@ - + + + + + - + diff --git a/components/lazutils/lazutils.pas b/components/lazutils/lazutils.pas index b8ea31c1dd..618dbbe094 100644 --- a/components/lazutils/lazutils.pas +++ b/components/lazutils/lazutils.pas @@ -16,7 +16,7 @@ uses TTProfile, TTRASTER, TTTables, TTTypes, EasyLazFreeType, LazLoggerBase, LazLoggerDummy, LazClasses, LazFreeTypeFontCollection, LazConfigStorage, UTF8Process, laz2_xpath, DictionaryStringList, LazLoggerProfiling, FPCAdds, - LazUtilities, lcsvutils, LazarusPackageIntf; + LazUtilities, lazfglhash, lcsvutils, LazarusPackageIntf; implementation