cclasses.pas 66 KB

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