cclasses.pas 74 KB

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