cclasses.pas 65 KB

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