cclasses.pas 87 KB

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