cclasses.pas 83 KB

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