cclasses.pas 74 KB

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