cclasses.pas 66 KB

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