cclasses.pas 79 KB

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