cclasses.pas 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. This module provides some basic classes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cclasses;
  18. {$i fpcdefs.inc}
  19. {$ifndef VER2_0}
  20. {$define CCLASSESINLINE}
  21. {$endif}
  22. interface
  23. uses
  24. {$IFNDEF USE_FAKE_SYSUTILS}
  25. SysUtils,
  26. {$ELSE}
  27. fksysutl,
  28. {$ENDIF}
  29. globtype,
  30. CUtils,CStreams;
  31. {********************************************
  32. TMemDebug
  33. ********************************************}
  34. type
  35. tmemdebug = class
  36. private
  37. totalmem,
  38. startmem : int64;
  39. infostr : string[40];
  40. public
  41. constructor Create(const s:string);
  42. destructor Destroy;override;
  43. procedure show;
  44. procedure start;
  45. procedure stop;
  46. end;
  47. {*******************************************************
  48. TFPList (From rtl/objpas/classes/classesh.inc)
  49. ********************************************************}
  50. const
  51. SListIndexError = 'List index exceeds bounds (%d)';
  52. SListCapacityError = 'The maximum list capacity is reached (%d)';
  53. SListCapacityPower2Error = 'The capacity has to be a power of 2, but is set to %d';
  54. SListCountError = 'List count too large (%d)';
  55. type
  56. EListError = class(Exception);
  57. const
  58. MaxListSize = Maxint div 16;
  59. type
  60. PPointerList = ^TPointerList;
  61. TPointerList = array[0..MaxListSize - 1] of Pointer;
  62. TListSortCompare = function (Item1, Item2: Pointer): Integer;
  63. TListCallback = procedure(data,arg:pointer) of object;
  64. TListStaticCallback = procedure(data,arg:pointer);
  65. TFPList = class(TObject)
  66. private
  67. FList: PPointerList;
  68. FCount: Integer;
  69. FCapacity: Integer;
  70. protected
  71. function Get(Index: Integer): Pointer;
  72. procedure Put(Index: Integer; Item: Pointer);
  73. procedure SetCapacity(NewCapacity: Integer);
  74. procedure SetCount(NewCount: Integer);
  75. Procedure RaiseIndexError(Index : Integer);
  76. public
  77. destructor Destroy; override;
  78. function Add(Item: Pointer): Integer;
  79. procedure Clear;
  80. procedure Delete(Index: Integer);
  81. class procedure Error(const Msg: string; Data: PtrInt);
  82. procedure Exchange(Index1, Index2: Integer);
  83. function Expand: TFPList;
  84. function Extract(item: Pointer): Pointer;
  85. function First: Pointer;
  86. function IndexOf(Item: Pointer): Integer;
  87. procedure Insert(Index: Integer; Item: Pointer);
  88. function Last: Pointer;
  89. procedure Move(CurIndex, NewIndex: Integer);
  90. procedure Assign(Obj:TFPList);
  91. function Remove(Item: Pointer): Integer;
  92. procedure Pack;
  93. procedure Sort(Compare: TListSortCompare);
  94. procedure ForEachCall(proc2call:TListCallback;arg:pointer);
  95. procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  96. property Capacity: Integer read FCapacity write SetCapacity;
  97. property Count: Integer read FCount write SetCount;
  98. property Items[Index: Integer]: Pointer read Get write Put; default;
  99. property List: PPointerList read FList;
  100. end;
  101. {*******************************************************
  102. TFPObjectList (From fcl/inc/contnrs.pp)
  103. ********************************************************}
  104. TObjectListCallback = procedure(data:TObject;arg:pointer) of object;
  105. TObjectListStaticCallback = procedure(data:TObject;arg:pointer);
  106. TFPObjectList = class(TObject)
  107. private
  108. FFreeObjects : Boolean;
  109. FList: TFPList;
  110. function GetCount: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  111. procedure SetCount(const AValue: integer);
  112. protected
  113. function GetItem(Index: Integer): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  114. procedure SetItem(Index: Integer; AObject: TObject);
  115. procedure SetCapacity(NewCapacity: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  116. function GetCapacity: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  117. public
  118. constructor Create;
  119. constructor Create(FreeObjects : Boolean);
  120. destructor Destroy; override;
  121. procedure Clear;
  122. function Add(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  123. procedure Delete(Index: Integer);
  124. procedure Exchange(Index1, Index2: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  125. function Expand: TFPObjectList;{$ifdef CCLASSESINLINE}inline;{$endif}
  126. function Extract(Item: TObject): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  127. function Remove(AObject: TObject): Integer;
  128. function IndexOf(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  129. function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
  130. procedure Insert(Index: Integer; AObject: TObject); {$ifdef CCLASSESINLINE}inline;{$endif}
  131. function First: TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  132. function Last: TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  133. procedure Move(CurIndex, NewIndex: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  134. procedure Assign(Obj:TFPObjectList);
  135. procedure Pack; {$ifdef CCLASSESINLINE}inline;{$endif}
  136. procedure Sort(Compare: TListSortCompare); {$ifdef CCLASSESINLINE}inline;{$endif}
  137. procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  138. procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  139. property Capacity: Integer read GetCapacity write SetCapacity;
  140. property Count: Integer read GetCount write SetCount;
  141. property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
  142. property Items[Index: Integer]: TObject read GetItem write SetItem; default;
  143. property List: TFPList read FList;
  144. end;
  145. type
  146. THashItem=record
  147. HashValue : LongWord;
  148. StrIndex : Integer;
  149. NextIndex : Integer;
  150. Data : Pointer;
  151. end;
  152. PHashItem=^THashItem;
  153. const
  154. MaxHashListSize = Maxint div 16;
  155. MaxHashStrSize = Maxint;
  156. MaxHashTableSize = Maxint div 4;
  157. MaxItemsPerHash = 3;
  158. type
  159. PHashItemList = ^THashItemList;
  160. THashItemList = array[0..MaxHashListSize - 1] of THashItem;
  161. PHashTable = ^THashTable;
  162. THashTable = array[0..MaxHashTableSize - 1] of Integer;
  163. TFPHashList = class(TObject)
  164. private
  165. { ItemList }
  166. FHashList : PHashItemList;
  167. FCount,
  168. FCapacity : Integer;
  169. FCapacityMask: LongWord;
  170. { Hash }
  171. FHashTable : PHashTable;
  172. FHashCapacity : Integer;
  173. { Strings }
  174. FStrs : PChar;
  175. FStrCount,
  176. FStrCapacity : Integer;
  177. function InternalFind(AHash:LongWord;const AName:shortstring;out PrevIndex:Integer):Integer;
  178. protected
  179. function Get(Index: Integer): Pointer;
  180. procedure Put(Index: Integer; Item: Pointer);
  181. procedure SetCapacity(NewCapacity: Integer);
  182. procedure SetCount(NewCount: Integer);
  183. Procedure RaiseIndexError(Index : Integer);
  184. function AddStr(const s:shortstring): Integer;
  185. procedure AddToHashTable(Index: Integer);
  186. procedure StrExpand(MinIncSize:Integer);
  187. procedure SetStrCapacity(NewCapacity: Integer);
  188. procedure SetHashCapacity(NewCapacity: Integer);
  189. procedure ReHash;
  190. public
  191. constructor Create;
  192. destructor Destroy; override;
  193. function Add(const AName:shortstring;Item: Pointer): Integer;
  194. procedure Clear;
  195. function NameOfIndex(Index: Integer): ShortString;
  196. function HashOfIndex(Index: Integer): LongWord;
  197. function GetNextCollision(Index: Integer): Integer;
  198. procedure Delete(Index: Integer);
  199. class procedure Error(const Msg: string; Data: PtrInt);
  200. function Expand: TFPHashList;
  201. function Extract(item: Pointer): Pointer;
  202. function IndexOf(Item: Pointer): Integer;
  203. function Find(const AName:shortstring): Pointer;
  204. function FindIndexOf(const AName:shortstring): Integer;
  205. function FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
  206. function Rename(const AOldName,ANewName:shortstring): Integer;
  207. function Remove(Item: Pointer): Integer;
  208. procedure Pack;
  209. procedure ShowStatistics;
  210. procedure ForEachCall(proc2call:TListCallback;arg:pointer);
  211. procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  212. property Capacity: Integer read FCapacity write SetCapacity;
  213. property Count: Integer read FCount write SetCount;
  214. property Items[Index: Integer]: Pointer read Get write Put; default;
  215. property List: PHashItemList read FHashList;
  216. property Strs: PChar read FStrs;
  217. end;
  218. {*******************************************************
  219. TFPHashObjectList (From fcl/inc/contnrs.pp)
  220. ********************************************************}
  221. TFPHashObjectList = class;
  222. { TFPHashObject }
  223. TFPHashObject = class
  224. private
  225. FOwner : TFPHashObjectList;
  226. FCachedStr : pshortstring;
  227. FStrIndex : Integer;
  228. procedure InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:shortstring);
  229. protected
  230. function GetName:shortstring;virtual;
  231. function GetHash:Longword;virtual;
  232. public
  233. constructor CreateNotOwned;
  234. constructor Create(HashObjectList:TFPHashObjectList;const s:shortstring);
  235. procedure ChangeOwner(HashObjectList:TFPHashObjectList);
  236. procedure ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:shortstring); {$ifdef CCLASSESINLINE}inline;{$endif}
  237. procedure Rename(const ANewName:shortstring);
  238. property Name:shortstring read GetName;
  239. property Hash:Longword read GetHash;
  240. end;
  241. TFPHashObjectList = class(TObject)
  242. private
  243. FFreeObjects : Boolean;
  244. FHashList: TFPHashList;
  245. function GetCount: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  246. procedure SetCount(const AValue: integer);
  247. protected
  248. function GetItem(Index: Integer): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  249. procedure SetItem(Index: Integer; AObject: TObject);
  250. procedure SetCapacity(NewCapacity: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
  251. function GetCapacity: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  252. public
  253. constructor Create(FreeObjects : boolean = True);
  254. destructor Destroy; override;
  255. procedure Clear;
  256. function Add(const AName:shortstring;AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  257. function NameOfIndex(Index: Integer): ShortString; {$ifdef CCLASSESINLINE}inline;{$endif}
  258. function HashOfIndex(Index: Integer): LongWord; {$ifdef CCLASSESINLINE}inline;{$endif}
  259. function GetNextCollision(Index: Integer): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  260. procedure Delete(Index: Integer);
  261. function Expand: TFPHashObjectList; {$ifdef CCLASSESINLINE}inline;{$endif}
  262. function Extract(Item: TObject): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  263. function Remove(AObject: TObject): Integer;
  264. function IndexOf(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  265. function Find(const s:shortstring): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
  266. function FindIndexOf(const s:shortstring): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  267. function FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
  268. function Rename(const AOldName,ANewName:shortstring): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  269. function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
  270. procedure Pack; {$ifdef CCLASSESINLINE}inline;{$endif}
  271. procedure ShowStatistics; {$ifdef CCLASSESINLINE}inline;{$endif}
  272. procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  273. procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
  274. property Capacity: Integer read GetCapacity write SetCapacity;
  275. property Count: Integer read GetCount write SetCount;
  276. property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
  277. property Items[Index: Integer]: TObject read GetItem write SetItem; default;
  278. property List: TFPHashList read FHashList;
  279. end;
  280. {********************************************
  281. TLinkedList
  282. ********************************************}
  283. type
  284. TLinkedListItem = class
  285. public
  286. Previous,
  287. Next : TLinkedListItem;
  288. Constructor Create;
  289. Destructor Destroy;override;
  290. Function GetCopy:TLinkedListItem;virtual;
  291. end;
  292. TLinkedListItemClass = class of TLinkedListItem;
  293. TLinkedList = class
  294. private
  295. FCount : integer;
  296. FFirst,
  297. FLast : TLinkedListItem;
  298. FNoClear : boolean;
  299. public
  300. constructor Create;
  301. destructor Destroy;override;
  302. { true when the List is empty }
  303. function Empty:boolean; {$ifdef CCLASSESINLINE}inline;{$endif}
  304. { deletes all Items }
  305. procedure Clear;
  306. { inserts an Item }
  307. procedure Insert(Item:TLinkedListItem);
  308. { inserts an Item before Loc }
  309. procedure InsertBefore(Item,Loc : TLinkedListItem);
  310. { inserts an Item after Loc }
  311. procedure InsertAfter(Item,Loc : TLinkedListItem);virtual;
  312. { concats an Item }
  313. procedure Concat(Item:TLinkedListItem);
  314. { deletes an Item }
  315. procedure Remove(Item:TLinkedListItem);
  316. { Gets First Item }
  317. function GetFirst:TLinkedListItem;
  318. { Gets last Item }
  319. function GetLast:TLinkedListItem;
  320. { inserts another List at the begin and make this List empty }
  321. procedure insertList(p : TLinkedList);
  322. { inserts another List before the provided item and make this List empty }
  323. procedure insertListBefore(Item:TLinkedListItem;p : TLinkedList);
  324. { inserts another List after the provided item and make this List empty }
  325. procedure insertListAfter(Item:TLinkedListItem;p : TLinkedList);
  326. { concats another List at the end and make this List empty }
  327. procedure concatList(p : TLinkedList);
  328. { concats another List at the start and makes a copy
  329. the list is ordered in reverse.
  330. }
  331. procedure insertListcopy(p : TLinkedList);
  332. { concats another List at the end and makes a copy }
  333. procedure concatListcopy(p : TLinkedList);
  334. property First:TLinkedListItem read FFirst;
  335. property Last:TLinkedListItem read FLast;
  336. property Count:Integer read FCount;
  337. property NoClear:boolean write FNoClear;
  338. end;
  339. {********************************************
  340. TCmdStrList
  341. ********************************************}
  342. { string containerItem }
  343. TCmdStrListItem = class(TLinkedListItem)
  344. FPStr : TCmdStr;
  345. public
  346. constructor Create(const s:TCmdStr);
  347. destructor Destroy;override;
  348. function GetCopy:TLinkedListItem;override;
  349. function Str:TCmdStr; {$ifdef CCLASSESINLINE}inline;{$endif}
  350. end;
  351. { string container }
  352. TCmdStrList = class(TLinkedList)
  353. private
  354. FDoubles : boolean; { if this is set to true, doubles are allowed }
  355. public
  356. constructor Create;
  357. constructor Create_No_Double;
  358. { inserts an Item }
  359. procedure Insert(const s:TCmdStr);
  360. { concats an Item }
  361. procedure Concat(const s:TCmdStr);
  362. { deletes an Item }
  363. procedure Remove(const s:TCmdStr);
  364. { Gets First Item }
  365. function GetFirst:TCmdStr;
  366. { Gets last Item }
  367. function GetLast:TCmdStr;
  368. { true if string is in the container, compare case sensitive }
  369. function FindCase(const s:TCmdStr):TCmdStrListItem;
  370. { true if string is in the container }
  371. function Find(const s:TCmdStr):TCmdStrListItem;
  372. { inserts an item }
  373. procedure InsertItem(item:TCmdStrListItem); {$ifdef CCLASSESINLINE}inline;{$endif}
  374. { concats an item }
  375. procedure ConcatItem(item:TCmdStrListItem); {$ifdef CCLASSESINLINE}inline;{$endif}
  376. property Doubles:boolean read FDoubles write FDoubles;
  377. end;
  378. {********************************************
  379. DynamicArray
  380. ********************************************}
  381. type
  382. { can't use sizeof(integer) because it crashes gdb }
  383. tdynamicblockdata=array[0..1024*1024-1] of byte;
  384. pdynamicblock = ^tdynamicblock;
  385. tdynamicblock = record
  386. pos,
  387. size,
  388. used : longword;
  389. Next : pdynamicblock;
  390. data : tdynamicblockdata;
  391. end;
  392. const
  393. dynamicblockbasesize = sizeof(tdynamicblock)-sizeof(tdynamicblockdata);
  394. type
  395. tdynamicarray = class
  396. private
  397. FPosn : longword;
  398. FPosnblock : pdynamicblock;
  399. FCurrBlocksize,
  400. FMaxBlocksize : longword;
  401. FFirstblock,
  402. FLastblock : pdynamicblock;
  403. procedure grow;
  404. public
  405. constructor Create(Ablocksize:longword);
  406. destructor Destroy;override;
  407. procedure reset;
  408. function size:longword;
  409. procedure align(i:longword);
  410. procedure seek(i:longword);
  411. function read(var d;len:longword):longword;
  412. procedure write(const d;len:longword);
  413. procedure writestr(const s:string); {$ifdef CCLASSESINLINE}inline;{$endif}
  414. procedure readstream(f:TCStream;maxlen:longword);
  415. procedure writestream(f:TCStream);
  416. property CurrBlockSize : longword read FCurrBlocksize;
  417. property FirstBlock : PDynamicBlock read FFirstBlock;
  418. property Pos : longword read FPosn;
  419. end;
  420. {******************************************************************
  421. THashSet (keys not limited to ShortString, no indexed access)
  422. *******************************************************************}
  423. PPHashSetItem = ^PHashSetItem;
  424. PHashSetItem = ^THashSetItem;
  425. THashSetItem = record
  426. Next: PHashSetItem;
  427. Key: Pointer;
  428. KeyLength: Integer;
  429. HashValue: LongWord;
  430. Data: TObject;
  431. end;
  432. THashSet = class(TObject)
  433. private
  434. FCount: LongWord;
  435. FOwnsObjects: Boolean;
  436. FOwnsKeys: Boolean;
  437. function Lookup(Key: Pointer; KeyLen: Integer; var Found: Boolean;
  438. CanCreate: Boolean): PHashSetItem;
  439. procedure Resize(NewCapacity: LongWord);
  440. protected
  441. FBucket: PPHashSetItem;
  442. FBucketCount: LongWord;
  443. class procedure FreeItem(item:PHashSetItem); virtual;
  444. class function SizeOfItem: Integer; virtual;
  445. public
  446. constructor Create(InitSize: Integer; OwnKeys, OwnObjects: Boolean);
  447. destructor Destroy; override;
  448. procedure Clear;
  449. { finds an entry by key }
  450. function Find(Key: Pointer; KeyLen: Integer): PHashSetItem;
  451. { finds an entry, creates one if not exists }
  452. function FindOrAdd(Key: Pointer; KeyLen: Integer;
  453. var Found: Boolean): PHashSetItem;
  454. { finds an entry, creates one if not exists }
  455. function FindOrAdd(Key: Pointer; KeyLen: Integer): PHashSetItem;
  456. { returns Data by given Key }
  457. function Get(Key: Pointer; KeyLen: Integer): TObject;
  458. { removes an entry, returns False if entry wasn't there }
  459. function Remove(Entry: PHashSetItem): Boolean;
  460. property Count: LongWord read FCount;
  461. end;
  462. {******************************************************************
  463. TTagHasSet
  464. *******************************************************************}
  465. PPTagHashSetItem = ^PTagHashSetItem;
  466. PTagHashSetItem = ^TTagHashSetItem;
  467. TTagHashSetItem = record
  468. Next: PTagHashSetItem;
  469. Key: Pointer;
  470. KeyLength: Integer;
  471. HashValue: LongWord;
  472. Data: TObject;
  473. Tag: LongWord;
  474. end;
  475. TTagHashSet = class(THashSet)
  476. private
  477. function Lookup(Key: Pointer; KeyLen: Integer; Tag: LongWord; var Found: Boolean;
  478. CanCreate: Boolean): PTagHashSetItem;
  479. protected
  480. class procedure FreeItem(item:PHashSetItem); override;
  481. class function SizeOfItem: Integer; override;
  482. public
  483. { finds an entry by key }
  484. function Find(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem; reintroduce;
  485. { finds an entry, creates one if not exists }
  486. function FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord;
  487. var Found: Boolean): PTagHashSetItem; reintroduce;
  488. { finds an entry, creates one if not exists }
  489. function FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem; reintroduce;
  490. { returns Data by given Key }
  491. function Get(Key: Pointer; KeyLen: Integer; Tag: LongWord): TObject; reintroduce;
  492. end;
  493. {******************************************************************
  494. tbitset
  495. *******************************************************************}
  496. tbitset = class
  497. private
  498. fdata: pbyte;
  499. fdatasize: longint;
  500. public
  501. constructor create(initsize: longint);
  502. constructor create_bytesize(bytesize: longint);
  503. destructor destroy; override;
  504. procedure clear;
  505. procedure grow(nsize: longint);
  506. { sets a bit }
  507. procedure include(index: longint);
  508. { clears a bit }
  509. procedure exclude(index: longint);
  510. { finds an entry, creates one if not exists }
  511. function isset(index: longint): boolean;
  512. procedure addset(aset: tbitset);
  513. procedure subset(aset: tbitset);
  514. property data: pbyte read fdata;
  515. property datasize: longint read fdatasize;
  516. end;
  517. function FPHash(const s:shortstring):LongWord;
  518. function FPHash(P: PChar; Len: Integer): LongWord;
  519. function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord;
  520. implementation
  521. {*****************************************************************************
  522. Memory debug
  523. *****************************************************************************}
  524. constructor tmemdebug.create(const s:string);
  525. begin
  526. infostr:=s;
  527. totalmem:=0;
  528. Start;
  529. end;
  530. procedure tmemdebug.start;
  531. var
  532. status : TFPCHeapStatus;
  533. begin
  534. status:=GetFPCHeapStatus;
  535. startmem:=status.CurrHeapUsed;
  536. end;
  537. procedure tmemdebug.stop;
  538. var
  539. status : TFPCHeapStatus;
  540. begin
  541. if startmem<>0 then
  542. begin
  543. status:=GetFPCHeapStatus;
  544. inc(TotalMem,startmem-status.CurrHeapUsed);
  545. startmem:=0;
  546. end;
  547. end;
  548. destructor tmemdebug.destroy;
  549. begin
  550. Stop;
  551. show;
  552. end;
  553. procedure tmemdebug.show;
  554. begin
  555. write('memory [',infostr,'] ');
  556. if TotalMem>0 then
  557. writeln(DStr(TotalMem shr 10),' Kb released')
  558. else
  559. writeln(DStr((-TotalMem) shr 10),' Kb allocated');
  560. end;
  561. {*****************************************************************************
  562. TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
  563. *****************************************************************************}
  564. procedure TFPList.RaiseIndexError(Index : Integer);
  565. begin
  566. Error(SListIndexError, Index);
  567. end;
  568. function TFPList.Get(Index: Integer): Pointer;
  569. begin
  570. If (Index < 0) or (Index >= FCount) then
  571. RaiseIndexError(Index);
  572. Result:=FList^[Index];
  573. end;
  574. procedure TFPList.Put(Index: Integer; Item: Pointer);
  575. begin
  576. if (Index < 0) or (Index >= FCount) then
  577. RaiseIndexError(Index);
  578. Flist^[Index] := Item;
  579. end;
  580. function TFPList.Extract(item: Pointer): Pointer;
  581. var
  582. i : Integer;
  583. begin
  584. result := nil;
  585. i := IndexOf(item);
  586. if i >= 0 then
  587. begin
  588. Result := item;
  589. FList^[i] := nil;
  590. Delete(i);
  591. end;
  592. end;
  593. procedure TFPList.SetCapacity(NewCapacity: Integer);
  594. begin
  595. If (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
  596. Error (SListCapacityError, NewCapacity);
  597. if NewCapacity = FCapacity then
  598. exit;
  599. ReallocMem(FList, SizeOf(Pointer)*NewCapacity);
  600. FCapacity := NewCapacity;
  601. end;
  602. procedure TFPList.SetCount(NewCount: Integer);
  603. begin
  604. if (NewCount < 0) or (NewCount > MaxListSize)then
  605. Error(SListCountError, NewCount);
  606. If NewCount > FCount then
  607. begin
  608. If NewCount > FCapacity then
  609. SetCapacity(NewCount);
  610. If FCount < NewCount then
  611. FillChar(Flist^[FCount], (NewCount-FCount) * sizeof(Pointer), 0);
  612. end;
  613. FCount := Newcount;
  614. end;
  615. destructor TFPList.Destroy;
  616. begin
  617. Self.Clear;
  618. inherited Destroy;
  619. end;
  620. function TFPList.Add(Item: Pointer): Integer;
  621. begin
  622. if FCount = FCapacity then
  623. Self.Expand;
  624. FList^[FCount] := Item;
  625. Result := FCount;
  626. inc(FCount);
  627. end;
  628. procedure TFPList.Clear;
  629. begin
  630. if Assigned(FList) then
  631. begin
  632. SetCount(0);
  633. SetCapacity(0);
  634. FList := nil;
  635. end;
  636. end;
  637. procedure TFPList.Delete(Index: Integer);
  638. begin
  639. If (Index<0) or (Index>=FCount) then
  640. Error (SListIndexError, Index);
  641. dec(FCount);
  642. System.Move (FList^[Index+1], FList^[Index], (FCount - Index) * SizeOf(Pointer));
  643. { Shrink the list if appropriate }
  644. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  645. begin
  646. FCapacity := FCapacity shr 1;
  647. ReallocMem(FList, SizeOf(Pointer) * FCapacity);
  648. end;
  649. end;
  650. class procedure TFPList.Error(const Msg: string; Data: PtrInt);
  651. begin
  652. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  653. end;
  654. procedure TFPList.Exchange(Index1, Index2: Integer);
  655. var
  656. Temp : Pointer;
  657. begin
  658. If ((Index1 >= FCount) or (Index1 < 0)) then
  659. Error(SListIndexError, Index1);
  660. If ((Index2 >= FCount) or (Index2 < 0)) then
  661. Error(SListIndexError, Index2);
  662. Temp := FList^[Index1];
  663. FList^[Index1] := FList^[Index2];
  664. FList^[Index2] := Temp;
  665. end;
  666. function TFPList.Expand: TFPList;
  667. var
  668. IncSize : Longint;
  669. begin
  670. Result := Self;
  671. if FCount < FCapacity then
  672. exit;
  673. IncSize := sizeof(ptrint)*2;
  674. if FCapacity > 127 then
  675. Inc(IncSize, FCapacity shr 2)
  676. else if FCapacity > sizeof(ptrint)*4 then
  677. Inc(IncSize, FCapacity shr 1)
  678. else if FCapacity >= sizeof(ptrint) then
  679. inc(IncSize,sizeof(ptrint));
  680. SetCapacity(FCapacity + IncSize);
  681. end;
  682. function TFPList.First: Pointer;
  683. begin
  684. If FCount<>0 then
  685. Result := Items[0]
  686. else
  687. Result := Nil;
  688. end;
  689. function TFPList.IndexOf(Item: Pointer): Integer;
  690. var
  691. psrc : PPointer;
  692. Index : Integer;
  693. begin
  694. Result:=-1;
  695. psrc:=@FList^[0];
  696. For Index:=0 To FCount-1 Do
  697. begin
  698. if psrc^=Item then
  699. begin
  700. Result:=Index;
  701. exit;
  702. end;
  703. inc(psrc);
  704. end;
  705. end;
  706. procedure TFPList.Insert(Index: Integer; Item: Pointer);
  707. begin
  708. if (Index < 0) or (Index > FCount )then
  709. Error(SlistIndexError, Index);
  710. iF FCount = FCapacity then Self.Expand;
  711. if Index<FCount then
  712. System.Move(Flist^[Index], Flist^[Index+1], (FCount - Index) * SizeOf(Pointer));
  713. FList^[Index] := Item;
  714. FCount := FCount + 1;
  715. end;
  716. function TFPList.Last: Pointer;
  717. begin
  718. If FCount<>0 then
  719. Result := Items[FCount - 1]
  720. else
  721. Result := nil
  722. end;
  723. procedure TFPList.Move(CurIndex, NewIndex: Integer);
  724. var
  725. Temp : Pointer;
  726. begin
  727. if ((CurIndex < 0) or (CurIndex > Count - 1)) then
  728. Error(SListIndexError, CurIndex);
  729. if (NewINdex < 0) then
  730. Error(SlistIndexError, NewIndex);
  731. Temp := FList^[CurIndex];
  732. FList^[CurIndex] := nil;
  733. Self.Delete(CurIndex);
  734. Self.Insert(NewIndex, nil);
  735. FList^[NewIndex] := Temp;
  736. end;
  737. function TFPList.Remove(Item: Pointer): Integer;
  738. begin
  739. Result := IndexOf(Item);
  740. If Result <> -1 then
  741. Self.Delete(Result);
  742. end;
  743. procedure TFPList.Pack;
  744. var
  745. NewCount,
  746. i : integer;
  747. pdest,
  748. psrc : PPointer;
  749. begin
  750. NewCount:=0;
  751. psrc:=@FList^[0];
  752. pdest:=psrc;
  753. For I:=0 To FCount-1 Do
  754. begin
  755. if assigned(psrc^) then
  756. begin
  757. pdest^:=psrc^;
  758. inc(pdest);
  759. inc(NewCount);
  760. end;
  761. inc(psrc);
  762. end;
  763. FCount:=NewCount;
  764. end;
  765. Procedure QuickSort(FList: PPointerList; L, R : Longint;Compare: TListSortCompare);
  766. var
  767. I, J, P: Longint;
  768. PItem, Q : Pointer;
  769. begin
  770. repeat
  771. I := L;
  772. J := R;
  773. P := (L + R) div 2;
  774. repeat
  775. PItem := FList^[P];
  776. while Compare(PItem, FList^[i]) > 0 do
  777. I := I + 1;
  778. while Compare(PItem, FList^[J]) < 0 do
  779. J := J - 1;
  780. If I <= J then
  781. begin
  782. Q := FList^[I];
  783. Flist^[I] := FList^[J];
  784. FList^[J] := Q;
  785. if P = I then
  786. P := J
  787. else if P = J then
  788. P := I;
  789. I := I + 1;
  790. J := J - 1;
  791. end;
  792. until I > J;
  793. if L < J then
  794. QuickSort(FList, L, J, Compare);
  795. L := I;
  796. until I >= R;
  797. end;
  798. procedure TFPList.Sort(Compare: TListSortCompare);
  799. begin
  800. if Not Assigned(FList) or (FCount < 2) then exit;
  801. QuickSort(Flist, 0, FCount-1, Compare);
  802. end;
  803. procedure TFPList.Assign(Obj: TFPList);
  804. var
  805. i: Integer;
  806. begin
  807. Clear;
  808. for I := 0 to Obj.Count - 1 do
  809. Add(Obj[i]);
  810. end;
  811. procedure TFPList.ForEachCall(proc2call:TListCallback;arg:pointer);
  812. var
  813. i : integer;
  814. p : pointer;
  815. begin
  816. For I:=0 To Count-1 Do
  817. begin
  818. p:=FList^[i];
  819. if assigned(p) then
  820. proc2call(p,arg);
  821. end;
  822. end;
  823. procedure TFPList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  824. var
  825. i : integer;
  826. p : pointer;
  827. begin
  828. For I:=0 To Count-1 Do
  829. begin
  830. p:=FList^[i];
  831. if assigned(p) then
  832. proc2call(p,arg);
  833. end;
  834. end;
  835. {*****************************************************************************
  836. TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
  837. *****************************************************************************}
  838. constructor TFPObjectList.Create(FreeObjects : boolean);
  839. begin
  840. Create;
  841. FFreeObjects := Freeobjects;
  842. end;
  843. destructor TFPObjectList.Destroy;
  844. begin
  845. if (FList <> nil) then
  846. begin
  847. Clear;
  848. FList.Destroy;
  849. end;
  850. inherited Destroy;
  851. end;
  852. procedure TFPObjectList.Clear;
  853. var
  854. i: integer;
  855. begin
  856. if FFreeObjects then
  857. for i := 0 to FList.Count - 1 do
  858. TObject(FList[i]).Free;
  859. FList.Clear;
  860. end;
  861. constructor TFPObjectList.Create;
  862. begin
  863. inherited Create;
  864. FList := TFPList.Create;
  865. FFreeObjects := True;
  866. end;
  867. function TFPObjectList.GetCount: integer;
  868. begin
  869. Result := FList.Count;
  870. end;
  871. procedure TFPObjectList.SetCount(const AValue: integer);
  872. begin
  873. if FList.Count <> AValue then
  874. FList.Count := AValue;
  875. end;
  876. function TFPObjectList.GetItem(Index: Integer): TObject;
  877. begin
  878. Result := TObject(FList[Index]);
  879. end;
  880. procedure TFPObjectList.SetItem(Index: Integer; AObject: TObject);
  881. begin
  882. if OwnsObjects then
  883. TObject(FList[Index]).Free;
  884. FList[index] := AObject;
  885. end;
  886. procedure TFPObjectList.SetCapacity(NewCapacity: Integer);
  887. begin
  888. FList.Capacity := NewCapacity;
  889. end;
  890. function TFPObjectList.GetCapacity: integer;
  891. begin
  892. Result := FList.Capacity;
  893. end;
  894. function TFPObjectList.Add(AObject: TObject): Integer;
  895. begin
  896. Result := FList.Add(AObject);
  897. end;
  898. procedure TFPObjectList.Delete(Index: Integer);
  899. begin
  900. if OwnsObjects then
  901. TObject(FList[Index]).Free;
  902. FList.Delete(Index);
  903. end;
  904. procedure TFPObjectList.Exchange(Index1, Index2: Integer);
  905. begin
  906. FList.Exchange(Index1, Index2);
  907. end;
  908. function TFPObjectList.Expand: TFPObjectList;
  909. begin
  910. FList.Expand;
  911. Result := Self;
  912. end;
  913. function TFPObjectList.Extract(Item: TObject): TObject;
  914. begin
  915. Result := TObject(FList.Extract(Item));
  916. end;
  917. function TFPObjectList.Remove(AObject: TObject): Integer;
  918. begin
  919. Result := IndexOf(AObject);
  920. if (Result <> -1) then
  921. begin
  922. if OwnsObjects then
  923. TObject(FList[Result]).Free;
  924. FList.Delete(Result);
  925. end;
  926. end;
  927. function TFPObjectList.IndexOf(AObject: TObject): Integer;
  928. begin
  929. Result := FList.IndexOf(Pointer(AObject));
  930. end;
  931. function TFPObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  932. var
  933. I : Integer;
  934. begin
  935. I:=AStartAt;
  936. Result:=-1;
  937. If AExact then
  938. while (I<Count) and (Result=-1) do
  939. If Items[i].ClassType=AClass then
  940. Result:=I
  941. else
  942. Inc(I)
  943. else
  944. while (I<Count) and (Result=-1) do
  945. If Items[i].InheritsFrom(AClass) then
  946. Result:=I
  947. else
  948. Inc(I);
  949. end;
  950. procedure TFPObjectList.Insert(Index: Integer; AObject: TObject);
  951. begin
  952. FList.Insert(Index, Pointer(AObject));
  953. end;
  954. procedure TFPObjectList.Move(CurIndex, NewIndex: Integer);
  955. begin
  956. FList.Move(CurIndex, NewIndex);
  957. end;
  958. procedure TFPObjectList.Assign(Obj: TFPObjectList);
  959. var
  960. i: Integer;
  961. begin
  962. Clear;
  963. for I := 0 to Obj.Count - 1 do
  964. Add(Obj[i]);
  965. end;
  966. procedure TFPObjectList.Pack;
  967. begin
  968. FList.Pack;
  969. end;
  970. procedure TFPObjectList.Sort(Compare: TListSortCompare);
  971. begin
  972. FList.Sort(Compare);
  973. end;
  974. function TFPObjectList.First: TObject;
  975. begin
  976. Result := TObject(FList.First);
  977. end;
  978. function TFPObjectList.Last: TObject;
  979. begin
  980. Result := TObject(FList.Last);
  981. end;
  982. procedure TFPObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
  983. begin
  984. FList.ForEachCall(TListCallBack(proc2call),arg);
  985. end;
  986. procedure TFPObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  987. begin
  988. FList.ForEachCall(TListStaticCallBack(proc2call),arg);
  989. end;
  990. {*****************************************************************************
  991. TFPHashList
  992. *****************************************************************************}
  993. function FPHash(const s:shortstring):LongWord;
  994. Var
  995. p,pmax : pchar;
  996. begin
  997. {$push}
  998. {$q-,r-}
  999. result:=0;
  1000. p:=@s[1];
  1001. pmax:=@s[length(s)+1];
  1002. while (p<pmax) do
  1003. begin
  1004. result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
  1005. inc(p);
  1006. end;
  1007. {$pop}
  1008. end;
  1009. function FPHash(P: PChar; Len: Integer): LongWord;
  1010. Var
  1011. pmax : pchar;
  1012. begin
  1013. {$push}
  1014. {$q-,r-}
  1015. result:=0;
  1016. pmax:=p+len;
  1017. while (p<pmax) do
  1018. begin
  1019. result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
  1020. inc(p);
  1021. end;
  1022. {$pop}
  1023. end;
  1024. function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord;
  1025. Var
  1026. pmax : pchar;
  1027. begin
  1028. {$push}
  1029. {$q-,r-}
  1030. result:=Tag;
  1031. pmax:=p+len;
  1032. while (p<pmax) do
  1033. begin
  1034. result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
  1035. inc(p);
  1036. end;
  1037. {$pop}
  1038. end;
  1039. procedure TFPHashList.RaiseIndexError(Index : Integer);
  1040. begin
  1041. Error(SListIndexError, Index);
  1042. end;
  1043. function TFPHashList.Get(Index: Integer): Pointer;
  1044. begin
  1045. If (Index < 0) or (Index >= FCount) then
  1046. RaiseIndexError(Index);
  1047. Result:=FHashList^[Index].Data;
  1048. end;
  1049. procedure TFPHashList.Put(Index: Integer; Item: Pointer);
  1050. begin
  1051. if (Index < 0) or (Index >= FCount) then
  1052. RaiseIndexError(Index);
  1053. FHashList^[Index].Data:=Item;
  1054. end;
  1055. function TFPHashList.NameOfIndex(Index: Integer): shortstring;
  1056. begin
  1057. If (Index < 0) or (Index >= FCount) then
  1058. RaiseIndexError(Index);
  1059. with FHashList^[Index] do
  1060. begin
  1061. if StrIndex>=0 then
  1062. Result:=PShortString(@FStrs[StrIndex])^
  1063. else
  1064. Result:='';
  1065. end;
  1066. end;
  1067. function TFPHashList.HashOfIndex(Index: Integer): LongWord;
  1068. begin
  1069. If (Index < 0) or (Index >= FCount) then
  1070. RaiseIndexError(Index);
  1071. Result:=FHashList^[Index].HashValue;
  1072. end;
  1073. function TFPHashList.GetNextCollision(Index: Integer): Integer;
  1074. begin
  1075. Result:=-1;
  1076. if ((Index > -1) and (Index < FCount)) then
  1077. Result:=FHashList^[Index].NextIndex;
  1078. end;
  1079. function TFPHashList.Extract(item: Pointer): Pointer;
  1080. var
  1081. i : Integer;
  1082. begin
  1083. result := nil;
  1084. i := IndexOf(item);
  1085. if i >= 0 then
  1086. begin
  1087. Result := item;
  1088. Delete(i);
  1089. end;
  1090. end;
  1091. procedure TFPHashList.SetCapacity(NewCapacity: Integer);
  1092. var
  1093. power: longint;
  1094. begin
  1095. { use a power of two to be able to quickly calculate the hash table index }
  1096. if NewCapacity <> 0 then
  1097. NewCapacity := nextpowerof2((NewCapacity+(MaxItemsPerHash-1)) div MaxItemsPerHash, power) * MaxItemsPerHash;
  1098. if (NewCapacity < FCount) or (NewCapacity > MaxHashListSize) then
  1099. Error (SListCapacityError, NewCapacity);
  1100. if NewCapacity = FCapacity then
  1101. exit;
  1102. ReallocMem(FHashList, NewCapacity*SizeOf(THashItem));
  1103. FCapacity := NewCapacity;
  1104. { Maybe expand hash also }
  1105. if FCapacity>FHashCapacity*MaxItemsPerHash then
  1106. SetHashCapacity(FCapacity div MaxItemsPerHash);
  1107. end;
  1108. procedure TFPHashList.SetCount(NewCount: Integer);
  1109. begin
  1110. if (NewCount < 0) or (NewCount > MaxHashListSize)then
  1111. Error(SListCountError, NewCount);
  1112. If NewCount > FCount then
  1113. begin
  1114. If NewCount > FCapacity then
  1115. SetCapacity(NewCount);
  1116. If FCount < NewCount then
  1117. { FCapacity is NewCount rounded up to the next power of 2 }
  1118. FillChar(FHashList^[FCount], (FCapacity-FCount) div Sizeof(THashItem), 0);
  1119. end;
  1120. FCount := Newcount;
  1121. end;
  1122. procedure TFPHashList.SetStrCapacity(NewCapacity: Integer);
  1123. begin
  1124. If (NewCapacity < FStrCount) or (NewCapacity > MaxHashStrSize) then
  1125. Error (SListCapacityError, NewCapacity);
  1126. if NewCapacity = FStrCapacity then
  1127. exit;
  1128. ReallocMem(FStrs, NewCapacity);
  1129. FStrCapacity := NewCapacity;
  1130. end;
  1131. procedure TFPHashList.SetHashCapacity(NewCapacity: Integer);
  1132. var
  1133. power: longint;
  1134. begin
  1135. If (NewCapacity < 1) then
  1136. Error (SListCapacityError, NewCapacity);
  1137. if FHashCapacity=NewCapacity then
  1138. exit;
  1139. if (NewCapacity<>0) and
  1140. not ispowerof2(NewCapacity,power) then
  1141. Error(SListCapacityPower2Error, NewCapacity);
  1142. FHashCapacity:=NewCapacity;
  1143. ReallocMem(FHashTable, FHashCapacity*sizeof(Integer));
  1144. FCapacityMask:=(1 shl power)-1;
  1145. ReHash;
  1146. end;
  1147. procedure TFPHashList.ReHash;
  1148. var
  1149. i : Integer;
  1150. begin
  1151. FillDword(FHashTable^,FHashCapacity,LongWord(-1));
  1152. For i:=0 To FCount-1 Do
  1153. AddToHashTable(i);
  1154. end;
  1155. constructor TFPHashList.Create;
  1156. begin
  1157. SetHashCapacity(1);
  1158. end;
  1159. destructor TFPHashList.Destroy;
  1160. begin
  1161. Clear;
  1162. if assigned(FHashTable) then
  1163. FreeMem(FHashTable);
  1164. inherited Destroy;
  1165. end;
  1166. function TFPHashList.AddStr(const s:shortstring): Integer;
  1167. var
  1168. Len : Integer;
  1169. begin
  1170. len:=length(s)+1;
  1171. if FStrCount+Len >= FStrCapacity then
  1172. StrExpand(Len);
  1173. System.Move(s[0],FStrs[FStrCount],Len);
  1174. result:=FStrCount;
  1175. inc(FStrCount,Len);
  1176. end;
  1177. procedure TFPHashList.AddToHashTable(Index: Integer);
  1178. var
  1179. HashIndex : Integer;
  1180. begin
  1181. with FHashList^[Index] do
  1182. begin
  1183. if not assigned(Data) then
  1184. exit;
  1185. HashIndex:=HashValue and FCapacityMask;
  1186. NextIndex:=FHashTable^[HashIndex];
  1187. FHashTable^[HashIndex]:=Index;
  1188. end;
  1189. end;
  1190. function TFPHashList.Add(const AName:shortstring;Item: Pointer): Integer;
  1191. begin
  1192. if FCount = FCapacity then
  1193. Expand;
  1194. with FHashList^[FCount] do
  1195. begin
  1196. HashValue:=FPHash(AName);
  1197. Data:=Item;
  1198. StrIndex:=AddStr(AName);
  1199. end;
  1200. AddToHashTable(FCount);
  1201. Result := FCount;
  1202. inc(FCount);
  1203. end;
  1204. procedure TFPHashList.Clear;
  1205. begin
  1206. if Assigned(FHashList) then
  1207. begin
  1208. FCount:=0;
  1209. SetCapacity(0);
  1210. FHashList := nil;
  1211. end;
  1212. SetHashCapacity(1);
  1213. FHashTable^[0]:=-1; // sethashcapacity does not always call rehash
  1214. if Assigned(FStrs) then
  1215. begin
  1216. FStrCount:=0;
  1217. SetStrCapacity(0);
  1218. FStrs := nil;
  1219. end;
  1220. end;
  1221. procedure TFPHashList.Delete(Index: Integer);
  1222. begin
  1223. If (Index<0) or (Index>=FCount) then
  1224. Error (SListIndexError, Index);
  1225. { Remove from HashList }
  1226. dec(FCount);
  1227. System.Move (FHashList^[Index+1], FHashList^[Index], (FCount - Index) * Sizeof(THashItem));
  1228. { All indexes are updated, we need to build the hashtable again }
  1229. Rehash;
  1230. { Shrink the list if appropriate }
  1231. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  1232. begin
  1233. FCapacity := FCapacity shr 1;
  1234. ReallocMem(FHashList, Sizeof(THashItem) * FCapacity);
  1235. end;
  1236. end;
  1237. function TFPHashList.Remove(Item: Pointer): Integer;
  1238. begin
  1239. Result := IndexOf(Item);
  1240. If Result <> -1 then
  1241. Self.Delete(Result);
  1242. end;
  1243. class procedure TFPHashList.Error(const Msg: string; Data: PtrInt);
  1244. begin
  1245. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  1246. end;
  1247. function TFPHashList.Expand: TFPHashList;
  1248. var
  1249. IncSize : Longint;
  1250. begin
  1251. Result := Self;
  1252. if FCount < FCapacity then
  1253. exit;
  1254. IncSize := sizeof(ptrint)*2;
  1255. SetCapacity(FCapacity + IncSize);
  1256. end;
  1257. procedure TFPHashList.StrExpand(MinIncSize:Integer);
  1258. var
  1259. IncSize : Longint;
  1260. begin
  1261. if FStrCount+MinIncSize < FStrCapacity then
  1262. exit;
  1263. IncSize := 64;
  1264. if FStrCapacity > 255 then
  1265. Inc(IncSize, FStrCapacity shr 2);
  1266. SetStrCapacity(FStrCapacity + IncSize + MinIncSize);
  1267. end;
  1268. function TFPHashList.IndexOf(Item: Pointer): Integer;
  1269. var
  1270. psrc : PHashItem;
  1271. Index : integer;
  1272. begin
  1273. Result:=-1;
  1274. psrc:=@FHashList^[0];
  1275. For Index:=0 To FCount-1 Do
  1276. begin
  1277. if psrc^.Data=Item then
  1278. begin
  1279. Result:=Index;
  1280. exit;
  1281. end;
  1282. inc(psrc);
  1283. end;
  1284. end;
  1285. function TFPHashList.InternalFind(AHash:LongWord;const AName:shortstring;out PrevIndex:Integer):Integer;
  1286. begin
  1287. prefetch(AName);
  1288. Result:=FHashTable^[AHash and FCapacityMask];
  1289. PrevIndex:=-1;
  1290. while Result<>-1 do
  1291. begin
  1292. with FHashList^[Result] do
  1293. begin
  1294. if assigned(Data) and
  1295. (HashValue=AHash) and
  1296. (AName=PShortString(@FStrs[StrIndex])^) then
  1297. exit;
  1298. PrevIndex:=Result;
  1299. Result:=NextIndex;
  1300. end;
  1301. end;
  1302. end;
  1303. function TFPHashList.Find(const AName:shortstring): Pointer;
  1304. var
  1305. Index,
  1306. PrevIndex : Integer;
  1307. begin
  1308. Result:=nil;
  1309. Index:=InternalFind(FPHash(AName),AName,PrevIndex);
  1310. if Index=-1 then
  1311. exit;
  1312. Result:=FHashList^[Index].Data;
  1313. end;
  1314. function TFPHashList.FindIndexOf(const AName:shortstring): Integer;
  1315. var
  1316. PrevIndex : Integer;
  1317. begin
  1318. Result:=InternalFind(FPHash(AName),AName,PrevIndex);
  1319. end;
  1320. function TFPHashList.FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
  1321. var
  1322. Index,
  1323. PrevIndex : Integer;
  1324. begin
  1325. Result:=nil;
  1326. Index:=InternalFind(AHash,AName,PrevIndex);
  1327. if Index=-1 then
  1328. exit;
  1329. Result:=FHashList^[Index].Data;
  1330. end;
  1331. function TFPHashList.Rename(const AOldName,ANewName:shortstring): Integer;
  1332. var
  1333. PrevIndex,
  1334. Index : Integer;
  1335. OldHash : LongWord;
  1336. begin
  1337. Result:=-1;
  1338. OldHash:=FPHash(AOldName);
  1339. Index:=InternalFind(OldHash,AOldName,PrevIndex);
  1340. if Index=-1 then
  1341. exit;
  1342. { Remove from current Hash }
  1343. if PrevIndex<>-1 then
  1344. FHashList^[PrevIndex].NextIndex:=FHashList^[Index].NextIndex
  1345. else
  1346. FHashTable^[OldHash and FCapacityMask]:=FHashList^[Index].NextIndex;
  1347. { Set new name and hash }
  1348. with FHashList^[Index] do
  1349. begin
  1350. HashValue:=FPHash(ANewName);
  1351. StrIndex:=AddStr(ANewName);
  1352. end;
  1353. { Insert back in Hash }
  1354. AddToHashTable(Index);
  1355. { Return Index }
  1356. Result:=Index;
  1357. end;
  1358. procedure TFPHashList.Pack;
  1359. var
  1360. NewCount,
  1361. i : integer;
  1362. pdest,
  1363. psrc : PHashItem;
  1364. begin
  1365. NewCount:=0;
  1366. psrc:=@FHashList^[0];
  1367. pdest:=psrc;
  1368. For I:=0 To FCount-1 Do
  1369. begin
  1370. if assigned(psrc^.Data) then
  1371. begin
  1372. pdest^:=psrc^;
  1373. inc(pdest);
  1374. inc(NewCount);
  1375. end;
  1376. inc(psrc);
  1377. end;
  1378. FCount:=NewCount;
  1379. { We need to ReHash to update the IndexNext }
  1380. ReHash;
  1381. { Release over-capacity }
  1382. SetCapacity(FCount);
  1383. SetStrCapacity(FStrCount);
  1384. end;
  1385. procedure TFPHashList.ShowStatistics;
  1386. var
  1387. HashMean,
  1388. HashStdDev : Double;
  1389. Index,
  1390. i,j : Integer;
  1391. begin
  1392. { Calculate Mean and StdDev }
  1393. HashMean:=0;
  1394. HashStdDev:=0;
  1395. for i:=0 to FHashCapacity-1 do
  1396. begin
  1397. j:=0;
  1398. Index:=FHashTable^[i];
  1399. while (Index<>-1) do
  1400. begin
  1401. inc(j);
  1402. Index:=FHashList^[Index].NextIndex;
  1403. end;
  1404. HashMean:=HashMean+j;
  1405. HashStdDev:=HashStdDev+Sqr(j);
  1406. end;
  1407. HashMean:=HashMean/FHashCapacity;
  1408. HashStdDev:=(HashStdDev-FHashCapacity*Sqr(HashMean));
  1409. If FHashCapacity>1 then
  1410. HashStdDev:=Sqrt(HashStdDev/(FHashCapacity-1))
  1411. else
  1412. HashStdDev:=0;
  1413. { Print info to stdout }
  1414. Writeln('HashSize : ',FHashCapacity);
  1415. Writeln('HashMean : ',HashMean:1:4);
  1416. Writeln('HashStdDev : ',HashStdDev:1:4);
  1417. Writeln('ListSize : ',FCount,'/',FCapacity);
  1418. Writeln('StringSize : ',FStrCount,'/',FStrCapacity);
  1419. end;
  1420. procedure TFPHashList.ForEachCall(proc2call:TListCallback;arg:pointer);
  1421. var
  1422. i : integer;
  1423. p : pointer;
  1424. begin
  1425. For I:=0 To Count-1 Do
  1426. begin
  1427. p:=FHashList^[i].Data;
  1428. if assigned(p) then
  1429. proc2call(p,arg);
  1430. end;
  1431. end;
  1432. procedure TFPHashList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  1433. var
  1434. i : integer;
  1435. p : pointer;
  1436. begin
  1437. For I:=0 To Count-1 Do
  1438. begin
  1439. p:=FHashList^[i].Data;
  1440. if assigned(p) then
  1441. proc2call(p,arg);
  1442. end;
  1443. end;
  1444. {*****************************************************************************
  1445. TFPHashObject
  1446. *****************************************************************************}
  1447. procedure TFPHashObject.InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:shortstring);
  1448. var
  1449. Index : integer;
  1450. begin
  1451. FOwner:=HashObjectList;
  1452. Index:=HashObjectList.Add(s,Self);
  1453. FStrIndex:=HashObjectList.List.List^[Index].StrIndex;
  1454. FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
  1455. end;
  1456. constructor TFPHashObject.CreateNotOwned;
  1457. begin
  1458. FStrIndex:=-1;
  1459. end;
  1460. constructor TFPHashObject.Create(HashObjectList:TFPHashObjectList;const s:shortstring);
  1461. begin
  1462. InternalChangeOwner(HashObjectList,s);
  1463. end;
  1464. procedure TFPHashObject.ChangeOwner(HashObjectList:TFPHashObjectList);
  1465. begin
  1466. InternalChangeOwner(HashObjectList,PShortString(@FOwner.List.Strs[FStrIndex])^);
  1467. end;
  1468. procedure TFPHashObject.ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:shortstring);
  1469. begin
  1470. InternalChangeOwner(HashObjectList,s);
  1471. end;
  1472. procedure TFPHashObject.Rename(const ANewName:shortstring);
  1473. var
  1474. Index : integer;
  1475. begin
  1476. Index:=FOwner.Rename(PShortString(@FOwner.List.Strs[FStrIndex])^,ANewName);
  1477. if Index<>-1 then
  1478. begin
  1479. FStrIndex:=FOwner.List.List^[Index].StrIndex;
  1480. FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
  1481. end;
  1482. end;
  1483. function TFPHashObject.GetName:shortstring;
  1484. begin
  1485. if FOwner<>nil then
  1486. begin
  1487. FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
  1488. Result:=FCachedStr^;
  1489. end
  1490. else
  1491. Result:='';
  1492. end;
  1493. function TFPHashObject.GetHash:Longword;
  1494. begin
  1495. if FOwner<>nil then
  1496. Result:=FPHash(PShortString(@FOwner.List.Strs[FStrIndex])^)
  1497. else
  1498. Result:=$ffffffff;
  1499. end;
  1500. {*****************************************************************************
  1501. TFPHashObjectList (Copied from rtl/objpas/classes/lists.inc)
  1502. *****************************************************************************}
  1503. constructor TFPHashObjectList.Create(FreeObjects : boolean = True);
  1504. begin
  1505. inherited Create;
  1506. FHashList := TFPHashList.Create;
  1507. FFreeObjects := Freeobjects;
  1508. end;
  1509. destructor TFPHashObjectList.Destroy;
  1510. begin
  1511. if (FHashList <> nil) then
  1512. begin
  1513. Clear;
  1514. FHashList.Destroy;
  1515. end;
  1516. inherited Destroy;
  1517. end;
  1518. procedure TFPHashObjectList.Clear;
  1519. var
  1520. i: integer;
  1521. begin
  1522. if FFreeObjects then
  1523. for i := 0 to FHashList.Count - 1 do
  1524. TObject(FHashList[i]).Free;
  1525. FHashList.Clear;
  1526. end;
  1527. function TFPHashObjectList.GetCount: integer;
  1528. begin
  1529. Result := FHashList.Count;
  1530. end;
  1531. procedure TFPHashObjectList.SetCount(const AValue: integer);
  1532. begin
  1533. if FHashList.Count <> AValue then
  1534. FHashList.Count := AValue;
  1535. end;
  1536. function TFPHashObjectList.GetItem(Index: Integer): TObject;
  1537. begin
  1538. Result := TObject(FHashList[Index]);
  1539. end;
  1540. procedure TFPHashObjectList.SetItem(Index: Integer; AObject: TObject);
  1541. begin
  1542. if OwnsObjects then
  1543. TObject(FHashList[Index]).Free;
  1544. FHashList[index] := AObject;
  1545. end;
  1546. procedure TFPHashObjectList.SetCapacity(NewCapacity: Integer);
  1547. begin
  1548. FHashList.Capacity := NewCapacity;
  1549. end;
  1550. function TFPHashObjectList.GetCapacity: integer;
  1551. begin
  1552. Result := FHashList.Capacity;
  1553. end;
  1554. function TFPHashObjectList.Add(const AName:shortstring;AObject: TObject): Integer;
  1555. begin
  1556. Result := FHashList.Add(AName,AObject);
  1557. end;
  1558. function TFPHashObjectList.NameOfIndex(Index: Integer): shortstring;
  1559. begin
  1560. Result := FHashList.NameOfIndex(Index);
  1561. end;
  1562. function TFPHashObjectList.HashOfIndex(Index: Integer): LongWord;
  1563. begin
  1564. Result := FHashList.HashOfIndex(Index);
  1565. end;
  1566. function TFPHashObjectList.GetNextCollision(Index: Integer): Integer;
  1567. begin
  1568. Result := FHashList.GetNextCollision(Index);
  1569. end;
  1570. procedure TFPHashObjectList.Delete(Index: Integer);
  1571. begin
  1572. if OwnsObjects then
  1573. TObject(FHashList[Index]).Free;
  1574. FHashList.Delete(Index);
  1575. end;
  1576. function TFPHashObjectList.Expand: TFPHashObjectList;
  1577. begin
  1578. FHashList.Expand;
  1579. Result := Self;
  1580. end;
  1581. function TFPHashObjectList.Extract(Item: TObject): TObject;
  1582. begin
  1583. Result := TObject(FHashList.Extract(Item));
  1584. end;
  1585. function TFPHashObjectList.Remove(AObject: TObject): Integer;
  1586. begin
  1587. Result := IndexOf(AObject);
  1588. if (Result <> -1) then
  1589. begin
  1590. if OwnsObjects then
  1591. TObject(FHashList[Result]).Free;
  1592. FHashList.Delete(Result);
  1593. end;
  1594. end;
  1595. function TFPHashObjectList.IndexOf(AObject: TObject): Integer;
  1596. begin
  1597. Result := FHashList.IndexOf(Pointer(AObject));
  1598. end;
  1599. function TFPHashObjectList.Find(const s:shortstring): TObject;
  1600. begin
  1601. result:=TObject(FHashList.Find(s));
  1602. end;
  1603. function TFPHashObjectList.FindIndexOf(const s:shortstring): Integer;
  1604. begin
  1605. result:=FHashList.FindIndexOf(s);
  1606. end;
  1607. function TFPHashObjectList.FindWithHash(const AName:shortstring;AHash:LongWord): Pointer;
  1608. begin
  1609. Result:=TObject(FHashList.FindWithHash(AName,AHash));
  1610. end;
  1611. function TFPHashObjectList.Rename(const AOldName,ANewName:shortstring): Integer;
  1612. begin
  1613. Result:=FHashList.Rename(AOldName,ANewName);
  1614. end;
  1615. function TFPHashObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  1616. var
  1617. I : Integer;
  1618. begin
  1619. I:=AStartAt;
  1620. Result:=-1;
  1621. If AExact then
  1622. while (I<Count) and (Result=-1) do
  1623. If Items[i].ClassType=AClass then
  1624. Result:=I
  1625. else
  1626. Inc(I)
  1627. else
  1628. while (I<Count) and (Result=-1) do
  1629. If Items[i].InheritsFrom(AClass) then
  1630. Result:=I
  1631. else
  1632. Inc(I);
  1633. end;
  1634. procedure TFPHashObjectList.Pack;
  1635. begin
  1636. FHashList.Pack;
  1637. end;
  1638. procedure TFPHashObjectList.ShowStatistics;
  1639. begin
  1640. FHashList.ShowStatistics;
  1641. end;
  1642. procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
  1643. begin
  1644. FHashList.ForEachCall(TListCallBack(proc2call),arg);
  1645. end;
  1646. procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  1647. begin
  1648. FHashList.ForEachCall(TListStaticCallBack(proc2call),arg);
  1649. end;
  1650. {****************************************************************************
  1651. TLinkedListItem
  1652. ****************************************************************************}
  1653. constructor TLinkedListItem.Create;
  1654. begin
  1655. Previous:=nil;
  1656. Next:=nil;
  1657. end;
  1658. destructor TLinkedListItem.Destroy;
  1659. begin
  1660. end;
  1661. function TLinkedListItem.GetCopy:TLinkedListItem;
  1662. var
  1663. p : TLinkedListItem;
  1664. l : integer;
  1665. begin
  1666. p:=TLinkedListItemClass(ClassType).Create;
  1667. l:=InstanceSize;
  1668. Move(pointer(self)^,pointer(p)^,l);
  1669. Result:=p;
  1670. end;
  1671. {****************************************************************************
  1672. TLinkedList
  1673. ****************************************************************************}
  1674. constructor TLinkedList.Create;
  1675. begin
  1676. FFirst:=nil;
  1677. Flast:=nil;
  1678. FCount:=0;
  1679. FNoClear:=False;
  1680. end;
  1681. destructor TLinkedList.destroy;
  1682. begin
  1683. if not FNoClear then
  1684. Clear;
  1685. end;
  1686. function TLinkedList.empty:boolean;
  1687. begin
  1688. Empty:=(FFirst=nil);
  1689. end;
  1690. procedure TLinkedList.Insert(Item:TLinkedListItem);
  1691. begin
  1692. if FFirst=nil then
  1693. begin
  1694. FLast:=Item;
  1695. Item.Previous:=nil;
  1696. Item.Next:=nil;
  1697. end
  1698. else
  1699. begin
  1700. FFirst.Previous:=Item;
  1701. Item.Previous:=nil;
  1702. Item.Next:=FFirst;
  1703. end;
  1704. FFirst:=Item;
  1705. inc(FCount);
  1706. end;
  1707. procedure TLinkedList.InsertBefore(Item,Loc : TLinkedListItem);
  1708. begin
  1709. Item.Previous:=Loc.Previous;
  1710. Item.Next:=Loc;
  1711. Loc.Previous:=Item;
  1712. if assigned(Item.Previous) then
  1713. Item.Previous.Next:=Item
  1714. else
  1715. { if we've no next item, we've to adjust FFist }
  1716. FFirst:=Item;
  1717. inc(FCount);
  1718. end;
  1719. procedure TLinkedList.InsertAfter(Item,Loc : TLinkedListItem);
  1720. begin
  1721. Item.Next:=Loc.Next;
  1722. Loc.Next:=Item;
  1723. Item.Previous:=Loc;
  1724. if assigned(Item.Next) then
  1725. Item.Next.Previous:=Item
  1726. else
  1727. { if we've no next item, we've to adjust FLast }
  1728. FLast:=Item;
  1729. inc(FCount);
  1730. end;
  1731. procedure TLinkedList.Concat(Item:TLinkedListItem);
  1732. begin
  1733. if FFirst=nil then
  1734. begin
  1735. FFirst:=Item;
  1736. Item.Previous:=nil;
  1737. Item.Next:=nil;
  1738. end
  1739. else
  1740. begin
  1741. Flast.Next:=Item;
  1742. Item.Previous:=Flast;
  1743. Item.Next:=nil;
  1744. end;
  1745. Flast:=Item;
  1746. inc(FCount);
  1747. end;
  1748. procedure TLinkedList.remove(Item:TLinkedListItem);
  1749. begin
  1750. if Item=nil then
  1751. exit;
  1752. if (FFirst=Item) and (Flast=Item) then
  1753. begin
  1754. FFirst:=nil;
  1755. Flast:=nil;
  1756. end
  1757. else if FFirst=Item then
  1758. begin
  1759. FFirst:=Item.Next;
  1760. if assigned(FFirst) then
  1761. FFirst.Previous:=nil;
  1762. end
  1763. else if Flast=Item then
  1764. begin
  1765. Flast:=Flast.Previous;
  1766. if assigned(Flast) then
  1767. Flast.Next:=nil;
  1768. end
  1769. else
  1770. begin
  1771. Item.Previous.Next:=Item.Next;
  1772. Item.Next.Previous:=Item.Previous;
  1773. end;
  1774. Item.Next:=nil;
  1775. Item.Previous:=nil;
  1776. dec(FCount);
  1777. end;
  1778. procedure TLinkedList.clear;
  1779. var
  1780. NewNode, Next : TLinkedListItem;
  1781. begin
  1782. NewNode:=FFirst;
  1783. while assigned(NewNode) do
  1784. begin
  1785. Next:=NewNode.Next;
  1786. prefetch(next.next);
  1787. NewNode.Free;
  1788. NewNode:=Next;
  1789. end;
  1790. FLast:=nil;
  1791. FFirst:=nil;
  1792. FCount:=0;
  1793. end;
  1794. function TLinkedList.GetFirst:TLinkedListItem;
  1795. begin
  1796. if FFirst=nil then
  1797. GetFirst:=nil
  1798. else
  1799. begin
  1800. GetFirst:=FFirst;
  1801. if FFirst=FLast then
  1802. FLast:=nil;
  1803. FFirst:=FFirst.Next;
  1804. dec(FCount);
  1805. end;
  1806. end;
  1807. function TLinkedList.GetLast:TLinkedListItem;
  1808. begin
  1809. if FLast=nil then
  1810. Getlast:=nil
  1811. else
  1812. begin
  1813. Getlast:=FLast;
  1814. if FLast=FFirst then
  1815. FFirst:=nil;
  1816. FLast:=FLast.Previous;
  1817. dec(FCount);
  1818. end;
  1819. end;
  1820. procedure TLinkedList.insertList(p : TLinkedList);
  1821. begin
  1822. { empty List ? }
  1823. if (p.FFirst=nil) then
  1824. exit;
  1825. p.Flast.Next:=FFirst;
  1826. { we have a double Linked List }
  1827. if assigned(FFirst) then
  1828. FFirst.Previous:=p.Flast;
  1829. FFirst:=p.FFirst;
  1830. if (FLast=nil) then
  1831. Flast:=p.Flast;
  1832. inc(FCount,p.FCount);
  1833. { p becomes empty }
  1834. p.FFirst:=nil;
  1835. p.Flast:=nil;
  1836. p.FCount:=0;
  1837. end;
  1838. procedure TLinkedList.insertListBefore(Item:TLinkedListItem;p : TLinkedList);
  1839. begin
  1840. { empty List ? }
  1841. if (p.FFirst=nil) then
  1842. exit;
  1843. if (Item=nil) then
  1844. begin
  1845. { Insert at begin }
  1846. InsertList(p);
  1847. exit;
  1848. end
  1849. else
  1850. begin
  1851. p.FLast.Next:=Item;
  1852. p.FFirst.Previous:=Item.Previous;
  1853. if assigned(Item.Previous) then
  1854. Item.Previous.Next:=p.FFirst
  1855. else
  1856. FFirst:=p.FFirst;
  1857. Item.Previous:=p.FLast;
  1858. inc(FCount,p.FCount);
  1859. end;
  1860. { p becomes empty }
  1861. p.FFirst:=nil;
  1862. p.Flast:=nil;
  1863. p.FCount:=0;
  1864. end;
  1865. procedure TLinkedList.insertListAfter(Item:TLinkedListItem;p : TLinkedList);
  1866. begin
  1867. { empty List ? }
  1868. if (p.FFirst=nil) then
  1869. exit;
  1870. if (Item=nil) then
  1871. begin
  1872. { Insert at begin }
  1873. InsertList(p);
  1874. exit;
  1875. end
  1876. else
  1877. begin
  1878. p.FFirst.Previous:=Item;
  1879. p.FLast.Next:=Item.Next;
  1880. if assigned(Item.Next) then
  1881. Item.Next.Previous:=p.FLast
  1882. else
  1883. FLast:=p.FLast;
  1884. Item.Next:=p.FFirst;
  1885. inc(FCount,p.FCount);
  1886. end;
  1887. { p becomes empty }
  1888. p.FFirst:=nil;
  1889. p.Flast:=nil;
  1890. p.FCount:=0;
  1891. end;
  1892. procedure TLinkedList.concatList(p : TLinkedList);
  1893. begin
  1894. if (p.FFirst=nil) then
  1895. exit;
  1896. if FFirst=nil then
  1897. FFirst:=p.FFirst
  1898. else
  1899. begin
  1900. FLast.Next:=p.FFirst;
  1901. p.FFirst.Previous:=Flast;
  1902. end;
  1903. Flast:=p.Flast;
  1904. inc(FCount,p.FCount);
  1905. { make p empty }
  1906. p.Flast:=nil;
  1907. p.FFirst:=nil;
  1908. p.FCount:=0;
  1909. end;
  1910. procedure TLinkedList.insertListcopy(p : TLinkedList);
  1911. var
  1912. NewNode,NewNode2 : TLinkedListItem;
  1913. begin
  1914. NewNode:=p.Last;
  1915. while assigned(NewNode) do
  1916. begin
  1917. NewNode2:=NewNode.Getcopy;
  1918. if assigned(NewNode2) then
  1919. Insert(NewNode2);
  1920. NewNode:=NewNode.Previous;
  1921. end;
  1922. end;
  1923. procedure TLinkedList.concatListcopy(p : TLinkedList);
  1924. var
  1925. NewNode,NewNode2 : TLinkedListItem;
  1926. begin
  1927. NewNode:=p.First;
  1928. while assigned(NewNode) do
  1929. begin
  1930. NewNode2:=NewNode.Getcopy;
  1931. if assigned(NewNode2) then
  1932. Concat(NewNode2);
  1933. NewNode:=NewNode.Next;
  1934. end;
  1935. end;
  1936. {****************************************************************************
  1937. TCmdStrListItem
  1938. ****************************************************************************}
  1939. constructor TCmdStrListItem.Create(const s:TCmdStr);
  1940. begin
  1941. inherited Create;
  1942. FPStr:=s;
  1943. end;
  1944. destructor TCmdStrListItem.Destroy;
  1945. begin
  1946. FPStr:='';
  1947. end;
  1948. function TCmdStrListItem.Str:TCmdStr;
  1949. begin
  1950. Str:=FPStr;
  1951. end;
  1952. function TCmdStrListItem.GetCopy:TLinkedListItem;
  1953. begin
  1954. Result:=(inherited GetCopy);
  1955. { TLinkedListItem.GetCopy performs a "move" to copy all data -> reinit
  1956. the ansistring, so the refcount is properly increased }
  1957. Initialize(TCmdStrListItem(Result).FPStr);
  1958. TCmdStrListItem(Result).FPStr:=FPstr;
  1959. end;
  1960. {****************************************************************************
  1961. TCmdStrList
  1962. ****************************************************************************}
  1963. constructor TCmdStrList.Create;
  1964. begin
  1965. inherited Create;
  1966. FDoubles:=true;
  1967. end;
  1968. constructor TCmdStrList.Create_no_double;
  1969. begin
  1970. inherited Create;
  1971. FDoubles:=false;
  1972. end;
  1973. procedure TCmdStrList.insert(const s : TCmdStr);
  1974. begin
  1975. if (s='') or
  1976. ((not FDoubles) and (find(s)<>nil)) then
  1977. exit;
  1978. inherited insert(TCmdStrListItem.create(s));
  1979. end;
  1980. procedure TCmdStrList.concat(const s : TCmdStr);
  1981. begin
  1982. if (s='') or
  1983. ((not FDoubles) and (find(s)<>nil)) then
  1984. exit;
  1985. inherited concat(TCmdStrListItem.create(s));
  1986. end;
  1987. procedure TCmdStrList.remove(const s : TCmdStr);
  1988. var
  1989. p : TCmdStrListItem;
  1990. begin
  1991. if s='' then
  1992. exit;
  1993. p:=find(s);
  1994. if assigned(p) then
  1995. begin
  1996. inherited Remove(p);
  1997. p.Free;
  1998. end;
  1999. end;
  2000. function TCmdStrList.GetFirst : TCmdStr;
  2001. var
  2002. p : TCmdStrListItem;
  2003. begin
  2004. p:=TCmdStrListItem(inherited GetFirst);
  2005. if p=nil then
  2006. GetFirst:=''
  2007. else
  2008. begin
  2009. GetFirst:=p.FPStr;
  2010. p.free;
  2011. end;
  2012. end;
  2013. function TCmdStrList.Getlast : TCmdStr;
  2014. var
  2015. p : TCmdStrListItem;
  2016. begin
  2017. p:=TCmdStrListItem(inherited Getlast);
  2018. if p=nil then
  2019. Getlast:=''
  2020. else
  2021. begin
  2022. Getlast:=p.FPStr;
  2023. p.free;
  2024. end;
  2025. end;
  2026. function TCmdStrList.FindCase(const s:TCmdStr):TCmdStrListItem;
  2027. var
  2028. NewNode : TCmdStrListItem;
  2029. begin
  2030. result:=nil;
  2031. if s='' then
  2032. exit;
  2033. NewNode:=TCmdStrListItem(FFirst);
  2034. while assigned(NewNode) do
  2035. begin
  2036. if NewNode.FPStr=s then
  2037. begin
  2038. result:=NewNode;
  2039. exit;
  2040. end;
  2041. NewNode:=TCmdStrListItem(NewNode.Next);
  2042. end;
  2043. end;
  2044. function TCmdStrList.Find(const s:TCmdStr):TCmdStrListItem;
  2045. var
  2046. NewNode : TCmdStrListItem;
  2047. begin
  2048. result:=nil;
  2049. if s='' then
  2050. exit;
  2051. NewNode:=TCmdStrListItem(FFirst);
  2052. while assigned(NewNode) do
  2053. begin
  2054. if SysUtils.CompareText(s, NewNode.FPStr)=0 then
  2055. begin
  2056. result:=NewNode;
  2057. exit;
  2058. end;
  2059. NewNode:=TCmdStrListItem(NewNode.Next);
  2060. end;
  2061. end;
  2062. procedure TCmdStrList.InsertItem(item:TCmdStrListItem);
  2063. begin
  2064. inherited Insert(item);
  2065. end;
  2066. procedure TCmdStrList.ConcatItem(item:TCmdStrListItem);
  2067. begin
  2068. inherited Concat(item);
  2069. end;
  2070. {****************************************************************************
  2071. tdynamicarray
  2072. ****************************************************************************}
  2073. constructor tdynamicarray.create(Ablocksize:longword);
  2074. begin
  2075. FPosn:=0;
  2076. FPosnblock:=nil;
  2077. FFirstblock:=nil;
  2078. FLastblock:=nil;
  2079. FCurrBlockSize:=0;
  2080. FMaxBlockSize:=Ablocksize;
  2081. grow;
  2082. end;
  2083. destructor tdynamicarray.destroy;
  2084. var
  2085. hp : pdynamicblock;
  2086. begin
  2087. while assigned(FFirstblock) do
  2088. begin
  2089. hp:=FFirstblock;
  2090. FFirstblock:=FFirstblock^.Next;
  2091. Freemem(hp);
  2092. end;
  2093. end;
  2094. function tdynamicarray.size:longword;
  2095. begin
  2096. if assigned(FLastblock) then
  2097. size:=FLastblock^.pos+FLastblock^.used
  2098. else
  2099. size:=0;
  2100. end;
  2101. procedure tdynamicarray.reset;
  2102. var
  2103. hp : pdynamicblock;
  2104. begin
  2105. while assigned(FFirstblock) do
  2106. begin
  2107. hp:=FFirstblock;
  2108. FFirstblock:=FFirstblock^.Next;
  2109. Freemem(hp);
  2110. end;
  2111. FPosn:=0;
  2112. FPosnblock:=nil;
  2113. FFirstblock:=nil;
  2114. FLastblock:=nil;
  2115. grow;
  2116. end;
  2117. procedure tdynamicarray.grow;
  2118. var
  2119. nblock : pdynamicblock;
  2120. OptBlockSize,
  2121. IncSize : integer;
  2122. begin
  2123. if CurrBlockSize<FMaxBlocksize then
  2124. begin
  2125. IncSize := sizeof(ptrint)*8;
  2126. if FCurrBlockSize > 255 then
  2127. Inc(IncSize, FCurrBlockSize shr 2);
  2128. inc(FCurrBlockSize,IncSize);
  2129. end;
  2130. if CurrBlockSize>FMaxBlocksize then
  2131. FCurrBlockSize:=FMaxBlocksize;
  2132. { Calculate the most optimal size so there is no alignment overhead
  2133. lost in the heap manager }
  2134. OptBlockSize:=cutils.Align(CurrBlockSize+dynamicblockbasesize,16)-dynamicblockbasesize-sizeof(ptrint);
  2135. Getmem(nblock,OptBlockSize+dynamicblockbasesize);
  2136. if not assigned(FFirstblock) then
  2137. begin
  2138. FFirstblock:=nblock;
  2139. FPosnblock:=nblock;
  2140. nblock^.pos:=0;
  2141. end
  2142. else
  2143. begin
  2144. FLastblock^.Next:=nblock;
  2145. nblock^.pos:=FLastblock^.pos+FLastblock^.size;
  2146. end;
  2147. nblock^.used:=0;
  2148. nblock^.size:=OptBlockSize;
  2149. nblock^.Next:=nil;
  2150. fillchar(nblock^.data,nblock^.size,0);
  2151. FLastblock:=nblock;
  2152. end;
  2153. procedure tdynamicarray.align(i:longword);
  2154. var
  2155. j : longword;
  2156. begin
  2157. j:=(FPosn mod i);
  2158. if j<>0 then
  2159. begin
  2160. j:=i-j;
  2161. if FPosnblock^.used+j>FPosnblock^.size then
  2162. begin
  2163. dec(j,FPosnblock^.size-FPosnblock^.used);
  2164. FPosnblock^.used:=FPosnblock^.size;
  2165. grow;
  2166. FPosnblock:=FLastblock;
  2167. end;
  2168. inc(FPosnblock^.used,j);
  2169. inc(FPosn,j);
  2170. end;
  2171. end;
  2172. procedure tdynamicarray.seek(i:longword);
  2173. begin
  2174. if (i<FPosnblock^.pos) or (i>=FPosnblock^.pos+FPosnblock^.size) then
  2175. begin
  2176. { set FPosnblock correct if the size is bigger then
  2177. the current block }
  2178. if FPosnblock^.pos>i then
  2179. FPosnblock:=FFirstblock;
  2180. while assigned(FPosnblock) do
  2181. begin
  2182. if FPosnblock^.pos+FPosnblock^.size>i then
  2183. break;
  2184. FPosnblock:=FPosnblock^.Next;
  2185. end;
  2186. { not found ? then increase blocks }
  2187. if not assigned(FPosnblock) then
  2188. begin
  2189. repeat
  2190. { the current FLastblock is now also fully used }
  2191. FLastblock^.used:=FLastblock^.size;
  2192. grow;
  2193. FPosnblock:=FLastblock;
  2194. until FPosnblock^.pos+FPosnblock^.size>=i;
  2195. end;
  2196. end;
  2197. FPosn:=i;
  2198. if FPosn-FPosnblock^.pos>FPosnblock^.used then
  2199. FPosnblock^.used:=FPosn-FPosnblock^.pos;
  2200. end;
  2201. procedure tdynamicarray.write(const d;len:longword);
  2202. var
  2203. p : pchar;
  2204. i,j : longword;
  2205. begin
  2206. p:=pchar(@d);
  2207. while (len>0) do
  2208. begin
  2209. i:=FPosn-FPosnblock^.pos;
  2210. if i+len>=FPosnblock^.size then
  2211. begin
  2212. j:=FPosnblock^.size-i;
  2213. move(p^,FPosnblock^.data[i],j);
  2214. inc(p,j);
  2215. inc(FPosn,j);
  2216. dec(len,j);
  2217. FPosnblock^.used:=FPosnblock^.size;
  2218. if assigned(FPosnblock^.Next) then
  2219. FPosnblock:=FPosnblock^.Next
  2220. else
  2221. begin
  2222. grow;
  2223. FPosnblock:=FLastblock;
  2224. end;
  2225. end
  2226. else
  2227. begin
  2228. move(p^,FPosnblock^.data[i],len);
  2229. inc(p,len);
  2230. inc(FPosn,len);
  2231. i:=FPosn-FPosnblock^.pos;
  2232. if i>FPosnblock^.used then
  2233. FPosnblock^.used:=i;
  2234. len:=0;
  2235. end;
  2236. end;
  2237. end;
  2238. procedure tdynamicarray.writestr(const s:string);
  2239. begin
  2240. write(s[1],length(s));
  2241. end;
  2242. function tdynamicarray.read(var d;len:longword):longword;
  2243. var
  2244. p : pchar;
  2245. i,j,res : longword;
  2246. begin
  2247. res:=0;
  2248. p:=pchar(@d);
  2249. while (len>0) do
  2250. begin
  2251. i:=FPosn-FPosnblock^.pos;
  2252. if i+len>=FPosnblock^.used then
  2253. begin
  2254. j:=FPosnblock^.used-i;
  2255. move(FPosnblock^.data[i],p^,j);
  2256. inc(p,j);
  2257. inc(FPosn,j);
  2258. inc(res,j);
  2259. dec(len,j);
  2260. if assigned(FPosnblock^.Next) then
  2261. FPosnblock:=FPosnblock^.Next
  2262. else
  2263. break;
  2264. end
  2265. else
  2266. begin
  2267. move(FPosnblock^.data[i],p^,len);
  2268. inc(p,len);
  2269. inc(FPosn,len);
  2270. inc(res,len);
  2271. len:=0;
  2272. end;
  2273. end;
  2274. read:=res;
  2275. end;
  2276. procedure tdynamicarray.readstream(f:TCStream;maxlen:longword);
  2277. var
  2278. i,left : longword;
  2279. begin
  2280. repeat
  2281. left:=FPosnblock^.size-FPosnblock^.used;
  2282. if left>maxlen then
  2283. left:=maxlen;
  2284. i:=f.Read(FPosnblock^.data[FPosnblock^.used],left);
  2285. dec(maxlen,i);
  2286. inc(FPosnblock^.used,i);
  2287. if FPosnblock^.used=FPosnblock^.size then
  2288. begin
  2289. if assigned(FPosnblock^.Next) then
  2290. FPosnblock:=FPosnblock^.Next
  2291. else
  2292. begin
  2293. grow;
  2294. FPosnblock:=FLastblock;
  2295. end;
  2296. end;
  2297. until (i<left) or (maxlen=0);
  2298. end;
  2299. procedure tdynamicarray.writestream(f:TCStream);
  2300. var
  2301. hp : pdynamicblock;
  2302. begin
  2303. hp:=FFirstblock;
  2304. while assigned(hp) do
  2305. begin
  2306. f.Write(hp^.data,hp^.used);
  2307. hp:=hp^.Next;
  2308. end;
  2309. end;
  2310. {****************************************************************************
  2311. thashset
  2312. ****************************************************************************}
  2313. constructor THashSet.Create(InitSize: Integer; OwnKeys, OwnObjects: Boolean);
  2314. var
  2315. I: Integer;
  2316. begin
  2317. inherited Create;
  2318. FOwnsObjects := OwnObjects;
  2319. FOwnsKeys := OwnKeys;
  2320. I := 64;
  2321. while I < InitSize do I := I shl 1;
  2322. FBucketCount := I;
  2323. FBucket := AllocMem(I * sizeof(PHashSetItem));
  2324. end;
  2325. destructor THashSet.Destroy;
  2326. begin
  2327. Clear;
  2328. FreeMem(FBucket);
  2329. inherited Destroy;
  2330. end;
  2331. procedure THashSet.Clear;
  2332. var
  2333. I: Integer;
  2334. item, next: PHashSetItem;
  2335. begin
  2336. for I := 0 to FBucketCount-1 do
  2337. begin
  2338. item := FBucket[I];
  2339. while Assigned(item) do
  2340. begin
  2341. next := item^.Next;
  2342. if FOwnsObjects then
  2343. item^.Data.Free;
  2344. if FOwnsKeys then
  2345. FreeMem(item^.Key);
  2346. FreeItem(item);
  2347. item := next;
  2348. end;
  2349. end;
  2350. FillChar(FBucket^, FBucketCount * sizeof(PHashSetItem), 0);
  2351. end;
  2352. function THashSet.Find(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2353. var
  2354. Dummy: Boolean;
  2355. begin
  2356. Result := Lookup(Key, KeyLen, Dummy, False);
  2357. end;
  2358. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer;
  2359. var Found: Boolean): PHashSetItem;
  2360. begin
  2361. Result := Lookup(Key, KeyLen, Found, True);
  2362. end;
  2363. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2364. var
  2365. Dummy: Boolean;
  2366. begin
  2367. Result := Lookup(Key, KeyLen, Dummy, True);
  2368. end;
  2369. function THashSet.Get(Key: Pointer; KeyLen: Integer): TObject;
  2370. var
  2371. e: PHashSetItem;
  2372. Dummy: Boolean;
  2373. begin
  2374. e := Lookup(Key, KeyLen, Dummy, False);
  2375. if Assigned(e) then
  2376. Result := e^.Data
  2377. else
  2378. Result := nil;
  2379. end;
  2380. function THashSet.Lookup(Key: Pointer; KeyLen: Integer;
  2381. var Found: Boolean; CanCreate: Boolean): PHashSetItem;
  2382. var
  2383. Entry: PPHashSetItem;
  2384. h: LongWord;
  2385. begin
  2386. h := FPHash(Key, KeyLen);
  2387. Entry := @FBucket[h mod FBucketCount];
  2388. while Assigned(Entry^) and
  2389. not ((Entry^^.HashValue = h) and (Entry^^.KeyLength = KeyLen) and
  2390. (CompareByte(Entry^^.Key^, Key^, KeyLen) = 0)) do
  2391. Entry := @Entry^^.Next;
  2392. Found := Assigned(Entry^);
  2393. if Found or (not CanCreate) then
  2394. begin
  2395. Result := Entry^;
  2396. Exit;
  2397. end;
  2398. if FCount > FBucketCount then { arbitrary limit, probably too high }
  2399. begin
  2400. { rehash and repeat search }
  2401. Resize(FBucketCount * 2);
  2402. Result := Lookup(Key, KeyLen, Found, CanCreate);
  2403. end
  2404. else
  2405. begin
  2406. New(Result);
  2407. if FOwnsKeys then
  2408. begin
  2409. GetMem(Result^.Key, KeyLen);
  2410. Move(Key^, Result^.Key^, KeyLen);
  2411. end
  2412. else
  2413. Result^.Key := Key;
  2414. Result^.KeyLength := KeyLen;
  2415. Result^.HashValue := h;
  2416. Result^.Data := nil;
  2417. Result^.Next := nil;
  2418. Inc(FCount);
  2419. Entry^ := Result;
  2420. end;
  2421. end;
  2422. procedure THashSet.Resize(NewCapacity: LongWord);
  2423. var
  2424. p, chain: PPHashSetItem;
  2425. i: Integer;
  2426. e, n: PHashSetItem;
  2427. begin
  2428. p := AllocMem(NewCapacity * SizeOfItem);
  2429. for i := 0 to FBucketCount-1 do
  2430. begin
  2431. e := FBucket[i];
  2432. while Assigned(e) do
  2433. begin
  2434. chain := @p[e^.HashValue mod NewCapacity];
  2435. n := e^.Next;
  2436. e^.Next := chain^;
  2437. chain^ := e;
  2438. e := n;
  2439. end;
  2440. end;
  2441. FBucketCount := NewCapacity;
  2442. FreeMem(FBucket);
  2443. FBucket := p;
  2444. end;
  2445. class procedure THashSet.FreeItem(item: PHashSetItem);
  2446. begin
  2447. Dispose(item);
  2448. end;
  2449. class function THashSet.SizeOfItem: Integer;
  2450. begin
  2451. Result := SizeOf(THashSetItem);
  2452. end;
  2453. function THashSet.Remove(Entry: PHashSetItem): Boolean;
  2454. var
  2455. chain: PPHashSetItem;
  2456. begin
  2457. chain := @FBucket[Entry^.HashValue mod FBucketCount];
  2458. while Assigned(chain^) do
  2459. begin
  2460. if chain^ = Entry then
  2461. begin
  2462. chain^ := Entry^.Next;
  2463. if FOwnsObjects then
  2464. Entry^.Data.Free;
  2465. if FOwnsKeys then
  2466. FreeMem(Entry^.Key);
  2467. FreeItem(Entry);
  2468. Dec(FCount);
  2469. Result := True;
  2470. Exit;
  2471. end;
  2472. chain := @chain^^.Next;
  2473. end;
  2474. Result := False;
  2475. end;
  2476. {****************************************************************************
  2477. ttaghashset
  2478. ****************************************************************************}
  2479. function TTagHashSet.Lookup(Key: Pointer; KeyLen: Integer;
  2480. Tag: LongWord; var Found: Boolean; CanCreate: Boolean): PTagHashSetItem;
  2481. var
  2482. Entry: PPTagHashSetItem;
  2483. h: LongWord;
  2484. begin
  2485. h := FPHash(Key, KeyLen, Tag);
  2486. Entry := @PPTagHashSetItem(FBucket)[h mod FBucketCount];
  2487. while Assigned(Entry^) and
  2488. not ((Entry^^.HashValue = h) and (Entry^^.KeyLength = KeyLen) and
  2489. (Entry^^.Tag = Tag) and (CompareByte(Entry^^.Key^, Key^, KeyLen) = 0)) do
  2490. Entry := @Entry^^.Next;
  2491. Found := Assigned(Entry^);
  2492. if Found or (not CanCreate) then
  2493. begin
  2494. Result := Entry^;
  2495. Exit;
  2496. end;
  2497. if FCount > FBucketCount then { arbitrary limit, probably too high }
  2498. begin
  2499. { rehash and repeat search }
  2500. Resize(FBucketCount * 2);
  2501. Result := Lookup(Key, KeyLen, Tag, Found, CanCreate);
  2502. end
  2503. else
  2504. begin
  2505. New(Result);
  2506. if FOwnsKeys then
  2507. begin
  2508. GetMem(Result^.Key, KeyLen);
  2509. Move(Key^, Result^.Key^, KeyLen);
  2510. end
  2511. else
  2512. Result^.Key := Key;
  2513. Result^.KeyLength := KeyLen;
  2514. Result^.HashValue := h;
  2515. Result^.Tag := Tag;
  2516. Result^.Data := nil;
  2517. Result^.Next := nil;
  2518. Inc(FCount);
  2519. Entry^ := Result;
  2520. end;
  2521. end;
  2522. class procedure TTagHashSet.FreeItem(item: PHashSetItem);
  2523. begin
  2524. Dispose(PTagHashSetItem(item));
  2525. end;
  2526. class function TTagHashSet.SizeOfItem: Integer;
  2527. begin
  2528. Result := SizeOf(TTagHashSetItem);
  2529. end;
  2530. function TTagHashSet.Find(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem;
  2531. var
  2532. Dummy: Boolean;
  2533. begin
  2534. Result := Lookup(Key, KeyLen, Tag, Dummy, False);
  2535. end;
  2536. function TTagHashSet.FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord;
  2537. var Found: Boolean): PTagHashSetItem;
  2538. begin
  2539. Result := Lookup(Key, KeyLen, Tag, Found, True);
  2540. end;
  2541. function TTagHashSet.FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem;
  2542. var
  2543. Dummy: Boolean;
  2544. begin
  2545. Result := Lookup(Key, KeyLen, Tag, Dummy, True);
  2546. end;
  2547. function TTagHashSet.Get(Key: Pointer; KeyLen: Integer; Tag: LongWord): TObject;
  2548. var
  2549. e: PTagHashSetItem;
  2550. Dummy: Boolean;
  2551. begin
  2552. e := Lookup(Key, KeyLen, Tag, Dummy, False);
  2553. if Assigned(e) then
  2554. Result := e^.Data
  2555. else
  2556. Result := nil;
  2557. end;
  2558. {****************************************************************************
  2559. tbitset
  2560. ****************************************************************************}
  2561. constructor tbitset.create(initsize: longint);
  2562. begin
  2563. create_bytesize((initsize+7) div 8);
  2564. end;
  2565. constructor tbitset.create_bytesize(bytesize: longint);
  2566. begin
  2567. fdatasize:=bytesize;
  2568. getmem(fdata,fdataSize);
  2569. clear;
  2570. end;
  2571. destructor tbitset.destroy;
  2572. begin
  2573. freemem(fdata,fdatasize);
  2574. inherited destroy;
  2575. end;
  2576. procedure tbitset.clear;
  2577. begin
  2578. fillchar(fdata^,fdatasize,0);
  2579. end;
  2580. procedure tbitset.grow(nsize: longint);
  2581. begin
  2582. reallocmem(fdata,nsize);
  2583. fillchar(fdata[fdatasize],nsize-fdatasize,0);
  2584. fdatasize:=nsize;
  2585. end;
  2586. procedure tbitset.include(index: longint);
  2587. var
  2588. dataindex: longint;
  2589. begin
  2590. { don't use bitpacked array, not endian-safe }
  2591. dataindex:=index shr 3;
  2592. if (dataindex>=datasize) then
  2593. grow(dataindex+16);
  2594. fdata[dataindex]:=fdata[dataindex] or (1 shl (index and 7));
  2595. end;
  2596. procedure tbitset.exclude(index: longint);
  2597. var
  2598. dataindex: longint;
  2599. begin
  2600. dataindex:=index shr 3;
  2601. if (dataindex>=datasize) then
  2602. exit;
  2603. fdata[dataindex]:=fdata[dataindex] and not(1 shl (index and 7));
  2604. end;
  2605. function tbitset.isset(index: longint): boolean;
  2606. var
  2607. dataindex: longint;
  2608. begin
  2609. dataindex:=index shr 3;
  2610. result:=
  2611. (dataindex<datasize) and
  2612. (((fdata[dataindex] shr (index and 7)) and 1)<>0);
  2613. end;
  2614. procedure tbitset.addset(aset: tbitset);
  2615. var
  2616. i: longint;
  2617. begin
  2618. if (aset.datasize>datasize) then
  2619. grow(aset.datasize);
  2620. for i:=0 to aset.datasize-1 do
  2621. fdata[i]:=fdata[i] or aset.data[i];
  2622. end;
  2623. procedure tbitset.subset(aset: tbitset);
  2624. var
  2625. i: longint;
  2626. begin
  2627. for i:=0 to min(datasize,aset.datasize)-1 do
  2628. fdata[i]:=fdata[i] and not(aset.data[i]);
  2629. end;
  2630. end.