cclasses.pas 65 KB

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