cclasses.pas 63 KB

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