cclasses.pas 82 KB

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