cclasses.pas 85 KB

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