cclasses.pas 79 KB

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