123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739 |
- {
- This file is part of the Free Component Library (FCL)
- Copyright (c) 2002 by Florian Klaempfl
- 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.
- **********************************************************************}
- {$ifdef fpc}
- {$mode objfpc}
- {$endif}
- {$H+}
- {$ifdef CLASSESINLINE}{$inline on}{$endif}
- unit contnrs;
- interface
- uses
- SysUtils,Classes;
- Type
- TObjectListCallback = procedure(data:TObject;arg:pointer) of object;
- TObjectListStaticCallback = procedure(data:TObject;arg:pointer);
- TFPObjectList = class(TObject)
- private
- FFreeObjects : Boolean;
- FList: TFPList;
- function GetCount: integer;
- procedure SetCount(const AValue: integer);
- protected
- function GetItem(Index: Integer): TObject; {$ifdef CLASSESINLINE}inline;{$endif}
- procedure SetItem(Index: Integer; AObject: TObject); {$ifdef CLASSESINLINE}inline;{$endif}
- procedure SetCapacity(NewCapacity: Integer);
- function GetCapacity: integer;
- public
- constructor Create;
- constructor Create(FreeObjects : Boolean);
- destructor Destroy; override;
- procedure Clear;
- function Add(AObject: TObject): Integer; {$ifdef CLASSESINLINE}inline;{$endif}
- procedure Delete(Index: Integer); {$ifdef CLASSESINLINE}inline;{$endif}
- procedure Exchange(Index1, Index2: Integer);
- function Expand: TFPObjectList;
- function Extract(Item: TObject): TObject;
- function Remove(AObject: TObject): Integer;
- function IndexOf(AObject: TObject): Integer;
- function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
- procedure Insert(Index: Integer; AObject: TObject); {$ifdef CLASSESINLINE}inline;{$endif}
- function First: TObject;
- function Last: TObject;
- procedure Move(CurIndex, NewIndex: Integer);
- procedure Assign(Obj:TFPObjectList);
- procedure Pack;
- procedure Sort(Compare: TListSortCompare);
- procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer);
- procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
- property Capacity: Integer read GetCapacity write SetCapacity;
- property Count: Integer read GetCount write SetCount;
- property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
- property Items[Index: Integer]: TObject read GetItem write SetItem; default;
- property List: TFPList read FList;
- end;
- TObjectList = class(TList)
- private
- ffreeobjects : boolean;
- Protected
- Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
- function GetItem(Index: Integer): TObject;
- Procedure SetItem(Index: Integer; AObject: TObject);
- public
- constructor create;
- constructor create(freeobjects : boolean);
- function Add(AObject: TObject): Integer;
- function Extract(Item: TObject): TObject;
- function Remove(AObject: TObject): Integer;
- function IndexOf(AObject: TObject): Integer;
- function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
- Procedure Insert(Index: Integer; AObject: TObject);
- function First: TObject;
- Function Last: TObject;
- property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
- property Items[Index: Integer]: TObject read GetItem write SetItem; default;
- end;
- TComponentList = class(TObjectList)
- Private
- FNotifier : TComponent;
- Protected
- Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
- Function GetItems(Index: Integer): TComponent;
- Procedure SetItems(Index: Integer; AComponent: TComponent);
- Procedure HandleFreeNotify(Sender: TObject; AComponent: TComponent);
- public
- destructor Destroy; override;
- Function Add(AComponent: TComponent): Integer;
- Function Extract(Item: TComponent): TComponent;
- Function Remove(AComponent: TComponent): Integer;
- Function IndexOf(AComponent: TComponent): Integer;
- Function First: TComponent;
- Function Last: TComponent;
- Procedure Insert(Index: Integer; AComponent: TComponent);
- property Items[Index: Integer]: TComponent read GetItems write SetItems; default;
- end;
- TClassList = class(TList)
- protected
- Function GetItems(Index: Integer): TClass;
- Procedure SetItems(Index: Integer; AClass: TClass);
- public
- Function Add(AClass: TClass): Integer;
- Function Extract(Item: TClass): TClass;
- Function Remove(AClass: TClass): Integer;
- Function IndexOf(AClass: TClass): Integer;
- Function First: TClass;
- Function Last: TClass;
- Procedure Insert(Index: Integer; AClass: TClass);
- property Items[Index: Integer]: TClass read GetItems write SetItems; default;
- end;
- TOrderedList = class(TObject)
- private
- FList: TList;
- protected
- Procedure PushItem(AItem: Pointer); virtual; abstract;
- Function PopItem: Pointer; virtual;
- Function PeekItem: Pointer; virtual;
- property List: TList read FList;
- public
- constructor Create;
- destructor Destroy; override;
- Function Count: Integer;
- Function AtLeast(ACount: Integer): Boolean;
- Function Push(AItem: Pointer): Pointer;
- Function Pop: Pointer;
- Function Peek: Pointer;
- end;
- { TStack class }
- TStack = class(TOrderedList)
- protected
- Procedure PushItem(AItem: Pointer); override;
- end;
- { TObjectStack class }
- TObjectStack = class(TStack)
- public
- Function Push(AObject: TObject): TObject;
- Function Pop: TObject;
- Function Peek: TObject;
- end;
- { TQueue class }
- TQueue = class(TOrderedList)
- protected
- Procedure PushItem(AItem: Pointer); override;
- end;
- { TObjectQueue class }
- TObjectQueue = class(TQueue)
- public
- Function Push(AObject: TObject): TObject;
- Function Pop: TObject;
- Function Peek: TObject;
- end;
- { ---------------------------------------------------------------------
- TFPList with Hash support
- ---------------------------------------------------------------------}
- type
- THashItem=record
- HashValue : LongWord;
- StrIndex : Integer;
- NextIndex : Integer;
- Data : Pointer;
- end;
- PHashItem=^THashItem;
- const
- MaxHashListSize = Maxint div 16;
- MaxHashStrSize = Maxint;
- MaxHashTableSize = Maxint div 4;
- MaxItemsPerHash = 3;
- type
- PHashItemList = ^THashItemList;
- THashItemList = array[0..MaxHashListSize - 1] of THashItem;
- PHashTable = ^THashTable;
- THashTable = array[0..MaxHashTableSize - 1] of Integer;
- TFPHashList = class(TObject)
- private
- { ItemList }
- FHashList : PHashItemList;
- FCount,
- FCapacity : Integer;
- { Hash }
- FHashTable : PHashTable;
- FHashCapacity : Integer;
- { Strings }
- FStrs : PChar;
- FStrCount,
- FStrCapacity : Integer;
- function InternalFind(AHash:LongWord;const AName:shortstring;out PrevIndex:Integer):Integer;
- protected
- function Get(Index: Integer): Pointer; {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure Put(Index: Integer; Item: Pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure SetCapacity(NewCapacity: Integer);
- procedure SetCount(NewCount: Integer);
- Procedure RaiseIndexError(Index : Integer);
- function AddStr(const s:shortstring): Integer;
- procedure AddToHashTable(Index: Integer);
- procedure StrExpand(MinIncSize:Integer);
- procedure SetStrCapacity(NewCapacity: Integer);
- procedure SetHashCapacity(NewCapacity: Integer);
- procedure ReHash;
- public
- constructor Create;
- destructor Destroy; override;
- function Add(const AName:shortstring;Item: Pointer): Integer;
- procedure Clear;
- function NameOfIndex(Index: Integer): ShortString; {$ifdef CCLASSESINLINE}inline;{$endif}
- function HashOfIndex(Index: Integer): LongWord; {$ifdef CCLASSESINLINE}inline;{$endif}
- function GetNextCollision(Index: Integer): Integer;
- procedure Delete(Index: Integer);
- class procedure Error(const Msg: string; Data: PtrInt);
- function Expand: TFPHashList;
- function Extract(item: Pointer): Pointer;
- function IndexOf(Item: Pointer): Integer;
- function Find(const AName:shortstring): Pointer;
- function FindIndexOf(const AName:shortstring): Integer;
- function FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
- function Rename(const AOldName,ANewName:shortstring): Integer;
- function Remove(Item: Pointer): Integer;
- procedure Pack;
- procedure ShowStatistics;
- procedure ForEachCall(proc2call:TListCallback;arg:pointer);
- procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
- property Capacity: Integer read FCapacity write SetCapacity;
- property Count: Integer read FCount write SetCount;
- property Items[Index: Integer]: Pointer read Get write Put; default;
- property List: PHashItemList read FHashList;
- property Strs: PChar read FStrs;
- end;
- {*******************************************************
- TFPHashObjectList (From fcl/inc/contnrs.pp)
- ********************************************************}
- TFPHashObjectList = class;
- { TFPHashObject }
- TFPHashObject = class
- private
- FOwner : TFPHashObjectList;
- FCachedStr : pshortstring;
- FStrIndex : Integer;
- procedure InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:shortstring);
- protected
- function GetName:shortstring;virtual;
- function GetHash:Longword;virtual;
- public
- constructor CreateNotOwned;
- constructor Create(HashObjectList:TFPHashObjectList;const s:shortstring);
- procedure ChangeOwner(HashObjectList:TFPHashObjectList); {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:shortstring); {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure Rename(const ANewName:shortstring);
- property Name:shortstring read GetName;
- property Hash:Longword read GetHash;
- end;
- TFPHashObjectList = class(TObject)
- private
- FFreeObjects : Boolean;
- FHashList: TFPHashList;
- function GetCount: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure SetCount(const AValue: integer); {$ifdef CCLASSESINLINE}inline;{$endif}
- protected
- function GetItem(Index: Integer): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure SetItem(Index: Integer; AObject: TObject); {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure SetCapacity(NewCapacity: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
- function GetCapacity: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
- public
- constructor Create(FreeObjects : boolean = True);
- destructor Destroy; override;
- procedure Clear;
- function Add(const AName:shortstring;AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
- function NameOfIndex(Index: Integer): ShortString; {$ifdef CCLASSESINLINE}inline;{$endif}
- function HashOfIndex(Index: Integer): LongWord; {$ifdef CCLASSESINLINE}inline;{$endif}
- function GetNextCollision(Index: Integer): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure Delete(Index: Integer);
- function Expand: TFPHashObjectList; {$ifdef CCLASSESINLINE}inline;{$endif}
- function Extract(Item: TObject): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
- function Remove(AObject: TObject): Integer;
- function IndexOf(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
- function Find(const s:shortstring): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
- function FindIndexOf(const s:shortstring): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
- function FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
- function Rename(const AOldName,ANewName:shortstring): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
- function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
- procedure Pack; {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure ShowStatistics; {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
- procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
- property Capacity: Integer read GetCapacity write SetCapacity;
- property Count: Integer read GetCount write SetCount;
- property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
- property Items[Index: Integer]: TObject read GetItem write SetItem; default;
- property List: TFPHashList read FHashList;
- end;
- { ---------------------------------------------------------------------
- Hash support, implemented by Dean Zobec
- ---------------------------------------------------------------------}
- { Must return a Longword value in the range 0..TableSize,
- usually via a mod operator; }
- THashFunction = function(const S: string; const TableSize: Longword): Longword;
- { THTNode }
- THTCustomNode = class(TObject)
- private
- FKey: string;
- public
- constructor CreateWith(const AString: String);
- function HasKey(const AKey: string): boolean;
- property Key: string read FKey;
- end;
- THTCustomNodeClass = Class of THTCustomNode;
- { TFPCustomHashTable }
- TFPCustomHashTable = class(TObject)
- private
- FHashTable: TFPObjectList;
- FHashTableSize: Longword;
- FHashFunction: THashFunction;
- FCount: Longword;
- function GetDensity: Longword;
- function GetNumberOfCollisions: Longword;
- procedure SetHashTableSize(const Value: Longword);
- procedure InitializeHashTable;
- function GetVoidSlots: Longword;
- function GetLoadFactor: double;
- function GetAVGChainLen: double;
- function GetMaxChainLength: Longword;
- function Chain(const index: Longword):TFPObjectList;
- protected
- Function CreateNewNode(const aKey : string) : THTCustomNode; virtual; abstract;
- Procedure AddNode(ANode : THTCustomNode); virtual; abstract;
- function ChainLength(const ChainIndex: Longword): Longword; virtual;
- function FindOrCreateNew(const aKey: string): THTCustomNode; virtual;
- procedure SetHashFunction(AHashFunction: THashFunction); virtual;
- Function FindChainForAdd(Const aKey : String) : TFPObjectList;
- public
- constructor Create;
- constructor CreateWith(AHashTableSize: Longword; aHashFunc: THashFunction);
- destructor Destroy; override;
- procedure ChangeTableSize(const ANewSize: Longword); virtual;
- procedure Clear; virtual;
- procedure Delete(const aKey: string); virtual;
- function Find(const aKey: string): THTCustomNode;
- function IsEmpty: boolean;
- property HashFunction: THashFunction read FHashFunction write SetHashFunction;
- property Count: Longword read FCount;
- property HashTableSize: Longword read FHashTableSize write SetHashTableSize;
- property HashTable: TFPObjectList read FHashTable;
- property VoidSlots: Longword read GetVoidSlots;
- property LoadFactor: double read GetLoadFactor;
- property AVGChainLen: double read GetAVGChainLen;
- property MaxChainLength: Longword read GetMaxChainLength;
- property NumberOfCollisions: Longword read GetNumberOfCollisions;
- property Density: Longword read GetDensity;
- end;
- { TFPDataHashTable : Hash table with simple data pointers }
- THTDataNode = Class(THTCustomNode)
- Private
- FData: pointer;
- public
- property Data: pointer read FData write FData;
- end;
- // For compatibility
- THTNode = THTDataNode;
- TDataIteratorMethod = procedure(Item: Pointer; const Key: string; var Continue: Boolean) of object;
- // For compatibility
- TIteratorMethod = TDataIteratorMethod;
- TFPDataHashTable = Class(TFPCustomHashTable)
- Protected
- Function CreateNewNode(const aKey : String) : THTCustomNode; override;
- Procedure AddNode(ANode : THTCustomNode); override;
- procedure SetData(const index: string; const AValue: Pointer); virtual;
- function GetData(const index: string):Pointer; virtual;
- function ForEachCall(aMethod: TDataIteratorMethod): THTDataNode; virtual;
- Public
- procedure Add(const aKey: string; AItem: pointer); virtual;
- property Items[const index: string]: Pointer read GetData write SetData; default;
- end;
- { TFPStringHashTable : Hash table with simple strings as data }
- THTStringNode = Class(THTCustomNode)
- Private
- FData : String;
- public
- property Data: String read FData write FData;
- end;
- TStringIteratorMethod = procedure(Item: String; const Key: string; var Continue: Boolean) of object;
- TFPStringHashTable = Class(TFPCustomHashTable)
- Protected
- Function CreateNewNode(const aKey : String) : THTCustomNode; override;
- Procedure AddNode(ANode : THTCustomNode); override;
- procedure SetData(const Index, AValue: string); virtual;
- function GetData(const index: string): String; virtual;
- function ForEachCall(aMethod: TStringIteratorMethod): THTStringNode; virtual;
- Public
- procedure Add(const aKey,aItem: string); virtual;
- property Items[const index: string]: String read GetData write SetData; default;
- end;
- { TFPStringHashTable : Hash table with simple strings as data }
- THTObjectNode = Class(THTCustomNode)
- Private
- FData : TObject;
- public
- property Data: TObject read FData write FData;
- end;
- THTOwnedObjectNode = Class(THTObjectNode)
- public
- Destructor Destroy; override;
- end;
- TObjectIteratorMethod = procedure(Item: TObject; const Key: string; var Continue: Boolean) of object;
- TFPObjectHashTable = Class(TFPCustomHashTable)
- Private
- FOwnsObjects : Boolean;
- Protected
- Function CreateNewNode(const aKey : String) : THTCustomNode; override;
- Procedure AddNode(ANode : THTCustomNode); override;
- procedure SetData(const Index: string; AObject : TObject); virtual;
- function GetData(const index: string): TObject; virtual;
- function ForEachCall(aMethod: TObjectIteratorMethod): THTObjectNode; virtual;
- Public
- constructor Create(AOwnsObjects : Boolean = True);
- constructor CreateWith(AHashTableSize: Longword; aHashFunc: THashFunction; AOwnsObjects : Boolean = True);
- procedure Add(const aKey: string; AItem : TObject); virtual;
- property Items[const index: string]: TObject read GetData write SetData; default;
- Property OwnsObjects : Boolean Read FOwnsObjects Write FOwnsObjects;
- end;
- EDuplicate = class(Exception);
- EKeyNotFound = class(Exception);
- function RSHash(const S: string; const TableSize: Longword): Longword;
- { ---------------------------------------------------------------------
- Bucket lists as in Delphi
- ---------------------------------------------------------------------}
- Type
- TBucketItem = record
- Item, Data: Pointer;
- end;
- TBucketItemArray = array of TBucketItem;
- TBucket = record
- Count : Integer;
- Items : TBucketItemArray;
- end;
- PBucket = ^TBucket;
- TBucketArray = array of TBucket;
- TBucketProc = procedure(AInfo, AItem, AData: Pointer; out AContinue: Boolean);
- TBucketProcObject = procedure(AItem, AData: Pointer; out AContinue: Boolean) of Object;
- { ---------------------------------------------------------------------
- TCustomBucketList
- ---------------------------------------------------------------------}
- { TCustomBucketList }
- TCustomBucketList = class(TObject)
- private
- FBuckets: TBucketArray;
- function GetBucketCount: Integer;
- function GetData(AItem: Pointer): Pointer;
- procedure SetData(AItem: Pointer; const AData: Pointer);
- procedure SetBucketCount(const Value: Integer);
- protected
- Procedure GetBucketItem(AItem: Pointer; out ABucket, AIndex: Integer);
- function AddItem(ABucket: Integer; AItem, AData: Pointer): Pointer; virtual;
- function BucketFor(AItem: Pointer): Integer; virtual; abstract;
- function DeleteItem(ABucket: Integer; AIndex: Integer): Pointer; virtual;
- Procedure Error(Msg : String; Args : Array of Const);
- function FindItem(AItem: Pointer; out ABucket, AIndex: Integer): Boolean; virtual;
- property Buckets: TBucketArray read FBuckets;
- property BucketCount: Integer read GetBucketCount write SetBucketCount;
- public
- destructor Destroy; override;
- procedure Clear;
- function Add(AItem, AData: Pointer): Pointer;
- procedure Assign(AList: TCustomBucketList);
- function Exists(AItem: Pointer): Boolean;
- function Find(AItem: Pointer; out AData: Pointer): Boolean;
- function ForEach(AProc: TBucketProc; AInfo: Pointer = nil): Boolean;
- function ForEach(AProc: TBucketProcObject): Boolean;
- function Remove(AItem: Pointer): Pointer;
- property Data[AItem: Pointer]: Pointer read GetData write SetData; default;
- end;
- { ---------------------------------------------------------------------
- TBucketList
- ---------------------------------------------------------------------}
- TBucketListSizes = (bl2, bl4, bl8, bl16, bl32, bl64, bl128, bl256);
- { TBucketList }
- TBucketList = class(TCustomBucketList)
- private
- FBucketMask: Byte;
- protected
- function BucketFor(AItem: Pointer): Integer; override;
- public
- constructor Create(ABuckets: TBucketListSizes = bl16);
- end;
- { ---------------------------------------------------------------------
- TObjectBucketList
- ---------------------------------------------------------------------}
- { TObjectBucketList }
- TObjectBucketList = class(TBucketList)
- protected
- function GetData(AItem: TObject): TObject;
- procedure SetData(AItem: TObject; const AData: TObject);
- public
- function Add(AItem, AData: TObject): TObject;
- function Remove(AItem: TObject): TObject;
- property Data[AItem: TObject]: TObject read GetData write SetData; default;
- end;
- implementation
- uses
- RtlConsts;
- ResourceString
- DuplicateMsg = 'An item with key %0:s already exists';
- KeyNotFoundMsg = 'Method: %0:s key [''%1:s''] not found in container';
- NotEmptyMsg = 'Hash table not empty.';
- SErrNoSuchItem = 'No item in list for %p';
- SDuplicateItem = 'Item already exists in list: %p';
- const
- NPRIMES = 28;
- PRIMELIST: array[0 .. NPRIMES-1] of Longword =
- ( 53, 97, 193, 389, 769,
- 1543, 3079, 6151, 12289, 24593,
- 49157, 98317, 196613, 393241, 786433,
- 1572869, 3145739, 6291469, 12582917, 25165843,
- 50331653, 100663319, 201326611, 402653189, 805306457,
- 1610612741, 3221225473, 4294967291 );
- constructor TFPObjectList.Create(FreeObjects : boolean);
- begin
- Create;
- FFreeObjects := Freeobjects;
- end;
- destructor TFPObjectList.Destroy;
- begin
- if (FList <> nil) then
- begin
- Clear;
- FList.Destroy;
- end;
- inherited Destroy;
- end;
- procedure TFPObjectList.Clear;
- var
- i: integer;
- begin
- if FFreeObjects then
- for i := 0 to FList.Count - 1 do
- TObject(FList[i]).Free;
- FList.Clear;
- end;
- constructor TFPObjectList.Create;
- begin
- inherited Create;
- FList := TFPList.Create;
- FFreeObjects := True;
- end;
- function TFPObjectList.GetCount: integer;
- begin
- Result := FList.Count;
- end;
- procedure TFPObjectList.SetCount(const AValue: integer);
- begin
- if FList.Count <> AValue then
- FList.Count := AValue;
- end;
- function TFPObjectList.GetItem(Index: Integer): TObject; {$ifdef CLASSESINLINE}inline;{$endif}
- begin
- Result := TObject(FList[Index]);
- end;
- procedure TFPObjectList.SetItem(Index: Integer; AObject: TObject); {$ifdef CLASSESINLINE}inline;{$endif}
- begin
- if OwnsObjects then
- TObject(FList[Index]).Free;
- FList[index] := AObject;
- end;
- procedure TFPObjectList.SetCapacity(NewCapacity: Integer);
- begin
- FList.Capacity := NewCapacity;
- end;
- function TFPObjectList.GetCapacity: integer;
- begin
- Result := FList.Capacity;
- end;
- function TFPObjectList.Add(AObject: TObject): Integer; {$ifdef CLASSESINLINE}inline;{$endif}
- begin
- Result := FList.Add(AObject);
- end;
- procedure TFPObjectList.Delete(Index: Integer); {$ifdef CLASSESINLINE}inline;{$endif}
- begin
- if OwnsObjects then
- TObject(FList[Index]).Free;
- FList.Delete(Index);
- end;
- procedure TFPObjectList.Exchange(Index1, Index2: Integer);
- begin
- FList.Exchange(Index1, Index2);
- end;
- function TFPObjectList.Expand: TFPObjectList;
- begin
- FList.Expand;
- Result := Self;
- end;
- function TFPObjectList.Extract(Item: TObject): TObject;
- begin
- Result := TObject(FList.Extract(Item));
- end;
- function TFPObjectList.Remove(AObject: TObject): Integer;
- begin
- Result := IndexOf(AObject);
- if (Result <> -1) then
- begin
- if OwnsObjects then
- TObject(FList[Result]).Free;
- FList.Delete(Result);
- end;
- end;
- function TFPObjectList.IndexOf(AObject: TObject): Integer;
- begin
- Result := FList.IndexOf(Pointer(AObject));
- end;
- function TFPObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
- var
- I : Integer;
- begin
- I:=AStartAt;
- Result:=-1;
- If AExact then
- while (I<Count) and (Result=-1) do
- If Items[i].ClassType=AClass then
- Result:=I
- else
- Inc(I)
- else
- while (I<Count) and (Result=-1) do
- If Items[i].InheritsFrom(AClass) then
- Result:=I
- else
- Inc(I);
- end;
- procedure TFPObjectList.Insert(Index: Integer; AObject: TObject); {$ifdef CLASSESINLINE}inline;{$endif}
- begin
- FList.Insert(Index, Pointer(AObject));
- end;
- procedure TFPObjectList.Move(CurIndex, NewIndex: Integer);
- begin
- FList.Move(CurIndex, NewIndex);
- end;
- procedure TFPObjectList.Assign(Obj: TFPObjectList);
- var
- i: Integer;
- begin
- Clear;
- for I := 0 to Obj.Count - 1 do
- Add(Obj[i]);
- end;
- procedure TFPObjectList.Pack;
- begin
- FList.Pack;
- end;
- procedure TFPObjectList.Sort(Compare: TListSortCompare);
- begin
- FList.Sort(Compare);
- end;
- function TFPObjectList.First: TObject;
- begin
- Result := TObject(FList.First);
- end;
- function TFPObjectList.Last: TObject;
- begin
- Result := TObject(FList.Last);
- end;
- procedure TFPObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
- begin
- FList.ForEachCall(TListCallBack(proc2call),arg);
- end;
- procedure TFPObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
- begin
- FList.ForEachCall(TListStaticCallBack(proc2call),arg);
- end;
- { TObjectList }
- constructor tobjectlist.create(freeobjects : boolean);
- begin
- inherited create;
- ffreeobjects:=freeobjects;
- end;
- Constructor tobjectlist.create;
- begin
- inherited create;
- ffreeobjects:=True;
- end;
- Procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
- begin
- if FFreeObjects then
- if (Action=lnDeleted) then
- TObject(Ptr).Free;
- inherited Notify(Ptr,Action);
- end;
- Function TObjectList.GetItem(Index: Integer): TObject;
- begin
- Result:=TObject(Inherited Get(Index));
- end;
- Procedure TObjectList.SetItem(Index: Integer; AObject: TObject);
- Var
- O : TObject;
- begin
- if OwnsObjects then
- begin
- O:=GetItem(Index);
- O.Free;
- end;
- Put(Index,Pointer(AObject));
- end;
- Function TObjectList.Add(AObject: TObject): Integer;
- begin
- Result:=Inherited Add(Pointer(AObject));
- end;
- Function TObjectList.Extract(Item: TObject): TObject;
- begin
- Result:=Tobject(Inherited Extract(Pointer(Item)));
- end;
- Function TObjectList.Remove(AObject: TObject): Integer;
- begin
- Result:=Inherited Remove(Pointer(AObject));
- end;
- Function TObjectList.IndexOf(AObject: TObject): Integer;
- begin
- Result:=Inherited indexOF(Pointer(AObject));
- end;
- Function TObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
- Var
- I : Integer;
- begin
- I:=AStartAt;
- Result:=-1;
- If AExact then
- While (I<Count) and (Result=-1) do
- If Items[i].ClassType=AClass then
- Result:=I
- else
- Inc(I)
- else
- While (I<Count) and (Result=-1) do
- If Items[i].InheritsFrom(AClass) then
- Result:=I
- else
- Inc(I);
- end;
- procedure TObjectList.Insert(Index: Integer; AObject: TObject);
- begin
- Inherited Insert(Index,Pointer(AObject));
- end;
- function TObjectList.First: TObject;
- begin
- Result := TObject(Inherited First);
- end;
- function TObjectList.Last: TObject;
- begin
- Result := TObject(Inherited Last);
- end;
- { TListComponent }
- Type
- TlistComponent = Class(TComponent)
- Private
- Flist : TComponentList;
- Public
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- end;
- procedure TlistComponent.Notification(AComponent: TComponent;
- Operation: TOperation);
- begin
- If (Operation=opremove) then
- Flist.HandleFreeNotify(Self,AComponent);
- inherited;
- end;
- { TComponentList }
- Function TComponentList.Add(AComponent: TComponent): Integer;
- begin
- Result:=Inherited Add(AComponent);
- end;
- destructor TComponentList.Destroy;
- begin
- inherited;
- FreeAndNil(FNotifier);
- end;
- Function TComponentList.Extract(Item: TComponent): TComponent;
- begin
- Result:=TComponent(Inherited Extract(Item));
- end;
- Function TComponentList.First: TComponent;
- begin
- Result:=TComponent(Inherited First);
- end;
- Function TComponentList.GetItems(Index: Integer): TComponent;
- begin
- Result:=TComponent(Inherited Items[Index]);
- end;
- Procedure TComponentList.HandleFreeNotify(Sender: TObject;
- AComponent: TComponent);
- begin
- Extract(Acomponent);
- end;
- Function TComponentList.IndexOf(AComponent: TComponent): Integer;
- begin
- Result:=Inherited IndexOf(AComponent);
- end;
- Procedure TComponentList.Insert(Index: Integer; AComponent: TComponent);
- begin
- Inherited Insert(Index,Acomponent)
- end;
- Function TComponentList.Last: TComponent;
- begin
- Result:=TComponent(Inherited Last);
- end;
- Procedure TComponentList.Notify(Ptr: Pointer; Action: TListNotification);
- begin
- If FNotifier=NIl then
- begin
- FNotifier:=TlistComponent.Create(nil);
- TlistComponent(FNotifier).FList:=Self;
- end;
- If Assigned(Ptr) then
- With TComponent(Ptr) do
- case Action of
- lnAdded : FreeNotification(FNotifier);
- lnExtracted, lnDeleted: RemoveFreeNotification(FNotifier);
- end;
- inherited Notify(Ptr, Action);
- end;
- Function TComponentList.Remove(AComponent: TComponent): Integer;
- begin
- Result:=Inherited Remove(AComponent);
- end;
- Procedure TComponentList.SetItems(Index: Integer; AComponent: TComponent);
- begin
- Put(Index,AComponent);
- end;
- { TClassList }
- Function TClassList.Add(AClass: TClass): Integer;
- begin
- Result:=Inherited Add(Pointer(AClass));
- end;
- Function TClassList.Extract(Item: TClass): TClass;
- begin
- Result:=TClass(Inherited Extract(Pointer(Item)));
- end;
- Function TClassList.First: TClass;
- begin
- Result:=TClass(Inherited First);
- end;
- Function TClassList.GetItems(Index: Integer): TClass;
- begin
- Result:=TClass(Inherited Items[Index]);
- end;
- Function TClassList.IndexOf(AClass: TClass): Integer;
- begin
- Result:=Inherited IndexOf(Pointer(AClass));
- end;
- Procedure TClassList.Insert(Index: Integer; AClass: TClass);
- begin
- Inherited Insert(index,Pointer(AClass));
- end;
- Function TClassList.Last: TClass;
- begin
- Result:=TClass(Inherited Last);
- end;
- Function TClassList.Remove(AClass: TClass): Integer;
- begin
- Result:=Inherited Remove(Pointer(AClass));
- end;
- Procedure TClassList.SetItems(Index: Integer; AClass: TClass);
- begin
- Put(Index,Pointer(Aclass));
- end;
- { TOrderedList }
- Function TOrderedList.AtLeast(ACount: Integer): Boolean;
- begin
- Result:=(FList.Count>=Acount)
- end;
- Function TOrderedList.Count: Integer;
- begin
- Result:=FList.Count;
- end;
- constructor TOrderedList.Create;
- begin
- FList:=Tlist.Create;
- end;
- destructor TOrderedList.Destroy;
- begin
- FList.Free;
- end;
- Function TOrderedList.Peek: Pointer;
- begin
- If AtLeast(1) then
- Result:=PeekItem
- else
- Result:=Nil;
- end;
- Function TOrderedList.PeekItem: Pointer;
- begin
- With Flist do
- Result:=Items[Count-1]
- end;
- Function TOrderedList.Pop: Pointer;
- begin
- If Atleast(1) then
- Result:=PopItem
- else
- Result:=Nil;
- end;
- Function TOrderedList.PopItem: Pointer;
- begin
- With FList do
- If Count>0 then
- begin
- Result:=Items[Count-1];
- Delete(Count-1);
- end
- else
- Result:=Nil;
- end;
- Function TOrderedList.Push(AItem: Pointer): Pointer;
- begin
- PushItem(Aitem);
- Result:=AItem;
- end;
- { TStack }
- Procedure TStack.PushItem(AItem: Pointer);
- begin
- FList.Add(Aitem);
- end;
- { TObjectStack }
- Function TObjectStack.Peek: TObject;
- begin
- Result:=TObject(Inherited Peek);
- end;
- Function TObjectStack.Pop: TObject;
- begin
- Result:=TObject(Inherited Pop);
- end;
- Function TObjectStack.Push(AObject: TObject): TObject;
- begin
- Result:=TObject(Inherited Push(Pointer(AObject)));
- end;
- { TQueue }
- Procedure TQueue.PushItem(AItem: Pointer);
- begin
- With Flist Do
- Insert(0,AItem);
- end;
- { TObjectQueue }
- Function TObjectQueue.Peek: TObject;
- begin
- Result:=TObject(Inherited Peek);
- end;
- Function TObjectQueue.Pop: TObject;
- begin
- Result:=TObject(Inherited Pop);
- end;
- Function TObjectQueue.Push(AObject: TObject): TObject;
- begin
- Result:=TObject(Inherited Push(Pointer(Aobject)));
- end;
- {*****************************************************************************
- TFPHashList
- *****************************************************************************}
- function FPHash(const s:shortstring):LongWord;
- Var
- p,pmax : pchar;
- begin
- {$ifopt Q+}
- {$define overflowon}
- {$Q-}
- {$endif}
- result:=0;
- p:=@s[1];
- pmax:=@s[length(s)+1];
- while (p<pmax) do
- begin
- result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
- inc(p);
- end;
- {$ifdef overflowon}
- {$Q+}
- {$undef overflowon}
- {$endif}
- end;
- function FPHash(P: PChar; Len: Integer): LongWord;
- Var
- pmax : pchar;
- begin
- {$ifopt Q+}
- {$define overflowon}
- {$Q-}
- {$endif}
- result:=0;
- pmax:=p+len;
- while (p<pmax) do
- begin
- result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
- inc(p);
- end;
- {$ifdef overflowon}
- {$Q+}
- {$undef overflowon}
- {$endif}
- end;
- procedure TFPHashList.RaiseIndexError(Index : Integer);
- begin
- Error(SListIndexError, Index);
- end;
- function TFPHashList.Get(Index: Integer): Pointer;
- begin
- If (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- Result:=FHashList^[Index].Data;
- end;
- procedure TFPHashList.Put(Index: Integer; Item: Pointer);
- begin
- if (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- FHashList^[Index].Data:=Item;
- end;
- function TFPHashList.NameOfIndex(Index: Integer): shortstring;
- begin
- If (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- with FHashList^[Index] do
- begin
- if StrIndex>=0 then
- Result:=PShortString(@FStrs[StrIndex])^
- else
- Result:='';
- end;
- end;
- function TFPHashList.HashOfIndex(Index: Integer): LongWord;
- begin
- If (Index < 0) or (Index >= FCount) then
- RaiseIndexError(Index);
- Result:=FHashList^[Index].HashValue;
- end;
- function TFPHashList.GetNextCollision(Index: Integer): Integer;
- begin
- Result:=-1;
- if ((Index > -1) and (Index < FCount)) then
- Result:=FHashList^[Index].NextIndex;
- end;
- function TFPHashList.Extract(item: Pointer): Pointer;
- var
- i : Integer;
- begin
- result := nil;
- i := IndexOf(item);
- if i >= 0 then
- begin
- Result := item;
- Delete(i);
- end;
- end;
- procedure TFPHashList.SetCapacity(NewCapacity: Integer);
- begin
- If (NewCapacity < FCount) or (NewCapacity > MaxHashListSize) then
- Error (SListCapacityError, NewCapacity);
- if NewCapacity = FCapacity then
- exit;
- ReallocMem(FHashList, NewCapacity*SizeOf(THashItem));
- FCapacity := NewCapacity;
- { Maybe expand hash also }
- if FCapacity>FHashCapacity*MaxItemsPerHash then
- SetHashCapacity(FCapacity div MaxItemsPerHash);
- end;
- procedure TFPHashList.SetCount(NewCount: Integer);
- begin
- if (NewCount < 0) or (NewCount > MaxHashListSize)then
- Error(SListCountError, NewCount);
- If NewCount > FCount then
- begin
- If NewCount > FCapacity then
- SetCapacity(NewCount);
- If FCount < NewCount then
- FillChar(FHashList^[FCount], (NewCount-FCount) div Sizeof(THashItem), 0);
- end;
- FCount := Newcount;
- end;
- procedure TFPHashList.SetStrCapacity(NewCapacity: Integer);
- begin
- If (NewCapacity < FStrCount) or (NewCapacity > MaxHashStrSize) then
- Error (SListCapacityError, NewCapacity);
- if NewCapacity = FStrCapacity then
- exit;
- ReallocMem(FStrs, NewCapacity);
- FStrCapacity := NewCapacity;
- end;
- procedure TFPHashList.SetHashCapacity(NewCapacity: Integer);
- begin
- If (NewCapacity < 1) then
- Error (SListCapacityError, NewCapacity);
- if FHashCapacity=NewCapacity then
- exit;
- FHashCapacity:=NewCapacity;
- ReallocMem(FHashTable, FHashCapacity*sizeof(Integer));
- ReHash;
- end;
- procedure TFPHashList.ReHash;
- var
- i : Integer;
- begin
- FillDword(FHashTable^,FHashCapacity,LongWord(-1));
- For i:=0 To FCount-1 Do
- AddToHashTable(i);
- end;
- constructor TFPHashList.Create;
- begin
- SetHashCapacity(1);
- end;
- destructor TFPHashList.Destroy;
- begin
- Clear;
- if assigned(FHashTable) then
- FreeMem(FHashTable);
- inherited Destroy;
- end;
- function TFPHashList.AddStr(const s:shortstring): Integer;
- var
- Len : Integer;
- begin
- len:=length(s)+1;
- if FStrCount+Len >= FStrCapacity then
- StrExpand(Len);
- System.Move(s[0],FStrs[FStrCount],Len);
- result:=FStrCount;
- inc(FStrCount,Len);
- end;
- procedure TFPHashList.AddToHashTable(Index: Integer);
- var
- HashIndex : Integer;
- begin
- with FHashList^[Index] do
- begin
- if not assigned(Data) then
- exit;
- HashIndex:=HashValue mod LongWord(FHashCapacity);
- NextIndex:=FHashTable^[HashIndex];
- FHashTable^[HashIndex]:=Index;
- end;
- end;
- function TFPHashList.Add(const AName:shortstring;Item: Pointer): Integer;
- begin
- if FCount = FCapacity then
- Expand;
- with FHashList^[FCount] do
- begin
- HashValue:=FPHash(AName);
- Data:=Item;
- StrIndex:=AddStr(AName);
- end;
- AddToHashTable(FCount);
- Result := FCount;
- inc(FCount);
- end;
- procedure TFPHashList.Clear;
- begin
- if Assigned(FHashList) then
- begin
- FCount:=0;
- SetCapacity(0);
- FHashList := nil;
- end;
- SetHashCapacity(1);
- FHashTable^[0]:=longword(-1); // sethashcapacity does not always call rehash
- if Assigned(FStrs) then
- begin
- FStrCount:=0;
- SetStrCapacity(0);
- FStrs := nil;
- end;
- end;
- procedure TFPHashList.Delete(Index: Integer);
- begin
- If (Index<0) or (Index>=FCount) then
- Error (SListIndexError, Index);
- { Remove from HashList }
- dec(FCount);
- System.Move (FHashList^[Index+1], FHashList^[Index], (FCount - Index) * Sizeof(THashItem));
- { All indexes are updated, we need to build the hashtable again }
- Rehash;
- { Shrink the list if appropriate }
- if (FCapacity > 256) and (FCount < FCapacity shr 2) then
- begin
- FCapacity := FCapacity shr 1;
- ReallocMem(FHashList, Sizeof(THashItem) * FCapacity);
- end;
- end;
- function TFPHashList.Remove(Item: Pointer): Integer;
- begin
- Result := IndexOf(Item);
- If Result <> -1 then
- Self.Delete(Result);
- end;
- class procedure TFPHashList.Error(const Msg: string; Data: PtrInt);
- begin
- Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
- end;
- function TFPHashList.Expand: TFPHashList;
- var
- IncSize : Longint;
- begin
- Result := Self;
- if FCount < FCapacity then
- exit;
- IncSize := sizeof(ptrint)*2;
- if FCapacity > 127 then
- Inc(IncSize, FCapacity shr 2)
- else if FCapacity > sizeof(ptrint)*3 then
- Inc(IncSize, FCapacity shr 1)
- else if FCapacity >= sizeof(ptrint) then
- inc(IncSize,sizeof(ptrint));
- SetCapacity(FCapacity + IncSize);
- end;
- procedure TFPHashList.StrExpand(MinIncSize:Integer);
- var
- IncSize : Longint;
- begin
- if FStrCount+MinIncSize < FStrCapacity then
- exit;
- IncSize := 64;
- if FStrCapacity > 255 then
- Inc(IncSize, FStrCapacity shr 2);
- SetStrCapacity(FStrCapacity + IncSize + MinIncSize);
- end;
- function TFPHashList.IndexOf(Item: Pointer): Integer;
- var
- psrc : PHashItem;
- Index : integer;
- begin
- Result:=-1;
- psrc:=@FHashList^[0];
- For Index:=0 To FCount-1 Do
- begin
- if psrc^.Data=Item then
- begin
- Result:=Index;
- exit;
- end;
- inc(psrc);
- end;
- end;
- function TFPHashList.InternalFind(AHash:LongWord;const AName:shortstring;out PrevIndex:Integer):Integer;
- var
- HashIndex : Integer;
- Len,
- LastChar : Char;
- begin
- HashIndex:=AHash mod LongWord(FHashCapacity);
- Result:=FHashTable^[HashIndex];
- Len:=Char(Length(AName));
- LastChar:=AName[Byte(Len)];
- PrevIndex:=-1;
- while Result<>-1 do
- begin
- with FHashList^[Result] do
- begin
- if assigned(Data) and
- (HashValue=AHash) and
- (Len=FStrs[StrIndex]) and
- (LastChar=FStrs[StrIndex+Byte(Len)]) and
- (AName=PShortString(@FStrs[StrIndex])^) then
- exit;
- PrevIndex:=Result;
- Result:=NextIndex;
- end;
- end;
- end;
- function TFPHashList.Find(const AName:shortstring): Pointer;
- var
- Index,
- PrevIndex : Integer;
- begin
- Result:=nil;
- Index:=InternalFind(FPHash(AName),AName,PrevIndex);
- if Index=-1 then
- exit;
- Result:=FHashList^[Index].Data;
- end;
- function TFPHashList.FindIndexOf(const AName:shortstring): Integer;
- var
- PrevIndex : Integer;
- begin
- Result:=InternalFind(FPHash(AName),AName,PrevIndex);
- end;
- function TFPHashList.FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
- var
- Index,
- PrevIndex : Integer;
- begin
- Result:=nil;
- Index:=InternalFind(AHash,AName,PrevIndex);
- if Index=-1 then
- exit;
- Result:=FHashList^[Index].Data;
- end;
- function TFPHashList.Rename(const AOldName,ANewName:shortstring): Integer;
- var
- PrevIndex,
- Index : Integer;
- OldHash : LongWord;
- begin
- Result:=-1;
- OldHash:=FPHash(AOldName);
- Index:=InternalFind(OldHash,AOldName,PrevIndex);
- if Index=-1 then
- exit;
- { Remove from current Hash }
- if PrevIndex<>-1 then
- FHashList^[PrevIndex].NextIndex:=FHashList^[Index].NextIndex
- else
- FHashTable^[OldHash mod LongWord(FHashCapacity)]:=FHashList^[Index].NextIndex;
- { Set new name and hash }
- with FHashList^[Index] do
- begin
- HashValue:=FPHash(ANewName);
- StrIndex:=AddStr(ANewName);
- end;
- { Insert back in Hash }
- AddToHashTable(Index);
- { Return Index }
- Result:=Index;
- end;
- procedure TFPHashList.Pack;
- var
- NewCount,
- i : integer;
- pdest,
- psrc : PHashItem;
- begin
- NewCount:=0;
- psrc:=@FHashList^[0];
- pdest:=psrc;
- For I:=0 To FCount-1 Do
- begin
- if assigned(psrc^.Data) then
- begin
- pdest^:=psrc^;
- inc(pdest);
- inc(NewCount);
- end;
- inc(psrc);
- end;
- FCount:=NewCount;
- { We need to ReHash to update the IndexNext }
- ReHash;
- { Release over-capacity }
- SetCapacity(FCount);
- SetStrCapacity(FStrCount);
- end;
- procedure TFPHashList.ShowStatistics;
- var
- HashMean,
- HashStdDev : Double;
- Index,
- i,j : Integer;
- begin
- { Calculate Mean and StdDev }
- HashMean:=0;
- HashStdDev:=0;
- for i:=0 to FHashCapacity-1 do
- begin
- j:=0;
- Index:=FHashTable^[i];
- while (Index<>-1) do
- begin
- inc(j);
- Index:=FHashList^[Index].NextIndex;
- end;
- HashMean:=HashMean+j;
- HashStdDev:=HashStdDev+Sqr(j);
- end;
- HashMean:=HashMean/FHashCapacity;
- HashStdDev:=(HashStdDev-FHashCapacity*Sqr(HashMean));
- If FHashCapacity>1 then
- HashStdDev:=Sqrt(HashStdDev/(FHashCapacity-1))
- else
- HashStdDev:=0;
- { Print info to stdout }
- Writeln('HashSize : ',FHashCapacity);
- Writeln('HashMean : ',HashMean:1:4);
- Writeln('HashStdDev : ',HashStdDev:1:4);
- Writeln('ListSize : ',FCount,'/',FCapacity);
- Writeln('StringSize : ',FStrCount,'/',FStrCapacity);
- end;
- procedure TFPHashList.ForEachCall(proc2call:TListCallback;arg:pointer);
- var
- i : integer;
- p : pointer;
- begin
- For I:=0 To Count-1 Do
- begin
- p:=FHashList^[i].Data;
- if assigned(p) then
- proc2call(p,arg);
- end;
- end;
- procedure TFPHashList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
- var
- i : integer;
- p : pointer;
- begin
- For I:=0 To Count-1 Do
- begin
- p:=FHashList^[i].Data;
- if assigned(p) then
- proc2call(p,arg);
- end;
- end;
- {*****************************************************************************
- TFPHashObject
- *****************************************************************************}
- procedure TFPHashObject.InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:shortstring);
- var
- Index : integer;
- begin
- FOwner:=HashObjectList;
- Index:=HashObjectList.Add(s,Self);
- FStrIndex:=HashObjectList.List.List^[Index].StrIndex;
- FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
- end;
- constructor TFPHashObject.CreateNotOwned;
- begin
- FStrIndex:=-1;
- end;
- constructor TFPHashObject.Create(HashObjectList:TFPHashObjectList;const s:shortstring);
- begin
- InternalChangeOwner(HashObjectList,s);
- end;
- procedure TFPHashObject.ChangeOwner(HashObjectList:TFPHashObjectList);
- begin
- InternalChangeOwner(HashObjectList,PShortString(@FOwner.List.Strs[FStrIndex])^);
- end;
- procedure TFPHashObject.ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:shortstring);
- begin
- InternalChangeOwner(HashObjectList,s);
- end;
- procedure TFPHashObject.Rename(const ANewName:shortstring);
- var
- Index : integer;
- begin
- Index:=FOwner.Rename(PShortString(@FOwner.List.Strs[FStrIndex])^,ANewName);
- if Index<>-1 then
- begin
- FStrIndex:=FOwner.List.List^[Index].StrIndex;
- FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
- end;
- end;
- function TFPHashObject.GetName:shortstring;
- begin
- if FOwner<>nil then
- begin
- FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
- Result:=FCachedStr^;
- end
- else
- Result:='';
- end;
- function TFPHashObject.GetHash:Longword;
- begin
- if FOwner<>nil then
- Result:=FPHash(PShortString(@FOwner.List.Strs[FStrIndex])^)
- else
- Result:=$ffffffff;
- end;
- {*****************************************************************************
- TFPHashObjectList (Copied from rtl/objpas/classes/lists.inc)
- *****************************************************************************}
- constructor TFPHashObjectList.Create(FreeObjects : boolean = True);
- begin
- inherited Create;
- FHashList := TFPHashList.Create;
- FFreeObjects := Freeobjects;
- end;
- destructor TFPHashObjectList.Destroy;
- begin
- if (FHashList <> nil) then
- begin
- Clear;
- FHashList.Destroy;
- end;
- inherited Destroy;
- end;
- procedure TFPHashObjectList.Clear;
- var
- i: integer;
- begin
- if FFreeObjects then
- for i := 0 to FHashList.Count - 1 do
- TObject(FHashList[i]).Free;
- FHashList.Clear;
- end;
- function TFPHashObjectList.GetCount: integer;
- begin
- Result := FHashList.Count;
- end;
- procedure TFPHashObjectList.SetCount(const AValue: integer);
- begin
- if FHashList.Count <> AValue then
- FHashList.Count := AValue;
- end;
- function TFPHashObjectList.GetItem(Index: Integer): TObject;
- begin
- Result := TObject(FHashList[Index]);
- end;
- procedure TFPHashObjectList.SetItem(Index: Integer; AObject: TObject);
- begin
- if OwnsObjects then
- TObject(FHashList[Index]).Free;
- FHashList[index] := AObject;
- end;
- procedure TFPHashObjectList.SetCapacity(NewCapacity: Integer);
- begin
- FHashList.Capacity := NewCapacity;
- end;
- function TFPHashObjectList.GetCapacity: integer;
- begin
- Result := FHashList.Capacity;
- end;
- function TFPHashObjectList.Add(const AName:shortstring;AObject: TObject): Integer;
- begin
- Result := FHashList.Add(AName,AObject);
- end;
- function TFPHashObjectList.NameOfIndex(Index: Integer): shortstring;
- begin
- Result := FHashList.NameOfIndex(Index);
- end;
- function TFPHashObjectList.HashOfIndex(Index: Integer): LongWord;
- begin
- Result := FHashList.HashOfIndex(Index);
- end;
- function TFPHashObjectList.GetNextCollision(Index: Integer): Integer;
- begin
- Result := FHashList.GetNextCollision(Index);
- end;
- procedure TFPHashObjectList.Delete(Index: Integer);
- begin
- if OwnsObjects then
- TObject(FHashList[Index]).Free;
- FHashList.Delete(Index);
- end;
- function TFPHashObjectList.Expand: TFPHashObjectList;
- begin
- FHashList.Expand;
- Result := Self;
- end;
- function TFPHashObjectList.Extract(Item: TObject): TObject;
- begin
- Result := TObject(FHashList.Extract(Item));
- end;
- function TFPHashObjectList.Remove(AObject: TObject): Integer;
- begin
- Result := IndexOf(AObject);
- if (Result <> -1) then
- begin
- if OwnsObjects then
- TObject(FHashList[Result]).Free;
- FHashList.Delete(Result);
- end;
- end;
- function TFPHashObjectList.IndexOf(AObject: TObject): Integer;
- begin
- Result := FHashList.IndexOf(Pointer(AObject));
- end;
- function TFPHashObjectList.Find(const s:shortstring): TObject;
- begin
- result:=TObject(FHashList.Find(s));
- end;
- function TFPHashObjectList.FindIndexOf(const s:shortstring): Integer;
- begin
- result:=FHashList.FindIndexOf(s);
- end;
- function TFPHashObjectList.FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
- begin
- Result:=TObject(FHashList.FindWithHash(AName,AHash));
- end;
- function TFPHashObjectList.Rename(const AOldName,ANewName:shortstring): Integer;
- begin
- Result:=FHashList.Rename(AOldName,ANewName);
- end;
- function TFPHashObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
- var
- I : Integer;
- begin
- I:=AStartAt;
- Result:=-1;
- If AExact then
- while (I<Count) and (Result=-1) do
- If Items[i].ClassType=AClass then
- Result:=I
- else
- Inc(I)
- else
- while (I<Count) and (Result=-1) do
- If Items[i].InheritsFrom(AClass) then
- Result:=I
- else
- Inc(I);
- end;
- procedure TFPHashObjectList.Pack;
- begin
- FHashList.Pack;
- end;
- procedure TFPHashObjectList.ShowStatistics;
- begin
- FHashList.ShowStatistics;
- end;
- procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
- begin
- FHashList.ForEachCall(TListCallBack(proc2call),arg);
- end;
- procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
- begin
- FHashList.ForEachCall(TListStaticCallBack(proc2call),arg);
- end;
- { ---------------------------------------------------------------------
- Hash support, by Dean Zobec
- ---------------------------------------------------------------------}
- { Default hash function }
- function RSHash(const S: string; const TableSize: Longword): Longword;
- const
- b = 378551;
- var
- a: Longword;
- i: Longword;
- begin
- a := 63689;
- Result := 0;
- if length(s)>0 then
- for i := 1 to Length(S) do
- begin
- Result := Result * a + Ord(S[i]);
- a := a * b;
- end;
- Result := (Result and $7FFFFFFF) mod TableSize;
- end;
- { THTNode }
- constructor THTCustomNode.CreateWith(const AString: string);
- begin
- inherited Create;
- FKey := AString;
- end;
- function THTCustomNode.HasKey(const AKey: string): boolean;
- begin
- if Length(AKey) <> Length(FKey) then
- begin
- Result := false;
- exit;
- end
- else
- Result := CompareMem(PChar(FKey), PChar(AKey), length(AKey));
- end;
- { TFPCustomHashTable }
- constructor TFPCustomHashTable.Create;
- begin
- CreateWith(196613,@RSHash);
- end;
- constructor TFPCustomHashTable.CreateWith(AHashTableSize: Longword;
- aHashFunc: THashFunction);
- begin
- Inherited Create;
- FHashTable := TFPObjectList.Create(True);
- HashTableSize := AHashTableSize;
- FHashFunction := aHashFunc;
- end;
- destructor TFPCustomHashTable.Destroy;
- begin
- FHashTable.Free;
- inherited Destroy;
- end;
- function TFPCustomHashTable.GetDensity: Longword;
- begin
- Result := FHashTableSize - VoidSlots
- end;
- function TFPCustomHashTable.GetNumberOfCollisions: Longword;
- begin
- Result := FCount -(FHashTableSize - VoidSlots)
- end;
- procedure TFPCustomHashTable.SetHashTableSize(const Value: Longword);
- var
- i: Longword;
- newSize: Longword;
- begin
- if Value <> FHashTableSize then
- begin
- i := 0;
- while (PRIMELIST[i] < Value) and (i < 27) do
- inc(i);
- newSize := PRIMELIST[i];
- if Count = 0 then
- begin
- FHashTableSize := newSize;
- InitializeHashTable;
- end
- else
- ChangeTableSize(newSize);
- end;
- end;
- procedure TFPCustomHashTable.InitializeHashTable;
- var
- i: LongWord;
- begin
- if FHashTableSize>0 Then
- for i := 0 to FHashTableSize-1 do
- FHashTable.Add(nil);
- FCount := 0;
- end;
- procedure TFPCustomHashTable.ChangeTableSize(const ANewSize: Longword);
- var
- SavedTable: TFPObjectList;
- SavedTableSize: Longword;
- i, j: Longword;
- temp: THTCustomNode;
- begin
- SavedTable := FHashTable;
- SavedTableSize := FHashTableSize;
- FHashTableSize := ANewSize;
- FHashTable := TFPObjectList.Create(True);
- InitializeHashTable;
- If SavedTableSize>0 Then
- for i := 0 to SavedTableSize-1 do
- begin
- if Assigned(SavedTable[i]) then
- for j := 0 to TFPObjectList(SavedTable[i]).Count -1 do
- begin
- temp := THTCustomNode(TFPObjectList(SavedTable[i])[j]);
- AddNode(temp);
- end;
- end;
- SavedTable.Free;
- end;
- procedure TFPCustomHashTable.SetHashFunction(AHashFunction: THashFunction);
- begin
- if IsEmpty then
- FHashFunction := AHashFunction
- else
- raise Exception.Create(NotEmptyMsg);
- end;
- function TFPCustomHashTable.Find(const aKey: string): THTCustomNode;
- var
- hashCode: Longword;
- chn: TFPObjectList;
- i: Longword;
- begin
- hashCode := FHashFunction(aKey, FHashTableSize);
- chn := Chain(hashCode);
- if Assigned(chn) then
- begin
- if chn.count>0 then
- for i := 0 to chn.Count - 1 do
- if THTCustomNode(chn[i]).HasKey(aKey) then
- begin
- result := THTCustomNode(chn[i]);
- exit;
- end;
- end;
- Result := nil;
- end;
- Function TFPCustomHashTable.FindChainForAdd(Const aKey : String) : TFPObjectList;
- var
- hashCode: Longword;
- i: Longword;
- begin
- hashCode := FHashFunction(aKey, FHashTableSize);
- Result := Chain(hashCode);
- if Assigned(Result) then
- begin
- if Result.count>0 then
- for i := 0 to Result.Count - 1 do
- if THTCustomNode(Result[i]).HasKey(aKey) then
- Raise EDuplicate.CreateFmt(DuplicateMsg, [aKey]);
- end
- else
- begin
- FHashTable[hashcode] := TFPObjectList.Create(true);
- Result := Chain(hashcode);
- end;
- inc(FCount);
- end;
- procedure TFPCustomHashTable.Delete(const aKey: string);
- var
- hashCode: Longword;
- chn: TFPObjectList;
- i: Longword;
- begin
- hashCode := FHashFunction(aKey, FHashTableSize);
- chn := Chain(hashCode);
- if Assigned(chn) then
- begin
- if chn.count>0 then
- for i := 0 to chn.Count - 1 do
- if THTCustomNode(chn[i]).HasKey(aKey) then
- begin
- chn.Delete(i);
- dec(FCount);
- exit;
- end;
- end;
- raise EKeyNotFound.CreateFmt(KeyNotFoundMsg, ['Delete', aKey]);
- end;
- function TFPCustomHashTable.IsEmpty: boolean;
- begin
- Result := (FCount = 0);
- end;
- function TFPCustomHashTable.Chain(const index: Longword): TFPObjectList;
- begin
- Result := TFPObjectList(FHashTable[index]);
- end;
- function TFPCustomHashTable.GetVoidSlots: Longword;
- var
- i: Longword;
- num: Longword;
- begin
- num := 0;
- if FHashTableSize>0 Then
- for i:= 0 to FHashTableSize-1 do
- if Not Assigned(Chain(i)) then
- inc(num);
- result := num;
- end;
- function TFPCustomHashTable.GetLoadFactor: double;
- begin
- Result := Count / FHashTableSize;
- end;
- function TFPCustomHashTable.GetAVGChainLen: double;
- begin
- result := Count / (FHashTableSize - VoidSlots);
- end;
- function TFPCustomHashTable.GetMaxChainLength: Longword;
- var
- i: Longword;
- begin
- Result := 0;
- if FHashTableSize>0 Then
- for i := 0 to FHashTableSize-1 do
- if ChainLength(i) > Result then
- Result := ChainLength(i);
- end;
- function TFPCustomHashTable.FindOrCreateNew(const aKey: string): THTCustomNode;
- var
- hashCode: Longword;
- chn: TFPObjectList;
- i: Longword;
- begin
- hashCode := FHashFunction(aKey, FHashTableSize);
- chn := Chain(hashCode);
- if Assigned(chn) then
- begin
- if chn.count>0 then
- for i := 0 to chn.Count - 1 do
- if THTCustomNode(chn[i]).HasKey(aKey) then
- begin
- Result := THTNode(chn[i]);
- exit;
- end
- end
- else
- begin
- FHashTable[hashcode] := TFPObjectList.Create(true);
- chn := Chain(hashcode);
- end;
- inc(FCount);
- Result := CreateNewNode(aKey);
- chn.Add(Result);
- end;
- function TFPCustomHashTable.ChainLength(const ChainIndex: Longword): Longword;
- begin
- if Assigned(Chain(ChainIndex)) then
- Result := Chain(ChainIndex).Count
- else
- Result := 0;
- end;
- procedure TFPCustomHashTable.Clear;
- var
- i: Longword;
- begin
- if FHashTableSize>0 Then
- for i := 0 to FHashTableSize - 1 do
- begin
- if Assigned(Chain(i)) then
- Chain(i).Clear;
- end;
- FCount := 0;
- end;
- { TFPDataHashTable }
- procedure TFPDataHashTable.Add(const aKey: string; aItem: pointer);
- var
- chn: TFPObjectList;
- NewNode: THtDataNode;
- begin
- chn:=FindChainForAdd(akey);
- NewNode := THtDataNode(CreateNewNode(aKey));
- NewNode.Data := aItem;
- chn.Add(NewNode);
- end;
- function TFPDataHashTable.GetData(const Index: string): Pointer;
- var
- node: THTDataNode;
- begin
- node := THTDataNode(Find(Index));
- if Assigned(node) then
- Result := node.Data
- else
- Result := nil;
- end;
- procedure TFPDataHashTable.SetData(const index: string; const AValue: Pointer);
- begin
- THTDataNode(FindOrCreateNew(index)).Data := AValue;
- end;
- Function TFPDataHashTable.CreateNewNode(const aKey : string) : THTCustomNode;
- begin
- Result:=THTDataNode.CreateWith(aKey);
- end;
- function TFPDataHashTable.ForEachCall(aMethod: TDataIteratorMethod): THTDataNode;
- var
- i, j: Longword;
- continue: boolean;
- begin
- Result := nil;
- continue := true;
- if FHashTableSize>0 then
- for i := 0 to FHashTableSize-1 do
- begin
- if assigned(Chain(i)) then
- begin
- if chain(i).count>0 then
- for j := 0 to Chain(i).Count-1 do
- begin
- aMethod(THTDataNode(Chain(i)[j]).Data, THTDataNode(Chain(i)[j]).Key, continue);
- if not continue then
- begin
- Result := THTDataNode(Chain(i)[j]);
- Exit;
- end;
- end;
- end;
- end;
- end;
- Procedure TFPDataHashTable.AddNode(ANode : THTCustomNode);
- begin
- With THTDataNode(ANode) do
- Add(Key,Data);
- end;
- { TFPStringHashTable }
- Procedure TFPStringHashTable.AddNode(ANode : THTCustomNode);
- begin
- With THTStringNode(ANode) do
- Add(Key,Data);
- end;
- function TFPStringHashTable.GetData(const Index: string): String;
- var
- node: THTStringNode;
- begin
- node := THTStringNode(Find(Index));
- if Assigned(node) then
- Result := node.Data
- else
- Result := '';
- end;
- procedure TFPStringHashTable.SetData(const index, AValue: string);
- begin
- THTStringNode(FindOrCreateNew(index)).Data := AValue;
- end;
- procedure TFPStringHashTable.Add(const aKey, aItem: string);
- var
- chn: TFPObjectList;
- NewNode: THtStringNode;
- begin
- chn:=FindChainForAdd(akey);
- NewNode := THtStringNode(CreateNewNode(aKey));
- NewNode.Data := aItem;
- chn.Add(NewNode);
- end;
- Function TFPStringHashTable.CreateNewNode(const aKey : string) : THTCustomNode;
- begin
- Result:=THTStringNode.CreateWith(aKey);
- end;
- function TFPStringHashTable.ForEachCall(aMethod: TStringIteratorMethod): THTStringNode;
- var
- i, j: Longword;
- continue: boolean;
- begin
- Result := nil;
- continue := true;
- if FHashTableSize>0 then
- for i := 0 to FHashTableSize-1 do
- begin
- if assigned(Chain(i)) then
- begin
- if chain(i).count>0 then
- for j := 0 to Chain(i).Count-1 do
- begin
- aMethod(THTStringNode(Chain(i)[j]).Data, THTStringNode(Chain(i)[j]).Key, continue);
- if not continue then
- begin
- Result := THTStringNode(Chain(i)[j]);
- Exit;
- end;
- end;
- end;
- end;
- end;
- { TFPObjectHashTable }
- Procedure TFPObjectHashTable.AddNode(ANode : THTCustomNode);
- begin
- With THTObjectNode(ANode) do
- Add(Key,Data);
- end;
- function TFPObjectHashTable.GetData(const Index: string): TObject;
- var
- node: THTObjectNode;
- begin
- node := THTObjectNode(Find(Index));
- if Assigned(node) then
- Result := node.Data
- else
- Result := Nil;
- end;
- procedure TFPObjectHashTable.SetData(const index : string; AObject : TObject);
- begin
- THTObjectNode(FindOrCreateNew(index)).Data := AObject;
- end;
- procedure TFPObjectHashTable.Add(const aKey: string; AItem : TObject);
- var
- chn: TFPObjectList;
- NewNode: THTObjectNode;
- begin
- chn:=FindChainForAdd(akey);
- NewNode := THTObjectNode(CreateNewNode(aKey));
- NewNode.Data := aItem;
- chn.Add(NewNode);
- end;
- Function TFPObjectHashTable.CreateNewNode(const aKey : string) : THTCustomNode;
- begin
- If OwnsObjects then
- Result:=THTOwnedObjectNode.CreateWith(aKey)
- else
- Result:=THTObjectNode.CreateWith(aKey);
- end;
- function TFPObjectHashTable.ForEachCall(aMethod: TObjectIteratorMethod): THTObjectNode;
- var
- i, j: Longword;
- continue: boolean;
- begin
- Result := nil;
- continue := true;
- if FHashTableSize>0 then
- for i := 0 to FHashTableSize-1 do
- begin
- if assigned(Chain(i)) then
- begin
- if chain(i).count>0 then
- for j := 0 to Chain(i).Count-1 do
- begin
- aMethod(THTObjectNode(Chain(i)[j]).Data, THTObjectNode(Chain(i)[j]).Key, continue);
- if not continue then
- begin
- Result := THTObjectNode(Chain(i)[j]);
- Exit;
- end;
- end;
- end;
- end;
- end;
- constructor TFPObjectHashTable.Create(AOwnsObjects : Boolean = True);
- begin
- Inherited Create;
- FOwnsObjects:=AOwnsObjects;
- end;
- constructor TFPObjectHashTable.CreateWith(AHashTableSize: Longword; aHashFunc: THashFunction; AOwnsObjects : Boolean = True);
- begin
- Inherited CreateWith(AHashTableSize,AHashFunc);
- FOwnsObjects:=AOwnsObjects;
- end;
- Destructor THTOwnedObjectNode.Destroy;
- begin
- FreeAndNil(FData);
- Inherited;
- end;
- { TCustomBucketList }
- function TCustomBucketList.GetData(AItem: Pointer): Pointer;
- Var
- B,I : Integer;
- begin
- GetBucketItem(AItem,B,I);
- Result:=FBuckets[B].Items[I].Data;
- end;
- function TCustomBucketList.GetBucketCount: Integer;
- begin
- Result:=Length(FBuckets);
- end;
- procedure TCustomBucketList.SetData(AItem: Pointer; const AData: Pointer);
- Var
- B,I : Integer;
- begin
- GetBucketItem(AItem,B,I);
- FBuckets[B].Items[I].Data:=AData;
- end;
- procedure TCustomBucketList.SetBucketCount(const Value: Integer);
- begin
- If (Value<>GetBucketCount) then
- SetLength(FBuckets,Value);
- end;
- procedure TCustomBucketList.GetBucketItem(AItem: Pointer; out ABucket,
- AIndex: Integer);
- begin
- If Not FindItem(AItem,ABucket,AIndex) then
- Error(SErrNoSuchItem,[AItem]);
- end;
- function TCustomBucketList.AddItem(ABucket: Integer; AItem, AData: Pointer
- ): Pointer;
- Var
- B : PBucket;
- L : Integer;
- begin
- B:=@FBuckets[ABucket];
- L:=Length(B^.Items);
- If (B^.Count=L) then
- begin
- If L<8 then
- L:=8
- else
- L:=L+L div 2;
- SetLength(B^.Items,L);
- end;
- With B^ do
- begin
- Items[Count].Item:=AItem;
- Items[Count].Data:=AData;
- Result:=AData;
- Inc(Count);
- end;
- end;
- function TCustomBucketList.DeleteItem(ABucket: Integer; AIndex: Integer): Pointer;
- Var
- B : PBucket;
- L : Integer;
- begin
- B:=@FBuckets[ABucket];
- Result:=B^.Items[Aindex].Data;
- If B^.Count=1 then
- SetLength(B^.Items,0)
- else
- begin
- L:=(B^.Count-AIndex-1);// No point in moving if last one...
- If L>0 then
- Move(B^.Items[AIndex+1],B^.Items[AIndex],L*SizeOf(TBucketItem));
- end;
- Dec(B^.Count);
- end;
- procedure TCustomBucketList.Error(Msg: String; Args: array of const);
- begin
- Raise ElistError.CreateFmt(Msg,Args);
- end;
- function TCustomBucketList.FindItem(AItem: Pointer; out ABucket, AIndex: Integer
- ): Boolean;
- Var
- I : Integer;
- B : TBucket;
- begin
- ABucket:=BucketFor(AItem);
- B:=FBuckets[ABucket];
- I:=B.Count-1;
- While (I>=0) And (B.Items[I].Item<>AItem) do
- Dec(I);
- Result:=I>=0;
- If Result then
- AIndex:=I;
- end;
- destructor TCustomBucketList.Destroy;
- begin
- Clear;
- inherited Destroy;
- end;
- procedure TCustomBucketList.Clear;
- Var
- B : TBucket;
- I,J : Integer;
- begin
- For I:=0 to Length(FBuckets)-1 do
- begin
- B:=FBuckets[I];
- For J:=B.Count-1 downto 0 do
- DeleteItem(I,J);
- end;
- SetLength(FBuckets,0);
- end;
- function TCustomBucketList.Add(AItem, AData: Pointer): Pointer;
- Var
- B,I : Integer;
- begin
- If FindItem(AItem,B,I) then
- Error(SDuplicateItem,[AItem]);
- Result:=AddItem(B,AItem,AData);
- end;
- procedure TCustomBucketList.Assign(AList: TCustomBucketList);
- Var
- I,J : Integer;
- begin
- Clear;
- SetLength(FBuckets,Length(Alist.FBuckets));
- For I:=0 to BucketCount-1 do
- begin
- SetLength(FBuckets[i].Items,Length(AList.Fbuckets[I].Items));
- For J:=0 to AList.Fbuckets[I].Count-1 do
- With AList.Fbuckets[I].Items[J] do
- AddItem(I,Item,Data);
- end;
- end;
- function TCustomBucketList.Exists(AItem: Pointer): Boolean;
- Var
- B,I : Integer;
- begin
- Result:=FindItem(Aitem,B,I);
- end;
- function TCustomBucketList.Find(AItem: Pointer; out AData: Pointer): Boolean;
- Var
- B,I : integer;
- begin
- Result:=FindItem(AItem,B,I);
- If Result then
- AData:=FBuckets[B].Items[I].Data;
- end;
- function TCustomBucketList.ForEach(AProc: TBucketProc; AInfo: Pointer
- ): Boolean;
- Var
- I,J,S : Integer;
- Bu : TBucket;
- begin
- I:=0;
- Result:=True;
- S:=GetBucketCount;
- While Result and (I<S) do
- begin
- J:=0;
- Bu:=FBuckets[I];
- While Result and (J<Bu.Count) do
- begin
- With Bu.Items[J] do
- AProc(AInfo,Item,Data,Result);
- Inc(J);
- end;
- Inc(I);
- end;
- end;
- function TCustomBucketList.ForEach(AProc: TBucketProcObject): Boolean;
- Var
- I,J,S : Integer;
- Bu : TBucket;
- begin
- I:=0;
- Result:=True;
- S:=GetBucketCount;
- While Result and (I<S) do
- begin
- J:=0;
- Bu:=FBuckets[I];
- While Result and (J<Bu.Count) do
- begin
- With Bu.Items[J] do
- AProc(Item,Data,Result);
- Inc(J);
- end;
- Inc(I);
- end;
- end;
- function TCustomBucketList.Remove(AItem: Pointer): Pointer;
- Var
- B,I : integer;
- begin
- If FindItem(AItem,B,I) then
- begin
- Result:=FBuckets[B].Items[I].Data;
- DeleteItem(B,I);
- end
- else
- Result:=Nil;
- end;
- { TBucketList }
- function TBucketList.BucketFor(AItem: Pointer): Integer;
- begin
- // Pointers on average have a granularity of 4
- Result:=(PtrInt(AItem) shr 2) and FBucketMask;
- end;
- constructor TBucketList.Create(ABuckets: TBucketListSizes);
- Var
- L : Integer;
- begin
- Inherited Create;
- L:=1 shl (Ord(Abuckets)+1);
- SetBucketCount(L);
- FBucketMask:=L-1;
- end;
- { TObjectBucketList }
- function TObjectBucketList.GetData(AItem: TObject): TObject;
- begin
- Result:=TObject(Inherited GetData(AItem));
- end;
- procedure TObjectBucketList.SetData(AItem: TObject; const AData: TObject);
- begin
- Inherited SetData(Pointer(AItem),Pointer(AData));
- end;
- function TObjectBucketList.Add(AItem, AData: TObject): TObject;
- begin
- Result:=TObject(Inherited Add(Pointer(AItem),Pointer(AData)));
- end;
- function TObjectBucketList.Remove(AItem: TObject): TObject;
- begin
- Result:=TObject(Inherited Remove(Pointer(AItem)));
- end;
- end.
|