cclasses.pas 85 KB

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