cclasses.pas 73 KB

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