cclasses.pas 73 KB

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