cclasses.pas 66 KB

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