cclasses.pas 86 KB

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