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