cclasses.pas 87 KB

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