cclasses.pas 78 KB

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