cclasses.pas 71 KB

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