cclasses.pas 79 KB

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