cclasses.pas 66 KB

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