cclasses.pas 86 KB

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