cclasses.pas 83 KB

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