cclasses.pas 79 KB

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