cclasses.pas 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252
  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. if FCount>0 then
  797. begin
  798. psrc:=@FList^[FCount-1];
  799. For Index:=FCount-1 downto 0 Do
  800. begin
  801. if psrc^=Item then
  802. begin
  803. Result:=Index;
  804. exit;
  805. end;
  806. dec(psrc);
  807. end;
  808. end;
  809. end;
  810. end;
  811. procedure TFPList.Insert(Index: Integer; Item: Pointer);
  812. begin
  813. if (Index < 0) or (Index > FCount )then
  814. Error(SlistIndexError, Index);
  815. iF FCount = FCapacity then Self.Expand;
  816. if Index<FCount then
  817. System.Move(Flist^[Index], Flist^[Index+1], (FCount - Index) * SizeOf(Pointer));
  818. FList^[Index] := Item;
  819. FCount := FCount + 1;
  820. end;
  821. function TFPList.Last: Pointer;
  822. begin
  823. If FCount<>0 then
  824. Result := Items[FCount - 1]
  825. else
  826. Result := nil
  827. end;
  828. procedure TFPList.Move(CurIndex, NewIndex: Integer);
  829. var
  830. Temp : Pointer;
  831. begin
  832. if ((CurIndex < 0) or (CurIndex > Count - 1)) then
  833. Error(SListIndexError, CurIndex);
  834. if (NewINdex < 0) then
  835. Error(SlistIndexError, NewIndex);
  836. Temp := FList^[CurIndex];
  837. FList^[CurIndex] := nil;
  838. Self.Delete(CurIndex);
  839. Self.Insert(NewIndex, nil);
  840. FList^[NewIndex] := Temp;
  841. end;
  842. function TFPList.Remove(Item: Pointer): Integer;
  843. begin
  844. Result := IndexOf(Item);
  845. If Result <> -1 then
  846. Self.Delete(Result);
  847. end;
  848. procedure TFPList.Pack;
  849. var
  850. NewCount,
  851. i : integer;
  852. pdest,
  853. psrc : PPointer;
  854. begin
  855. NewCount:=0;
  856. psrc:=@FList^[0];
  857. pdest:=psrc;
  858. For I:=0 To FCount-1 Do
  859. begin
  860. if assigned(psrc^) then
  861. begin
  862. pdest^:=psrc^;
  863. inc(pdest);
  864. inc(NewCount);
  865. end;
  866. inc(psrc);
  867. end;
  868. FCount:=NewCount;
  869. end;
  870. Procedure QuickSort(FList: PPointerList; L, R : Longint;Compare: TListSortCompare);
  871. var
  872. I, J, P: Longint;
  873. PItem, Q : Pointer;
  874. begin
  875. repeat
  876. I := L;
  877. J := R;
  878. P := (L + R) div 2;
  879. repeat
  880. PItem := FList^[P];
  881. while Compare(PItem, FList^[i]) > 0 do
  882. I := I + 1;
  883. while Compare(PItem, FList^[J]) < 0 do
  884. J := J - 1;
  885. If I <= J then
  886. begin
  887. Q := FList^[I];
  888. Flist^[I] := FList^[J];
  889. FList^[J] := Q;
  890. if P = I then
  891. P := J
  892. else if P = J then
  893. P := I;
  894. I := I + 1;
  895. J := J - 1;
  896. end;
  897. until I > J;
  898. if L < J then
  899. QuickSort(FList, L, J, Compare);
  900. L := I;
  901. until I >= R;
  902. end;
  903. procedure TFPList.Sort(Compare: TListSortCompare);
  904. begin
  905. if Not Assigned(FList) or (FCount < 2) then exit;
  906. QuickSort(Flist, 0, FCount-1, Compare);
  907. end;
  908. procedure TFPList.Assign(Obj: TFPList);
  909. var
  910. i: Integer;
  911. begin
  912. Clear;
  913. for I := 0 to Obj.Count - 1 do
  914. Add(Obj[i]);
  915. end;
  916. procedure TFPList.ForEachCall(proc2call:TListCallback;arg:pointer);
  917. var
  918. i : integer;
  919. p : pointer;
  920. begin
  921. For I:=0 To Count-1 Do
  922. begin
  923. p:=FList^[i];
  924. if assigned(p) then
  925. proc2call(p,arg);
  926. end;
  927. end;
  928. procedure TFPList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  929. var
  930. i : integer;
  931. p : pointer;
  932. begin
  933. For I:=0 To Count-1 Do
  934. begin
  935. p:=FList^[i];
  936. if assigned(p) then
  937. proc2call(p,arg);
  938. end;
  939. end;
  940. {*****************************************************************************
  941. TFPObjectList (Copied from rtl/objpas/classes/lists.inc)
  942. *****************************************************************************}
  943. constructor TFPObjectList.Create(FreeObjects : boolean);
  944. begin
  945. Create;
  946. FFreeObjects := Freeobjects;
  947. end;
  948. destructor TFPObjectList.Destroy;
  949. begin
  950. if (FList <> nil) then
  951. begin
  952. Clear;
  953. FList.Destroy;
  954. FList:=nil;
  955. end;
  956. inherited Destroy;
  957. end;
  958. procedure TFPObjectList.Clear;
  959. var
  960. i: integer;
  961. begin
  962. if FFreeObjects then
  963. for i := 0 to FList.Count - 1 do
  964. TObject(FList[i]).Free;
  965. FList.Clear;
  966. end;
  967. constructor TFPObjectList.Create;
  968. begin
  969. inherited Create;
  970. FList := TFPList.Create;
  971. FFreeObjects := True;
  972. end;
  973. function TFPObjectList.IndexOf(AObject: TObject): Integer;
  974. begin
  975. Result := FList.IndexOf(Pointer(AObject));
  976. end;
  977. function TFPObjectList.IndexOfItem(AObject: TObject; Direction: TDirection): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
  978. begin
  979. Result := FList.IndexOfItem(Pointer(AObject),Direction);
  980. end;
  981. function TFPObjectList.GetCount: integer;
  982. begin
  983. Result := FList.Count;
  984. end;
  985. procedure TFPObjectList.SetCount(const AValue: integer);
  986. begin
  987. if FList.Count <> AValue then
  988. FList.Count := AValue;
  989. end;
  990. function TFPObjectList.GetItem(Index: Integer): TObject;
  991. begin
  992. Result := TObject(FList[Index]);
  993. end;
  994. procedure TFPObjectList.SetItem(Index: Integer; AObject: TObject);
  995. begin
  996. if OwnsObjects then
  997. TObject(FList[Index]).Free;
  998. FList[index] := AObject;
  999. end;
  1000. procedure TFPObjectList.SetCapacity(NewCapacity: Integer);
  1001. begin
  1002. FList.Capacity := NewCapacity;
  1003. end;
  1004. function TFPObjectList.GetCapacity: integer;
  1005. begin
  1006. Result := FList.Capacity;
  1007. end;
  1008. function TFPObjectList.Add(AObject: TObject): Integer;
  1009. begin
  1010. Result := FList.Add(AObject);
  1011. end;
  1012. procedure TFPObjectList.Delete(Index: Integer);
  1013. begin
  1014. if OwnsObjects then
  1015. TObject(FList[Index]).Free;
  1016. FList.Delete(Index);
  1017. end;
  1018. procedure TFPObjectList.Exchange(Index1, Index2: Integer);
  1019. begin
  1020. FList.Exchange(Index1, Index2);
  1021. end;
  1022. function TFPObjectList.Expand: TFPObjectList;
  1023. begin
  1024. FList.Expand;
  1025. Result := Self;
  1026. end;
  1027. function TFPObjectList.Extract(Item: TObject): TObject;
  1028. begin
  1029. Result := TObject(FList.Extract(Item));
  1030. end;
  1031. function TFPObjectList.Remove(AObject: TObject): Integer;
  1032. begin
  1033. Result := IndexOf(AObject);
  1034. if (Result <> -1) then
  1035. begin
  1036. if OwnsObjects then
  1037. TObject(FList[Result]).Free;
  1038. FList.Delete(Result);
  1039. end;
  1040. end;
  1041. function TFPObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  1042. var
  1043. I : Integer;
  1044. begin
  1045. I:=AStartAt;
  1046. Result:=-1;
  1047. If AExact then
  1048. while (I<Count) and (Result=-1) do
  1049. If Items[i].ClassType=AClass then
  1050. Result:=I
  1051. else
  1052. Inc(I)
  1053. else
  1054. while (I<Count) and (Result=-1) do
  1055. If Items[i].InheritsFrom(AClass) then
  1056. Result:=I
  1057. else
  1058. Inc(I);
  1059. end;
  1060. procedure TFPObjectList.Insert(Index: Integer; AObject: TObject);
  1061. begin
  1062. FList.Insert(Index, Pointer(AObject));
  1063. end;
  1064. procedure TFPObjectList.Move(CurIndex, NewIndex: Integer);
  1065. begin
  1066. FList.Move(CurIndex, NewIndex);
  1067. end;
  1068. procedure TFPObjectList.Assign(Obj: TFPObjectList);
  1069. begin
  1070. Clear;
  1071. ConcatListCopy(Obj);
  1072. end;
  1073. procedure TFPObjectList.ConcatListCopy(Obj: TFPObjectList);
  1074. var
  1075. i: Integer;
  1076. begin
  1077. for I := 0 to Obj.Count - 1 do
  1078. Add(Obj[i]);
  1079. end;
  1080. procedure TFPObjectList.Pack;
  1081. begin
  1082. FList.Pack;
  1083. end;
  1084. procedure TFPObjectList.Sort(Compare: TListSortCompare);
  1085. begin
  1086. FList.Sort(Compare);
  1087. end;
  1088. function TFPObjectList.First: TObject;
  1089. begin
  1090. Result := TObject(FList.First);
  1091. end;
  1092. function TFPObjectList.Last: TObject;
  1093. begin
  1094. Result := TObject(FList.Last);
  1095. end;
  1096. procedure TFPObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
  1097. begin
  1098. FList.ForEachCall(TListCallBack(proc2call),arg);
  1099. end;
  1100. procedure TFPObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  1101. begin
  1102. FList.ForEachCall(TListStaticCallBack(proc2call),arg);
  1103. end;
  1104. {*****************************************************************************
  1105. TFPHashList
  1106. *****************************************************************************}
  1107. function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord;
  1108. Var
  1109. pmax : pchar;
  1110. begin
  1111. {$push}
  1112. {$q-,r-}
  1113. result:=Tag;
  1114. pmax:=p+len;
  1115. while (p<pmax) do
  1116. begin
  1117. {DJBHash: result:=result*33 + next_char}
  1118. result:=LongWord(LongInt(result shl 5) + LongInt(result)) + LongWord(P^);
  1119. inc(p);
  1120. end;
  1121. {$pop}
  1122. end;
  1123. function FPHash(P: PChar; Len: Integer): LongWord; inline;
  1124. begin
  1125. result:=fphash(P,Len, 5381);
  1126. end;
  1127. function FPHash(const s: shortstring): LongWord; inline;
  1128. begin
  1129. result:=fphash(pchar(@s[1]),length(s));
  1130. end;
  1131. function FPHash(const a: ansistring): LongWord; inline;
  1132. begin
  1133. result:=fphash(pchar(a),length(a));
  1134. end;
  1135. procedure TFPHashList.RaiseIndexError(Index : Integer);
  1136. begin
  1137. Error(SListIndexError, Index);
  1138. end;
  1139. function TFPHashList.Get(Index: Integer): Pointer;
  1140. begin
  1141. If (Index < 0) or (Index >= FCount) then
  1142. RaiseIndexError(Index);
  1143. Result:=FHashList^[Index].Data;
  1144. end;
  1145. procedure TFPHashList.Put(Index: Integer; Item: Pointer);
  1146. begin
  1147. if (Index < 0) or (Index >= FCount) then
  1148. RaiseIndexError(Index);
  1149. FHashList^[Index].Data:=Item;
  1150. end;
  1151. function TFPHashList.NameOfIndex(Index: Integer): TSymStr;
  1152. begin
  1153. If (Index < 0) or (Index >= FCount) then
  1154. RaiseIndexError(Index);
  1155. with FHashList^[Index] do
  1156. begin
  1157. if StrIndex>=0 then
  1158. Result:=PSymStr(@FStrs[StrIndex])^
  1159. else
  1160. Result:='';
  1161. end;
  1162. end;
  1163. function TFPHashList.HashOfIndex(Index: Integer): LongWord;
  1164. begin
  1165. If (Index < 0) or (Index >= FCount) then
  1166. RaiseIndexError(Index);
  1167. Result:=FHashList^[Index].HashValue;
  1168. end;
  1169. function TFPHashList.GetNextCollision(Index: Integer): Integer;
  1170. begin
  1171. Result:=-1;
  1172. if ((Index > -1) and (Index < FCount)) then
  1173. Result:=FHashList^[Index].NextIndex;
  1174. end;
  1175. function TFPHashList.Extract(item: Pointer): Pointer;
  1176. var
  1177. i : Integer;
  1178. begin
  1179. result := nil;
  1180. i := IndexOf(item);
  1181. if i >= 0 then
  1182. begin
  1183. Result := item;
  1184. Delete(i);
  1185. end;
  1186. end;
  1187. procedure TFPHashList.SetCapacity(NewCapacity: Integer);
  1188. var
  1189. power: longint;
  1190. begin
  1191. { use a power of two to be able to quickly calculate the hash table index }
  1192. if NewCapacity <> 0 then
  1193. NewCapacity := nextpowerof2((NewCapacity+(MaxItemsPerHash-1)) div MaxItemsPerHash, power) * MaxItemsPerHash;
  1194. if (NewCapacity < FCount) or (NewCapacity > MaxHashListSize) then
  1195. Error (SListCapacityError, NewCapacity);
  1196. if NewCapacity = FCapacity then
  1197. exit;
  1198. ReallocMem(FHashList, NewCapacity*SizeOf(THashItem));
  1199. FCapacity := NewCapacity;
  1200. { Maybe expand hash also }
  1201. if FCapacity>FHashCapacity*MaxItemsPerHash then
  1202. SetHashCapacity(FCapacity div MaxItemsPerHash);
  1203. end;
  1204. procedure TFPHashList.SetCount(NewCount: Integer);
  1205. begin
  1206. if (NewCount < 0) or (NewCount > MaxHashListSize)then
  1207. Error(SListCountError, NewCount);
  1208. If NewCount > FCount then
  1209. begin
  1210. If NewCount > FCapacity then
  1211. SetCapacity(NewCount);
  1212. If FCount < NewCount then
  1213. { FCapacity is NewCount rounded up to the next power of 2 }
  1214. FillChar(FHashList^[FCount], (FCapacity-FCount) div Sizeof(THashItem), 0);
  1215. end;
  1216. FCount := Newcount;
  1217. end;
  1218. procedure TFPHashList.SetStrCapacity(NewCapacity: Integer);
  1219. {$ifdef symansistr}
  1220. var
  1221. i: longint;
  1222. {$endif symansistr}
  1223. begin
  1224. {$push}{$warnings off}
  1225. If (NewCapacity < FStrCount) or (NewCapacity > MaxHashStrSize) then
  1226. Error (SListCapacityError, NewCapacity);
  1227. {$pop}
  1228. if NewCapacity = FStrCapacity then
  1229. exit;
  1230. {$ifdef symansistr}
  1231. { array of ansistrings -> finalize }
  1232. if (NewCapacity < FStrCapacity) then
  1233. for i:=NewCapacity to FStrCapacity-1 do
  1234. finalize(FStrs[i]);
  1235. ReallocMem(FStrs, NewCapacity*sizeof(pansistring));
  1236. { array of ansistrings -> initialize to nil }
  1237. if (NewCapacity > FStrCapacity) then
  1238. fillchar(FStrs[FStrCapacity],(NewCapacity-FStrCapacity)*sizeof(pansistring),0);
  1239. {$else symansistr}
  1240. ReallocMem(FStrs, NewCapacity);
  1241. {$endif symansistr}
  1242. FStrCapacity := NewCapacity;
  1243. end;
  1244. procedure TFPHashList.SetHashCapacity(NewCapacity: Integer);
  1245. var
  1246. power: longint;
  1247. begin
  1248. If (NewCapacity < 1) then
  1249. Error (SListCapacityError, NewCapacity);
  1250. if FHashCapacity=NewCapacity then
  1251. exit;
  1252. if (NewCapacity<>0) and
  1253. not ispowerof2(NewCapacity,power) then
  1254. Error(SListCapacityPower2Error, NewCapacity);
  1255. FHashCapacity:=NewCapacity;
  1256. ReallocMem(FHashTable, FHashCapacity*sizeof(Integer));
  1257. FCapacityMask:=(1 shl power)-1;
  1258. ReHash;
  1259. end;
  1260. procedure TFPHashList.ReHash;
  1261. var
  1262. i : Integer;
  1263. begin
  1264. FillDword(FHashTable^,FHashCapacity,LongWord(-1));
  1265. For i:=0 To FCount-1 Do
  1266. AddToHashTable(i);
  1267. end;
  1268. constructor TFPHashList.Create;
  1269. begin
  1270. SetHashCapacity(1);
  1271. end;
  1272. destructor TFPHashList.Destroy;
  1273. begin
  1274. Clear;
  1275. if assigned(FHashTable) then
  1276. FreeMem(FHashTable);
  1277. inherited Destroy;
  1278. end;
  1279. function TFPHashList.AddStr(const s:TSymStr): Integer;
  1280. {$ifndef symansistr}
  1281. var
  1282. Len : Integer;
  1283. {$endif symansistr}
  1284. begin
  1285. {$ifdef symansistr}
  1286. if FStrCount+1 >= FStrCapacity then
  1287. StrExpand(FStrCount+1);
  1288. FStrs[FStrCount]:=s;
  1289. result:=FStrCount;
  1290. inc(FStrCount);
  1291. {$else symansistr}
  1292. len:=length(s)+1;
  1293. if FStrCount+Len >= FStrCapacity then
  1294. StrExpand(Len);
  1295. System.Move(s[0],FStrs[FStrCount],Len);
  1296. result:=FStrCount;
  1297. inc(FStrCount,Len);
  1298. {$endif symansistr}
  1299. end;
  1300. procedure TFPHashList.AddToHashTable(Index: Integer);
  1301. var
  1302. HashIndex : Integer;
  1303. begin
  1304. with FHashList^[Index] do
  1305. begin
  1306. if not assigned(Data) then
  1307. exit;
  1308. HashIndex:=HashValue and FCapacityMask;
  1309. NextIndex:=FHashTable^[HashIndex];
  1310. FHashTable^[HashIndex]:=Index;
  1311. end;
  1312. end;
  1313. function TFPHashList.Add(const AName:TSymStr;Item: Pointer): Integer;
  1314. begin
  1315. if FCount = FCapacity then
  1316. Expand;
  1317. with FHashList^[FCount] do
  1318. begin
  1319. HashValue:=FPHash(AName);
  1320. Data:=Item;
  1321. StrIndex:=AddStr(AName);
  1322. end;
  1323. AddToHashTable(FCount);
  1324. Result := FCount;
  1325. inc(FCount);
  1326. end;
  1327. procedure TFPHashList.Clear;
  1328. begin
  1329. if Assigned(FHashList) then
  1330. begin
  1331. FCount:=0;
  1332. SetCapacity(0);
  1333. FHashList := nil;
  1334. end;
  1335. SetHashCapacity(1);
  1336. FHashTable^[0]:=-1; // sethashcapacity does not always call rehash
  1337. if Assigned(FStrs) then
  1338. begin
  1339. FStrCount:=0;
  1340. SetStrCapacity(0);
  1341. FStrs := nil;
  1342. end;
  1343. end;
  1344. procedure TFPHashList.Delete(Index: Integer);
  1345. begin
  1346. If (Index<0) or (Index>=FCount) then
  1347. Error (SListIndexError, Index);
  1348. { Remove from HashList }
  1349. dec(FCount);
  1350. System.Move (FHashList^[Index+1], FHashList^[Index], (FCount - Index) * Sizeof(THashItem));
  1351. { All indexes are updated, we need to build the hashtable again }
  1352. Rehash;
  1353. { Shrink the list if appropriate }
  1354. if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  1355. begin
  1356. FCapacity := FCapacity shr 1;
  1357. ReallocMem(FHashList, Sizeof(THashItem) * FCapacity);
  1358. end;
  1359. end;
  1360. function TFPHashList.Remove(Item: Pointer): Integer;
  1361. begin
  1362. Result := IndexOf(Item);
  1363. If Result <> -1 then
  1364. Self.Delete(Result);
  1365. end;
  1366. class procedure TFPHashList.Error(const Msg: string; Data: PtrInt);noreturn;
  1367. begin
  1368. Raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  1369. end;
  1370. function TFPHashList.Expand: TFPHashList;
  1371. var
  1372. IncSize : Longint;
  1373. begin
  1374. Result := Self;
  1375. if FCount < FCapacity then
  1376. exit;
  1377. IncSize := sizeof(ptrint)*2;
  1378. SetCapacity(FCapacity + IncSize);
  1379. end;
  1380. procedure TFPHashList.StrExpand(MinIncSize:Integer);
  1381. var
  1382. IncSize : Longint;
  1383. begin
  1384. if FStrCount+MinIncSize < FStrCapacity then
  1385. exit;
  1386. IncSize := 64;
  1387. if FStrCapacity > 255 then
  1388. Inc(IncSize, FStrCapacity shr 2);
  1389. SetStrCapacity(FStrCapacity + IncSize + MinIncSize);
  1390. end;
  1391. function TFPHashList.IndexOf(Item: Pointer): Integer;
  1392. var
  1393. psrc : PHashItem;
  1394. Index : integer;
  1395. begin
  1396. Result:=-1;
  1397. psrc:=@FHashList^[0];
  1398. For Index:=0 To FCount-1 Do
  1399. begin
  1400. if psrc^.Data=Item then
  1401. begin
  1402. Result:=Index;
  1403. exit;
  1404. end;
  1405. inc(psrc);
  1406. end;
  1407. end;
  1408. function TFPHashList.InternalFind(AHash:LongWord;const AName:TSymStr;out PrevIndex:Integer):Integer;
  1409. begin
  1410. prefetch(AName[1]);
  1411. Result:=FHashTable^[AHash and FCapacityMask];
  1412. PrevIndex:=-1;
  1413. while Result<>-1 do
  1414. begin
  1415. with FHashList^[Result] do
  1416. begin
  1417. if assigned(Data) and
  1418. (HashValue=AHash) and
  1419. (AName=PSymStr(@FStrs[StrIndex])^) then
  1420. exit;
  1421. PrevIndex:=Result;
  1422. Result:=NextIndex;
  1423. end;
  1424. end;
  1425. end;
  1426. function TFPHashList.Find(const AName:TSymStr): Pointer;
  1427. var
  1428. Index,
  1429. PrevIndex : Integer;
  1430. begin
  1431. Result:=nil;
  1432. Index:=InternalFind(FPHash(AName),AName,PrevIndex);
  1433. if Index=-1 then
  1434. exit;
  1435. Result:=FHashList^[Index].Data;
  1436. end;
  1437. function TFPHashList.FindIndexOf(const AName:TSymStr): Integer;
  1438. var
  1439. PrevIndex : Integer;
  1440. begin
  1441. Result:=InternalFind(FPHash(AName),AName,PrevIndex);
  1442. end;
  1443. function TFPHashList.FindWithHash(const AName:TSymStr;AHash:LongWord): Pointer;
  1444. var
  1445. Index,
  1446. PrevIndex : Integer;
  1447. begin
  1448. Result:=nil;
  1449. Index:=InternalFind(AHash,AName,PrevIndex);
  1450. if Index=-1 then
  1451. exit;
  1452. Result:=FHashList^[Index].Data;
  1453. end;
  1454. function TFPHashList.Rename(const AOldName,ANewName:TSymStr): Integer;
  1455. var
  1456. PrevIndex,
  1457. Index : Integer;
  1458. OldHash : LongWord;
  1459. begin
  1460. Result:=-1;
  1461. OldHash:=FPHash(AOldName);
  1462. Index:=InternalFind(OldHash,AOldName,PrevIndex);
  1463. if Index=-1 then
  1464. exit;
  1465. { Remove from current Hash }
  1466. if PrevIndex<>-1 then
  1467. FHashList^[PrevIndex].NextIndex:=FHashList^[Index].NextIndex
  1468. else
  1469. FHashTable^[OldHash and FCapacityMask]:=FHashList^[Index].NextIndex;
  1470. { Set new name and hash }
  1471. with FHashList^[Index] do
  1472. begin
  1473. HashValue:=FPHash(ANewName);
  1474. StrIndex:=AddStr(ANewName);
  1475. end;
  1476. { Insert back in Hash }
  1477. AddToHashTable(Index);
  1478. { Return Index }
  1479. Result:=Index;
  1480. end;
  1481. procedure TFPHashList.Pack;
  1482. var
  1483. NewCount,
  1484. i : integer;
  1485. pdest,
  1486. psrc : PHashItem;
  1487. begin
  1488. NewCount:=0;
  1489. psrc:=@FHashList^[0];
  1490. pdest:=psrc;
  1491. For I:=0 To FCount-1 Do
  1492. begin
  1493. if assigned(psrc^.Data) then
  1494. begin
  1495. pdest^:=psrc^;
  1496. inc(pdest);
  1497. inc(NewCount);
  1498. end;
  1499. inc(psrc);
  1500. end;
  1501. FCount:=NewCount;
  1502. { We need to ReHash to update the IndexNext }
  1503. ReHash;
  1504. { Release over-capacity }
  1505. SetCapacity(FCount);
  1506. SetStrCapacity(FStrCount);
  1507. end;
  1508. procedure TFPHashList.ShowStatistics;
  1509. var
  1510. HashMean,
  1511. HashStdDev : Double;
  1512. Index,
  1513. i,j : Integer;
  1514. begin
  1515. { Calculate Mean and StdDev }
  1516. HashMean:=0;
  1517. HashStdDev:=0;
  1518. for i:=0 to FHashCapacity-1 do
  1519. begin
  1520. j:=0;
  1521. Index:=FHashTable^[i];
  1522. while (Index<>-1) do
  1523. begin
  1524. inc(j);
  1525. Index:=FHashList^[Index].NextIndex;
  1526. end;
  1527. HashMean:=HashMean+j;
  1528. HashStdDev:=HashStdDev+Sqr(j);
  1529. end;
  1530. HashMean:=HashMean/FHashCapacity;
  1531. HashStdDev:=(HashStdDev-FHashCapacity*Sqr(HashMean));
  1532. If FHashCapacity>1 then
  1533. HashStdDev:=Sqrt(HashStdDev/(FHashCapacity-1))
  1534. else
  1535. HashStdDev:=0;
  1536. { Print info to stdout }
  1537. Writeln('HashSize : ',FHashCapacity);
  1538. Writeln('HashMean : ',HashMean:1:4);
  1539. Writeln('HashStdDev : ',HashStdDev:1:4);
  1540. Writeln('ListSize : ',FCount,'/',FCapacity);
  1541. Writeln('StringSize : ',FStrCount,'/',FStrCapacity);
  1542. end;
  1543. procedure TFPHashList.ForEachCall(proc2call:TListCallback;arg:pointer);
  1544. var
  1545. i : integer;
  1546. p : pointer;
  1547. begin
  1548. For I:=0 To Count-1 Do
  1549. begin
  1550. p:=FHashList^[i].Data;
  1551. if assigned(p) then
  1552. proc2call(p,arg);
  1553. end;
  1554. end;
  1555. procedure TFPHashList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  1556. var
  1557. i : integer;
  1558. p : pointer;
  1559. begin
  1560. For I:=0 To Count-1 Do
  1561. begin
  1562. p:=FHashList^[i].Data;
  1563. if assigned(p) then
  1564. proc2call(p,arg);
  1565. end;
  1566. end;
  1567. procedure TFPHashList.WhileEachCall(proc2call:TListCallback;arg:pointer);
  1568. var
  1569. i : integer;
  1570. p : pointer;
  1571. begin
  1572. i:=0;
  1573. while i<count do
  1574. begin
  1575. p:=FHashList^[i].Data;
  1576. if assigned(p) then
  1577. proc2call(p,arg);
  1578. inc(i);
  1579. end;
  1580. end;
  1581. procedure TFPHashList.WhileEachCall(proc2call:TListStaticCallback;arg:pointer);
  1582. var
  1583. i : integer;
  1584. p : pointer;
  1585. begin
  1586. i:=0;
  1587. while i<count do
  1588. begin
  1589. p:=FHashList^[i].Data;
  1590. if assigned(p) then
  1591. proc2call(p,arg);
  1592. inc(i);
  1593. end;
  1594. end;
  1595. {*****************************************************************************
  1596. TFPHashObjectList (Copied from rtl/objpas/classes/lists.inc)
  1597. *****************************************************************************}
  1598. constructor TFPHashObjectList.Create(FreeObjects : boolean = True);
  1599. begin
  1600. inherited Create;
  1601. FHashList := TFPHashList.Create;
  1602. FFreeObjects := Freeobjects;
  1603. end;
  1604. destructor TFPHashObjectList.Destroy;
  1605. begin
  1606. if (FHashList <> nil) then
  1607. begin
  1608. Clear;
  1609. FHashList.Destroy;
  1610. FHashList:=nil;
  1611. end;
  1612. inherited Destroy;
  1613. end;
  1614. procedure TFPHashObjectList.Clear;
  1615. var
  1616. i: integer;
  1617. begin
  1618. if FFreeObjects then
  1619. for i := 0 to FHashList.Count - 1 do
  1620. TObject(FHashList[i]).Free;
  1621. FHashList.Clear;
  1622. end;
  1623. function TFPHashObjectList.IndexOf(AObject: TObject): Integer;
  1624. begin
  1625. Result := FHashList.IndexOf(Pointer(AObject));
  1626. end;
  1627. function TFPHashObjectList.GetCount: integer;
  1628. begin
  1629. Result := FHashList.Count;
  1630. end;
  1631. procedure TFPHashObjectList.SetCount(const AValue: integer);
  1632. begin
  1633. if FHashList.Count <> AValue then
  1634. FHashList.Count := AValue;
  1635. end;
  1636. function TFPHashObjectList.GetItem(Index: Integer): TObject;
  1637. begin
  1638. Result := TObject(FHashList[Index]);
  1639. end;
  1640. procedure TFPHashObjectList.SetItem(Index: Integer; AObject: TObject);
  1641. begin
  1642. if OwnsObjects then
  1643. TObject(FHashList[Index]).Free;
  1644. FHashList[index] := AObject;
  1645. end;
  1646. procedure TFPHashObjectList.SetCapacity(NewCapacity: Integer);
  1647. begin
  1648. FHashList.Capacity := NewCapacity;
  1649. end;
  1650. function TFPHashObjectList.GetCapacity: integer;
  1651. begin
  1652. Result := FHashList.Capacity;
  1653. end;
  1654. function TFPHashObjectList.Add(const AName:TSymStr;AObject: TObject): Integer;
  1655. begin
  1656. Result := FHashList.Add(AName,AObject);
  1657. end;
  1658. function TFPHashObjectList.NameOfIndex(Index: Integer): TSymStr;
  1659. begin
  1660. Result := FHashList.NameOfIndex(Index);
  1661. end;
  1662. function TFPHashObjectList.HashOfIndex(Index: Integer): LongWord;
  1663. begin
  1664. Result := FHashList.HashOfIndex(Index);
  1665. end;
  1666. function TFPHashObjectList.GetNextCollision(Index: Integer): Integer;
  1667. begin
  1668. Result := FHashList.GetNextCollision(Index);
  1669. end;
  1670. procedure TFPHashObjectList.Delete(Index: Integer);
  1671. begin
  1672. if OwnsObjects then
  1673. TObject(FHashList[Index]).Free;
  1674. FHashList.Delete(Index);
  1675. end;
  1676. function TFPHashObjectList.Expand: TFPHashObjectList;
  1677. begin
  1678. FHashList.Expand;
  1679. Result := Self;
  1680. end;
  1681. function TFPHashObjectList.Extract(Item: TObject): TObject;
  1682. begin
  1683. Result := TObject(FHashList.Extract(Item));
  1684. end;
  1685. function TFPHashObjectList.Remove(AObject: TObject): Integer;
  1686. begin
  1687. Result := IndexOf(AObject);
  1688. if (Result <> -1) then
  1689. begin
  1690. if OwnsObjects then
  1691. TObject(FHashList[Result]).Free;
  1692. FHashList.Delete(Result);
  1693. end;
  1694. end;
  1695. function TFPHashObjectList.Find(const s:TSymStr): TObject;
  1696. begin
  1697. result:=TObject(FHashList.Find(s));
  1698. end;
  1699. function TFPHashObjectList.FindIndexOf(const s:TSymStr): Integer;
  1700. begin
  1701. result:=FHashList.FindIndexOf(s);
  1702. end;
  1703. function TFPHashObjectList.FindWithHash(const AName:TSymStr;AHash:LongWord): Pointer;
  1704. begin
  1705. Result:=TObject(FHashList.FindWithHash(AName,AHash));
  1706. end;
  1707. function TFPHashObjectList.Rename(const AOldName,ANewName:TSymStr): Integer;
  1708. begin
  1709. Result:=FHashList.Rename(AOldName,ANewName);
  1710. end;
  1711. function TFPHashObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  1712. var
  1713. I : Integer;
  1714. begin
  1715. I:=AStartAt;
  1716. Result:=-1;
  1717. If AExact then
  1718. while (I<Count) and (Result=-1) do
  1719. If Items[i].ClassType=AClass then
  1720. Result:=I
  1721. else
  1722. Inc(I)
  1723. else
  1724. while (I<Count) and (Result=-1) do
  1725. If Items[i].InheritsFrom(AClass) then
  1726. Result:=I
  1727. else
  1728. Inc(I);
  1729. end;
  1730. procedure TFPHashObjectList.Pack;
  1731. begin
  1732. FHashList.Pack;
  1733. end;
  1734. procedure TFPHashObjectList.ShowStatistics;
  1735. begin
  1736. FHashList.ShowStatistics;
  1737. end;
  1738. procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
  1739. begin
  1740. FHashList.ForEachCall(TListCallBack(proc2call),arg);
  1741. end;
  1742. procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  1743. begin
  1744. FHashList.ForEachCall(TListStaticCallBack(proc2call),arg);
  1745. end;
  1746. procedure TFPHashObjectList.WhileEachCall(proc2call:TObjectListCallback;arg:pointer);
  1747. begin
  1748. FHashList.WhileEachCall(TListCallBack(proc2call),arg);
  1749. end;
  1750. procedure TFPHashObjectList.WhileEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
  1751. begin
  1752. FHashList.WhileEachCall(TListStaticCallBack(proc2call),arg);
  1753. end;
  1754. {*****************************************************************************
  1755. TFPHashObject
  1756. *****************************************************************************}
  1757. procedure TFPHashObject.InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:TSymStr);
  1758. var
  1759. Index : integer;
  1760. begin
  1761. FOwner:=HashObjectList;
  1762. Index:=HashObjectList.Add(s,Self);
  1763. FStrIndex:=HashObjectList.List.List^[Index].StrIndex;
  1764. end;
  1765. constructor TFPHashObject.CreateNotOwned;
  1766. begin
  1767. FStrIndex:=-1;
  1768. end;
  1769. constructor TFPHashObject.Create(HashObjectList:TFPHashObjectList;const s:TSymStr);
  1770. begin
  1771. InternalChangeOwner(HashObjectList,s);
  1772. end;
  1773. procedure TFPHashObject.ChangeOwner(HashObjectList:TFPHashObjectList);
  1774. begin
  1775. InternalChangeOwner(HashObjectList,PSymStr(@FOwner.List.Strs[FStrIndex])^);
  1776. end;
  1777. procedure TFPHashObject.ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:TSymStr);
  1778. begin
  1779. InternalChangeOwner(HashObjectList,s);
  1780. end;
  1781. procedure TFPHashObject.Rename(const ANewName:TSymStr);
  1782. var
  1783. Index : integer;
  1784. begin
  1785. Index:=FOwner.Rename(PSymStr(@FOwner.List.Strs[FStrIndex])^,ANewName);
  1786. if Index<>-1 then
  1787. FStrIndex:=FOwner.List.List^[Index].StrIndex;
  1788. end;
  1789. function TFPHashObject.GetName:TSymStr;
  1790. begin
  1791. if FOwner<>nil then
  1792. Result:=PSymStr(@FOwner.List.Strs[FStrIndex])^
  1793. else
  1794. Result:='';
  1795. end;
  1796. function TFPHashObject.GetHash:Longword;
  1797. begin
  1798. if FOwner<>nil then
  1799. Result:=FPHash(PSymStr(@FOwner.List.Strs[FStrIndex])^)
  1800. else
  1801. Result:=$ffffffff;
  1802. end;
  1803. {****************************************************************************
  1804. TLinkedListItem
  1805. ****************************************************************************}
  1806. constructor TLinkedListItem.Create;
  1807. begin
  1808. Previous:=nil;
  1809. Next:=nil;
  1810. end;
  1811. destructor TLinkedListItem.Destroy;
  1812. begin
  1813. end;
  1814. function TLinkedListItem.GetCopy:TLinkedListItem;
  1815. var
  1816. p : TLinkedListItem;
  1817. l : integer;
  1818. begin
  1819. p:=TLinkedListItemClass(ClassType).Create;
  1820. l:=InstanceSize;
  1821. Move(pointer(self)^,pointer(p)^,l);
  1822. Result:=p;
  1823. end;
  1824. {****************************************************************************
  1825. TLinkedList
  1826. ****************************************************************************}
  1827. constructor TLinkedList.Create;
  1828. begin
  1829. FFirst:=nil;
  1830. Flast:=nil;
  1831. FCount:=0;
  1832. FNoClear:=False;
  1833. end;
  1834. destructor TLinkedList.destroy;
  1835. begin
  1836. if not FNoClear then
  1837. Clear;
  1838. end;
  1839. function TLinkedList.empty:boolean;
  1840. begin
  1841. Empty:=(FFirst=nil);
  1842. end;
  1843. procedure TLinkedList.Insert(Item:TLinkedListItem);
  1844. begin
  1845. if FFirst=nil then
  1846. begin
  1847. FLast:=Item;
  1848. Item.Previous:=nil;
  1849. Item.Next:=nil;
  1850. end
  1851. else
  1852. begin
  1853. FFirst.Previous:=Item;
  1854. Item.Previous:=nil;
  1855. Item.Next:=FFirst;
  1856. end;
  1857. FFirst:=Item;
  1858. inc(FCount);
  1859. end;
  1860. procedure TLinkedList.InsertBefore(Item,Loc : TLinkedListItem);
  1861. begin
  1862. Item.Previous:=Loc.Previous;
  1863. Item.Next:=Loc;
  1864. Loc.Previous:=Item;
  1865. if assigned(Item.Previous) then
  1866. Item.Previous.Next:=Item
  1867. else
  1868. { if we've no next item, we've to adjust FFist }
  1869. FFirst:=Item;
  1870. inc(FCount);
  1871. end;
  1872. procedure TLinkedList.InsertAfter(Item,Loc : TLinkedListItem);
  1873. begin
  1874. Item.Next:=Loc.Next;
  1875. Loc.Next:=Item;
  1876. Item.Previous:=Loc;
  1877. if assigned(Item.Next) then
  1878. Item.Next.Previous:=Item
  1879. else
  1880. { if we've no next item, we've to adjust FLast }
  1881. FLast:=Item;
  1882. inc(FCount);
  1883. end;
  1884. procedure TLinkedList.Concat(Item:TLinkedListItem);
  1885. begin
  1886. if FFirst=nil then
  1887. begin
  1888. FFirst:=Item;
  1889. Item.Previous:=nil;
  1890. Item.Next:=nil;
  1891. end
  1892. else
  1893. begin
  1894. Flast.Next:=Item;
  1895. Item.Previous:=Flast;
  1896. Item.Next:=nil;
  1897. end;
  1898. Flast:=Item;
  1899. inc(FCount);
  1900. end;
  1901. procedure TLinkedList.remove(Item:TLinkedListItem);
  1902. begin
  1903. if Item=nil then
  1904. exit;
  1905. if (FFirst=Item) and (Flast=Item) then
  1906. begin
  1907. FFirst:=nil;
  1908. Flast:=nil;
  1909. end
  1910. else if FFirst=Item then
  1911. begin
  1912. FFirst:=Item.Next;
  1913. if assigned(FFirst) then
  1914. FFirst.Previous:=nil;
  1915. end
  1916. else if Flast=Item then
  1917. begin
  1918. Flast:=Flast.Previous;
  1919. if assigned(Flast) then
  1920. Flast.Next:=nil;
  1921. end
  1922. else
  1923. begin
  1924. Item.Previous.Next:=Item.Next;
  1925. Item.Next.Previous:=Item.Previous;
  1926. end;
  1927. Item.Next:=nil;
  1928. Item.Previous:=nil;
  1929. dec(FCount);
  1930. end;
  1931. procedure TLinkedList.clear;
  1932. var
  1933. NewNode, Next : TLinkedListItem;
  1934. begin
  1935. NewNode:=FFirst;
  1936. while assigned(NewNode) do
  1937. begin
  1938. Next:=NewNode.Next;
  1939. prefetch(pointer(Next)^);
  1940. NewNode.Free;
  1941. NewNode:=Next;
  1942. end;
  1943. FLast:=nil;
  1944. FFirst:=nil;
  1945. FCount:=0;
  1946. end;
  1947. function TLinkedList.GetFirst:TLinkedListItem;
  1948. begin
  1949. if FFirst=nil then
  1950. GetFirst:=nil
  1951. else
  1952. begin
  1953. GetFirst:=FFirst;
  1954. if FFirst=FLast then
  1955. FLast:=nil;
  1956. FFirst:=FFirst.Next;
  1957. dec(FCount);
  1958. end;
  1959. end;
  1960. function TLinkedList.GetLast:TLinkedListItem;
  1961. begin
  1962. if FLast=nil then
  1963. Getlast:=nil
  1964. else
  1965. begin
  1966. Getlast:=FLast;
  1967. if FLast=FFirst then
  1968. FFirst:=nil;
  1969. FLast:=FLast.Previous;
  1970. dec(FCount);
  1971. end;
  1972. end;
  1973. procedure TLinkedList.insertList(p : TLinkedList);
  1974. begin
  1975. { empty List ? }
  1976. if (p.FFirst=nil) then
  1977. exit;
  1978. p.Flast.Next:=FFirst;
  1979. { we have a double Linked List }
  1980. if assigned(FFirst) then
  1981. FFirst.Previous:=p.Flast;
  1982. FFirst:=p.FFirst;
  1983. if (FLast=nil) then
  1984. Flast:=p.Flast;
  1985. inc(FCount,p.FCount);
  1986. { p becomes empty }
  1987. p.FFirst:=nil;
  1988. p.Flast:=nil;
  1989. p.FCount:=0;
  1990. end;
  1991. procedure TLinkedList.insertListBefore(Item:TLinkedListItem;p : TLinkedList);
  1992. begin
  1993. { empty List ? }
  1994. if (p.FFirst=nil) then
  1995. exit;
  1996. if (Item=nil) then
  1997. begin
  1998. { Insert at begin }
  1999. InsertList(p);
  2000. exit;
  2001. end
  2002. else
  2003. begin
  2004. p.FLast.Next:=Item;
  2005. p.FFirst.Previous:=Item.Previous;
  2006. if assigned(Item.Previous) then
  2007. Item.Previous.Next:=p.FFirst
  2008. else
  2009. FFirst:=p.FFirst;
  2010. Item.Previous:=p.FLast;
  2011. inc(FCount,p.FCount);
  2012. end;
  2013. { p becomes empty }
  2014. p.FFirst:=nil;
  2015. p.Flast:=nil;
  2016. p.FCount:=0;
  2017. end;
  2018. procedure TLinkedList.insertListAfter(Item:TLinkedListItem;p : TLinkedList);
  2019. begin
  2020. { empty List ? }
  2021. if (p.FFirst=nil) then
  2022. exit;
  2023. if (Item=nil) then
  2024. begin
  2025. { Insert at begin }
  2026. InsertList(p);
  2027. exit;
  2028. end
  2029. else
  2030. begin
  2031. p.FFirst.Previous:=Item;
  2032. p.FLast.Next:=Item.Next;
  2033. if assigned(Item.Next) then
  2034. Item.Next.Previous:=p.FLast
  2035. else
  2036. FLast:=p.FLast;
  2037. Item.Next:=p.FFirst;
  2038. inc(FCount,p.FCount);
  2039. end;
  2040. { p becomes empty }
  2041. p.FFirst:=nil;
  2042. p.Flast:=nil;
  2043. p.FCount:=0;
  2044. end;
  2045. procedure TLinkedList.concatList(p : TLinkedList);
  2046. begin
  2047. if (p.FFirst=nil) then
  2048. exit;
  2049. if FFirst=nil then
  2050. FFirst:=p.FFirst
  2051. else
  2052. begin
  2053. FLast.Next:=p.FFirst;
  2054. p.FFirst.Previous:=Flast;
  2055. end;
  2056. Flast:=p.Flast;
  2057. inc(FCount,p.FCount);
  2058. { make p empty }
  2059. p.Flast:=nil;
  2060. p.FFirst:=nil;
  2061. p.FCount:=0;
  2062. end;
  2063. procedure TLinkedList.insertListcopy(p : TLinkedList);
  2064. var
  2065. NewNode,NewNode2 : TLinkedListItem;
  2066. begin
  2067. NewNode:=p.Last;
  2068. while assigned(NewNode) do
  2069. begin
  2070. NewNode2:=NewNode.Getcopy;
  2071. if assigned(NewNode2) then
  2072. Insert(NewNode2);
  2073. NewNode:=NewNode.Previous;
  2074. end;
  2075. end;
  2076. procedure TLinkedList.concatListcopy(p : TLinkedList);
  2077. var
  2078. NewNode,NewNode2 : TLinkedListItem;
  2079. begin
  2080. NewNode:=p.First;
  2081. while assigned(NewNode) do
  2082. begin
  2083. NewNode2:=NewNode.Getcopy;
  2084. if assigned(NewNode2) then
  2085. Concat(NewNode2);
  2086. NewNode:=NewNode.Next;
  2087. end;
  2088. end;
  2089. procedure TLinkedList.RemoveAll;
  2090. begin
  2091. FFirst:=nil;
  2092. FLast:=nil;
  2093. FCount:=0;
  2094. end;
  2095. {****************************************************************************
  2096. TCmdStrListItem
  2097. ****************************************************************************}
  2098. constructor TCmdStrListItem.Create(const s:TCmdStr);
  2099. begin
  2100. inherited Create;
  2101. FPStr:=s;
  2102. end;
  2103. destructor TCmdStrListItem.Destroy;
  2104. begin
  2105. FPStr:='';
  2106. end;
  2107. function TCmdStrListItem.GetCopy:TLinkedListItem;
  2108. begin
  2109. Result:=(inherited GetCopy);
  2110. { TLinkedListItem.GetCopy performs a "move" to copy all data -> reinit
  2111. the ansistring, so the refcount is properly increased }
  2112. Initialize(TCmdStrListItem(Result).FPStr);
  2113. TCmdStrListItem(Result).FPStr:=FPstr;
  2114. end;
  2115. {****************************************************************************
  2116. TCmdStrList
  2117. ****************************************************************************}
  2118. constructor TCmdStrList.Create;
  2119. begin
  2120. inherited Create;
  2121. FDoubles:=true;
  2122. end;
  2123. constructor TCmdStrList.Create_no_double;
  2124. begin
  2125. inherited Create;
  2126. FDoubles:=false;
  2127. end;
  2128. procedure TCmdStrList.insert(const s : TCmdStr);
  2129. begin
  2130. if (s='') or
  2131. ((not FDoubles) and (findcase(s)<>nil)) then
  2132. exit;
  2133. inherited insert(TCmdStrListItem.create(s));
  2134. end;
  2135. procedure TCmdStrList.concat(const s : TCmdStr);
  2136. begin
  2137. if (s='') or
  2138. ((not FDoubles) and (findcase(s)<>nil)) then
  2139. exit;
  2140. inherited concat(TCmdStrListItem.create(s));
  2141. end;
  2142. procedure TCmdStrList.remove(const s : TCmdStr);
  2143. var
  2144. p : TCmdStrListItem;
  2145. begin
  2146. if s='' then
  2147. exit;
  2148. p:=findcase(s);
  2149. if assigned(p) then
  2150. begin
  2151. inherited Remove(p);
  2152. p.Free;
  2153. end;
  2154. end;
  2155. function TCmdStrList.GetFirst : TCmdStr;
  2156. var
  2157. p : TCmdStrListItem;
  2158. begin
  2159. p:=TCmdStrListItem(inherited GetFirst);
  2160. if p=nil then
  2161. GetFirst:=''
  2162. else
  2163. begin
  2164. GetFirst:=p.FPStr;
  2165. p.free;
  2166. end;
  2167. end;
  2168. function TCmdStrList.Getlast : TCmdStr;
  2169. var
  2170. p : TCmdStrListItem;
  2171. begin
  2172. p:=TCmdStrListItem(inherited Getlast);
  2173. if p=nil then
  2174. Getlast:=''
  2175. else
  2176. begin
  2177. Getlast:=p.FPStr;
  2178. p.free;
  2179. end;
  2180. end;
  2181. function TCmdStrList.FindCase(const s:TCmdStr):TCmdStrListItem;
  2182. var
  2183. NewNode : TCmdStrListItem;
  2184. begin
  2185. result:=nil;
  2186. if s='' then
  2187. exit;
  2188. NewNode:=TCmdStrListItem(FFirst);
  2189. while assigned(NewNode) do
  2190. begin
  2191. if NewNode.FPStr=s then
  2192. begin
  2193. result:=NewNode;
  2194. exit;
  2195. end;
  2196. NewNode:=TCmdStrListItem(NewNode.Next);
  2197. end;
  2198. end;
  2199. function TCmdStrList.Find(const s:TCmdStr):TCmdStrListItem;
  2200. var
  2201. NewNode : TCmdStrListItem;
  2202. begin
  2203. result:=nil;
  2204. if s='' then
  2205. exit;
  2206. NewNode:=TCmdStrListItem(FFirst);
  2207. while assigned(NewNode) do
  2208. begin
  2209. if SysUtils.CompareText(s, NewNode.FPStr)=0 then
  2210. begin
  2211. result:=NewNode;
  2212. exit;
  2213. end;
  2214. NewNode:=TCmdStrListItem(NewNode.Next);
  2215. end;
  2216. end;
  2217. procedure TCmdStrList.InsertItem(item:TCmdStrListItem);
  2218. begin
  2219. inherited Insert(item);
  2220. end;
  2221. procedure TCmdStrList.ConcatItem(item:TCmdStrListItem);
  2222. begin
  2223. inherited Concat(item);
  2224. end;
  2225. {****************************************************************************
  2226. tdynamicarray
  2227. ****************************************************************************}
  2228. constructor tdynamicarray.create(Ablocksize:longword);
  2229. begin
  2230. FPosn:=0;
  2231. FPosnblock:=nil;
  2232. FFirstblock:=nil;
  2233. FLastblock:=nil;
  2234. FCurrBlockSize:=0;
  2235. { Every block needs at least a header and alignment slack,
  2236. therefore its size cannot be arbitrarily small. However,
  2237. the blocksize argument is often confused with data size.
  2238. See e.g. Mantis #20929. }
  2239. if Ablocksize<mindynamicblocksize then
  2240. Ablocksize:=mindynamicblocksize;
  2241. FMaxBlockSize:=Ablocksize;
  2242. grow;
  2243. end;
  2244. destructor tdynamicarray.destroy;
  2245. var
  2246. hp : pdynamicblock;
  2247. begin
  2248. while assigned(FFirstblock) do
  2249. begin
  2250. hp:=FFirstblock;
  2251. FFirstblock:=FFirstblock^.Next;
  2252. Freemem(hp);
  2253. end;
  2254. end;
  2255. function tdynamicarray.size:longword;
  2256. begin
  2257. if assigned(FLastblock) then
  2258. size:=FLastblock^.pos+FLastblock^.used
  2259. else
  2260. size:=0;
  2261. end;
  2262. procedure tdynamicarray.reset;
  2263. var
  2264. hp : pdynamicblock;
  2265. begin
  2266. while assigned(FFirstblock) do
  2267. begin
  2268. hp:=FFirstblock;
  2269. FFirstblock:=FFirstblock^.Next;
  2270. Freemem(hp);
  2271. end;
  2272. FPosn:=0;
  2273. FPosnblock:=nil;
  2274. FFirstblock:=nil;
  2275. FLastblock:=nil;
  2276. grow;
  2277. end;
  2278. procedure tdynamicarray.grow;
  2279. var
  2280. nblock : pdynamicblock;
  2281. OptBlockSize,
  2282. IncSize : integer;
  2283. begin
  2284. if CurrBlockSize<FMaxBlocksize then
  2285. begin
  2286. IncSize := mindynamicblocksize;
  2287. if FCurrBlockSize > 255 then
  2288. Inc(IncSize, FCurrBlockSize shr 2);
  2289. inc(FCurrBlockSize,IncSize);
  2290. end;
  2291. if CurrBlockSize>FMaxBlocksize then
  2292. FCurrBlockSize:=FMaxBlocksize;
  2293. { Calculate the most optimal size so there is no alignment overhead
  2294. lost in the heap manager }
  2295. OptBlockSize:=cutils.Align(CurrBlockSize+dynamicblockbasesize,16)-dynamicblockbasesize-sizeof(ptrint);
  2296. Getmem(nblock,OptBlockSize+dynamicblockbasesize);
  2297. if not assigned(FFirstblock) then
  2298. begin
  2299. FFirstblock:=nblock;
  2300. FPosnblock:=nblock;
  2301. nblock^.pos:=0;
  2302. end
  2303. else
  2304. begin
  2305. FLastblock^.Next:=nblock;
  2306. nblock^.pos:=FLastblock^.pos+FLastblock^.size;
  2307. end;
  2308. nblock^.used:=0;
  2309. nblock^.size:=OptBlockSize;
  2310. nblock^.Next:=nil;
  2311. fillchar(nblock^.data,nblock^.size,0);
  2312. FLastblock:=nblock;
  2313. end;
  2314. procedure tdynamicarray.align(i:longword);
  2315. var
  2316. j : longword;
  2317. begin
  2318. j:=(FPosn mod i);
  2319. if j<>0 then
  2320. begin
  2321. j:=i-j;
  2322. if FPosnblock^.used+j>FPosnblock^.size then
  2323. begin
  2324. dec(j,FPosnblock^.size-FPosnblock^.used);
  2325. FPosnblock^.used:=FPosnblock^.size;
  2326. grow;
  2327. FPosnblock:=FLastblock;
  2328. end;
  2329. inc(FPosnblock^.used,j);
  2330. inc(FPosn,j);
  2331. end;
  2332. end;
  2333. procedure tdynamicarray.seek(i:longword);
  2334. begin
  2335. if (i<FPosnblock^.pos) or (i>=FPosnblock^.pos+FPosnblock^.size) then
  2336. begin
  2337. { set FPosnblock correct if the size is bigger then
  2338. the current block }
  2339. if FPosnblock^.pos>i then
  2340. FPosnblock:=FFirstblock;
  2341. while assigned(FPosnblock) do
  2342. begin
  2343. if FPosnblock^.pos+FPosnblock^.size>i then
  2344. break;
  2345. FPosnblock:=FPosnblock^.Next;
  2346. end;
  2347. { not found ? then increase blocks }
  2348. if not assigned(FPosnblock) then
  2349. begin
  2350. repeat
  2351. { the current FLastblock is now also fully used }
  2352. FLastblock^.used:=FLastblock^.size;
  2353. grow;
  2354. FPosnblock:=FLastblock;
  2355. until FPosnblock^.pos+FPosnblock^.size>=i;
  2356. end;
  2357. end;
  2358. FPosn:=i;
  2359. if FPosn-FPosnblock^.pos>FPosnblock^.used then
  2360. FPosnblock^.used:=FPosn-FPosnblock^.pos;
  2361. end;
  2362. procedure tdynamicarray.write(const d;len:longword);
  2363. var
  2364. p : pchar;
  2365. i,j : longword;
  2366. begin
  2367. p:=pchar(@d);
  2368. while (len>0) do
  2369. begin
  2370. i:=FPosn-FPosnblock^.pos;
  2371. if i+len>=FPosnblock^.size then
  2372. begin
  2373. j:=FPosnblock^.size-i;
  2374. move(p^,FPosnblock^.data[i],j);
  2375. inc(p,j);
  2376. inc(FPosn,j);
  2377. dec(len,j);
  2378. FPosnblock^.used:=FPosnblock^.size;
  2379. if assigned(FPosnblock^.Next) then
  2380. FPosnblock:=FPosnblock^.Next
  2381. else
  2382. begin
  2383. grow;
  2384. FPosnblock:=FLastblock;
  2385. end;
  2386. end
  2387. else
  2388. begin
  2389. move(p^,FPosnblock^.data[i],len);
  2390. inc(p,len);
  2391. inc(FPosn,len);
  2392. i:=FPosn-FPosnblock^.pos;
  2393. if i>FPosnblock^.used then
  2394. FPosnblock^.used:=i;
  2395. len:=0;
  2396. end;
  2397. end;
  2398. end;
  2399. procedure tdynamicarray.writestr(const s:string);
  2400. begin
  2401. write(s[1],length(s));
  2402. end;
  2403. function tdynamicarray.read(var d;len:longword):longword;
  2404. var
  2405. p : pchar;
  2406. i,j,res : longword;
  2407. begin
  2408. res:=0;
  2409. p:=pchar(@d);
  2410. while (len>0) do
  2411. begin
  2412. i:=FPosn-FPosnblock^.pos;
  2413. if i+len>=FPosnblock^.used then
  2414. begin
  2415. j:=FPosnblock^.used-i;
  2416. move(FPosnblock^.data[i],p^,j);
  2417. inc(p,j);
  2418. inc(FPosn,j);
  2419. inc(res,j);
  2420. dec(len,j);
  2421. if assigned(FPosnblock^.Next) then
  2422. FPosnblock:=FPosnblock^.Next
  2423. else
  2424. break;
  2425. end
  2426. else
  2427. begin
  2428. move(FPosnblock^.data[i],p^,len);
  2429. inc(p,len);
  2430. inc(FPosn,len);
  2431. inc(res,len);
  2432. len:=0;
  2433. end;
  2434. end;
  2435. read:=res;
  2436. end;
  2437. procedure tdynamicarray.readstream(f:TCStream;maxlen:longword);
  2438. var
  2439. i,left : longword;
  2440. begin
  2441. repeat
  2442. left:=FPosnblock^.size-FPosnblock^.used;
  2443. if left>maxlen then
  2444. left:=maxlen;
  2445. i:=f.Read(FPosnblock^.data[FPosnblock^.used],left);
  2446. dec(maxlen,i);
  2447. inc(FPosnblock^.used,i);
  2448. if FPosnblock^.used=FPosnblock^.size then
  2449. begin
  2450. if assigned(FPosnblock^.Next) then
  2451. FPosnblock:=FPosnblock^.Next
  2452. else
  2453. begin
  2454. grow;
  2455. FPosnblock:=FLastblock;
  2456. end;
  2457. end;
  2458. until (i<left) or (maxlen=0);
  2459. end;
  2460. procedure tdynamicarray.writestream(f:TCStream);
  2461. var
  2462. hp : pdynamicblock;
  2463. begin
  2464. hp:=FFirstblock;
  2465. while assigned(hp) do
  2466. begin
  2467. f.Write(hp^.data,hp^.used);
  2468. hp:=hp^.Next;
  2469. end;
  2470. end;
  2471. function tdynamicarray.equal(other:tdynamicarray):boolean;
  2472. var
  2473. ofsthis,
  2474. ofsother,
  2475. remthis,
  2476. remother,
  2477. len : sizeint;
  2478. blockthis,
  2479. blockother : pdynamicblock;
  2480. begin
  2481. if not assigned(other) then
  2482. exit(false);
  2483. if size<>other.size then
  2484. exit(false);
  2485. blockthis:=Firstblock;
  2486. blockother:=other.FirstBlock;
  2487. ofsthis:=0;
  2488. ofsother:=0;
  2489. while assigned(blockthis) and assigned(blockother) do
  2490. begin
  2491. remthis:=blockthis^.used-ofsthis;
  2492. remother:=blockother^.used-ofsother;
  2493. len:=min(remthis,remother);
  2494. if not CompareMem(@blockthis^.data[ofsthis],@blockother^.data[ofsother],len) then
  2495. exit(false);
  2496. inc(ofsthis,len);
  2497. inc(ofsother,len);
  2498. if ofsthis=blockthis^.used then
  2499. begin
  2500. blockthis:=blockthis^.next;
  2501. ofsthis:=0;
  2502. end;
  2503. if ofsother=blockother^.used then
  2504. begin
  2505. blockother:=blockother^.next;
  2506. ofsother:=0;
  2507. end;
  2508. end;
  2509. if assigned(blockthis) and not assigned(blockother) then
  2510. result:=blockthis^.used=0
  2511. else if assigned(blockother) and not assigned(blockthis) then
  2512. result:=blockother^.used=0
  2513. else
  2514. result:=true;
  2515. end;
  2516. {****************************************************************************
  2517. thashset
  2518. ****************************************************************************}
  2519. constructor THashSet.Create(InitSize: Integer; OwnKeys, OwnObjects: Boolean);
  2520. var
  2521. I: Integer;
  2522. begin
  2523. inherited Create;
  2524. FOwnsObjects := OwnObjects;
  2525. FOwnsKeys := OwnKeys;
  2526. I := 64;
  2527. while I < InitSize do I := I shl 1;
  2528. FBucketCount := I;
  2529. FBucket := AllocMem(I * sizeof(PHashSetItem));
  2530. end;
  2531. destructor THashSet.Destroy;
  2532. begin
  2533. Clear;
  2534. FreeMem(FBucket);
  2535. inherited Destroy;
  2536. end;
  2537. procedure THashSet.Clear;
  2538. var
  2539. I: Integer;
  2540. item, next: PHashSetItem;
  2541. begin
  2542. for I := 0 to FBucketCount-1 do
  2543. begin
  2544. item := FBucket[I];
  2545. while Assigned(item) do
  2546. begin
  2547. next := item^.Next;
  2548. if FOwnsObjects then
  2549. item^.Data.Free;
  2550. if FOwnsKeys then
  2551. FreeMem(item^.Key);
  2552. FreeItem(item);
  2553. item := next;
  2554. end;
  2555. end;
  2556. FillChar(FBucket^, FBucketCount * sizeof(PHashSetItem), 0);
  2557. end;
  2558. function THashSet.Find(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2559. var
  2560. Dummy: Boolean;
  2561. begin
  2562. Result := Lookup(Key, KeyLen, Dummy, False);
  2563. end;
  2564. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer;
  2565. var Found: Boolean): PHashSetItem;
  2566. begin
  2567. Result := Lookup(Key, KeyLen, Found, True);
  2568. end;
  2569. function THashSet.FindOrAdd(Key: Pointer; KeyLen: Integer): PHashSetItem;
  2570. var
  2571. Dummy: Boolean;
  2572. begin
  2573. Result := Lookup(Key, KeyLen, Dummy, True);
  2574. end;
  2575. function THashSet.Get(Key: Pointer; KeyLen: Integer): TObject;
  2576. var
  2577. e: PHashSetItem;
  2578. Dummy: Boolean;
  2579. begin
  2580. e := Lookup(Key, KeyLen, Dummy, False);
  2581. if Assigned(e) then
  2582. Result := e^.Data
  2583. else
  2584. Result := nil;
  2585. end;
  2586. function THashSet.Lookup(Key: Pointer; KeyLen: Integer;
  2587. var Found: Boolean; CanCreate: Boolean): PHashSetItem;
  2588. var
  2589. Entry: PPHashSetItem;
  2590. h: LongWord;
  2591. begin
  2592. h := FPHash(Key, KeyLen);
  2593. Entry := @FBucket[h and (FBucketCount-1)];
  2594. while Assigned(Entry^) and
  2595. not ((Entry^^.HashValue = h) and (Entry^^.KeyLength = KeyLen) and
  2596. (CompareByte(Entry^^.Key^, Key^, KeyLen) = 0)) do
  2597. Entry := @Entry^^.Next;
  2598. Found := Assigned(Entry^);
  2599. if Found or (not CanCreate) then
  2600. begin
  2601. Result := Entry^;
  2602. Exit;
  2603. end;
  2604. if FCount > FBucketCount then { arbitrary limit, probably too high }
  2605. begin
  2606. { rehash and repeat search }
  2607. Resize(FBucketCount * 2);
  2608. Result := Lookup(Key, KeyLen, Found, CanCreate);
  2609. end
  2610. else
  2611. begin
  2612. GetMem(Result,SizeOfItem);
  2613. if FOwnsKeys then
  2614. begin
  2615. GetMem(Result^.Key, KeyLen);
  2616. Move(Key^, Result^.Key^, KeyLen);
  2617. end
  2618. else
  2619. Result^.Key := Key;
  2620. Result^.KeyLength := KeyLen;
  2621. Result^.HashValue := h;
  2622. Result^.Data := nil;
  2623. Result^.Next := nil;
  2624. Inc(FCount);
  2625. Entry^ := Result;
  2626. end;
  2627. end;
  2628. procedure THashSet.Resize(NewCapacity: LongWord);
  2629. var
  2630. p, chain: PPHashSetItem;
  2631. i: Integer;
  2632. e, n: PHashSetItem;
  2633. begin
  2634. p := AllocMem(NewCapacity * SizeOf(PHashSetItem));
  2635. for i := 0 to FBucketCount-1 do
  2636. begin
  2637. e := FBucket[i];
  2638. while Assigned(e) do
  2639. begin
  2640. chain := @p[e^.HashValue and (NewCapacity-1)];
  2641. n := e^.Next;
  2642. e^.Next := chain^;
  2643. chain^ := e;
  2644. e := n;
  2645. end;
  2646. end;
  2647. FBucketCount := NewCapacity;
  2648. FreeMem(FBucket);
  2649. FBucket := p;
  2650. end;
  2651. class procedure THashSet.FreeItem(item: PHashSetItem);
  2652. begin
  2653. Dispose(item);
  2654. end;
  2655. class function THashSet.SizeOfItem: Integer;
  2656. begin
  2657. Result := SizeOf(THashSetItem);
  2658. end;
  2659. function THashSet.Remove(Entry: PHashSetItem): Boolean;
  2660. var
  2661. chain: PPHashSetItem;
  2662. begin
  2663. chain := @FBucket[Entry^.HashValue mod FBucketCount];
  2664. while Assigned(chain^) do
  2665. begin
  2666. if chain^ = Entry then
  2667. begin
  2668. chain^ := Entry^.Next;
  2669. if FOwnsObjects then
  2670. Entry^.Data.Free;
  2671. if FOwnsKeys then
  2672. FreeMem(Entry^.Key);
  2673. FreeItem(Entry);
  2674. Dec(FCount);
  2675. Result := True;
  2676. Exit;
  2677. end;
  2678. chain := @chain^^.Next;
  2679. end;
  2680. Result := False;
  2681. end;
  2682. {****************************************************************************
  2683. ttaghashset
  2684. ****************************************************************************}
  2685. function TTagHashSet.Lookup(Key: Pointer; KeyLen: Integer;
  2686. Tag: LongWord; var Found: Boolean; CanCreate: Boolean): PTagHashSetItem;
  2687. var
  2688. Entry: PPTagHashSetItem;
  2689. h: LongWord;
  2690. begin
  2691. h := FPHash(Key, KeyLen, Tag);
  2692. Entry := @PPTagHashSetItem(FBucket)[h and (FBucketCount-1)];
  2693. while Assigned(Entry^) and
  2694. not ((Entry^^.HashValue = h) and (Entry^^.KeyLength = KeyLen) and
  2695. (Entry^^.Tag = Tag) and (CompareByte(Entry^^.Key^, Key^, KeyLen) = 0)) do
  2696. Entry := @Entry^^.Next;
  2697. Found := Assigned(Entry^);
  2698. if Found or (not CanCreate) then
  2699. begin
  2700. Result := Entry^;
  2701. Exit;
  2702. end;
  2703. if FCount > FBucketCount then { arbitrary limit, probably too high }
  2704. begin
  2705. { rehash and repeat search }
  2706. Resize(FBucketCount * 2);
  2707. Result := Lookup(Key, KeyLen, Tag, Found, CanCreate);
  2708. end
  2709. else
  2710. begin
  2711. Getmem(Result,SizeOfItem);
  2712. if FOwnsKeys then
  2713. begin
  2714. GetMem(Result^.Key, KeyLen);
  2715. Move(Key^, Result^.Key^, KeyLen);
  2716. end
  2717. else
  2718. Result^.Key := Key;
  2719. Result^.KeyLength := KeyLen;
  2720. Result^.HashValue := h;
  2721. Result^.Tag := Tag;
  2722. Result^.Data := nil;
  2723. Result^.Next := nil;
  2724. Inc(FCount);
  2725. Entry^ := Result;
  2726. end;
  2727. end;
  2728. class procedure TTagHashSet.FreeItem(item: PHashSetItem);
  2729. begin
  2730. Dispose(PTagHashSetItem(item));
  2731. end;
  2732. class function TTagHashSet.SizeOfItem: Integer;
  2733. begin
  2734. Result := SizeOf(TTagHashSetItem);
  2735. end;
  2736. function TTagHashSet.Find(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem;
  2737. var
  2738. Dummy: Boolean;
  2739. begin
  2740. Result := Lookup(Key, KeyLen, Tag, Dummy, False);
  2741. end;
  2742. function TTagHashSet.FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord;
  2743. var Found: Boolean): PTagHashSetItem;
  2744. begin
  2745. Result := Lookup(Key, KeyLen, Tag, Found, True);
  2746. end;
  2747. function TTagHashSet.FindOrAdd(Key: Pointer; KeyLen: Integer; Tag: LongWord): PTagHashSetItem;
  2748. var
  2749. Dummy: Boolean;
  2750. begin
  2751. Result := Lookup(Key, KeyLen, Tag, Dummy, True);
  2752. end;
  2753. function TTagHashSet.Get(Key: Pointer; KeyLen: Integer; Tag: LongWord): TObject;
  2754. var
  2755. e: PTagHashSetItem;
  2756. Dummy: Boolean;
  2757. begin
  2758. e := Lookup(Key, KeyLen, Tag, Dummy, False);
  2759. if Assigned(e) then
  2760. Result := e^.Data
  2761. else
  2762. Result := nil;
  2763. end;
  2764. {****************************************************************************
  2765. tbitset
  2766. ****************************************************************************}
  2767. constructor tbitset.create(initsize: longint);
  2768. begin
  2769. create_bytesize((initsize+7) div 8);
  2770. end;
  2771. constructor tbitset.create_bytesize(bytesize: longint);
  2772. begin
  2773. fdatasize:=bytesize;
  2774. getmem(fdata,fdataSize);
  2775. clear;
  2776. end;
  2777. destructor tbitset.destroy;
  2778. begin
  2779. freemem(fdata,fdatasize);
  2780. inherited destroy;
  2781. end;
  2782. procedure tbitset.clear;
  2783. begin
  2784. fillchar(fdata^,fdatasize,0);
  2785. end;
  2786. procedure tbitset.grow(nsize: longint);
  2787. begin
  2788. reallocmem(fdata,nsize);
  2789. fillchar(fdata[fdatasize],nsize-fdatasize,0);
  2790. fdatasize:=nsize;
  2791. end;
  2792. procedure tbitset.include(index: longint);
  2793. var
  2794. dataindex: longint;
  2795. begin
  2796. { don't use bitpacked array, not endian-safe }
  2797. dataindex:=index shr 3;
  2798. if (dataindex>=datasize) then
  2799. grow(dataindex+16);
  2800. fdata[dataindex]:=fdata[dataindex] or (1 shl (index and 7));
  2801. end;
  2802. procedure tbitset.exclude(index: longint);
  2803. var
  2804. dataindex: longint;
  2805. begin
  2806. dataindex:=index shr 3;
  2807. if (dataindex>=datasize) then
  2808. exit;
  2809. fdata[dataindex]:=fdata[dataindex] and not(1 shl (index and 7));
  2810. end;
  2811. function tbitset.isset(index: longint): boolean;
  2812. var
  2813. dataindex: longint;
  2814. begin
  2815. dataindex:=index shr 3;
  2816. result:=
  2817. (dataindex<datasize) and
  2818. (((fdata[dataindex] shr (index and 7)) and 1)<>0);
  2819. end;
  2820. procedure tbitset.addset(aset: tbitset);
  2821. var
  2822. i: longint;
  2823. begin
  2824. if (aset.datasize>datasize) then
  2825. grow(aset.datasize);
  2826. for i:=0 to aset.datasize-1 do
  2827. fdata[i]:=fdata[i] or aset.data[i];
  2828. end;
  2829. procedure tbitset.subset(aset: tbitset);
  2830. var
  2831. i: longint;
  2832. begin
  2833. for i:=0 to min(datasize,aset.datasize)-1 do
  2834. fdata[i]:=fdata[i] and not(aset.data[i]);
  2835. end;
  2836. end.