cclasses.pas 74 KB

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