cclasses.pas 64 KB

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