cclasses.pas 85 KB

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