cclasses.pas 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885
  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. type
  395. tdynamicarray = class
  396. private
  397. FPosn : longword;
  398. FPosnblock : pdynamicblock;
  399. FCurrBlocksize,
  400. FMaxBlocksize : longword;
  401. FFirstblock,
  402. FLastblock : pdynamicblock;
  403. procedure grow;
  404. public
  405. constructor Create(Ablocksize:longword);
  406. destructor Destroy;override;
  407. procedure reset;
  408. function size:longword;
  409. procedure align(i:longword);
  410. procedure seek(i:longword);
  411. function read(var d;len:longword):longword;
  412. procedure write(const d;len:longword);
  413. procedure writestr(const s:string); {$ifdef CCLASSESINLINE}inline;{$endif}
  414. procedure readstream(f:TCStream;maxlen:longword);
  415. procedure writestream(f:TCStream);
  416. property CurrBlockSize : longword read FCurrBlocksize;
  417. property FirstBlock : PDynamicBlock read FFirstBlock;
  418. property Pos : longword read FPosn;
  419. end;
  420. {******************************************************************
  421. THashSet (keys not limited to ShortString, no indexed access)
  422. *******************************************************************}
  423. PPHashSetItem = ^PHashSetItem;
  424. PHashSetItem = ^THashSetItem;
  425. THashSetItem = record
  426. Next: PHashSetItem;
  427. Key: Pointer;
  428. KeyLength: Integer;
  429. HashValue: LongWord;
  430. Data: TObject;
  431. end;
  432. THashSet = class(TObject)
  433. private
  434. FCount: LongWord;
  435. FBucketCount: LongWord;
  436. FBucket: PPHashSetItem;
  437. FOwnsObjects: Boolean;
  438. FOwnsKeys: Boolean;
  439. function Lookup(Key: Pointer; KeyLen: Integer; var Found: Boolean;
  440. CanCreate: Boolean): PHashSetItem;
  441. procedure Resize(NewCapacity: LongWord);
  442. public
  443. constructor Create(InitSize: Integer; OwnKeys, OwnObjects: Boolean);
  444. destructor Destroy; override;
  445. procedure Clear;
  446. { finds an entry by key }
  447. function Find(Key: Pointer; KeyLen: Integer): PHashSetItem;
  448. { finds an entry, creates one if not exists }
  449. function FindOrAdd(Key: Pointer; KeyLen: Integer;
  450. var Found: Boolean): PHashSetItem;
  451. { finds an entry, creates one if not exists }
  452. function FindOrAdd(Key: Pointer; KeyLen: Integer): PHashSetItem;
  453. { returns Data by given Key }
  454. function Get(Key: Pointer; KeyLen: Integer): TObject;
  455. { removes an entry, returns False if entry wasn't there }
  456. function Remove(Entry: PHashSetItem): Boolean;
  457. property Count: LongWord read FCount;
  458. end;
  459. {******************************************************************
  460. tbitset
  461. *******************************************************************}
  462. tbitset = class
  463. private
  464. fdata: pbyte;
  465. fdatasize: longint;
  466. public
  467. constructor create(initsize: longint);
  468. constructor create_bytesize(bytesize: longint);
  469. destructor destroy; override;
  470. procedure clear;
  471. procedure grow(nsize: longint);
  472. { sets a bit }
  473. procedure include(index: longint);
  474. { clears a bit }
  475. procedure exclude(index: longint);
  476. { finds an entry, creates one if not exists }
  477. function isset(index: longint): boolean;
  478. procedure addset(aset: tbitset);
  479. procedure subset(aset: tbitset);
  480. property data: pbyte read fdata;
  481. property datasize: longint read fdatasize;
  482. end;
  483. function FPHash(const s:shortstring):LongWord;
  484. function FPHash(P: PChar; Len: Integer): LongWord;
  485. implementation
  486. {*****************************************************************************
  487. Memory debug
  488. *****************************************************************************}
  489. constructor tmemdebug.create(const s:string);
  490. begin
  491. infostr:=s;
  492. totalmem:=0;
  493. Start;
  494. end;
  495. procedure tmemdebug.start;
  496. var
  497. status : TFPCHeapStatus;
  498. begin
  499. status:=GetFPCHeapStatus;
  500. startmem:=status.CurrHeapUsed;
  501. end;
  502. procedure tmemdebug.stop;
  503. var
  504. status : TFPCHeapStatus;
  505. begin
  506. if startmem<>0 then
  507. begin
  508. status:=GetFPCHeapStatus;
  509. inc(TotalMem,startmem-status.CurrHeapUsed);
  510. startmem:=0;
  511. end;
  512. end;
  513. destructor tmemdebug.destroy;
  514. begin
  515. Stop;
  516. show;
  517. end;
  518. procedure tmemdebug.show;
  519. begin
  520. write('memory [',infostr,'] ');
  521. if TotalMem>0 then
  522. writeln(DStr(TotalMem shr 10),' Kb released')
  523. else
  524. writeln(DStr((-TotalMem) shr 10),' Kb allocated');
  525. end;
  526. {*****************************************************************************
  527. TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
  528. *****************************************************************************}
  529. procedure TFPList.RaiseIndexError(Index : Integer);
  530. begin
  531. Error(SListIndexError, Index);
  532. end;
  533. function TFPList.Get(Index: Integer): Pointer;
  534. begin
  535. If (Index < 0) or (Index >= FCount) then
  536. RaiseIndexError(Index);
  537. Result:=FList^[Index];
  538. end;
  539. procedure TFPList.Put(Index: Integer; Item: Pointer);
  540. begin
  541. if (Index < 0) or (Index >= FCount) then
  542. RaiseIndexError(Index);
  543. Flist^[Index] := Item;
  544. end;
  545. function TFPList.Extract(item: Pointer): Pointer;
  546. var
  547. i : Integer;
  548. begin
  549. result := nil;
  550. i := IndexOf(item);
  551. if i >= 0 then
  552. begin
  553. Result := item;
  554. FList^[i] := nil;
  555. Delete(i);
  556. end;
  557. end;
  558. procedure TFPList.SetCapacity(NewCapacity: Integer);
  559. begin
  560. If (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
  561. Error (SListCapacityError, NewCapacity);
  562. if NewCapacity = FCapacity then
  563. exit;
  564. ReallocMem(FList, SizeOf(Pointer)*NewCapacity);
  565. FCapacity := NewCapacity;
  566. end;
  567. procedure TFPList.SetCount(NewCount: Integer);
  568. begin
  569. if (NewCount < 0) or (NewCount > MaxListSize)then
  570. Error(SListCountError, NewCount);
  571. If NewCount > FCount then
  572. begin
  573. If NewCount > FCapacity then
  574. SetCapacity(NewCount);
  575. If FCount < NewCount then
  576. FillChar(Flist^[FCount], (NewCount-FCount) * sizeof(Pointer), 0);
  577. end;
  578. FCount := Newcount;
  579. end;
  580. destructor TFPList.Destroy;
  581. begin
  582. Self.Clear;
  583. inherited Destroy;
  584. end;
  585. function TFPList.Add(Item: Pointer): Integer;
  586. begin
  587. if FCount = FCapacity then
  588. Self.Expand;
  589. FList^[FCount] := Item;
  590. Result := FCount;
  591. inc(FCount);
  592. end;
  593. procedure TFPList.Clear;
  594. begin
  595. if Assigned(FList) then
  596. begin
  597. SetCount(0);
  598. SetCapacity(0);
  599. FList := nil;
  600. end;
  601. end;
  602. procedure TFPList.Delete(Index: Integer);
  603. begin
  604. If (Index<0) or (Index>=FCount) then
  605. Error (SListIndexError, Index);
  606. dec(FCount);
  607. System.Move (FList^[Index+1], FList^[Index], (FCount - Index) * SizeOf(Pointer));
  608. { Shrink the list if appropriate }
  609. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  610. begin
  611. FCapacity := FCapacity shr 1;
  612. ReallocMem(FList, SizeOf(Pointer) * FCapacity);
  613. end;
  614. end;
  615. class procedure TFPList.Error(const Msg: string; Data: PtrInt);
  616. begin
  617. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  618. end;
  619. procedure TFPList.Exchange(Index1, Index2: Integer);
  620. var
  621. Temp : Pointer;
  622. begin
  623. If ((Index1 >= FCount) or (Index1 < 0)) then
  624. Error(SListIndexError, Index1);
  625. If ((Index2 >= FCount) or (Index2 < 0)) then
  626. Error(SListIndexError, Index2);
  627. Temp := FList^[Index1];
  628. FList^[Index1] := FList^[Index2];
  629. FList^[Index2] := Temp;
  630. end;
  631. function TFPList.Expand: TFPList;
  632. var
  633. IncSize : Longint;
  634. begin
  635. Result := Self;
  636. if FCount < FCapacity then
  637. exit;
  638. IncSize := sizeof(ptrint)*2;
  639. if FCapacity > 127 then
  640. Inc(IncSize, FCapacity shr 2)
  641. else if FCapacity > sizeof(ptrint)*4 then
  642. Inc(IncSize, FCapacity shr 1)
  643. else if FCapacity >= sizeof(ptrint) then
  644. inc(IncSize,sizeof(ptrint));
  645. SetCapacity(FCapacity + IncSize);
  646. end;
  647. function TFPList.First: Pointer;
  648. begin
  649. If FCount<>0 then
  650. Result := Items[0]
  651. else
  652. Result := Nil;
  653. end;
  654. function TFPList.IndexOf(Item: Pointer): Integer;
  655. var
  656. psrc : PPointer;
  657. Index : Integer;
  658. begin
  659. Result:=-1;
  660. psrc:=@FList^[0];
  661. For Index:=0 To FCount-1 Do
  662. begin
  663. if psrc^=Item then
  664. begin
  665. Result:=Index;
  666. exit;
  667. end;
  668. inc(psrc);
  669. end;
  670. end;
  671. procedure TFPList.Insert(Index: Integer; Item: Pointer);
  672. begin
  673. if (Index < 0) or (Index > FCount )then
  674. Error(SlistIndexError, Index);
  675. iF FCount = FCapacity then Self.Expand;
  676. if Index<FCount then
  677. System.Move(Flist^[Index], Flist^[Index+1], (FCount - Index) * SizeOf(Pointer));
  678. FList^[Index] := Item;
  679. FCount := FCount + 1;
  680. end;
  681. function TFPList.Last: Pointer;
  682. begin
  683. If FCount<>0 then
  684. Result := Items[FCount - 1]
  685. else
  686. Result := nil
  687. end;
  688. procedure TFPList.Move(CurIndex, NewIndex: Integer);
  689. var
  690. Temp : Pointer;
  691. begin
  692. if ((CurIndex < 0) or (CurIndex > Count - 1)) then
  693. Error(SListIndexError, CurIndex);
  694. if (NewINdex < 0) then
  695. Error(SlistIndexError, NewIndex);
  696. Temp := FList^[CurIndex];
  697. FList^[CurIndex] := nil;
  698. Self.Delete(CurIndex);
  699. Self.Insert(NewIndex, nil);
  700. FList^[NewIndex] := Temp;
  701. end;
  702. function TFPList.Remove(Item: Pointer): Integer;
  703. begin
  704. Result := IndexOf(Item);
  705. If Result <> -1 then
  706. Self.Delete(Result);
  707. end;
  708. procedure TFPList.Pack;
  709. var
  710. NewCount,
  711. i : integer;
  712. pdest,
  713. psrc : PPointer;
  714. begin
  715. NewCount:=0;
  716. psrc:=@FList^[0];
  717. pdest:=psrc;
  718. For I:=0 To FCount-1 Do
  719. begin
  720. if assigned(psrc^) then
  721. begin
  722. pdest^:=psrc^;
  723. inc(pdest);
  724. inc(NewCount);
  725. end;
  726. inc(psrc);
  727. end;
  728. FCount:=NewCount;
  729. end;
  730. Procedure QuickSort(FList: PPointerList; L, R : Longint;Compare: TListSortCompare);
  731. var
  732. I, J, P: Longint;
  733. PItem, Q : Pointer;
  734. begin
  735. repeat
  736. I := L;
  737. J := R;
  738. P := (L + R) div 2;
  739. repeat
  740. PItem := FList^[P];
  741. while Compare(PItem, FList^[i]) > 0 do
  742. I := I + 1;
  743. while Compare(PItem, FList^[J]) < 0 do
  744. J := J - 1;
  745. If I <= J then
  746. begin
  747. Q := FList^[I];
  748. Flist^[I] := FList^[J];
  749. FList^[J] := Q;
  750. if P = I then
  751. P := J
  752. else if P = J then
  753. P := I;
  754. I := I + 1;
  755. J := J - 1;
  756. end;
  757. until I > J;
  758. if L < J then
  759. QuickSort(FList, L, J, Compare);
  760. L := I;
  761. until I >= R;
  762. end;
  763. procedure TFPList.Sort(Compare: TListSortCompare);
  764. begin
  765. if Not Assigned(FList) or (FCount < 2) then exit;
  766. QuickSort(Flist, 0, FCount-1, Compare);
  767. end;
  768. procedure TFPList.Assign(Obj: TFPList);
  769. var
  770. i: Integer;
  771. begin
  772. Clear;
  773. for I := 0 to Obj.Count - 1 do
  774. Add(Obj[i]);
  775. end;
  776. procedure TFPList.ForEachCall(proc2call:TListCallback;arg:pointer);
  777. var
  778. i : integer;
  779. p : pointer;
  780. begin
  781. For I:=0 To Count-1 Do
  782. begin
  783. p:=FList^[i];
  784. if assigned(p) then
  785. proc2call(p,arg);
  786. end;
  787. end;
  788. procedure TFPList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  789. var
  790. i : integer;
  791. p : pointer;
  792. begin
  793. For I:=0 To Count-1 Do
  794. begin
  795. p:=FList^[i];
  796. if assigned(p) then
  797. proc2call(p,arg);
  798. end;
  799. end;
  800. {*****************************************************************************
  801. TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
  802. *****************************************************************************}
  803. constructor TFPObjectList.Create(FreeObjects : boolean);
  804. begin
  805. Create;
  806. FFreeObjects := Freeobjects;
  807. end;
  808. destructor TFPObjectList.Destroy;
  809. begin
  810. if (FList <> nil) then
  811. begin
  812. Clear;
  813. FList.Destroy;
  814. end;
  815. inherited Destroy;
  816. end;
  817. procedure TFPObjectList.Clear;
  818. var
  819. i: integer;
  820. begin
  821. if FFreeObjects then
  822. for i := 0 to FList.Count - 1 do
  823. TObject(FList[i]).Free;
  824. FList.Clear;
  825. end;
  826. constructor TFPObjectList.Create;
  827. begin
  828. inherited Create;
  829. FList := TFPList.Create;
  830. FFreeObjects := True;
  831. end;
  832. function TFPObjectList.GetCount: integer;
  833. begin
  834. Result := FList.Count;
  835. end;
  836. procedure TFPObjectList.SetCount(const AValue: integer);
  837. begin
  838. if FList.Count <> AValue then
  839. FList.Count := AValue;
  840. end;
  841. function TFPObjectList.GetItem(Index: Integer): TObject;
  842. begin
  843. Result := TObject(FList[Index]);
  844. end;
  845. procedure TFPObjectList.SetItem(Index: Integer; AObject: TObject);
  846. begin
  847. if OwnsObjects then
  848. TObject(FList[Index]).Free;
  849. FList[index] := AObject;
  850. end;
  851. procedure TFPObjectList.SetCapacity(NewCapacity: Integer);
  852. begin
  853. FList.Capacity := NewCapacity;
  854. end;
  855. function TFPObjectList.GetCapacity: integer;
  856. begin
  857. Result := FList.Capacity;
  858. end;
  859. function TFPObjectList.Add(AObject: TObject): Integer;
  860. begin
  861. Result := FList.Add(AObject);
  862. end;
  863. procedure TFPObjectList.Delete(Index: Integer);
  864. begin
  865. if OwnsObjects then
  866. TObject(FList[Index]).Free;
  867. FList.Delete(Index);
  868. end;
  869. procedure TFPObjectList.Exchange(Index1, Index2: Integer);
  870. begin
  871. FList.Exchange(Index1, Index2);
  872. end;
  873. function TFPObjectList.Expand: TFPObjectList;
  874. begin
  875. FList.Expand;
  876. Result := Self;
  877. end;
  878. function TFPObjectList.Extract(Item: TObject): TObject;
  879. begin
  880. Result := TObject(FList.Extract(Item));
  881. end;
  882. function TFPObjectList.Remove(AObject: TObject): Integer;
  883. begin
  884. Result := IndexOf(AObject);
  885. if (Result <> -1) then
  886. begin
  887. if OwnsObjects then
  888. TObject(FList[Result]).Free;
  889. FList.Delete(Result);
  890. end;
  891. end;
  892. function TFPObjectList.IndexOf(AObject: TObject): Integer;
  893. begin
  894. Result := FList.IndexOf(Pointer(AObject));
  895. end;
  896. function TFPObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  897. var
  898. I : Integer;
  899. begin
  900. I:=AStartAt;
  901. Result:=-1;
  902. If AExact then
  903. while (I<Count) and (Result=-1) do
  904. If Items[i].ClassType=AClass then
  905. Result:=I
  906. else
  907. Inc(I)
  908. else
  909. while (I<Count) and (Result=-1) do
  910. If Items[i].InheritsFrom(AClass) then
  911. Result:=I
  912. else
  913. Inc(I);
  914. end;
  915. procedure TFPObjectList.Insert(Index: Integer; AObject: TObject);
  916. begin
  917. FList.Insert(Index, Pointer(AObject));
  918. end;
  919. procedure TFPObjectList.Move(CurIndex, NewIndex: Integer);
  920. begin
  921. FList.Move(CurIndex, NewIndex);
  922. end;
  923. procedure TFPObjectList.Assign(Obj: TFPObjectList);
  924. var
  925. i: Integer;
  926. begin
  927. Clear;
  928. for I := 0 to Obj.Count - 1 do
  929. Add(Obj[i]);
  930. end;
  931. procedure TFPObjectList.Pack;
  932. begin
  933. FList.Pack;
  934. end;
  935. procedure TFPObjectList.Sort(Compare: TListSortCompare);
  936. begin
  937. FList.Sort(Compare);
  938. end;
  939. function TFPObjectList.First: TObject;
  940. begin
  941. Result := TObject(FList.First);
  942. end;
  943. function TFPObjectList.Last: TObject;
  944. begin
  945. Result := TObject(FList.Last);
  946. end;
  947. procedure TFPObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
  948. begin
  949. FList.ForEachCall(TListCallBack(proc2call),arg);
  950. end;
  951. procedure TFPObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  952. begin
  953. FList.ForEachCall(TListStaticCallBack(proc2call),arg);
  954. end;
  955. {*****************************************************************************
  956. TFPHashList
  957. *****************************************************************************}
  958. function FPHash(const s:shortstring):LongWord;
  959. Var
  960. p,pmax : pchar;
  961. begin
  962. {$ifopt Q+}
  963. {$define overflowon}
  964. {$Q-}
  965. {$endif}
  966. result:=0;
  967. p:=@s[1];
  968. pmax:=@s[length(s)+1];
  969. while (p<pmax) do
  970. begin
  971. result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
  972. inc(p);
  973. end;
  974. {$ifdef overflowon}
  975. {$Q+}
  976. {$undef overflowon}
  977. {$endif}
  978. end;
  979. function FPHash(P: PChar; Len: Integer): LongWord;
  980. Var
  981. pmax : pchar;
  982. begin
  983. {$ifopt Q+}
  984. {$define overflowon}
  985. {$Q-}
  986. {$endif}
  987. result:=0;
  988. pmax:=p+len;
  989. while (p<pmax) do
  990. begin
  991. result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
  992. inc(p);
  993. end;
  994. {$ifdef overflowon}
  995. {$Q+}
  996. {$undef overflowon}
  997. {$endif}
  998. end;
  999. procedure TFPHashList.RaiseIndexError(Index : Integer);
  1000. begin
  1001. Error(SListIndexError, Index);
  1002. end;
  1003. function TFPHashList.Get(Index: Integer): Pointer;
  1004. begin
  1005. If (Index < 0) or (Index >= FCount) then
  1006. RaiseIndexError(Index);
  1007. Result:=FHashList^[Index].Data;
  1008. end;
  1009. procedure TFPHashList.Put(Index: Integer; Item: Pointer);
  1010. begin
  1011. if (Index < 0) or (Index >= FCount) then
  1012. RaiseIndexError(Index);
  1013. FHashList^[Index].Data:=Item;
  1014. end;
  1015. function TFPHashList.NameOfIndex(Index: Integer): shortstring;
  1016. begin
  1017. If (Index < 0) or (Index >= FCount) then
  1018. RaiseIndexError(Index);
  1019. with FHashList^[Index] do
  1020. begin
  1021. if StrIndex>=0 then
  1022. Result:=PShortString(@FStrs[StrIndex])^
  1023. else
  1024. Result:='';
  1025. end;
  1026. end;
  1027. function TFPHashList.HashOfIndex(Index: Integer): LongWord;
  1028. begin
  1029. If (Index < 0) or (Index >= FCount) then
  1030. RaiseIndexError(Index);
  1031. Result:=FHashList^[Index].HashValue;
  1032. end;
  1033. function TFPHashList.GetNextCollision(Index: Integer): Integer;
  1034. begin
  1035. Result:=-1;
  1036. if ((Index > -1) and (Index < FCount)) then
  1037. Result:=FHashList^[Index].NextIndex;
  1038. end;
  1039. function TFPHashList.Extract(item: Pointer): Pointer;
  1040. var
  1041. i : Integer;
  1042. begin
  1043. result := nil;
  1044. i := IndexOf(item);
  1045. if i >= 0 then
  1046. begin
  1047. Result := item;
  1048. Delete(i);
  1049. end;
  1050. end;
  1051. procedure TFPHashList.SetCapacity(NewCapacity: Integer);
  1052. var
  1053. power: longint;
  1054. begin
  1055. { use a power of two to be able to quickly calculate the hash table index }
  1056. if NewCapacity <> 0 then
  1057. NewCapacity := nextpowerof2((NewCapacity+(MaxItemsPerHash-1)) div MaxItemsPerHash, power) * MaxItemsPerHash;
  1058. if (NewCapacity < FCount) or (NewCapacity > MaxHashListSize) then
  1059. Error (SListCapacityError, NewCapacity);
  1060. if NewCapacity = FCapacity then
  1061. exit;
  1062. ReallocMem(FHashList, NewCapacity*SizeOf(THashItem));
  1063. FCapacity := NewCapacity;
  1064. { Maybe expand hash also }
  1065. if FCapacity>FHashCapacity*MaxItemsPerHash then
  1066. SetHashCapacity(FCapacity div MaxItemsPerHash);
  1067. end;
  1068. procedure TFPHashList.SetCount(NewCount: Integer);
  1069. begin
  1070. if (NewCount < 0) or (NewCount > MaxHashListSize)then
  1071. Error(SListCountError, NewCount);
  1072. If NewCount > FCount then
  1073. begin
  1074. If NewCount > FCapacity then
  1075. SetCapacity(NewCount);
  1076. If FCount < NewCount then
  1077. { FCapacity is NewCount rounded up to the next power of 2 }
  1078. FillChar(FHashList^[FCount], (FCapacity-FCount) div Sizeof(THashItem), 0);
  1079. end;
  1080. FCount := Newcount;
  1081. end;
  1082. procedure TFPHashList.SetStrCapacity(NewCapacity: Integer);
  1083. begin
  1084. If (NewCapacity < FStrCount) or (NewCapacity > MaxHashStrSize) then
  1085. Error (SListCapacityError, NewCapacity);
  1086. if NewCapacity = FStrCapacity then
  1087. exit;
  1088. ReallocMem(FStrs, NewCapacity);
  1089. FStrCapacity := NewCapacity;
  1090. end;
  1091. procedure TFPHashList.SetHashCapacity(NewCapacity: Integer);
  1092. var
  1093. power: longint;
  1094. begin
  1095. If (NewCapacity < 1) then
  1096. Error (SListCapacityError, NewCapacity);
  1097. if FHashCapacity=NewCapacity then
  1098. exit;
  1099. if (NewCapacity<>0) and
  1100. not ispowerof2(NewCapacity,power) then
  1101. Error(SListCapacityPower2Error, NewCapacity);
  1102. FHashCapacity:=NewCapacity;
  1103. ReallocMem(FHashTable, FHashCapacity*sizeof(Integer));
  1104. FCapacityMask:=(1 shl power)-1;
  1105. ReHash;
  1106. end;
  1107. procedure TFPHashList.ReHash;
  1108. var
  1109. i : Integer;
  1110. begin
  1111. FillDword(FHashTable^,FHashCapacity,LongWord(-1));
  1112. For i:=0 To FCount-1 Do
  1113. AddToHashTable(i);
  1114. end;
  1115. constructor TFPHashList.Create;
  1116. begin
  1117. SetHashCapacity(1);
  1118. end;
  1119. destructor TFPHashList.Destroy;
  1120. begin
  1121. Clear;
  1122. if assigned(FHashTable) then
  1123. FreeMem(FHashTable);
  1124. inherited Destroy;
  1125. end;
  1126. function TFPHashList.AddStr(const s:shortstring): Integer;
  1127. var
  1128. Len : Integer;
  1129. begin
  1130. len:=length(s)+1;
  1131. if FStrCount+Len >= FStrCapacity then
  1132. StrExpand(Len);
  1133. System.Move(s[0],FStrs[FStrCount],Len);
  1134. result:=FStrCount;
  1135. inc(FStrCount,Len);
  1136. end;
  1137. procedure TFPHashList.AddToHashTable(Index: Integer);
  1138. var
  1139. HashIndex : Integer;
  1140. begin
  1141. with FHashList^[Index] do
  1142. begin
  1143. if not assigned(Data) then
  1144. exit;
  1145. HashIndex:=HashValue and FCapacityMask;
  1146. NextIndex:=FHashTable^[HashIndex];
  1147. FHashTable^[HashIndex]:=Index;
  1148. end;
  1149. end;
  1150. function TFPHashList.Add(const AName:shortstring;Item: Pointer): Integer;
  1151. begin
  1152. if FCount = FCapacity then
  1153. Expand;
  1154. with FHashList^[FCount] do
  1155. begin
  1156. HashValue:=FPHash(AName);
  1157. Data:=Item;
  1158. StrIndex:=AddStr(AName);
  1159. end;
  1160. AddToHashTable(FCount);
  1161. Result := FCount;
  1162. inc(FCount);
  1163. end;
  1164. procedure TFPHashList.Clear;
  1165. begin
  1166. if Assigned(FHashList) then
  1167. begin
  1168. FCount:=0;
  1169. SetCapacity(0);
  1170. FHashList := nil;
  1171. end;
  1172. SetHashCapacity(1);
  1173. FHashTable^[0]:=-1; // sethashcapacity does not always call rehash
  1174. if Assigned(FStrs) then
  1175. begin
  1176. FStrCount:=0;
  1177. SetStrCapacity(0);
  1178. FStrs := nil;
  1179. end;
  1180. end;
  1181. procedure TFPHashList.Delete(Index: Integer);
  1182. begin
  1183. If (Index<0) or (Index>=FCount) then
  1184. Error (SListIndexError, Index);
  1185. { Remove from HashList }
  1186. dec(FCount);
  1187. System.Move (FHashList^[Index+1], FHashList^[Index], (FCount - Index) * Sizeof(THashItem));
  1188. { All indexes are updated, we need to build the hashtable again }
  1189. Rehash;
  1190. { Shrink the list if appropriate }
  1191. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  1192. begin
  1193. FCapacity := FCapacity shr 1;
  1194. ReallocMem(FHashList, Sizeof(THashItem) * FCapacity);
  1195. end;
  1196. end;
  1197. function TFPHashList.Remove(Item: Pointer): Integer;
  1198. begin
  1199. Result := IndexOf(Item);
  1200. If Result <> -1 then
  1201. Self.Delete(Result);
  1202. end;
  1203. class procedure TFPHashList.Error(const Msg: string; Data: PtrInt);
  1204. begin
  1205. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  1206. end;
  1207. function TFPHashList.Expand: TFPHashList;
  1208. var
  1209. IncSize : Longint;
  1210. begin
  1211. Result := Self;
  1212. if FCount < FCapacity then
  1213. exit;
  1214. IncSize := sizeof(ptrint)*2;
  1215. SetCapacity(FCapacity + IncSize);
  1216. end;
  1217. procedure TFPHashList.StrExpand(MinIncSize:Integer);
  1218. var
  1219. IncSize : Longint;
  1220. begin
  1221. if FStrCount+MinIncSize < FStrCapacity then
  1222. exit;
  1223. IncSize := 64;
  1224. if FStrCapacity > 255 then
  1225. Inc(IncSize, FStrCapacity shr 2);
  1226. SetStrCapacity(FStrCapacity + IncSize + MinIncSize);
  1227. end;
  1228. function TFPHashList.IndexOf(Item: Pointer): Integer;
  1229. var
  1230. psrc : PHashItem;
  1231. Index : integer;
  1232. begin
  1233. Result:=-1;
  1234. psrc:=@FHashList^[0];
  1235. For Index:=0 To FCount-1 Do
  1236. begin
  1237. if psrc^.Data=Item then
  1238. begin
  1239. Result:=Index;
  1240. exit;
  1241. end;
  1242. inc(psrc);
  1243. end;
  1244. end;
  1245. function TFPHashList.InternalFind(AHash:LongWord;const AName:shortstring;out PrevIndex:Integer):Integer;
  1246. begin
  1247. prefetch(AName);
  1248. Result:=FHashTable^[AHash and FCapacityMask];
  1249. PrevIndex:=-1;
  1250. while Result<>-1 do
  1251. begin
  1252. with FHashList^[Result] do
  1253. begin
  1254. if assigned(Data) and
  1255. (HashValue=AHash) 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 and FCapacityMask]:=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. { TLinkedListItem.GetCopy performs a "move" to copy all data -> reinit
  1916. the ansistring, so the refcount is properly increased }
  1917. Initialize(TCmdStrListItem(Result).FPStr);
  1918. TCmdStrListItem(Result).FPStr:=FPstr;
  1919. end;
  1920. {****************************************************************************
  1921. TCmdStrList
  1922. ****************************************************************************}
  1923. constructor TCmdStrList.Create;
  1924. begin
  1925. inherited Create;
  1926. FDoubles:=true;
  1927. end;
  1928. constructor TCmdStrList.Create_no_double;
  1929. begin
  1930. inherited Create;
  1931. FDoubles:=false;
  1932. end;
  1933. procedure TCmdStrList.insert(const s : TCmdStr);
  1934. begin
  1935. if (s='') or
  1936. ((not FDoubles) and (find(s)<>nil)) then
  1937. exit;
  1938. inherited insert(TCmdStrListItem.create(s));
  1939. end;
  1940. procedure TCmdStrList.concat(const s : TCmdStr);
  1941. begin
  1942. if (s='') or
  1943. ((not FDoubles) and (find(s)<>nil)) then
  1944. exit;
  1945. inherited concat(TCmdStrListItem.create(s));
  1946. end;
  1947. procedure TCmdStrList.remove(const s : TCmdStr);
  1948. var
  1949. p : TCmdStrListItem;
  1950. begin
  1951. if s='' then
  1952. exit;
  1953. p:=find(s);
  1954. if assigned(p) then
  1955. begin
  1956. inherited Remove(p);
  1957. p.Free;
  1958. end;
  1959. end;
  1960. function TCmdStrList.GetFirst : TCmdStr;
  1961. var
  1962. p : TCmdStrListItem;
  1963. begin
  1964. p:=TCmdStrListItem(inherited GetFirst);
  1965. if p=nil then
  1966. GetFirst:=''
  1967. else
  1968. begin
  1969. GetFirst:=p.FPStr;
  1970. p.free;
  1971. end;
  1972. end;
  1973. function TCmdStrList.Getlast : TCmdStr;
  1974. var
  1975. p : TCmdStrListItem;
  1976. begin
  1977. p:=TCmdStrListItem(inherited Getlast);
  1978. if p=nil then
  1979. Getlast:=''
  1980. else
  1981. begin
  1982. Getlast:=p.FPStr;
  1983. p.free;
  1984. end;
  1985. end;
  1986. function TCmdStrList.FindCase(const s:TCmdStr):TCmdStrListItem;
  1987. var
  1988. NewNode : TCmdStrListItem;
  1989. begin
  1990. result:=nil;
  1991. if s='' then
  1992. exit;
  1993. NewNode:=TCmdStrListItem(FFirst);
  1994. while assigned(NewNode) do
  1995. begin
  1996. if NewNode.FPStr=s then
  1997. begin
  1998. result:=NewNode;
  1999. exit;
  2000. end;
  2001. NewNode:=TCmdStrListItem(NewNode.Next);
  2002. end;
  2003. end;
  2004. function TCmdStrList.Find(const s:TCmdStr):TCmdStrListItem;
  2005. var
  2006. NewNode : TCmdStrListItem;
  2007. begin
  2008. result:=nil;
  2009. if s='' then
  2010. exit;
  2011. NewNode:=TCmdStrListItem(FFirst);
  2012. while assigned(NewNode) do
  2013. begin
  2014. if SysUtils.CompareText(s, NewNode.FPStr)=0 then
  2015. begin
  2016. result:=NewNode;
  2017. exit;
  2018. end;
  2019. NewNode:=TCmdStrListItem(NewNode.Next);
  2020. end;
  2021. end;
  2022. procedure TCmdStrList.InsertItem(item:TCmdStrListItem);
  2023. begin
  2024. inherited Insert(item);
  2025. end;
  2026. procedure TCmdStrList.ConcatItem(item:TCmdStrListItem);
  2027. begin
  2028. inherited Concat(item);
  2029. end;
  2030. {****************************************************************************
  2031. tdynamicarray
  2032. ****************************************************************************}
  2033. constructor tdynamicarray.create(Ablocksize:longword);
  2034. begin
  2035. FPosn:=0;
  2036. FPosnblock:=nil;
  2037. FFirstblock:=nil;
  2038. FLastblock:=nil;
  2039. FCurrBlockSize:=0;
  2040. FMaxBlockSize:=Ablocksize;
  2041. grow;
  2042. end;
  2043. destructor tdynamicarray.destroy;
  2044. var
  2045. hp : pdynamicblock;
  2046. begin
  2047. while assigned(FFirstblock) do
  2048. begin
  2049. hp:=FFirstblock;
  2050. FFirstblock:=FFirstblock^.Next;
  2051. Freemem(hp);
  2052. end;
  2053. end;
  2054. function tdynamicarray.size:longword;
  2055. begin
  2056. if assigned(FLastblock) then
  2057. size:=FLastblock^.pos+FLastblock^.used
  2058. else
  2059. size:=0;
  2060. end;
  2061. procedure tdynamicarray.reset;
  2062. var
  2063. hp : pdynamicblock;
  2064. begin
  2065. while assigned(FFirstblock) do
  2066. begin
  2067. hp:=FFirstblock;
  2068. FFirstblock:=FFirstblock^.Next;
  2069. Freemem(hp);
  2070. end;
  2071. FPosn:=0;
  2072. FPosnblock:=nil;
  2073. FFirstblock:=nil;
  2074. FLastblock:=nil;
  2075. grow;
  2076. end;
  2077. procedure tdynamicarray.grow;
  2078. var
  2079. nblock : pdynamicblock;
  2080. OptBlockSize,
  2081. IncSize : integer;
  2082. begin
  2083. if CurrBlockSize<FMaxBlocksize then
  2084. begin
  2085. IncSize := sizeof(ptrint)*8;
  2086. if FCurrBlockSize > 255 then
  2087. Inc(IncSize, FCurrBlockSize shr 2);
  2088. inc(FCurrBlockSize,IncSize);
  2089. end;
  2090. if CurrBlockSize>FMaxBlocksize then
  2091. FCurrBlockSize:=FMaxBlocksize;
  2092. { Calculate the most optimal size so there is no alignment overhead
  2093. lost in the heap manager }
  2094. OptBlockSize:=cutils.Align(CurrBlockSize+dynamicblockbasesize,16)-dynamicblockbasesize-sizeof(ptrint);
  2095. Getmem(nblock,OptBlockSize+dynamicblockbasesize);
  2096. if not assigned(FFirstblock) then
  2097. begin
  2098. FFirstblock:=nblock;
  2099. FPosnblock:=nblock;
  2100. nblock^.pos:=0;
  2101. end
  2102. else
  2103. begin
  2104. FLastblock^.Next:=nblock;
  2105. nblock^.pos:=FLastblock^.pos+FLastblock^.size;
  2106. end;
  2107. nblock^.used:=0;
  2108. nblock^.size:=OptBlockSize;
  2109. nblock^.Next:=nil;
  2110. fillchar(nblock^.data,nblock^.size,0);
  2111. FLastblock:=nblock;
  2112. end;
  2113. procedure tdynamicarray.align(i:longword);
  2114. var
  2115. j : longword;
  2116. begin
  2117. j:=(FPosn mod i);
  2118. if j<>0 then
  2119. begin
  2120. j:=i-j;
  2121. if FPosnblock^.used+j>FPosnblock^.size then
  2122. begin
  2123. dec(j,FPosnblock^.size-FPosnblock^.used);
  2124. FPosnblock^.used:=FPosnblock^.size;
  2125. grow;
  2126. FPosnblock:=FLastblock;
  2127. end;
  2128. inc(FPosnblock^.used,j);
  2129. inc(FPosn,j);
  2130. end;
  2131. end;
  2132. procedure tdynamicarray.seek(i:longword);
  2133. begin
  2134. if (i<FPosnblock^.pos) or (i>=FPosnblock^.pos+FPosnblock^.size) then
  2135. begin
  2136. { set FPosnblock correct if the size is bigger then
  2137. the current block }
  2138. if FPosnblock^.pos>i then
  2139. FPosnblock:=FFirstblock;
  2140. while assigned(FPosnblock) do
  2141. begin
  2142. if FPosnblock^.pos+FPosnblock^.size>i then
  2143. break;
  2144. FPosnblock:=FPosnblock^.Next;
  2145. end;
  2146. { not found ? then increase blocks }
  2147. if not assigned(FPosnblock) then
  2148. begin
  2149. repeat
  2150. { the current FLastblock is now also fully used }
  2151. FLastblock^.used:=FLastblock^.size;
  2152. grow;
  2153. FPosnblock:=FLastblock;
  2154. until FPosnblock^.pos+FPosnblock^.size>=i;
  2155. end;
  2156. end;
  2157. FPosn:=i;
  2158. if FPosn-FPosnblock^.pos>FPosnblock^.used then
  2159. FPosnblock^.used:=FPosn-FPosnblock^.pos;
  2160. end;
  2161. procedure tdynamicarray.write(const d;len:longword);
  2162. var
  2163. p : pchar;
  2164. i,j : longword;
  2165. begin
  2166. p:=pchar(@d);
  2167. while (len>0) do
  2168. begin
  2169. i:=FPosn-FPosnblock^.pos;
  2170. if i+len>=FPosnblock^.size then
  2171. begin
  2172. j:=FPosnblock^.size-i;
  2173. move(p^,FPosnblock^.data[i],j);
  2174. inc(p,j);
  2175. inc(FPosn,j);
  2176. dec(len,j);
  2177. FPosnblock^.used:=FPosnblock^.size;
  2178. if assigned(FPosnblock^.Next) then
  2179. FPosnblock:=FPosnblock^.Next
  2180. else
  2181. begin
  2182. grow;
  2183. FPosnblock:=FLastblock;
  2184. end;
  2185. end
  2186. else
  2187. begin
  2188. move(p^,FPosnblock^.data[i],len);
  2189. inc(p,len);
  2190. inc(FPosn,len);
  2191. i:=FPosn-FPosnblock^.pos;
  2192. if i>FPosnblock^.used then
  2193. FPosnblock^.used:=i;
  2194. len:=0;
  2195. end;
  2196. end;
  2197. end;
  2198. procedure tdynamicarray.writestr(const s:string);
  2199. begin
  2200. write(s[1],length(s));
  2201. end;
  2202. function tdynamicarray.read(var d;len:longword):longword;
  2203. var
  2204. p : pchar;
  2205. i,j,res : longword;
  2206. begin
  2207. res:=0;
  2208. p:=pchar(@d);
  2209. while (len>0) do
  2210. begin
  2211. i:=FPosn-FPosnblock^.pos;
  2212. if i+len>=FPosnblock^.used then
  2213. begin
  2214. j:=FPosnblock^.used-i;
  2215. move(FPosnblock^.data[i],p^,j);
  2216. inc(p,j);
  2217. inc(FPosn,j);
  2218. inc(res,j);
  2219. dec(len,j);
  2220. if assigned(FPosnblock^.Next) then
  2221. FPosnblock:=FPosnblock^.Next
  2222. else
  2223. break;
  2224. end
  2225. else
  2226. begin
  2227. move(FPosnblock^.data[i],p^,len);
  2228. inc(p,len);
  2229. inc(FPosn,len);
  2230. inc(res,len);
  2231. len:=0;
  2232. end;
  2233. end;
  2234. read:=res;
  2235. end;
  2236. procedure tdynamicarray.readstream(f:TCStream;maxlen:longword);
  2237. var
  2238. i,left : longword;
  2239. begin
  2240. repeat
  2241. left:=FPosnblock^.size-FPosnblock^.used;
  2242. if left>maxlen then
  2243. left:=maxlen;
  2244. i:=f.Read(FPosnblock^.data[FPosnblock^.used],left);
  2245. dec(maxlen,i);
  2246. inc(FPosnblock^.used,i);
  2247. if FPosnblock^.used=FPosnblock^.size then
  2248. begin
  2249. if assigned(FPosnblock^.Next) then
  2250. FPosnblock:=FPosnblock^.Next
  2251. else
  2252. begin
  2253. grow;
  2254. FPosnblock:=FLastblock;
  2255. end;
  2256. end;
  2257. until (i<left) or (maxlen=0);
  2258. end;
  2259. procedure tdynamicarray.writestream(f:TCStream);
  2260. var
  2261. hp : pdynamicblock;
  2262. begin
  2263. hp:=FFirstblock;
  2264. while assigned(hp) do
  2265. begin
  2266. f.Write(hp^.data,hp^.used);
  2267. hp:=hp^.Next;
  2268. end;
  2269. end;
  2270. {****************************************************************************
  2271. thashset
  2272. ****************************************************************************}
  2273. constructor THashSet.Create(InitSize: Integer; OwnKeys, OwnObjects: Boolean);
  2274. var
  2275. I: Integer;
  2276. begin
  2277. inherited Create;
  2278. FOwnsObjects := OwnObjects;
  2279. FOwnsKeys := OwnKeys;
  2280. I := 64;
  2281. while I < InitSize do I := I shl 1;
  2282. FBucketCount := I;
  2283. FBucket := AllocMem(I * sizeof(PHashSetItem));
  2284. end;
  2285. destructor THashSet.Destroy;
  2286. begin
  2287. Clear;
  2288. FreeMem(FBucket);
  2289. inherited Destroy;
  2290. end;
  2291. procedure THashSet.Clear;
  2292. var
  2293. I: Integer;
  2294. item, next: PHashSetItem;
  2295. begin
  2296. for I := 0 to FBucketCount-1 do
  2297. begin
  2298. item := FBucket[I];
  2299. while Assigned(item) do
  2300. begin
  2301. next := item^.Next;
  2302. if FOwnsObjects then
  2303. item^.Data.Free;
  2304. if FOwnsKeys then
  2305. FreeMem(item^.Key);
  2306. Dispose(item);
  2307. item := next;
  2308. end;
  2309. end;
  2310. FillChar(FBucket^, FBucketCount * sizeof(PHashSetItem), 0);
  2311. end;
  2312. function THashSet.Find(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2313. var
  2314. Dummy: Boolean;
  2315. begin
  2316. Result := Lookup(Key, KeyLen, Dummy, False);
  2317. end;
  2318. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer;
  2319. var Found: Boolean): PHashSetItem;
  2320. begin
  2321. Result := Lookup(Key, KeyLen, Found, True);
  2322. end;
  2323. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2324. var
  2325. Dummy: Boolean;
  2326. begin
  2327. Result := Lookup(Key, KeyLen, Dummy, True);
  2328. end;
  2329. function THashSet.Get(Key: Pointer; KeyLen: Integer): TObject;
  2330. var
  2331. e: PHashSetItem;
  2332. Dummy: Boolean;
  2333. begin
  2334. e := Lookup(Key, KeyLen, Dummy, False);
  2335. if Assigned(e) then
  2336. Result := e^.Data
  2337. else
  2338. Result := nil;
  2339. end;
  2340. function THashSet.Lookup(Key: Pointer; KeyLen: Integer;
  2341. var Found: Boolean; CanCreate: Boolean): PHashSetItem;
  2342. var
  2343. Entry: PPHashSetItem;
  2344. h: LongWord;
  2345. begin
  2346. h := FPHash(Key, KeyLen);
  2347. Entry := @FBucket[h mod FBucketCount];
  2348. while Assigned(Entry^) and
  2349. not ((Entry^^.HashValue = h) and (Entry^^.KeyLength = KeyLen) and
  2350. (CompareByte(Entry^^.Key^, Key^, KeyLen) = 0)) do
  2351. Entry := @Entry^^.Next;
  2352. Found := Assigned(Entry^);
  2353. if Found or (not CanCreate) then
  2354. begin
  2355. Result := Entry^;
  2356. Exit;
  2357. end;
  2358. if FCount > FBucketCount then { arbitrary limit, probably too high }
  2359. begin
  2360. { rehash and repeat search }
  2361. Resize(FBucketCount * 2);
  2362. Result := Lookup(Key, KeyLen, Found, CanCreate);
  2363. end
  2364. else
  2365. begin
  2366. New(Result);
  2367. if FOwnsKeys then
  2368. begin
  2369. GetMem(Result^.Key, KeyLen);
  2370. Move(Key^, Result^.Key^, KeyLen);
  2371. end
  2372. else
  2373. Result^.Key := Key;
  2374. Result^.KeyLength := KeyLen;
  2375. Result^.HashValue := h;
  2376. Result^.Data := nil;
  2377. Result^.Next := nil;
  2378. Inc(FCount);
  2379. Entry^ := Result;
  2380. end;
  2381. end;
  2382. procedure THashSet.Resize(NewCapacity: LongWord);
  2383. var
  2384. p, chain: PPHashSetItem;
  2385. i: Integer;
  2386. e, n: PHashSetItem;
  2387. begin
  2388. p := AllocMem(NewCapacity * sizeof(PHashSetItem));
  2389. for i := 0 to FBucketCount-1 do
  2390. begin
  2391. e := FBucket[i];
  2392. while Assigned(e) do
  2393. begin
  2394. chain := @p[e^.HashValue mod NewCapacity];
  2395. n := e^.Next;
  2396. e^.Next := chain^;
  2397. chain^ := e;
  2398. e := n;
  2399. end;
  2400. end;
  2401. FBucketCount := NewCapacity;
  2402. FreeMem(FBucket);
  2403. FBucket := p;
  2404. end;
  2405. function THashSet.Remove(Entry: PHashSetItem): Boolean;
  2406. var
  2407. chain: PPHashSetItem;
  2408. begin
  2409. chain := @FBucket[Entry^.HashValue mod FBucketCount];
  2410. while Assigned(chain^) do
  2411. begin
  2412. if chain^ = Entry then
  2413. begin
  2414. chain^ := Entry^.Next;
  2415. if FOwnsObjects then
  2416. Entry^.Data.Free;
  2417. if FOwnsKeys then
  2418. FreeMem(Entry^.Key);
  2419. Dispose(Entry);
  2420. Dec(FCount);
  2421. Result := True;
  2422. Exit;
  2423. end;
  2424. chain := @chain^^.Next;
  2425. end;
  2426. Result := False;
  2427. end;
  2428. {****************************************************************************
  2429. tbitset
  2430. ****************************************************************************}
  2431. constructor tbitset.create(initsize: longint);
  2432. begin
  2433. create_bytesize((initsize+7) div 8);
  2434. end;
  2435. constructor tbitset.create_bytesize(bytesize: longint);
  2436. begin
  2437. fdatasize:=bytesize;
  2438. getmem(fdata,fdataSize);
  2439. clear;
  2440. end;
  2441. destructor tbitset.destroy;
  2442. begin
  2443. freemem(fdata,fdatasize);
  2444. inherited destroy;
  2445. end;
  2446. procedure tbitset.clear;
  2447. begin
  2448. fillchar(fdata^,fdatasize,0);
  2449. end;
  2450. procedure tbitset.grow(nsize: longint);
  2451. begin
  2452. reallocmem(fdata,nsize);
  2453. fillchar(fdata[fdatasize],nsize-fdatasize,0);
  2454. fdatasize:=nsize;
  2455. end;
  2456. procedure tbitset.include(index: longint);
  2457. var
  2458. dataindex: longint;
  2459. begin
  2460. { don't use bitpacked array, not endian-safe }
  2461. dataindex:=index shr 3;
  2462. if (dataindex>=datasize) then
  2463. grow(dataindex+16);
  2464. fdata[dataindex]:=fdata[dataindex] or (1 shl (index and 7));
  2465. end;
  2466. procedure tbitset.exclude(index: longint);
  2467. var
  2468. dataindex: longint;
  2469. begin
  2470. dataindex:=index shr 3;
  2471. if (dataindex>=datasize) then
  2472. exit;
  2473. fdata[dataindex]:=fdata[dataindex] and not(1 shl (index and 7));
  2474. end;
  2475. function tbitset.isset(index: longint): boolean;
  2476. var
  2477. dataindex: longint;
  2478. begin
  2479. dataindex:=index shr 3;
  2480. result:=
  2481. (dataindex<datasize) and
  2482. (((fdata[dataindex] shr (index and 7)) and 1)<>0);
  2483. end;
  2484. procedure tbitset.addset(aset: tbitset);
  2485. var
  2486. i: longint;
  2487. begin
  2488. if (aset.datasize>datasize) then
  2489. grow(aset.datasize);
  2490. for i:=0 to aset.datasize-1 do
  2491. fdata[i]:=fdata[i] or aset.data[i];
  2492. end;
  2493. procedure tbitset.subset(aset: tbitset);
  2494. var
  2495. i: longint;
  2496. begin
  2497. for i:=0 to min(datasize,aset.datasize)-1 do
  2498. fdata[i]:=fdata[i] and not(aset.data[i]);
  2499. end;
  2500. end.