cclasses.pas 79 KB

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