cclasses.pas 71 KB

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